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