1 /*
2 * Copyright (c) 2021-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_death_recipient.h"
17 #include "app_mgr_service_inner.h"
18 #include "hilog_tag_wrapper.h"
19
20 namespace OHOS {
21 namespace AppExecFwk {
22 namespace {
23 const std::string TASK_ON_REMOTE_DIED = "OnRemoteDiedTask";
24 } // namespace
25
OnRemoteDied(const wptr<IRemoteObject> & remote)26 void AppDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
27 {
28 if (remote == nullptr) {
29 TAG_LOGE(AAFwkTag::APPMGR, "null remote");
30 return;
31 }
32
33 auto handler = handler_.lock();
34 if (!handler) {
35 TAG_LOGE(AAFwkTag::APPMGR, "null handler");
36 return;
37 }
38
39 auto onRemoteDiedFunc = [appMgrServiceInner = appMgrServiceInner_, remote,
40 isRenderProcess = isRenderProcess_,
41 isChildProcess = isChildProcess_]() {
42 auto serviceInner = appMgrServiceInner.lock();
43 if (!serviceInner) {
44 TAG_LOGE(AAFwkTag::APPMGR, "null serviceInner");
45 return;
46 }
47 serviceInner->OnRemoteDied(remote, isRenderProcess, isChildProcess);
48 TAG_LOGW(AAFwkTag::APPMGR, "OnRemoteDiedTask end");
49 };
50 handler->SubmitTask(onRemoteDiedFunc, TASK_ON_REMOTE_DIED);
51 }
52
SetTaskHandler(const std::shared_ptr<AAFwk::TaskHandlerWrap> & handler)53 void AppDeathRecipient::SetTaskHandler(const std::shared_ptr<AAFwk::TaskHandlerWrap> &handler)
54 {
55 handler_ = handler;
56 }
57
SetAppMgrServiceInner(const std::shared_ptr<AppMgrServiceInner> & serviceInner)58 void AppDeathRecipient::SetAppMgrServiceInner(const std::shared_ptr<AppMgrServiceInner> &serviceInner)
59 {
60 appMgrServiceInner_ = serviceInner;
61 }
62
SetIsRenderProcess(bool isRenderProcess)63 void AppDeathRecipient::SetIsRenderProcess(bool isRenderProcess)
64 {
65 isRenderProcess_ = isRenderProcess;
66 }
67
SetIsChildProcess(bool isChildProcess)68 void AppDeathRecipient::SetIsChildProcess(bool isChildProcess)
69 {
70 isChildProcess_ = isChildProcess;
71 }
72
AppStateCallbackDeathRecipient(std::weak_ptr<AppMgrServiceInner> appMgrServiceInner)73 AppStateCallbackDeathRecipient::AppStateCallbackDeathRecipient(std::weak_ptr<AppMgrServiceInner> appMgrServiceInner)
74 : appMgrServiceInner_(appMgrServiceInner) {}
75
OnRemoteDied(const wptr<IRemoteObject> & remote)76 void AppStateCallbackDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
77 {
78 auto appMgrInner = appMgrServiceInner_.lock();
79 if (appMgrInner) {
80 appMgrInner->RemoveDeadAppStateCallback(remote);
81 }
82 }
83 } // namespace AppExecFwk
84 } // namespace OHOS
85