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