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 "userfile_client.h"
17
18 #include "ability.h"
19
20 #include "medialibrary_errno.h"
21 #include "medialibrary_napi_log.h"
22 #include "medialibrary_helper_container.h"
23
24 using namespace std;
25 using namespace OHOS::DataShare;
26 using namespace OHOS::AppExecFwk;
27 namespace OHOS {
28 namespace Media {
29
GetDataShareHelper(napi_env env,napi_callback_info info)30 shared_ptr<DataShare::DataShareHelper> UserFileClient::GetDataShareHelper(napi_env env, napi_callback_info info)
31 {
32 size_t argc = ARGS_ONE;
33 napi_value argv[ARGS_ONE] = {0};
34 napi_value thisVar = nullptr;
35 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
36
37 std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = nullptr;
38 bool isStageMode = false;
39 napi_status status = OHOS::AbilityRuntime::IsStageContext(env, argv[0], isStageMode);
40 if (status != napi_ok || !isStageMode) {
41 auto ability = OHOS::AbilityRuntime::GetCurrentAbility(env);
42 if (ability == nullptr) {
43 NAPI_ERR_LOG("Failed to get native ability instance");
44 return nullptr;
45 }
46 auto context = ability->GetContext();
47 if (context == nullptr) {
48 NAPI_ERR_LOG("Failed to get native context instance");
49 return nullptr;
50 }
51 dataShareHelper = DataShare::DataShareHelper::Creator(context->GetToken(), MEDIALIBRARY_DATA_URI);
52 } else {
53 auto context = OHOS::AbilityRuntime::GetStageModeContext(env, argv[0]);
54 if (context == nullptr) {
55 NAPI_ERR_LOG("Failed to get native stage context instance");
56 return nullptr;
57 }
58 dataShareHelper = DataShare::DataShareHelper::Creator(context->GetToken(), MEDIALIBRARY_DATA_URI);
59 }
60 MediaLibraryHelperContainer::GetInstance()->SetDataShareHelper(dataShareHelper);
61 return dataShareHelper;
62 }
63
CheckIsStage(napi_env env,napi_callback_info info,bool & result)64 napi_status UserFileClient::CheckIsStage(napi_env env, napi_callback_info info, bool &result)
65 {
66 size_t argc = ARGS_ONE;
67 napi_value argv[ARGS_ONE] = {0};
68 napi_value thisVar = nullptr;
69 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
70 if (status != napi_ok) {
71 NAPI_ERR_LOG("Failed to get cb info, status=%{public}d", (int) status);
72 return status;
73 }
74
75 result = false;
76 status = OHOS::AbilityRuntime::IsStageContext(env, argv[0], result);
77 if (status != napi_ok) {
78 NAPI_ERR_LOG("Failed to get stage mode, status=%{public}d", (int) status);
79 return status;
80 }
81 return napi_ok;
82 }
83
ParseTokenInStageMode(napi_env env,napi_callback_info info)84 sptr<IRemoteObject> UserFileClient::ParseTokenInStageMode(napi_env env, napi_callback_info info)
85 {
86 size_t argc = ARGS_ONE;
87 napi_value argv[ARGS_ONE] = {0};
88 napi_value thisVar = nullptr;
89 if (napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr) != napi_ok) {
90 NAPI_ERR_LOG("Failed to get cb info");
91 return nullptr;
92 }
93
94 auto context = OHOS::AbilityRuntime::GetStageModeContext(env, argv[0]);
95 if (context == nullptr) {
96 NAPI_ERR_LOG("Failed to get native stage context instance");
97 return nullptr;
98 }
99 return context->GetToken();
100 }
101
ParseTokenInAbility(napi_env env,napi_callback_info info)102 sptr<IRemoteObject> UserFileClient::ParseTokenInAbility(napi_env env, napi_callback_info info)
103 {
104 size_t argc = ARGS_ONE;
105 napi_value argv[ARGS_ONE] = {0};
106 napi_value thisVar = nullptr;
107 if (napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr) != napi_ok) {
108 NAPI_ERR_LOG("Failed to get cb info");
109 return nullptr;
110 }
111
112 auto ability = OHOS::AbilityRuntime::GetCurrentAbility(env);
113 if (ability == nullptr) {
114 NAPI_ERR_LOG("Failed to get native ability instance");
115 return nullptr;
116 }
117 auto context = ability->GetContext();
118 if (context == nullptr) {
119 NAPI_ERR_LOG("Failed to get native context instance");
120 return nullptr;
121 }
122 return context->GetToken();
123 }
124
IsValid()125 bool UserFileClient::IsValid()
126 {
127 return sDataShareHelper_ != nullptr;
128 }
129
Init(const sptr<IRemoteObject> & token,bool isSetHelper)130 void UserFileClient::Init(const sptr<IRemoteObject> &token, bool isSetHelper)
131 {
132 sDataShareHelper_ = DataShare::DataShareHelper::Creator(token, MEDIALIBRARY_DATA_URI);
133 if (isSetHelper) {
134 MediaLibraryHelperContainer::GetInstance()->SetDataShareHelper(sDataShareHelper_);
135 }
136 }
137
Init(napi_env env,napi_callback_info info)138 void UserFileClient::Init(napi_env env, napi_callback_info info)
139 {
140 sDataShareHelper_ = GetDataShareHelper(env, info);
141 }
142
Query(Uri & uri,const DataSharePredicates & predicates,std::vector<std::string> & columns,int & errCode)143 shared_ptr<DataShareResultSet> UserFileClient::Query(Uri &uri, const DataSharePredicates &predicates,
144 std::vector<std::string> &columns, int &errCode)
145 {
146 if (!IsValid()) {
147 NAPI_ERR_LOG("Query fail, helper null");
148 return nullptr;
149 }
150 DatashareBusinessError businessError;
151 auto resultSet = sDataShareHelper_->Query(uri, predicates, columns, &businessError);
152 errCode = businessError.GetCode();
153 return resultSet;
154 }
155
Insert(Uri & uri,const DataShareValuesBucket & value)156 int UserFileClient::Insert(Uri &uri, const DataShareValuesBucket &value)
157 {
158 if (!IsValid()) {
159 NAPI_ERR_LOG("insert fail, helper null");
160 return E_FAIL;
161 }
162 int index = sDataShareHelper_->Insert(uri, value);
163 return index;
164 }
165
InsertExt(Uri & uri,const DataShareValuesBucket & value,string & result)166 int UserFileClient::InsertExt(Uri &uri, const DataShareValuesBucket &value, string &result)
167 {
168 if (!IsValid()) {
169 NAPI_ERR_LOG("insert fail, helper null");
170 return E_FAIL;
171 }
172 int index = sDataShareHelper_->InsertExt(uri, value, result);
173 return index;
174 }
175
BatchInsert(Uri & uri,const std::vector<DataShare::DataShareValuesBucket> & values)176 int UserFileClient::BatchInsert(Uri &uri, const std::vector<DataShare::DataShareValuesBucket> &values)
177 {
178 if (!IsValid()) {
179 NAPI_ERR_LOG("Batch insert fail, helper null");
180 return E_FAIL;
181 }
182 return sDataShareHelper_->BatchInsert(uri, values);
183 }
184
Delete(Uri & uri,const DataSharePredicates & predicates)185 int UserFileClient::Delete(Uri &uri, const DataSharePredicates &predicates)
186 {
187 if (!IsValid()) {
188 NAPI_ERR_LOG("delete fail, helper null");
189 return E_FAIL;
190 }
191 return sDataShareHelper_->Delete(uri, predicates);
192 }
193
NotifyChange(const Uri & uri)194 void UserFileClient::NotifyChange(const Uri &uri)
195 {
196 if (!IsValid()) {
197 NAPI_ERR_LOG("notify change fail, helper null");
198 return;
199 }
200 sDataShareHelper_->NotifyChange(uri);
201 }
202
RegisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)203 void UserFileClient::RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
204 {
205 if (!IsValid()) {
206 NAPI_ERR_LOG("register observer fail, helper null");
207 return;
208 }
209 sDataShareHelper_->RegisterObserver(uri, dataObserver);
210 }
211
UnregisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)212 void UserFileClient::UnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
213 {
214 if (!IsValid()) {
215 NAPI_ERR_LOG("unregister observer fail, helper null");
216 return;
217 }
218 sDataShareHelper_->UnregisterObserver(uri, dataObserver);
219 }
220
OpenFile(Uri & uri,const std::string & mode)221 int UserFileClient::OpenFile(Uri &uri, const std::string &mode)
222 {
223 if (!IsValid()) {
224 NAPI_ERR_LOG("Open file fail, helper null");
225 return E_FAIL;
226 }
227 return sDataShareHelper_->OpenFile(uri, mode);
228 }
229
Update(Uri & uri,const DataSharePredicates & predicates,const DataShareValuesBucket & value)230 int UserFileClient::Update(Uri &uri, const DataSharePredicates &predicates,
231 const DataShareValuesBucket &value)
232 {
233 if (!IsValid()) {
234 NAPI_ERR_LOG("update fail, helper null");
235 return E_FAIL;
236 }
237 return sDataShareHelper_->Update(uri, predicates, value);
238 }
239
RegisterObserverExt(const Uri & uri,shared_ptr<DataShare::DataShareObserver> dataObserver,bool isDescendants)240 void UserFileClient::RegisterObserverExt(const Uri &uri,
241 shared_ptr<DataShare::DataShareObserver> dataObserver, bool isDescendants)
242 {
243 if (!IsValid()) {
244 NAPI_ERR_LOG("register observer fail, helper null");
245 return;
246 }
247 sDataShareHelper_->RegisterObserverExt(uri, std::move(dataObserver), isDescendants);
248 }
249
UnregisterObserverExt(const Uri & uri,std::shared_ptr<DataShare::DataShareObserver> dataObserver)250 void UserFileClient::UnregisterObserverExt(const Uri &uri, std::shared_ptr<DataShare::DataShareObserver> dataObserver)
251 {
252 if (!IsValid()) {
253 NAPI_ERR_LOG("unregister observer fail, helper null");
254 return;
255 }
256 sDataShareHelper_->UnregisterObserverExt(uri, std::move(dataObserver));
257 }
258
Clear()259 void UserFileClient::Clear()
260 {
261 sDataShareHelper_ = nullptr;
262 }
263 }
264 }
265