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_proxy.h"
17
18 #include <string_ex.h>
19
20 #include "datashare_result_set.h"
21 #include "data_ability_observer_interface.h"
22 #include "datashare_log.h"
23 #include "ipc_types.h"
24 #include "ishared_result_set.h"
25 #include "pac_map.h"
26 #include "itypes_utils.h"
27
28 namespace OHOS {
29 namespace DataShare {
30 constexpr int32_t PERMISSION_ERR = 1;
31 constexpr int PERMISSION_ERR_CODE = -2;
GetFileTypes(const Uri & uri,const std::string & mimeTypeFilter)32 std::vector<std::string> DataShareProxy::GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter)
33 {
34 std::vector<std::string> types;
35
36 MessageParcel data;
37 MessageParcel reply;
38 MessageOption option;
39
40 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
41 LOG_ERROR("WriteInterfaceToken failed");
42 return types;
43 }
44
45 if (!data.WriteParcelable(&uri)) {
46 LOG_ERROR("fail to WriteParcelable uri");
47 return types;
48 }
49
50 if (!data.WriteString(mimeTypeFilter)) {
51 LOG_ERROR("fail to WriteString mimeTypeFilter");
52 return types;
53 }
54
55 int32_t err = Remote()->SendRequest(CMD_GET_FILE_TYPES, data, reply, option);
56 if (err != DATA_SHARE_NO_ERROR) {
57 LOG_ERROR("GetFileTypes fail to SendRequest. err: %{public}d", err);
58 }
59
60 if (!reply.ReadStringVector(&types)) {
61 LOG_ERROR("fail to ReadStringVector types");
62 }
63
64 return types;
65 }
66
OpenFile(const Uri & uri,const std::string & mode)67 int DataShareProxy::OpenFile(const Uri &uri, const std::string &mode)
68 {
69 int fd = -1;
70 MessageParcel data;
71 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
72 LOG_ERROR("WriteInterfaceToken failed");
73 return fd;
74 }
75
76 if (!data.WriteParcelable(&uri)) {
77 LOG_ERROR("fail to WriteParcelable uri");
78 return fd;
79 }
80
81 if (!data.WriteString(mode)) {
82 LOG_ERROR("fail to WriteString mode");
83 return fd;
84 }
85
86 MessageParcel reply;
87 MessageOption option;
88 int32_t err = Remote()->SendRequest(CMD_OPEN_FILE, data, reply, option);
89 if (err != DATA_SHARE_NO_ERROR) {
90 LOG_ERROR("OpenFile fail to SendRequest. err: %{public}d", err);
91 return fd;
92 }
93
94 fd = reply.ReadFileDescriptor();
95 if (fd == -1) {
96 LOG_ERROR("fail to ReadFileDescriptor fd");
97 return fd;
98 }
99
100 return fd;
101 }
102
OpenRawFile(const Uri & uri,const std::string & mode)103 int DataShareProxy::OpenRawFile(const Uri &uri, const std::string &mode)
104 {
105 int fd = -1;
106 MessageParcel data;
107 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
108 LOG_ERROR("WriteInterfaceToken failed");
109 return fd;
110 }
111
112 if (!data.WriteParcelable(&uri)) {
113 LOG_ERROR("fail to WriteParcelable uri");
114 return fd;
115 }
116
117 if (!data.WriteString(mode)) {
118 LOG_ERROR("fail to WriteString mode");
119 return fd;
120 }
121
122 MessageParcel reply;
123 MessageOption option;
124 int32_t err = Remote()->SendRequest(CMD_OPEN_RAW_FILE, data, reply, option);
125 if (err != DATA_SHARE_NO_ERROR) {
126 LOG_ERROR("OpenRawFile fail to SendRequest. err: %{public}d", err);
127 return fd;
128 }
129
130 if (!reply.ReadInt32(fd)) {
131 LOG_ERROR("fail to ReadInt32 fd");
132 return fd;
133 }
134
135 return fd;
136 }
137
Insert(const Uri & uri,const DataShareValuesBucket & value)138 int DataShareProxy::Insert(const Uri &uri, const DataShareValuesBucket &value)
139 {
140 int index = -1;
141 MessageParcel data;
142 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
143 LOG_ERROR("WriteInterfaceToken failed");
144 return index;
145 }
146
147 if (!data.WriteParcelable(&uri)) {
148 LOG_ERROR("fail to WriteParcelable uri");
149 return index;
150 }
151
152 if (!ITypesUtils::Marshalling(value, data)) {
153 LOG_ERROR("fail to WriteParcelable value");
154 return index;
155 }
156
157 MessageParcel reply;
158 MessageOption option;
159 int32_t err = Remote()->SendRequest(CMD_INSERT, data, reply, option);
160 if (err != DATA_SHARE_NO_ERROR) {
161 LOG_ERROR("Insert fail to SendRequest. err: %{public}d", err);
162 return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : index;
163 }
164
165 if (!reply.ReadInt32(index)) {
166 LOG_ERROR("fail to ReadInt32 index");
167 return index;
168 }
169
170 return index;
171 }
172
Update(const Uri & uri,const DataSharePredicates & predicates,const DataShareValuesBucket & value)173 int DataShareProxy::Update(const Uri &uri, const DataSharePredicates &predicates,
174 const DataShareValuesBucket &value)
175 {
176 int index = -1;
177 MessageParcel data;
178 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
179 LOG_ERROR("WriteInterfaceToken failed");
180 return index;
181 }
182
183 if (!data.WriteParcelable(&uri)) {
184 LOG_ERROR("fail to WriteParcelable uri");
185 return index;
186 }
187
188 if (!ITypesUtils::Marshalling(predicates, data)) {
189 LOG_ERROR("fail to Marshalling predicates");
190 return index;
191 }
192
193 if (!ITypesUtils::Marshalling(value, data)) {
194 LOG_ERROR("fail to Marshalling value");
195 return index;
196 }
197
198 MessageParcel reply;
199 MessageOption option;
200 int32_t err = Remote()->SendRequest(CMD_UPDATE, data, reply, option);
201 if (err != DATA_SHARE_NO_ERROR) {
202 LOG_ERROR("Update fail to SendRequest. err: %{public}d", err);
203 return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : index;
204 }
205
206 if (!reply.ReadInt32(index)) {
207 LOG_ERROR("fail to ReadInt32 index");
208 return index;
209 }
210
211 return index;
212 }
213
Delete(const Uri & uri,const DataSharePredicates & predicates)214 int DataShareProxy::Delete(const Uri &uri, const DataSharePredicates &predicates)
215 {
216 int index = -1;
217 MessageParcel data;
218 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
219 LOG_ERROR("WriteInterfaceToken failed");
220 return index;
221 }
222
223 if (!data.WriteParcelable(&uri)) {
224 LOG_ERROR("fail to WriteParcelable uri");
225 return index;
226 }
227
228 if (!ITypesUtils::Marshalling(predicates, data)) {
229 LOG_ERROR("fail to Marshalling predicates");
230 return index;
231 }
232
233 MessageParcel reply;
234 MessageOption option;
235 int32_t err = Remote()->SendRequest(CMD_DELETE, data, reply, option);
236 if (err != DATA_SHARE_NO_ERROR) {
237 LOG_ERROR("Delete fail to SendRequest. err: %{public}d", err);
238 return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : index;
239 }
240
241 if (!reply.ReadInt32(index)) {
242 LOG_ERROR("fail to ReadInt32 index");
243 return index;
244 }
245
246 return index;
247 }
248
Query(const Uri & uri,const DataSharePredicates & predicates,std::vector<std::string> & columns)249 std::shared_ptr<DataShareResultSet> DataShareProxy::Query(const Uri &uri,
250 const DataSharePredicates &predicates, std::vector<std::string> &columns)
251 {
252 MessageParcel data;
253 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
254 LOG_ERROR("WriteInterfaceToken failed");
255 return nullptr;
256 }
257
258 if (!data.WriteParcelable(&uri)) {
259 LOG_ERROR("fail to WriteParcelable uri");
260 return nullptr;
261 }
262
263 if (!ITypesUtils::Marshalling(predicates, data)) {
264 LOG_ERROR("fail to Marshalling predicates");
265 return nullptr;
266 }
267
268 if (!data.WriteStringVector(columns)) {
269 LOG_ERROR("fail to WriteStringVector columns");
270 return nullptr;
271 }
272
273 MessageParcel reply;
274 MessageOption option;
275 int32_t err = Remote()->SendRequest(CMD_QUERY, data, reply, option);
276 if (err != DATA_SHARE_NO_ERROR) {
277 LOG_ERROR("Query fail to SendRequest. err: %{public}d", err);
278 return nullptr;
279 }
280 return ISharedResultSet::ReadFromParcel(reply);
281 }
282
GetType(const Uri & uri)283 std::string DataShareProxy::GetType(const Uri &uri)
284 {
285 LOG_INFO("begin.");
286 std::string type;
287 MessageParcel data;
288 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
289 LOG_ERROR("WriteInterfaceToken failed");
290 return type;
291 }
292 if (!data.WriteParcelable(&uri)) {
293 LOG_ERROR("fail to WriteParcelable uri");
294 return type;
295 }
296
297 MessageParcel reply;
298 MessageOption option;
299 int32_t err = Remote()->SendRequest(CMD_GET_TYPE, data, reply, option);
300 if (err != DATA_SHARE_NO_ERROR) {
301 LOG_ERROR("GetFileTypes fail to SendRequest. err: %{public}d", err);
302 return type;
303 }
304
305 type = reply.ReadString();
306 if (type.empty()) {
307 LOG_ERROR("fail to ReadString type");
308 return type;
309 }
310
311 LOG_INFO("end successfully.");
312 return type;
313 }
314
BatchInsert(const Uri & uri,const std::vector<DataShareValuesBucket> & values)315 int DataShareProxy::BatchInsert(const Uri &uri, const std::vector<DataShareValuesBucket> &values)
316 {
317 LOG_INFO("begin.");
318 int ret = -1;
319 MessageParcel data;
320 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
321 LOG_ERROR("WriteInterfaceToken failed");
322 return ret;
323 }
324
325 if (!data.WriteParcelable(&uri)) {
326 LOG_ERROR("fail to WriteParcelable uri");
327 return ret;
328 }
329
330 int count = (int)values.size();
331 if (!data.WriteInt32(count)) {
332 LOG_ERROR("fail to WriteInt32 ret");
333 return ret;
334 }
335
336 for (int i = 0; i < count; i++) {
337 if (!ITypesUtils::Marshalling(values[i], data)) {
338 LOG_ERROR("fail to WriteParcelable ret, index = %{public}d", i);
339 return ret;
340 }
341 }
342
343 MessageParcel reply;
344 MessageOption option;
345 int32_t err = Remote()->SendRequest(CMD_BATCH_INSERT, data, reply, option);
346 if (err != DATA_SHARE_NO_ERROR) {
347 LOG_ERROR("fail to SendRequest. err: %{public}d", err);
348 return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : ret;
349 }
350
351 if (!reply.ReadInt32(ret)) {
352 LOG_ERROR("fail to ReadInt32 index");
353 return ret;
354 }
355
356 LOG_INFO("end successfully.");
357 return ret;
358 }
359
RegisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)360 bool DataShareProxy::RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
361 {
362 LOG_INFO("begin.");
363 MessageParcel data;
364 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
365 LOG_ERROR("WriteInterfaceToken failed");
366 return false;
367 }
368
369 if (!data.WriteParcelable(&uri)) {
370 LOG_ERROR("failed to WriteParcelable uri ");
371 return false;
372 }
373
374 if (!data.WriteRemoteObject(dataObserver->AsObject())) {
375 LOG_ERROR("failed to WriteParcelable dataObserver ");
376 return false;
377 }
378
379 MessageParcel reply;
380 MessageOption option;
381 int32_t result = Remote()->SendRequest(CMD_REGISTER_OBSERVER, data, reply, option);
382 if (result == ERR_NONE) {
383 LOG_INFO("SendRequest ok, retval is %{public}d", reply.ReadInt32());
384 } else {
385 LOG_ERROR("SendRequest error, result=%{public}d", result);
386 return false;
387 }
388 LOG_INFO("end.");
389 return true;
390 }
391
UnregisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)392 bool DataShareProxy::UnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
393 {
394 LOG_INFO("begin.");
395 MessageParcel data;
396 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
397 LOG_ERROR("WriteInterfaceToken failed");
398 return false;
399 }
400
401 if (!data.WriteParcelable(&uri)) {
402 LOG_ERROR("failed to WriteParcelable uri ");
403 return false;
404 }
405
406 if (!data.WriteRemoteObject(dataObserver->AsObject())) {
407 LOG_ERROR("failed to WriteParcelable dataObserver ");
408 return false;
409 }
410
411 MessageParcel reply;
412 MessageOption option;
413 int32_t result = Remote()->SendRequest(CMD_UNREGISTER_OBSERVER, data, reply, option);
414 if (result == ERR_NONE) {
415 LOG_INFO("SendRequest ok, retval is %{public}d", reply.ReadInt32());
416 } else {
417 LOG_ERROR("SendRequest error, result=%{public}d", result);
418 return false;
419 }
420 LOG_INFO("end successfully.");
421 return true;
422 }
423
NotifyChange(const Uri & uri)424 bool DataShareProxy::NotifyChange(const Uri &uri)
425 {
426 LOG_INFO("begin.");
427 MessageParcel data;
428 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
429 LOG_ERROR("WriteInterfaceToken failed");
430 return false;
431 }
432
433 if (!data.WriteParcelable(&uri)) {
434 LOG_ERROR("failed to WriteParcelable uri ");
435 return false;
436 }
437
438 MessageParcel reply;
439 MessageOption option;
440 int32_t result = Remote()->SendRequest(CMD_NOTIFY_CHANGE, data, reply, option);
441 if (result == ERR_NONE) {
442 LOG_INFO("SendRequest ok, retval is %{public}d", reply.ReadInt32());
443 } else {
444 LOG_ERROR("SendRequest error, result=%{public}d", result);
445 return false;
446 }
447 LOG_INFO("end successfully.");
448 return true;
449 }
450
NormalizeUri(const Uri & uri)451 Uri DataShareProxy::NormalizeUri(const Uri &uri)
452 {
453 LOG_INFO("begin.");
454 Uri urivalue("");
455 MessageParcel data;
456 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
457 LOG_ERROR("WriteInterfaceToken failed");
458 return urivalue;
459 }
460
461 if (!data.WriteParcelable(&uri)) {
462 LOG_ERROR("fail to WriteParcelable uri");
463 return urivalue;
464 }
465
466 MessageParcel reply;
467 MessageOption option;
468 int32_t err = Remote()->SendRequest(CMD_NORMALIZE_URI, data, reply, option);
469 if (err != DATA_SHARE_NO_ERROR) {
470 LOG_ERROR("NormalizeUri fail to SendRequest. err: %{public}d", err);
471 return urivalue;
472 }
473
474 std::unique_ptr<Uri> info(reply.ReadParcelable<Uri>());
475 if (!info) {
476 LOG_ERROR("ReadParcelable value is nullptr.");
477 return urivalue;
478 }
479 LOG_INFO("end successfully.");
480 return *info;
481 }
482
DenormalizeUri(const Uri & uri)483 Uri DataShareProxy::DenormalizeUri(const Uri &uri)
484 {
485 LOG_INFO("begin.");
486 Uri urivalue("");
487 MessageParcel data;
488 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
489 LOG_ERROR("WriteInterfaceToken failed");
490 return urivalue;
491 }
492
493 if (!data.WriteParcelable(&uri)) {
494 LOG_ERROR("fail to WriteParcelable uri");
495 return urivalue;
496 }
497
498 MessageParcel reply;
499 MessageOption option;
500 int32_t err = Remote()->SendRequest(CMD_DENORMALIZE_URI, data, reply, option);
501 if (err != DATA_SHARE_NO_ERROR) {
502 LOG_ERROR("DenormalizeUri fail to SendRequest. err: %{public}d", err);
503 return urivalue;
504 }
505
506 std::unique_ptr<Uri> info(reply.ReadParcelable<Uri>());
507 if (!info) {
508 LOG_ERROR("ReadParcelable value is nullptr.");
509 return urivalue;
510 }
511 LOG_INFO("end successfully.");
512 return *info;
513 }
514 } // namespace DataShare
515 } // namespace OHOS