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 #ifndef TITLE
23 #define TITLE __PRETTY_FUNCTION__
24 #endif
25
26 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_RPC, "DbinderDeathRecipient" };
27 #define DBINDER_LOGE(fmt, args...) \
28 (void)OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, "%{public}s %{public}d: " fmt, TITLE, __LINE__, ##args)
29 #define DBINDER_LOGI(fmt, args...) \
30 (void)OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "%{public}s %{public}d: " fmt, TITLE, __LINE__, ##args)
31
OnRemoteDied(const wptr<IRemoteObject> & remote)32 void DbinderDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
33 {
34 if (remote == nullptr) {
35 DBINDER_LOGE("remote object is null");
36 return;
37 }
38
39 sptr<IRemoteObject> object = remote.promote();
40 IPCObjectProxy *callbackProxy = reinterpret_cast<IPCObjectProxy *>(object.GetRefPtr());
41
42 sptr<DBinderService> dBinderService = DBinderService::GetInstance();
43 if (dBinderService == nullptr) {
44 DBINDER_LOGE("dBinderService is null");
45 return;
46 }
47
48 sptr<IRemoteObject::DeathRecipient> death = dBinderService->QueryDeathRecipient(object);
49 if (death != nullptr) {
50 // Continue to clear subsequent data
51 callbackProxy->RemoveDeathRecipient(death);
52 }
53
54 if (!dBinderService->DetachDeathRecipient(object)) {
55 DBINDER_LOGE("detaching death recipient is failed");
56 return;
57 }
58
59 if (!dBinderService->DetachCallbackProxy(object)) {
60 DBINDER_LOGE("detaching callback proxy is failed");
61 return;
62 }
63 }
64 } // namespace OHOS
65