• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 V2_0 {
26 
27 static std::map<IRemoteObject *, std::set<uint32_t>> g_remoteCompsMap;
28 static std::map<IRemoteObject *, sptr<CodecDeathRecipient>> g_deathReciMap;
29 static std::map<uint32_t, IRemoteObject *> g_compRemoteMap;
30 static std::mutex g_mutex;
31 
CleanResourceOfDiedService(sptr<IRemoteObject> object,wptr<CodecComponentManagerService> managerService)32 void CleanResourceOfDiedService(sptr<IRemoteObject> object, wptr<CodecComponentManagerService> managerService)
33 {
34     std::unique_lock<std::mutex> lk(g_mutex, std::defer_lock);
35 
36     lk.lock();
37     auto remoteComps = g_remoteCompsMap.find(object.GetRefPtr());
38     if (remoteComps == g_remoteCompsMap.end()) {
39         CODEC_LOGE("can not find remote service in g_remoteCompsMap!");
40         lk.unlock();
41         return;
42     }
43     lk.unlock();
44 
45     std::set<uint32_t> compIds = remoteComps->second;
46     for (auto id = compIds.begin(); id != compIds.end(); id++) {
47         managerService->DestroyComponent(*id);
48     }
49     lk.lock();
50     g_remoteCompsMap.erase(remoteComps);
51     lk.unlock();
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     g_compRemoteMap.emplace(std::make_pair(componentId, remote.GetRefPtr()));
62 
63     auto remoteComps = g_remoteCompsMap.find(remote.GetRefPtr());
64     if (remoteComps != g_remoteCompsMap.end()) {
65         CODEC_LOGI("RemoteService had been added deathRecipient!");
66         remoteComps->second.insert(componentId);
67         return;
68     }
69 
70     const sptr<CodecDeathRecipient> deathCallBack(new CodecDeathRecipient(service));
71     bool ret = remote->AddDeathRecipient(deathCallBack);
72     if (!ret) {
73         CODEC_LOGE("RemoteService add deathRecipient fail!");
74         return ;
75     }
76 
77     std::set<uint32_t> compIds;
78     compIds.insert(componentId);
79     g_remoteCompsMap.emplace(std::make_pair(remote.GetRefPtr(), compIds));
80     g_deathReciMap[remote.GetRefPtr()] = deathCallBack;
81     CODEC_LOGI("Add deathRecipient success!");
82 }
83 
RemoveMapperOfDestoryedComponent(uint32_t componentId)84 void RemoveMapperOfDestoryedComponent(uint32_t componentId)
85 {
86     std::unique_lock<std::mutex> lk(g_mutex);
87 
88     auto compRemote = g_compRemoteMap.find(componentId);
89     if (compRemote == g_compRemoteMap.end()) {
90         return;
91     }
92 
93     IRemoteObject *remote = compRemote->second;
94     auto remoteComps = g_remoteCompsMap.find(remote);
95     if (remoteComps == g_remoteCompsMap.end()) {
96         return;
97     }
98     remoteComps->second.erase(componentId);
99     g_compRemoteMap.erase(compRemote);
100 
101     auto deathReci = g_deathReciMap.find(remote);
102     if (deathReci == g_deathReciMap.end()) {
103         CODEC_LOGE("%{public}s: not find recipient", __func__);
104         return;
105     }
106     bool result = remote->RemoveDeathRecipient(deathReci->second);
107     g_deathReciMap.erase(deathReci);
108     if (!result) {
109         CODEC_LOGE("%{public}s: removeDeathRecipient fail", __func__);
110         return;
111     }
112     CODEC_LOGI("Remove mapper destoryedComponent success!");
113 }
114 }  // namespace V2_0
115 }  // namespace Codec
116 }  // namespace HDI
117 }  // namespace OHOS
118