1 /*
2 * Copyright (c) 2022-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 "mem_mgr_event_center.h"
17 #include <string>
18 #include "memmgr_log.h"
19 #include "memmgr_ptr_util.h"
20 #include "common_event_observer.h"
21 #include "reclaim_priority_manager.h"
22 #ifdef CONFIG_BGTASK_MGR
23 #include "background_task_mgr_helper.h"
24 #endif
25 #include "connection_observer_client.h"
26 #include "common_event_support.h"
27 #include "common_event_manager.h"
28
29 namespace OHOS {
30 namespace Memory {
31 namespace {
32 const std::string TAG = "MemMgrEventCenter";
33 const std::string MEMMGR_CENTER_HANDLER = "MemMgrEventCenterHandler";
34 const int ACCOUNT_MAX_RETRY_TIMES = 10;
35 const int ACCOUNT_RETRY_DELAY = 3000;
36 const int EXTCONN_RETRY_TIME = 1000;
37 }
38
39 IMPLEMENT_SINGLE_INSTANCE(MemMgrEventCenter);
40
MemMgrEventCenter()41 MemMgrEventCenter::MemMgrEventCenter()
42 {}
43
Init()44 bool MemMgrEventCenter::Init()
45 {
46 HILOGI("called");
47 if (CreateRegisterHandler()) {
48 HandlerRegisterEvent(RegisterEvent::REG_ALLOBS_EVENT);
49 return true;
50 }
51 return false;
52 }
53
CreateRegisterHandler()54 bool MemMgrEventCenter::CreateRegisterHandler()
55 {
56 if (!regObsHandler_) {
57 MAKE_POINTER(regObsHandler_, shared, AppExecFwk::EventHandler, "failed to create register handler",
58 return false, AppExecFwk::EventRunner::Create(MEMMGR_CENTER_HANDLER));
59 }
60 return true;
61 }
62
RemoveEventObserver(int32_t systemAbilityId)63 void MemMgrEventCenter::RemoveEventObserver(int32_t systemAbilityId)
64 {
65 HILOGI("called");
66
67 if (systemAbilityId == ABILITY_MGR_SERVICE_ID || systemAbilityId == APP_MGR_SERVICE_ID) {
68 appStateObserver_ = nullptr;
69 extConnObserver_ = nullptr;
70 ReclaimPriorityManager::GetInstance().Reset();
71 }
72 if (systemAbilityId == SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN) {
73 accountObserver_ = nullptr;
74 }
75
76 if (systemAbilityId == COMMON_EVENT_SERVICE_ID || systemAbilityId == COMMON_EVENT_SERVICE_ABILITY_ID) {
77 commonEventObserver_ = nullptr;
78 }
79 }
80
RegisterEventObserver()81 bool MemMgrEventCenter::RegisterEventObserver()
82 {
83 HILOGI("called");
84 if (!memoryPressureObserver_) {
85 RegisterMemoryPressureObserver();
86 }
87
88 if (!appStateObserver_) {
89 RegisterAppStateObserver();
90 }
91
92 if (!extConnObserver_) {
93 RegisterExtConnObserver();
94 }
95
96 if (!accountObserver_) {
97 RegisterAccountObserver();
98 }
99
100 if (!commonEventObserver_) {
101 RegisterCommonEventObserver();
102 }
103
104 #ifdef CONFIG_BGTASK_MGR
105 if (!bgTaskObserver_) {
106 RegisterBgTaskObserver();
107 }
108 #endif
109 return true;
110 }
111
HandlerRegisterEvent(int64_t registerEventId)112 void MemMgrEventCenter::HandlerRegisterEvent(int64_t registerEventId)
113 {
114 switch (registerEventId)
115 {
116 case RegisterEvent::REG_ALLOBS_EVENT:
117 {
118 std::function<void()> RegisterEventObserverFunc =
119 std::bind(&MemMgrEventCenter::RegisterEventObserver, this);
120 regObsHandler_->PostImmediateTask(RegisterEventObserverFunc);
121 }
122 break;
123 case RegisterEvent::REG_MEMPRESSOBS_EVENT:
124 {
125 std::function<void()> RegisterMemoryPressureObserverFunc =
126 std::bind(&MemMgrEventCenter::RegisterMemoryPressureObserver, this);
127 regObsHandler_->PostImmediateTask(RegisterMemoryPressureObserverFunc);
128 }
129 break;
130 case RegisterEvent::REG_APPOBS_EVENT:
131 {
132 std::function<void()> RegisterAppStateObserverFunc =
133 std::bind(&MemMgrEventCenter::RegisterAppStateObserver, this);
134 regObsHandler_->PostImmediateTask(RegisterAppStateObserverFunc);
135 }
136 break;
137 case RegisterEvent::REG_EXTOBS_EVENT:
138 {
139 std::function<void()> RegisterExtConnObserverFunc =
140 std::bind(&MemMgrEventCenter::RegisterExtConnObserver, this);
141 regObsHandler_->PostImmediateTask(RegisterExtConnObserverFunc);
142 }
143 break;
144 case RegisterEvent::REG_ACCOUNTOBS_EVENT:
145 {
146 std::function<void()> RegisterAccountObserverFunc =
147 std::bind(&MemMgrEventCenter::RegisterAccountObserver, this);
148 regObsHandler_->PostImmediateTask(RegisterAccountObserverFunc);
149 }
150 break;
151 case RegisterEvent::REG_COMMONOBS_EVENT:
152 {
153 std::function<void()> RegisterCommonEventObserverFunc =
154 std::bind(&MemMgrEventCenter::RegisterCommonEventObserver, this);
155 regObsHandler_->PostImmediateTask(RegisterCommonEventObserverFunc);
156 }
157 break;
158 case RegisterEvent::REG_BGTASKOBS_EVENT:
159 #ifdef CONFIG_BGTASK_MGR
160 {
161 std::function<void()> RegisterBgTaskObserverFunc =
162 std::bind(&MemMgrEventCenter::RegisterBgTaskObserver, this);
163 regObsHandler_->PostImmediateTask(RegisterBgTaskObserverFunc);
164 }
165 break;
166 #endif
167 }
168 return;
169 }
170
RegisterAppStateObserver()171 void MemMgrEventCenter::RegisterAppStateObserver()
172 {
173 HILOGI("called");
174 MAKE_POINTER(appMgrClient_, unique, AppExecFwk::AppMgrClient, "make appMgrClient failed",
175 return, /* no param */);
176 appStateObserver_ = new (std::nothrow) AppStateObserver();
177 while (appMgrClient_->ConnectAppMgrService() != AppExecFwk::AppMgrResultCode::RESULT_OK) {
178 HILOGE("ConnectAppMgrService fail, try again! retryTimes=%{public}d", ++regAppStatusObsRetry_);
179 }
180 auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
181 sptr<AppExecFwk::IAppMgr> appObject =
182 iface_cast<AppExecFwk::IAppMgr>(systemAbilityManager->GetSystemAbility(APP_MGR_SERVICE_ID));
183 if (appObject) {
184 int ret = appObject->RegisterApplicationStateObserver(appStateObserver_);
185 if (ret == ERR_OK) {
186 HILOGI("register success");
187 return;
188 }
189 HILOGE("register fail, ret = %{public}d", ret);
190 return;
191 }
192 HILOGE("get SystemAbilityManager fail");
193 }
194
RegisterExtConnObserver()195 void MemMgrEventCenter::RegisterExtConnObserver()
196 {
197 HILOGI("called");
198 MAKE_POINTER(extConnObserver_, shared, ExtensionConnectionObserver, "make ExtensionConnectionObserver failed",
199 /* no return */, /* no param */);
200 if (extConnObserver_ != nullptr) {
201 int32_t ret = AbilityRuntime::ConnectionObserverClient::GetInstance().RegisterObserver(extConnObserver_);
202 if (ret == ERR_OK) {
203 HILOGI("register success");
204 return;
205 }
206 HILOGE("register fail, ret = %{public}d", ret);
207 }
208 std::function<void()> RegisterExtConnObserverFunc = std::bind(&MemMgrEventCenter::RegisterExtConnObserver, this);
209 regObsHandler_->PostTask(RegisterExtConnObserverFunc, EXTCONN_RETRY_TIME, AppExecFwk::EventQueue::Priority::LOW);
210 }
211
RegisterBgTaskObserver()212 void MemMgrEventCenter::RegisterBgTaskObserver()
213 {
214 HILOGI("called");
215 #ifdef CONFIG_BGTASK_MGR
216 if (bgTaskObserver_) {
217 return;
218 }
219 MAKE_POINTER(bgTaskObserver_, shared, BgTaskObserver, "make BgTaskObserver failed",
220 return, /* no param */);
221 ErrCode ret = BackgroundTaskMgr::BackgroundTaskMgrHelper::SubscribeBackgroundTask(*bgTaskObserver_);
222 if (ret == ERR_OK) {
223 HILOGI("register success");
224 return;
225 }
226 HILOGE("register fail, ret = %{public}d", ret);
227 #else
228 HILOGI("BackgroundTaskMgr is not enable.");
229 #endif
230 }
231
RegisterCommonEventObserver()232 void MemMgrEventCenter::RegisterCommonEventObserver()
233 {
234 HILOGI("called");
235 EventFwk::MatchingSkills matchingSkills;
236 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED);
237 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED);
238 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED);
239 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
240 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
241 EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills);
242 MAKE_POINTER(commonEventObserver_, shared, CommonEventObserver, "make unique failed",
243 return, commonEventSubscribeInfo);
244 if (EventFwk::CommonEventManager::SubscribeCommonEvent(commonEventObserver_)) {
245 HILOGI("register success");
246 return;
247 }
248 HILOGI("register fail");
249 }
250
RegisterAccountObserver()251 void MemMgrEventCenter::RegisterAccountObserver()
252 {
253 HILOGI("called");
254 regAccountObsRetry_++;
255 AccountSA::OsAccountSubscribeInfo osAccountSubscribeInfo;
256 osAccountSubscribeInfo.SetOsAccountSubscribeType(AccountSA::OS_ACCOUNT_SUBSCRIBE_TYPE::ACTIVED);
257 osAccountSubscribeInfo.SetName("MemMgrAccountActivedSubscriber");
258 MAKE_POINTER(accountObserver_, shared, AccountObserver, "make unique failed", return, osAccountSubscribeInfo);
259 ErrCode errCode = AccountSA::OsAccountManager::SubscribeOsAccount(accountObserver_);
260 if (errCode == ERR_OK) {
261 HILOGI("register success");
262 return;
263 }
264
265 if (regAccountObsRetry_ < ACCOUNT_MAX_RETRY_TIMES) {
266 std::function<void()> RegisterAccountObserverFunc =
267 std::bind(&MemMgrEventCenter::RegisterAccountObserver, this);
268 HILOGE("register fail, retCode = %{public}d, try again after 3s!, retryTimes=%{public}d/10",
269 errCode, regAccountObsRetry_);
270 regObsHandler_->PostTask(RegisterAccountObserverFunc, ACCOUNT_RETRY_DELAY,
271 AppExecFwk::EventQueue::Priority::LOW); // 3000 means 3s
272 }
273 }
274
RegisterMemoryPressureObserver()275 void MemMgrEventCenter::RegisterMemoryPressureObserver()
276 {
277 HILOGI("called");
278 MAKE_POINTER(memoryPressureObserver_, shared, MemoryPressureObserver, "make MemoryPressureObserver failed",
279 /* no return */, /* no param */);
280 std::function<void()> initFunc = std::bind(&MemoryPressureObserver::Init, memoryPressureObserver_);
281 regObsHandler_->PostTask(initFunc, 10000, AppExecFwk::EventQueue::Priority::HIGH); // 10000 means 10s
282 }
283
~MemMgrEventCenter()284 MemMgrEventCenter::~MemMgrEventCenter()
285 {
286 UnregisterEventObserver();
287 }
288
UnregisterEventObserver()289 void MemMgrEventCenter::UnregisterEventObserver()
290 {
291 #ifdef CONFIG_BGTASK_MGR
292 if (bgTaskObserver_) {
293 BackgroundTaskMgr::BackgroundTaskMgrHelper::UnsubscribeBackgroundTask(*bgTaskObserver_);
294 }
295 bgTaskObserver_ = nullptr;
296 #endif
297 if (accountObserver_) {
298 AccountSA::OsAccountManager::UnsubscribeOsAccount(accountObserver_);
299 }
300 if (commonEventObserver_) {
301 EventFwk::CommonEventManager::UnSubscribeCommonEvent(commonEventObserver_);
302 }
303 if (appStateObserver_) {
304 delete appStateObserver_;
305 appStateObserver_ = nullptr;
306 }
307 appMgrClient_ = nullptr;
308 regObsHandler_ = nullptr;
309 extConnObserver_ = nullptr;
310 accountObserver_ = nullptr;
311 }
312
Dump(int fd)313 void MemMgrEventCenter::Dump(int fd)
314 {
315 dprintf(fd, "state list of all observer\n");
316 dprintf(fd, " name state \n");
317 dprintf(fd, "%30s %8s\n", "MemoryPressureObserver", memoryPressureObserver_ == nullptr ? "N" : "Y");
318 dprintf(fd, "-----------------------------------------------------------------\n");
319 dprintf(fd, "%30s %8s\n", "AppStateObserver", appStateObserver_ == nullptr ? "N" : "Y");
320 dprintf(fd, "-----------------------------------------------------------------\n");
321 dprintf(fd, "%30s %8s\n", "ExtConnObserver", extConnObserver_ == nullptr ? "N" : "Y");
322 dprintf(fd, "-----------------------------------------------------------------\n");
323 #ifdef CONFIG_BGTASK_MGR
324 dprintf(fd, "%30s %8s\n", "BgTaskObserver", bgTaskObserver_ == nullptr ? "N" : "Y");
325 dprintf(fd, "-----------------------------------------------------------------\n");
326 #endif
327 dprintf(fd, "%30s %8s\n", "AccountObserver", accountObserver_ == nullptr ? "N" : "Y");
328 dprintf(fd, "-----------------------------------------------------------------\n");
329 dprintf(fd, "%30s %8s\n", "CommonEventObserver", commonEventObserver_ == nullptr ? "N" : "Y");
330 dprintf(fd, "-----------------------------------------------------------------\n");
331 }
332
RetryRegisterEventObserver(int32_t systemAbilityId)333 void MemMgrEventCenter::RetryRegisterEventObserver(int32_t systemAbilityId)
334 {
335 #ifdef CONFIG_BGTASK_MGR
336 if (systemAbilityId == BACKGROUND_TASK_MANAGER_SERVICE_ID) {
337 HandlerRegisterEvent(RegisterEvent::REG_BGTASKOBS_EVENT);
338 }
339 #endif
340 if (systemAbilityId == ABILITY_MGR_SERVICE_ID || systemAbilityId == APP_MGR_SERVICE_ID) {
341 HandlerRegisterEvent(RegisterEvent::REG_APPOBS_EVENT);
342 }
343
344 if (systemAbilityId == SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN) {
345 HandlerRegisterEvent(RegisterEvent::REG_ACCOUNTOBS_EVENT);
346 }
347
348 if (systemAbilityId == COMMON_EVENT_SERVICE_ID || systemAbilityId == COMMON_EVENT_SERVICE_ABILITY_ID) {
349 HandlerRegisterEvent(RegisterEvent::REG_COMMONOBS_EVENT);
350 }
351 }
352 } // namespace Memory
353 } // namespace OHOS
354