1 /*
2 * Copyright (c) 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 "cm_napi_open_dialog.h"
17
18 #include "securec.h"
19
20 #include "cert_manager_api.h"
21 #include "cm_log.h"
22
23 #include "cm_napi_dialog_common.h"
24 #include "want.h"
25 #include "want_params_wrapper.h"
26
27 namespace CMNapi {
28
CommonAsyncContext(napi_env env)29 CommonAsyncContext::CommonAsyncContext(napi_env env)
30 {
31 CM_LOG_D("CommonAsyncContext");
32 this->env = env;
33 }
34
~CommonAsyncContext()35 CommonAsyncContext::~CommonAsyncContext()
36 {
37 CM_LOG_D("~CommonAsyncContext");
38 }
39
CmUIExtensionCallback(std::shared_ptr<CmUIExtensionRequestContext> & reqContext)40 CmUIExtensionCallback::CmUIExtensionCallback(std::shared_ptr<CmUIExtensionRequestContext>& reqContext)
41 {
42 this->reqContext_ = reqContext;
43 }
44
~CmUIExtensionCallback()45 CmUIExtensionCallback::~CmUIExtensionCallback()
46 {
47 CM_LOG_D("~CmUIExtensionCallback");
48 }
49
SetSessionId(const int32_t sessionId)50 void CmUIExtensionCallback::SetSessionId(const int32_t sessionId)
51 {
52 this->sessionId_ = sessionId;
53 }
54
SetErrorCode(int32_t code)55 bool CmUIExtensionCallback::SetErrorCode(int32_t code)
56 {
57 if (this->reqContext_ == nullptr) {
58 CM_LOG_E("OnError reqContext is nullptr");
59 return false;
60 }
61 if (this->alreadyCallback_) {
62 CM_LOG_D("alreadyCallback");
63 return false;
64 }
65 this->alreadyCallback_ = true;
66 this->reqContext_->errCode = code;
67 return true;
68 }
69
OnRelease(const int32_t releaseCode)70 void CmUIExtensionCallback::OnRelease(const int32_t releaseCode)
71 {
72 CM_LOG_D("UIExtensionComponent OnRelease(), releaseCode = %d", releaseCode);
73 if (SetErrorCode(releaseCode)) {
74 SendMessageBack();
75 }
76 }
77
OnResult(const int32_t resultCode,const OHOS::AAFwk::Want & result)78 void CmUIExtensionCallback::OnResult(const int32_t resultCode, const OHOS::AAFwk::Want& result)
79 {
80 CM_LOG_D("UIExtensionComponent OnResult(), resultCode = %d", resultCode);
81 this->resultCode_ = resultCode;
82 this->resultWant_ = result;
83 if (SetErrorCode(0)) {
84 SendMessageBack();
85 }
86 }
87
OnReceive(const OHOS::AAFwk::WantParams & request)88 void CmUIExtensionCallback::OnReceive(const OHOS::AAFwk::WantParams& request)
89 {
90 CM_LOG_D("UIExtensionComponent OnReceive()");
91 if (SetErrorCode(0)) {
92 SendMessageBack();
93 }
94 }
95
OnError(const int32_t errorCode,const std::string & name,const std::string & message)96 void CmUIExtensionCallback::OnError(const int32_t errorCode, const std::string& name, const std::string& message)
97 {
98 CM_LOG_E("UIExtensionComponent OnError(), errorCode = %d, name = %s, message = %s",
99 errorCode, name.c_str(), message.c_str());
100 if (SetErrorCode(errorCode)) {
101 SendMessageBack();
102 }
103 }
104
OnRemoteReady(const std::shared_ptr<OHOS::Ace::ModalUIExtensionProxy> & uiProxy)105 void CmUIExtensionCallback::OnRemoteReady(const std::shared_ptr<OHOS::Ace::ModalUIExtensionProxy>& uiProxy)
106 {
107 CM_LOG_D("UIExtensionComponent OnRemoteReady()");
108 }
109
OnDestroy()110 void CmUIExtensionCallback::OnDestroy()
111 {
112 CM_LOG_D("UIExtensionComponent OnDestroy()");
113 if (SetErrorCode(0)) {
114 SendMessageBack();
115 }
116 }
117
ProcessCallback(napi_env env,const CommonAsyncContext * asyncContext)118 void CmUIExtensionCallback::ProcessCallback(napi_env env, const CommonAsyncContext* asyncContext)
119 {
120 napi_value args[PARAM_SIZE_TWO] = {nullptr};
121
122 if (asyncContext->errCode == CM_SUCCESS) {
123 NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &args[PARAM0]));
124 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, true, &args[PARAM1]));
125 } else {
126 args[PARAM0] = GenerateBusinessError(env, asyncContext->errCode);
127 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &args[PARAM1]));
128 }
129
130 if (asyncContext->deferred != nullptr) {
131 GeneratePromise(env, asyncContext->deferred, asyncContext->errCode, args, CM_ARRAY_SIZE(args));
132 }
133 }
134
SendMessageBack()135 void CmUIExtensionCallback::SendMessageBack()
136 {
137 CM_LOG_I("start SendMessageBack");
138 if (this->reqContext_ == nullptr) {
139 CM_LOG_E("reqContext is nullptr");
140 return;
141 }
142
143 auto abilityContext = this->reqContext_->context;
144 if (abilityContext != nullptr) {
145 auto uiContent = abilityContext->GetUIContent();
146 if (uiContent != nullptr) {
147 CM_LOG_D("CloseModalUIExtension");
148 uiContent->CloseModalUIExtension(this->sessionId_);
149 }
150 }
151
152 CM_LOG_D("ProcessCallback");
153 ProcessCallback(this->reqContext_->env, this->reqContext_.get());
154 }
155
IsCmDialogPageTypeEnum(const uint32_t value)156 static bool IsCmDialogPageTypeEnum(const uint32_t value)
157 {
158 switch (static_cast<CmDialogPageType>(value)) {
159 case CmDialogPageType::PAGE_MAIN:
160 case CmDialogPageType::PAGE_CA_CERTIFICATE:
161 case CmDialogPageType::PAGE_CREDENTIAL:
162 case CmDialogPageType::PAGE_INSTALL_CERTIFICATE:
163 return true;
164 default:
165 return false;
166 }
167 }
168
CMNapiOpenCertManagerDialog(napi_env env,napi_callback_info info)169 napi_value CMNapiOpenCertManagerDialog(napi_env env, napi_callback_info info)
170 {
171 CM_LOG_I("cert manager dialog enter");
172 size_t argc = PARAM_SIZE_TWO;
173 napi_value argv[PARAM_SIZE_TWO] = { nullptr };
174 napi_value result = nullptr;
175 NAPI_CALL(env, napi_get_undefined(env, &result));
176 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
177 if (argc != PARAM_SIZE_TWO) {
178 CM_LOG_E("params number mismatch");
179 std::string errMsg = "Parameter Error. Params number mismatch, need " + std::to_string(PARAM_SIZE_TWO)
180 + ", given " + std::to_string(argc);
181 ThrowError(env, PARAM_ERROR, errMsg);
182 return result;
183 }
184
185 // Parse first argument for context.
186 auto asyncContext = std::make_shared<CmUIExtensionRequestContext>(env);
187 if (!ParseCmUIAbilityContextReq(env, argv[PARAM0], asyncContext->context)) {
188 CM_LOG_E("ParseUIAbilityContextReq failed");
189 ThrowError(env, PARAM_ERROR, "Get context failed.");
190 return result;
191 }
192
193 // Parse second argument for page type.
194 result = ParseUint32(env, argv[PARAM1], asyncContext->pageType);
195 if (result == nullptr) {
196 CM_LOG_E("parse type failed");
197 ThrowError(env, PARAM_ERROR, "parse type failed");
198 return result;
199 }
200
201 if (!IsCmDialogPageTypeEnum(asyncContext->pageType)) {
202 CM_LOG_E("pageType invalid");
203 ThrowError(env, PARAM_ERROR, "pageType invalid");
204 return nullptr;
205 }
206
207 asyncContext->env = env;
208 OHOS::AAFwk::Want want;
209 want.SetElementName(CERT_MANAGER_BUNDLENAME, CERT_MANAGER_ABILITYNAME);
210 want.SetParam(CERT_MANAGER_PAGE_TYPE, static_cast<int32_t>(asyncContext->pageType));
211 want.SetParam(PARAM_UI_EXTENSION_TYPE, SYS_COMMON_UI);
212 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
213
214 auto uiExtCallback = std::make_shared<CmUIExtensionCallback>(asyncContext);
215 // Start ui extension by context.
216 StartUIExtensionAbility(asyncContext, want, uiExtCallback);
217 CM_LOG_D("cert manager dialog end");
218 return result;
219 }
220 } // namespace CMNapi
221
222