1 /*
2 * Copyright (C) 2022-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
16 #include "media_userfile_client.h"
17 #include "medialibrary_errno.h"
18 #include "medialibrary_helper_container.h"
19 #include "media_log.h"
20 #include "userfilemgr_uri.h"
21 #include "medialibrary_operation.h"
22 #include "media_asset_rdbstore.h"
23 #include "iservice_registry.h"
24
25 using namespace std;
26 using namespace OHOS::DataShare;
27 using namespace OHOS::AppExecFwk;
28
29 static const int STORAGE_MANAGER_MANAGER_ID = 5003;
30
31 namespace OHOS {
32 namespace Media {
33
IsValid()34 bool UserFileClient::IsValid()
35 {
36 return sDataShareHelper_ != nullptr;
37 }
38
Init()39 void UserFileClient::Init()
40 {
41 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
42 if (saManager == nullptr) {
43 MEDIA_ERR_LOG("Get system ability mgr failed.");
44 return;
45 }
46 auto remoteObj = saManager->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
47 if (remoteObj == nullptr) {
48 MEDIA_ERR_LOG("GetSystemAbility Service Failed.");
49 return;
50 }
51
52 Init(remoteObj);
53 if (sDataShareHelper_ == nullptr) {
54 MEDIA_ERR_LOG("Create DataShareHelper failed.");
55 return;
56 }
57 }
58
Init(const sptr<IRemoteObject> & token,bool isSetHelper)59 void UserFileClient::Init(const sptr<IRemoteObject> &token, bool isSetHelper)
60 {
61 if (sDataShareHelper_ == nullptr) {
62 sDataShareHelper_ = DataShare::DataShareHelper::Creator(token, MEDIALIBRARY_DATA_URI);
63 }
64
65 if (isSetHelper) {
66 MediaLibraryHelperContainer::GetInstance()->SetDataShareHelper(sDataShareHelper_);
67 }
68 }
69
Query(Uri & uri,const DataSharePredicates & predicates,std::vector<std::string> & columns,int & errCode)70 shared_ptr<DataShareResultSet> UserFileClient::Query(Uri &uri, const DataSharePredicates &predicates,
71 std::vector<std::string> &columns, int &errCode)
72 {
73 if (!IsValid()) {
74 MEDIA_ERR_LOG("Query fail, helper null");
75 return nullptr;
76 }
77
78 shared_ptr<DataShareResultSet> resultSet = nullptr;
79 OperationObject object = OperationObject::UNKNOWN_OBJECT;
80 if (MediaAssetRdbStore::GetInstance()->IsQueryAccessibleViaSandBox(uri, object, predicates)) {
81 resultSet = MediaAssetRdbStore::GetInstance()->Query(predicates, columns, object, errCode);
82 } else {
83 DatashareBusinessError businessError;
84 resultSet = sDataShareHelper_->Query(uri, predicates, columns, &businessError);
85 errCode = businessError.GetCode();
86 }
87 return resultSet;
88 }
89
Insert(Uri & uri,const DataShareValuesBucket & value)90 int UserFileClient::Insert(Uri &uri, const DataShareValuesBucket &value)
91 {
92 if (!IsValid()) {
93 MEDIA_ERR_LOG("insert fail, helper null");
94 return E_FAIL;
95 }
96 int index = sDataShareHelper_->Insert(uri, value);
97 return index;
98 }
99
InsertExt(Uri & uri,const DataShareValuesBucket & value,string & result)100 int UserFileClient::InsertExt(Uri &uri, const DataShareValuesBucket &value, string &result)
101 {
102 if (!IsValid()) {
103 MEDIA_ERR_LOG("insert fail, helper null");
104 return E_FAIL;
105 }
106 int index = sDataShareHelper_->InsertExt(uri, value, result);
107 return index;
108 }
109
110
Delete(Uri & uri,const DataSharePredicates & predicates)111 int UserFileClient::Delete(Uri &uri, const DataSharePredicates &predicates)
112 {
113 if (!IsValid()) {
114 MEDIA_ERR_LOG("delete fail, helper null");
115 return E_FAIL;
116 }
117 return sDataShareHelper_->Delete(uri, predicates);
118 }
119
OpenFile(Uri & uri,const std::string & mode)120 int UserFileClient::OpenFile(Uri &uri, const std::string &mode)
121 {
122 if (!IsValid()) {
123 MEDIA_ERR_LOG("Open file fail, helper null");
124 return E_FAIL;
125 }
126 return sDataShareHelper_->OpenFile(uri, mode);
127 }
128
Update(Uri & uri,const DataSharePredicates & predicates,const DataShareValuesBucket & value)129 int UserFileClient::Update(Uri &uri, const DataSharePredicates &predicates,
130 const DataShareValuesBucket &value)
131 {
132 if (!IsValid()) {
133 MEDIA_ERR_LOG("update fail, helper null");
134 return E_FAIL;
135 }
136 return sDataShareHelper_->Update(uri, predicates, value);
137 }
138
Clear()139 void UserFileClient::Clear()
140 {
141 sDataShareHelper_ = nullptr;
142 }
143
144 }
145 }
146