1 /*
2  * Copyright (C) 2021 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 "dbinder_death_recipient.h"
17 #include "dbinder_service.h"
18 #include "dbinder_log.h"
19 #include "log_tags.h"
20 
21 namespace OHOS {
22 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_RPC_DBINDER_SER, "DbinderDeathRecipient" };
23 
OnRemoteDied(const wptr<IRemoteObject> &remote)24 void DbinderDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
25 {
26     DBINDER_LOGE(LOG_LABEL, "DbinderDeathRecipient OnRemoteDied");
27     if (remote == nullptr) {
28         DBINDER_LOGE(LOG_LABEL, "remote object is null");
29         return;
30     }
31 
32     sptr<IRemoteObject> object = remote.promote();
33     if (object == nullptr) {
34         DBINDER_LOGE(LOG_LABEL, "Remote object is null");
35         return;
36     }
37     IPCObjectProxy *callbackProxy = reinterpret_cast<IPCObjectProxy *>(object.GetRefPtr());
38 
39     sptr<DBinderService> dBinderService = DBinderService::GetInstance();
40     if (dBinderService == nullptr) {
41         DBINDER_LOGE(LOG_LABEL, "dBinderService is null");
42         return;
43     }
44 
45     sptr<IRemoteObject::DeathRecipient> death = dBinderService->QueryDeathRecipient(object);
46     if (death != nullptr) {
47         // Continue to clear subsequent data
48         callbackProxy->RemoveDeathRecipient(death);
49     }
50 
51     if (!dBinderService->DetachDeathRecipient(object)) {
52         DBINDER_LOGE(LOG_LABEL, "detaching death recipient is failed");
53         return;
54     }
55 
56     if (!dBinderService->DetachCallbackProxy(object)) {
57         DBINDER_LOGE(LOG_LABEL, "detaching callback proxy is failed");
58         return;
59     }
60 }
61 } // namespace OHOS
62