1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "app_state_observer_manager.h"
17 #include "application_state_observer_stub.h"
18 #include "hilog_wrapper.h"
19 #include "ui_extension_utils.h"
20
21 namespace OHOS {
22 namespace AppExecFwk {
23 namespace {
24 const std::string THREAD_NAME = "AppStateObserverManager";
25 const int BUNDLE_NAME_LIST_MAX_SIZE = 128;
26 } // namespace
AppStateObserverManager()27 AppStateObserverManager::AppStateObserverManager()
28 {
29 HILOG_DEBUG("AppStateObserverManager instance is created");
30 }
31
~AppStateObserverManager()32 AppStateObserverManager::~AppStateObserverManager()
33 {
34 HILOG_DEBUG("AppStateObserverManager instance is destroyed");
35 }
36
Init()37 void AppStateObserverManager::Init()
38 {
39 if (!handler_) {
40 handler_ = AAFwk::TaskHandlerWrap::CreateQueueHandler("app_state_task_queue");
41 }
42 }
43
RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer,const std::vector<std::string> & bundleNameList)44 int32_t AppStateObserverManager::RegisterApplicationStateObserver(
45 const sptr<IApplicationStateObserver> &observer, const std::vector<std::string> &bundleNameList)
46 {
47 HILOG_DEBUG("called");
48 if (bundleNameList.size() > BUNDLE_NAME_LIST_MAX_SIZE) {
49 HILOG_ERROR("the bundleNameList passed in is too long");
50 return ERR_INVALID_VALUE;
51 }
52 if (AAFwk::PermissionVerification::GetInstance()->VerifyAppStateObserverPermission() == ERR_PERMISSION_DENIED) {
53 HILOG_ERROR("Permission verification failed");
54 return ERR_PERMISSION_DENIED;
55 }
56 if (observer == nullptr) {
57 HILOG_ERROR("The param observer is nullptr.");
58 return ERR_INVALID_VALUE;
59 }
60 if (ObserverExist(observer)) {
61 HILOG_ERROR("Observer exist.");
62 return ERR_INVALID_VALUE;
63 }
64 std::lock_guard<ffrt::mutex> lockRegister(observerLock_);
65 appStateObserverMap_.emplace(observer, bundleNameList);
66 HILOG_DEBUG("appStateObserverMap_ size:%{public}zu", appStateObserverMap_.size());
67 AddObserverDeathRecipient(observer);
68 return ERR_OK;
69 }
70
UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)71 int32_t AppStateObserverManager::UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer)
72 {
73 HILOG_DEBUG("called");
74 if (AAFwk::PermissionVerification::GetInstance()->VerifyAppStateObserverPermission() == ERR_PERMISSION_DENIED) {
75 HILOG_ERROR("Permission verification failed");
76 return ERR_PERMISSION_DENIED;
77 }
78 std::lock_guard<ffrt::mutex> lockUnregister(observerLock_);
79 if (observer == nullptr) {
80 HILOG_ERROR("Observer nullptr");
81 return ERR_INVALID_VALUE;
82 }
83 std::map<sptr<IApplicationStateObserver>, std::vector<std::string>>::iterator it;
84 for (it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
85 if (it->first->AsObject() == observer->AsObject()) {
86 appStateObserverMap_.erase(it);
87 HILOG_DEBUG("appStateObserverMap_ size:%{public}zu", appStateObserverMap_.size());
88 RemoveObserverDeathRecipient(observer);
89 return ERR_OK;
90 }
91 }
92 HILOG_ERROR("Observer not exist.");
93 return ERR_INVALID_VALUE;
94 }
95
OnAppStarted(const std::shared_ptr<AppRunningRecord> & appRecord)96 void AppStateObserverManager::OnAppStarted(const std::shared_ptr<AppRunningRecord> &appRecord)
97 {
98 if (handler_ == nullptr) {
99 HILOG_ERROR("handler is nullptr, OnAppStarted failed.");
100 return;
101 }
102
103 auto task = [weak = weak_from_this(), appRecord]() {
104 auto self = weak.lock();
105 if (self == nullptr) {
106 HILOG_ERROR("self is nullptr, OnAppStarted failed.");
107 return;
108 }
109 HILOG_DEBUG("OnAppStarted come.");
110 self->HandleOnAppStarted(appRecord);
111 };
112 handler_->SubmitTask(task);
113 }
114
OnAppStopped(const std::shared_ptr<AppRunningRecord> & appRecord)115 void AppStateObserverManager::OnAppStopped(const std::shared_ptr<AppRunningRecord> &appRecord)
116 {
117 if (handler_ == nullptr) {
118 HILOG_ERROR("handler is nullptr, OnAppStopped failed.");
119 return;
120 }
121
122 auto task = [weak = weak_from_this(), appRecord]() {
123 auto self = weak.lock();
124 if (self == nullptr) {
125 HILOG_ERROR("self is nullptr, OnAppStopped failed.");
126 return;
127 }
128 HILOG_DEBUG("OnAppStopped come.");
129 self->HandleOnAppStopped(appRecord);
130 };
131 handler_->SubmitTask(task);
132 }
133
134
OnAppStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord,const ApplicationState state,bool needNotifyApp)135 void AppStateObserverManager::OnAppStateChanged(
136 const std::shared_ptr<AppRunningRecord> &appRecord, const ApplicationState state, bool needNotifyApp)
137 {
138 if (handler_ == nullptr) {
139 HILOG_ERROR("handler is nullptr, OnAppStateChanged failed.");
140 return;
141 }
142
143 auto task = [weak = weak_from_this(), appRecord, state, needNotifyApp]() {
144 auto self = weak.lock();
145 if (self == nullptr) {
146 HILOG_ERROR("self is nullptr, OnAppStateChanged failed.");
147 return;
148 }
149 HILOG_DEBUG("OnAppStateChanged come.");
150 self->HandleAppStateChanged(appRecord, state, needNotifyApp);
151 };
152 handler_->SubmitTask(task);
153 }
154
OnProcessDied(const std::shared_ptr<AppRunningRecord> & appRecord)155 void AppStateObserverManager::OnProcessDied(const std::shared_ptr<AppRunningRecord> &appRecord)
156 {
157 if (handler_ == nullptr) {
158 HILOG_ERROR("handler is nullptr, OnProcessDied failed.");
159 return;
160 }
161
162 auto task = [weak = weak_from_this(), appRecord]() {
163 auto self = weak.lock();
164 if (self == nullptr) {
165 HILOG_ERROR("self is nullptr, OnProcessDied failed.");
166 return;
167 }
168 HILOG_DEBUG("OnProcessDied come.");
169 self->HandleOnAppProcessDied(appRecord);
170 };
171 handler_->SubmitTask(task);
172 }
173
OnRenderProcessDied(const std::shared_ptr<RenderRecord> & renderRecord)174 void AppStateObserverManager::OnRenderProcessDied(const std::shared_ptr<RenderRecord> &renderRecord)
175 {
176 if (handler_ == nullptr) {
177 HILOG_ERROR("handler is nullptr, OnRenderProcessDied failed.");
178 return;
179 }
180
181 auto task = [weak = weak_from_this(), renderRecord]() {
182 auto self = weak.lock();
183 if (self == nullptr) {
184 HILOG_ERROR("self is nullptr, OnRenderProcessDied failed.");
185 return;
186 }
187 HILOG_DEBUG("OnRenderProcessDied come.");
188 self->HandleOnRenderProcessDied(renderRecord);
189 };
190 handler_->SubmitTask(task);
191 }
192
OnProcessStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord)193 void AppStateObserverManager::OnProcessStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord)
194 {
195 if (handler_ == nullptr) {
196 HILOG_ERROR("handler is nullptr, OnProcessStateChanged failed.");
197 return;
198 }
199
200 auto task = [weak = weak_from_this(), appRecord]() {
201 auto self = weak.lock();
202 if (self == nullptr) {
203 HILOG_ERROR("self is nullptr, OnProcessStateChanged failed.");
204 return;
205 }
206 HILOG_DEBUG("OnProcessStateChanged come.");
207 self->HandleOnProcessStateChanged(appRecord);
208 };
209 handler_->SubmitTask(task);
210 }
211
OnProcessCreated(const std::shared_ptr<AppRunningRecord> & appRecord)212 void AppStateObserverManager::OnProcessCreated(const std::shared_ptr<AppRunningRecord> &appRecord)
213 {
214 if (handler_ == nullptr) {
215 HILOG_ERROR("handler is nullptr, OnProcessCreated failed.");
216 return;
217 }
218
219 auto task = [weak = weak_from_this(), appRecord]() {
220 auto self = weak.lock();
221 if (self == nullptr) {
222 HILOG_ERROR("self is nullptr, OnProcessCreated failed.");
223 return;
224 }
225 HILOG_DEBUG("OnProcessCreated come.");
226 self->HandleOnAppProcessCreated(appRecord);
227 };
228 handler_->SubmitTask(task);
229 }
230
OnProcessReused(const std::shared_ptr<AppRunningRecord> & appRecord)231 void AppStateObserverManager::OnProcessReused(const std::shared_ptr<AppRunningRecord> &appRecord)
232 {
233 if (handler_ == nullptr) {
234 HILOG_ERROR("handler is nullptr, OnProcessReused failed.");
235 return;
236 }
237
238 auto task = [weak = weak_from_this(), appRecord]() {
239 auto self = weak.lock();
240 if (self == nullptr) {
241 HILOG_ERROR("self is nullptr, OnProcessReused failed.");
242 return;
243 }
244 HILOG_DEBUG("OnProcessReused come.");
245 self->HandleOnProcessResued(appRecord);
246 };
247 handler_->SubmitTask(task);
248 }
249
OnRenderProcessCreated(const std::shared_ptr<RenderRecord> & renderRecord)250 void AppStateObserverManager::OnRenderProcessCreated(const std::shared_ptr<RenderRecord> &renderRecord)
251 {
252 if (handler_ == nullptr) {
253 HILOG_ERROR("handler is nullptr, OnRenderProcessCreated failed.");
254 return;
255 }
256
257 auto task = [weak = weak_from_this(), renderRecord]() {
258 auto self = weak.lock();
259 if (self == nullptr) {
260 HILOG_ERROR("self is nullptr, OnRenderProcessCreated failed.");
261 return;
262 }
263 HILOG_DEBUG("OnRenderProcessCreated come.");
264 self->HandleOnRenderProcessCreated(renderRecord);
265 };
266 handler_->SubmitTask(task);
267 }
268
StateChangedNotifyObserver(const AbilityStateData abilityStateData,bool isAbility)269 void AppStateObserverManager::StateChangedNotifyObserver(const AbilityStateData abilityStateData, bool isAbility)
270 {
271 if (handler_ == nullptr) {
272 HILOG_ERROR("handler is nullptr, StateChangedNotifyObserver failed.");
273 return;
274 }
275
276 auto task = [weak = weak_from_this(), abilityStateData, isAbility]() {
277 auto self = weak.lock();
278 if (self == nullptr) {
279 HILOG_ERROR("self is nullptr, StateChangedNotifyObserver failed.");
280 return;
281 }
282 HILOG_DEBUG("StateChangedNotifyObserver come.");
283 self->HandleStateChangedNotifyObserver(abilityStateData, isAbility);
284 };
285 handler_->SubmitTask(task);
286 }
287
HandleOnAppStarted(const std::shared_ptr<AppRunningRecord> & appRecord)288 void AppStateObserverManager::HandleOnAppStarted(const std::shared_ptr<AppRunningRecord> &appRecord)
289 {
290 if (appRecord == nullptr) {
291 HILOG_ERROR("app record is null");
292 return;
293 }
294
295 AppStateData data = WrapAppStateData(appRecord, ApplicationState::APP_STATE_CREATE);
296 HILOG_DEBUG("HandleOnAppStarted, bundle:%{public}s, uid:%{public}d, state:%{public}d",
297 data.bundleName.c_str(), data.uid, data.state);
298 std::lock_guard<ffrt::mutex> lockNotify(observerLock_);
299 for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
300 std::vector<std::string>::iterator iter = std::find(it->second.begin(),
301 it->second.end(), data.bundleName);
302 if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
303 it->first->OnAppStarted(data);
304 }
305 }
306 }
307
HandleOnAppStopped(const std::shared_ptr<AppRunningRecord> & appRecord)308 void AppStateObserverManager::HandleOnAppStopped(const std::shared_ptr<AppRunningRecord> &appRecord)
309 {
310 if (appRecord == nullptr) {
311 HILOG_ERROR("app record is null");
312 return;
313 }
314
315 AppStateData data = WrapAppStateData(appRecord, ApplicationState::APP_STATE_TERMINATED);
316 HILOG_DEBUG("HandleOnAppStopped, bundle:%{public}s, uid:%{public}d, state:%{public}d",
317 data.bundleName.c_str(), data.uid, data.state);
318 std::lock_guard<ffrt::mutex> lockNotify(observerLock_);
319 for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
320 std::vector<std::string>::iterator iter = std::find(it->second.begin(),
321 it->second.end(), data.bundleName);
322 if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
323 it->first->OnAppStopped(data);
324 }
325 }
326 }
327
HandleAppStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord,const ApplicationState state,bool needNotifyApp)328 void AppStateObserverManager::HandleAppStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord,
329 const ApplicationState state, bool needNotifyApp)
330 {
331 if (appRecord == nullptr) {
332 return;
333 }
334
335 if ((state == ApplicationState::APP_STATE_FOREGROUND || state == ApplicationState::APP_STATE_BACKGROUND)
336 && !AAFwk::UIExtensionUtils::IsUIExtension(appRecord->GetExtensionType())
337 && !AAFwk::UIExtensionUtils::IsWindowExtension(appRecord->GetExtensionType())) {
338 AppStateData data = WrapAppStateData(appRecord, state);
339 HILOG_DEBUG("HandleAppStateChanged, name:%{public}s, uid:%{public}d, state:%{public}d, notify:%{public}d",
340 data.bundleName.c_str(), data.uid, data.state, needNotifyApp);
341 std::lock_guard<ffrt::mutex> lockNotify(observerLock_);
342 for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
343 std::vector<std::string>::iterator iter = std::find(it->second.begin(),
344 it->second.end(), data.bundleName);
345 bool valid = (it->second.empty() || iter != it->second.end()) && it->first != nullptr;
346 if (valid) {
347 it->first->OnForegroundApplicationChanged(data);
348 }
349 if (valid && needNotifyApp) {
350 it->first->OnAppStateChanged(data);
351 }
352 }
353 }
354
355 if (state == ApplicationState::APP_STATE_CREATE || state == ApplicationState::APP_STATE_TERMINATED) {
356 AppStateData data = WrapAppStateData(appRecord, state);
357 HILOG_DEBUG("OnApplicationStateChanged, name:%{public}s, uid:%{public}d, state:%{public}d",
358 data.bundleName.c_str(), data.uid, data.state);
359 std::lock_guard<ffrt::mutex> lockNotify(observerLock_);
360 for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
361 std::vector<std::string>::iterator iter = std::find(it->second.begin(),
362 it->second.end(), data.bundleName);
363 if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
364 it->first->OnApplicationStateChanged(data);
365 }
366 }
367 }
368 }
369
HandleStateChangedNotifyObserver(const AbilityStateData abilityStateData,bool isAbility)370 void AppStateObserverManager::HandleStateChangedNotifyObserver(const AbilityStateData abilityStateData, bool isAbility)
371 {
372 std::lock_guard<ffrt::mutex> lockNotify(observerLock_);
373 HILOG_DEBUG("Handle state change, module:%{public}s, bundle:%{public}s, ability:%{public}s, state:%{public}d,"
374 "pid:%{public}d ,uid:%{public}d, abilityType:%{public}d, isAbility:%{public}d",
375 abilityStateData.moduleName.c_str(), abilityStateData.bundleName.c_str(),
376 abilityStateData.abilityName.c_str(), abilityStateData.abilityState,
377 abilityStateData.pid, abilityStateData.uid, abilityStateData.abilityType, isAbility);
378 for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
379 std::vector<std::string>::iterator iter = std::find(it->second.begin(),
380 it->second.end(), abilityStateData.bundleName);
381 if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
382 if (isAbility) {
383 it->first->OnAbilityStateChanged(abilityStateData);
384 } else {
385 it->first->OnExtensionStateChanged(abilityStateData);
386 }
387 }
388 }
389 }
390
HandleOnAppProcessCreated(const std::shared_ptr<AppRunningRecord> & appRecord)391 void AppStateObserverManager::HandleOnAppProcessCreated(const std::shared_ptr<AppRunningRecord> &appRecord)
392 {
393 if (!appRecord) {
394 HILOG_ERROR("app record is null");
395 return;
396 }
397 ProcessData data = WrapProcessData(appRecord);
398 HILOG_INFO("Process Create, bundle:%{public}s, pid:%{public}d, uid:%{public}d, processType:%{public}d, "
399 "extensionType:%{public}d, processName:%{public}s, renderUid:%{public}d",
400 data.bundleName.c_str(), data.pid, data.uid, data.processType, data.extensionType, data.processName.c_str(),
401 data.renderUid);
402 HandleOnProcessCreated(data);
403 }
404
HandleOnProcessResued(const std::shared_ptr<AppRunningRecord> & appRecord)405 void AppStateObserverManager::HandleOnProcessResued(const std::shared_ptr<AppRunningRecord> &appRecord)
406 {
407 if (!appRecord) {
408 HILOG_ERROR("app record is null");
409 return;
410 }
411 ProcessData data = WrapProcessData(appRecord);
412 HILOG_DEBUG("Process Resued, bundle:%{public}s, pid:%{public}d, uid:%{public}d",
413 data.bundleName.c_str(), data.pid, data.uid);
414
415 std::lock_guard<ffrt::mutex> lockNotify(observerLock_);
416 for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
417 std::vector<std::string>::iterator iter = std::find(it->second.begin(),
418 it->second.end(), data.bundleName);
419 if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
420 it->first->OnProcessReused(data);
421 }
422 }
423 }
424
HandleOnRenderProcessCreated(const std::shared_ptr<RenderRecord> & renderRecord)425 void AppStateObserverManager::HandleOnRenderProcessCreated(const std::shared_ptr<RenderRecord> &renderRecord)
426 {
427 if (!renderRecord) {
428 HILOG_ERROR("render record is null");
429 return;
430 }
431 ProcessData data = WrapRenderProcessData(renderRecord);
432 HILOG_DEBUG("RenderProcess Create, bundle:%{public}s, pid:%{public}d, uid:%{public}d, processType:%{public}d, "
433 "processName:%{public}s, renderUid:%{public}d",
434 data.bundleName.c_str(), data.pid, data.uid, data.processType, data.processName.c_str(), data.renderUid);
435 HandleOnProcessCreated(data);
436 }
437
HandleOnProcessCreated(const ProcessData & data)438 void AppStateObserverManager::HandleOnProcessCreated(const ProcessData &data)
439 {
440 std::lock_guard<ffrt::mutex> lockNotify(observerLock_);
441 for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
442 std::vector<std::string>::iterator iter = std::find(it->second.begin(),
443 it->second.end(), data.bundleName);
444 if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
445 it->first->OnProcessCreated(data);
446 }
447 }
448 }
449
HandleOnProcessStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord)450 void AppStateObserverManager::HandleOnProcessStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord)
451 {
452 if (!appRecord) {
453 HILOG_ERROR("app record is null");
454 return;
455 }
456 ProcessData data = WrapProcessData(appRecord);
457 HILOG_DEBUG("bundle:%{public}s pid:%{public}d uid:%{public}d state:%{public}d isContinuousTask:%{public}d",
458 data.bundleName.c_str(), data.pid, data.uid, data.state, data.isContinuousTask);
459 std::lock_guard<ffrt::mutex> lockNotify(observerLock_);
460 for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
461 std::vector<std::string>::iterator iter = std::find(it->second.begin(),
462 it->second.end(), data.bundleName);
463 if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
464 it->first->OnProcessStateChanged(data);
465 }
466 }
467 }
468
HandleOnAppProcessDied(const std::shared_ptr<AppRunningRecord> & appRecord)469 void AppStateObserverManager::HandleOnAppProcessDied(const std::shared_ptr<AppRunningRecord> &appRecord)
470 {
471 if (!appRecord) {
472 HILOG_ERROR("app record is null");
473 return;
474 }
475 ProcessData data = WrapProcessData(appRecord);
476 HILOG_DEBUG("Process died, bundle:%{public}s, pid:%{public}d, uid:%{public}d, renderUid:%{public}d",
477 data.bundleName.c_str(), data.pid, data.uid, data.renderUid);
478 HandleOnProcessDied(data);
479 }
480
HandleOnRenderProcessDied(const std::shared_ptr<RenderRecord> & renderRecord)481 void AppStateObserverManager::HandleOnRenderProcessDied(const std::shared_ptr<RenderRecord> &renderRecord)
482 {
483 if (!renderRecord) {
484 HILOG_ERROR("render record is null");
485 return;
486 }
487 ProcessData data = WrapRenderProcessData(renderRecord);
488 HILOG_DEBUG("Render Process died, bundle:%{public}s, pid:%{public}d, uid:%{public}d, renderUid:%{public}d",
489 data.bundleName.c_str(), data.pid, data.uid, data.renderUid);
490 HandleOnProcessDied(data);
491 }
492
HandleOnProcessDied(const ProcessData & data)493 void AppStateObserverManager::HandleOnProcessDied(const ProcessData &data)
494 {
495 std::lock_guard<ffrt::mutex> lockNotify(observerLock_);
496 for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
497 std::vector<std::string>::iterator iter = std::find(it->second.begin(),
498 it->second.end(), data.bundleName);
499 if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
500 it->first->OnProcessDied(data);
501 }
502 }
503 }
504
WrapProcessData(const std::shared_ptr<AppRunningRecord> & appRecord)505 ProcessData AppStateObserverManager::WrapProcessData(const std::shared_ptr<AppRunningRecord> &appRecord)
506 {
507 ProcessData processData;
508 processData.bundleName = appRecord->GetBundleName();
509 processData.pid = appRecord->GetPriorityObject()->GetPid();
510 processData.uid = appRecord->GetUid();
511 processData.state = static_cast<AppProcessState>(appRecord->GetState());
512 processData.isContinuousTask = appRecord->IsContinuousTask();
513 processData.isKeepAlive = appRecord->IsKeepAliveApp();
514 processData.isFocused = appRecord->GetFocusFlag();
515 processData.requestProcCode = appRecord->GetRequestProcCode();
516 processData.processChangeReason = static_cast<int32_t>(appRecord->GetProcessChangeReason());
517 processData.processName = appRecord->GetProcessName();
518 processData.extensionType = AAFwk::UIExtensionUtils::ConvertType(appRecord->GetExtensionType());
519 processData.processType = appRecord->GetProcessType();
520 return processData;
521 }
522
WrapRenderProcessData(const std::shared_ptr<RenderRecord> & renderRecord)523 ProcessData AppStateObserverManager::WrapRenderProcessData(const std::shared_ptr<RenderRecord> &renderRecord)
524 {
525 ProcessData processData;
526 processData.bundleName = renderRecord->GetHostBundleName();
527 processData.pid = renderRecord->GetPid();
528 processData.uid = renderRecord->GetHostUid();
529 processData.renderUid = renderRecord->GetUid();
530 processData.processName = renderRecord->GetProcessName();
531 processData.processType = renderRecord->GetProcessType();
532 return processData;
533 }
534
ObserverExist(const sptr<IApplicationStateObserver> & observer)535 bool AppStateObserverManager::ObserverExist(const sptr<IApplicationStateObserver> &observer)
536 {
537 if (observer == nullptr) {
538 HILOG_ERROR("The param observer is nullptr.");
539 return false;
540 }
541 std::lock_guard<ffrt::mutex> lockRegister(observerLock_);
542 for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
543 if (it->first->AsObject() == observer->AsObject()) {
544 return true;
545 }
546 }
547 return false;
548 }
549
AddObserverDeathRecipient(const sptr<IApplicationStateObserver> & observer)550 void AppStateObserverManager::AddObserverDeathRecipient(const sptr<IApplicationStateObserver> &observer)
551 {
552 HILOG_DEBUG("Add observer death recipient begin.");
553 if (observer == nullptr || observer->AsObject() == nullptr) {
554 HILOG_ERROR("The param observer is nullptr.");
555 return;
556 }
557 auto it = recipientMap_.find(observer->AsObject());
558 if (it != recipientMap_.end()) {
559 HILOG_ERROR("This death recipient has been added.");
560 return;
561 } else {
562 std::weak_ptr<AppStateObserverManager> thisWeakPtr(shared_from_this());
563 sptr<IRemoteObject::DeathRecipient> deathRecipient =
564 new ApplicationStateObserverRecipient([thisWeakPtr](const wptr<IRemoteObject> &remote) {
565 auto appStateObserverManager = thisWeakPtr.lock();
566 if (appStateObserverManager) {
567 appStateObserverManager->OnObserverDied(remote);
568 }
569 });
570 if (!observer->AsObject()->AddDeathRecipient(deathRecipient)) {
571 HILOG_ERROR("AddDeathRecipient failed.");
572 }
573 recipientMap_.emplace(observer->AsObject(), deathRecipient);
574 }
575 }
576
RemoveObserverDeathRecipient(const sptr<IApplicationStateObserver> & observer)577 void AppStateObserverManager::RemoveObserverDeathRecipient(const sptr<IApplicationStateObserver> &observer)
578 {
579 HILOG_DEBUG("Remove observer death recipient begin.");
580 if (observer == nullptr || observer->AsObject() == nullptr) {
581 HILOG_ERROR("The param observer is nullptr.");
582 return;
583 }
584 auto it = recipientMap_.find(observer->AsObject());
585 if (it != recipientMap_.end()) {
586 it->first->RemoveDeathRecipient(it->second);
587 recipientMap_.erase(it);
588 return;
589 }
590 }
591
OnObserverDied(const wptr<IRemoteObject> & remote)592 void AppStateObserverManager::OnObserverDied(const wptr<IRemoteObject> &remote)
593 {
594 HILOG_INFO("OnObserverDied");
595 auto object = remote.promote();
596 if (object == nullptr) {
597 HILOG_ERROR("observer nullptr.");
598 return;
599 }
600 sptr<IApplicationStateObserver> observer = iface_cast<IApplicationStateObserver>(object);
601 UnregisterApplicationStateObserver(observer);
602 }
603
WrapAppStateData(const std::shared_ptr<AppRunningRecord> & appRecord,const ApplicationState state)604 AppStateData AppStateObserverManager::WrapAppStateData(const std::shared_ptr<AppRunningRecord> &appRecord,
605 const ApplicationState state)
606 {
607 AppStateData appStateData;
608 appStateData.pid = appRecord->GetPriorityObject()->GetPid();
609 appStateData.bundleName = appRecord->GetBundleName();
610 appStateData.state = static_cast<int32_t>(state);
611 appStateData.uid = appRecord->GetUid();
612 appStateData.extensionType = AAFwk::UIExtensionUtils::ConvertType(appRecord->GetExtensionType());
613 if (appRecord->GetApplicationInfo() != nullptr) {
614 appStateData.accessTokenId = static_cast<int32_t>(appRecord->GetApplicationInfo()->accessTokenId);
615 }
616 appStateData.isFocused = appRecord->GetFocusFlag();
617 auto renderRecordMap = appRecord->GetRenderRecordMap();
618 if (!renderRecordMap.empty()) {
619 for (auto iter : renderRecordMap) {
620 auto renderRecord = iter.second;
621 if (renderRecord != nullptr) {
622 appStateData.renderPids.emplace_back(renderRecord->GetPid());
623 }
624 }
625 }
626 return appStateData;
627 }
628 } // namespace AppExecFwk
629 } // namespace OHOS
630