• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_dialog_request_callback.h"
17 
18 #include "hilog_wrapper.h"
19 #include "js_context_utils.h"
20 #include "js_error_utils.h"
21 #include "js_runtime.h"
22 #include "js_runtime_utils.h"
23 
24 namespace OHOS {
25 namespace AbilityRuntime {
26 namespace { // nameless
27 class JsDialogRequestCallback {
28 public:
JsDialogRequestCallback(const sptr<IDialogRequestCallback> remoteObj)29     explicit JsDialogRequestCallback(const sptr<IDialogRequestCallback> remoteObj) :callback_(remoteObj) {}
30 
31     virtual ~JsDialogRequestCallback() = default;
32 
Finalizer(NativeEngine * engine,void * data,void * hint)33     static void Finalizer(NativeEngine* engine, void* data, void* hint)
34     {
35         HILOG_DEBUG("JsDialogRequestCallback::Finalizer is called.");
36         std::unique_ptr<JsDialogRequestCallback>(static_cast<JsDialogRequestCallback*>(data));
37     }
38 
SetRequestResult(NativeEngine * engine,NativeCallbackInfo * info)39     static NativeValue* SetRequestResult(NativeEngine* engine, NativeCallbackInfo* info)
40     {
41         if (engine == nullptr || info == nullptr) {
42             HILOG_ERROR("input parameters %{public}s is nullptr", ((engine == nullptr) ? "engine" : "info"));
43             return nullptr;
44         }
45 
46         auto object = CheckParamsAndGetThis<JsDialogRequestCallback>(engine, info);
47         if (object == nullptr) {
48             HILOG_ERROR("CheckParamsAndGetThis return nullptr");
49             return nullptr;
50         }
51 
52         return object->OnSetRequestResult(*engine, *info);
53     }
54 
55 private:
OnSetRequestResult(NativeEngine & engine,NativeCallbackInfo & info)56     NativeValue* OnSetRequestResult(NativeEngine& engine, NativeCallbackInfo& info)
57     {
58         HILOG_INFO("function called");
59         if (info.argc < 1) {
60             HILOG_ERROR("Params not match");
61             ThrowTooFewParametersError(engine);
62             return engine.CreateUndefined();
63         }
64 
65         if (info.argv[0]->TypeOf() != NativeValueType::NATIVE_OBJECT) {
66             HILOG_ERROR("param type mismatch!");
67             ThrowError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM);
68             return engine.CreateUndefined();
69         }
70 
71         NativeObject* paramObject = ConvertNativeValueTo<NativeObject>(info.argv[0]);
72         NativeValue* resultCode = paramObject->GetProperty("result");
73         int32_t resultCodeValue = 0;
74         if (!ConvertFromJsValue(engine, resultCode, resultCodeValue)) {
75             HILOG_ERROR("Convert result failed!");
76             ThrowError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM);
77             return engine.CreateUndefined();
78         }
79 
80         if (callback_ == nullptr) {
81             HILOG_ERROR("JsDialogRequestCallback::%{public}s, callback_ is nullptr", __func__);
82             ThrowError(engine, AbilityErrorCode::ERROR_CODE_INNER);
83             return engine.CreateUndefined();
84         }
85         callback_->SendResult(resultCodeValue);
86         HILOG_INFO("function called end.");
87         return engine.CreateUndefined();
88     }
89 
90 private:
91     sptr<IDialogRequestCallback> callback_;
92 };
93 } // nameless
94 
CreateJsDialogRequestCallback(NativeEngine & engine,const sptr<IDialogRequestCallback> & remoteObj)95 NativeValue* CreateJsDialogRequestCallback(NativeEngine &engine, const sptr<IDialogRequestCallback> &remoteObj)
96 {
97     HILOG_INFO("CreateJsDialogRequestCallback");
98     if (!remoteObj) {
99         HILOG_ERROR("remoteObj is invalid.");
100         return engine.CreateUndefined();
101     }
102 
103     NativeValue* objValue = engine.CreateObject();
104     NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
105     if (object == nullptr) {
106         HILOG_ERROR("object is invalid.");
107         return engine.CreateUndefined();
108     }
109 
110     auto jsDialogRequestCallback = std::make_unique<JsDialogRequestCallback>(remoteObj);
111     object->SetNativePointer(jsDialogRequestCallback.release(), JsDialogRequestCallback::Finalizer, nullptr);
112     const char *moduleName = "JsDialogRequestCallback";
113     BindNativeFunction(engine, *object, "setRequestResult", moduleName, JsDialogRequestCallback::SetRequestResult);
114 
115     HILOG_INFO("CreateJsDialogRequestCallback end");
116     return objValue;
117 }
118 } // AbilityRuntime
119 } // OHOS
120