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