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, "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, "verification failed");
66 return ERR_PERMISSION_DENIED;
67 }
68 if (observer == nullptr) {
69 TAG_LOGE(AAFwkTag::APPMGR, "null observer");
70 return ERR_INVALID_VALUE;
71 }
72 if (ObserverExist(observer)) {
73 TAG_LOGW(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, "verification failed");
93 return ERR_PERMISSION_DENIED;
94 }
95 std::lock_guard lockUnregister(observerLock_);
96 if (observer == nullptr) {
97 TAG_LOGE(AAFwkTag::APPMGR, "null observer");
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, "null observer");
119 return ERR_INVALID_VALUE;
120 }
121 if (AAFwk::PermissionVerification::GetInstance()->VerifyAppStateObserverPermission() == ERR_PERMISSION_DENIED) {
122 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
123 return ERR_PERMISSION_DENIED;
124 }
125 if (IsAppForegroundObserverExist(observer)) {
126 TAG_LOGW(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, "null observer");
147 return ERR_INVALID_VALUE;
148 }
149 if (AAFwk::PermissionVerification::GetInstance()->VerifyAppStateObserverPermission() == ERR_PERMISSION_DENIED) {
150 TAG_LOGE(AAFwkTag::APPMGR, "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, "null observer");
172 return ERR_INVALID_VALUE;
173 }
174 if (AAFwk::PermissionVerification::GetInstance()->VerifyAppStateObserverPermission() == ERR_PERMISSION_DENIED) {
175 TAG_LOGE(AAFwkTag::APPMGR, "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, "null observer");
201 return ERR_INVALID_VALUE;
202 }
203 if (AAFwk::PermissionVerification::GetInstance()->VerifyAppStateObserverPermission() == ERR_PERMISSION_DENIED) {
204 TAG_LOGE(AAFwkTag::APPMGR, "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, "null handler");
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, "null self");
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, "null handler");
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, "null self");
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, "null handler");
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, "null self");
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, "null handler");
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, "null self");
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, "null handler");
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, "null self");
311 return;
312 }
313 TAG_LOGD(AAFwkTag::APPMGR, "OnRenderProcessDied come.");
314 self->HandleOnRenderProcessDied(renderRecord);
315 };
316 handler_->SubmitTask(task);
317 }
318
319 #ifdef SUPPORT_CHILD_PROCESS
OnChildProcessDied(std::shared_ptr<ChildProcessRecord> childRecord)320 void AppStateObserverManager::OnChildProcessDied(std::shared_ptr<ChildProcessRecord> childRecord)
321 {
322 if (handler_ == nullptr) {
323 TAG_LOGE(AAFwkTag::APPMGR, "null handler");
324 return;
325 }
326
327 auto task = [weak = weak_from_this(), childRecord]() {
328 auto self = weak.lock();
329 if (self == nullptr) {
330 TAG_LOGE(AAFwkTag::APPMGR, "null self");
331 return;
332 }
333 TAG_LOGD(AAFwkTag::APPMGR, "OnChildProcessDied come.");
334 self->HandleOnChildProcessDied(childRecord);
335 };
336 handler_->SubmitTask(task);
337 }
338 #endif // SUPPORT_CHILD_PROCESS
339
OnProcessStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord,bool isFromWindowFocusChanged)340 void AppStateObserverManager::OnProcessStateChanged(
341 const std::shared_ptr<AppRunningRecord> &appRecord, bool isFromWindowFocusChanged)
342 {
343 if (handler_ == nullptr) {
344 TAG_LOGE(AAFwkTag::APPMGR, "null handler");
345 return;
346 }
347
348 auto task = [weak = weak_from_this(), appRecord, isFromWindowFocusChanged]() {
349 auto self = weak.lock();
350 if (self == nullptr) {
351 TAG_LOGE(AAFwkTag::APPMGR, "null self");
352 return;
353 }
354 TAG_LOGD(AAFwkTag::APPMGR, "OnProcessStateChanged come.");
355 self->HandleOnProcessStateChanged(appRecord, isFromWindowFocusChanged);
356 };
357 handler_->SubmitTask(task);
358 }
359
OnWindowShow(const std::shared_ptr<AppRunningRecord> & appRecord)360 void AppStateObserverManager::OnWindowShow(const std::shared_ptr<AppRunningRecord> &appRecord)
361 {
362 if (handler_ == nullptr) {
363 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnWindowShow failed.");
364 return;
365 }
366
367 auto task = [weak = weak_from_this(), appRecord]() {
368 auto self = weak.lock();
369 if (self == nullptr) {
370 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnWindowShow failed.");
371 return;
372 }
373 TAG_LOGD(AAFwkTag::APPMGR, "OnWindowShow come.");
374 self->HandleOnWindowShow(appRecord);
375 };
376 handler_->SubmitTask(task);
377 }
378
OnWindowHidden(const std::shared_ptr<AppRunningRecord> & appRecord)379 void AppStateObserverManager::OnWindowHidden(const std::shared_ptr<AppRunningRecord> &appRecord)
380 {
381 if (handler_ == nullptr) {
382 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnWindowHidden failed.");
383 return;
384 }
385
386 auto task = [weak = weak_from_this(), appRecord]() {
387 auto self = weak.lock();
388 if (self == nullptr) {
389 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnWindowHidden failed.");
390 return;
391 }
392 TAG_LOGD(AAFwkTag::APPMGR, "OnWindowHidden come.");
393 self->HandleOnWindowHidden(appRecord);
394 };
395 handler_->SubmitTask(task);
396 }
397
OnProcessCreated(const std::shared_ptr<AppRunningRecord> & appRecord,bool isPreload)398 void AppStateObserverManager::OnProcessCreated(const std::shared_ptr<AppRunningRecord> &appRecord, bool isPreload)
399 {
400 if (handler_ == nullptr) {
401 TAG_LOGE(AAFwkTag::APPMGR, "null handler");
402 return;
403 }
404
405 auto task = [weak = weak_from_this(), appRecord, isPreload]() {
406 auto self = weak.lock();
407 if (self == nullptr) {
408 TAG_LOGE(AAFwkTag::APPMGR, "null self");
409 return;
410 }
411 self->HandleOnAppProcessCreated(appRecord, isPreload);
412 };
413 handler_->SubmitTask(task);
414 }
415
OnProcessReused(const std::shared_ptr<AppRunningRecord> & appRecord)416 void AppStateObserverManager::OnProcessReused(const std::shared_ptr<AppRunningRecord> &appRecord)
417 {
418 if (handler_ == nullptr) {
419 TAG_LOGE(AAFwkTag::APPMGR, "null handler");
420 return;
421 }
422
423 auto task = [weak = weak_from_this(), appRecord]() {
424 auto self = weak.lock();
425 if (self == nullptr) {
426 TAG_LOGE(AAFwkTag::APPMGR, "null self");
427 return;
428 }
429 TAG_LOGD(AAFwkTag::APPMGR, "OnProcessReused come.");
430 self->HandleOnProcessResued(appRecord);
431 };
432 handler_->SubmitTask(task);
433 }
434
OnRenderProcessCreated(const std::shared_ptr<RenderRecord> & renderRecord,const bool isPreload)435 void AppStateObserverManager::OnRenderProcessCreated(const std::shared_ptr<RenderRecord> &renderRecord,
436 const bool isPreload)
437 {
438 if (handler_ == nullptr) {
439 TAG_LOGE(AAFwkTag::APPMGR, "null handler");
440 return;
441 }
442
443 auto task = [weak = weak_from_this(), renderRecord, isPreload]() {
444 auto self = weak.lock();
445 if (self == nullptr) {
446 TAG_LOGE(AAFwkTag::APPMGR, "null self");
447 return;
448 }
449 TAG_LOGD(AAFwkTag::APPMGR, "OnRenderProcessCreated come.");
450 self->HandleOnRenderProcessCreated(renderRecord, isPreload);
451 };
452 handler_->SubmitTask(task);
453 }
454
455 #ifdef SUPPORT_CHILD_PROCESS
OnChildProcessCreated(std::shared_ptr<ChildProcessRecord> childRecord)456 void AppStateObserverManager::OnChildProcessCreated(std::shared_ptr<ChildProcessRecord> childRecord)
457 {
458 if (handler_ == nullptr) {
459 TAG_LOGE(AAFwkTag::APPMGR, "null handler");
460 return;
461 }
462
463 auto task = [weak = weak_from_this(), childRecord]() {
464 auto self = weak.lock();
465 if (self == nullptr) {
466 TAG_LOGE(AAFwkTag::APPMGR, "null self");
467 return;
468 }
469 TAG_LOGD(AAFwkTag::APPMGR, "OnChildProcessCreated come.");
470 self->HandleOnChildProcessCreated(childRecord);
471 };
472 handler_->SubmitTask(task);
473 }
474 #endif // SUPPORT_CHILD_PROCESS
475
StateChangedNotifyObserver(const AbilityStateData abilityStateData,bool isAbility,bool isFromWindowFocusChanged)476 void AppStateObserverManager::StateChangedNotifyObserver(
477 const AbilityStateData abilityStateData, bool isAbility, bool isFromWindowFocusChanged)
478 {
479 if (handler_ == nullptr) {
480 TAG_LOGE(AAFwkTag::APPMGR, "null handler");
481 return;
482 }
483
484 auto task = [weak = weak_from_this(), abilityStateData, isAbility, isFromWindowFocusChanged]() {
485 auto self = weak.lock();
486 if (self == nullptr) {
487 TAG_LOGE(AAFwkTag::APPMGR, "null self");
488 return;
489 }
490 TAG_LOGD(AAFwkTag::APPMGR, "StateChangedNotifyObserver come.");
491 self->HandleStateChangedNotifyObserver(abilityStateData, isAbility, isFromWindowFocusChanged);
492 };
493 handler_->SubmitTask(task);
494 }
495
HandleOnAppStarted(const std::shared_ptr<AppRunningRecord> & appRecord)496 void AppStateObserverManager::HandleOnAppStarted(const std::shared_ptr<AppRunningRecord> &appRecord)
497 {
498 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
499 if (appRecord == nullptr) {
500 TAG_LOGE(AAFwkTag::APPMGR, "null appRecord");
501 return;
502 }
503
504 AppStateData data = WrapAppStateData(appRecord, ApplicationState::APP_STATE_CREATE);
505 data.isSpecifyTokenId = appRecord->GetAssignTokenId() > 0 ? true : false;
506 TAG_LOGD(AAFwkTag::APPMGR, "HandleOnAppStarted, bundle:%{public}s, uid:%{public}d, state:%{public}d",
507 data.bundleName.c_str(), data.uid, data.state);
508 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
509 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
510 const auto &bundleNames = it->second.bundleNames;
511 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
512 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
513 it->first->OnAppStarted(data);
514 }
515 }
516 }
517
HandleOnAppStopped(const std::shared_ptr<AppRunningRecord> & appRecord)518 void AppStateObserverManager::HandleOnAppStopped(const std::shared_ptr<AppRunningRecord> &appRecord)
519 {
520 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
521 if (appRecord == nullptr) {
522 TAG_LOGE(AAFwkTag::APPMGR, "null appRecord");
523 return;
524 }
525
526 AppStateData data = WrapAppStateData(appRecord, ApplicationState::APP_STATE_TERMINATED);
527 TAG_LOGD(AAFwkTag::APPMGR, "HandleOnAppStopped, bundle:%{public}s, uid:%{public}d, state:%{public}d",
528 data.bundleName.c_str(), data.uid, data.state);
529 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
530 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
531 const auto &bundleNames = it->second.bundleNames;
532 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
533 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
534 it->first->OnAppStopped(data);
535 }
536 }
537 }
538
HandleAppStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord,const ApplicationState state,bool needNotifyApp,bool isFromWindowFocusChanged)539 void AppStateObserverManager::HandleAppStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord,
540 const ApplicationState state, bool needNotifyApp, bool isFromWindowFocusChanged)
541 {
542 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
543 if (appRecord == nullptr) {
544 return;
545 }
546 if (state == ApplicationState::APP_STATE_FOREGROUND || state == ApplicationState::APP_STATE_BACKGROUND) {
547 if (needNotifyApp && !isFromWindowFocusChanged) {
548 AppStateData data = WrapAppStateData(appRecord, state, isFromWindowFocusChanged);
549 appRecord->GetSplitModeAndFloatingMode(data.isSplitScreenMode, data.isFloatingWindowMode);
550 auto appForegroundStateObserverMap = GetAppForegroundStateObserverMapCopy();
551 for (const auto &[observer, uid] : appForegroundStateObserverMap) {
552 if (observer != nullptr) {
553 observer->OnAppStateChanged(data);
554 }
555 }
556 }
557 if (!AAFwk::UIExtensionUtils::IsUIExtension(appRecord->GetExtensionType()) &&
558 !AAFwk::UIExtensionUtils::IsWindowExtension(appRecord->GetExtensionType())) {
559 AppStateData data = WrapAppStateData(appRecord, state, isFromWindowFocusChanged);
560 TAG_LOGD(AAFwkTag::APPMGR, "name:%{public}s, uid:%{public}d, state:%{public}d, notify:%{public}d",
561 data.bundleName.c_str(), data.uid, data.state, needNotifyApp);
562 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
563 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
564 const auto &bundleNames = it->second.bundleNames;
565 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
566 bool valid = (bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr;
567 if (valid) {
568 it->first->OnForegroundApplicationChanged(data);
569 }
570 if (valid && needNotifyApp) {
571 it->first->OnAppStateChanged(data);
572 }
573 }
574 }
575 }
576 if (state == ApplicationState::APP_STATE_CREATE || state == ApplicationState::APP_STATE_TERMINATED) {
577 AppStateData data = WrapAppStateData(appRecord, state, isFromWindowFocusChanged);
578 TAG_LOGD(AAFwkTag::APPMGR, "OnApplicationStateChanged, name:%{public}s, uid:%{public}d, state:%{public}d",
579 data.bundleName.c_str(), data.uid, data.state);
580 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
581 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
582 const auto &bundleNames = it->second.bundleNames;
583 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
584 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
585 it->first->OnApplicationStateChanged(data);
586 }
587 }
588 }
589 }
590
HandleStateChangedNotifyObserver(const AbilityStateData abilityStateData,bool isAbility,bool isFromWindowFocusChanged)591 void AppStateObserverManager::HandleStateChangedNotifyObserver(
592 const AbilityStateData abilityStateData, bool isAbility, bool isFromWindowFocusChanged)
593 {
594 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
595 TAG_LOGD(AAFwkTag::APPMGR,
596 "Handle state change, module:%{public}s, bundle:%{public}s, ability:%{public}s, state:%{public}d,"
597 "pid:%{public}d ,uid:%{public}d, abilityType:%{public}d, isAbility:%{public}d, callerBundleName:%{public}s,"
598 "callerAbilityName:%{public}s, isAtomicService:%{public}d, callerUid:%{public}d",
599 abilityStateData.moduleName.c_str(), abilityStateData.bundleName.c_str(),
600 abilityStateData.abilityName.c_str(), abilityStateData.abilityState,
601 abilityStateData.pid, abilityStateData.uid, abilityStateData.abilityType, isAbility,
602 abilityStateData.callerBundleName.c_str(), abilityStateData.callerAbilityName.c_str(),
603 abilityStateData.isAtomicService, abilityStateData.callerUid);
604 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
605 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
606 const auto &bundleNames = it->second.bundleNames;
607 auto iter = std::find(bundleNames.begin(), bundleNames.end(), abilityStateData.bundleName);
608 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
609 if (isAbility) {
610 it->first->OnAbilityStateChanged(abilityStateData);
611 } else {
612 it->first->OnExtensionStateChanged(abilityStateData);
613 }
614 }
615 }
616
617 if ((abilityStateData.abilityState == static_cast<int32_t>(AbilityState::ABILITY_STATE_FOREGROUND) ||
618 abilityStateData.abilityState == static_cast<int32_t>(AbilityState::ABILITY_STATE_BACKGROUND)) &&
619 isAbility && !isFromWindowFocusChanged) {
620 auto abilityForegroundObserverMap = GetAbilityForegroundObserverMapCopy();
621 for (auto &[observer, uid] : abilityForegroundObserverMap) {
622 if (observer != nullptr) {
623 observer->OnAbilityStateChanged(abilityStateData);
624 }
625 }
626 }
627 }
628
HandleOnAppProcessCreated(const std::shared_ptr<AppRunningRecord> & appRecord,bool isPreload)629 void AppStateObserverManager::HandleOnAppProcessCreated(const std::shared_ptr<AppRunningRecord> &appRecord,
630 bool isPreload)
631 {
632 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
633 if (!appRecord) {
634 TAG_LOGE(AAFwkTag::APPMGR, "null appRecord");
635 return;
636 }
637 ProcessData data = WrapProcessData(appRecord);
638 data.isPreload = isPreload;
639 data.isPreloadModule = appRecord->GetPreloadMode() != PreloadMode::PRESS_DOWN;
640 if (data.bundleName == XIAOYI_BUNDLE_NAME && data.extensionType == ExtensionAbilityType::SERVICE) {
641 TAG_LOGI(AAFwkTag::APPMGR, "change processType to NORMAL");
642 data.processType = ProcessType::NORMAL;
643 }
644 TAG_LOGI(AAFwkTag::APPMGR,
645 "bundle:%{public}s, pid:%{public}d, uid:%{public}d, processType:%{public}d, "
646 "extensionType:%{public}d, processName:%{public}s, renderUid:%{public}d, isTestMode:%{public}d, "
647 "callerPid:%{public}d, callerUid:%{public}d",
648 data.bundleName.c_str(), data.pid, data.uid, data.processType, data.extensionType, data.processName.c_str(),
649 data.renderUid, data.isTestMode, data.callerPid, data.callerUid);
650 HandleOnProcessCreated(data);
651 }
652
HandleOnProcessResued(const std::shared_ptr<AppRunningRecord> & appRecord)653 void AppStateObserverManager::HandleOnProcessResued(const std::shared_ptr<AppRunningRecord> &appRecord)
654 {
655 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
656 if (!appRecord) {
657 TAG_LOGE(AAFwkTag::APPMGR, "null appRecord");
658 return;
659 }
660 ProcessData data = WrapProcessData(appRecord);
661 TAG_LOGD(AAFwkTag::APPMGR, "Process Resued, bundle:%{public}s, pid:%{public}d, uid:%{public}d",
662 data.bundleName.c_str(), data.pid, data.uid);
663
664 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
665 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
666 const auto &bundleNames = it->second.bundleNames;
667 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
668 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
669 it->first->OnProcessReused(data);
670 }
671 }
672 }
673
HandleOnRenderProcessCreated(const std::shared_ptr<RenderRecord> & renderRecord,const bool isPreload)674 void AppStateObserverManager::HandleOnRenderProcessCreated(const std::shared_ptr<RenderRecord> &renderRecord,
675 const bool isPreload)
676 {
677 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
678 if (!renderRecord) {
679 TAG_LOGE(AAFwkTag::APPMGR, "null renderRecord");
680 return;
681 }
682 ProcessData data = WrapRenderProcessData(renderRecord);
683 data.isPreload = isPreload;
684 TAG_LOGD(AAFwkTag::APPMGR,
685 "RenderProcess Create, bundle:%{public}s, pid:%{public}d, uid:%{public}d, processType:%{public}d, "
686 "processName:%{public}s, renderUid:%{public}d",
687 data.bundleName.c_str(), data.pid, data.uid, data.processType, data.processName.c_str(), data.renderUid);
688 HandleOnProcessCreated(data);
689 }
690
691 #ifdef SUPPORT_CHILD_PROCESS
HandleOnChildProcessCreated(std::shared_ptr<ChildProcessRecord> childRecord)692 void AppStateObserverManager::HandleOnChildProcessCreated(std::shared_ptr<ChildProcessRecord> childRecord)
693 {
694 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
695 if (!childRecord) {
696 TAG_LOGE(AAFwkTag::APPMGR, "null childRecord");
697 return;
698 }
699 ProcessData data;
700 if (WrapChildProcessData(data, childRecord) != ERR_OK) {
701 TAG_LOGE(AAFwkTag::APPMGR, "WrapChildProcessData failed");
702 return;
703 }
704 TAG_LOGD(AAFwkTag::APPMGR,
705 "ChildProcess Create, bundleName:%{public}s, pid:%{public}d, uid:%{public}d, "
706 "processType:%{public}d, processName:%{public}s",
707 data.bundleName.c_str(), data.pid, data.uid, data.processType, data.processName.c_str());
708 HandleOnProcessCreated(data);
709 }
710 #endif // SUPPORT_CHILD_PROCESS
711
HandleOnProcessCreated(const ProcessData & data)712 void AppStateObserverManager::HandleOnProcessCreated(const ProcessData &data)
713 {
714 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
715 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
716 const auto &bundleNames = it->second.bundleNames;
717 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
718 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
719 it->first->OnProcessCreated(data);
720 }
721 }
722 }
723
HandleOnProcessStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord,bool isFromWindowFocusChanged)724 void AppStateObserverManager::HandleOnProcessStateChanged(
725 const std::shared_ptr<AppRunningRecord> &appRecord, bool isFromWindowFocusChanged)
726 {
727 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
728 if (!appRecord) {
729 TAG_LOGE(AAFwkTag::APPMGR, "null appRecord");
730 return;
731 }
732 ProcessData data = WrapProcessData(appRecord, isFromWindowFocusChanged);
733 if (data.bundleName == XIAOYI_BUNDLE_NAME && data.extensionType == ExtensionAbilityType::SERVICE) {
734 TAG_LOGI(AAFwkTag::APPMGR, "change processType to NORMAL");
735 data.processType = ProcessType::NORMAL;
736 }
737 TAG_LOGD(AAFwkTag::APPMGR,
738 "bundle:%{public}s, pid:%{public}d, uid:%{public}d, state:%{public}d, "
739 "isContinuousTask:%{public}d, gpuPid:%{public}d",
740 data.bundleName.c_str(), data.pid, data.uid, data.state, data.isContinuousTask, data.gpuPid);
741 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
742 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
743 const auto &bundleNames = it->second.bundleNames;
744 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
745 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
746 it->first->OnProcessStateChanged(data);
747 }
748 }
749 }
750
HandleOnWindowShow(const std::shared_ptr<AppRunningRecord> & appRecord)751 void AppStateObserverManager::HandleOnWindowShow(const std::shared_ptr<AppRunningRecord> &appRecord)
752 {
753 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
754 if (!appRecord) {
755 TAG_LOGE(AAFwkTag::APPMGR, "app record is null");
756 return;
757 }
758 ProcessData data = WrapProcessData(appRecord);
759 TAG_LOGD(AAFwkTag::APPMGR,
760 "bundle:%{public}s, pid:%{public}d, uid:%{public}d, state:%{public}d, "
761 "isContinuousTask:%{public}d, gpuPid:%{public}d",
762 data.bundleName.c_str(), data.pid, data.uid, data.state, data.isContinuousTask, data.gpuPid);
763 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
764 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
765 const auto &bundleNames = it->second.bundleNames;
766 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
767 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
768 it->first->OnWindowShow(data);
769 }
770 }
771 }
772
HandleOnWindowHidden(const std::shared_ptr<AppRunningRecord> & appRecord)773 void AppStateObserverManager::HandleOnWindowHidden(const std::shared_ptr<AppRunningRecord> &appRecord)
774 {
775 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
776 if (!appRecord) {
777 TAG_LOGE(AAFwkTag::APPMGR, "app record is null");
778 return;
779 }
780 ProcessData data = WrapProcessData(appRecord);
781 TAG_LOGD(AAFwkTag::APPMGR,
782 "bundle:%{public}s, pid:%{public}d, uid:%{public}d, state:%{public}d, "
783 "isContinuousTask:%{public}d, gpuPid:%{public}d",
784 data.bundleName.c_str(), data.pid, data.uid, data.state, data.isContinuousTask, data.gpuPid);
785 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
786 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
787 const auto &bundleNames = it->second.bundleNames;
788 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
789 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
790 it->first->OnWindowHidden(data);
791 }
792 }
793 }
794
HandleOnAppProcessDied(const std::shared_ptr<AppRunningRecord> & appRecord)795 void AppStateObserverManager::HandleOnAppProcessDied(const std::shared_ptr<AppRunningRecord> &appRecord)
796 {
797 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
798 if (!appRecord) {
799 TAG_LOGE(AAFwkTag::APPMGR, "null appRecord");
800 return;
801 }
802 ProcessData data = WrapProcessData(appRecord);
803 TAG_LOGD(AAFwkTag::APPMGR, "Process died, bundle:%{public}s, pid:%{public}d, uid:%{public}d, renderUid:%{public}d,"
804 " exitReason:%{public}d, exitMsg:%{public}s",
805 data.bundleName.c_str(), data.pid, data.uid, data.renderUid, data.exitReason, data.exitMsg.c_str());
806 HandleOnProcessDied(data);
807 }
808
HandleOnRenderProcessDied(const std::shared_ptr<RenderRecord> & renderRecord)809 void AppStateObserverManager::HandleOnRenderProcessDied(const std::shared_ptr<RenderRecord> &renderRecord)
810 {
811 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
812 if (!renderRecord) {
813 TAG_LOGE(AAFwkTag::APPMGR, "null renderRecord");
814 return;
815 }
816 ProcessData data = WrapRenderProcessData(renderRecord);
817 TAG_LOGD(AAFwkTag::APPMGR,
818 "Render Process died, bundle:%{public}s, pid:%{public}d, uid:%{public}d, renderUid:%{public}d",
819 data.bundleName.c_str(), data.pid, data.uid, data.renderUid);
820 HandleOnProcessDied(data);
821 }
822
823 #ifdef SUPPORT_CHILD_PROCESS
HandleOnChildProcessDied(std::shared_ptr<ChildProcessRecord> childRecord)824 void AppStateObserverManager::HandleOnChildProcessDied(std::shared_ptr<ChildProcessRecord> childRecord)
825 {
826 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
827 if (!childRecord) {
828 TAG_LOGE(AAFwkTag::APPMGR, "null childRecord");
829 return;
830 }
831 ProcessData data;
832 if (WrapChildProcessData(data, childRecord) != ERR_OK) {
833 TAG_LOGE(AAFwkTag::APPMGR, "WrapChildProcessData failed");
834 return;
835 }
836 TAG_LOGD(AAFwkTag::APPMGR,
837 "ChildProcess died, bundleName:%{public}s, pid:%{public}d, uid:%{public}d, "
838 "processType:%{public}d, processName:%{public}s",
839 data.bundleName.c_str(), data.pid, data.uid, data.processType, data.processName.c_str());
840 HandleOnProcessDied(data);
841 }
842 #endif // SUPPORT_CHILD_PROCESS
843
HandleOnProcessDied(const ProcessData & data)844 void AppStateObserverManager::HandleOnProcessDied(const ProcessData &data)
845 {
846 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
847 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
848 const auto &bundleNames = it->second.bundleNames;
849 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
850 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
851 it->first->OnProcessDied(data);
852 }
853 }
854 }
855
WrapProcessData(const std::shared_ptr<AppRunningRecord> & appRecord,bool isFromWindowFocusChanged)856 ProcessData AppStateObserverManager::WrapProcessData(
857 const std::shared_ptr<AppRunningRecord> &appRecord, bool isFromWindowFocusChanged)
858 {
859 ProcessData processData;
860 processData.bundleName = appRecord->GetBundleName();
861 processData.pid = appRecord->GetPid();
862 processData.uid = appRecord->GetUid();
863 auto applicationInfo = appRecord->GetApplicationInfo();
864 if (applicationInfo) {
865 processData.accessTokenId = applicationInfo->accessTokenId;
866 }
867 processData.state = static_cast<AppProcessState>(appRecord->GetState());
868 processData.isContinuousTask = appRecord->IsContinuousTask();
869 processData.isKeepAlive = appRecord->IsKeepAliveApp() || appRecord->IsKeepAliveAppService();
870 processData.isFocused = appRecord->GetFocusFlag();
871 processData.requestProcCode = appRecord->GetRequestProcCode();
872 processData.processChangeReason = static_cast<int32_t>(appRecord->GetProcessChangeReason());
873 processData.processName = appRecord->GetProcessName();
874 processData.extensionType = appRecord->GetExtensionType();
875 processData.processType = appRecord->GetProcessType();
876 if (appRecord->GetUserTestInfo() != nullptr && system::GetBoolParameter(DEVELOPER_MODE_STATE, false)) {
877 processData.isTestMode = true;
878 }
879 processData.exitReason = appRecord->GetExitReason();
880 processData.exitMsg = appRecord->GetExitMsg();
881 processData.gpuPid = appRecord->GetGPUPid();
882 processData.callerPid = appRecord->GetCallerPid();
883 processData.callerUid = appRecord->GetCallerUid();
884 processData.killReason = appRecord->GetKillReason();
885 processData.isFromWindowFocusChanged = isFromWindowFocusChanged;
886 return processData;
887 }
888
WrapRenderProcessData(const std::shared_ptr<RenderRecord> & renderRecord)889 ProcessData AppStateObserverManager::WrapRenderProcessData(const std::shared_ptr<RenderRecord> &renderRecord)
890 {
891 ProcessData processData;
892 processData.bundleName = renderRecord->GetHostBundleName();
893 processData.pid = renderRecord->GetPid();
894 processData.uid = renderRecord->GetHostUid();
895 processData.renderUid = renderRecord->GetUid();
896 processData.processName = renderRecord->GetProcessName();
897 processData.processType = renderRecord->GetProcessType();
898 processData.hostPid = renderRecord->GetHostPid();
899 return processData;
900 }
901
902 #ifdef SUPPORT_CHILD_PROCESS
WrapChildProcessData(ProcessData & processData,std::shared_ptr<ChildProcessRecord> childRecord)903 int32_t AppStateObserverManager::WrapChildProcessData(ProcessData &processData,
904 std::shared_ptr<ChildProcessRecord> childRecord)
905 {
906 if (!childRecord) {
907 TAG_LOGE(AAFwkTag::APPMGR, "null childRecord");
908 return ERR_INVALID_VALUE;
909 }
910 auto hostRecord = childRecord->GetHostRecord();
911 if (!hostRecord) {
912 TAG_LOGE(AAFwkTag::APPMGR, "null hostRecord");
913 return ERR_INVALID_VALUE;
914 }
915 processData.bundleName = hostRecord->GetBundleName();
916 processData.uid = hostRecord->GetUid();
917 processData.hostPid = childRecord->GetHostPid();
918 processData.pid = childRecord->GetPid();
919 processData.childUid = childRecord->GetUid();
920 processData.processName = childRecord->GetProcessName();
921 processData.processType = childRecord->GetProcessType();
922 return ERR_OK;
923 }
924 #endif // SUPPORT_CHILD_PROCESS
925
ObserverExist(const sptr<IRemoteBroker> & observer)926 bool AppStateObserverManager::ObserverExist(const sptr<IRemoteBroker> &observer)
927 {
928 if (observer == nullptr) {
929 TAG_LOGE(AAFwkTag::APPMGR, "null observer");
930 return false;
931 }
932 std::lock_guard lockRegister(observerLock_);
933 for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
934 if (it->first->AsObject() == observer->AsObject()) {
935 return true;
936 }
937 }
938 return false;
939 }
940
IsAbilityForegroundObserverExist(const sptr<IRemoteBroker> & observer)941 bool AppStateObserverManager::IsAbilityForegroundObserverExist(const sptr<IRemoteBroker> &observer)
942 {
943 if (observer == nullptr) {
944 TAG_LOGE(AAFwkTag::APPMGR, "null observer");
945 return false;
946 }
947 std::lock_guard lockRegister(abilityForegroundObserverLock_);
948 for (const auto &[it, uid] : abilityForegroundObserverMap_) {
949 if (it != nullptr && it->AsObject() == observer->AsObject()) {
950 return true;
951 }
952 }
953 return false;
954 }
955
IsAppForegroundObserverExist(const sptr<IRemoteBroker> & observer)956 bool AppStateObserverManager::IsAppForegroundObserverExist(const sptr<IRemoteBroker> &observer)
957 {
958 if (observer == nullptr) {
959 TAG_LOGE(AAFwkTag::APPMGR, "null observer");
960 return false;
961 }
962 std::lock_guard lockRegister(appForegroundObserverLock_);
963 for (const auto &[it, uid] : appForegroundStateObserverMap_) {
964 if (it != nullptr && it->AsObject() == observer->AsObject()) {
965 return true;
966 }
967 }
968 return false;
969 }
970
AddObserverDeathRecipient(const sptr<IRemoteBroker> & observer,const ObserverType & type)971 void AppStateObserverManager::AddObserverDeathRecipient(const sptr<IRemoteBroker> &observer, const ObserverType &type)
972 {
973 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
974 TAG_LOGD(AAFwkTag::APPMGR, "Add observer death recipient begin.");
975 if (observer == nullptr || observer->AsObject() == nullptr) {
976 TAG_LOGE(AAFwkTag::APPMGR, "null observer");
977 return;
978 }
979 std::lock_guard lock(recipientMapMutex_);
980 auto it = recipientMap_.find(observer->AsObject());
981 if (it != recipientMap_.end()) {
982 TAG_LOGE(AAFwkTag::APPMGR, "Death recipient added");
983 return;
984 } else {
985 std::weak_ptr<AppStateObserverManager> thisWeakPtr(shared_from_this());
986 sptr<IRemoteObject::DeathRecipient> deathRecipient = nullptr;
987 auto deathRecipientFunc = [thisWeakPtr, type](const wptr<IRemoteObject> &remote) {
988 auto appStateObserverManager = thisWeakPtr.lock();
989 if (appStateObserverManager) {
990 appStateObserverManager->OnObserverDied(remote, type);
991 }
992 };
993 if (type == ObserverType::APPLICATION_STATE_OBSERVER) {
994 deathRecipient = new (std::nothrow) ApplicationStateObserverRecipient(deathRecipientFunc);
995 } else if (type == ObserverType::APP_FOREGROUND_STATE_OBSERVER) {
996 deathRecipient = new (std::nothrow) AppForegroundStateObserverRecipient(deathRecipientFunc);
997 } else if (type == ObserverType::ABILITY_FOREGROUND_STATE_OBSERVER) {
998 deathRecipient = new (std::nothrow) AbilityForegroundStateObserverRecipient(deathRecipientFunc);
999 } else {
1000 TAG_LOGW(AAFwkTag::APPMGR, "null ObserverType");
1001 return;
1002 }
1003 if (deathRecipient == nullptr) {
1004 TAG_LOGE(AAFwkTag::APPMGR, "null deathRecipient");
1005 return;
1006 }
1007 if (!observer->AsObject()->AddDeathRecipient(deathRecipient)) {
1008 TAG_LOGE(AAFwkTag::APPMGR, "AddDeathRecipient failed");
1009 }
1010 recipientMap_.emplace(observer->AsObject(), deathRecipient);
1011 }
1012 }
1013
RemoveObserverDeathRecipient(const sptr<IRemoteBroker> & observer)1014 void AppStateObserverManager::RemoveObserverDeathRecipient(const sptr<IRemoteBroker> &observer)
1015 {
1016 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1017 TAG_LOGD(AAFwkTag::APPMGR, "Remove observer death recipient begin.");
1018 if (observer == nullptr || observer->AsObject() == nullptr) {
1019 TAG_LOGE(AAFwkTag::APPMGR, "null observer");
1020 return;
1021 }
1022 std::lock_guard lock(recipientMapMutex_);
1023 auto it = recipientMap_.find(observer->AsObject());
1024 if (it != recipientMap_.end()) {
1025 it->first->RemoveDeathRecipient(it->second);
1026 recipientMap_.erase(it);
1027 return;
1028 }
1029 }
1030
AddObserverCount(int32_t uid)1031 void AppStateObserverManager::AddObserverCount(int32_t uid)
1032 {
1033 std::lock_guard lock(observerCountMapMutex_);
1034 observerAmount_++;
1035 auto it = observerCountMap_.find(uid);
1036 if (it == observerCountMap_.end()) {
1037 observerCountMap_.emplace(uid, 1);
1038 } else {
1039 it->second++;
1040 if (it->second > OBSERVER_UID_COUNT_LOG) {
1041 TAG_LOGW(AAFwkTag::APPMGR, "too many observer uid: %{public}d, count: %{public}d", uid, it->second);
1042 }
1043 if (observerAmount_ % OBSERVER_AMOUNT_COUNT_LOG == 0) {
1044 for (const auto &[uid, count] : observerCountMap_) {
1045 TAG_LOGW(AAFwkTag::APPMGR, "observer overview uid: %{public}d, count: %{public}d", uid, count);
1046 }
1047 }
1048 }
1049 }
1050
DecreaseObserverCount(int32_t uid)1051 void AppStateObserverManager::DecreaseObserverCount(int32_t uid)
1052 {
1053 std::lock_guard lock(observerCountMapMutex_);
1054 auto it = observerCountMap_.find(uid);
1055 if (it == observerCountMap_.end()) {
1056 return;
1057 }
1058 it->second--;
1059 if (it->second <= 0) {
1060 observerCountMap_.erase(it);
1061 }
1062 observerAmount_--;
1063 }
1064
GetAppStateObserverMapCopy()1065 AppStateObserverMap AppStateObserverManager::GetAppStateObserverMapCopy()
1066 {
1067 std::lock_guard lock(observerLock_);
1068 return appStateObserverMap_;
1069 }
1070
GetAppForegroundStateObserverMapCopy()1071 AppForegroundStateObserverMap AppStateObserverManager::GetAppForegroundStateObserverMapCopy()
1072 {
1073 std::lock_guard lock(appForegroundObserverLock_);
1074 return appForegroundStateObserverMap_;
1075 }
1076
GetAbilityForegroundObserverMapCopy()1077 AbilityForegroundObserverMap AppStateObserverManager::GetAbilityForegroundObserverMapCopy()
1078 {
1079 std::lock_guard lock(abilityForegroundObserverLock_);
1080 return abilityForegroundObserverMap_;
1081 }
1082
OnObserverDied(const wptr<IRemoteObject> & remote,const ObserverType & type)1083 void AppStateObserverManager::OnObserverDied(const wptr<IRemoteObject> &remote, const ObserverType &type)
1084 {
1085 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1086 TAG_LOGD(AAFwkTag::APPMGR, "OnObserverDied");
1087 auto object = remote.promote();
1088 if (object == nullptr) {
1089 TAG_LOGE(AAFwkTag::APPMGR, "null observer");
1090 return;
1091 }
1092
1093 if (type == ObserverType::APPLICATION_STATE_OBSERVER) {
1094 sptr<IApplicationStateObserver> observer = iface_cast<IApplicationStateObserver>(object);
1095 UnregisterApplicationStateObserver(observer);
1096 } else if (type == ObserverType::ABILITY_FOREGROUND_STATE_OBSERVER) {
1097 sptr<IAbilityForegroundStateObserver> observer = iface_cast<IAbilityForegroundStateObserver>(object);
1098 UnregisterAbilityForegroundStateObserver(observer);
1099 } else if (type == ObserverType::APP_FOREGROUND_STATE_OBSERVER) {
1100 sptr<IAppForegroundStateObserver> observer = iface_cast<IAppForegroundStateObserver>(object);
1101 UnregisterAppForegroundStateObserver(observer);
1102 } else {
1103 TAG_LOGW(AAFwkTag::APPMGR, "null ObserverType");
1104 return;
1105 }
1106 }
1107
WrapAppStateData(const std::shared_ptr<AppRunningRecord> & appRecord,const ApplicationState state,bool isFromWindowFocusChanged)1108 AppStateData AppStateObserverManager::WrapAppStateData(const std::shared_ptr<AppRunningRecord> &appRecord,
1109 const ApplicationState state, bool isFromWindowFocusChanged)
1110 {
1111 AppStateData appStateData;
1112 appStateData.pid = appRecord->GetPid();
1113 appStateData.bundleName = appRecord->GetBundleName();
1114 appStateData.state = static_cast<int32_t>(state);
1115 appStateData.uid = appRecord->GetUid();
1116 appStateData.extensionType = appRecord->GetExtensionType();
1117 appStateData.isPreloadModule = appRecord->GetPreloadMode() != PreloadMode::PRESS_DOWN;
1118 appStateData.callerUid = appRecord->GetCallerUid();
1119 appStateData.isFromWindowFocusChanged = isFromWindowFocusChanged;
1120 if (appRecord->GetApplicationInfo() != nullptr) {
1121 appStateData.accessTokenId = static_cast<uint32_t>(appRecord->GetApplicationInfo()->accessTokenId);
1122 }
1123 appStateData.isFocused = appRecord->GetFocusFlag();
1124 auto renderRecordMap = appRecord->GetRenderRecordMap();
1125 if (!renderRecordMap.empty()) {
1126 for (auto iter : renderRecordMap) {
1127 auto renderRecord = iter.second;
1128 if (renderRecord != nullptr) {
1129 appStateData.renderPids.emplace_back(renderRecord->GetPid());
1130 }
1131 }
1132 }
1133 std::shared_ptr<RemoteClientManager> remoteClientManager = std::make_shared<RemoteClientManager>();
1134 auto bundleMgr = remoteClientManager->GetBundleManagerHelper();
1135 std::string callerBundleName;
1136 if (bundleMgr != nullptr &&
1137 IN_PROCESS_CALL(bundleMgr->GetNameForUid(appRecord->GetCallerUid(), callerBundleName)) == ERR_OK) {
1138 appStateData.callerBundleName = callerBundleName;
1139 } else {
1140 appStateData.callerBundleName = "";
1141 }
1142 appStateData.appIndex = appRecord->GetAppIndex();
1143 TAG_LOGD(AAFwkTag::APPMGR, "Handle state change, bundle:%{public}s, state:%{public}d, pid:%{public}d,"
1144 "uid:%{public}d, isFocused:%{public}d, callerBUndleName: %{public}s, appIndex:%{public}d, callerUid:%{public}d",
1145 appStateData.bundleName.c_str(), appStateData.state, appStateData.pid, appStateData.uid,
1146 appStateData.isFocused, appStateData.callerBundleName.c_str(), appStateData.appIndex, appStateData.callerUid);
1147 return appStateData;
1148 }
1149
OnPageShow(const PageStateData pageStateData)1150 void AppStateObserverManager::OnPageShow(const PageStateData pageStateData)
1151 {
1152 TAG_LOGD(AAFwkTag::APPMGR, "call");
1153 if (handler_ == nullptr) {
1154 TAG_LOGE(AAFwkTag::APPMGR, "null handler");
1155 return;
1156 }
1157
1158 auto task = [weak = weak_from_this(), pageStateData]() {
1159 auto self = weak.lock();
1160 if (self == nullptr) {
1161 TAG_LOGE(AAFwkTag::APPMGR, "null self");
1162 return;
1163 }
1164 TAG_LOGD(AAFwkTag::APPMGR, "OnProcessCreated come.");
1165 self->HandleOnPageShow(pageStateData);
1166 };
1167 handler_->SubmitTask(task);
1168 }
1169
OnPageHide(const PageStateData pageStateData)1170 void AppStateObserverManager::OnPageHide(const PageStateData pageStateData)
1171 {
1172 TAG_LOGD(AAFwkTag::APPMGR, "call");
1173 if (handler_ == nullptr) {
1174 TAG_LOGE(AAFwkTag::APPMGR, "null handler");
1175 return;
1176 }
1177
1178 auto task = [weak = weak_from_this(), pageStateData]() {
1179 auto self = weak.lock();
1180 if (self == nullptr) {
1181 TAG_LOGE(AAFwkTag::APPMGR, "null self");
1182 return;
1183 }
1184 TAG_LOGD(AAFwkTag::APPMGR, "OnProcessCreated come.");
1185 self->HandleOnPageHide(pageStateData);
1186 };
1187 handler_->SubmitTask(task);
1188 }
1189
HandleOnPageShow(const PageStateData pageStateData)1190 void AppStateObserverManager::HandleOnPageShow(const PageStateData pageStateData)
1191 {
1192 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1193 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
1194 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
1195 const auto &bundleNames = it->second.bundleNames;
1196 auto iter = std::find(bundleNames.begin(), bundleNames.end(), pageStateData.bundleName);
1197 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
1198 it->first->OnPageShow(pageStateData);
1199 }
1200 }
1201 }
1202
HandleOnPageHide(const PageStateData pageStateData)1203 void AppStateObserverManager::HandleOnPageHide(const PageStateData pageStateData)
1204 {
1205 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1206 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
1207 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
1208 const auto &bundleNames = it->second.bundleNames;
1209 auto iter = std::find(bundleNames.begin(), bundleNames.end(), pageStateData.bundleName);
1210 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
1211 it->first->OnPageHide(pageStateData);
1212 }
1213 }
1214 }
1215
OnAppCacheStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord,ApplicationState state)1216 void AppStateObserverManager::OnAppCacheStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord,
1217 ApplicationState state)
1218 {
1219 if (handler_ == nullptr) {
1220 TAG_LOGE(AAFwkTag::APPMGR, "null handler");
1221 return;
1222 }
1223
1224 auto task = [weak = weak_from_this(), appRecord, state]() {
1225 auto self = weak.lock();
1226 if (self == nullptr) {
1227 TAG_LOGE(AAFwkTag::APPMGR, "null self");
1228 return;
1229 }
1230 TAG_LOGD(AAFwkTag::APPMGR, "OnAppCacheStateChanged come.");
1231 self->HandleOnAppCacheStateChanged(appRecord, state);
1232 };
1233 handler_->SubmitTask(task);
1234 }
1235
HandleOnAppCacheStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord,ApplicationState state)1236 void AppStateObserverManager::HandleOnAppCacheStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord,
1237 ApplicationState state)
1238 {
1239 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1240 if (appRecord == nullptr) {
1241 TAG_LOGE(AAFwkTag::APPMGR, "null appRecord");
1242 return;
1243 }
1244
1245 AppStateData data = WrapAppStateData(appRecord, state);
1246 data.isSpecifyTokenId = appRecord->GetAssignTokenId() > 0 ? true : false;
1247 TAG_LOGD(AAFwkTag::APPMGR, "HandleOnAppCacheStateChanged, bundle:%{public}s, uid:%{public}d, state:%{public}d",
1248 data.bundleName.c_str(), data.uid, data.state);
1249 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
1250 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
1251 const auto &bundleNames = it->second.bundleNames;
1252 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
1253 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
1254 it->first->OnAppCacheStateChanged(data);
1255 }
1256 }
1257 }
1258
WrapProcessBindData(const UIExtensionProcessBindInfo & bindInfo,int32_t bindingRelation)1259 ProcessBindData AppStateObserverManager::WrapProcessBindData(
1260 const UIExtensionProcessBindInfo &bindInfo, int32_t bindingRelation)
1261 {
1262 ProcessBindData processBindData;
1263 processBindData.bundleName = bindInfo.bundleName;
1264 processBindData.pid = bindInfo.pid;
1265 processBindData.uid = bindInfo.uid;
1266 processBindData.isKeepAlive = bindInfo.isKeepAlive;
1267 processBindData.extensionType = bindInfo.extensionType;
1268 processBindData.processType = bindInfo.processType;
1269 processBindData.callerPid = bindInfo.callerPid;
1270 processBindData.callerUid = bindInfo.callerUid;
1271 processBindData.callerBundleName = bindInfo.callerBundleName;
1272 processBindData.bindingRelation = bindingRelation;
1273 return processBindData;
1274 }
1275
OnProcessBindingRelationChanged(const std::shared_ptr<AppRunningRecord> & appRecord,const UIExtensionProcessBindInfo & bindInfo,int32_t bindingRelation)1276 void AppStateObserverManager::OnProcessBindingRelationChanged(
1277 const std::shared_ptr<AppRunningRecord> &appRecord,
1278 const UIExtensionProcessBindInfo &bindInfo, int32_t bindingRelation)
1279 {
1280 if (handler_ == nullptr) {
1281 TAG_LOGE(AAFwkTag::APPMGR, "null handler");
1282 return;
1283 }
1284
1285 auto task = [weak = weak_from_this(), appRecord, bindInfo, bindingRelation]() {
1286 auto self = weak.lock();
1287 if (self == nullptr) {
1288 TAG_LOGE(AAFwkTag::APPMGR, "null self");
1289 return;
1290 }
1291 TAG_LOGD(AAFwkTag::APPMGR, "OnProcessBindingRelationChanged come.");
1292 self->HandleOnProcessBindingRelationChanged(appRecord, bindInfo, bindingRelation);
1293 };
1294 handler_->SubmitTask(task);
1295 }
1296
HandleOnProcessBindingRelationChanged(const std::shared_ptr<AppRunningRecord> & appRecord,const UIExtensionProcessBindInfo & bindInfo,int32_t bindingRelation)1297 void AppStateObserverManager::HandleOnProcessBindingRelationChanged(
1298 const std::shared_ptr<AppRunningRecord> &appRecord,
1299 const UIExtensionProcessBindInfo &bindInfo, int32_t bindingRelation)
1300 {
1301 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1302 if (appRecord == nullptr) {
1303 TAG_LOGE(AAFwkTag::APPMGR, "null appRecord");
1304 return;
1305 }
1306
1307 ProcessBindData data = WrapProcessBindData(bindInfo, bindingRelation);
1308 TAG_LOGD(AAFwkTag::APPMGR,
1309 "HandleOnProcessBindingRelationChanged, pid:%{public}d, uid:%{public}d, bundleName:%{public}s, "
1310 "callerPid:%{public}d, callerUid:%{public}d, callerBundleName:%{public}s, bindingRelation:%{public}d",
1311 data.pid,
1312 data.uid,
1313 data.bundleName.c_str(),
1314 data.callerPid,
1315 data.callerUid,
1316 data.callerBundleName.c_str(),
1317 data.bindingRelation);
1318 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
1319 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
1320 const auto &bundleNames = it->second.bundleNames;
1321 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
1322 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
1323 it->first->OnProcessBindingRelationChanged(data);
1324 }
1325 }
1326 }
1327
OnKeepAliveStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord)1328 void AppStateObserverManager::OnKeepAliveStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord)
1329 {
1330 if (handler_ == nullptr) {
1331 TAG_LOGE(AAFwkTag::APPMGR, "null handler");
1332 return;
1333 }
1334
1335 auto task = [weak = weak_from_this(), appRecord]() {
1336 auto self = weak.lock();
1337 if (self == nullptr) {
1338 TAG_LOGE(AAFwkTag::APPMGR, "null self");
1339 return;
1340 }
1341 TAG_LOGD(AAFwkTag::APPMGR, "HandleOnKeepAliveStateChanged come.");
1342 self->HandleOnKeepAliveStateChanged(appRecord);
1343 };
1344 handler_->SubmitTask(task);
1345 }
1346
HandleOnKeepAliveStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord)1347 void AppStateObserverManager::HandleOnKeepAliveStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord)
1348 {
1349 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1350 if (!appRecord) {
1351 TAG_LOGE(AAFwkTag::APPMGR, "null appRecord");
1352 return;
1353 }
1354 ProcessData data = WrapProcessData(appRecord);
1355 TAG_LOGI(AAFwkTag::APPMGR,
1356 "bundle:%{public}s, pid:%{public}d, uid:%{public}d, processType:%{public}d, "
1357 "extensionType:%{public}d, processName:%{public}s, renderUid:%{public}d, isTestMode:%{public}d, "
1358 "callerPid:%{public}d, callerUid:%{public}d, isKeepAlive:%{public}d",
1359 data.bundleName.c_str(), data.pid, data.uid, data.processType, data.extensionType, data.processName.c_str(),
1360 data.renderUid, data.isTestMode, data.callerPid, data.callerUid, data.isKeepAlive);
1361
1362 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
1363 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
1364 const auto &bundleNames = it->second.bundleNames;
1365 auto iter = std::find(bundleNames.begin(), bundleNames.end(), data.bundleName);
1366 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
1367 it->first->OnKeepAliveStateChanged(data);
1368 }
1369 }
1370 }
1371
OnProcessPreForegroundChanged(std::shared_ptr<AppRunningRecord> appRecord)1372 void AppStateObserverManager::OnProcessPreForegroundChanged(std::shared_ptr<AppRunningRecord> appRecord)
1373 {
1374 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1375 TAG_LOGI(AAFwkTag::APPMGR, "OnProcessPreForegroundChanged");
1376 if (!appRecord) {
1377 TAG_LOGE(AAFwkTag::APPMGR, "null appRecord");
1378 return;
1379 }
1380 if (handler_ == nullptr) {
1381 TAG_LOGE(AAFwkTag::APPMGR, "null handler");
1382 return;
1383 }
1384
1385 auto task = [weak = weak_from_this(), appRecord]() {
1386 auto self = weak.lock();
1387 if (self == nullptr) {
1388 TAG_LOGE(AAFwkTag::APPMGR, "null self");
1389 return;
1390 }
1391 TAG_LOGD(AAFwkTag::APPMGR, "OnProcessPreForegroundChanged task.");
1392 self->HandleOnProcessPreForegroundChanged(appRecord);
1393 };
1394 handler_->SubmitTask(task);
1395 }
1396
HandleOnProcessPreForegroundChanged(std::shared_ptr<AppRunningRecord> appRecord)1397 void AppStateObserverManager::HandleOnProcessPreForegroundChanged(std::shared_ptr<AppRunningRecord> appRecord)
1398 {
1399 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
1400 if (appRecord == nullptr) {
1401 TAG_LOGE(AAFwkTag::APPMGR, "null appRecord");
1402 return;
1403 }
1404
1405 auto bundleName = appRecord->GetBundleName();
1406 PreloadProcessData preloadProcessData;
1407 preloadProcessData.isPreForeground = appRecord->IsPreForeground();
1408 preloadProcessData.pid = appRecord->GetPid();
1409 preloadProcessData.uid = appRecord->GetUid();
1410 preloadProcessData.bundleName = bundleName;
1411
1412 TAG_LOGI(AAFwkTag::APPMGR,
1413 "HandleOnProcessPreForegroundChanged, pid:%{public}d, bundle:%{public}s, uid:%{public}d, isPreFore:%{public}d",
1414 preloadProcessData.pid, bundleName.c_str(), preloadProcessData.uid, preloadProcessData.isPreForeground);
1415 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
1416 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
1417 const auto &bundleNames = it->second.bundleNames;
1418 auto iter = std::find(bundleNames.begin(), bundleNames.end(), bundleName);
1419 if ((bundleNames.empty() || iter != bundleNames.end()) && it->first != nullptr) {
1420 it->first->OnProcessPreForegroundChanged(preloadProcessData);
1421 }
1422 }
1423 }
1424 } // namespace AppExecFwk
1425 } // namespace OHOS
1426