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 #ifdef APP_NO_RESPONSE_DIALOG
17 #include "modal_system_app_freeze_uiextension.h"
18 
19 #include <mutex>
20 
21 #include "hilog_tag_wrapper.h"
22 #include "hitrace_meter.h"
23 #include "in_process_call_wrapper.h"
24 #include "scene_board_judgement.h"
25 
26 namespace OHOS {
27 namespace AppExecFwk {
28 const std::string UIEXTENSION_TYPE_KEY = "ability.want.params.uiExtensionType";
29 const std::string UIEXTENSION_SYS_COMMON_UI = "sysDialog/common";
30 const std::string APP_FREEZE_PID = "APP_FREEZE_PID";
31 const std::string START_BUNDLE_NAME = "startBundleName";
32 constexpr int32_t INVALID_USERID = -1;
33 constexpr int32_t MESSAGE_PARCEL_KEY_SIZE = 3;
34 constexpr uint32_t COMMAND_START_DIALOG = 1;
35 
GetInstance()36 ModalSystemAppFreezeUIExtension &ModalSystemAppFreezeUIExtension::GetInstance()
37 {
38     static ModalSystemAppFreezeUIExtension instance;
39     return instance;
40 }
41 
~ModalSystemAppFreezeUIExtension()42 ModalSystemAppFreezeUIExtension::~ModalSystemAppFreezeUIExtension()
43 {
44     dialogConnectionCallback_ = nullptr;
45 }
46 
GetConnection()47 sptr<ModalSystemAppFreezeUIExtension::AppFreezeDialogConnection> ModalSystemAppFreezeUIExtension::GetConnection()
48 {
49     if (dialogConnectionCallback_ == nullptr) {
50         std::lock_guard lock(dialogConnectionMutex_);
51         if (dialogConnectionCallback_ == nullptr) {
52             dialogConnectionCallback_ = new (std::nothrow) AppFreezeDialogConnection();
53         }
54     }
55 
56     return dialogConnectionCallback_;
57 }
58 
ProcessAppFreeze(bool focusFlag,const FaultData & faultData,std::string pid,std::string bundleName,std::function<void ()> callback,bool isDialogExist)59 void ModalSystemAppFreezeUIExtension::ProcessAppFreeze(bool focusFlag, const FaultData &faultData, std::string pid,
60     std::string bundleName, std::function<void()> callback, bool isDialogExist)
61 {
62     const std::string SCENE_BAOARD_NAME = "com.ohos.sceneboard";
63     if (bundleName == SCENE_BAOARD_NAME && callback) {
64         callback();
65         return;
66     }
67     FaultDataType faultType = faultData.faultType;
68     std::string name = faultData.errorObject.name;
69     bool isAppFreezeDialog = name == AppFreezeType::THREAD_BLOCK_6S || name == AppFreezeType::APP_INPUT_BLOCK ||
70         name == AppFreezeType::LIFECYCLE_TIMEOUT;
71     isAppFreezeDialog = isAppFreezeDialog && (!isDialogExist || (isDialogExist && pid != lastFreezePid));
72     TAG_LOGI(AAFwkTag::ABILITYMGR, "%{public}s is %{public}s", bundleName.c_str(), focusFlag ? " focus" : " not focus");
73     if (focusFlag && isAppFreezeDialog) {
74         CreateModalUIExtension(pid, bundleName);
75     } else if (callback && faultType != FaultDataType::APP_FREEZE) {
76         callback();
77     }
78 }
79 
CreateModalUIExtension(std::string pid,std::string bundleName)80 bool ModalSystemAppFreezeUIExtension::CreateModalUIExtension(std::string pid, std::string bundleName)
81 {
82     TAG_LOGD(AAFwkTag::ABILITYMGR, "called");
83     AAFwk::Want want = CreateSystemDialogWant(pid, bundleName);
84     std::unique_lock<std::mutex> lockAssertResult(appFreezeResultMutex_);
85     auto callback = GetConnection();
86     if (callback == nullptr) {
87         TAG_LOGE(AAFwkTag::ABILITYMGR, "CreateModalUIExtension Callback is nullptr.");
88         return false;
89     }
90     callback->SetReqeustAppFreezeDialogWant(want);
91     auto abilityManagerClient = AAFwk::AbilityManagerClient::GetInstance();
92     if (abilityManagerClient == nullptr) {
93         TAG_LOGE(AAFwkTag::ABILITYMGR, "CreateModalUIExtension ConnectSystemUi AbilityManagerClient is nullptr");
94         return false;
95     }
96     AAFwk::Want systemUIWant;
97     if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
98         systemUIWant.SetElementName("com.ohos.sceneboard", "com.ohos.sceneboard.systemdialog");
99     } else {
100         systemUIWant.SetElementName("com.ohos.systemui", "com.ohos.systemui.dialog");
101     }
102     IN_PROCESS_CALL_WITHOUT_RET(abilityManagerClient->DisconnectAbility(callback));
103     auto result = IN_PROCESS_CALL(abilityManagerClient->ConnectAbility(systemUIWant, callback, INVALID_USERID));
104     if (result != ERR_OK) {
105         TAG_LOGE(AAFwkTag::ABILITYMGR,
106             "CreateModalUIExtension ConnectSystemUi ConnectAbility dialog failed, result = %{public}d", result);
107         return false;
108     }
109     lastFreezePid = pid;
110     TAG_LOGI(AAFwkTag::ABILITYMGR,
111         "CreateModalUIExtension ConnectSystemUi ConnectAbility dialog success, result = %{public}d", result);
112     return true;
113 }
114 
CreateSystemDialogWant(std::string pid,std::string bundleName)115 AAFwk::Want ModalSystemAppFreezeUIExtension::CreateSystemDialogWant(std::string pid, std::string bundleName)
116 {
117     AAFwk::Want want;
118     want.SetElementName(APP_NO_RESPONSE_BUNDLENAME, APP_NO_RESPONSE_ABILITY);
119     want.SetParam(UIEXTENSION_TYPE_KEY, UIEXTENSION_SYS_COMMON_UI);
120     want.SetParam(APP_FREEZE_PID, pid);
121     want.SetParam(START_BUNDLE_NAME, bundleName);
122     return want;
123 }
124 
SetReqeustAppFreezeDialogWant(const AAFwk::Want & want)125 void ModalSystemAppFreezeUIExtension::AppFreezeDialogConnection::SetReqeustAppFreezeDialogWant(const AAFwk::Want &want)
126 {
127     want_ = want;
128 }
129 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remote,int resultCode)130 void ModalSystemAppFreezeUIExtension::AppFreezeDialogConnection::OnAbilityConnectDone(
131     const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remote, int resultCode)
132 {
133     TAG_LOGD(AAFwkTag::ABILITYMGR, "called");
134     if (remote == nullptr) {
135         TAG_LOGE(AAFwkTag::ABILITYMGR, "Input remote object is nullptr.");
136         return;
137     }
138 
139     MessageParcel data;
140     MessageParcel reply;
141     MessageOption option;
142     data.WriteInt32(MESSAGE_PARCEL_KEY_SIZE);
143     data.WriteString16(u"bundleName");
144     data.WriteString16(Str8ToStr16(want_.GetElement().GetBundleName()));
145     data.WriteString16(u"abilityName");
146     data.WriteString16(Str8ToStr16(want_.GetElement().GetAbilityName()));
147     data.WriteString16(u"parameters");
148     nlohmann::json param;
149     param[UIEXTENSION_TYPE_KEY.c_str()] = want_.GetStringParam(UIEXTENSION_TYPE_KEY);
150     param[APP_FREEZE_PID.c_str()] = want_.GetStringParam(APP_FREEZE_PID);
151     param[START_BUNDLE_NAME.c_str()] = want_.GetStringParam(START_BUNDLE_NAME);
152     std::string paramStr = param.dump();
153     data.WriteString16(Str8ToStr16(paramStr));
154     uint32_t code = !Rosen::SceneBoardJudgement::IsSceneBoardEnabled() ?
155         COMMAND_START_DIALOG :
156         AAFwk::IAbilityConnection::ON_ABILITY_CONNECT_DONE;
157     TAG_LOGI(AAFwkTag::ABILITYMGR, "AppFreezeDialogConnection::OnAbilityConnectDone Show dialog");
158     auto ret = remote->SendRequest(code, data, reply, option);
159     if (ret != ERR_OK) {
160         TAG_LOGE(AAFwkTag::ABILITYMGR, "Show dialog is failed");
161         return;
162     }
163 }
164 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)165 void ModalSystemAppFreezeUIExtension::AppFreezeDialogConnection::OnAbilityDisconnectDone(
166     const AppExecFwk::ElementName &element, int resultCode)
167 {
168     TAG_LOGI(AAFwkTag::ABILITYMGR, "called");
169 }
170 } // namespace AppExecFwk
171 } // namespace OHOS
172 #endif // APP_NO_RESPONSE_DIALOG
173