1 /*
2  * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "js_auto_fill_extension_context.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #include "js_error_utils.h"
20 #include "js_extension_context.h"
21 #include "napi/native_api.h"
22 #include "napi_common_want.h"
23 #include "napi_common_util.h"
24 
25 namespace OHOS {
26 namespace AbilityRuntime {
27 namespace {
28 constexpr int32_t INDEX_ZERO = 0;
29 constexpr size_t ARGC_ONE = 1;
30 }
Finalizer(napi_env env,void * data,void * hint)31 void JsAutoFillExtensionContext::Finalizer(napi_env env, void *data, void *hint)
32 {
33     TAG_LOGD(AAFwkTag::AUTOFILL_EXT, "called");
34     std::unique_ptr<JsAutoFillExtensionContext>(static_cast<JsAutoFillExtensionContext*>(data));
35 }
36 
ReloadInModal(napi_env env,napi_callback_info info)37 napi_value JsAutoFillExtensionContext::ReloadInModal(napi_env env, napi_callback_info info)
38 {
39     GET_NAPI_INFO_AND_CALL(env, info, JsAutoFillExtensionContext, OnReloadInModal);
40 }
41 
OnReloadInModal(napi_env env,NapiCallbackInfo & info)42 napi_value JsAutoFillExtensionContext::OnReloadInModal(napi_env env, NapiCallbackInfo &info)
43 {
44     TAG_LOGD(AAFwkTag::AUTOFILL_EXT, "called");
45     if (info.argc < ARGC_ONE) {
46         TAG_LOGE(AAFwkTag::AUTOFILL_EXT, "invalid argc");
47         ThrowError(env, AbilityErrorCode::ERROR_CODE_INVALID_PARAM);
48         return CreateJsUndefined(env);
49     }
50 
51     napi_value jsCustomData = GetPropertyValueByPropertyName(env, info.argv[INDEX_ZERO], "data", napi_object);
52     CustomData customData;
53     if (jsCustomData == nullptr || !AppExecFwk::UnwrapWantParams(env, jsCustomData, customData.data)) {
54         TAG_LOGE(AAFwkTag::AUTOFILL_EXT, "Parse custom data failed");
55         ThrowError(env, AbilityErrorCode::ERROR_CODE_INVALID_PARAM);
56         return CreateJsUndefined(env);
57     }
58 
59     auto retVal = std::make_shared<int32_t>(0);
60     NapiAsyncTask::ExecuteCallback execute = [weak = context_, customData, ret = retVal, env]() {
61         auto context = weak.lock();
62         if (context == nullptr) {
63             TAG_LOGE(AAFwkTag::AUTOFILL_EXT, "null context");
64             ThrowError(env, AbilityErrorCode::ERROR_CODE_INVALID_CONTEXT);
65             return;
66         }
67         if (ret == nullptr) {
68             TAG_LOGE(AAFwkTag::AUTOFILL_EXT, "invalid param");
69             return;
70         }
71         *ret = context->ReloadInModal(customData);
72     };
73 
74     NapiAsyncTask::CompleteCallback complete = [ret = retVal](napi_env env, NapiAsyncTask &task, int32_t status) {
75         if (ret == nullptr) {
76             TAG_LOGE(AAFwkTag::AUTOFILL_EXT, "invalid param");
77             task.Reject(env, CreateJsError(env, AbilityErrorCode::ERROR_CODE_INNER));
78             return;
79         }
80         if (*ret != ERR_OK) {
81             TAG_LOGE(AAFwkTag::AUTOFILL_EXT, "Failed error %{public}d", *ret);
82             task.Reject(env, CreateJsError(env, GetJsErrorCodeByNativeError(*ret)));
83             return;
84         }
85         task.ResolveWithNoError(env, CreateJsUndefined(env));
86     };
87     napi_value result = nullptr;
88     NapiAsyncTask::ScheduleHighQos("JsAutoFillExtensionContext::OnReloadInModal",
89         env, CreateAsyncTaskWithLastParam(env, nullptr, std::move(execute), std::move(complete), &result));
90     return result;
91 }
92 
CreateJsAutoFillExtensionContext(napi_env env,const std::shared_ptr<AutoFillExtensionContext> & context)93 napi_value JsAutoFillExtensionContext::CreateJsAutoFillExtensionContext(
94     napi_env env, const std::shared_ptr<AutoFillExtensionContext> &context)
95 {
96     TAG_LOGD(AAFwkTag::AUTOFILL_EXT, "called");
97     std::shared_ptr<OHOS::AppExecFwk::AbilityInfo> abilityInfo = nullptr;
98     if (context != nullptr) {
99         abilityInfo = context->GetAbilityInfo();
100     }
101     napi_value objValue = CreateJsExtensionContext(env, context, abilityInfo);
102 
103     auto jsContext = std::make_unique<JsAutoFillExtensionContext>(context);
104     napi_wrap(env, objValue, jsContext.release(), Finalizer, nullptr, nullptr);
105 
106     const char *moduleName = "JsAutoFillExtensionContext";
107     BindNativeFunction(env, objValue, "reloadInModal", moduleName, ReloadInModal);
108 
109     return objValue;
110 }
111 } // namespace AbilityRuntime
112 } // namespace OHOS
113