• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_stub.h"
17 
18 #include <cinttypes>
19 
20 #include "data_ability_observer_interface.h"
21 #include "datashare_itypes_utils.h"
22 #include "datashare_log.h"
23 #include "ipc_skeleton.h"
24 #include "ipc_types.h"
25 #include "ishared_result_set.h"
26 #include "datashare_operation_statement.h"
27 #include "unistd.h"
28 #include "string_ex.h"
29 
30 using namespace OHOS::DistributedShare::DataShare;
31 
32 namespace OHOS {
33 namespace DataShare {
34 constexpr int DEFAULT_NUMBER = -1;
35 constexpr int PERMISSION_ERROR_NUMBER = -2;
DataShareStub()36 DataShareStub::DataShareStub()
37 {
38     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_GET_FILE_TYPES)] = &DataShareStub::CmdGetFileTypes;
39     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_OPEN_FILE)] = &DataShareStub::CmdOpenFile;
40     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_OPEN_FILE_WITH_ERR_CODE)] =
41         &DataShareStub::CmdOpenFileWithErrCode;
42     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_OPEN_RAW_FILE)] = &DataShareStub::CmdOpenRawFile;
43     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_INSERT)] = &DataShareStub::CmdInsert;
44     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_UPDATE)] = &DataShareStub::CmdUpdate;
45     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_DELETE)] = &DataShareStub::CmdDelete;
46     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_QUERY)] = &DataShareStub::CmdQuery;
47     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_GET_TYPE)] = &DataShareStub::CmdGetType;
48     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_BATCH_INSERT)] = &DataShareStub::CmdBatchInsert;
49     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_REGISTER_OBSERVER)] =
50         &DataShareStub::CmdRegisterObserver;
51     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_UNREGISTER_OBSERVER)] =
52         &DataShareStub::CmdUnregisterObserver;
53     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_NOTIFY_CHANGE)] = &DataShareStub::CmdNotifyChange;
54     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_NORMALIZE_URI)] = &DataShareStub::CmdNormalizeUri;
55     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_DENORMALIZE_URI)] =
56         &DataShareStub::CmdDenormalizeUri;
57     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_EXECUTE_BATCH)] = &DataShareStub::CmdExecuteBatch;
58     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_INSERT_EXT)] = &DataShareStub::CmdInsertExt;
59     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_BATCH_UPDATE)] = &DataShareStub::CmdBatchUpdate;
60     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_INSERT_EX)] = &DataShareStub::CmdInsertEx;
61     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_UPDATE_EX)] = &DataShareStub::CmdUpdateEx;
62     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_DELETE_EX)] = &DataShareStub::CmdDeleteEx;
63     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_REGISTER_OBSERVEREXT_PROVIDER)] =
64         &DataShareStub::CmdRegisterObserverExtProvider;
65     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_UNREGISTER_OBSERVEREXT_PROVIDER)] =
66         &DataShareStub::CmdUnregisterObserverExtProvider;
67     stubFuncMap_[static_cast<uint32_t>(IDataShareInterfaceCode::CMD_NOTIFY_CHANGEEXT_PROVIDER)] =
68         &DataShareStub::CmdNotifyChangeExtProvider;
69 }
70 
~DataShareStub()71 DataShareStub::~DataShareStub()
72 {
73     stubFuncMap_.clear();
74 }
75 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)76 int DataShareStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
77     MessageOption& option)
78 {
79     std::u16string descriptor = DataShareStub::GetDescriptor();
80     std::u16string remoteDescriptor = data.ReadInterfaceToken();
81     if (descriptor != remoteDescriptor) {
82         LOG_INFO("local descriptor is not equal to remote, localDescriptor = %{public}s, remoteDescriptor = %{public}s",
83             Str16ToStr8(descriptor).c_str(), Str16ToStr8(remoteDescriptor).c_str());
84         return ERR_INVALID_STATE;
85     }
86 
87     const auto &itFunc = stubFuncMap_.find(code);
88     auto start = std::chrono::steady_clock::now();
89     int32_t ret = 0;
90     bool isCodeValid = false;
91     if (itFunc != stubFuncMap_.end()) {
92         isCodeValid = true;
93         ret = (this->*(itFunc->second))(data, reply);
94     } else if (code == static_cast<uint32_t>(IDataShareInterfaceCode::CMD_USER_DEFINE_FUNC)) {
95         isCodeValid = true;
96         ret = CmdUserDefineFunc(data, reply, option);
97     }
98     if (isCodeValid) {
99         auto finish = std::chrono::steady_clock::now();
100         auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(finish - start);
101         if (duration >= TIME_THRESHOLD) {
102             auto callingPid = IPCSkeleton::GetCallingPid();
103             int64_t milliseconds = duration.count();
104             LOG_ERROR("extension time over, code:%{public}u callingPid:%{public}d, cost:%{public}" PRIi64 "ms",
105                 code, callingPid, milliseconds);
106         }
107         return ret;
108     }
109     LOG_DEBUG("remote request unhandled: %{public}d", code);
110     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
111 }
112 
CmdGetFileTypes(MessageParcel & data,MessageParcel & reply)113 ErrCode DataShareStub::CmdGetFileTypes(MessageParcel &data, MessageParcel &reply)
114 {
115     Uri uri("");
116     std::string mimeTypeFilter;
117     if (!ITypesUtil::Unmarshal(data, uri, mimeTypeFilter)) {
118         LOG_ERROR("Unmarshalling value is nullptr");
119         return ERR_INVALID_VALUE;
120     }
121     if (mimeTypeFilter.empty()) {
122         LOG_ERROR("mimeTypeFilter is nullptr");
123         return ERR_INVALID_VALUE;
124     }
125     std::vector<std::string> types = GetFileTypes(uri, mimeTypeFilter);
126     if (!ITypesUtil::Marshal(reply, types)) {
127         LOG_ERROR("Marshal value is nullptr");
128         return ERR_INVALID_VALUE;
129     }
130     return E_OK;
131 }
132 
OpenFileInner(MessageParcel & data,MessageParcel & reply,int & fd)133 ErrCode DataShareStub::OpenFileInner(MessageParcel &data, MessageParcel &reply, int &fd)
134 {
135     Uri uri("");
136     std::string mode;
137     if (!ITypesUtil::Unmarshal(data, uri, mode)) {
138         LOG_ERROR("Unmarshalling value is nullptr");
139         return ERR_INVALID_VALUE;
140     }
141     if (mode.empty()) {
142         LOG_ERROR("mode is nullptr");
143         return ERR_INVALID_VALUE;
144     }
145     fd = OpenFile(uri, mode);
146     return E_OK;
147 }
148 
CmdOpenFile(MessageParcel & data,MessageParcel & reply)149 ErrCode DataShareStub::CmdOpenFile(MessageParcel &data, MessageParcel &reply)
150 {
151     int fd = -1;
152     int ret = OpenFileInner(data, reply, fd);
153     if (ret != E_OK) {
154         return ret;
155     }
156 
157     if (fd < 0) {
158         return ERR_INVALID_DATA;
159     }
160     if (!reply.WriteFileDescriptor(fd)) {
161         LOG_ERROR("fail to WriteFileDescriptor fd");
162         close(fd);
163         return ERR_INVALID_VALUE;
164     }
165     close(fd);
166     return E_OK;
167 }
168 
CmdOpenFileWithErrCode(MessageParcel & data,MessageParcel & reply)169 ErrCode DataShareStub::CmdOpenFileWithErrCode(MessageParcel &data, MessageParcel &reply)
170 {
171     int fd = -1;
172     int ret = OpenFileInner(data, reply, fd);
173     if (ret != E_OK) {
174         return ret;
175     }
176 
177     if (fd < 0) {
178         return fd;
179     }
180     if (!reply.WriteFileDescriptor(fd)) {
181         LOG_ERROR("fail to WriteFileDescriptor fd");
182         close(fd);
183         return ERR_INVALID_VALUE;
184     }
185     close(fd);
186     return E_OK;
187 }
188 
CmdOpenRawFile(MessageParcel & data,MessageParcel & reply)189 ErrCode DataShareStub::CmdOpenRawFile(MessageParcel &data, MessageParcel &reply)
190 {
191     Uri uri("");
192     std::string mode;
193     if (!ITypesUtil::Unmarshal(data, uri, mode)) {
194         LOG_ERROR("Unmarshalling value is nullptr");
195         return ERR_INVALID_VALUE;
196     }
197     int fd = OpenRawFile(uri, mode);
198     if (!ITypesUtil::Marshal(reply, fd)) {
199         LOG_ERROR("Marshal value is nullptr");
200         return ERR_INVALID_VALUE;
201     }
202     return E_OK;
203 }
204 
CmdInsert(MessageParcel & data,MessageParcel & reply)205 ErrCode DataShareStub::CmdInsert(MessageParcel &data, MessageParcel &reply)
206 {
207     Uri uri("");
208     DataShareValuesBucket value;
209     if (!ITypesUtil::Unmarshal(data, uri, value)) {
210         LOG_ERROR("Unmarshalling value is nullptr");
211         return ERR_INVALID_VALUE;
212     }
213     int index = Insert(uri, value);
214     if (index == DEFAULT_NUMBER) {
215         LOG_ERROR("Insert inner error");
216         return ERR_INVALID_VALUE;
217     } else if (index == PERMISSION_ERROR_NUMBER) {
218         LOG_ERROR("Insert permission error");
219         return ERR_PERMISSION_DENIED;
220     }
221     if (!reply.WriteInt32(index)) {
222         LOG_ERROR("fail to WriteInt32 index");
223         return ERR_INVALID_VALUE;
224     }
225     return E_OK;
226 }
227 
CmdUpdate(MessageParcel & data,MessageParcel & reply)228 ErrCode DataShareStub::CmdUpdate(MessageParcel &data, MessageParcel &reply)
229 {
230     Uri uri("");
231     DataSharePredicates predicates;
232     DataShareValuesBucket value;
233     if (!ITypesUtil::Unmarshal(data, uri, predicates, value)) {
234         LOG_ERROR("Unmarshalling predicates is nullptr");
235         return ERR_INVALID_VALUE;
236     }
237     int index = Update(uri, predicates, value);
238     if (index == DEFAULT_NUMBER) {
239         LOG_ERROR("Update inner error");
240         return ERR_INVALID_VALUE;
241     } else if (index == PERMISSION_ERROR_NUMBER) {
242         LOG_ERROR("Update permission error");
243         return ERR_PERMISSION_DENIED;
244     }
245     if (!reply.WriteInt32(index)) {
246         LOG_ERROR("fail to WriteInt32 index");
247         return ERR_INVALID_VALUE;
248     }
249     return E_OK;
250 }
251 
CmdBatchUpdate(OHOS::MessageParcel & data,OHOS::MessageParcel & reply)252 ErrCode DataShareStub::CmdBatchUpdate(OHOS::MessageParcel &data, OHOS::MessageParcel &reply)
253 {
254     UpdateOperations updateOperations;
255     if (!ITypesUtil::Unmarshal(data, updateOperations)) {
256         LOG_ERROR("Unmarshalling updateOperations is nullptr");
257         return ERR_INVALID_VALUE;
258     }
259     std::vector<BatchUpdateResult> results;
260     int ret = BatchUpdate(updateOperations, results);
261     if (ret == DEFAULT_NUMBER) {
262         LOG_ERROR("BatchUpdate inner error, ret is %{public}d.", ret);
263         return ERR_INVALID_VALUE;
264     } else if (ret == PERMISSION_ERROR_NUMBER) {
265         LOG_ERROR("BatchUpdate permission error");
266         return ERR_PERMISSION_DENIED;
267     }
268     if (!ITypesUtil::Marshal(reply, results)) {
269         LOG_ERROR("marshalling updateOperations is failed");
270         return ERR_INVALID_VALUE;
271     }
272     return E_OK;
273 }
274 
CmdDelete(MessageParcel & data,MessageParcel & reply)275 ErrCode DataShareStub::CmdDelete(MessageParcel &data, MessageParcel &reply)
276 {
277     Uri uri("");
278     DataSharePredicates predicates;
279     if (!ITypesUtil::Unmarshal(data, uri, predicates)) {
280         LOG_ERROR("Unmarshalling predicates is nullptr");
281         return ERR_INVALID_VALUE;
282     }
283     int index = Delete(uri, predicates);
284     if (index == DEFAULT_NUMBER) {
285         LOG_ERROR("Delete inner error");
286         return ERR_INVALID_VALUE;
287     } else if (index == PERMISSION_ERROR_NUMBER) {
288         LOG_ERROR("Delete permission error");
289         return ERR_PERMISSION_DENIED;
290     }
291     if (!reply.WriteInt32(index)) {
292         LOG_ERROR("fail to WriteInt32 index");
293         return ERR_INVALID_VALUE;
294     }
295     return E_OK;
296 }
297 
CmdInsertEx(MessageParcel & data,MessageParcel & reply)298 ErrCode DataShareStub::CmdInsertEx(MessageParcel &data, MessageParcel &reply)
299 {
300     Uri uri("");
301     DataShareValuesBucket value;
302     if (!ITypesUtil::Unmarshal(data, uri, value)) {
303         LOG_ERROR("Unmarshalling value is nullptr");
304         return E_UNMARSHAL_ERROR;
305     }
306 
307     auto [errCode, result] = InsertEx(uri, value);
308     if (errCode == DEFAULT_NUMBER) {
309         LOG_ERROR("Insert inner error");
310         return ERR_INVALID_VALUE;
311     } else if (errCode == PERMISSION_ERROR_NUMBER) {
312         LOG_ERROR("Insert permission error");
313         return ERR_PERMISSION_DENIED;
314     }
315 
316     if (!ITypesUtil::Marshal(reply, errCode, result)) {
317         LOG_ERROR("Marshal value is nullptr");
318         return E_MARSHAL_ERROR;
319     }
320     return E_OK;
321 }
322 
CmdUpdateEx(MessageParcel & data,MessageParcel & reply)323 ErrCode DataShareStub::CmdUpdateEx(MessageParcel &data, MessageParcel &reply)
324 {
325     Uri uri("");
326     DataSharePredicates predicates;
327     DataShareValuesBucket value;
328     if (!ITypesUtil::Unmarshal(data, uri, predicates, value)) {
329         LOG_ERROR("Unmarshalling predicates is nullptr");
330         return E_UNMARSHAL_ERROR;
331     }
332 
333     auto [errCode, result] = UpdateEx(uri, predicates, value);
334     if (errCode == DEFAULT_NUMBER) {
335         LOG_ERROR("Update inner error");
336         return ERR_INVALID_VALUE;
337     } else if (errCode == PERMISSION_ERROR_NUMBER) {
338         LOG_ERROR("Update permission error");
339         return ERR_PERMISSION_DENIED;
340     }
341 
342     if (!ITypesUtil::Marshal(reply, errCode, result)) {
343         LOG_ERROR("Marshal value is nullptr");
344         return E_MARSHAL_ERROR;
345     }
346     return E_OK;
347 }
348 
CmdDeleteEx(MessageParcel & data,MessageParcel & reply)349 ErrCode DataShareStub::CmdDeleteEx(MessageParcel &data, MessageParcel &reply)
350 {
351     Uri uri("");
352     DataSharePredicates predicates;
353     if (!ITypesUtil::Unmarshal(data, uri, predicates)) {
354         LOG_ERROR("Unmarshalling predicates is nullptr");
355         return E_UNMARSHAL_ERROR;
356     }
357     auto [errCode, result] = DeleteEx(uri, predicates);
358     if (errCode == DEFAULT_NUMBER) {
359         LOG_ERROR("Delete inner error");
360         return ERR_INVALID_VALUE;
361     } else if (errCode == PERMISSION_ERROR_NUMBER) {
362         LOG_ERROR("Delete permission error");
363         return ERR_PERMISSION_DENIED;
364     }
365     if (!ITypesUtil::Marshal(reply, errCode, result)) {
366         LOG_ERROR("Marshal value is nullptr");
367         return E_MARSHAL_ERROR;
368     }
369     return E_OK;
370 }
371 
CmdQuery(MessageParcel & data,MessageParcel & reply)372 ErrCode DataShareStub::CmdQuery(MessageParcel &data, MessageParcel &reply)
373 {
374     Uri uri("");
375     DataSharePredicates predicates;
376     std::vector<std::string> columns;
377     if (!ITypesUtil::Unmarshal(data, uri, columns)) {
378         LOG_ERROR("Unmarshalling uri and columns to data failed");
379         return ERR_INVALID_VALUE;
380     }
381     if (!ITypesUtil::UnmarshalPredicates(predicates, data)) {
382         LOG_ERROR("Unmarshalling predicates to shared-memory failed");
383         return ERR_INVALID_VALUE;
384     }
385     DatashareBusinessError businessError;
386     auto resultSet = Query(uri, predicates, columns, businessError);
387     auto result = ISharedResultSet::WriteToParcel(std::move(resultSet), reply);
388     reply.WriteInt32(businessError.GetCode());
389     reply.WriteString(businessError.GetMessage());
390     if (result == nullptr) {
391         LOG_ERROR("!resultSet->Marshalling(reply)");
392         return ERR_INVALID_VALUE;
393     }
394     return E_OK;
395 }
396 
CmdGetType(MessageParcel & data,MessageParcel & reply)397 ErrCode DataShareStub::CmdGetType(MessageParcel &data, MessageParcel &reply)
398 {
399     Uri uri("");
400     if (!ITypesUtil::Unmarshal(data, uri)) {
401         LOG_ERROR("Unmarshalling predicates is nullptr");
402         return ERR_INVALID_VALUE;
403     }
404     std::string type = GetType(uri);
405     if (!reply.WriteString(type)) {
406         LOG_ERROR("fail to WriteString type");
407         return ERR_INVALID_VALUE;
408     }
409     return E_OK;
410 }
411 
CmdUserDefineFunc(MessageParcel & data,MessageParcel & reply,MessageOption & option)412 ErrCode DataShareStub::CmdUserDefineFunc(MessageParcel &data, MessageParcel &reply, MessageOption &option)
413 {
414     UserDefineFunc(data, reply, option);
415     return E_OK;
416 }
417 
CmdBatchInsert(MessageParcel & data,MessageParcel & reply)418 ErrCode DataShareStub::CmdBatchInsert(MessageParcel &data, MessageParcel &reply)
419 {
420     Uri uri("");
421     std::vector<DataShareValuesBucket> values;
422     if (!ITypesUtil::Unmarshal(data, uri)) {
423         LOG_ERROR("Unmarshalling uri from data failed");
424         return ERR_INVALID_VALUE;
425     }
426     if (!ITypesUtil::UnmarshalValuesBucketVec(values, data)) {
427         LOG_ERROR("Unmarshalling DataShareValuesBucket from shared-memory failed");
428         return ERR_INVALID_VALUE;
429     }
430     int ret = BatchInsert(uri, values);
431     if (ret == DEFAULT_NUMBER) {
432         LOG_ERROR("BatchInsert inner error");
433         return ERR_INVALID_VALUE;
434     } else if (ret == PERMISSION_ERROR_NUMBER) {
435         LOG_ERROR("BatchInsert permission error");
436         return ERR_PERMISSION_DENIED;
437     }
438     if (!reply.WriteInt32(ret)) {
439         LOG_ERROR("fail to WriteInt32 ret");
440         return ERR_INVALID_VALUE;
441     }
442     return E_OK;
443 }
444 
CmdRegisterObserver(MessageParcel & data,MessageParcel & reply)445 ErrCode DataShareStub::CmdRegisterObserver(MessageParcel &data, MessageParcel &reply)
446 {
447     Uri uri("");
448     sptr<IRemoteObject> observer;
449     if (!ITypesUtil::Unmarshal(data, uri, observer)) {
450         LOG_ERROR("Unmarshalling predicates is nullptr");
451         return ERR_INVALID_VALUE;
452     }
453     auto obServer = iface_cast<AAFwk::IDataAbilityObserver>(observer);
454     if (obServer == nullptr) {
455         LOG_ERROR("obServer is nullptr");
456         return ERR_INVALID_VALUE;
457     }
458 
459     bool ret = RegisterObserver(uri, obServer);
460     if (!reply.WriteInt32(ret)) {
461         LOG_ERROR("fail to WriteInt32 ret");
462         return ERR_INVALID_VALUE;
463     }
464     return E_OK;
465 }
466 
CmdUnregisterObserver(MessageParcel & data,MessageParcel & reply)467 ErrCode DataShareStub::CmdUnregisterObserver(MessageParcel &data, MessageParcel &reply)
468 {
469     Uri uri("");
470     sptr<IRemoteObject> observer;
471     if (!ITypesUtil::Unmarshal(data, uri, observer)) {
472         LOG_ERROR("Unmarshalling predicates is nullptr");
473         return ERR_INVALID_VALUE;
474     }
475     auto obServer = iface_cast<AAFwk::IDataAbilityObserver>(observer);
476     if (obServer == nullptr) {
477         LOG_ERROR("obServer is nullptr");
478         return ERR_INVALID_VALUE;
479     }
480 
481     bool ret = UnregisterObserver(uri, obServer);
482     if (!reply.WriteInt32(ret)) {
483         LOG_ERROR("fail to WriteInt32 ret");
484         return ERR_INVALID_VALUE;
485     }
486     return E_OK;
487 }
488 
CmdNotifyChange(MessageParcel & data,MessageParcel & reply)489 ErrCode DataShareStub::CmdNotifyChange(MessageParcel &data, MessageParcel &reply)
490 {
491     Uri uri("");
492     if (!ITypesUtil::Unmarshal(data, uri)) {
493         LOG_ERROR("Unmarshalling predicates is nullptr");
494         return ERR_INVALID_VALUE;
495     }
496 
497     bool ret = NotifyChange(uri);
498     if (!reply.WriteInt32(ret)) {
499         LOG_ERROR("fail to WriteInt32 ret");
500         return ERR_INVALID_VALUE;
501     }
502     return E_OK;
503 }
504 
CmdRegisterObserverExtProvider(MessageParcel & data,MessageParcel & reply)505 ErrCode DataShareStub::CmdRegisterObserverExtProvider(MessageParcel &data, MessageParcel &reply)
506 {
507     Uri uri("");
508     sptr<IRemoteObject> observer;
509     bool isDescendants = false;
510     RegisterOption option;
511     if (!ITypesUtil::Unmarshal(data, uri, observer, isDescendants, option)) {
512         LOG_ERROR("Unmarshalling uri and observer failed");
513         return ERR_INVALID_VALUE;
514     }
515     auto obServer = iface_cast<AAFwk::IDataAbilityObserver>(observer);
516     if (obServer == nullptr) {
517         LOG_ERROR("obServer is nullptr");
518         return ERR_INVALID_VALUE;
519     }
520 
521     int ret = RegisterObserverExtProvider(uri, obServer, isDescendants, option);
522     if (!reply.WriteInt32(ret)) {
523         LOG_ERROR("fail to WriteInt32 ret");
524         return ERR_INVALID_VALUE;
525     }
526     return E_OK;
527 }
528 
CmdUnregisterObserverExtProvider(MessageParcel & data,MessageParcel & reply)529 ErrCode DataShareStub::CmdUnregisterObserverExtProvider(MessageParcel &data, MessageParcel &reply)
530 {
531     Uri uri("");
532     sptr<IRemoteObject> observer;
533     if (!ITypesUtil::Unmarshal(data, uri, observer)) {
534         LOG_ERROR("Unmarshalling uri and observer failed");
535         return ERR_INVALID_VALUE;
536     }
537     auto obServer = iface_cast<AAFwk::IDataAbilityObserver>(observer);
538     if (obServer == nullptr) {
539         LOG_ERROR("obServer is nullptr");
540         return ERR_INVALID_VALUE;
541     }
542 
543     int ret = UnregisterObserverExtProvider(uri, obServer);
544     if (!reply.WriteInt32(ret)) {
545         LOG_ERROR("fail to WriteInt32 ret");
546         return ERR_INVALID_VALUE;
547     }
548     return E_OK;
549 }
550 
CmdNotifyChangeExtProvider(MessageParcel & data,MessageParcel & reply)551 ErrCode DataShareStub::CmdNotifyChangeExtProvider(MessageParcel &data, MessageParcel &reply)
552 {
553     ChangeInfo changeInfo;
554     if (!ChangeInfo::Unmarshalling(changeInfo, data)) {
555         LOG_ERROR("Failed to unmarshall changeInfo.");
556         return ERR_INVALID_VALUE;
557     }
558 
559     int ret = NotifyChangeExtProvider(changeInfo);
560     if (!reply.WriteInt32(ret)) {
561         LOG_ERROR("fail to WriteInt32 ret");
562         return ERR_INVALID_VALUE;
563     }
564     return E_OK;
565 }
566 
CmdNormalizeUri(MessageParcel & data,MessageParcel & reply)567 ErrCode DataShareStub::CmdNormalizeUri(MessageParcel &data, MessageParcel &reply)
568 {
569     Uri uri("");
570     if (!ITypesUtil::Unmarshal(data, uri)) {
571         LOG_ERROR("Unmarshalling predicates is nullptr");
572         return ERR_INVALID_VALUE;
573     }
574     auto ret = NormalizeUri(uri);
575     if (!ITypesUtil::Marshal(reply, ret)) {
576         LOG_ERROR("Write to message parcel failed!");
577         return ERR_INVALID_VALUE;
578     }
579     return E_OK;
580 }
581 
CmdDenormalizeUri(MessageParcel & data,MessageParcel & reply)582 ErrCode DataShareStub::CmdDenormalizeUri(MessageParcel &data, MessageParcel &reply)
583 {
584     Uri uri("");
585     if (!ITypesUtil::Unmarshal(data, uri)) {
586         LOG_ERROR("Unmarshalling predicates is nullptr");
587         return ERR_INVALID_VALUE;
588     }
589 
590     auto ret = DenormalizeUri(uri);
591     if (!ITypesUtil::Marshal(reply, ret)) {
592         LOG_ERROR("Write to message parcel failed!");
593         return ERR_INVALID_VALUE;
594     }
595     return E_OK;
596 }
597 
CmdExecuteBatch(MessageParcel & data,MessageParcel & reply)598 ErrCode DataShareStub::CmdExecuteBatch(MessageParcel &data, MessageParcel &reply)
599 {
600     std::vector<OperationStatement> statements;
601     ExecResultSet result;
602     if (!ITypesUtil::Unmarshal(data, statements)) {
603         LOG_ERROR("Unmarshalling OperationStatement failed");
604         return ERR_INVALID_VALUE;
605     }
606     auto ret = ExecuteBatch(statements, result);
607     if (ret == DEFAULT_NUMBER) {
608         LOG_ERROR("ExecuteBatch error");
609         return ERR_INVALID_VALUE;
610     }
611     if (!ITypesUtil::Marshal(reply, result)) {
612         LOG_ERROR("fail to write result");
613         return ERR_INVALID_VALUE;
614     }
615     return E_OK;
616 }
617 
CmdInsertExt(MessageParcel & data,MessageParcel & reply)618 ErrCode DataShareStub::CmdInsertExt(MessageParcel &data, MessageParcel &reply)
619 {
620     Uri uri("");
621     DataShareValuesBucket value;
622     if (!ITypesUtil::Unmarshal(data, uri, value)) {
623         LOG_ERROR("Unmarshalling value is nullptr");
624         return ERR_INVALID_VALUE;
625     }
626     std::string result;
627     int index = InsertExt(uri, value, result);
628     if (index == DEFAULT_NUMBER) {
629         LOG_ERROR("Insert inner error");
630         return ERR_INVALID_VALUE;
631     }
632     if (!ITypesUtil::Marshal(reply, index, result)) {
633         LOG_ERROR("fail to write result");
634         return ERR_INVALID_VALUE;
635     }
636     return E_OK;
637 }
638 
ExecuteBatch(const std::vector<OperationStatement> & statements,ExecResultSet & result)639 int DataShareStub::ExecuteBatch(const std::vector<OperationStatement> &statements, ExecResultSet &result)
640 {
641     return 0;
642 }
643 
InsertExt(const Uri & uri,const DataShareValuesBucket & value,std::string & result)644 int DataShareStub::InsertExt(const Uri &uri, const DataShareValuesBucket &value, std::string &result)
645 {
646     return 0;
647 }
648 
BatchUpdate(const UpdateOperations & operations,std::vector<BatchUpdateResult> & results)649 int DataShareStub::BatchUpdate(const UpdateOperations &operations, std::vector<BatchUpdateResult> &results)
650 {
651     return 0;
652 }
InsertEx(const Uri & uri,const DataShareValuesBucket & value)653 std::pair<int32_t, int32_t> DataShareStub::InsertEx(const Uri &uri, const DataShareValuesBucket &value)
654 {
655     return std::make_pair(0, 0);
656 }
UpdateEx(const Uri & uri,const DataSharePredicates & predicates,const DataShareValuesBucket & value)657 std::pair<int32_t, int32_t> DataShareStub::UpdateEx(const Uri &uri, const DataSharePredicates &predicates,
658     const DataShareValuesBucket &value)
659 {
660     return std::make_pair(0, 0);
661 }
DeleteEx(const Uri & uri,const DataSharePredicates & predicates)662 std::pair<int32_t, int32_t> DataShareStub::DeleteEx(const Uri &uri, const DataSharePredicates &predicates)
663 {
664     return std::make_pair(0, 0);
665 }
666 
UserDefineFunc(MessageParcel & data,MessageParcel & reply,MessageOption & option)667 int32_t DataShareStub::UserDefineFunc(
668     MessageParcel &data, MessageParcel &reply, MessageOption &option)
669 {
670     LOG_ERROR("UserDefineFunc excuted.");
671     return 0;
672 }
673 
RegisterObserverExtProvider(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver,bool isDescendants,RegisterOption option)674 int DataShareStub::RegisterObserverExtProvider(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver,
675     bool isDescendants, RegisterOption option)
676 {
677     return 0;
678 }
679 
UnregisterObserverExtProvider(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)680 int DataShareStub::UnregisterObserverExtProvider(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
681 {
682     return 0;
683 }
NotifyChangeExtProvider(const ChangeInfo & changeInfo)684 int DataShareStub::NotifyChangeExtProvider(const ChangeInfo &changeInfo)
685 {
686     return 0;
687 }
688 } // namespace DataShare
689 } // namespace OHOS