1 /*
2 * Copyright (c) 2023 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 #include <map>
16 #include <set>
17 #include <mutex>
18 #include <iproxy_broker.h>
19 #include <codec_death_recipient.h>
20 #include "codec_log_wrapper.h"
21
22 namespace OHOS {
23 namespace HDI {
24 namespace Codec {
25 namespace V4_0 {
26 struct ComponentRemoteInfo {
27 IRemoteObject *remoteObj{nullptr};
28 uint32_t remotePid{0};
29 };
30
31 static std::map<IRemoteObject *, std::set<uint32_t>> g_remoteCompsMap;
32 static std::map<IRemoteObject *, sptr<CodecDeathRecipient>> g_deathReciMap;
33 static std::map<uint32_t, ComponentRemoteInfo> g_compRemoteMap;
34 static std::mutex g_mutex;
35
CleanResourceOfDiedService(sptr<IRemoteObject> object,wptr<CodecComponentManagerService> managerService)36 void CleanResourceOfDiedService(sptr<IRemoteObject> object, wptr<CodecComponentManagerService> managerService)
37 {
38 std::set<uint32_t> compIds {};
39 {
40 std::lock_guard<std::mutex> lk(g_mutex);
41 auto remoteComps = g_remoteCompsMap.find(object.GetRefPtr());
42 if (remoteComps == g_remoteCompsMap.end()) {
43 CODEC_LOGE("can not find remote service in g_remoteCompsMap!");
44 return;
45 }
46 compIds = remoteComps->second;
47 }
48
49 for (auto id = compIds.begin(); id != compIds.end(); id++) {
50 managerService->DestroyComponent(*id);
51 }
52 CODEC_LOGI("Clean died remoteService resource success!");
53 }
54
RegisterDeathRecipientService(const sptr<ICodecCallback> callbacks,uint32_t componentId,wptr<CodecComponentManagerService> service)55 void RegisterDeathRecipientService(const sptr<ICodecCallback> callbacks, uint32_t componentId,
56 wptr<CodecComponentManagerService> service)
57 {
58 std::unique_lock<std::mutex> lk(g_mutex);
59
60 const sptr<OHOS::IRemoteObject> &remote = OHOS::HDI::hdi_objcast<ICodecCallback>(callbacks);
61 uint32_t remotePid = static_cast<uint32_t>(HdfRemoteGetCallingPid());
62 ComponentRemoteInfo compRemote {remote.GetRefPtr(), remotePid};
63 g_compRemoteMap.emplace(componentId, compRemote);
64
65 auto remoteComps = g_remoteCompsMap.find(remote.GetRefPtr());
66 if (remoteComps != g_remoteCompsMap.end()) {
67 CODEC_LOGI("RemoteService had been added deathRecipient!");
68 remoteComps->second.insert(componentId);
69 return;
70 }
71
72 const sptr<CodecDeathRecipient> deathCallBack(new CodecDeathRecipient(service));
73 bool ret = remote->AddDeathRecipient(deathCallBack);
74 if (!ret) {
75 CODEC_LOGE("RemoteService add deathRecipient fail!");
76 return ;
77 }
78
79 std::set<uint32_t> compIds;
80 compIds.insert(componentId);
81 g_remoteCompsMap.emplace(std::make_pair(remote.GetRefPtr(), compIds));
82 g_deathReciMap[remote.GetRefPtr()] = deathCallBack;
83 CODEC_LOGI("Add deathRecipient success!");
84 }
85
RemoveMapperOfDestoryedComponent(uint32_t componentId)86 void RemoveMapperOfDestoryedComponent(uint32_t componentId)
87 {
88 std::unique_lock<std::mutex> lk(g_mutex);
89
90 auto compRemote = g_compRemoteMap.find(componentId);
91 if (compRemote == g_compRemoteMap.end()) {
92 return;
93 }
94
95 IRemoteObject *remote = compRemote->second.remoteObj;
96 auto remoteComps = g_remoteCompsMap.find(remote);
97 if (remoteComps == g_remoteCompsMap.end()) {
98 return;
99 }
100 remoteComps->second.erase(componentId);
101 g_compRemoteMap.erase(compRemote);
102 if (!remoteComps->second.empty()) {
103 return;
104 }
105
106 g_remoteCompsMap.erase(remoteComps);
107 auto deathReci = g_deathReciMap.find(remote);
108 if (deathReci == g_deathReciMap.end()) {
109 CODEC_LOGE("%{public}s: not find recipient", __func__);
110 return;
111 }
112 bool result = remote->RemoveDeathRecipient(deathReci->second);
113 g_deathReciMap.erase(deathReci);
114 if (!result) {
115 CODEC_LOGE("%{public}s: removeDeathRecipient fail", __func__);
116 return;
117 }
118 CODEC_LOGI("Remove mapper destoryedComponent success!");
119 }
120
CheckComponentIdOwnership(uint32_t componentId)121 bool CheckComponentIdOwnership(uint32_t componentId)
122 {
123 std::unique_lock<std::mutex> lk(g_mutex);
124 auto compRemote = g_compRemoteMap.find(componentId);
125 if (compRemote == g_compRemoteMap.end()) {
126 CODEC_LOGE("invalid componentId %{public}d", componentId);
127 return false;
128 }
129 uint32_t remotePid = compRemote->second.remotePid;
130 uint32_t curPid = static_cast<uint32_t>(HdfRemoteGetCallingPid());
131 if (remotePid != curPid) {
132 CODEC_LOGE("componentId %{public}d do not belong to this pid %{public}d", componentId, curPid);
133 return false;
134 }
135 return true;
136 }
137 } // namespace V4_0
138 } // namespace Codec
139 } // namespace HDI
140 } // namespace OHOS
141