1 /*
2 * Copyright (c) 2021-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
16
17 #include "softbus_client_info_manager.h"
18 #include "permission_status_change_cb.h"
19 #include "softbus_def.h"
20 #include "softbus_server.h"
21
22 namespace OHOS {
23 typedef std::pair<std::unordered_multimap<std::string, ClientObjPair>::const_iterator,
24 std::unordered_multimap<std::string, ClientObjPair>::const_iterator> ClientObjRange;
25
GetInstance()26 SoftbusClientInfoManager &SoftbusClientInfoManager::GetInstance()
27 {
28 static SoftbusClientInfoManager instance;
29 return instance;
30 }
31
SoftbusAddService(const std::string & pkgName,const sptr<IRemoteObject> & object,const sptr<IRemoteObject::DeathRecipient> & abilityDeath,int32_t pid)32 int32_t SoftbusClientInfoManager::SoftbusAddService(const std::string &pkgName, const sptr<IRemoteObject> &object,
33 const sptr<IRemoteObject::DeathRecipient> &abilityDeath, int32_t pid)
34 {
35 if (pkgName.empty() || object == nullptr || abilityDeath == nullptr) {
36 COMM_LOGE(COMM_SVC, "package name, object or abilityDeath is nullptr\n");
37 return SOFTBUS_INVALID_PARAM;
38 }
39 COMM_LOGI(COMM_SVC, "add SoftbusAddService, pid=%{public}d, pkgname=%{public}s", pid, pkgName.c_str());
40 std::lock_guard<std::recursive_mutex> autoLock(clientObjectMapLock_);
41 std::pair<sptr<IRemoteObject>, sptr<IRemoteObject::DeathRecipient>> clientObject(object, abilityDeath);
42 ClientObjPair clientObjPair(pid, clientObject);
43 clientObjectMap_.emplace(pkgName, clientObjPair);
44
45 uint32_t tokenCaller = IPCSkeleton::GetCallingTokenID();
46 std::string permissionName = OHOS_PERMISSION_DISTRIBUTED_DATASYNC;
47 RegisterDataSyncPermission(tokenCaller, permissionName, pkgName, pid);
48
49 return SOFTBUS_OK;
50 }
51
SoftbusAddServiceInner(const std::string & pkgName,ISessionListener * listener,int32_t pid)52 int32_t SoftbusClientInfoManager::SoftbusAddServiceInner(const std::string &pkgName, ISessionListener *listener,
53 int32_t pid)
54 {
55 if (pkgName.empty() || listener == nullptr) {
56 COMM_LOGE(COMM_SVC, "package name or listener is nullptr\n");
57 return SOFTBUS_INVALID_PARAM;
58 }
59 COMM_LOGI(COMM_SVC, "add SoftbusAddServiceInner, pid=%{public}d, pkgname=%{public}s", pid, pkgName.c_str());
60 std::lock_guard<std::recursive_mutex> autoLock(clientObjectMapLock_);
61 innerObjectMap_.emplace(pkgName, *listener);
62
63 return SOFTBUS_OK;
64 }
65
SoftbusRemoveServiceInner(const std::string & pkgName)66 int32_t SoftbusClientInfoManager::SoftbusRemoveServiceInner(const std::string &pkgName)
67 {
68 if (pkgName.empty()) {
69 COMM_LOGE(COMM_SVC, "package name is nullptr\n");
70 return SOFTBUS_INVALID_PARAM;
71 }
72 std::lock_guard<std::recursive_mutex> autoLock(clientObjectMapLock_);
73 innerObjectMap_.erase(pkgName);
74 COMM_LOGI(COMM_SVC, "SoftbusRemoveServiceInner, pkgname=%{public}s", pkgName.c_str());
75
76 return SOFTBUS_OK;
77 }
78
SoftbusRemoveService(const sptr<IRemoteObject> & object,std::string & pkgName,int32_t * pid)79 int32_t SoftbusClientInfoManager::SoftbusRemoveService(const sptr<IRemoteObject> &object, std::string &pkgName,
80 int32_t* pid)
81 {
82 if (object == nullptr || pid == nullptr) {
83 COMM_LOGE(COMM_SVC, "RemoveService object is nullptr\n");
84 return SOFTBUS_INVALID_PARAM;
85 }
86
87 std::lock_guard<std::recursive_mutex> autoLock(clientObjectMapLock_);
88 for (auto iter = clientObjectMap_.begin(); iter != clientObjectMap_.end(); ++iter) {
89 if (iter->second.second.first == object) {
90 pkgName = iter->first;
91 *pid = iter->second.first;
92 object->RemoveDeathRecipient(iter->second.second.second);
93 (void)clientObjectMap_.erase(iter);
94 break;
95 }
96 }
97 COMM_LOGI(COMM_SVC, "SoftbusRemoveService, pid=%{public}d, pkgName=%{public}s", (*pid), pkgName.c_str());
98 return SOFTBUS_OK;
99 }
100
GetSoftbusInnerObject(const std::string & pkgName,ISessionListener * listener)101 int32_t SoftbusClientInfoManager::GetSoftbusInnerObject(const std::string &pkgName, ISessionListener *listener)
102 {
103 if (listener == nullptr) {
104 COMM_LOGE(COMM_SVC, "listener is nullptr\n");
105 return SOFTBUS_INVALID_PARAM;
106 }
107 std::lock_guard<std::recursive_mutex> autoLock(clientObjectMapLock_);
108 if (innerObjectMap_.find(pkgName) == innerObjectMap_.end()) {
109 COMM_LOGE(COMM_SVC, "no find pkgname = %{public}s in map", pkgName.c_str());
110 return SOFTBUS_NOT_FIND;
111 }
112 *listener = innerObjectMap_[pkgName];
113 return SOFTBUS_OK;
114 }
115
GetSoftbusClientProxy(const std::string & pkgName)116 sptr<IRemoteObject> SoftbusClientInfoManager::GetSoftbusClientProxy(const std::string &pkgName)
117 {
118 std::lock_guard<std::recursive_mutex> autoLock(clientObjectMapLock_);
119 auto iter = clientObjectMap_.find(pkgName);
120 if (iter != clientObjectMap_.end()) {
121 return iter->second.second.first;
122 }
123 COMM_LOGE(COMM_SVC, "GetSoftbusClientProxy client proxy is nullptr\n");
124 return nullptr;
125 }
126
GetSoftbusClientProxy(const std::string & pkgName,int32_t pid)127 sptr<IRemoteObject> SoftbusClientInfoManager::GetSoftbusClientProxy(const std::string &pkgName, int32_t pid)
128 {
129 std::lock_guard<std::recursive_mutex> autoLock(clientObjectMapLock_);
130 COMM_LOGD(COMM_SVC, "GetSoftbusClientProxy, pid=%{public}d, pkgname=%{public}s", pid, pkgName.c_str());
131 ClientObjRange range = clientObjectMap_.equal_range(pkgName);
132 auto iter = std::find_if(range.first, range.second, [&pid](auto iter) {return pid == iter.second.first;});
133 if (iter != range.second) {
134 return iter->second.second.first;
135 }
136 COMM_LOGE(COMM_SVC, "GetSoftbusClientProxy with pid is nullptr\n");
137 return nullptr;
138 }
139
GetSoftbusClientProxyMap(std::multimap<std::string,sptr<IRemoteObject>> & softbusClientMap)140 void SoftbusClientInfoManager::GetSoftbusClientProxyMap(std::multimap<std::string,
141 sptr<IRemoteObject>> &softbusClientMap)
142 {
143 std::lock_guard<std::recursive_mutex> autoLock(clientObjectMapLock_);
144 for (auto iter = clientObjectMap_.begin(); iter != clientObjectMap_.end(); ++iter) {
145 softbusClientMap.emplace(iter->first, iter->second.second.first);
146 }
147 }
148
SoftbusClientIsExist(const std::string & pkgName,int32_t pid)149 bool SoftbusClientInfoManager::SoftbusClientIsExist(const std::string &pkgName, int32_t pid)
150 {
151 std::lock_guard<std::recursive_mutex> autoLock(clientObjectMapLock_);
152 ClientObjRange range = clientObjectMap_.equal_range(pkgName);
153 for (auto &iter = range.first; iter != range.second; iter++) {
154 if (pid == iter->second.first) {
155 return true;
156 }
157 }
158 return false;
159 }
160 } // namespace OHOS