• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "data_ability_helper.h"
17 #include "ability_thread.h"
18 #include "ability_scheduler_interface.h"
19 #include "app_log_wrapper.h"
20 #include "abs_shared_result_set.h"
21 #include "data_ability_predicates.h"
22 #include "values_bucket.h"
23 #include "data_ability_result.h"
24 #include "data_ability_operation.h"
25 #include "data_ability_observer_interface.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
29 std::string SchemeOhos = "dataability";
30 std::mutex DataAbilityHelper::oplock_;
31 using IAbilityScheduler = OHOS::AAFwk::IAbilityScheduler;
32 using AbilityManagerClient = OHOS::AAFwk::AbilityManagerClient;
DataAbilityHelper(const std::shared_ptr<Context> & context,const std::shared_ptr<Uri> & uri,const sptr<IAbilityScheduler> & dataAbilityProxy,bool tryBind)33 DataAbilityHelper::DataAbilityHelper(const std::shared_ptr<Context> &context, const std::shared_ptr<Uri> &uri,
34     const sptr<IAbilityScheduler> &dataAbilityProxy, bool tryBind)
35 {
36     APP_LOGI("DataAbilityHelper::DataAbilityHelper start");
37     token_ = context->GetToken();
38     context_ = std::weak_ptr<Context>(context);
39     uri_ = uri;
40     tryBind_ = tryBind;
41     dataAbilityProxy_ = dataAbilityProxy;
42     APP_LOGI("DataAbilityHelper::DataAbilityHelper end");
43 }
44 
DataAbilityHelper(const std::shared_ptr<Context> & context)45 DataAbilityHelper::DataAbilityHelper(const std::shared_ptr<Context> &context)
46 {
47     APP_LOGI("DataAbilityHelper::DataAbilityHelper only with context start");
48     token_ = context->GetToken();
49     context_ = std::weak_ptr<Context>(context);
50     APP_LOGI("DataAbilityHelper::DataAbilityHelper only with context end");
51 }
52 
DataAbilityHelper(const sptr<IRemoteObject> & token,const std::shared_ptr<Uri> & uri,const sptr<AAFwk::IAbilityScheduler> & dataAbilityProxy)53 DataAbilityHelper::DataAbilityHelper(const sptr<IRemoteObject> &token, const std::shared_ptr<Uri> &uri,
54     const sptr<AAFwk::IAbilityScheduler> &dataAbilityProxy)
55 {
56     APP_LOGI("DataAbilityHelper::DataAbilityHelper start");
57     token_ = token;
58     uri_ = uri;
59     tryBind_ = false;
60     dataAbilityProxy_ = dataAbilityProxy;
61     isSystemCaller_ = true;
62     if (isSystemCaller_ && dataAbilityProxy_) {
63         AddDataAbilityDeathRecipient(dataAbilityProxy_->AsObject());
64     }
65     APP_LOGI("DataAbilityHelper::DataAbilityHelper end");
66 }
67 
DataAbilityHelper(const sptr<IRemoteObject> & token)68 DataAbilityHelper::DataAbilityHelper(const sptr<IRemoteObject> &token)
69 {
70     APP_LOGI("DataAbilityHelper::DataAbilityHelper only with token_start");
71     token_ = token;
72     isSystemCaller_ = true;
73     APP_LOGI("DataAbilityHelper::DataAbilityHelper only with token end");
74 }
75 
AddDataAbilityDeathRecipient(const sptr<IRemoteObject> & token)76 void DataAbilityHelper::AddDataAbilityDeathRecipient(const sptr<IRemoteObject> &token)
77 {
78     APP_LOGI("DataAbilityHelper::AddDataAbilityDeathRecipient start.");
79     if (token != nullptr && callerDeathRecipient_ != nullptr) {
80         APP_LOGI("token RemoveDeathRecipient.");
81         token->RemoveDeathRecipient(callerDeathRecipient_);
82     }
83     if (callerDeathRecipient_ == nullptr) {
84         callerDeathRecipient_ =
85             new DataAbilityDeathRecipient(std::bind(&DataAbilityHelper::OnSchedulerDied, this, std::placeholders::_1));
86     }
87     if (token != nullptr) {
88         APP_LOGI("token AddDeathRecipient.");
89         token->AddDeathRecipient(callerDeathRecipient_);
90     }
91     APP_LOGI("DataAbilityHelper::AddDataAbilityDeathRecipient end.");
92 }
93 
OnSchedulerDied(const wptr<IRemoteObject> & remote)94 void DataAbilityHelper::OnSchedulerDied(const wptr<IRemoteObject> &remote)
95 {
96     APP_LOGI("'%{public}s start':", __func__);
97     std::lock_guard<std::mutex> guard(lock_);
98     auto object = remote.promote();
99     object = nullptr;
100     dataAbilityProxy_ = nullptr;
101     uri_ = nullptr;
102     APP_LOGI("DataAbilityHelper::OnSchedulerDied end.");
103 }
104 
105 /**
106  * @brief Creates a DataAbilityHelper instance without specifying the Uri based on the given Context.
107  *
108  * @param context Indicates the Context object on OHOS.
109  *
110  * @return Returns the created DataAbilityHelper instance where Uri is not specified.
111  */
Creator(const std::shared_ptr<Context> & context)112 std::shared_ptr<DataAbilityHelper> DataAbilityHelper::Creator(const std::shared_ptr<Context> &context)
113 {
114     APP_LOGI("DataAbilityHelper::Creator with context start.");
115     if (context == nullptr) {
116         APP_LOGE("DataAbilityHelper::Creator (context, uri, tryBind) failed, context == nullptr");
117         return nullptr;
118     }
119 
120     DataAbilityHelper *ptrDataAbilityHelper = new (std::nothrow) DataAbilityHelper(context);
121     if (ptrDataAbilityHelper == nullptr) {
122         APP_LOGE("DataAbilityHelper::Creator (context) failed, create DataAbilityHelper failed");
123         return nullptr;
124     }
125 
126     APP_LOGI("DataAbilityHelper::Creator with context end.");
127     return std::shared_ptr<DataAbilityHelper>(ptrDataAbilityHelper);
128 }
129 
130 /**
131  * @brief Creates a DataAbilityHelper instance with the Uri specified based on the given Context.
132  *
133  * @param context Indicates the Context object on OHOS.
134  * @param uri Indicates the database table or disk file to operate.
135  *
136  * @return Returns the created DataAbilityHelper instance with a specified Uri.
137  */
Creator(const std::shared_ptr<Context> & context,const std::shared_ptr<Uri> & uri)138 std::shared_ptr<DataAbilityHelper> DataAbilityHelper::Creator(
139     const std::shared_ptr<Context> &context, const std::shared_ptr<Uri> &uri)
140 {
141     APP_LOGI("DataAbilityHelper::Creator with context uri called.");
142     return DataAbilityHelper::Creator(context, uri, false);
143 }
144 
145 /**
146  * @brief You can use this method to specify the Uri of the data to operate and set the binding relationship
147  * between the ability using the Data template (Data ability for short) and the associated client process in
148  * a DataAbilityHelper instance.
149  *
150  * @param context Indicates the Context object on OHOS.
151  * @param uri Indicates the database table or disk file to operate.
152  * @param tryBind Specifies whether the exit of the corresponding Data ability process causes the exit of the
153  * client process.
154  *
155  * @return Returns the created DataAbilityHelper instance.
156  */
Creator(const std::shared_ptr<Context> & context,const std::shared_ptr<Uri> & uri,const bool tryBind)157 std::shared_ptr<DataAbilityHelper> DataAbilityHelper::Creator(
158     const std::shared_ptr<Context> &context, const std::shared_ptr<Uri> &uri, const bool tryBind)
159 {
160     APP_LOGI("DataAbilityHelper::Creator with context uri tryBind called start.");
161     if (context == nullptr) {
162         APP_LOGE("DataAbilityHelper::Creator (context, uri, tryBind) failed, context == nullptr");
163         return nullptr;
164     }
165 
166     if (uri == nullptr) {
167         APP_LOGE("DataAbilityHelper::Creator (context, uri, tryBind) failed, uri == nullptr");
168         return nullptr;
169     }
170 
171     if (uri->GetScheme() != SchemeOhos) {
172         APP_LOGE("DataAbilityHelper::Creator (context, uri, tryBind) failed, the Scheme is not dataability, Scheme: "
173                  "%{public}s",
174             uri->GetScheme().c_str());
175         return nullptr;
176     }
177 
178     APP_LOGI("DataAbilityHelper::Creator before AcquireDataAbility.");
179     sptr<IAbilityScheduler> dataAbilityProxy =
180         AbilityManagerClient::GetInstance()->AcquireDataAbility(*uri.get(), tryBind, context->GetToken());
181     if (dataAbilityProxy == nullptr) {
182         APP_LOGE("DataAbilityHelper::Creator failed get dataAbilityProxy");
183         return nullptr;
184     }
185     APP_LOGI("DataAbilityHelper::Creator after AcquireDataAbility.");
186 
187     DataAbilityHelper *ptrDataAbilityHelper =
188         new (std::nothrow) DataAbilityHelper(context, uri, dataAbilityProxy, tryBind);
189     if (ptrDataAbilityHelper == nullptr) {
190         APP_LOGE("DataAbilityHelper::Creator (context, uri, tryBind) failed, create DataAbilityHelper failed");
191         return nullptr;
192     }
193 
194     APP_LOGI("DataAbilityHelper::Creator with context uri tryBind called end.");
195     return std::shared_ptr<DataAbilityHelper>(ptrDataAbilityHelper);
196 }
197 
198 /**
199  * @brief Creates a DataAbilityHelper instance without specifying the Uri based.
200  *
201  * @param token Indicates the System token.
202  *
203  * @return Returns the created DataAbilityHelper instance where Uri is not specified.
204  */
Creator(const sptr<IRemoteObject> & token)205 std::shared_ptr<DataAbilityHelper> DataAbilityHelper::Creator(const sptr<IRemoteObject> &token)
206 {
207     APP_LOGI("DataAbilityHelper::Creator with token start.");
208     if (token == nullptr) {
209         APP_LOGE("DataAbilityHelper::Creator (token) failed, token == nullptr");
210         return nullptr;
211     }
212 
213     DataAbilityHelper *ptrDataAbilityHelper = new (std::nothrow) DataAbilityHelper(token);
214     if (ptrDataAbilityHelper == nullptr) {
215         APP_LOGE("DataAbilityHelper::Creator (token) failed, create DataAbilityHelper failed");
216         return nullptr;
217     }
218 
219     APP_LOGI("DataAbilityHelper::Creator with token end.");
220     return std::shared_ptr<DataAbilityHelper>(ptrDataAbilityHelper);
221 }
222 
223 /**
224  * @brief You can use this method to specify the Uri of the data to operate and set the binding relationship
225  * between the ability using the Data template (Data ability for short) and the associated client process in
226  * a DataAbilityHelper instance.
227  *
228  * @param token Indicates the System token.
229  * @param uri Indicates the database table or disk file to operate.
230  *
231  * @return Returns the created DataAbilityHelper instance.
232  */
Creator(const sptr<IRemoteObject> & token,const std::shared_ptr<Uri> & uri)233 std::shared_ptr<DataAbilityHelper> DataAbilityHelper::Creator(
234     const sptr<IRemoteObject> &token, const std::shared_ptr<Uri> &uri)
235 {
236     APP_LOGI("DataAbilityHelper::Creator with token uri called start.");
237     if (token == nullptr) {
238         APP_LOGE("DataAbilityHelper::Creator (token, uri) failed, token == nullptr");
239         return nullptr;
240     }
241 
242     if (uri == nullptr) {
243         APP_LOGE("DataAbilityHelper::Creator (token, uri) failed, uri == nullptr");
244         return nullptr;
245     }
246 
247     if (uri->GetScheme() != SchemeOhos) {
248         APP_LOGE("DataAbilityHelper::Creator (token, uri) failed, the Scheme is not dataability, Scheme: "
249                  "%{public}s",
250             uri->GetScheme().c_str());
251         return nullptr;
252     }
253 
254     APP_LOGI("DataAbilityHelper::Creator before AcquireDataAbility.");
255     sptr<IAbilityScheduler> dataAbilityProxy =
256         AbilityManagerClient::GetInstance()->AcquireDataAbility(*uri.get(), false, token);
257     if (dataAbilityProxy == nullptr) {
258         APP_LOGE("DataAbilityHelper::Creator failed get dataAbilityProxy");
259         return nullptr;
260     }
261     APP_LOGI("DataAbilityHelper::Creator after AcquireDataAbility.");
262 
263     DataAbilityHelper *ptrDataAbilityHelper = new (std::nothrow) DataAbilityHelper(token, uri, dataAbilityProxy);
264     if (ptrDataAbilityHelper == nullptr) {
265         APP_LOGE("DataAbilityHelper::Creator (token, uri) failed, create DataAbilityHelper failed");
266         return nullptr;
267     }
268 
269     APP_LOGI("DataAbilityHelper::Creator with token uri called end.");
270     return std::shared_ptr<DataAbilityHelper>(ptrDataAbilityHelper);
271 }
272 
273 /**
274  * @brief Releases the client resource of the Data ability.
275  * You should call this method to releases client resource after the data operations are complete.
276  *
277  * @return Returns true if the resource is successfully released; returns false otherwise.
278  */
Release()279 bool DataAbilityHelper::Release()
280 {
281     APP_LOGI("DataAbilityHelper::Release start.");
282     std::lock_guard<std::mutex> guard(lock_);
283     if (uri_ == nullptr) {
284         APP_LOGE("DataAbilityHelper::Release failed, uri_ is nullptr");
285         return false;
286     }
287 
288     APP_LOGI("DataAbilityHelper::Release before ReleaseDataAbility.");
289     int err = AbilityManagerClient::GetInstance()->ReleaseDataAbility(dataAbilityProxy_, token_);
290     if (err != ERR_OK) {
291         APP_LOGE("DataAbilityHelper::GetFileTypes failed to ReleaseDataAbility err = %{public}d", err);
292         return false;
293     }
294     APP_LOGI("DataAbilityHelper::Release after ReleaseDataAbility.");
295     dataAbilityProxy_ = nullptr;
296     uri_.reset();
297     APP_LOGI("DataAbilityHelper::Release end.");
298     return true;
299 }
300 
301 /**
302  * @brief Obtains the MIME types of files supported.
303  *
304  * @param uri Indicates the path of the files to obtain.
305  * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null.
306  *
307  * @return Returns the matched MIME types. If there is no match, null is returned.
308  */
GetFileTypes(Uri & uri,const std::string & mimeTypeFilter)309 std::vector<std::string> DataAbilityHelper::GetFileTypes(Uri &uri, const std::string &mimeTypeFilter)
310 {
311     APP_LOGI("DataAbilityHelper::GetFileTypes start.");
312     std::lock_guard<std::mutex> guard(lock_);
313     std::vector<std::string> matchedMIMEs;
314     if (!CheckUriParam(uri)) {
315         APP_LOGE("%{public}s called. CheckUriParam uri failed", __func__);
316         return matchedMIMEs;
317     }
318 
319     if (uri_ == nullptr) {
320         APP_LOGI("DataAbilityHelper::GetFileTypes before AcquireDataAbility.");
321         dataAbilityProxy_ = AbilityManagerClient::GetInstance()->AcquireDataAbility(uri, tryBind_, token_);
322         APP_LOGI("DataAbilityHelper::GetFileTypes after AcquireDataAbility.");
323         if (dataAbilityProxy_ == nullptr) {
324             APP_LOGE("DataAbilityHelper::GetFileTypes failed dataAbility == nullptr");
325             return matchedMIMEs;
326         }
327         if (isSystemCaller_) {
328             AddDataAbilityDeathRecipient(dataAbilityProxy_->AsObject());
329         }
330     }
331 
332     APP_LOGI("DataAbilityHelper::GetFileTypes before dataAbilityProxy_->GetFileTypes.");
333     matchedMIMEs = dataAbilityProxy_->GetFileTypes(uri, mimeTypeFilter);
334     APP_LOGI("DataAbilityHelper::GetFileTypes after dataAbilityProxy_->GetFileTypes.");
335     if (uri_ == nullptr) {
336         APP_LOGI("DataAbilityHelper::GetFileTypes before ReleaseDataAbility.");
337         int err = AbilityManagerClient::GetInstance()->ReleaseDataAbility(dataAbilityProxy_, token_);
338         APP_LOGI("DataAbilityHelper::GetFileTypes after ReleaseDataAbility.");
339         if (err != ERR_OK) {
340             APP_LOGE("DataAbilityHelper::GetFileTypes failed to ReleaseDataAbility err = %{public}d", err);
341         }
342         dataAbilityProxy_ = nullptr;
343     }
344 
345     APP_LOGI("DataAbilityHelper::GetFileTypes end.");
346     return matchedMIMEs;
347 }
348 
349 /**
350  * @brief Opens a file in a specified remote path.
351  *
352  * @param uri Indicates the path of the file to open.
353  * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
354  * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
355  * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data,
356  *  or "rwt" for read and write access that truncates any existing file.
357  *
358  * @return Returns the file descriptor.
359  */
OpenFile(Uri & uri,const std::string & mode)360 int DataAbilityHelper::OpenFile(Uri &uri, const std::string &mode)
361 {
362     APP_LOGI("DataAbilityHelper::OpenFile start.");
363     std::lock_guard<std::mutex> guard(lock_);
364     int fd = -1;
365     if (!CheckUriParam(uri)) {
366         APP_LOGE("%{public}s called. CheckUriParam uri failed", __func__);
367         return fd;
368     }
369 
370     if (uri_ == nullptr) {
371         APP_LOGI("DataAbilityHelper::OpenFile before AcquireDataAbility.");
372         dataAbilityProxy_ = AbilityManagerClient::GetInstance()->AcquireDataAbility(uri, tryBind_, token_);
373         APP_LOGI("DataAbilityHelper::OpenFile after AcquireDataAbility.");
374         if (dataAbilityProxy_ == nullptr) {
375             APP_LOGE("DataAbilityHelper::OpenFile failed dataAbility == nullptr");
376             return fd;
377         }
378         if (isSystemCaller_) {
379             AddDataAbilityDeathRecipient(dataAbilityProxy_->AsObject());
380         }
381     }
382 
383     APP_LOGI("DataAbilityHelper::OpenFile before dataAbilityProxy_->OpenFile.");
384     fd = dataAbilityProxy_->OpenFile(uri, mode);
385     APP_LOGI("DataAbilityHelper::OpenFile after dataAbilityProxy_->OpenFile.");
386     if (uri_ == nullptr) {
387         APP_LOGI("DataAbilityHelper::OpenFile before ReleaseDataAbility.");
388         int err = AbilityManagerClient::GetInstance()->ReleaseDataAbility(dataAbilityProxy_, token_);
389         APP_LOGI("DataAbilityHelper::OpenFile after ReleaseDataAbility.");
390         if (err != ERR_OK) {
391             APP_LOGE("DataAbilityHelper::OpenFile failed to ReleaseDataAbility err = %{public}d", err);
392         }
393         dataAbilityProxy_ = nullptr;
394     }
395     APP_LOGI("DataAbilityHelper::OpenFile end.");
396     return fd;
397 }
398 
399 /**
400  * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets
401  * inside of their .hap.
402  *
403  * @param uri Indicates the path of the file to open.
404  * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
405  * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
406  * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing
407  * data, or "rwt" for read and write access that truncates any existing file.
408  *
409  * @return Returns the RawFileDescriptor object containing file descriptor.
410  */
OpenRawFile(Uri & uri,const std::string & mode)411 int DataAbilityHelper::OpenRawFile(Uri &uri, const std::string &mode)
412 {
413     APP_LOGI("DataAbilityHelper::OpenRawFile start.");
414     std::lock_guard<std::mutex> guard(lock_);
415     int fd = -1;
416     if (!CheckUriParam(uri)) {
417         APP_LOGE("%{public}s called. CheckUriParam uri failed", __func__);
418         return fd;
419     }
420 
421     if (uri_ == nullptr) {
422         APP_LOGI("DataAbilityHelper::OpenRawFile before AcquireDataAbility.");
423         dataAbilityProxy_ = AbilityManagerClient::GetInstance()->AcquireDataAbility(uri, tryBind_, token_);
424         APP_LOGI("DataAbilityHelper::OpenRawFile after AcquireDataAbility.");
425         if (dataAbilityProxy_ == nullptr) {
426             APP_LOGE("DataAbilityHelper::OpenRawFile failed dataAbility == nullptr");
427             return fd;
428         }
429         if (isSystemCaller_) {
430             AddDataAbilityDeathRecipient(dataAbilityProxy_->AsObject());
431         }
432     }
433     APP_LOGI("DataAbilityHelper::OpenRawFile before dataAbilityProxy_->OpenRawFile.");
434     fd = dataAbilityProxy_->OpenRawFile(uri, mode);
435     APP_LOGI("DataAbilityHelper::OpenRawFile after dataAbilityProxy_->OpenRawFile.");
436     if (uri_ == nullptr) {
437         APP_LOGI("DataAbilityHelper::OpenRawFile before ReleaseDataAbility.");
438         int err = AbilityManagerClient::GetInstance()->ReleaseDataAbility(dataAbilityProxy_, token_);
439         APP_LOGI("DataAbilityHelper::OpenRawFile after ReleaseDataAbility.");
440         if (err != ERR_OK) {
441             APP_LOGE("DataAbilityHelper::OpenRawFile failed to ReleaseDataAbility err = %{public}d", err);
442         }
443         dataAbilityProxy_ = nullptr;
444     }
445     APP_LOGI("DataAbilityHelper::OpenRawFile end.");
446     return fd;
447 }
448 
449 /**
450  * @brief Inserts a single data record into the database.
451  *
452  * @param uri Indicates the path of the data to operate.
453  * @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
454  *
455  * @return Returns the index of the inserted data record.
456  */
Insert(Uri & uri,const NativeRdb::ValuesBucket & value)457 int DataAbilityHelper::Insert(Uri &uri, const NativeRdb::ValuesBucket &value)
458 {
459     APP_LOGI("DataAbilityHelper::Insert start.");
460     std::lock_guard<std::mutex> guard(lock_);
461     int index = -1;
462     if (!CheckUriParam(uri)) {
463         APP_LOGE("%{public}s called. CheckUriParam uri failed", __func__);
464         return index;
465     }
466 
467     if (uri_ == nullptr) {
468         APP_LOGI("DataAbilityHelper::Insert before AcquireDataAbility.");
469         dataAbilityProxy_ = AbilityManagerClient::GetInstance()->AcquireDataAbility(uri, tryBind_, token_);
470         APP_LOGI("DataAbilityHelper::Insert after AcquireDataAbility.");
471         if (dataAbilityProxy_ == nullptr) {
472             APP_LOGE("DataAbilityHelper::Insert failed dataAbility == nullptr");
473             return index;
474         }
475         if (isSystemCaller_) {
476             AddDataAbilityDeathRecipient(dataAbilityProxy_->AsObject());
477         }
478     }
479     APP_LOGI("DataAbilityHelper::Insert before dataAbilityProxy_->Insert.");
480     index = dataAbilityProxy_->Insert(uri, value);
481     APP_LOGI("DataAbilityHelper::Insert after dataAbilityProxy_->Insert.");
482     if (uri_ == nullptr) {
483         APP_LOGI("DataAbilityHelper::Insert before ReleaseDataAbility.");
484         int err = AbilityManagerClient::GetInstance()->ReleaseDataAbility(dataAbilityProxy_, token_);
485         APP_LOGI("DataAbilityHelper::Insert after ReleaseDataAbility.");
486         if (err != ERR_OK) {
487             APP_LOGE("DataAbilityHelper::Insert failed to ReleaseDataAbility err = %{public}d", err);
488         }
489         dataAbilityProxy_ = nullptr;
490     }
491     APP_LOGI("DataAbilityHelper::Insert end.");
492     return index;
493 }
494 
495 /**
496  * @brief Updates data records in the database.
497  *
498  * @param uri Indicates the path of data to update.
499  * @param value Indicates the data to update. This parameter can be null.
500  * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
501  *
502  * @return Returns the number of data records updated.
503  */
Update(Uri & uri,const NativeRdb::ValuesBucket & value,const NativeRdb::DataAbilityPredicates & predicates)504 int DataAbilityHelper::Update(
505     Uri &uri, const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates)
506 {
507     APP_LOGI("DataAbilityHelper::Update start.");
508     std::lock_guard<std::mutex> guard(lock_);
509     int index = -1;
510     if (!CheckUriParam(uri)) {
511         APP_LOGE("%{public}s called. CheckUriParam uri failed", __func__);
512         return index;
513     }
514 
515     if (uri_ == nullptr) {
516         APP_LOGI("DataAbilityHelper::Update before AcquireDataAbility.");
517         dataAbilityProxy_ = AbilityManagerClient::GetInstance()->AcquireDataAbility(uri, tryBind_, token_);
518         APP_LOGI("DataAbilityHelper::Update after AcquireDataAbility.");
519         if (dataAbilityProxy_ == nullptr) {
520             APP_LOGE("DataAbilityHelper::Update failed dataAbility == nullptr");
521             return index;
522         }
523         if (isSystemCaller_) {
524             AddDataAbilityDeathRecipient(dataAbilityProxy_->AsObject());
525         }
526     }
527     APP_LOGI("DataAbilityHelper::Update before dataAbilityProxy_->Update.");
528     index = dataAbilityProxy_->Update(uri, value, predicates);
529     APP_LOGI("DataAbilityHelper::Update after dataAbilityProxy_->Update.");
530     if (uri_ == nullptr) {
531         APP_LOGI("DataAbilityHelper::Update before ReleaseDataAbility.");
532         int err = AbilityManagerClient::GetInstance()->ReleaseDataAbility(dataAbilityProxy_, token_);
533         APP_LOGI("DataAbilityHelper::Update after ReleaseDataAbility.");
534         if (err != ERR_OK) {
535             APP_LOGE("DataAbilityHelper::Update failed to ReleaseDataAbility err = %{public}d", err);
536         }
537         dataAbilityProxy_ = nullptr;
538     }
539     APP_LOGI("DataAbilityHelper::Update end.");
540     return index;
541 }
542 
543 /**
544  * @brief Deletes one or more data records from the database.
545  *
546  * @param uri Indicates the path of the data to operate.
547  * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
548  *
549  * @return Returns the number of data records deleted.
550  */
Delete(Uri & uri,const NativeRdb::DataAbilityPredicates & predicates)551 int DataAbilityHelper::Delete(Uri &uri, const NativeRdb::DataAbilityPredicates &predicates)
552 {
553     APP_LOGI("DataAbilityHelper::Delete start.");
554     std::lock_guard<std::mutex> guard(lock_);
555     int index = -1;
556     if (!CheckUriParam(uri)) {
557         APP_LOGE("%{public}s called. CheckUriParam uri failed", __func__);
558         return index;
559     }
560 
561     if (uri_ == nullptr) {
562         APP_LOGI("DataAbilityHelper::Delete before AcquireDataAbility.");
563         dataAbilityProxy_ = AbilityManagerClient::GetInstance()->AcquireDataAbility(uri, tryBind_, token_);
564         APP_LOGI("DataAbilityHelper::Delete after AcquireDataAbility.");
565         if (dataAbilityProxy_ == nullptr) {
566             APP_LOGE("DataAbilityHelper::Delete failed dataAbility == nullptr");
567             return index;
568         }
569         if (isSystemCaller_) {
570             AddDataAbilityDeathRecipient(dataAbilityProxy_->AsObject());
571         }
572     }
573     APP_LOGI("DataAbilityHelper::Delete before dataAbilityProxy_->Delete.");
574     index = dataAbilityProxy_->Delete(uri, predicates);
575     APP_LOGI("DataAbilityHelper::Delete after dataAbilityProxy_->Delete.");
576     if (uri_ == nullptr) {
577         APP_LOGI("DataAbilityHelper::Delete before ReleaseDataAbility.");
578         int err = AbilityManagerClient::GetInstance()->ReleaseDataAbility(dataAbilityProxy_, token_);
579         APP_LOGI("DataAbilityHelper::Delete after ReleaseDataAbility.");
580         if (err != ERR_OK) {
581             APP_LOGE("DataAbilityHelper::Delete failed to ReleaseDataAbility err = %{public}d", err);
582         }
583         dataAbilityProxy_ = nullptr;
584     }
585     APP_LOGI("DataAbilityHelper::Delete end.");
586     return index;
587 }
588 
589 /**
590  * @brief Deletes one or more data records from the database.
591  *
592  * @param uri Indicates the path of data to query.
593  * @param columns Indicates the columns to query. If this parameter is null, all columns are queried.
594  * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
595  *
596  * @return Returns the query result.
597  */
Query(Uri & uri,std::vector<std::string> & columns,const NativeRdb::DataAbilityPredicates & predicates)598 std::shared_ptr<NativeRdb::AbsSharedResultSet> DataAbilityHelper::Query(
599     Uri &uri, std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates)
600 {
601     APP_LOGI("DataAbilityHelper::Query start.");
602     std::lock_guard<std::mutex> guard(lock_);
603     std::shared_ptr<NativeRdb::AbsSharedResultSet> resultset = nullptr;
604 
605     if (!CheckUriParam(uri)) {
606         APP_LOGE("%{public}s called. CheckUriParam uri failed", __func__);
607         return resultset;
608     }
609 
610     if (uri_ == nullptr) {
611         APP_LOGI("DataAbilityHelper::Query before AcquireDataAbility.");
612         dataAbilityProxy_ = AbilityManagerClient::GetInstance()->AcquireDataAbility(uri, tryBind_, token_);
613         APP_LOGI("DataAbilityHelper::Query after AcquireDataAbility.");
614         if (dataAbilityProxy_ == nullptr) {
615             APP_LOGE("DataAbilityHelper::Query failed dataAbility == nullptr");
616             return resultset;
617         }
618         if (isSystemCaller_) {
619             AddDataAbilityDeathRecipient(dataAbilityProxy_->AsObject());
620         }
621     }
622     APP_LOGI("DataAbilityHelper::Query before dataAbilityProxy_->Query.");
623     resultset = dataAbilityProxy_->Query(uri, columns, predicates);
624     APP_LOGI("DataAbilityHelper::Query after dataAbilityProxy_->Query.");
625     if (uri_ == nullptr) {
626         APP_LOGI("DataAbilityHelper::Query before ReleaseDataAbility.");
627         int err = AbilityManagerClient::GetInstance()->ReleaseDataAbility(dataAbilityProxy_, token_);
628         APP_LOGI("DataAbilityHelper::Query after ReleaseDataAbility.");
629         if (err != ERR_OK) {
630             APP_LOGE("DataAbilityHelper::Query failed to ReleaseDataAbility err = %{public}d", err);
631         }
632         dataAbilityProxy_ = nullptr;
633     }
634     APP_LOGI("DataAbilityHelper::Query end.");
635     return resultset;
636 }
637 
638 /**
639  * @brief Obtains the MIME type matching the data specified by the URI of the Data ability. This method should be
640  * implemented by a Data ability. Data abilities supports general data types, including text, HTML, and JPEG.
641  *
642  * @param uri Indicates the URI of the data.
643  *
644  * @return Returns the MIME type that matches the data specified by uri.
645  */
GetType(Uri & uri)646 std::string DataAbilityHelper::GetType(Uri &uri)
647 {
648     APP_LOGI("DataAbilityHelper::GetType start.");
649     std::lock_guard<std::mutex> guard(lock_);
650     std::string type;
651     if (!CheckUriParam(uri)) {
652         APP_LOGE("%{public}s called. CheckUriParam uri failed", __func__);
653         return type;
654     }
655 
656     if (uri_ == nullptr) {
657         APP_LOGI("DataAbilityHelper::GetType before AcquireDataAbility.");
658         dataAbilityProxy_ = AbilityManagerClient::GetInstance()->AcquireDataAbility(uri, tryBind_, token_);
659         APP_LOGI("DataAbilityHelper::GetType after AcquireDataAbility.");
660         if (dataAbilityProxy_ == nullptr) {
661             APP_LOGE("DataAbilityHelper::GetType failed dataAbility == nullptr");
662             return type;
663         }
664         if (isSystemCaller_) {
665             AddDataAbilityDeathRecipient(dataAbilityProxy_->AsObject());
666         }
667     }
668     APP_LOGI("DataAbilityHelper::GetType before dataAbilityProxy_->GetType.");
669     type = dataAbilityProxy_->GetType(uri);
670     APP_LOGI("DataAbilityHelper::GetType after dataAbilityProxy_->GetType.");
671     if (uri_ == nullptr) {
672         APP_LOGI("DataAbilityHelper::GetType before ReleaseDataAbility.");
673         int err = AbilityManagerClient::GetInstance()->ReleaseDataAbility(dataAbilityProxy_, token_);
674         APP_LOGI("DataAbilityHelper::GetType after ReleaseDataAbility.");
675         if (err != ERR_OK) {
676             APP_LOGE("DataAbilityHelper::GetType failed to ReleaseDataAbility err = %{public}d", err);
677         }
678         dataAbilityProxy_ = nullptr;
679     }
680     APP_LOGI("DataAbilityHelper::GetType end.");
681     return type;
682 }
683 
684 /**
685  * @brief Reloads data in the database.
686  *
687  * @param uri Indicates the position where the data is to reload. This parameter is mandatory.
688  * @param extras Indicates the PacMap object containing the additional parameters to be passed in this call. This
689  * parameter can be null. If a custom Sequenceable object is put in the PacMap object and will be transferred across
690  * processes, you must call BasePacMap.setClassLoader(ClassLoader) to set a class loader for the custom object.
691  *
692  * @return Returns true if the data is successfully reloaded; returns false otherwise.
693  */
Reload(Uri & uri,const PacMap & extras)694 bool DataAbilityHelper::Reload(Uri &uri, const PacMap &extras)
695 {
696     APP_LOGI("DataAbilityHelper::Reload start.");
697     std::lock_guard<std::mutex> guard(lock_);
698     bool ret = false;
699     if (!CheckUriParam(uri)) {
700         APP_LOGE("%{public}s called. CheckUriParam uri failed", __func__);
701         return ret;
702     }
703 
704     if (uri_ == nullptr) {
705         APP_LOGI("DataAbilityHelper::Reload before AcquireDataAbility.");
706         dataAbilityProxy_ = AbilityManagerClient::GetInstance()->AcquireDataAbility(uri, tryBind_, token_);
707         APP_LOGI("DataAbilityHelper::Reload after AcquireDataAbility.");
708         if (dataAbilityProxy_ == nullptr) {
709             APP_LOGE("DataAbilityHelper::Reload failed dataAbility == nullptr");
710             return ret;
711         }
712         if (isSystemCaller_) {
713             AddDataAbilityDeathRecipient(dataAbilityProxy_->AsObject());
714         }
715     }
716     APP_LOGI("DataAbilityHelper::Reload before dataAbilityProxy_->Reload.");
717     ret = dataAbilityProxy_->Reload(uri, extras);
718     APP_LOGI("DataAbilityHelper::Reload after dataAbilityProxy_->Reload.");
719     if (uri_ == nullptr) {
720         APP_LOGI("DataAbilityHelper::Reload before ReleaseDataAbility.");
721         int err = AbilityManagerClient::GetInstance()->ReleaseDataAbility(dataAbilityProxy_, token_);
722         APP_LOGI("DataAbilityHelper::Reload after ReleaseDataAbility.");
723         if (err != ERR_OK) {
724             APP_LOGE("DataAbilityHelper::Reload failed to ReleaseDataAbility err = %{public}d", err);
725         }
726         dataAbilityProxy_ = nullptr;
727     }
728     APP_LOGI("DataAbilityHelper::Reload end.");
729     return ret;
730 }
731 
732 /**
733  * @brief Inserts multiple data records into the database.
734  *
735  * @param uri Indicates the path of the data to operate.
736  * @param values Indicates the data records to insert.
737  *
738  * @return Returns the number of data records inserted.
739  */
BatchInsert(Uri & uri,const std::vector<NativeRdb::ValuesBucket> & values)740 int DataAbilityHelper::BatchInsert(Uri &uri, const std::vector<NativeRdb::ValuesBucket> &values)
741 {
742     APP_LOGI("DataAbilityHelper::BatchInsert start.");
743     std::lock_guard<std::mutex> guard(lock_);
744     int ret = -1;
745     if (!CheckUriParam(uri)) {
746         APP_LOGE("%{public}s called. CheckUriParam uri failed", __func__);
747         return ret;
748     }
749 
750     if (uri_ == nullptr) {
751         APP_LOGI("DataAbilityHelper::BatchInsert before AcquireDataAbility.");
752         dataAbilityProxy_ = AbilityManagerClient::GetInstance()->AcquireDataAbility(uri, tryBind_, token_);
753         APP_LOGI("DataAbilityHelper::BatchInsert after AcquireDataAbility.");
754         if (dataAbilityProxy_ == nullptr) {
755             APP_LOGE("DataAbilityHelper::BatchInsert failed dataAbility == nullptr");
756             return ret;
757         }
758         if (isSystemCaller_) {
759             AddDataAbilityDeathRecipient(dataAbilityProxy_->AsObject());
760         }
761     }
762     APP_LOGI("DataAbilityHelper::BatchInsert before dataAbilityProxy_->BatchInsert.");
763     ret = dataAbilityProxy_->BatchInsert(uri, values);
764     APP_LOGI("DataAbilityHelper::BatchInsert after dataAbilityProxy_->BatchInsert.");
765     if (uri_ == nullptr) {
766         APP_LOGI("DataAbilityHelper::BatchInsert before ReleaseDataAbility.");
767         int err = AbilityManagerClient::GetInstance()->ReleaseDataAbility(dataAbilityProxy_, token_);
768         APP_LOGI("DataAbilityHelper::BatchInsert after ReleaseDataAbility.");
769         if (err != ERR_OK) {
770             APP_LOGE("DataAbilityHelper::BatchInsert failed to ReleaseDataAbility err = %{public}d", err);
771         }
772         dataAbilityProxy_ = nullptr;
773     }
774     APP_LOGI("DataAbilityHelper::BatchInsert end.");
775     return ret;
776 }
777 
CheckUriParam(const Uri & uri)778 bool DataAbilityHelper::CheckUriParam(const Uri &uri)
779 {
780     APP_LOGI("DataAbilityHelper::CheckUriParam start.");
781     Uri checkUri(uri.ToString());
782     if (!CheckOhosUri(checkUri)) {
783         APP_LOGE("DataAbilityHelper::CheckUriParam failed. CheckOhosUri uri failed");
784         return false;
785     }
786 
787     if (uri_ != nullptr) {
788         if (!CheckOhosUri(*uri_)) {
789             APP_LOGE("DataAbilityHelper::CheckUriParam failed. CheckOhosUri uri_ failed");
790             return false;
791         }
792 
793         std::vector<std::string> checkSegments;
794         checkUri.GetPathSegments(checkSegments);
795 
796         std::vector<std::string> segments;
797         uri_->GetPathSegments(segments);
798 
799         if (checkSegments[0] != segments[0]) {
800             APP_LOGE("DataAbilityHelper::CheckUriParam failed. the dataability in uri doesn't equal the one in uri_.");
801             return false;
802         }
803     }
804     APP_LOGI("DataAbilityHelper::CheckUriParam end.");
805     return true;
806 }
807 
CheckOhosUri(const Uri & uri)808 bool DataAbilityHelper::CheckOhosUri(const Uri &uri)
809 {
810     APP_LOGI("DataAbilityHelper::CheckOhosUri start.");
811     Uri checkUri(uri.ToString());
812     if (checkUri.GetScheme() != SchemeOhos) {
813         APP_LOGE("DataAbilityHelper::CheckOhosUri failed. uri is not a dataability one.");
814         return false;
815     }
816 
817     std::vector<std::string> segments;
818     checkUri.GetPathSegments(segments);
819     if (segments.empty()) {
820         APP_LOGE("DataAbilityHelper::CheckOhosUri failed. There is no segments in the uri.");
821         return false;
822     }
823 
824     if (checkUri.GetPath() == "") {
825         APP_LOGE("DataAbilityHelper::CheckOhosUri failed. The path in the uri is empty.");
826         return false;
827     }
828     APP_LOGI("DataAbilityHelper::CheckOhosUri end.");
829     return true;
830 }
831 
832 /**
833  * @brief Registers an observer to DataObsMgr specified by the given Uri.
834  *
835  * @param uri, Indicates the path of the data to operate.
836  * @param dataObserver, Indicates the IDataAbilityObserver object.
837  */
RegisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)838 void DataAbilityHelper::RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
839 {
840     APP_LOGI("DataAbilityHelper::RegisterObserver start.");
841     if (!CheckUriParam(uri)) {
842         APP_LOGE("%{public}s called. CheckUriParam uri failed", __func__);
843         return;
844     }
845 
846     if (dataObserver == nullptr) {
847         APP_LOGE("%{public}s called. dataObserver is nullptr", __func__);
848         return;
849     }
850 
851     Uri tmpUri(uri.ToString());
852 
853     std::lock_guard<std::mutex> lock_l(oplock_);
854     sptr<AAFwk::IAbilityScheduler> dataAbilityProxy = nullptr;
855     if (uri_ == nullptr) {
856         auto dataability = registerMap_.find(dataObserver);
857         if (dataability == registerMap_.end()) {
858             dataAbilityProxy = AbilityManagerClient::GetInstance()->AcquireDataAbility(uri, tryBind_, token_);
859             registerMap_.emplace(dataObserver, dataAbilityProxy);
860             uriMap_.emplace(dataObserver, tmpUri.GetPath());
861         } else {
862             auto path = uriMap_.find(dataObserver);
863             if (path->second != tmpUri.GetPath()) {
864                 APP_LOGE("DataAbilityHelper::RegisterObserver failed input uri's path is not equal the one the "
865                          "observer used");
866                 return;
867             }
868             dataAbilityProxy = dataability->second;
869         }
870     } else {
871         dataAbilityProxy = dataAbilityProxy_;
872     }
873 
874     if (dataAbilityProxy == nullptr) {
875         APP_LOGE("DataAbilityHelper::RegisterObserver failed dataAbility == nullptr");
876         registerMap_.erase(dataObserver);
877         uriMap_.erase(dataObserver);
878         return;
879     }
880     dataAbilityProxy->ScheduleRegisterObserver(uri, dataObserver);
881     APP_LOGI("DataAbilityHelper::RegisterObserver end.");
882 }
883 
884 /**
885  * @brief Deregisters an observer used for DataObsMgr specified by the given Uri.
886  *
887  * @param uri, Indicates the path of the data to operate.
888  * @param dataObserver, Indicates the IDataAbilityObserver object.
889  */
UnregisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)890 void DataAbilityHelper::UnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
891 {
892     APP_LOGI("DataAbilityHelper::UnregisterObserver start.");
893     if (!CheckUriParam(uri)) {
894         APP_LOGE("%{public}s called. CheckUriParam uri failed", __func__);
895         return;
896     }
897 
898     if (dataObserver == nullptr) {
899         APP_LOGE("%{public}s called. dataObserver is nullptr", __func__);
900         return;
901     }
902 
903     Uri tmpUri(uri.ToString());
904     std::lock_guard<std::mutex> lock_l(oplock_);
905     sptr<AAFwk::IAbilityScheduler> dataAbilityProxy = nullptr;
906     if (uri_ == nullptr) {
907         auto dataability = registerMap_.find(dataObserver);
908         if (dataability == registerMap_.end()) {
909             return;
910         }
911         auto path = uriMap_.find(dataObserver);
912         if (path->second != tmpUri.GetPath()) {
913             APP_LOGE("DataAbilityHelper::UnregisterObserver failed input uri's path is not equal the one the "
914                      "observer used");
915             return;
916         }
917         dataAbilityProxy = dataability->second;
918     } else {
919         dataAbilityProxy = dataAbilityProxy_;
920     }
921 
922     if (dataAbilityProxy == nullptr) {
923         APP_LOGE("DataAbilityHelper::UnregisterObserver failed dataAbility == nullptr");
924         return;
925     }
926 
927     dataAbilityProxy->ScheduleUnregisterObserver(uri, dataObserver);
928     int err = AbilityManagerClient::GetInstance()->ReleaseDataAbility(dataAbilityProxy, token_);
929     if (err != ERR_OK) {
930         APP_LOGE("DataAbilityHelper::UnregisterObserver failed to ReleaseDataAbility err = %{public}d", err);
931     }
932     registerMap_.erase(dataObserver);
933     uriMap_.erase(dataObserver);
934     APP_LOGI("DataAbilityHelper::UnregisterObserver end.");
935 }
936 
937 /**
938  * @brief Notifies the registered observers of a change to the data resource specified by Uri.
939  *
940  * @param uri, Indicates the path of the data to operate.
941  */
NotifyChange(const Uri & uri)942 void DataAbilityHelper::NotifyChange(const Uri &uri)
943 {
944     APP_LOGI("DataAbilityHelper::NotifyChange start.");
945     if (!CheckUriParam(uri)) {
946         APP_LOGE("%{public}s called. CheckUriParam uri failed", __func__);
947         return;
948     }
949 
950     if (dataAbilityProxy_ == nullptr) {
951         dataAbilityProxy_ = AbilityManagerClient::GetInstance()->AcquireDataAbility(uri, tryBind_, token_);
952         if (dataAbilityProxy_ == nullptr) {
953             APP_LOGE("DataAbilityHelper::NotifyChange failed dataAbility == nullptr");
954             return;
955         }
956     }
957 
958     dataAbilityProxy_->ScheduleNotifyChange(uri);
959 
960     if (uri_ == nullptr) {
961         int err = AbilityManagerClient::GetInstance()->ReleaseDataAbility(dataAbilityProxy_, token_);
962         if (err != ERR_OK) {
963             APP_LOGE("DataAbilityHelper::NotifyChange failed to ReleaseDataAbility err = %{public}d", err);
964         }
965         dataAbilityProxy_ = nullptr;
966     }
967     APP_LOGI("DataAbilityHelper::NotifyChange end.");
968 }
969 
970 /**
971  * @brief Converts the given uri that refer to the Data ability into a normalized URI. A normalized URI can be used
972  * across devices, persisted, backed up, and restored. It can refer to the same item in the Data ability even if the
973  * context has changed. If you implement URI normalization for a Data ability, you must also implement
974  * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to any
975  * method that is called on the Data ability must require normalization verification and denormalization. The default
976  * implementation of this method returns null, indicating that this Data ability does not support URI normalization.
977  *
978  * @param uri Indicates the Uri object to normalize.
979  *
980  * @return Returns the normalized Uri object if the Data ability supports URI normalization; returns null otherwise.
981  */
NormalizeUri(Uri & uri)982 Uri DataAbilityHelper::NormalizeUri(Uri &uri)
983 {
984     APP_LOGI("DataAbilityHelper::NormalizeUri start.");
985     std::lock_guard<std::mutex> guard(lock_);
986     Uri urivalue("");
987     if (!CheckUriParam(uri)) {
988         APP_LOGE("%{public}s called. CheckUriParam uri failed", __func__);
989         return urivalue;
990     }
991 
992     if (uri_ == nullptr) {
993         APP_LOGI("DataAbilityHelper::NormalizeUri before AcquireDataAbility.");
994         dataAbilityProxy_ = AbilityManagerClient::GetInstance()->AcquireDataAbility(uri, tryBind_, token_);
995         APP_LOGI("DataAbilityHelper::NormalizeUri after AcquireDataAbility.");
996         if (dataAbilityProxy_ == nullptr) {
997             APP_LOGE("DataAbilityHelper::NormalizeUri failed dataAbility == nullptr");
998             return urivalue;
999         }
1000         if (isSystemCaller_) {
1001             AddDataAbilityDeathRecipient(dataAbilityProxy_->AsObject());
1002         }
1003     }
1004     APP_LOGI("DataAbilityHelper::NormalizeUri before dataAbilityProxy_->NormalizeUri.");
1005     urivalue = dataAbilityProxy_->NormalizeUri(uri);
1006     APP_LOGI("DataAbilityHelper::NormalizeUri after dataAbilityProxy_->NormalizeUri.");
1007     if (uri_ == nullptr) {
1008         APP_LOGI("DataAbilityHelper::NormalizeUri before ReleaseDataAbility.");
1009         int err = AbilityManagerClient::GetInstance()->ReleaseDataAbility(dataAbilityProxy_, token_);
1010         APP_LOGI("DataAbilityHelper::NormalizeUri after ReleaseDataAbility.");
1011         if (err != ERR_OK) {
1012             APP_LOGE("DataAbilityHelper::NormalizeUri failed to ReleaseDataAbility err = %{public}d", err);
1013         }
1014         dataAbilityProxy_ = nullptr;
1015     }
1016     APP_LOGI("DataAbilityHelper::NormalizeUri end.");
1017     return urivalue;
1018 }
1019 
1020 /**
1021  * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one.
1022  * The default implementation of this method returns the original URI passed to it.
1023  *
1024  * @param uri uri Indicates the Uri object to denormalize.
1025  *
1026  * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri passed to
1027  * this method if there is nothing to do; returns null if the data identified by the original Uri cannot be found in the
1028  * current environment.
1029  */
DenormalizeUri(Uri & uri)1030 Uri DataAbilityHelper::DenormalizeUri(Uri &uri)
1031 {
1032     APP_LOGI("DataAbilityHelper::DenormalizeUri start.");
1033     std::lock_guard<std::mutex> guard(lock_);
1034     Uri urivalue("");
1035     if (!CheckUriParam(uri)) {
1036         APP_LOGE("%{public}s called. CheckUriParam uri failed", __func__);
1037         return urivalue;
1038     }
1039 
1040     if (uri_ == nullptr) {
1041         APP_LOGI("DataAbilityHelper::DenormalizeUri before AcquireDataAbility.");
1042         dataAbilityProxy_ = AbilityManagerClient::GetInstance()->AcquireDataAbility(uri, tryBind_, token_);
1043         APP_LOGI("DataAbilityHelper::DenormalizeUri after AcquireDataAbility.");
1044         if (dataAbilityProxy_ == nullptr) {
1045             APP_LOGE("DataAbilityHelper::DenormalizeUri failed dataAbility == nullptr");
1046             return urivalue;
1047         }
1048         if (isSystemCaller_) {
1049             AddDataAbilityDeathRecipient(dataAbilityProxy_->AsObject());
1050         }
1051     }
1052     APP_LOGI("DataAbilityHelper::DenormalizeUri before dataAbilityProxy_->DenormalizeUri.");
1053     urivalue = dataAbilityProxy_->DenormalizeUri(uri);
1054     APP_LOGI("DataAbilityHelper::DenormalizeUri after dataAbilityProxy_->DenormalizeUri.");
1055     if (uri_ == nullptr) {
1056         APP_LOGI("DataAbilityHelper::DenormalizeUri before ReleaseDataAbility.");
1057         int err = AbilityManagerClient::GetInstance()->ReleaseDataAbility(dataAbilityProxy_, token_);
1058         APP_LOGI("DataAbilityHelper::DenormalizeUri after ReleaseDataAbility.");
1059         if (err != ERR_OK) {
1060             APP_LOGE("DataAbilityHelper::DenormalizeUri failed to ReleaseDataAbility err = %{public}d", err);
1061         }
1062         dataAbilityProxy_ = nullptr;
1063     }
1064     APP_LOGI("DataAbilityHelper::DenormalizeUri end.");
1065     return urivalue;
1066 }
1067 
OnRemoteDied(const wptr<IRemoteObject> & remote)1068 void DataAbilityDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
1069 {
1070     APP_LOGI("recv DataAbilityDeathRecipient death notice");
1071     if (handler_) {
1072         handler_(remote);
1073     }
1074     APP_LOGI("DataAbilityHelper::OnRemoteDied end.");
1075 }
1076 
DataAbilityDeathRecipient(RemoteDiedHandler handler)1077 DataAbilityDeathRecipient::DataAbilityDeathRecipient(RemoteDiedHandler handler) : handler_(handler)
1078 {}
1079 
~DataAbilityDeathRecipient()1080 DataAbilityDeathRecipient::~DataAbilityDeathRecipient()
1081 {}
ExecuteBatch(const Uri & uri,const std::vector<std::shared_ptr<DataAbilityOperation>> & operations)1082 std::vector<std::shared_ptr<DataAbilityResult>> DataAbilityHelper::ExecuteBatch(
1083     const Uri &uri, const std::vector<std::shared_ptr<DataAbilityOperation>> &operations)
1084 {
1085     APP_LOGI("DataAbilityHelper::ExecuteBatch start");
1086     std::vector<std::shared_ptr<DataAbilityResult>> results;
1087     if (!CheckUriParam(uri)) {
1088         APP_LOGE("DataAbilityHelper::ExecuteBatch. CheckUriParam uri failed");
1089         return results;
1090     }
1091     if (uri_ == nullptr) {
1092         APP_LOGI("DataAbilityHelper::ExecuteBatch before AcquireDataAbility.");
1093         dataAbilityProxy_ = AbilityManagerClient::GetInstance()->AcquireDataAbility(uri, tryBind_, token_);
1094         APP_LOGI("DataAbilityHelper::ExecuteBatch after AcquireDataAbility.");
1095         if (dataAbilityProxy_ == nullptr) {
1096             APP_LOGE("DataAbilityHelper::ExecuteBatch failed dataAbility == nullptr");
1097             return results;
1098         }
1099     }
1100 
1101     APP_LOGI("DataAbilityHelper::ExecuteBatch before dataAbilityProxy_->ExecuteBatch.");
1102     results = dataAbilityProxy_->ExecuteBatch(operations);
1103     APP_LOGI("DataAbilityHelper::ExecuteBatch after dataAbilityProxy_->ExecuteBatch.");
1104     if (uri_ == nullptr) {
1105         APP_LOGI("DataAbilityHelper::ExecuteBatch before ReleaseDataAbility.");
1106         int err = AbilityManagerClient::GetInstance()->ReleaseDataAbility(dataAbilityProxy_, token_);
1107         APP_LOGI("DataAbilityHelper::ExecuteBatch after ReleaseDataAbility.");
1108         if (err != ERR_OK) {
1109             APP_LOGE("DataAbilityHelper::ExecuteBatch failed to ReleaseDataAbility err = %{public}d", err);
1110         }
1111         dataAbilityProxy_ = nullptr;
1112     }
1113     APP_LOGI("DataAbilityHelper::ExecuteBatch end");
1114     return results;
1115 }
1116 }  // namespace AppExecFwk
1117 }  // namespace OHOS