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 "modal_extension_callback.h"
17
18 #include "ability.h"
19 #include "system_ability_definition.h"
20 #include "ui_holder_extension_context.h"
21
22 #include "iam_logger.h"
23 #include "iam_ptr.h"
24
25 #include "user_auth_napi_client_impl.h"
26
27 #define LOG_TAG "USER_AUTH_SDK"
28
29 namespace OHOS {
30 namespace UserIam {
31 namespace UserAuth {
ModalExtensionCallback()32 ModalExtensionCallback::ModalExtensionCallback()
33 {}
34
~ModalExtensionCallback()35 ModalExtensionCallback::~ModalExtensionCallback()
36 {}
37
OnResult(int32_t code,const AAFwk::Want & result)38 void ModalExtensionCallback::OnResult(int32_t code, const AAFwk::Want& result)
39 {
40 IAM_LOGI("OnResult, code: %{public}d", code);
41 }
42
OnReceive(const AAFwk::WantParams & receive)43 void ModalExtensionCallback::OnReceive(const AAFwk::WantParams& receive)
44 {
45 IAM_LOGI("OnReceive");
46 }
47
OnRelease(int32_t code)48 void ModalExtensionCallback::OnRelease(int32_t code)
49 {
50 IAM_LOGI("OnRelease, code: %{public}d", code);
51 std::lock_guard<std::recursive_mutex> lock(mutex_);
52 if (code != 0) {
53 CancelAuthentication();
54 }
55 ReleaseOrErrorHandle(code);
56 }
57
OnError(int32_t code,const std::string & name,const std::string & message)58 void ModalExtensionCallback::OnError(int32_t code, const std::string& name, const std::string& message)
59 {
60 IAM_LOGE("OnError, name: %{public}s, message: %{public}s", name.c_str(), message.c_str());
61 std::lock_guard<std::recursive_mutex> lock(mutex_);
62 CancelAuthentication();
63 ReleaseOrErrorHandle(code);
64 }
65
OnRemoteReady(const std::shared_ptr<Ace::ModalUIExtensionProxy> & uiProxy)66 void ModalExtensionCallback::OnRemoteReady(const std::shared_ptr<Ace::ModalUIExtensionProxy>& uiProxy)
67 {
68 IAM_LOGI("OnRemoteReady");
69 }
70
OnDestroy()71 void ModalExtensionCallback::OnDestroy()
72 {
73 IAM_LOGI("OnDestroy");
74 std::lock_guard<std::recursive_mutex> lock(mutex_);
75 isDestroy_ = true;
76 }
77
SetSessionId(int32_t sessionId)78 void ModalExtensionCallback::SetSessionId(int32_t sessionId)
79 {
80 std::lock_guard<std::recursive_mutex> lock(mutex_);
81 sessionId_ = sessionId;
82 }
83
SetContextId(uint64_t contextId)84 void ModalExtensionCallback::SetContextId(uint64_t contextId)
85 {
86 std::lock_guard<std::recursive_mutex> lock(mutex_);
87 contextId_ = contextId;
88 }
89
SetAbilityContext(std::shared_ptr<OHOS::AbilityRuntime::AbilityContext> abilityContext)90 void ModalExtensionCallback::SetAbilityContext(std::shared_ptr<OHOS::AbilityRuntime::AbilityContext> abilityContext)
91 {
92 std::lock_guard<std::recursive_mutex> lock(mutex_);
93 abilityContext_ = abilityContext;
94 }
95
SetHolderContext(std::shared_ptr<OHOS::AbilityRuntime::UIHolderExtensionContext> uiHolderContext)96 void ModalExtensionCallback::SetHolderContext(
97 std::shared_ptr<OHOS::AbilityRuntime::UIHolderExtensionContext> uiHolderContext)
98 {
99 std::lock_guard<std::recursive_mutex> lock(mutex_);
100 uiHolderContext_ = uiHolderContext;
101 }
102
ReleaseOrErrorHandle(int32_t code)103 void ModalExtensionCallback::ReleaseOrErrorHandle(int32_t code)
104 {
105 IAM_LOGI("ReleaseOrErrorHandle start, code: %{public}d", code);
106 std::lock_guard<std::recursive_mutex> lock(mutex_);
107 if (abilityContext_ != nullptr) {
108 Ace::UIContent* uiContent = abilityContext_->GetUIContent();
109 if (uiContent == nullptr) {
110 IAM_LOGE("uiContent is null");
111 return;
112 }
113 uiContent->CloseModalUIExtension(sessionId_);
114 }
115 if (uiHolderContext_ != nullptr) {
116 Ace::UIContent* uiContent = uiHolderContext_->GetUIContent();
117 if (uiContent == nullptr) {
118 IAM_LOGE("uiContent is null");
119 return;
120 }
121 uiContent->CloseModalUIExtension(sessionId_);
122 }
123 IAM_LOGI("ReleaseOrErrorHandle end");
124 isDestroy_ = true;
125 return;
126 }
127
IsModalDestroy()128 bool ModalExtensionCallback::IsModalDestroy()
129 {
130 std::lock_guard<std::recursive_mutex> lock(mutex_);
131 return isDestroy_;
132 }
133
CancelAuthentication()134 void ModalExtensionCallback::CancelAuthentication()
135 {
136 // cancel for failed
137 int32_t code = UserAuthNapiClientImpl::Instance().CancelAuthentication(contextId_, CancelReason::MODAL_RUN_ERROR);
138 IAM_LOGI("CancelAuthentication, code: %{public}d", code);
139 }
140 } // namespace UserAuth
141 } // namespace UserIam
142 } // namespace OHOS