1 /*
2 * Copyright (c) 2022 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 "datashare_helper.h"
17 #include "datashare_helper_impl.h"
18
19 #include "concurrent_map.h"
20 #include "data_ability_observer_interface.h"
21 #include "data_ability_observer_stub.h"
22 #include "dataobs_mgr_client.h"
23 #include "datashare_log.h"
24
25 namespace OHOS {
26 namespace DataShare {
27 using namespace AppExecFwk;
28 namespace {
29 static constexpr const char *DATA_SHARE_PREFIX = "datashare:///";
30 static constexpr const char *FILE_PREFIX = "file://";
31 } // namespace
32 constexpr int INVALID_VALUE = -1;
33 class ObserverImpl : public AAFwk::DataAbilityObserverStub {
34 public:
ObserverImpl(const std::shared_ptr<DataShareObserver> dataShareObserver)35 explicit ObserverImpl(const std::shared_ptr<DataShareObserver> dataShareObserver)
36 : dataShareObserver_(dataShareObserver){};
37 void OnChange();
38 void OnChangeExt(const AAFwk::ChangeInfo &info);
39 static DataShareObserver::ChangeInfo ConvertInfo(const AAFwk::ChangeInfo &info);
40 static AAFwk::ChangeInfo ConvertInfo(const DataShareObserver::ChangeInfo &info);
41 static sptr<ObserverImpl> GetObserver(const Uri& uri, const std::shared_ptr<DataShareObserver> &observer);
42 static bool FindObserver(const Uri& uri, const std::shared_ptr<DataShareObserver> &observer);
43 static bool DeleteObserver(const Uri& uri, const std::shared_ptr<DataShareObserver> &observer);
44 private:
45 struct ObserverParam {
46 sptr<ObserverImpl> obs_;
47 std::list<Uri> uris_;
48 };
49 std::shared_ptr<DataShareObserver> dataShareObserver_;
50 static ConcurrentMap<DataShareObserver *, ObserverParam> observers_;
51 };
52
53 ConcurrentMap<DataShareObserver *, ObserverImpl::ObserverParam> ObserverImpl::observers_;
54
TransferUriPrefix(const std::string & originPrefix,const std::string & replacedPrefix,const std::string & originUriStr)55 std::string DataShareHelper::TransferUriPrefix(const std::string &originPrefix, const std::string &replacedPrefix,
56 const std::string &originUriStr)
57 {
58 if (originUriStr.find(originPrefix) != 0) {
59 return originUriStr;
60 }
61 return replacedPrefix + originUriStr.substr(originPrefix.length());
62 }
63
64 /**
65 * @brief You can use this method to specify the Uri of the data to operate and set the binding relationship
66 * between the ability using the Data template (data share for short) and the associated client process in
67 * a DataShareHelper instance.
68 *
69 * @param token Indicates the System token.
70 * @param strUri Indicates the database table or disk file to operate.
71 *
72 * @return Returns the created DataShareHelper instance.
73 */
Creator(const sptr<IRemoteObject> & token,const std::string & strUri,const std::string & extUri)74 std::shared_ptr<DataShareHelper> DataShareHelper::Creator(
75 const sptr<IRemoteObject> &token, const std::string &strUri, const std::string &extUri)
76 {
77 if (token == nullptr) {
78 LOG_ERROR("token == nullptr");
79 return nullptr;
80 }
81
82 std::string replacedUriStr = TransferUriPrefix(FILE_PREFIX, DATA_SHARE_PREFIX, strUri);
83 Uri uri(replacedUriStr);
84
85 if (uri.GetQuery().find("Proxy=true") != std::string::npos) {
86 auto result = CreateServiceHelper();
87 if (result != nullptr && IsSilentProxyEnable(strUri)) {
88 return result;
89 }
90 if (extUri.empty()) {
91 return nullptr;
92 }
93 Uri ext(extUri);
94 return CreateExtHelper(ext, token);
95 }
96 return CreateExtHelper(uri, token);
97 }
98
Creator(const string & strUri,const CreateOptions & options,const std::string & bundleName)99 std::shared_ptr<DataShareHelper> DataShareHelper::Creator(const string &strUri, const CreateOptions &options,
100 const std::string &bundleName)
101 {
102 Uri uri(strUri);
103 if (!options.isProxy_ && options.token_ == nullptr) {
104 LOG_ERROR("token is nullptr");
105 return nullptr;
106 }
107 if (options.isProxy_) {
108 return CreateServiceHelper(bundleName);
109 }
110 return CreateExtHelper(uri, options.token_);
111 }
112
CreateServiceHelper(const std::string & bundleName)113 std::shared_ptr<DataShareHelper> DataShareHelper::CreateServiceHelper(const std::string &bundleName)
114 {
115 auto manager = DataShareManagerImpl::GetInstance();
116 if (manager == nullptr) {
117 LOG_ERROR("manager_ is nullptr");
118 return nullptr;
119 }
120 manager->SetBundleName(bundleName);
121 if (DataShareManagerImpl::GetServiceProxy() == nullptr) {
122 LOG_ERROR("service proxy is nullptr.");
123 return nullptr;
124 }
125 return std::make_shared<DataShareHelperImpl>();
126 }
127
IsSilentProxyEnable(const std::string & uri)128 bool DataShareHelper::IsSilentProxyEnable(const std::string &uri)
129 {
130 auto proxy = DataShareManagerImpl::GetServiceProxy();
131 if (proxy == nullptr) {
132 LOG_ERROR("Service proxy is nullptr.");
133 return false;
134 }
135 return proxy->IsSilentProxyEnable(uri);
136 }
137
CreateExtHelper(Uri & uri,const sptr<IRemoteObject> & token)138 std::shared_ptr<DataShareHelper> DataShareHelper::CreateExtHelper(Uri &uri, const sptr<IRemoteObject> &token)
139 {
140 sptr<DataShareConnection> connection = new (std::nothrow) DataShareConnection(uri, token);
141 if (connection == nullptr) {
142 LOG_ERROR("Create DataShareConnection failed.");
143 return nullptr;
144 }
145 auto dataShareConnection =
146 std::shared_ptr<DataShareConnection>(connection.GetRefPtr(), [holder = connection](const auto *) {
147 holder->DisconnectDataShareExtAbility();
148 });
149 if (dataShareConnection->GetDataShareProxy(uri, token) == nullptr) {
150 LOG_ERROR("connect failed");
151 return nullptr;
152 }
153 return std::make_shared<DataShareHelperImpl>(uri, token, dataShareConnection);
154 }
155
156 /**
157 * Registers an observer to DataObsMgr specified by the given Uri.
158 *
159 * @param uri, Indicates the path of the data to operate.
160 * @param dataObserver, Indicates the DataShareObserver object.
161 * @param isDescendants, Indicates the Whether to note the change of descendants.
162 */
RegisterObserverExt(const Uri & uri,std::shared_ptr<DataShareObserver> dataObserver,bool isDescendants)163 void DataShareHelper::RegisterObserverExt(const Uri &uri, std::shared_ptr<DataShareObserver> dataObserver,
164 bool isDescendants)
165 {
166 LOG_INFO("Start");
167 if (dataObserver == nullptr) {
168 LOG_ERROR("dataObserver is nullptr");
169 return;
170 }
171 auto obsMgrClient = OHOS::AAFwk::DataObsMgrClient::GetInstance();
172 if (obsMgrClient == nullptr) {
173 LOG_ERROR("get DataObsMgrClient failed");
174 return;
175 }
176 sptr<ObserverImpl> obs = ObserverImpl::GetObserver(uri, dataObserver);
177 if (obs == nullptr) {
178 LOG_ERROR("new ObserverImpl failed");
179 return;
180 }
181 ErrCode ret = obsMgrClient->RegisterObserverExt(uri, obs, isDescendants);
182 if (ret != ERR_OK) {
183 ObserverImpl::DeleteObserver(uri, dataObserver);
184 LOG_ERROR("RegisterObserverExt failed");
185 }
186 }
187
188 /**
189 * Deregisters an observer used for DataObsMgr specified by the given Uri.
190 *
191 * @param uri, Indicates the path of the data to operate.
192 * @param dataObserver, Indicates the DataShareObserver object.
193 */
UnregisterObserverExt(const Uri & uri,std::shared_ptr<DataShareObserver> dataObserver)194 void DataShareHelper::UnregisterObserverExt(const Uri &uri, std::shared_ptr<DataShareObserver> dataObserver)
195 {
196 LOG_INFO("Start");
197 if (dataObserver == nullptr) {
198 LOG_ERROR("dataObserver is nullptr");
199 return;
200 }
201 auto obsMgrClient = OHOS::AAFwk::DataObsMgrClient::GetInstance();
202 if (obsMgrClient == nullptr) {
203 LOG_ERROR("get DataObsMgrClient failed");
204 return;
205 }
206
207 if (!ObserverImpl::FindObserver(uri, dataObserver)) {
208 LOG_ERROR("observer not exit!");
209 return;
210 }
211
212 sptr<ObserverImpl> obs = ObserverImpl::GetObserver(uri, dataObserver);
213 if (obs == nullptr) {
214 LOG_ERROR("new ObserverImpl failed");
215 return;
216 }
217 ErrCode ret = obsMgrClient->UnregisterObserverExt(uri, obs);
218 if (ret != ERR_OK) {
219 LOG_ERROR("UnregisterObserverExt failed");
220 return;
221 }
222 ObserverImpl::DeleteObserver(uri, dataObserver);
223 }
224
225 /**
226 * Notifies the registered observers of a change to the data resource specified by Uris.
227 *
228 * @param changeInfo Indicates the info of the data to operate.
229 */
NotifyChangeExt(const DataShareObserver::ChangeInfo & changeInfo)230 void DataShareHelper::NotifyChangeExt(const DataShareObserver::ChangeInfo &changeInfo)
231 {
232 LOG_INFO("Start");
233
234 auto obsMgrClient = OHOS::AAFwk::DataObsMgrClient::GetInstance();
235 if (obsMgrClient == nullptr) {
236 LOG_ERROR("get DataObsMgrClient failed");
237 return;
238 }
239
240 ErrCode ret = obsMgrClient->NotifyChangeExt(ObserverImpl::ConvertInfo(changeInfo));
241 if (ret != ERR_OK) {
242 LOG_ERROR("NotifyChangeExt failed");
243 }
244 }
245
OnChange()246 void ObserverImpl::OnChange() {}
247
OnChangeExt(const AAFwk::ChangeInfo & info)248 void ObserverImpl::OnChangeExt(const AAFwk::ChangeInfo &info)
249 {
250 dataShareObserver_->OnChange(ConvertInfo(info));
251 }
252
ConvertInfo(const AAFwk::ChangeInfo & info)253 DataShareObserver::ChangeInfo ObserverImpl::ConvertInfo(const AAFwk::ChangeInfo &info)
254 {
255 DataShareObserver::ChangeInfo changeInfo;
256 changeInfo.changeType_ = static_cast<const DataShareObserver::ChangeType>(info.changeType_);
257 changeInfo.uris_ = std::move(info.uris_);
258 changeInfo.data_ = info.data_;
259 changeInfo.size_ = info.size_;
260 return changeInfo;
261 }
262
ConvertInfo(const DataShareObserver::ChangeInfo & info)263 AAFwk::ChangeInfo ObserverImpl::ConvertInfo(const DataShareObserver::ChangeInfo &info)
264 {
265 AAFwk::ChangeInfo changeInfo;
266 changeInfo.changeType_ = static_cast<const AAFwk::ChangeInfo::ChangeType>(info.changeType_);
267 changeInfo.uris_ = std::move(info.uris_);
268 changeInfo.data_ = const_cast<void*>(info.data_);
269 changeInfo.size_ = info.size_;
270 return changeInfo;
271 }
272
GetObserver(const Uri & uri,const std::shared_ptr<DataShareObserver> & observer)273 sptr<ObserverImpl> ObserverImpl::GetObserver(const Uri& uri, const std::shared_ptr<DataShareObserver> &observer)
274 {
275 sptr<ObserverImpl> result = nullptr;
276 observers_.Compute(observer.get(), [&result, &uri, &observer](const auto &key, auto &value) {
277 if (value.obs_ == nullptr) {
278 value.obs_ = new (std::nothrow) ObserverImpl(observer);
279 value.uris_.push_back(uri);
280 } else {
281 auto it = std::find(value.uris_.begin(), value.uris_.end(), uri);
282 if (it == value.uris_.end()) {
283 value.uris_.push_back(uri);
284 }
285 }
286
287 result = value.obs_;
288 return result != nullptr;
289 });
290
291 return result;
292 }
293
FindObserver(const Uri & uri,const std::shared_ptr<DataShareObserver> & observer)294 bool ObserverImpl::FindObserver(const Uri& uri, const std::shared_ptr<DataShareObserver> &observer)
295 {
296 auto result = observers_.Find(observer.get());
297 if (result.first) {
298 auto it = std::find(result.second.uris_.begin(), result.second.uris_.end(), uri);
299 if (it == result.second.uris_.end()) {
300 return false;
301 }
302 }
303 return result.first;
304 }
305
DeleteObserver(const Uri & uri,const std::shared_ptr<DataShareObserver> & observer)306 bool ObserverImpl::DeleteObserver(const Uri& uri, const std::shared_ptr<DataShareObserver> &observer)
307 {
308 return observers_.ComputeIfPresent(observer.get(), [&uri](auto &key, auto &value) {
309 value.uris_.remove_if([&uri](const auto &value) {
310 return uri == value;
311 });
312 return !value.uris_.empty();
313 });
314 }
315
SetSilentSwitch(Uri & uri,bool enable)316 int DataShareHelper::SetSilentSwitch(Uri &uri, bool enable)
317 {
318 auto proxy = DataShareManagerImpl::GetServiceProxy();
319 if (proxy == nullptr) {
320 LOG_ERROR("proxy is nullptr");
321 return INVALID_VALUE;
322 }
323 return proxy->SetSilentSwitch(uri, enable);
324 }
325 } // namespace DataShare
326 } // namespace OHOS