• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_save_request_callback.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #include "js_auto_fill_manager.h"
20 #include "js_runtime.h"
21 #include "js_runtime_utils.h"
22 
23 namespace OHOS {
24 namespace AbilityRuntime {
25 namespace {
26 const std::string METHOD_ON_SAVE_REQUEST_SUCCESS = "onSuccess";
27 const std::string METHOD_ON_SAVE_REQUEST_FAILED = "onFailure";
28 } // namespace
JsAutoSaveRequestCallback(napi_env env,int32_t instanceId,AutoFillManagerFunc autoFillManagerFunc)29 JsAutoSaveRequestCallback::JsAutoSaveRequestCallback(
30     napi_env env, int32_t instanceId, AutoFillManagerFunc autoFillManagerFunc)
31     : env_(env), instanceId_(instanceId), autoFillManagerFunc_(autoFillManagerFunc) {}
32 
~JsAutoSaveRequestCallback()33 JsAutoSaveRequestCallback::~JsAutoSaveRequestCallback() {}
34 
OnSaveRequestSuccess()35 void JsAutoSaveRequestCallback::OnSaveRequestSuccess()
36 {
37     TAG_LOGD(AAFwkTag::AUTOFILLMGR, "called");
38     JSCallFunction(METHOD_ON_SAVE_REQUEST_SUCCESS);
39     if (autoFillManagerFunc_ != nullptr) {
40         autoFillManagerFunc_(instanceId_);
41     }
42 }
43 
OnSaveRequestFailed()44 void JsAutoSaveRequestCallback::OnSaveRequestFailed()
45 {
46     TAG_LOGD(AAFwkTag::AUTOFILLMGR, "called");
47     JSCallFunction(METHOD_ON_SAVE_REQUEST_FAILED);
48     if (autoFillManagerFunc_ != nullptr) {
49         autoFillManagerFunc_(instanceId_);
50     }
51 }
52 
Register(napi_value value)53 void JsAutoSaveRequestCallback::Register(napi_value value)
54 {
55     TAG_LOGD(AAFwkTag::AUTOFILLMGR, "called");
56     if (IsJsCallbackEquals(callback_, value)) {
57         TAG_LOGE(AAFwkTag::AUTOFILLMGR, "callback exist");
58         return;
59     }
60 
61     napi_ref ref = nullptr;
62     napi_create_reference(env_, value, 1, &ref);
63     callback_ = std::unique_ptr<NativeReference>(reinterpret_cast<NativeReference *>(ref));
64 }
65 
JSCallFunction(const std::string & methodName)66 void JsAutoSaveRequestCallback::JSCallFunction(const std::string &methodName)
67 {
68     auto thisPtr = shared_from_this();
69     NapiAsyncTask::CompleteCallback complete =
70         [thisPtr, methodName](napi_env env, NapiAsyncTask &task, int32_t status) {
71             if (thisPtr) {
72                 thisPtr->JSCallFunctionWorker(methodName);
73             }
74         };
75 
76     NapiAsyncTask::Schedule("JsAutoSaveRequestCallback::JSCallFunction:" + methodName,
77         env_,
78         CreateAsyncTaskWithLastParam(env_, nullptr, nullptr, std::move(complete), nullptr));
79 }
80 
JSCallFunctionWorker(const std::string & methodName)81 void JsAutoSaveRequestCallback::JSCallFunctionWorker(const std::string &methodName)
82 {
83     if (callback_ == nullptr) {
84         TAG_LOGE(AAFwkTag::AUTOFILLMGR, "null callback_");
85         return;
86     }
87 
88     auto obj = callback_->GetNapiValue();
89     if (obj == nullptr) {
90         TAG_LOGE(AAFwkTag::AUTOFILLMGR, "null obj");
91         return;
92     }
93 
94     napi_value funcObject;
95     if (napi_get_named_property(env_, obj, methodName.c_str(), &funcObject) != napi_ok) {
96         TAG_LOGE(AAFwkTag::AUTOFILLMGR, "Get function failed");
97         return;
98     }
99 
100     napi_call_function(env_, obj, funcObject, 0, NULL, nullptr);
101 }
102 
IsJsCallbackEquals(std::shared_ptr<NativeReference> callback,napi_value value)103 bool JsAutoSaveRequestCallback::IsJsCallbackEquals(std::shared_ptr<NativeReference> callback, napi_value value)
104 {
105     if (callback == nullptr) {
106         TAG_LOGE(AAFwkTag::AUTOFILLMGR, "Invalid jsCallback");
107         return false;
108     }
109 
110     auto object = callback->GetNapiValue();
111     if (object == nullptr) {
112         TAG_LOGE(AAFwkTag::AUTOFILLMGR, "null obj");
113         return false;
114     }
115 
116     bool result = false;
117     if (napi_strict_equals(env_, object, value, &result) != napi_ok) {
118         TAG_LOGE(AAFwkTag::AUTOFILLMGR, "Object not match");
119         return false;
120     }
121 
122     return result;
123 }
124 } // namespace AbilityRuntime
125 } // namespace OHOS