1 /*
2 * Copyright (c) 2025 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_certificate_operation.h"
17
18 #include "cm_napi_dialog_common.h"
19 #include "cm_log.h"
20 #include "napi/native_api.h"
21 #include "napi/native_node_api.h"
22
23 namespace CMNapi {
CmOperationUIExtensionCallback(std::shared_ptr<CmUIExtensionRequestContext> & reqContext)24 CmOperationUIExtensionCallback::CmOperationUIExtensionCallback(
25 std::shared_ptr<CmUIExtensionRequestContext>& reqContext) : CmUIExtensionCallback(reqContext)
26 {
27 this->reqContext_ = reqContext;
28 }
29
~CmOperationUIExtensionCallback()30 CmOperationUIExtensionCallback::~CmOperationUIExtensionCallback()
31 {
32 CM_LOG_D("~CmOperationUIExtensionCallback");
33 }
34
OnRelease(const int32_t releaseCode)35 void CmOperationUIExtensionCallback::OnRelease(const int32_t releaseCode)
36 {
37 CM_LOG_D("OperationUIExtensionComponent OnRelease(), releaseCode = %d", releaseCode);
38 if (SetErrorCode(CMR_DIALOG_ERROR_OPERATION_CANCELS)) {
39 SendMessageBack();
40 }
41 }
42
OnResult(const int32_t resultCode,const OHOS::AAFwk::Want & result)43 void CmOperationUIExtensionCallback::OnResult(const int32_t resultCode, const OHOS::AAFwk::Want& result)
44 {
45 CM_LOG_D("OperationUIExtensionComponent OnResult(), resultCode = %d", resultCode);
46 this->resultCode_ = resultCode;
47 this->resultWant_ = result;
48 if (SetErrorCode(this->resultCode_)) {
49 SendMessageBack();
50 }
51 }
52
OnReceive(const OHOS::AAFwk::WantParams & request)53 void CmOperationUIExtensionCallback::OnReceive(const OHOS::AAFwk::WantParams& request)
54 {
55 CM_LOG_D("OperationUIExtensionComponent OnReceive()");
56 this->reqContext_->uri = request.GetStringParam("uri");
57 if (SetErrorCode(0)) {
58 SendMessageBack();
59 }
60 }
61
ProcessCallback(napi_env env,const CommonAsyncContext * asyncContext)62 void CmOperationUIExtensionCallback::ProcessCallback(napi_env env, const CommonAsyncContext* asyncContext)
63 {
64 napi_value args = nullptr;
65 if (asyncContext->errCode == CM_SUCCESS) {
66 if (asyncContext->opType == DIALOG_OPERATION_INSTALL || asyncContext->opType == DIALOG_OPERATION_AUTHORIZE) {
67 NAPI_CALL_RETURN_VOID(env,
68 napi_create_string_utf8(env, asyncContext->uri.c_str(), NAPI_AUTO_LENGTH, &args));
69 } else {
70 NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &args));
71 }
72 } else {
73 args = GenerateBusinessError(env, asyncContext->errCode);
74 }
75
76 if (asyncContext->deferred != nullptr) {
77 if (asyncContext->errCode == CM_SUCCESS) {
78 NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, asyncContext->deferred, args));
79 } else {
80 NAPI_CALL_RETURN_VOID(env, napi_reject_deferred(env, asyncContext->deferred, args));
81 }
82 }
83 }
84
OnDestroy()85 void CmOperationUIExtensionCallback::OnDestroy()
86 {
87 CM_LOG_D("OperationUIExtensionComponent OnDestroy()");
88 }
89
90
SetErrorCode(int32_t errCode)91 bool CmOperationUIExtensionCallback::SetErrorCode(int32_t errCode)
92 {
93 if (this->reqContext_ == nullptr) {
94 CM_LOG_E("OnError reqContext is nullptr");
95 return false;
96 }
97 if (this->alreadyCallback_) {
98 CM_LOG_D("alreadyCallback");
99 return false;
100 }
101 this->alreadyCallback_ = true;
102 this->reqContext_->errCode = errCode;
103 return true;
104 }
105 } // namespace CMNapi
106