• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 int ACCOUNT_MAX_RETRY_TIMES = 10;
34 const int ACCOUNT_RETRY_DELAY = 3000;
35 const int EXTCONN_RETRY_TIME = 1000;
36 }
37 
38 IMPLEMENT_SINGLE_INSTANCE(MemMgrEventCenter);
39 
MemMgrEventCenter()40 MemMgrEventCenter::MemMgrEventCenter()
41 {}
42 
Init()43 bool MemMgrEventCenter::Init()
44 {
45     HILOGI("called");
46     if (CreateRegisterHandler()) {
47         return RegisterEventObserver();
48     }
49     return false;
50 }
51 
CreateRegisterHandler()52 bool MemMgrEventCenter::CreateRegisterHandler()
53 {
54     if (!regObsHandler_) {
55         MAKE_POINTER(regObsHandler_, shared, AppExecFwk::EventHandler, "failed to create register handler",
56         return false, AppExecFwk::EventRunner::Create());
57     }
58     return true;
59 }
60 
RemoveEventObserver(int32_t systemAbilityId)61 void MemMgrEventCenter::RemoveEventObserver(int32_t systemAbilityId)
62 {
63     HILOGI("called");
64 
65     if (systemAbilityId == ABILITY_MGR_SERVICE_ID || systemAbilityId == APP_MGR_SERVICE_ID) {
66         appStateObserver_ = nullptr;
67         extConnObserver_ = nullptr;
68         ReclaimPriorityManager::GetInstance().Reset();
69     }
70     if (systemAbilityId == SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN) {
71         accountObserver_ = nullptr;
72     }
73 
74     if (systemAbilityId == COMMON_EVENT_SERVICE_ID || systemAbilityId == COMMON_EVENT_SERVICE_ABILITY_ID) {
75         commonEventObserver_ = nullptr;
76     }
77 }
78 
RegisterEventObserver()79 bool MemMgrEventCenter::RegisterEventObserver()
80 {
81     HILOGI("called");
82     if (!memoryPressureObserver_) {
83         RegisterMemoryPressureObserver();
84     }
85 
86     if (!appStateObserver_) {
87         RegisterAppStateObserver();
88     }
89 
90     if (!extConnObserver_) {
91         RegisterExtConnObserver();
92     }
93 
94     if (!accountObserver_) {
95         RegisterAccountObserver();
96     }
97 
98     if (!commonEventObserver_) {
99         RegisterCommonEventObserver();
100     }
101 
102     return true;
103 }
104 
RegisterAppStateObserver()105 void MemMgrEventCenter::RegisterAppStateObserver()
106 {
107     HILOGI("called");
108     MAKE_POINTER(appMgrClient_, unique, AppExecFwk::AppMgrClient, "make appMgrClient failed",
109          return, /* no param */);
110     appStateObserver_ = new (std::nothrow) AppStateObserver();
111     while (appMgrClient_->ConnectAppMgrService() != AppExecFwk::AppMgrResultCode::RESULT_OK) {
112         HILOGE("ConnectAppMgrService fail, try again! retryTimes=%{public}d", ++regAppStatusObsRetry_);
113     }
114     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
115     sptr<AppExecFwk::IAppMgr> appObject =
116         iface_cast<AppExecFwk::IAppMgr>(systemAbilityManager->GetSystemAbility(APP_MGR_SERVICE_ID));
117     if (appObject) {
118         int ret = appObject->RegisterApplicationStateObserver(appStateObserver_);
119         if (ret == ERR_OK) {
120             HILOGI("register success");
121             return;
122         }
123         HILOGE("register fail, ret = %{public}d", ret);
124         return;
125     }
126     HILOGE("get SystemAbilityManager fail");
127 }
128 
RegisterExtConnObserver()129 void MemMgrEventCenter::RegisterExtConnObserver()
130 {
131     HILOGI("called");
132     MAKE_POINTER(extConnObserver_, shared, ExtensionConnectionObserver, "make ExtensionConnectionObserver failed",
133         /* no return */, /* no param */);
134     if (extConnObserver_ != nullptr) {
135         int32_t ret = AbilityRuntime::ConnectionObserverClient::GetInstance().RegisterObserver(extConnObserver_);
136         if (ret == ERR_OK) {
137             HILOGI("register success");
138             return;
139         }
140         HILOGE("register fail, ret = %{public}d", ret);
141     }
142     std::function<void()> RegisterExtConnObserverFunc = std::bind(&MemMgrEventCenter::RegisterExtConnObserver, this);
143     regObsHandler_->PostTask(RegisterExtConnObserverFunc, EXTCONN_RETRY_TIME, AppExecFwk::EventQueue::Priority::LOW);
144 }
145 
RegisterBgTaskObserver()146 void MemMgrEventCenter::RegisterBgTaskObserver()
147 {
148     HILOGI("called");
149 #ifdef CONFIG_BGTASK_MGR
150     MAKE_POINTER(bgTaskObserver_, shared, BgTaskObserver, "make BgTaskObserver failed",
151         /* no return */, /* no param */);
152     ErrCode ret = BackgroundTaskMgr::BackgroundTaskMgrHelper::SubscribeBackgroundTask(*bgTaskObserver_);
153     if (ret == ERR_OK) {
154         HILOGI("register success");
155         return;
156     }
157     HILOGE("register fail, ret = %{public}d", ret);
158     std::function<void()> RegisterBgTaskObserverFunc = std::bind(&MemMgrEventCenter::RegisterBgTaskObserver, this);
159     regObsHandler_->PostTask(RegisterBgTaskObserverFunc, EXTCONN_RETRY_TIME, AppExecFwk::EventQueue::Priority::LOW);
160 #else
161     HILOGI("BackgroundTaskMgr is not enable.");
162 #endif
163 }
164 
RegisterCommonEventObserver()165 void MemMgrEventCenter::RegisterCommonEventObserver()
166 {
167     HILOGI("called");
168     EventFwk::MatchingSkills matchingSkills;
169     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED);
170     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED);
171     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED);
172     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
173     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
174     EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills);
175     MAKE_POINTER(commonEventObserver_, shared, CommonEventObserver, "make unique failed",
176         return, commonEventSubscribeInfo);
177     if (EventFwk::CommonEventManager::SubscribeCommonEvent(commonEventObserver_)) {
178         HILOGI("register success");
179         return;
180     }
181     HILOGI("register fail");
182 }
183 
RegisterAccountObserver()184 void MemMgrEventCenter::RegisterAccountObserver()
185 {
186     HILOGI("called");
187     regAccountObsRetry_++;
188     AccountSA::OsAccountSubscribeInfo osAccountSubscribeInfo;
189     osAccountSubscribeInfo.SetOsAccountSubscribeType(AccountSA::OS_ACCOUNT_SUBSCRIBE_TYPE::ACTIVED);
190     osAccountSubscribeInfo.SetName("MemMgrAccountActivedSubscriber");
191     MAKE_POINTER(accountObserver_, shared, AccountObserver, "make unique failed", return, osAccountSubscribeInfo);
192     ErrCode errCode = AccountSA::OsAccountManager::SubscribeOsAccount(accountObserver_);
193     if (errCode == ERR_OK) {
194         HILOGI("register success");
195         return;
196     }
197 
198     if (regAccountObsRetry_ < ACCOUNT_MAX_RETRY_TIMES) {
199         std::function<void()> RegisterAccountObserverFunc =
200             std::bind(&MemMgrEventCenter::RegisterAccountObserver, this);
201         HILOGE("register fail, retCode = %{public}d, try again after 3s!, retryTimes=%{public}d/10",
202             errCode, regAccountObsRetry_);
203         regObsHandler_->PostTask(RegisterAccountObserverFunc, ACCOUNT_RETRY_DELAY,
204             AppExecFwk::EventQueue::Priority::LOW); // 3000 means 3s
205     }
206 }
207 
RegisterMemoryPressureObserver()208 void MemMgrEventCenter::RegisterMemoryPressureObserver()
209 {
210     HILOGI("called");
211     MAKE_POINTER(memoryPressureObserver_, shared, MemoryPressureObserver, "make MemoryPressureObserver failed",
212         /* no return */, /* no param */);
213     std::function<void()> initFunc = std::bind(&MemoryPressureObserver::Init, memoryPressureObserver_);
214     regObsHandler_->PostTask(initFunc, 10000, AppExecFwk::EventQueue::Priority::HIGH); // 10000 means 10s
215 }
216 
~MemMgrEventCenter()217 MemMgrEventCenter::~MemMgrEventCenter()
218 {
219     UnregisterEventObserver();
220 }
221 
UnregisterEventObserver()222 void MemMgrEventCenter::UnregisterEventObserver()
223 {
224 #ifdef CONFIG_BGTASK_MGR
225     if (bgTaskObserver_) {
226         BackgroundTaskMgr::BackgroundTaskMgrHelper::UnsubscribeBackgroundTask(*bgTaskObserver_);
227     }
228     bgTaskObserver_ = nullptr;
229 #endif
230     if (accountObserver_) {
231         AccountSA::OsAccountManager::UnsubscribeOsAccount(accountObserver_);
232     }
233     if (commonEventObserver_) {
234         EventFwk::CommonEventManager::UnSubscribeCommonEvent(commonEventObserver_);
235     }
236     if (appStateObserver_) {
237         delete appStateObserver_;
238         appStateObserver_ = nullptr;
239     }
240     appMgrClient_ = nullptr;
241     regObsHandler_ = nullptr;
242     extConnObserver_ = nullptr;
243     accountObserver_ = nullptr;
244 }
245 
Dump(int fd)246 void MemMgrEventCenter::Dump(int fd)
247 {
248     dprintf(fd, "state list of all observer\n");
249     dprintf(fd, "                 name               state \n");
250     dprintf(fd, "%30s %8s\n", "MemoryPressureObserver", memoryPressureObserver_ == nullptr ? "N" : "Y");
251     dprintf(fd, "-----------------------------------------------------------------\n");
252     dprintf(fd, "%30s %8s\n", "AppStateObserver", appStateObserver_ == nullptr ? "N" : "Y");
253     dprintf(fd, "-----------------------------------------------------------------\n");
254     dprintf(fd, "%30s %8s\n", "ExtConnObserver", extConnObserver_ == nullptr ? "N" : "Y");
255     dprintf(fd, "-----------------------------------------------------------------\n");
256 #ifdef CONFIG_BGTASK_MGR
257     dprintf(fd, "%30s %8s\n", "BgTaskObserver", bgTaskObserver_ == nullptr ? "N" : "Y");
258     dprintf(fd, "-----------------------------------------------------------------\n");
259 #endif
260     dprintf(fd, "%30s %8s\n", "AccountObserver", accountObserver_ == nullptr ? "N" : "Y");
261     dprintf(fd, "-----------------------------------------------------------------\n");
262     dprintf(fd, "%30s %8s\n", "CommonEventObserver", commonEventObserver_ == nullptr ? "N" : "Y");
263     dprintf(fd, "-----------------------------------------------------------------\n");
264 }
265 
RetryRegisterEventObserver(int32_t systemAbilityId)266 void MemMgrEventCenter::RetryRegisterEventObserver(int32_t systemAbilityId)
267 {
268 #ifdef CONFIG_BGTASK_MGR
269     if (systemAbilityId == BACKGROUND_TASK_MANAGER_SERVICE_ID) {
270         RegisterBgTaskObserver();
271     }
272 #endif
273     RegisterEventObserver();
274 }
275 
276 } // namespace Memory
277 } // namespace OHOS
278