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
16 #include "process_frozen_state_observer.h"
17
18 #include "ability_manager_service.h"
19 #include "hilog/log.h"
20 #include "suspend_manager_client.h"
21
22 namespace OHOS {
23 namespace AAFwk {
24 namespace {
25 constexpr int32_t MAX_FAIL_COUNT = 10;
26 constexpr int32_t FAIL_RETRY_INTERVAL = 5000; // ms
27
28 class SuspendMgrDeathRecipient : public IRemoteObject::DeathRecipient {
29 public:
AddSuspendMgrDeathRecipient()30 static void AddSuspendMgrDeathRecipient()
31 {
32 auto systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
33 if (!systemManager) {
34 HILOG_ERROR("Failed to get SystemAbilityManager.");
35 return;
36 }
37 auto remoteObj = systemManager->GetSystemAbility(SUSPEND_MANAGER_SYSTEM_ABILITY_ID);
38 if (!remoteObj) {
39 HILOG_ERROR("Failed to get SuspendManager sa.");
40 return;
41 }
42 if (!remoteObj->IsProxyObject()) {
43 HILOG_ERROR("Not proxy object.");
44 return;
45 }
46 if (!remoteObj->AddDeathRecipient(sptr(new SuspendMgrDeathRecipient))) {
47 HILOG_ERROR("Add death recipient for SuspendManager failed.");
48 return;
49 }
50 }
51
OnRemoteDied(const wptr<IRemoteObject> &)52 void OnRemoteDied(const wptr<IRemoteObject>&)
53 {
54 auto taskHandler = DelayedSingleton<AbilityManagerService>::GetInstance()->GetTaskHandler();
55 if (!taskHandler) {
56 HILOG_ERROR("taskHandler null.");
57 return;
58 }
59 taskHandler->SubmitTask([taskHandler]() {
60 ProcessFrozenStateObserver::RegisterSuspendObserver(taskHandler);
61 }, FAIL_RETRY_INTERVAL);
62 }
63 };
64 }
65 int ProcessFrozenStateObserver::g_registerCount = 0;
RegisterSuspendObserver(std::shared_ptr<TaskHandlerWrap> taskHandler)66 void ProcessFrozenStateObserver::RegisterSuspendObserver(std::shared_ptr<TaskHandlerWrap> taskHandler)
67 {
68 if (!taskHandler) {
69 HILOG_ERROR("taskHandler null");
70 return;
71 }
72 auto ret = SuspendManager::SuspendManagerClient::GetInstance().RegisterSuspendObserver(
73 sptr(new ProcessFrozenStateObserver()));
74 if (ret != ERR_OK) {
75 HILOG_ERROR("failed err: %{public}d", ret);
76 g_registerCount++;
77 if (g_registerCount < MAX_FAIL_COUNT) {
78 taskHandler->SubmitTask([taskHandler]() {
79 RegisterSuspendObserver(taskHandler);
80 }, FAIL_RETRY_INTERVAL);
81 } else {
82 HILOG_ERROR("failed 10 times");
83 g_registerCount = 0;
84 }
85 } else {
86 g_registerCount = 0;
87 HILOG_INFO("RegisterSuspendObserver success");
88 SuspendMgrDeathRecipient::AddSuspendMgrDeathRecipient();
89 }
90 }
91
OnActive(const std::vector<int32_t> & pidList,const int32_t uid)92 void ProcessFrozenStateObserver::OnActive(const std::vector<int32_t> &pidList, const int32_t uid)
93 {
94 HILOG_DEBUG("OnActive: %{public}d", uid);
95 }
96
OnDoze(const int32_t uid)97 void ProcessFrozenStateObserver::OnDoze(const int32_t uid)
98 {
99 HILOG_DEBUG("OnDoze: %{public}d", uid);
100 }
101
OnFrozen(const std::vector<int32_t> & pidList,const int32_t uid)102 void ProcessFrozenStateObserver::OnFrozen(const std::vector<int32_t> &pidList, const int32_t uid)
103 {
104 HILOG_DEBUG("OnFrozen: %{public}d", uid);
105 if (pidList.empty()) {
106 HILOG_WARN("OnFrozen pidList empty");
107 return;
108 }
109 DelayedSingleton<AbilityManagerService>::GetInstance()->HandleProcessFrozen(pidList, uid);
110 }
111 } // namespace AAFwk
112 } // namespace OHOS