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(napi_env env,void * data,void * hint)34 static void Finalizer(napi_env env, void* data, void* hint)
35 {
36 HILOG_DEBUG("JsDialogRequestCallback::Finalizer is called.");
37 std::unique_ptr<JsDialogRequestCallback>(static_cast<JsDialogRequestCallback*>(data));
38 }
39
SetRequestResult(napi_env env,napi_callback_info info)40 static napi_value SetRequestResult(napi_env env, napi_callback_info info)
41 {
42 if (env == nullptr || info == nullptr) {
43 HILOG_ERROR("input parameters %{public}s is nullptr", ((env == nullptr) ? "env" : "info"));
44 return nullptr;
45 }
46
47 GET_NAPI_INFO_AND_CALL(env, info, JsDialogRequestCallback, OnSetRequestResult);
48 }
49
50 private:
OnSetRequestResult(napi_env env,NapiCallbackInfo & info)51 napi_value OnSetRequestResult(napi_env env, NapiCallbackInfo& info)
52 {
53 HILOG_INFO("function called");
54 if (info.argc < 1) {
55 HILOG_ERROR("Params not match");
56 ThrowTooFewParametersError(env);
57 return CreateJsUndefined(env);
58 }
59
60 if (!CheckTypeForNapiValue(env, info.argv[0], napi_object)) {
61 HILOG_ERROR("param type mismatch!");
62 ThrowError(env, AbilityErrorCode::ERROR_CODE_INVALID_PARAM);
63 return CreateJsUndefined(env);
64 }
65
66 napi_value resultCode = nullptr;
67 napi_get_named_property(env, info.argv[0], "result", &resultCode);
68 int32_t resultCodeValue = 0;
69 if (!ConvertFromJsValue(env, resultCode, resultCodeValue)) {
70 HILOG_ERROR("Convert result failed!");
71 ThrowError(env, AbilityErrorCode::ERROR_CODE_INVALID_PARAM);
72 return CreateJsUndefined(env);
73 }
74
75 AAFwk::Want wantValue;
76 napi_value jWant = nullptr;
77 napi_get_named_property(env, info.argv[0], "want", &jWant);
78 if (jWant != nullptr && CheckTypeForNapiValue(env, jWant, napi_object)) {
79 AppExecFwk::UnwrapWant(env, jWant, wantValue);
80 } else {
81 HILOG_WARN("jWant is invalid data!");
82 }
83
84 if (callback_ == nullptr) {
85 HILOG_ERROR("JsDialogRequestCallback::%{public}s, callback_ is nullptr", __func__);
86 ThrowError(env, AbilityErrorCode::ERROR_CODE_INNER);
87 return CreateJsUndefined(env);
88 }
89 callback_->SendResult(resultCodeValue, wantValue);
90 HILOG_INFO("function called end.");
91 return CreateJsUndefined(env);
92 }
93
94 private:
95 sptr<IDialogRequestCallback> callback_;
96 };
97 } // nameless
98
CreateJsDialogRequestCallback(napi_env env,const sptr<IDialogRequestCallback> & remoteObj)99 napi_value CreateJsDialogRequestCallback(napi_env env, const sptr<IDialogRequestCallback> &remoteObj)
100 {
101 HILOG_INFO("CreateJsDialogRequestCallback");
102 if (!remoteObj) {
103 HILOG_ERROR("remoteObj is invalid.");
104 return CreateJsUndefined(env);
105 }
106
107 napi_value objValue = nullptr;
108 napi_create_object(env, &objValue);
109 if (objValue == nullptr) {
110 HILOG_ERROR("object is invalid.");
111 return CreateJsUndefined(env);
112 }
113
114 auto jsDialogRequestCallback = std::make_unique<JsDialogRequestCallback>(remoteObj);
115 napi_wrap(env, objValue, jsDialogRequestCallback.release(), JsDialogRequestCallback::Finalizer, nullptr, nullptr);
116 const char *moduleName = "JsDialogRequestCallback";
117 BindNativeFunction(env, objValue, "setRequestResult", moduleName, JsDialogRequestCallback::SetRequestResult);
118
119 HILOG_INFO("CreateJsDialogRequestCallback end");
120 return objValue;
121 }
122 } // AbilityRuntime
123 } // OHOS
124