• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 "dataobs_mgr_inner_ext.h"
16 
17 #include "data_ability_observer_stub.h"
18 #include "data_share_permission.h"
19 #include "datashare_errno.h"
20 #include "dataobs_mgr_errors.h"
21 #include "hilog_tag_wrapper.h"
22 #include "common_utils.h"
23 
24 namespace OHOS {
25 namespace AAFwk {
26 
DataObsMgrInnerExt()27 DataObsMgrInnerExt::DataObsMgrInnerExt() : root_(std::make_shared<Node>("root")) {}
28 
~DataObsMgrInnerExt()29 DataObsMgrInnerExt::~DataObsMgrInnerExt() {}
30 
HandleRegisterObserver(Uri & uri,sptr<IDataAbilityObserver> dataObserver,ObserverInfo & info,bool isDescendants)31 Status DataObsMgrInnerExt::HandleRegisterObserver(Uri &uri, sptr<IDataAbilityObserver> dataObserver,
32     ObserverInfo &info, bool isDescendants)
33 {
34     if (dataObserver->AsObject() == nullptr) {
35         return DATA_OBSERVER_IS_NULL;
36     }
37     std::lock_guard<ffrt::mutex> lock(nodeMutex_);
38     auto deathRecipientRef = AddObsDeathRecipient(dataObserver->AsObject());
39     if (deathRecipientRef == nullptr) {
40         return DATAOBS_SERVICE_OBS_LIMMIT;
41     }
42 
43     std::vector<std::string> path = { uri.GetScheme(), uri.GetAuthority() };
44     uri.GetPathSegments(path);
45     Entry entry = Entry(dataObserver, info.userId, info.tokenId, deathRecipientRef, isDescendants);
46     if (root_ != nullptr && !root_->AddObserver(path, 0, entry)) {
47         TAG_LOGE(AAFwkTag::DBOBSMGR,
48             "subscribers:%{public}s num maxed",
49             CommonUtils::Anonymous(uri.ToString()).c_str());
50         RemoveObsDeathRecipient(dataObserver->AsObject());
51         return DATAOBS_SERVICE_OBS_LIMMIT;
52     }
53     return SUCCESS;
54 }
55 
HandleUnregisterObserver(Uri & uri,sptr<IDataAbilityObserver> dataObserver)56 Status DataObsMgrInnerExt::HandleUnregisterObserver(Uri &uri, sptr<IDataAbilityObserver> dataObserver)
57 {
58     if (dataObserver->AsObject() == nullptr) {
59         TAG_LOGE(AAFwkTag::DBOBSMGR, "null dataObserver, uri:%{public}s num maxed",
60             CommonUtils::Anonymous(uri.ToString()).c_str());
61         return DATA_OBSERVER_IS_NULL;
62     }
63     std::lock_guard<ffrt::mutex> lock(nodeMutex_);
64     std::vector<std::string> path = { uri.GetScheme(), uri.GetAuthority() };
65     uri.GetPathSegments(path);
66     if (root_ != nullptr) {
67         root_->RemoveObserver(path, 0, dataObserver);
68     }
69     RemoveObsDeathRecipient(dataObserver->AsObject());
70     return SUCCESS;
71 }
72 
HandleUnregisterObserver(sptr<IDataAbilityObserver> dataObserver)73 Status DataObsMgrInnerExt::HandleUnregisterObserver(sptr<IDataAbilityObserver> dataObserver)
74 {
75     if (dataObserver->AsObject() == nullptr) {
76         TAG_LOGE(AAFwkTag::DBOBSMGR, "null dataObserver");
77         return DATA_OBSERVER_IS_NULL;
78     }
79     std::lock_guard<ffrt::mutex> lock(nodeMutex_);
80     if (root_ != nullptr) {
81         root_->RemoveObserver(dataObserver);
82     }
83     RemoveObsDeathRecipient(dataObserver->AsObject(), true);
84     return SUCCESS;
85 }
86 
HandleNotifyChange(const ChangeInfo & changeInfo,int32_t userId)87 Status DataObsMgrInnerExt::HandleNotifyChange(const ChangeInfo &changeInfo, int32_t userId)
88 {
89     ObsMap changeRes;
90     std::vector<std::string> path;
91     {
92         std::lock_guard<ffrt::mutex> lock(nodeMutex_);
93         for (auto &uri : changeInfo.uris_) {
94             path.clear();
95             path.emplace_back(uri.GetScheme());
96             path.emplace_back(uri.GetAuthority());
97             uri.GetPathSegments(path);
98             root_->GetObs(path, 0, uri, userId, changeRes);
99         }
100     }
101     if (changeRes.empty()) {
102         TAG_LOGD(AAFwkTag::DBOBSMGR,
103             "uris no obs, changeType:%{public}ud, uris num:%{public}zu,"
104             "null data:%{public}d, size:%{public}ud",
105             changeInfo.changeType_, changeInfo.uris_.size(), changeInfo.data_ == nullptr, changeInfo.size_);
106         return NO_OBS_FOR_URI;
107     }
108     for (const auto &[obs, value] : changeRes) {
109         if (obs == nullptr || value.uriList.empty()) {
110             continue;
111         }
112         std::string permission = value.permission;
113         if (!permission.empty() &&
114             !DataShare::DataSharePermission::VerifyPermission(value.tokenId, permission)) {
115             std::string uriStr = value.uriList.front().ToString();
116             TAG_LOGW(AAFwkTag::DBOBSMGR, "permission deny, uri:%{public}s, token %{public}d permission %{public}s",
117                 CommonUtils::Anonymous(uriStr).c_str(), value.tokenId, permission.c_str());
118             // just hisysevent now
119             std::string msg = __FUNCTION__;
120             DataShare::DataSharePermission::ReportExtensionFault(DataShare::E_DATASHARE_PERMISSION_DENIED,
121                 value.tokenId, uriStr, msg);
122         }
123         obs->OnChangeExt(
124             { changeInfo.changeType_, move(value.uriList),
125             changeInfo.data_, changeInfo.size_, changeInfo.valueBuckets_ });
126     }
127 
128     return SUCCESS;
129 }
130 
AddObsDeathRecipient(const sptr<IRemoteObject> & dataObserver)131 std::shared_ptr<DataObsMgrInnerExt::DeathRecipientRef> DataObsMgrInnerExt::AddObsDeathRecipient(
132     const sptr<IRemoteObject> &dataObserver)
133 {
134     auto it = obsRecipientRefs.find(dataObserver);
135     if (it != obsRecipientRefs.end()) {
136         if (std::numeric_limits<uint32_t>::max() - 1 < it->second->ref) {
137             TAG_LOGE(AAFwkTag::DBOBSMGR, "observer num maxed");
138             return nullptr;
139         }
140     } else {
141         std::weak_ptr<DataObsMgrInnerExt> thisWeakPtr(shared_from_this());
142         sptr<IRemoteObject::DeathRecipient> deathRecipient =
143             new DataObsCallbackRecipient([thisWeakPtr](const wptr<IRemoteObject> &remote) {
144                 auto DataObsMgrInnerExt = thisWeakPtr.lock();
145                 if (DataObsMgrInnerExt) {
146                     DataObsMgrInnerExt->OnCallBackDied(remote);
147                 }
148             });
149         dataObserver->AddDeathRecipient(deathRecipient);
150         it = obsRecipientRefs.emplace(dataObserver, std::make_shared<DeathRecipientRef>(deathRecipient)).first;
151     }
152     TAG_LOGD(AAFwkTag::DBOBSMGR, "add observer, sum:%{public}ud",
153         it->second->ref.load(std::memory_order_relaxed));
154     return it->second;
155 }
156 
RemoveObsDeathRecipient(const sptr<IRemoteObject> & dataObserver,bool isForce)157 void DataObsMgrInnerExt::RemoveObsDeathRecipient(const sptr<IRemoteObject> &dataObserver, bool isForce)
158 {
159     auto it = obsRecipientRefs.find(dataObserver);
160     if (it == obsRecipientRefs.end()) {
161         return;
162     }
163 
164     if (isForce || it->second->ref <= 1) {
165         TAG_LOGD(AAFwkTag::DBOBSMGR, "remove deathRecipient, sum:%{public}ud",
166             it->second->ref.load(std::memory_order_relaxed));
167         dataObserver->RemoveDeathRecipient(it->second->deathRecipient);
168         obsRecipientRefs.erase(it);
169         return;
170     }
171 }
172 
OnCallBackDied(const wptr<IRemoteObject> & remote)173 void DataObsMgrInnerExt::OnCallBackDied(const wptr<IRemoteObject> &remote)
174 {
175     TAG_LOGD(AAFwkTag::DBOBSMGR, "observer died");
176     auto dataObserver = remote.promote();
177     if (dataObserver == nullptr) {
178         return;
179     }
180     std::lock_guard<ffrt::mutex> lock(nodeMutex_);
181     if (root_ != nullptr) {
182         root_->RemoveObserver(dataObserver);
183     }
184     RemoveObsDeathRecipient(dataObserver, true);
185 }
186 
Node(const std::string & name)187 DataObsMgrInnerExt::Node::Node(const std::string &name) : name_(name) {}
188 
GetObs(const std::vector<std::string> & path,uint32_t index,Uri & uri,int32_t userId,ObsMap & obsRes)189 void DataObsMgrInnerExt::Node::GetObs(const std::vector<std::string> &path, uint32_t index,
190     Uri &uri, int32_t userId, ObsMap &obsRes)
191 {
192     if (path.size() == index) {
193         for (auto entry : entrys_) {
194             if (entry.userId != userId && entry.userId != 0 && userId != 0) {
195                 TAG_LOGW(AAFwkTag::DBOBSMGR, "Not allow across user notify, uri:%{public}s, from %{public}d to"
196                     "%{public}d", CommonUtils::Anonymous(uri.ToString()).c_str(), userId, entry.userId);
197                 continue;
198             }
199             ObsNotifyInfo &notifyInfo = obsRes.try_emplace(entry.observer, ObsNotifyInfo()).first->second;
200             notifyInfo.uriList.push_back(uri);
201             notifyInfo.tokenId = entry.tokenId;
202             notifyInfo.permission = entry.permission;
203         }
204         return;
205     }
206 
207     for (const auto &entry : entrys_) {
208         if (entry.isDescendants) {
209             if (entry.userId != userId && entry.userId != 0 && userId != 0) {
210                 TAG_LOGW(AAFwkTag::DBOBSMGR, "Not allow across user notify, uri:%{public}s, from %{public}d to"
211                     "%{public}d", CommonUtils::Anonymous(uri.ToString()).c_str(), userId, entry.userId);
212                 continue;
213             }
214             ObsNotifyInfo &notifyInfo = obsRes.try_emplace(entry.observer, ObsNotifyInfo()).first->second;
215             notifyInfo.uriList.push_back(uri);
216             notifyInfo.tokenId = entry.tokenId;
217             notifyInfo.permission = entry.permission;
218         }
219     }
220 
221     auto it = childrens_.find(path[index]);
222     if (it == childrens_.end()) {
223         return;
224     }
225     it->second->GetObs(path, ++index, uri, userId, obsRes);
226 
227     return;
228 }
229 
IsLimit(const Entry & entry)230 bool DataObsMgrInnerExt::Node::IsLimit(const Entry &entry)
231 {
232     if (entrys_.size() >= OBS_ALL_NUM_MAX) {
233         return true;
234     }
235     uint32_t count = 0;
236     for (Entry& existEntry : entrys_) {
237         if (existEntry.tokenId == entry.tokenId) {
238             count++;
239             if (count > OBS_NUM_MAX) {
240                 return true;
241             }
242         }
243     }
244     TAG_LOGE(AAFwkTag::DBOBSMGR, "subscribers num :%{public}d", count);
245     return false;
246 }
247 
AddObserver(const std::vector<std::string> & path,uint32_t index,const Entry & entry)248 bool DataObsMgrInnerExt::Node::AddObserver(const std::vector<std::string> &path, uint32_t index, const Entry &entry)
249 {
250     if (path.size() == index) {
251         if (IsLimit(entry)) {
252             TAG_LOGE(AAFwkTag::DBOBSMGR, "subscribers num maxed, token:%{public}d", entry.tokenId);
253             return false;
254         }
255         entry.deathRecipientRef->ref++;
256         entrys_.emplace_back(entry);
257         return true;
258     }
259     auto it = childrens_.try_emplace(path[index], std::make_shared<Node>(path[index])).first;
260     return it->second->AddObserver(path, ++index, entry);
261 }
262 
RemoveObserver(const std::vector<std::string> & path,uint32_t index,sptr<IDataAbilityObserver> dataObserver)263 bool DataObsMgrInnerExt::Node::RemoveObserver(const std::vector<std::string> &path, uint32_t index,
264     sptr<IDataAbilityObserver> dataObserver)
265 {
266     if (index == path.size()) {
267         entrys_.remove_if([dataObserver](const Entry &entry) {
268             if (entry.observer->AsObject() != dataObserver->AsObject()) {
269                 return false;
270             }
271             entry.deathRecipientRef->ref--;
272             return true;
273         });
274         return entrys_.empty() && childrens_.empty();
275     }
276     auto child = childrens_.find(path[index]);
277     if (child != childrens_.end() && child->second->RemoveObserver(path, ++index, dataObserver)) {
278         childrens_.erase(child);
279     }
280     return entrys_.empty() && childrens_.empty();
281 }
282 
283 // remove observer of all users
RemoveObserver(sptr<IRemoteObject> dataObserver)284 bool DataObsMgrInnerExt::Node::RemoveObserver(sptr<IRemoteObject> dataObserver)
285 {
286     for (auto child = childrens_.begin(); child != childrens_.end();) {
287         if (child->second->RemoveObserver(dataObserver)) {
288             child = childrens_.erase(child);
289         } else {
290             child++;
291         }
292     }
293     entrys_.remove_if([dataObserver](const Entry &entry) {
294         if (entry.observer->AsObject() != dataObserver) {
295             return false;
296         }
297         entry.deathRecipientRef->ref--;
298         return true;
299     });
300     return entrys_.empty() && childrens_.empty();
301 }
302 
RemoveObserver(sptr<IDataAbilityObserver> dataObserver)303 inline bool DataObsMgrInnerExt::Node::RemoveObserver(sptr<IDataAbilityObserver> dataObserver)
304 {
305     auto obs = dataObserver->AsObject();
306     return obs != nullptr && RemoveObserver(obs);
307 }
308 
309 } // namespace AAFwk
310 } // namespace OHOS
311