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 #include "ui_extension_ability_connection.h"
16 #include "ability_connect_callback_interface.h"
17 #include "ability_manager_client.h"
18 #include "iam_logger.h"
19 #include "widget_client.h"
20 
21 #define LOG_TAG "USER_AUTH_SA"
22 constexpr int32_t SIGNAL_NUM = 3;
23 
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
27 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int32_t resultCode)28 void UIExtensionAbilityConnection::OnAbilityConnectDone(const AppExecFwk::ElementName &element,
29     const sptr<IRemoteObject> &remoteObject, int32_t resultCode)
30 {
31     IAM_LOGI("on ability connected");
32     std::lock_guard<std::recursive_mutex> lock(mutex_);
33     if (!isConnectionRelease_) {
34         IAM_LOGE("connection already release");
35         return;
36     }
37     if (remoteObject == nullptr) {
38         IAM_LOGE("remoteObject is nullptr");
39         WidgetClient::Instance().ForceStopAuth();
40         return;
41     }
42     connectAbilityHitrace_ = nullptr;
43     MessageParcel data;
44     MessageParcel reply;
45     MessageOption option;
46     data.WriteInt32(SIGNAL_NUM);
47     data.WriteString16(u"bundleName");
48     data.WriteString16(u"com.ohos.useriam.authwidget");
49     data.WriteString16(u"abilityName");
50     data.WriteString16(u"userauthuiextensionability");
51     data.WriteString16(u"parameters");
52     data.WriteString16(Str8ToStr16(commandStr_));
53     extRemoteObject_ = remoteObject;
54     int32_t errCode = remoteObject->SendRequest(IAbilityConnection::ON_ABILITY_CONNECT_DONE, data, reply, option);
55     IAM_LOGI("connect ability result %{public}d", errCode);
56     if (errCode != SUCCESS) {
57         IAM_LOGE("widget schedule error, stop auth");
58         connectAbilityHitrace_ = nullptr;
59         ReleaseUIExtensionComponentInner();
60         WidgetClient::Instance().ForceStopAuth();
61     }
62 }
63 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int32_t resultCode)64 void UIExtensionAbilityConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element,
65     int32_t resultCode)
66 {
67     IAM_LOGI("on ability disconnected");
68     std::lock_guard<std::recursive_mutex> lock(mutex_);
69     connectAbilityHitrace_ = nullptr;
70     if (!isConnectionRelease_) {
71         IAM_LOGE("connection already release");
72         return;
73     }
74     WidgetClient::Instance().ForceStopAuth();
75 }
76 
ReleaseUIExtensionComponentInner()77 void UIExtensionAbilityConnection::ReleaseUIExtensionComponentInner()
78 {
79     IAM_LOGI("release UIExtensionComponent inner");
80     if (extRemoteObject_ != nullptr) {
81         MessageParcel data;
82         MessageParcel reply;
83         MessageOption option;
84         option.SetFlags(MessageOption::TF_ASYNC);
85         int32_t errCode = extRemoteObject_->SendRequest(IAbilityConnection::ON_REMOTE_STATE_CHANGED, data, reply,
86             option);
87         IAM_LOGI("release UIExtensionComponent result %{public}d", errCode);
88         extRemoteObject_ = nullptr;
89     }
90     isConnectionRelease_ = false;
91 }
92 
ReleaseUIExtensionComponent()93 void UIExtensionAbilityConnection::ReleaseUIExtensionComponent()
94 {
95     IAM_LOGI("release UIExtensionComponent");
96     std::lock_guard<std::recursive_mutex> lock(mutex_);
97     ReleaseUIExtensionComponentInner();
98 }
99 } // namespace UserAuth
100 } // namespace UserIam
101 } // namespace OHOS
102