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, "the 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, "Permission verification failed");
63 return ERR_PERMISSION_DENIED;
64 }
65 if (observer == nullptr) {
66 TAG_LOGE(AAFwkTag::APPMGR, "The param observer is nullptr.");
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, "Permission 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, "Observer nullptr");
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, "The param observer is nullptr.");
111 return ERR_INVALID_VALUE;
112 }
113 if (AAFwk::PermissionVerification::GetInstance()->VerifyAppStateObserverPermission() == ERR_PERMISSION_DENIED) {
114 TAG_LOGE(AAFwkTag::APPMGR, "Permission 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, "Observer nullptr.");
134 return ERR_INVALID_VALUE;
135 }
136 if (AAFwk::PermissionVerification::GetInstance()->VerifyAppStateObserverPermission() == ERR_PERMISSION_DENIED) {
137 TAG_LOGE(AAFwkTag::APPMGR, "Permission 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, "The param observer is nullptr.");
158 return ERR_INVALID_VALUE;
159 }
160 if (AAFwk::PermissionVerification::GetInstance()->VerifyAppStateObserverPermission() == ERR_PERMISSION_DENIED) {
161 TAG_LOGE(AAFwkTag::APPMGR, "Permission 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, "Observer nullptr.");
182 return ERR_INVALID_VALUE;
183 }
184 if (AAFwk::PermissionVerification::GetInstance()->VerifyAppStateObserverPermission() == ERR_PERMISSION_DENIED) {
185 TAG_LOGE(AAFwkTag::APPMGR, "Permission 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, "handler is nullptr, OnAppStarted failed.");
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, "self is nullptr, OnAppStarted failed.");
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, "handler is nullptr, OnAppStopped failed.");
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, "self is nullptr, OnAppStopped failed.");
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, "handler is nullptr, OnAppStateChanged failed.");
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, "self is nullptr, OnAppStateChanged failed.");
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, "handler is nullptr, OnProcessDied failed.");
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, "self is nullptr, OnProcessDied failed.");
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, "handler is nullptr, OnRenderProcessDied failed.");
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, "self is nullptr, OnRenderProcessDied failed.");
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, "handler is nullptr, OnChildProcessDied failed.");
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, "self is nullptr, OnChildProcessDied failed.");
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, "handler is nullptr, OnProcessStateChanged failed.");
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, "self is nullptr, OnProcessStateChanged failed.");
330 return;
331 }
332 TAG_LOGD(AAFwkTag::APPMGR, "OnProcessStateChanged come.");
333 self->HandleOnProcessStateChanged(appRecord);
334 };
335 handler_->SubmitTask(task);
336 }
337
OnProcessCreated(const std::shared_ptr<AppRunningRecord> & appRecord,bool isPreload)338 void AppStateObserverManager::OnProcessCreated(const std::shared_ptr<AppRunningRecord> &appRecord, bool isPreload)
339 {
340 if (handler_ == nullptr) {
341 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnProcessCreated failed.");
342 return;
343 }
344
345 auto task = [weak = weak_from_this(), appRecord, isPreload]() {
346 auto self = weak.lock();
347 if (self == nullptr) {
348 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnProcessCreated failed.");
349 return;
350 }
351 self->HandleOnAppProcessCreated(appRecord, isPreload);
352 };
353 handler_->SubmitTask(task);
354 }
355
OnProcessReused(const std::shared_ptr<AppRunningRecord> & appRecord)356 void AppStateObserverManager::OnProcessReused(const std::shared_ptr<AppRunningRecord> &appRecord)
357 {
358 if (handler_ == nullptr) {
359 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnProcessReused failed.");
360 return;
361 }
362
363 auto task = [weak = weak_from_this(), appRecord]() {
364 auto self = weak.lock();
365 if (self == nullptr) {
366 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnProcessReused failed.");
367 return;
368 }
369 TAG_LOGD(AAFwkTag::APPMGR, "OnProcessReused come.");
370 self->HandleOnProcessResued(appRecord);
371 };
372 handler_->SubmitTask(task);
373 }
374
OnRenderProcessCreated(const std::shared_ptr<RenderRecord> & renderRecord)375 void AppStateObserverManager::OnRenderProcessCreated(const std::shared_ptr<RenderRecord> &renderRecord)
376 {
377 if (handler_ == nullptr) {
378 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnRenderProcessCreated failed.");
379 return;
380 }
381
382 auto task = [weak = weak_from_this(), renderRecord]() {
383 auto self = weak.lock();
384 if (self == nullptr) {
385 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnRenderProcessCreated failed.");
386 return;
387 }
388 TAG_LOGD(AAFwkTag::APPMGR, "OnRenderProcessCreated come.");
389 self->HandleOnRenderProcessCreated(renderRecord);
390 };
391 handler_->SubmitTask(task);
392 }
393
OnChildProcessCreated(std::shared_ptr<ChildProcessRecord> childRecord)394 void AppStateObserverManager::OnChildProcessCreated(std::shared_ptr<ChildProcessRecord> childRecord)
395 {
396 if (handler_ == nullptr) {
397 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnChildProcessCreated failed.");
398 return;
399 }
400
401 auto task = [weak = weak_from_this(), childRecord]() {
402 auto self = weak.lock();
403 if (self == nullptr) {
404 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnChildProcessCreated failed.");
405 return;
406 }
407 TAG_LOGD(AAFwkTag::APPMGR, "OnChildProcessCreated come.");
408 self->HandleOnChildProcessCreated(childRecord);
409 };
410 handler_->SubmitTask(task);
411 }
412
StateChangedNotifyObserver(const AbilityStateData abilityStateData,bool isAbility,bool isFromWindowFocusChanged)413 void AppStateObserverManager::StateChangedNotifyObserver(
414 const AbilityStateData abilityStateData, bool isAbility, bool isFromWindowFocusChanged)
415 {
416 if (handler_ == nullptr) {
417 TAG_LOGE(AAFwkTag::APPMGR, "null handler, failed");
418 return;
419 }
420
421 auto task = [weak = weak_from_this(), abilityStateData, isAbility, isFromWindowFocusChanged]() {
422 auto self = weak.lock();
423 if (self == nullptr) {
424 TAG_LOGE(AAFwkTag::APPMGR, "null self, failed");
425 return;
426 }
427 TAG_LOGD(AAFwkTag::APPMGR, "StateChangedNotifyObserver come");
428 self->HandleStateChangedNotifyObserver(abilityStateData, isAbility, isFromWindowFocusChanged);
429 };
430 handler_->SubmitTask(task);
431 }
432
HandleOnAppStarted(const std::shared_ptr<AppRunningRecord> & appRecord)433 void AppStateObserverManager::HandleOnAppStarted(const std::shared_ptr<AppRunningRecord> &appRecord)
434 {
435 if (appRecord == nullptr) {
436 TAG_LOGE(AAFwkTag::APPMGR, "app record is null");
437 return;
438 }
439
440 AppStateData data = WrapAppStateData(appRecord, ApplicationState::APP_STATE_CREATE);
441 data.isSpecifyTokenId = appRecord->GetAssignTokenId() > 0 ? true : false;
442 TAG_LOGD(AAFwkTag::APPMGR, "HandleOnAppStarted, bundle:%{public}s, uid:%{public}d, state:%{public}d",
443 data.bundleName.c_str(), data.uid, data.state);
444 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
445 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
446 std::vector<std::string>::iterator iter = std::find(it->second.begin(),
447 it->second.end(), data.bundleName);
448 if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
449 it->first->OnAppStarted(data);
450 }
451 }
452 }
453
HandleOnAppStopped(const std::shared_ptr<AppRunningRecord> & appRecord)454 void AppStateObserverManager::HandleOnAppStopped(const std::shared_ptr<AppRunningRecord> &appRecord)
455 {
456 if (appRecord == nullptr) {
457 TAG_LOGE(AAFwkTag::APPMGR, "app record is null");
458 return;
459 }
460
461 AppStateData data = WrapAppStateData(appRecord, ApplicationState::APP_STATE_TERMINATED);
462 TAG_LOGD(AAFwkTag::APPMGR, "HandleOnAppStopped, bundle:%{public}s, uid:%{public}d, state:%{public}d",
463 data.bundleName.c_str(), data.uid, data.state);
464 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
465 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
466 std::vector<std::string>::iterator iter = std::find(it->second.begin(),
467 it->second.end(), data.bundleName);
468 if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
469 it->first->OnAppStopped(data);
470 }
471 }
472 }
473
HandleAppStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord,const ApplicationState state,bool needNotifyApp,bool isFromWindowFocusChanged)474 void AppStateObserverManager::HandleAppStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord,
475 const ApplicationState state, bool needNotifyApp, bool isFromWindowFocusChanged)
476 {
477 if (appRecord == nullptr) {
478 return;
479 }
480 dummyCode_ = __LINE__;
481 if (state == ApplicationState::APP_STATE_FOREGROUND || state == ApplicationState::APP_STATE_BACKGROUND) {
482 if (needNotifyApp && !isFromWindowFocusChanged) {
483 AppStateData data = WrapAppStateData(appRecord, state);
484 appRecord->GetSplitModeAndFloatingMode(data.isSplitScreenMode, data.isFloatingWindowMode);
485 dummyCode_ = __LINE__;
486 auto appForegroundStateObserverSetCopy = GetAppForegroundStateObserverSetCopy();
487 for (auto it : appForegroundStateObserverSetCopy) {
488 if (it != nullptr) {
489 it->OnAppStateChanged(data);
490 }
491 }
492 }
493 dummyCode_ = __LINE__;
494 if (!AAFwk::UIExtensionUtils::IsUIExtension(appRecord->GetExtensionType()) &&
495 !AAFwk::UIExtensionUtils::IsWindowExtension(appRecord->GetExtensionType())) {
496 AppStateData data = WrapAppStateData(appRecord, state);
497 TAG_LOGD(AAFwkTag::APPMGR, "name:%{public}s, uid:%{public}d, state:%{public}d, notify:%{public}d",
498 data.bundleName.c_str(), data.uid, data.state, needNotifyApp);
499 dummyCode_ = __LINE__;
500 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
501 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
502 std::vector<std::string>::iterator iter =
503 std::find(it->second.begin(), it->second.end(), data.bundleName);
504 bool valid = (it->second.empty() || iter != it->second.end()) && it->first != nullptr;
505 if (valid) {
506 it->first->OnForegroundApplicationChanged(data);
507 }
508 if (valid && needNotifyApp) {
509 it->first->OnAppStateChanged(data);
510 }
511 }
512 }
513 }
514 dummyCode_ = __LINE__;
515 if (state == ApplicationState::APP_STATE_CREATE || state == ApplicationState::APP_STATE_TERMINATED) {
516 AppStateData data = WrapAppStateData(appRecord, state);
517 TAG_LOGD(AAFwkTag::APPMGR, "OnApplicationStateChanged, name:%{public}s, uid:%{public}d, state:%{public}d",
518 data.bundleName.c_str(), data.uid, data.state);
519 dummyCode_ = __LINE__;
520 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
521 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
522 std::vector<std::string>::iterator iter = std::find(it->second.begin(),
523 it->second.end(), data.bundleName);
524 if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
525 it->first->OnApplicationStateChanged(data);
526 }
527 }
528 }
529 }
530
HandleStateChangedNotifyObserver(const AbilityStateData abilityStateData,bool isAbility,bool isFromWindowFocusChanged)531 void AppStateObserverManager::HandleStateChangedNotifyObserver(
532 const AbilityStateData abilityStateData, bool isAbility, bool isFromWindowFocusChanged)
533 {
534 TAG_LOGD(AAFwkTag::APPMGR,
535 "Handle state change, module:%{public}s, bundle:%{public}s, ability:%{public}s, state:%{public}d,"
536 "pid:%{public}d ,uid:%{public}d, abilityType:%{public}d, isAbility:%{public}d, callerBundleName:%{public}s,"
537 "callerAbilityName:%{public}s, isAtomicService:%{public}d",
538 abilityStateData.moduleName.c_str(), abilityStateData.bundleName.c_str(),
539 abilityStateData.abilityName.c_str(), abilityStateData.abilityState,
540 abilityStateData.pid, abilityStateData.uid, abilityStateData.abilityType, isAbility,
541 abilityStateData.callerBundleName.c_str(), abilityStateData.callerAbilityName.c_str(),
542 abilityStateData.isAtomicService);
543 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
544 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
545 std::vector<std::string>::iterator iter = std::find(it->second.begin(),
546 it->second.end(), abilityStateData.bundleName);
547 if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
548 if (isAbility) {
549 it->first->OnAbilityStateChanged(abilityStateData);
550 } else {
551 it->first->OnExtensionStateChanged(abilityStateData);
552 }
553 }
554 }
555
556 if ((abilityStateData.abilityState == static_cast<int32_t>(AbilityState::ABILITY_STATE_FOREGROUND) ||
557 abilityStateData.abilityState == static_cast<int32_t>(AbilityState::ABILITY_STATE_BACKGROUND)) &&
558 isAbility && !isFromWindowFocusChanged) {
559 auto abilityforegroundObserverSetCopy = GetAbilityforegroundObserverSetCopy();
560 for (auto &it : abilityforegroundObserverSetCopy) {
561 if (it != nullptr) {
562 it->OnAbilityStateChanged(abilityStateData);
563 }
564 }
565 }
566 }
567
HandleOnAppProcessCreated(const std::shared_ptr<AppRunningRecord> & appRecord,bool isPreload)568 void AppStateObserverManager::HandleOnAppProcessCreated(const std::shared_ptr<AppRunningRecord> &appRecord,
569 bool isPreload)
570 {
571 if (!appRecord) {
572 TAG_LOGE(AAFwkTag::APPMGR, "app record is null");
573 return;
574 }
575 ProcessData data = WrapProcessData(appRecord);
576 data.isPreload = isPreload;
577 if (data.bundleName == XIAOYI_BUNDLE_NAME && data.extensionType == ExtensionAbilityType::SERVICE) {
578 TAG_LOGI(AAFwkTag::APPMGR, "bundleName is com.huawei.chmos.vassistant, change processType to NORMAL");
579 data.processType = ProcessType::NORMAL;
580 }
581 TAG_LOGI(AAFwkTag::APPMGR,
582 "Process Create, bundle:%{public}s, pid:%{public}d, uid:%{public}d, processType:%{public}d, "
583 "extensionType:%{public}d, processName:%{public}s, renderUid:%{public}d, isTestMode:%{public}d",
584 data.bundleName.c_str(), data.pid, data.uid, data.processType, data.extensionType, data.processName.c_str(),
585 data.renderUid, data.isTestMode);
586 HandleOnProcessCreated(data);
587 }
588
HandleOnProcessResued(const std::shared_ptr<AppRunningRecord> & appRecord)589 void AppStateObserverManager::HandleOnProcessResued(const std::shared_ptr<AppRunningRecord> &appRecord)
590 {
591 if (!appRecord) {
592 TAG_LOGE(AAFwkTag::APPMGR, "app record is null");
593 return;
594 }
595 ProcessData data = WrapProcessData(appRecord);
596 TAG_LOGD(AAFwkTag::APPMGR, "Process Resued, bundle:%{public}s, pid:%{public}d, uid:%{public}d",
597 data.bundleName.c_str(), data.pid, data.uid);
598
599 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
600 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
601 std::vector<std::string>::iterator iter = std::find(it->second.begin(),
602 it->second.end(), data.bundleName);
603 if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
604 it->first->OnProcessReused(data);
605 }
606 }
607 }
608
HandleOnRenderProcessCreated(const std::shared_ptr<RenderRecord> & renderRecord)609 void AppStateObserverManager::HandleOnRenderProcessCreated(const std::shared_ptr<RenderRecord> &renderRecord)
610 {
611 if (!renderRecord) {
612 TAG_LOGE(AAFwkTag::APPMGR, "render record is null");
613 return;
614 }
615 ProcessData data = WrapRenderProcessData(renderRecord);
616 TAG_LOGD(AAFwkTag::APPMGR,
617 "RenderProcess Create, bundle:%{public}s, pid:%{public}d, uid:%{public}d, processType:%{public}d, "
618 "processName:%{public}s, renderUid:%{public}d",
619 data.bundleName.c_str(), data.pid, data.uid, data.processType, data.processName.c_str(), data.renderUid);
620 HandleOnProcessCreated(data);
621 }
622
HandleOnChildProcessCreated(std::shared_ptr<ChildProcessRecord> childRecord)623 void AppStateObserverManager::HandleOnChildProcessCreated(std::shared_ptr<ChildProcessRecord> childRecord)
624 {
625 if (!childRecord) {
626 TAG_LOGE(AAFwkTag::APPMGR, "ChildProcessRecord record is nullptr.");
627 return;
628 }
629 ProcessData data;
630 if (WrapChildProcessData(data, childRecord) != ERR_OK) {
631 TAG_LOGE(AAFwkTag::APPMGR, "WrapChildProcessData failed.");
632 return;
633 }
634 TAG_LOGD(AAFwkTag::APPMGR,
635 "ChildProcess Create, bundleName:%{public}s, pid:%{public}d, uid:%{public}d, "
636 "processType:%{public}d, processName:%{public}s",
637 data.bundleName.c_str(), data.pid, data.uid, data.processType, data.processName.c_str());
638 HandleOnProcessCreated(data);
639 }
640
HandleOnProcessCreated(const ProcessData & data)641 void AppStateObserverManager::HandleOnProcessCreated(const ProcessData &data)
642 {
643 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
644 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
645 std::vector<std::string>::iterator iter = std::find(it->second.begin(),
646 it->second.end(), data.bundleName);
647 if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
648 it->first->OnProcessCreated(data);
649 }
650 }
651 }
652
HandleOnProcessStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord)653 void AppStateObserverManager::HandleOnProcessStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord)
654 {
655 if (!appRecord) {
656 TAG_LOGE(AAFwkTag::APPMGR, "app record is null");
657 return;
658 }
659 ProcessData data = WrapProcessData(appRecord);
660 if (data.bundleName == XIAOYI_BUNDLE_NAME && data.extensionType == ExtensionAbilityType::SERVICE) {
661 TAG_LOGI(AAFwkTag::APPMGR, "bundleName is com.huawei.chmos.vassistant, change processType to NORMAL");
662 data.processType = ProcessType::NORMAL;
663 }
664 TAG_LOGD(AAFwkTag::APPMGR,
665 "bundle:%{public}s, pid:%{public}d, uid:%{public}d, state:%{public}d, "
666 "isContinuousTask:%{public}d, gpuPid:%{public}d",
667 data.bundleName.c_str(), data.pid, data.uid, data.state, data.isContinuousTask, data.gpuPid);
668 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
669 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
670 std::vector<std::string>::iterator iter = std::find(it->second.begin(),
671 it->second.end(), data.bundleName);
672 if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
673 it->first->OnProcessStateChanged(data);
674 }
675 }
676 }
677
HandleOnAppProcessDied(const std::shared_ptr<AppRunningRecord> & appRecord)678 void AppStateObserverManager::HandleOnAppProcessDied(const std::shared_ptr<AppRunningRecord> &appRecord)
679 {
680 if (!appRecord) {
681 TAG_LOGE(AAFwkTag::APPMGR, "app record is null");
682 return;
683 }
684 ProcessData data = WrapProcessData(appRecord);
685 TAG_LOGD(AAFwkTag::APPMGR, "Process died, bundle:%{public}s, pid:%{public}d, uid:%{public}d, renderUid:%{public}d,"
686 " exitReason:%{public}d, exitMsg:%{public}s",
687 data.bundleName.c_str(), data.pid, data.uid, data.renderUid, data.exitReason, data.exitMsg.c_str());
688 HandleOnProcessDied(data);
689 }
690
HandleOnRenderProcessDied(const std::shared_ptr<RenderRecord> & renderRecord)691 void AppStateObserverManager::HandleOnRenderProcessDied(const std::shared_ptr<RenderRecord> &renderRecord)
692 {
693 if (!renderRecord) {
694 TAG_LOGE(AAFwkTag::APPMGR, "render record is null");
695 return;
696 }
697 ProcessData data = WrapRenderProcessData(renderRecord);
698 TAG_LOGD(AAFwkTag::APPMGR,
699 "Render Process died, bundle:%{public}s, pid:%{public}d, uid:%{public}d, renderUid:%{public}d",
700 data.bundleName.c_str(), data.pid, data.uid, data.renderUid);
701 HandleOnProcessDied(data);
702 }
703
HandleOnChildProcessDied(std::shared_ptr<ChildProcessRecord> childRecord)704 void AppStateObserverManager::HandleOnChildProcessDied(std::shared_ptr<ChildProcessRecord> childRecord)
705 {
706 if (!childRecord) {
707 TAG_LOGE(AAFwkTag::APPMGR, "childRecord is nullptr.");
708 return;
709 }
710 ProcessData data;
711 if (WrapChildProcessData(data, childRecord) != ERR_OK) {
712 TAG_LOGE(AAFwkTag::APPMGR, "WrapChildProcessData failed.");
713 return;
714 }
715 TAG_LOGD(AAFwkTag::APPMGR,
716 "ChildProcess died, bundleName:%{public}s, pid:%{public}d, uid:%{public}d, "
717 "processType:%{public}d, processName:%{public}s",
718 data.bundleName.c_str(), data.pid, data.uid, data.processType, data.processName.c_str());
719 HandleOnProcessDied(data);
720 }
721
HandleOnProcessDied(const ProcessData & data)722 void AppStateObserverManager::HandleOnProcessDied(const ProcessData &data)
723 {
724 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
725 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
726 std::vector<std::string>::iterator iter = std::find(it->second.begin(),
727 it->second.end(), data.bundleName);
728 if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
729 it->first->OnProcessDied(data);
730 }
731 }
732 }
733
WrapProcessData(const std::shared_ptr<AppRunningRecord> & appRecord)734 ProcessData AppStateObserverManager::WrapProcessData(const std::shared_ptr<AppRunningRecord> &appRecord)
735 {
736 ProcessData processData;
737 processData.bundleName = appRecord->GetBundleName();
738 processData.pid = appRecord->GetPriorityObject()->GetPid();
739 processData.uid = appRecord->GetUid();
740 auto applicationInfo = appRecord->GetApplicationInfo();
741 if (applicationInfo) {
742 processData.accessTokenId = applicationInfo->accessTokenId;
743 }
744 processData.state = static_cast<AppProcessState>(appRecord->GetState());
745 processData.isContinuousTask = appRecord->IsContinuousTask();
746 processData.isKeepAlive = appRecord->IsKeepAliveApp();
747 processData.isFocused = appRecord->GetFocusFlag();
748 processData.requestProcCode = appRecord->GetRequestProcCode();
749 processData.processChangeReason = static_cast<int32_t>(appRecord->GetProcessChangeReason());
750 processData.processName = appRecord->GetProcessName();
751 processData.extensionType = appRecord->GetExtensionType();
752 processData.processType = appRecord->GetProcessType();
753 if (appRecord->GetUserTestInfo() != nullptr && system::GetBoolParameter(DEVELOPER_MODE_STATE, false)) {
754 processData.isTestMode = true;
755 }
756 processData.exitReason = appRecord->GetExitReason();
757 processData.exitMsg = appRecord->GetExitMsg();
758 processData.gpuPid = appRecord->GetGPUPid();
759 return processData;
760 }
761
WrapRenderProcessData(const std::shared_ptr<RenderRecord> & renderRecord)762 ProcessData AppStateObserverManager::WrapRenderProcessData(const std::shared_ptr<RenderRecord> &renderRecord)
763 {
764 ProcessData processData;
765 processData.bundleName = renderRecord->GetHostBundleName();
766 processData.pid = renderRecord->GetPid();
767 processData.uid = renderRecord->GetHostUid();
768 processData.renderUid = renderRecord->GetUid();
769 processData.processName = renderRecord->GetProcessName();
770 processData.processType = renderRecord->GetProcessType();
771 processData.hostPid = renderRecord->GetHostPid();
772 return processData;
773 }
774
WrapChildProcessData(ProcessData & processData,std::shared_ptr<ChildProcessRecord> childRecord)775 int32_t AppStateObserverManager::WrapChildProcessData(ProcessData &processData,
776 std::shared_ptr<ChildProcessRecord> childRecord)
777 {
778 if (!childRecord) {
779 TAG_LOGE(AAFwkTag::APPMGR, "childRecord is nullptr.");
780 return ERR_INVALID_VALUE;
781 }
782 auto hostRecord = childRecord->GetHostRecord();
783 if (!hostRecord) {
784 TAG_LOGE(AAFwkTag::APPMGR, "hostRecord is nullptr.");
785 return ERR_INVALID_VALUE;
786 }
787 processData.bundleName = hostRecord->GetBundleName();
788 processData.uid = hostRecord->GetUid();
789 processData.hostPid = childRecord->GetHostPid();
790 processData.pid = childRecord->GetPid();
791 processData.childUid = childRecord->GetUid();
792 processData.processName = childRecord->GetProcessName();
793 processData.processType = childRecord->GetProcessType();
794 return ERR_OK;
795 }
796
ObserverExist(const sptr<IRemoteBroker> & observer)797 bool AppStateObserverManager::ObserverExist(const sptr<IRemoteBroker> &observer)
798 {
799 if (observer == nullptr) {
800 TAG_LOGE(AAFwkTag::APPMGR, "The param observer is nullptr.");
801 return false;
802 }
803 std::lock_guard<ffrt::mutex> lockRegister(observerLock_);
804 for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
805 if (it->first->AsObject() == observer->AsObject()) {
806 return true;
807 }
808 }
809 return false;
810 }
811
IsAbilityForegroundObserverExist(const sptr<IRemoteBroker> & observer)812 bool AppStateObserverManager::IsAbilityForegroundObserverExist(const sptr<IRemoteBroker> &observer)
813 {
814 if (observer == nullptr) {
815 TAG_LOGE(AAFwkTag::APPMGR, "The param observer is nullptr.");
816 return false;
817 }
818 std::lock_guard<ffrt::mutex> lockRegister(abilityforegroundObserverLock_);
819 for (auto &it : abilityforegroundObserverSet_) {
820 if (it != nullptr && it->AsObject() == observer->AsObject()) {
821 return true;
822 }
823 }
824 return false;
825 }
826
IsAppForegroundObserverExist(const sptr<IRemoteBroker> & observer)827 bool AppStateObserverManager::IsAppForegroundObserverExist(const sptr<IRemoteBroker> &observer)
828 {
829 if (observer == nullptr) {
830 TAG_LOGE(AAFwkTag::APPMGR, "The param observer is nullptr.");
831 return false;
832 }
833 std::lock_guard<ffrt::mutex> lockRegister(appForegroundObserverLock_);
834 for (auto &it : appForegroundStateObserverSet_) {
835 if (it != nullptr && it->AsObject() == observer->AsObject()) {
836 return true;
837 }
838 }
839 return false;
840 }
841
AddObserverDeathRecipient(const sptr<IRemoteBroker> & observer,const ObserverType & type)842 void AppStateObserverManager::AddObserverDeathRecipient(const sptr<IRemoteBroker> &observer, const ObserverType &type)
843 {
844 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
845 TAG_LOGD(AAFwkTag::APPMGR, "Add observer death recipient begin.");
846 if (observer == nullptr || observer->AsObject() == nullptr) {
847 TAG_LOGE(AAFwkTag::APPMGR, "The param observer is nullptr.");
848 return;
849 }
850 auto it = recipientMap_.find(observer->AsObject());
851 if (it != recipientMap_.end()) {
852 TAG_LOGE(AAFwkTag::APPMGR, "This death recipient has been added.");
853 return;
854 } else {
855 std::weak_ptr<AppStateObserverManager> thisWeakPtr(shared_from_this());
856 sptr<IRemoteObject::DeathRecipient> deathRecipient = nullptr;
857 auto deathRecipientFunc = [thisWeakPtr, type](const wptr<IRemoteObject> &remote) {
858 auto appStateObserverManager = thisWeakPtr.lock();
859 if (appStateObserverManager) {
860 appStateObserverManager->OnObserverDied(remote, type);
861 }
862 };
863 if (type == ObserverType::APPLICATION_STATE_OBSERVER) {
864 deathRecipient = new (std::nothrow) ApplicationStateObserverRecipient(deathRecipientFunc);
865 } else if (type == ObserverType::APP_FOREGROUND_STATE_OBSERVER) {
866 deathRecipient = new (std::nothrow) AppForegroundStateObserverRecipient(deathRecipientFunc);
867 } else if (type == ObserverType::ABILITY_FOREGROUND_STATE_OBSERVER) {
868 deathRecipient = new (std::nothrow) AbilityForegroundStateObserverRecipient(deathRecipientFunc);
869 } else {
870 TAG_LOGW(AAFwkTag::APPMGR, "ObserverType is not exists");
871 return;
872 }
873 if (deathRecipient == nullptr) {
874 TAG_LOGE(AAFwkTag::APPMGR, "deathRecipient is nullptr.");
875 return;
876 }
877 if (!observer->AsObject()->AddDeathRecipient(deathRecipient)) {
878 TAG_LOGE(AAFwkTag::APPMGR, "AddDeathRecipient failed.");
879 }
880 recipientMap_.emplace(observer->AsObject(), deathRecipient);
881 }
882 }
883
RemoveObserverDeathRecipient(const sptr<IRemoteBroker> & observer)884 void AppStateObserverManager::RemoveObserverDeathRecipient(const sptr<IRemoteBroker> &observer)
885 {
886 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
887 TAG_LOGD(AAFwkTag::APPMGR, "Remove observer death recipient begin.");
888 if (observer == nullptr || observer->AsObject() == nullptr) {
889 TAG_LOGE(AAFwkTag::APPMGR, "The param observer is nullptr.");
890 return;
891 }
892 auto it = recipientMap_.find(observer->AsObject());
893 if (it != recipientMap_.end()) {
894 it->first->RemoveDeathRecipient(it->second);
895 recipientMap_.erase(it);
896 return;
897 }
898 }
899
GetAppStateObserverMapCopy()900 AppStateObserverMap AppStateObserverManager::GetAppStateObserverMapCopy()
901 {
902 std::lock_guard<ffrt::mutex> lock(observerLock_);
903 return appStateObserverMap_;
904 }
905
GetAppForegroundStateObserverSetCopy()906 AppForegroundStateObserverSet AppStateObserverManager::GetAppForegroundStateObserverSetCopy()
907 {
908 std::lock_guard<ffrt::mutex> lock(appForegroundObserverLock_);
909 return appForegroundStateObserverSet_;
910 }
911
GetAbilityforegroundObserverSetCopy()912 AbilityforegroundObserverSet AppStateObserverManager::GetAbilityforegroundObserverSetCopy()
913 {
914 std::lock_guard<ffrt::mutex> lock(abilityforegroundObserverLock_);
915 return abilityforegroundObserverSet_;
916 }
917
OnObserverDied(const wptr<IRemoteObject> & remote,const ObserverType & type)918 void AppStateObserverManager::OnObserverDied(const wptr<IRemoteObject> &remote, const ObserverType &type)
919 {
920 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
921 TAG_LOGD(AAFwkTag::APPMGR, "OnObserverDied");
922 auto object = remote.promote();
923 if (object == nullptr) {
924 TAG_LOGE(AAFwkTag::APPMGR, "observer nullptr.");
925 return;
926 }
927
928 if (type == ObserverType::APPLICATION_STATE_OBSERVER) {
929 sptr<IApplicationStateObserver> observer = iface_cast<IApplicationStateObserver>(object);
930 UnregisterApplicationStateObserver(observer);
931 } else if (type == ObserverType::ABILITY_FOREGROUND_STATE_OBSERVER) {
932 sptr<IAbilityForegroundStateObserver> observer = iface_cast<IAbilityForegroundStateObserver>(object);
933 UnregisterAbilityForegroundStateObserver(observer);
934 } else if (type == ObserverType::APP_FOREGROUND_STATE_OBSERVER) {
935 sptr<IAppForegroundStateObserver> observer = iface_cast<IAppForegroundStateObserver>(object);
936 UnregisterAppForegroundStateObserver(observer);
937 } else {
938 TAG_LOGW(AAFwkTag::APPMGR, "ObserverType is not exists");
939 return;
940 }
941 }
942
WrapAppStateData(const std::shared_ptr<AppRunningRecord> & appRecord,const ApplicationState state)943 AppStateData AppStateObserverManager::WrapAppStateData(const std::shared_ptr<AppRunningRecord> &appRecord,
944 const ApplicationState state)
945 {
946 AppStateData appStateData;
947 appStateData.pid = appRecord->GetPriorityObject()->GetPid();
948 appStateData.bundleName = appRecord->GetBundleName();
949 appStateData.state = static_cast<int32_t>(state);
950 appStateData.uid = appRecord->GetUid();
951 appStateData.extensionType = appRecord->GetExtensionType();
952 if (appRecord->GetApplicationInfo() != nullptr) {
953 appStateData.accessTokenId = static_cast<uint32_t>(appRecord->GetApplicationInfo()->accessTokenId);
954 }
955 appStateData.isFocused = appRecord->GetFocusFlag();
956 auto renderRecordMap = appRecord->GetRenderRecordMap();
957 if (!renderRecordMap.empty()) {
958 for (auto iter : renderRecordMap) {
959 auto renderRecord = iter.second;
960 if (renderRecord != nullptr) {
961 appStateData.renderPids.emplace_back(renderRecord->GetPid());
962 }
963 }
964 }
965 std::shared_ptr<RemoteClientManager> remoteClientManager = std::make_shared<RemoteClientManager>();
966 auto bundleMgr = remoteClientManager->GetBundleManagerHelper();
967 std::string callerBundleName;
968 if (bundleMgr != nullptr &&
969 IN_PROCESS_CALL(bundleMgr->GetNameForUid(appRecord->GetCallerUid(), callerBundleName)) == ERR_OK) {
970 appStateData.callerBundleName = callerBundleName;
971 } else {
972 appStateData.callerBundleName = "";
973 }
974 appStateData.appIndex = appRecord->GetAppIndex();
975 TAG_LOGD(AAFwkTag::APPMGR, "Handle state change, bundle:%{public}s, state:%{public}d,"
976 "pid:%{public}d ,uid:%{public}d, isFocused:%{public}d, callerBUndleName: %{public}s, appIndex:%{public}d",
977 appStateData.bundleName.c_str(), appStateData.state, appStateData.pid, appStateData.uid,
978 appStateData.isFocused, appStateData.callerBundleName.c_str(), appStateData.appIndex);
979 return appStateData;
980 }
981
OnPageShow(const PageStateData pageStateData)982 void AppStateObserverManager::OnPageShow(const PageStateData pageStateData)
983 {
984 TAG_LOGD(AAFwkTag::APPMGR, "call");
985 if (handler_ == nullptr) {
986 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnProcessCreated failed.");
987 return;
988 }
989
990 auto task = [weak = weak_from_this(), pageStateData]() {
991 auto self = weak.lock();
992 if (self == nullptr) {
993 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnProcessCreated failed.");
994 return;
995 }
996 TAG_LOGD(AAFwkTag::APPMGR, "OnProcessCreated come.");
997 self->HandleOnPageShow(pageStateData);
998 };
999 handler_->SubmitTask(task);
1000 }
1001
OnPageHide(const PageStateData pageStateData)1002 void AppStateObserverManager::OnPageHide(const PageStateData pageStateData)
1003 {
1004 TAG_LOGD(AAFwkTag::APPMGR, "call");
1005 if (handler_ == nullptr) {
1006 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnProcessCreated failed.");
1007 return;
1008 }
1009
1010 auto task = [weak = weak_from_this(), pageStateData]() {
1011 auto self = weak.lock();
1012 if (self == nullptr) {
1013 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnProcessCreated failed.");
1014 return;
1015 }
1016 TAG_LOGD(AAFwkTag::APPMGR, "OnProcessCreated come.");
1017 self->HandleOnPageHide(pageStateData);
1018 };
1019 handler_->SubmitTask(task);
1020 }
1021
HandleOnPageShow(const PageStateData pageStateData)1022 void AppStateObserverManager::HandleOnPageShow(const PageStateData pageStateData)
1023 {
1024 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
1025 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
1026 std::vector<std::string>::iterator iter = std::find(it->second.begin(),
1027 it->second.end(), pageStateData.bundleName);
1028 if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
1029 it->first->OnPageShow(pageStateData);
1030 }
1031 }
1032 }
1033
HandleOnPageHide(const PageStateData pageStateData)1034 void AppStateObserverManager::HandleOnPageHide(const PageStateData pageStateData)
1035 {
1036 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
1037 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
1038 std::vector<std::string>::iterator iter = std::find(it->second.begin(),
1039 it->second.end(), pageStateData.bundleName);
1040 if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
1041 it->first->OnPageHide(pageStateData);
1042 }
1043 }
1044 }
1045
OnAppCacheStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord,ApplicationState state)1046 void AppStateObserverManager::OnAppCacheStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord,
1047 ApplicationState state)
1048 {
1049 if (handler_ == nullptr) {
1050 TAG_LOGE(AAFwkTag::APPMGR, "handler is nullptr, OnAppCacheStateChanged failed.");
1051 return;
1052 }
1053
1054 auto task = [weak = weak_from_this(), appRecord, state]() {
1055 auto self = weak.lock();
1056 if (self == nullptr) {
1057 TAG_LOGE(AAFwkTag::APPMGR, "self is nullptr, OnAppCacheStateChanged failed.");
1058 return;
1059 }
1060 TAG_LOGD(AAFwkTag::APPMGR, "OnAppCacheStateChanged come.");
1061 self->HandleOnAppCacheStateChanged(appRecord, state);
1062 };
1063 handler_->SubmitTask(task);
1064 }
1065
HandleOnAppCacheStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord,ApplicationState state)1066 void AppStateObserverManager::HandleOnAppCacheStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord,
1067 ApplicationState state)
1068 {
1069 if (appRecord == nullptr) {
1070 TAG_LOGE(AAFwkTag::APPMGR, "app record is null");
1071 return;
1072 }
1073
1074 AppStateData data = WrapAppStateData(appRecord, state);
1075 data.isSpecifyTokenId = appRecord->GetAssignTokenId() > 0 ? true : false;
1076 TAG_LOGD(AAFwkTag::APPMGR, "HandleOnAppCacheStateChanged, bundle:%{public}s, uid:%{public}d, state:%{public}d",
1077 data.bundleName.c_str(), data.uid, data.state);
1078 auto appStateObserverMapCopy = GetAppStateObserverMapCopy();
1079 for (auto it = appStateObserverMapCopy.begin(); it != appStateObserverMapCopy.end(); ++it) {
1080 std::vector<std::string>::iterator iter = std::find(it->second.begin(),
1081 it->second.end(), data.bundleName);
1082 if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
1083 it->first->OnAppCacheStateChanged(data);
1084 }
1085 }
1086 }
1087 } // namespace AppExecFwk
1088 } // namespace OHOS
1089