1 /*
2 * Copyright (c) 2022-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 #include "app_state_observer_manager.h"
17
18 #include "ability_foreground_state_observer_stub.h"
19 #include "app_foreground_state_observer_stub.h"
20 #include "application_state_observer_stub.h"
21 #include "hilog_tag_wrapper.h"
22 #include "hitrace_meter.h"
23 #include "in_process_call_wrapper.h"
24 #include "remote_client_manager.h"
25 #include "ui_extension_utils.h"
26 #include "parameters.h"
27
28 namespace OHOS {
29 namespace AppExecFwk {
30 namespace {
31 const std::string XIAOYI_BUNDLE_NAME = "com.huawei.hmos.vassistant";
32 constexpr int BUNDLE_NAME_LIST_MAX_SIZE = 128;
33 constexpr int OBSERVER_SINGLE_COUNT_LOG = 40;
34 constexpr int OBSERVER_SINGLE_STEP_LOG = 10;
35 constexpr int OBSERVER_UID_COUNT_LOG = 3;
36 constexpr int OBSERVER_AMOUNT_COUNT_LOG = 70;
37 constexpr const char* DEVELOPER_MODE_STATE = "const.security.developermode.state";
38 } // namespace
AppStateObserverManager()39 AppStateObserverManager::AppStateObserverManager()
40 {
41 TAG_LOGD(AAFwkTag::APPMGR, "AppStateObserverManager instance is created");
42 }
43
~AppStateObserverManager()44 AppStateObserverManager::~AppStateObserverManager()
45 {
46 TAG_LOGD(AAFwkTag::APPMGR, "AppStateObserverManager instance is destroyed");
47 }
48
Init()49 void AppStateObserverManager::Init()
50 {
51 if (!handler_) {
52 handler_ = AAFwk::TaskHandlerWrap::CreateQueueHandler("app_state_task_queue");
53 }
54 }
55
RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer,const std::vector<std::string> & bundleNameList)56 int32_t AppStateObserverManager::RegisterApplicationStateObserver(
57 const sptr<IApplicationStateObserver> &observer, const std::vector<std::string> &bundleNameList)
58 {
59 TAG_LOGD(AAFwkTag::APPMGR, "called");
60 if (bundleNameList.size() > BUNDLE_NAME_LIST_MAX_SIZE) {
61 TAG_LOGE(AAFwkTag::APPMGR, "the bundleNameList passed in is too long");
62 return ERR_INVALID_VALUE;
63 }
64 if (AAFwk::PermissionVerification::GetInstance()->VerifyAppStateObserverPermission() == ERR_PERMISSION_DENIED) {
65 TAG_LOGE(AAFwkTag::APPMGR, "Permission verification failed");
66 return ERR_PERMISSION_DENIED;
67 }
68 if (observer == nullptr) {
69 TAG_LOGE(AAFwkTag::APPMGR, "The param observer is nullptr.");
70 return ERR_INVALID_VALUE;
71 }
72 if (ObserverExist(observer)) {
73 TAG_LOGE(AAFwkTag::APPMGR, "Observer exist.");
74 return ERR_INVALID_VALUE;
75 }
76 std::lock_guard lockRegister(observerLock_);
77 appStateObserverMap_.emplace(observer, AppStateObserverInfo{IPCSkeleton::GetCallingUid(), bundleNameList});
78 if (appStateObserverMap_.size() >= OBSERVER_SINGLE_COUNT_LOG &&
79 appStateObserverMap_.size() % OBSERVER_SINGLE_STEP_LOG == 0) {
80 TAG_LOGW(AAFwkTag::APPMGR, "appStateObserverMap_ size:%{public}zu", appStateObserverMap_.size());
81 }
82 AddObserverCount(IPCSkeleton::GetCallingUid());
83 AddObserverDeathRecipient(observer, ObserverType::APPLICATION_STATE_OBSERVER);
84 return ERR_OK;
85 }
86
UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)87 int32_t AppStateObserverManager::UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer)
88 {
89 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
90 TAG_LOGD(AAFwkTag::APPMGR, "called");
91 if (AAFwk::PermissionVerification::GetInstance()->VerifyAppStateObserverPermission() == ERR_PERMISSION_DENIED) {
92 TAG_LOGE(AAFwkTag::APPMGR, "Permission verification failed");
93 return ERR_PERMISSION_DENIED;
94 }
95 std::lock_guard lockUnregister(observerLock_);
96 if (observer == nullptr) {
97 TAG_LOGE(AAFwkTag::APPMGR, "Observer nullptr");
98 return ERR_INVALID_VALUE;
99 }
100
101 for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
102 if (it->first->AsObject() == observer->AsObject()) {
103 DecreaseObserverCount(it->second.uid);
104 appStateObserverMap_.erase(it);
105 TAG_LOGD(AAFwkTag::APPMGR, "appStateObserverMap_ size:%{public}zu", appStateObserverMap_.size());
106 RemoveObserverDeathRecipient(observer);
107 return ERR_OK;
108 }
109 }
110 TAG_LOGE(AAFwkTag::APPMGR, "Observer not exist.");
111 return ERR_INVALID_VALUE;
112 }
113
RegisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> & observer)114 int32_t AppStateObserverManager::RegisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer)
115 {
116 TAG_LOGD(AAFwkTag::APPMGR, "called");
117 if (observer == nullptr) {
118 TAG_LOGE(AAFwkTag::APPMGR, "The param observer is nullptr.");
119 return ERR_INVALID_VALUE;
120 }
121 if (AAFwk::PermissionVerification::GetInstance()->VerifyAppStateObserverPermission() == ERR_PERMISSION_DENIED) {
122 TAG_LOGE(AAFwkTag::APPMGR, "Permission verification failed.");
123 return ERR_PERMISSION_DENIED;
124 }
125 if (IsAppForegroundObserverExist(observer)) {
126 TAG_LOGE(AAFwkTag::APPMGR, "Observer exist.");
127 return ERR_INVALID_VALUE;
128 }
129
130 std::lock_guard lockRegister(appForegroundObserverLock_);
131 appForegroundStateObserverMap_.emplace(observer, IPCSkeleton::GetCallingUid());
132 AddObserverDeathRecipient(observer, ObserverType::APP_FOREGROUND_STATE_OBSERVER);
133 AddObserverCount(IPCSkeleton::GetCallingUid());
134 if (appForegroundStateObserverMap_.size() >= OBSERVER_SINGLE_COUNT_LOG &&
135 appForegroundStateObserverMap_.size() % OBSERVER_SINGLE_STEP_LOG == 0) {
136 TAG_LOGW(AAFwkTag::APPMGR, "appForegroundObserver size:%{public}zu", appForegroundStateObserverMap_.size());
137 }
138 return ERR_OK;
139 }
140
UnregisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> & observer)141 int32_t AppStateObserverManager::UnregisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer)
142 {
143 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
144 TAG_LOGD(AAFwkTag::APPMGR, "called");
145 if (observer == nullptr) {
146 TAG_LOGE(AAFwkTag::APPMGR, "Observer nullptr.");
147 return ERR_INVALID_VALUE;
148 }
149 if (AAFwk::PermissionVerification::GetInstance()->VerifyAppStateObserverPermission() == ERR_PERMISSION_DENIED) {
150 TAG_LOGE(AAFwkTag::APPMGR, "Permission verification failed.");
151 return ERR_PERMISSION_DENIED;
152 }
153 std::lock_guard lockUnregister(appForegroundObserverLock_);
154 for (const auto &[it, uid] : appForegroundStateObserverMap_) {
155 if (it != nullptr && it->AsObject() == observer->AsObject()) {
156 DecreaseObserverCount(uid);
157 appForegroundStateObserverMap_.erase(it);
158 RemoveObserverDeathRecipient(observer);
159 return ERR_OK;
160 }
161 }
162 return ERR_INVALID_VALUE;
163 }
164
RegisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> & observer)165 int32_t AppStateObserverManager::RegisterAbilityForegroundStateObserver(
166 const sptr<IAbilityForegroundStateObserver> &observer)
167 {
168 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
169 TAG_LOGD(AAFwkTag::APPMGR, "called");
170 if (observer == nullptr) {
171 TAG_LOGE(AAFwkTag::APPMGR, "The param observer is nullptr.");
172 return ERR_INVALID_VALUE;
173 }
174 if (AAFwk::PermissionVerification::GetInstance()->VerifyAppStateObserverPermission() == ERR_PERMISSION_DENIED) {
175 TAG_LOGE(AAFwkTag::APPMGR, "Permission verification failed.");
176 return ERR_PERMISSION_DENIED;
177 }
178 if (IsAbilityForegroundObserverExist(observer)) {
179 TAG_LOGW(AAFwkTag::APPMGR, "Observer exist.");
180 return ERR_OK;
181 }
182
183 std::lock_guard lockRegister(abilityForegroundObserverLock_);
184 abilityForegroundObserverMap_.emplace(observer, IPCSkeleton::GetCallingUid());
185 AddObserverDeathRecipient(observer, ObserverType::ABILITY_FOREGROUND_STATE_OBSERVER);
186 AddObserverCount(IPCSkeleton::GetCallingUid());
187 if (abilityForegroundObserverMap_.size() >= OBSERVER_SINGLE_COUNT_LOG &&
188 abilityForegroundObserverMap_.size() % OBSERVER_SINGLE_STEP_LOG == 0) {
189 TAG_LOGW(AAFwkTag::APPMGR, "abilityForegroundObserver size:%{public}zu", abilityForegroundObserverMap_.size());
190 }
191 return ERR_OK;
192 }
193
UnregisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> & observer)194 int32_t AppStateObserverManager::UnregisterAbilityForegroundStateObserver(
195 const sptr<IAbilityForegroundStateObserver> &observer)
196 {
197 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
198 TAG_LOGD(AAFwkTag::APPMGR, "called");
199 if (observer == nullptr) {
200 TAG_LOGE(AAFwkTag::APPMGR, "Observer nullptr.");
201 return ERR_INVALID_VALUE;
202 }
203 if (AAFwk::PermissionVerification::GetInstance()->VerifyAppStateObserverPermission() == ERR_PERMISSION_DENIED) {
204 TAG_LOGE(AAFwkTag::APPMGR, "Permission verification failed.");
205 return ERR_PERMISSION_DENIED;
206 }
207 std::lock_guard lockUnregister(abilityForegroundObserverLock_);
208 for (const auto &[it, uid] : abilityForegroundObserverMap_) {
209 if (it != nullptr && it->AsObject() == observer->AsObject()) {
210 DecreaseObserverCount(uid);
211 abilityForegroundObserverMap_.erase(it);
212 RemoveObserverDeathRecipient(observer);
213 return ERR_OK;
214 }
215 }
216 return ERR_INVALID_VALUE;
217 }
218
OnAppStarted(const std::shared_ptr<AppRunningRecord> & appRecord)219 void AppStateObserverManager::OnAppStarted(const std::shared_ptr<AppRunningRecord> &appRecord)
220 {
221 if (handler_ == nullptr) {
222 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnAppStarted failed.");
223 return;
224 }
225
226 auto task = [weak = weak_from_this(), appRecord]() {
227 auto self = weak.lock();
228 if (self == nullptr) {
229 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnAppStarted failed.");
230 return;
231 }
232 TAG_LOGD(AAFwkTag::APPMGR, "OnAppStarted come.");
233 self->HandleOnAppStarted(appRecord);
234 };
235 handler_->SubmitTask(task);
236 }
237
OnAppStopped(const std::shared_ptr<AppRunningRecord> & appRecord)238 void AppStateObserverManager::OnAppStopped(const std::shared_ptr<AppRunningRecord> &appRecord)
239 {
240 if (handler_ == nullptr) {
241 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnAppStopped failed.");
242 return;
243 }
244
245 auto task = [weak = weak_from_this(), appRecord]() {
246 auto self = weak.lock();
247 if (self == nullptr) {
248 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnAppStopped failed.");
249 return;
250 }
251 TAG_LOGD(AAFwkTag::APPMGR, "OnAppStopped come.");
252 self->HandleOnAppStopped(appRecord);
253 };
254 handler_->SubmitTask(task);
255 }
256
257
OnAppStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord,const ApplicationState state,bool needNotifyApp,bool isFromWindowFocusChanged)258 void AppStateObserverManager::OnAppStateChanged(
259 const std::shared_ptr<AppRunningRecord> &appRecord,
260 const ApplicationState state,
261 bool needNotifyApp,
262 bool isFromWindowFocusChanged)
263 {
264 if (handler_ == nullptr) {
265 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnAppStateChanged failed.");
266 return;
267 }
268
269 auto task = [weak = weak_from_this(), appRecord, state, needNotifyApp, isFromWindowFocusChanged]() {
270 auto self = weak.lock();
271 if (self == nullptr) {
272 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnAppStateChanged failed.");
273 return;
274 }
275 TAG_LOGD(AAFwkTag::APPMGR, "OnAppStateChanged come.");
276 self->HandleAppStateChanged(appRecord, state, needNotifyApp, isFromWindowFocusChanged);
277 };
278 handler_->SubmitTask(task);
279 }
280
OnProcessDied(const std::shared_ptr<AppRunningRecord> & appRecord)281 void AppStateObserverManager::OnProcessDied(const std::shared_ptr<AppRunningRecord> &appRecord)
282 {
283 if (handler_ == nullptr) {
284 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnProcessDied failed.");
285 return;
286 }
287
288 auto task = [weak = weak_from_this(), appRecord]() {
289 auto self = weak.lock();
290 if (self == nullptr) {
291 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnProcessDied failed.");
292 return;
293 }
294 TAG_LOGD(AAFwkTag::APPMGR, "OnProcessDied come.");
295 self->HandleOnAppProcessDied(appRecord);
296 };
297 handler_->SubmitTask(task);
298 }
299
OnRenderProcessDied(const std::shared_ptr<RenderRecord> & renderRecord)300 void AppStateObserverManager::OnRenderProcessDied(const std::shared_ptr<RenderRecord> &renderRecord)
301 {
302 if (handler_ == nullptr) {
303 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnRenderProcessDied failed.");
304 return;
305 }
306
307 auto task = [weak = weak_from_this(), renderRecord]() {
308 auto self = weak.lock();
309 if (self == nullptr) {
310 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnRenderProcessDied failed.");
311 return;
312 }
313 TAG_LOGD(AAFwkTag::APPMGR, "OnRenderProcessDied come.");
314 self->HandleOnRenderProcessDied(renderRecord);
315 };
316 handler_->SubmitTask(task);
317 }
318
OnChildProcessDied(std::shared_ptr<ChildProcessRecord> childRecord)319 void AppStateObserverManager::OnChildProcessDied(std::shared_ptr<ChildProcessRecord> childRecord)
320 {
321 if (handler_ == nullptr) {
322 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnChildProcessDied failed.");
323 return;
324 }
325
326 auto task = [weak = weak_from_this(), childRecord]() {
327 auto self = weak.lock();
328 if (self == nullptr) {
329 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnChildProcessDied failed.");
330 return;
331 }
332 TAG_LOGD(AAFwkTag::APPMGR, "OnChildProcessDied come.");
333 self->HandleOnChildProcessDied(childRecord);
334 };
335 handler_->SubmitTask(task);
336 }
337
OnProcessStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord)338 void AppStateObserverManager::OnProcessStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord)
339 {
340 if (handler_ == nullptr) {
341 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnProcessStateChanged failed.");
342 return;
343 }
344
345 auto task = [weak = weak_from_this(), appRecord]() {
346 auto self = weak.lock();
347 if (self == nullptr) {
348 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnProcessStateChanged failed.");
349 return;
350 }
351 TAG_LOGD(AAFwkTag::APPMGR, "OnProcessStateChanged come.");
352 self->HandleOnProcessStateChanged(appRecord);
353 };
354 handler_->SubmitTask(task);
355 }
356
OnProcessCreated(const std::shared_ptr<AppRunningRecord> & appRecord,bool isPreload)357 void AppStateObserverManager::OnProcessCreated(const std::shared_ptr<AppRunningRecord> &appRecord, bool isPreload)
358 {
359 if (handler_ == nullptr) {
360 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnProcessCreated failed.");
361 return;
362 }
363
364 auto task = [weak = weak_from_this(), appRecord, isPreload]() {
365 auto self = weak.lock();
366 if (self == nullptr) {
367 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnProcessCreated failed.");
368 return;
369 }
370 self->HandleOnAppProcessCreated(appRecord, isPreload);
371 };
372 handler_->SubmitTask(task);
373 }
374
OnProcessReused(const std::shared_ptr<AppRunningRecord> & appRecord)375 void AppStateObserverManager::OnProcessReused(const std::shared_ptr<AppRunningRecord> &appRecord)
376 {
377 if (handler_ == nullptr) {
378 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnProcessReused failed.");
379 return;
380 }
381
382 auto task = [weak = weak_from_this(), appRecord]() {
383 auto self = weak.lock();
384 if (self == nullptr) {
385 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnProcessReused failed.");
386 return;
387 }
388 TAG_LOGD(AAFwkTag::APPMGR, "OnProcessReused come.");
389 self->HandleOnProcessResued(appRecord);
390 };
391 handler_->SubmitTask(task);
392 }
393
OnRenderProcessCreated(const std::shared_ptr<RenderRecord> & renderRecord)394 void AppStateObserverManager::OnRenderProcessCreated(const std::shared_ptr<RenderRecord> &renderRecord)
395 {
396 if (handler_ == nullptr) {
397 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnRenderProcessCreated failed.");
398 return;
399 }
400
401 auto task = [weak = weak_from_this(), renderRecord]() {
402 auto self = weak.lock();
403 if (self == nullptr) {
404 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnRenderProcessCreated failed.");
405 return;
406 }
407 TAG_LOGD(AAFwkTag::APPMGR, "OnRenderProcessCreated come.");
408 self->HandleOnRenderProcessCreated(renderRecord);
409 };
410 handler_->SubmitTask(task);
411 }
412
OnChildProcessCreated(std::shared_ptr<ChildProcessRecord> childRecord)413 void AppStateObserverManager::OnChildProcessCreated(std::shared_ptr<ChildProcessRecord> childRecord)
414 {
415 if (handler_ == nullptr) {
416 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnChildProcessCreated failed.");
417 return;
418 }
419
420 auto task = [weak = weak_from_this(), childRecord]() {
421 auto self = weak.lock();
422 if (self == nullptr) {
423 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnChildProcessCreated failed.");
424 return;
425 }
426 TAG_LOGD(AAFwkTag::APPMGR, "OnChildProcessCreated come.");
427 self->HandleOnChildProcessCreated(childRecord);
428 };
429 handler_->SubmitTask(task);
430 }
431
StateChangedNotifyObserver(const AbilityStateData abilityStateData,bool isAbility,bool isFromWindowFocusChanged)432 void AppStateObserverManager::StateChangedNotifyObserver(
433 const AbilityStateData abilityStateData, bool isAbility, bool isFromWindowFocusChanged)
434 {
435 if (handler_ == nullptr) {
436 TAG_LOGE(AAFwkTag::APPMGR, "null handler, failed");
437 return;
438 }
439
440 auto task = [weak = weak_from_this(), abilityStateData, isAbility, isFromWindowFocusChanged]() {
441 auto self = weak.lock();
442 if (self == nullptr) {
443 TAG_LOGE(AAFwkTag::APPMGR, "null self, failed");
444 return;
445 }
446 TAG_LOGD(AAFwkTag::APPMGR, "StateChangedNotifyObserver come");
447 self->HandleStateChangedNotifyObserver(abilityStateData, isAbility, isFromWindowFocusChanged);
448 };
449 handler_->SubmitTask(task);
450 }
451
HandleOnAppStarted(const std::shared_ptr<AppRunningRecord> & appRecord)452 void AppStateObserverManager::HandleOnAppStarted(const std::shared_ptr<AppRunningRecord> &appRecord)
453 {
454 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
455 if (appRecord == nullptr) {
456 TAG_LOGE(AAFwkTag::APPMGR, "app record is null");
457 return;
458 }
459
460 AppStateData data = WrapAppStateData(appRecord, ApplicationState::APP_STATE_CREATE);
461 data.isSpecifyTokenId = appRecord->GetAssignTokenId() > 0 ? true : false;
462 TAG_LOGD(AAFwkTag::APPMGR, "HandleOnAppStarted, bundle:%{public}s, uid:%{public}d, state:%{public}d",
463 data.bundleName.c_str(), data.uid, data.state);
464 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
465 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
466 const auto &bundleNames = it->second.bundleNames;
467 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
468 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
469 it->first->OnAppStarted(data);
470 }
471 }
472 }
473
HandleOnAppStopped(const std::shared_ptr<AppRunningRecord> & appRecord)474 void AppStateObserverManager::HandleOnAppStopped(const std::shared_ptr<AppRunningRecord> &appRecord)
475 {
476 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
477 if (appRecord == nullptr) {
478 TAG_LOGE(AAFwkTag::APPMGR, "app record is null");
479 return;
480 }
481
482 AppStateData data = WrapAppStateData(appRecord, ApplicationState::APP_STATE_TERMINATED);
483 TAG_LOGD(AAFwkTag::APPMGR, "HandleOnAppStopped, bundle:%{public}s, uid:%{public}d, state:%{public}d",
484 data.bundleName.c_str(), data.uid, data.state);
485 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
486 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
487 const auto &bundleNames = it->second.bundleNames;
488 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
489 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
490 it->first->OnAppStopped(data);
491 }
492 }
493 }
494
HandleAppStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord,const ApplicationState state,bool needNotifyApp,bool isFromWindowFocusChanged)495 void AppStateObserverManager::HandleAppStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord,
496 const ApplicationState state, bool needNotifyApp, bool isFromWindowFocusChanged)
497 {
498 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
499 if (appRecord == nullptr) {
500 return;
501 }
502 if (state == ApplicationState::APP_STATE_FOREGROUND || state == ApplicationState::APP_STATE_BACKGROUND) {
503 if (needNotifyApp && !isFromWindowFocusChanged) {
504 AppStateData data = WrapAppStateData(appRecord, state);
505 appRecord->GetSplitModeAndFloatingMode(data.isSplitScreenMode, data.isFloatingWindowMode);
506 auto appForegroundStateObserverMap = GetAppForegroundStateObserverMapCopy();
507 for (const auto &[observer, uid] : appForegroundStateObserverMap) {
508 if (observer != nullptr) {
509 observer->OnAppStateChanged(data);
510 }
511 }
512 }
513 if (!AAFwk::UIExtensionUtils::IsUIExtension(appRecord->GetExtensionType()) &&
514 !AAFwk::UIExtensionUtils::IsWindowExtension(appRecord->GetExtensionType())) {
515 AppStateData data = WrapAppStateData(appRecord, state);
516 TAG_LOGD(AAFwkTag::APPMGR, "name:%{public}s, uid:%{public}d, state:%{public}d, notify:%{public}d",
517 data.bundleName.c_str(), data.uid, data.state, needNotifyApp);
518 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
519 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
520 const auto &bundleNames = it->second.bundleNames;
521 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
522 bool valid = (bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr;
523 if (valid) {
524 it->first->OnForegroundApplicationChanged(data);
525 }
526 if (valid && needNotifyApp) {
527 it->first->OnAppStateChanged(data);
528 }
529 }
530 }
531 }
532 if (state == ApplicationState::APP_STATE_CREATE || state == ApplicationState::APP_STATE_TERMINATED) {
533 AppStateData data = WrapAppStateData(appRecord, state);
534 TAG_LOGD(AAFwkTag::APPMGR, "OnApplicationStateChanged, name:%{public}s, uid:%{public}d, state:%{public}d",
535 data.bundleName.c_str(), data.uid, data.state);
536 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
537 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
538 const auto &bundleNames = it->second.bundleNames;
539 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
540 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
541 it->first->OnApplicationStateChanged(data);
542 }
543 }
544 }
545 }
546
HandleStateChangedNotifyObserver(const AbilityStateData abilityStateData,bool isAbility,bool isFromWindowFocusChanged)547 void AppStateObserverManager::HandleStateChangedNotifyObserver(
548 const AbilityStateData abilityStateData, bool isAbility, bool isFromWindowFocusChanged)
549 {
550 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
551 TAG_LOGD(AAFwkTag::APPMGR,
552 "Handle state change, module:%{public}s, bundle:%{public}s, ability:%{public}s, state:%{public}d,"
553 "pid:%{public}d ,uid:%{public}d, abilityType:%{public}d, isAbility:%{public}d, callerBundleName:%{public}s,"
554 "callerAbilityName:%{public}s, isAtomicService:%{public}d",
555 abilityStateData.moduleName.c_str(), abilityStateData.bundleName.c_str(),
556 abilityStateData.abilityName.c_str(), abilityStateData.abilityState,
557 abilityStateData.pid, abilityStateData.uid, abilityStateData.abilityType, isAbility,
558 abilityStateData.callerBundleName.c_str(), abilityStateData.callerAbilityName.c_str(),
559 abilityStateData.isAtomicService);
560 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
561 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
562 const auto &bundleNames = it->second.bundleNames;
563 auto iter = std::find(bundleNames.begin(), bundleNames.end(), abilityStateData.bundleName);
564 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
565 if (isAbility) {
566 it->first->OnAbilityStateChanged(abilityStateData);
567 } else {
568 it->first->OnExtensionStateChanged(abilityStateData);
569 }
570 }
571 }
572
573 if ((abilityStateData.abilityState == static_cast<int32_t>(AbilityState::ABILITY_STATE_FOREGROUND) ||
574 abilityStateData.abilityState == static_cast<int32_t>(AbilityState::ABILITY_STATE_BACKGROUND)) &&
575 isAbility && !isFromWindowFocusChanged) {
576 auto abilityForegroundObserverMap = GetAbilityForegroundObserverMapCopy();
577 for (auto &[observer, uid] : abilityForegroundObserverMap) {
578 if (observer != nullptr) {
579 observer->OnAbilityStateChanged(abilityStateData);
580 }
581 }
582 }
583 }
584
HandleOnAppProcessCreated(const std::shared_ptr<AppRunningRecord> & appRecord,bool isPreload)585 void AppStateObserverManager::HandleOnAppProcessCreated(const std::shared_ptr<AppRunningRecord> &appRecord,
586 bool isPreload)
587 {
588 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
589 if (!appRecord) {
590 TAG_LOGE(AAFwkTag::APPMGR, "app record is null");
591 return;
592 }
593 ProcessData data = WrapProcessData(appRecord);
594 data.isPreload = isPreload;
595 data.isPreloadModule = appRecord->GetNeedLimitPrio();
596 if (data.bundleName == XIAOYI_BUNDLE_NAME && data.extensionType == ExtensionAbilityType::SERVICE) {
597 TAG_LOGI(AAFwkTag::APPMGR, "bundleName is com.huawei.chmos.vassistant, change processType to NORMAL");
598 data.processType = ProcessType::NORMAL;
599 }
600 TAG_LOGI(AAFwkTag::APPMGR,
601 "Process Create, bundle:%{public}s, pid:%{public}d, uid:%{public}d, processType:%{public}d, "
602 "extensionType:%{public}d, processName:%{public}s, renderUid:%{public}d, isTestMode:%{public}d, "
603 "callerPid:%{public}d, callerUid:%{public}d",
604 data.bundleName.c_str(), data.pid, data.uid, data.processType, data.extensionType, data.processName.c_str(),
605 data.renderUid, data.isTestMode, data.callerPid, data.callerUid);
606 HandleOnProcessCreated(data);
607 }
608
HandleOnProcessResued(const std::shared_ptr<AppRunningRecord> & appRecord)609 void AppStateObserverManager::HandleOnProcessResued(const std::shared_ptr<AppRunningRecord> &appRecord)
610 {
611 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
612 if (!appRecord) {
613 TAG_LOGE(AAFwkTag::APPMGR, "app record is null");
614 return;
615 }
616 ProcessData data = WrapProcessData(appRecord);
617 TAG_LOGD(AAFwkTag::APPMGR, "Process Resued, bundle:%{public}s, pid:%{public}d, uid:%{public}d",
618 data.bundleName.c_str(), data.pid, data.uid);
619
620 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
621 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
622 const auto &bundleNames = it->second.bundleNames;
623 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
624 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
625 it->first->OnProcessReused(data);
626 }
627 }
628 }
629
HandleOnRenderProcessCreated(const std::shared_ptr<RenderRecord> & renderRecord)630 void AppStateObserverManager::HandleOnRenderProcessCreated(const std::shared_ptr<RenderRecord> &renderRecord)
631 {
632 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
633 if (!renderRecord) {
634 TAG_LOGE(AAFwkTag::APPMGR, "render record is null");
635 return;
636 }
637 ProcessData data = WrapRenderProcessData(renderRecord);
638 TAG_LOGD(AAFwkTag::APPMGR,
639 "RenderProcess Create, bundle:%{public}s, pid:%{public}d, uid:%{public}d, processType:%{public}d, "
640 "processName:%{public}s, renderUid:%{public}d",
641 data.bundleName.c_str(), data.pid, data.uid, data.processType, data.processName.c_str(), data.renderUid);
642 HandleOnProcessCreated(data);
643 }
644
HandleOnChildProcessCreated(std::shared_ptr<ChildProcessRecord> childRecord)645 void AppStateObserverManager::HandleOnChildProcessCreated(std::shared_ptr<ChildProcessRecord> childRecord)
646 {
647 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
648 if (!childRecord) {
649 TAG_LOGE(AAFwkTag::APPMGR, "ChildProcessRecord record is nullptr.");
650 return;
651 }
652 ProcessData data;
653 if (WrapChildProcessData(data, childRecord) != ERR_OK) {
654 TAG_LOGE(AAFwkTag::APPMGR, "WrapChildProcessData failed.");
655 return;
656 }
657 TAG_LOGD(AAFwkTag::APPMGR,
658 "ChildProcess Create, bundleName:%{public}s, pid:%{public}d, uid:%{public}d, "
659 "processType:%{public}d, processName:%{public}s",
660 data.bundleName.c_str(), data.pid, data.uid, data.processType, data.processName.c_str());
661 HandleOnProcessCreated(data);
662 }
663
HandleOnProcessCreated(const ProcessData & data)664 void AppStateObserverManager::HandleOnProcessCreated(const ProcessData &data)
665 {
666 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
667 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
668 const auto &bundleNames = it->second.bundleNames;
669 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
670 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
671 it->first->OnProcessCreated(data);
672 }
673 }
674 }
675
HandleOnProcessStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord)676 void AppStateObserverManager::HandleOnProcessStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord)
677 {
678 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
679 if (!appRecord) {
680 TAG_LOGE(AAFwkTag::APPMGR, "app record is null");
681 return;
682 }
683 ProcessData data = WrapProcessData(appRecord);
684 if (data.bundleName == XIAOYI_BUNDLE_NAME && data.extensionType == ExtensionAbilityType::SERVICE) {
685 TAG_LOGI(AAFwkTag::APPMGR, "bundleName is com.huawei.chmos.vassistant, change processType to NORMAL");
686 data.processType = ProcessType::NORMAL;
687 }
688 TAG_LOGD(AAFwkTag::APPMGR,
689 "bundle:%{public}s, pid:%{public}d, uid:%{public}d, state:%{public}d, "
690 "isContinuousTask:%{public}d, gpuPid:%{public}d",
691 data.bundleName.c_str(), data.pid, data.uid, data.state, data.isContinuousTask, data.gpuPid);
692 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
693 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
694 const auto &bundleNames = it->second.bundleNames;
695 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
696 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
697 it->first->OnProcessStateChanged(data);
698 }
699 }
700 }
701
HandleOnAppProcessDied(const std::shared_ptr<AppRunningRecord> & appRecord)702 void AppStateObserverManager::HandleOnAppProcessDied(const std::shared_ptr<AppRunningRecord> &appRecord)
703 {
704 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
705 if (!appRecord) {
706 TAG_LOGE(AAFwkTag::APPMGR, "app record is null");
707 return;
708 }
709 ProcessData data = WrapProcessData(appRecord);
710 TAG_LOGD(AAFwkTag::APPMGR, "Process died, bundle:%{public}s, pid:%{public}d, uid:%{public}d, renderUid:%{public}d,"
711 " exitReason:%{public}d, exitMsg:%{public}s",
712 data.bundleName.c_str(), data.pid, data.uid, data.renderUid, data.exitReason, data.exitMsg.c_str());
713 HandleOnProcessDied(data);
714 }
715
HandleOnRenderProcessDied(const std::shared_ptr<RenderRecord> & renderRecord)716 void AppStateObserverManager::HandleOnRenderProcessDied(const std::shared_ptr<RenderRecord> &renderRecord)
717 {
718 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
719 if (!renderRecord) {
720 TAG_LOGE(AAFwkTag::APPMGR, "render record is null");
721 return;
722 }
723 ProcessData data = WrapRenderProcessData(renderRecord);
724 TAG_LOGD(AAFwkTag::APPMGR,
725 "Render Process died, bundle:%{public}s, pid:%{public}d, uid:%{public}d, renderUid:%{public}d",
726 data.bundleName.c_str(), data.pid, data.uid, data.renderUid);
727 HandleOnProcessDied(data);
728 }
729
HandleOnChildProcessDied(std::shared_ptr<ChildProcessRecord> childRecord)730 void AppStateObserverManager::HandleOnChildProcessDied(std::shared_ptr<ChildProcessRecord> childRecord)
731 {
732 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
733 if (!childRecord) {
734 TAG_LOGE(AAFwkTag::APPMGR, "childRecord is nullptr.");
735 return;
736 }
737 ProcessData data;
738 if (WrapChildProcessData(data, childRecord) != ERR_OK) {
739 TAG_LOGE(AAFwkTag::APPMGR, "WrapChildProcessData failed.");
740 return;
741 }
742 TAG_LOGD(AAFwkTag::APPMGR,
743 "ChildProcess died, bundleName:%{public}s, pid:%{public}d, uid:%{public}d, "
744 "processType:%{public}d, processName:%{public}s",
745 data.bundleName.c_str(), data.pid, data.uid, data.processType, data.processName.c_str());
746 HandleOnProcessDied(data);
747 }
748
HandleOnProcessDied(const ProcessData & data)749 void AppStateObserverManager::HandleOnProcessDied(const ProcessData &data)
750 {
751 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
752 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
753 const auto &bundleNames = it->second.bundleNames;
754 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
755 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
756 it->first->OnProcessDied(data);
757 }
758 }
759 }
760
WrapProcessData(const std::shared_ptr<AppRunningRecord> & appRecord)761 ProcessData AppStateObserverManager::WrapProcessData(const std::shared_ptr<AppRunningRecord> &appRecord)
762 {
763 ProcessData processData;
764 processData.bundleName = appRecord->GetBundleName();
765 processData.pid = appRecord->GetPriorityObject()->GetPid();
766 processData.uid = appRecord->GetUid();
767 auto applicationInfo = appRecord->GetApplicationInfo();
768 if (applicationInfo) {
769 processData.accessTokenId = applicationInfo->accessTokenId;
770 }
771 processData.state = static_cast<AppProcessState>(appRecord->GetState());
772 processData.isContinuousTask = appRecord->IsContinuousTask();
773 processData.isKeepAlive = appRecord->IsKeepAliveApp();
774 processData.isFocused = appRecord->GetFocusFlag();
775 processData.requestProcCode = appRecord->GetRequestProcCode();
776 processData.processChangeReason = static_cast<int32_t>(appRecord->GetProcessChangeReason());
777 processData.processName = appRecord->GetProcessName();
778 processData.extensionType = appRecord->GetExtensionType();
779 processData.processType = appRecord->GetProcessType();
780 if (appRecord->GetUserTestInfo() != nullptr && system::GetBoolParameter(DEVELOPER_MODE_STATE, false)) {
781 processData.isTestMode = true;
782 }
783 processData.exitReason = appRecord->GetExitReason();
784 processData.exitMsg = appRecord->GetExitMsg();
785 processData.gpuPid = appRecord->GetGPUPid();
786 processData.callerPid = appRecord->GetCallerPid();
787 processData.callerUid = appRecord->GetCallerUid();
788 return processData;
789 }
790
WrapRenderProcessData(const std::shared_ptr<RenderRecord> & renderRecord)791 ProcessData AppStateObserverManager::WrapRenderProcessData(const std::shared_ptr<RenderRecord> &renderRecord)
792 {
793 ProcessData processData;
794 processData.bundleName = renderRecord->GetHostBundleName();
795 processData.pid = renderRecord->GetPid();
796 processData.uid = renderRecord->GetHostUid();
797 processData.renderUid = renderRecord->GetUid();
798 processData.processName = renderRecord->GetProcessName();
799 processData.processType = renderRecord->GetProcessType();
800 processData.hostPid = renderRecord->GetHostPid();
801 return processData;
802 }
803
WrapChildProcessData(ProcessData & processData,std::shared_ptr<ChildProcessRecord> childRecord)804 int32_t AppStateObserverManager::WrapChildProcessData(ProcessData &processData,
805 std::shared_ptr<ChildProcessRecord> childRecord)
806 {
807 if (!childRecord) {
808 TAG_LOGE(AAFwkTag::APPMGR, "childRecord is nullptr.");
809 return ERR_INVALID_VALUE;
810 }
811 auto hostRecord = childRecord->GetHostRecord();
812 if (!hostRecord) {
813 TAG_LOGE(AAFwkTag::APPMGR, "hostRecord is nullptr.");
814 return ERR_INVALID_VALUE;
815 }
816 processData.bundleName = hostRecord->GetBundleName();
817 processData.uid = hostRecord->GetUid();
818 processData.hostPid = childRecord->GetHostPid();
819 processData.pid = childRecord->GetPid();
820 processData.childUid = childRecord->GetUid();
821 processData.processName = childRecord->GetProcessName();
822 processData.processType = childRecord->GetProcessType();
823 return ERR_OK;
824 }
825
ObserverExist(const sptr<IRemoteBroker> & observer)826 bool AppStateObserverManager::ObserverExist(const sptr<IRemoteBroker> &observer)
827 {
828 if (observer == nullptr) {
829 TAG_LOGE(AAFwkTag::APPMGR, "The param observer is nullptr.");
830 return false;
831 }
832 std::lock_guard lockRegister(observerLock_);
833 for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
834 if (it->first->AsObject() == observer->AsObject()) {
835 return true;
836 }
837 }
838 return false;
839 }
840
IsAbilityForegroundObserverExist(const sptr<IRemoteBroker> & observer)841 bool AppStateObserverManager::IsAbilityForegroundObserverExist(const sptr<IRemoteBroker> &observer)
842 {
843 if (observer == nullptr) {
844 TAG_LOGE(AAFwkTag::APPMGR, "The param observer is nullptr.");
845 return false;
846 }
847 std::lock_guard lockRegister(abilityForegroundObserverLock_);
848 for (const auto &[it, uid] : abilityForegroundObserverMap_) {
849 if (it != nullptr && it->AsObject() == observer->AsObject()) {
850 return true;
851 }
852 }
853 return false;
854 }
855
IsAppForegroundObserverExist(const sptr<IRemoteBroker> & observer)856 bool AppStateObserverManager::IsAppForegroundObserverExist(const sptr<IRemoteBroker> &observer)
857 {
858 if (observer == nullptr) {
859 TAG_LOGE(AAFwkTag::APPMGR, "The param observer is nullptr.");
860 return false;
861 }
862 std::lock_guard lockRegister(appForegroundObserverLock_);
863 for (const auto &[it, uid] : appForegroundStateObserverMap_) {
864 if (it != nullptr && it->AsObject() == observer->AsObject()) {
865 return true;
866 }
867 }
868 return false;
869 }
870
AddObserverDeathRecipient(const sptr<IRemoteBroker> & observer,const ObserverType & type)871 void AppStateObserverManager::AddObserverDeathRecipient(const sptr<IRemoteBroker> &observer, const ObserverType &type)
872 {
873 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
874 TAG_LOGD(AAFwkTag::APPMGR, "Add observer death recipient begin.");
875 if (observer == nullptr || observer->AsObject() == nullptr) {
876 TAG_LOGE(AAFwkTag::APPMGR, "The param observer is nullptr.");
877 return;
878 }
879 std::lock_guard lock(recipientMapMutex_);
880 auto it = recipientMap_.find(observer->AsObject());
881 if (it != recipientMap_.end()) {
882 TAG_LOGE(AAFwkTag::APPMGR, "This death recipient has been added.");
883 return;
884 } else {
885 std::weak_ptr<AppStateObserverManager> thisWeakPtr(shared_from_this());
886 sptr<IRemoteObject::DeathRecipient> deathRecipient = nullptr;
887 auto deathRecipientFunc = [thisWeakPtr, type](const wptr<IRemoteObject> &remote) {
888 auto appStateObserverManager = thisWeakPtr.lock();
889 if (appStateObserverManager) {
890 appStateObserverManager->OnObserverDied(remote, type);
891 }
892 };
893 if (type == ObserverType::APPLICATION_STATE_OBSERVER) {
894 deathRecipient = new (std::nothrow) ApplicationStateObserverRecipient(deathRecipientFunc);
895 } else if (type == ObserverType::APP_FOREGROUND_STATE_OBSERVER) {
896 deathRecipient = new (std::nothrow) AppForegroundStateObserverRecipient(deathRecipientFunc);
897 } else if (type == ObserverType::ABILITY_FOREGROUND_STATE_OBSERVER) {
898 deathRecipient = new (std::nothrow) AbilityForegroundStateObserverRecipient(deathRecipientFunc);
899 } else {
900 TAG_LOGW(AAFwkTag::APPMGR, "ObserverType is not exists");
901 return;
902 }
903 if (deathRecipient == nullptr) {
904 TAG_LOGE(AAFwkTag::APPMGR, "deathRecipient is nullptr.");
905 return;
906 }
907 if (!observer->AsObject()->AddDeathRecipient(deathRecipient)) {
908 TAG_LOGE(AAFwkTag::APPMGR, "AddDeathRecipient failed.");
909 }
910 recipientMap_.emplace(observer->AsObject(), deathRecipient);
911 }
912 }
913
RemoveObserverDeathRecipient(const sptr<IRemoteBroker> & observer)914 void AppStateObserverManager::RemoveObserverDeathRecipient(const sptr<IRemoteBroker> &observer)
915 {
916 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
917 TAG_LOGD(AAFwkTag::APPMGR, "Remove observer death recipient begin.");
918 if (observer == nullptr || observer->AsObject() == nullptr) {
919 TAG_LOGE(AAFwkTag::APPMGR, "The param observer is nullptr.");
920 return;
921 }
922 std::lock_guard lock(recipientMapMutex_);
923 auto it = recipientMap_.find(observer->AsObject());
924 if (it != recipientMap_.end()) {
925 it->first->RemoveDeathRecipient(it->second);
926 recipientMap_.erase(it);
927 return;
928 }
929 }
930
AddObserverCount(int32_t uid)931 void AppStateObserverManager::AddObserverCount(int32_t uid)
932 {
933 std::lock_guard lock(observerCountMapMutex_);
934 observerAmount_++;
935 auto it = observerCountMap_.find(uid);
936 if (it == observerCountMap_.end()) {
937 observerCountMap_.emplace(uid, 1);
938 } else {
939 it->second++;
940 if (it->second > OBSERVER_UID_COUNT_LOG) {
941 TAG_LOGW(AAFwkTag::APPMGR, "too many observer uid: %{public}d, count: %{public}d", uid, it->second);
942 }
943 if (observerAmount_ % OBSERVER_AMOUNT_COUNT_LOG == 0) {
944 for (const auto &[uid, count] : observerCountMap_) {
945 TAG_LOGW(AAFwkTag::APPMGR, "observer overview uid: %{public}d, count: %{public}d", uid, count);
946 }
947 }
948 }
949 }
950
DecreaseObserverCount(int32_t uid)951 void AppStateObserverManager::DecreaseObserverCount(int32_t uid)
952 {
953 std::lock_guard lock(observerCountMapMutex_);
954 auto it = observerCountMap_.find(uid);
955 if (it == observerCountMap_.end()) {
956 return;
957 }
958 it->second--;
959 if (it->second <= 0) {
960 observerCountMap_.erase(it);
961 }
962 observerAmount_--;
963 }
964
GetAppStateObserverMapCopy()965 AppStateObserverMap AppStateObserverManager::GetAppStateObserverMapCopy()
966 {
967 std::lock_guard lock(observerLock_);
968 return appStateObserverMap_;
969 }
970
GetAppForegroundStateObserverMapCopy()971 AppForegroundStateObserverMap AppStateObserverManager::GetAppForegroundStateObserverMapCopy()
972 {
973 std::lock_guard lock(appForegroundObserverLock_);
974 return appForegroundStateObserverMap_;
975 }
976
GetAbilityForegroundObserverMapCopy()977 AbilityForegroundObserverMap AppStateObserverManager::GetAbilityForegroundObserverMapCopy()
978 {
979 std::lock_guard lock(abilityForegroundObserverLock_);
980 return abilityForegroundObserverMap_;
981 }
982
OnObserverDied(const wptr<IRemoteObject> & remote,const ObserverType & type)983 void AppStateObserverManager::OnObserverDied(const wptr<IRemoteObject> &remote, const ObserverType &type)
984 {
985 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
986 TAG_LOGD(AAFwkTag::APPMGR, "OnObserverDied");
987 auto object = remote.promote();
988 if (object == nullptr) {
989 TAG_LOGE(AAFwkTag::APPMGR, "observer nullptr.");
990 return;
991 }
992
993 if (type == ObserverType::APPLICATION_STATE_OBSERVER) {
994 sptr<IApplicationStateObserver> observer = iface_cast<IApplicationStateObserver>(object);
995 UnregisterApplicationStateObserver(observer);
996 } else if (type == ObserverType::ABILITY_FOREGROUND_STATE_OBSERVER) {
997 sptr<IAbilityForegroundStateObserver> observer = iface_cast<IAbilityForegroundStateObserver>(object);
998 UnregisterAbilityForegroundStateObserver(observer);
999 } else if (type == ObserverType::APP_FOREGROUND_STATE_OBSERVER) {
1000 sptr<IAppForegroundStateObserver> observer = iface_cast<IAppForegroundStateObserver>(object);
1001 UnregisterAppForegroundStateObserver(observer);
1002 } else {
1003 TAG_LOGW(AAFwkTag::APPMGR, "ObserverType is not exists");
1004 return;
1005 }
1006 }
1007
WrapAppStateData(const std::shared_ptr<AppRunningRecord> & appRecord,const ApplicationState state)1008 AppStateData AppStateObserverManager::WrapAppStateData(const std::shared_ptr<AppRunningRecord> &appRecord,
1009 const ApplicationState state)
1010 {
1011 AppStateData appStateData;
1012 appStateData.pid = appRecord->GetPriorityObject()->GetPid();
1013 appStateData.bundleName = appRecord->GetBundleName();
1014 appStateData.state = static_cast<int32_t>(state);
1015 appStateData.uid = appRecord->GetUid();
1016 appStateData.extensionType = appRecord->GetExtensionType();
1017 appStateData.isPreloadModule = appRecord->GetNeedLimitPrio();
1018 if (appRecord->GetApplicationInfo() != nullptr) {
1019 appStateData.accessTokenId = static_cast<uint32_t>(appRecord->GetApplicationInfo()->accessTokenId);
1020 }
1021 appStateData.isFocused = appRecord->GetFocusFlag();
1022 auto renderRecordMap = appRecord->GetRenderRecordMap();
1023 if (!renderRecordMap.empty()) {
1024 for (auto iter : renderRecordMap) {
1025 auto renderRecord = iter.second;
1026 if (renderRecord != nullptr) {
1027 appStateData.renderPids.emplace_back(renderRecord->GetPid());
1028 }
1029 }
1030 }
1031 std::shared_ptr<RemoteClientManager> remoteClientManager = std::make_shared<RemoteClientManager>();
1032 auto bundleMgr = remoteClientManager->GetBundleManagerHelper();
1033 std::string callerBundleName;
1034 if (bundleMgr != nullptr &&
1035 IN_PROCESS_CALL(bundleMgr->GetNameForUid(appRecord->GetCallerUid(), callerBundleName)) == ERR_OK) {
1036 appStateData.callerBundleName = callerBundleName;
1037 } else {
1038 appStateData.callerBundleName = "";
1039 }
1040 appStateData.appIndex = appRecord->GetAppIndex();
1041 TAG_LOGD(AAFwkTag::APPMGR, "Handle state change, bundle:%{public}s, state:%{public}d,"
1042 "pid:%{public}d ,uid:%{public}d, isFocused:%{public}d, callerBUndleName: %{public}s, appIndex:%{public}d",
1043 appStateData.bundleName.c_str(), appStateData.state, appStateData.pid, appStateData.uid,
1044 appStateData.isFocused, appStateData.callerBundleName.c_str(), appStateData.appIndex);
1045 return appStateData;
1046 }
1047
OnPageShow(const PageStateData pageStateData)1048 void AppStateObserverManager::OnPageShow(const PageStateData pageStateData)
1049 {
1050 TAG_LOGD(AAFwkTag::APPMGR, "call");
1051 if (handler_ == nullptr) {
1052 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnProcessCreated failed.");
1053 return;
1054 }
1055
1056 auto task = [weak = weak_from_this(), pageStateData]() {
1057 auto self = weak.lock();
1058 if (self == nullptr) {
1059 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnProcessCreated failed.");
1060 return;
1061 }
1062 TAG_LOGD(AAFwkTag::APPMGR, "OnProcessCreated come.");
1063 self->HandleOnPageShow(pageStateData);
1064 };
1065 handler_->SubmitTask(task);
1066 }
1067
OnPageHide(const PageStateData pageStateData)1068 void AppStateObserverManager::OnPageHide(const PageStateData pageStateData)
1069 {
1070 TAG_LOGD(AAFwkTag::APPMGR, "call");
1071 if (handler_ == nullptr) {
1072 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnProcessCreated failed.");
1073 return;
1074 }
1075
1076 auto task = [weak = weak_from_this(), pageStateData]() {
1077 auto self = weak.lock();
1078 if (self == nullptr) {
1079 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnProcessCreated failed.");
1080 return;
1081 }
1082 TAG_LOGD(AAFwkTag::APPMGR, "OnProcessCreated come.");
1083 self->HandleOnPageHide(pageStateData);
1084 };
1085 handler_->SubmitTask(task);
1086 }
1087
HandleOnPageShow(const PageStateData pageStateData)1088 void AppStateObserverManager::HandleOnPageShow(const PageStateData pageStateData)
1089 {
1090 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1091 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
1092 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
1093 const auto &bundleNames = it->second.bundleNames;
1094 auto iter = std::find(bundleNames.begin(), bundleNames.end(), pageStateData.bundleName);
1095 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
1096 it->first->OnPageShow(pageStateData);
1097 }
1098 }
1099 }
1100
HandleOnPageHide(const PageStateData pageStateData)1101 void AppStateObserverManager::HandleOnPageHide(const PageStateData pageStateData)
1102 {
1103 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1104 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
1105 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
1106 const auto &bundleNames = it->second.bundleNames;
1107 auto iter = std::find(bundleNames.begin(), bundleNames.end(), pageStateData.bundleName);
1108 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
1109 it->first->OnPageHide(pageStateData);
1110 }
1111 }
1112 }
1113
OnAppCacheStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord,ApplicationState state)1114 void AppStateObserverManager::OnAppCacheStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord,
1115 ApplicationState state)
1116 {
1117 if (handler_ == nullptr) {
1118 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnAppCacheStateChanged failed.");
1119 return;
1120 }
1121
1122 auto task = [weak = weak_from_this(), appRecord, state]() {
1123 auto self = weak.lock();
1124 if (self == nullptr) {
1125 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnAppCacheStateChanged failed.");
1126 return;
1127 }
1128 TAG_LOGD(AAFwkTag::APPMGR, "OnAppCacheStateChanged come.");
1129 self->HandleOnAppCacheStateChanged(appRecord, state);
1130 };
1131 handler_->SubmitTask(task);
1132 }
1133
HandleOnAppCacheStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord,ApplicationState state)1134 void AppStateObserverManager::HandleOnAppCacheStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord,
1135 ApplicationState state)
1136 {
1137 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1138 if (appRecord == nullptr) {
1139 TAG_LOGE(AAFwkTag::APPMGR, "app record is null");
1140 return;
1141 }
1142
1143 AppStateData data = WrapAppStateData(appRecord, state);
1144 data.isSpecifyTokenId = appRecord->GetAssignTokenId() > 0 ? true : false;
1145 TAG_LOGD(AAFwkTag::APPMGR, "HandleOnAppCacheStateChanged, bundle:%{public}s, uid:%{public}d, state:%{public}d",
1146 data.bundleName.c_str(), data.uid, data.state);
1147 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
1148 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
1149 const auto &bundleNames = it->second.bundleNames;
1150 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
1151 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
1152 it->first->OnAppCacheStateChanged(data);
1153 }
1154 }
1155 }
1156 } // namespace AppExecFwk
1157 } // namespace OHOS
1158