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 "ISessionService.h"
20 #include "log_tags.h"
21
22 namespace OHOS {
23 using Communication::SoftBus::ISessionService;
24
25 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_RPC, "DbinderDeathRecipient" };
26
OnRemoteDied(const wptr<IRemoteObject> & remote)27 void DbinderDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
28 {
29 DBINDER_LOGE(LOG_LABEL, "DbinderDeathRecipient OnRemoteDied");
30 if (remote == nullptr) {
31 DBINDER_LOGE(LOG_LABEL, "remote object is null");
32 return;
33 }
34
35 sptr<IRemoteObject> object = remote.promote();
36 IPCObjectProxy *callbackProxy = reinterpret_cast<IPCObjectProxy *>(object.GetRefPtr());
37
38 sptr<DBinderService> dBinderService = DBinderService::GetInstance();
39 if (dBinderService == nullptr) {
40 DBINDER_LOGE(LOG_LABEL, "dBinderService is null");
41 return;
42 }
43
44 std::shared_ptr<ISessionService> softbusManager = ISessionService::GetInstance();
45 if (softbusManager == nullptr) {
46 DBINDER_LOGE(LOG_LABEL, "fail to get softbus service");
47 return;
48 }
49 std::string sessionName = dBinderService->QueryBusNameObject(callbackProxy);
50 if (sessionName.empty()) {
51 DBINDER_LOGE(LOG_LABEL, "proxy sessionName not found");
52 return;
53 }
54 softbusManager->RemovePermission(sessionName);
55
56 sptr<IRemoteObject::DeathRecipient> death = dBinderService->QueryDeathRecipient(object);
57 if (death != nullptr) {
58 // Continue to clear subsequent data
59 callbackProxy->RemoveDeathRecipient(death);
60 }
61
62 if (!dBinderService->DetachDeathRecipient(object)) {
63 DBINDER_LOGE(LOG_LABEL, "detaching death recipient is failed");
64 return;
65 }
66
67 if (!dBinderService->DetachCallbackProxy(object)) {
68 DBINDER_LOGE(LOG_LABEL, "detaching callback proxy is failed");
69 return;
70 }
71 }
72 } // namespace OHOS
73