• 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 "data_ability_observer_interface.h"
19 #include "data_ability_operation.h"
20 #include "data_ability_predicates.h"
21 #include "data_ability_result.h"
22 #include "hilog_wrapper.h"
23 #include "ipc_types.h"
24 #include "ishared_result_set.h"
25 #include "values_bucket.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
DataShareStub()29 DataShareStub::DataShareStub()
30 {
31     stubFuncMap_[CMD_GET_FILE_TYPES] = &DataShareStub::CmdGetFileTypes;
32     stubFuncMap_[CMD_OPEN_FILE] = &DataShareStub::CmdOpenFile;
33     stubFuncMap_[CMD_OPEN_RAW_FILE] = &DataShareStub::CmdOpenRawFile;
34     stubFuncMap_[CMD_INSERT] = &DataShareStub::CmdInsert;
35     stubFuncMap_[CMD_UPDATE] = &DataShareStub::CmdUpdate;
36     stubFuncMap_[CMD_DELETE] = &DataShareStub::CmdDelete;
37     stubFuncMap_[CMD_QUERY] = &DataShareStub::CmdQuery;
38     stubFuncMap_[CMD_GET_TYPE] = &DataShareStub::CmdGetType;
39     stubFuncMap_[CMD_BATCH_INSERT] = &DataShareStub::CmdBatchInsert;
40     stubFuncMap_[CMD_REGISTER_OBSERVER] = &DataShareStub::CmdRegisterObserver;
41     stubFuncMap_[CMD_UNREGISTER_OBSERVER] = &DataShareStub::CmdUnregisterObserver;
42     stubFuncMap_[CMD_NOTIFY_CHANGE] = &DataShareStub::CmdNotifyChange;
43     stubFuncMap_[CMD_NORMALIZE_URI] = &DataShareStub::CmdNormalizeUri;
44     stubFuncMap_[CMD_DENORMALIZE_URI] = &DataShareStub::CmdDenormalizeUri;
45     stubFuncMap_[CMD_EXECUTE_BATCH] = &DataShareStub::CmdExecuteBatch;
46 }
47 
~DataShareStub()48 DataShareStub::~DataShareStub()
49 {
50     stubFuncMap_.clear();
51 }
52 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)53 int DataShareStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
54     MessageOption& option)
55 {
56     HILOG_INFO("%{public}s Received stub message: %{public}d", __func__, code);
57     std::u16string descriptor = DataShareStub::GetDescriptor();
58     std::u16string remoteDescriptor = data.ReadInterfaceToken();
59     if (descriptor != remoteDescriptor) {
60         HILOG_INFO("local descriptor is not equal to remote");
61         return ERR_INVALID_STATE;
62     }
63 
64     const auto &itFunc = stubFuncMap_.find(code);
65     if (itFunc != stubFuncMap_.end()) {
66         return (this->*(itFunc->second))(data, reply);
67     }
68 
69     HILOG_INFO("%{public}s remote request unhandled: %{public}d", __func__, code);
70     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
71 }
72 
CmdGetFileTypes(MessageParcel & data,MessageParcel & reply)73 ErrCode DataShareStub::CmdGetFileTypes(MessageParcel &data, MessageParcel &reply)
74 {
75     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
76     if (uri == nullptr) {
77         HILOG_ERROR("DataShareStub uri is nullptr");
78         return ERR_INVALID_VALUE;
79     }
80     std::string mimeTypeFilter = data.ReadString();
81     if (mimeTypeFilter.empty()) {
82         HILOG_ERROR("DataShareStub mimeTypeFilter is nullptr");
83         return ERR_INVALID_VALUE;
84     }
85     std::vector<std::string> types = GetFileTypes(*uri, mimeTypeFilter);
86     if (!reply.WriteStringVector(types)) {
87         HILOG_ERROR("fail to WriteStringVector types");
88         return ERR_INVALID_VALUE;
89     }
90     return NO_ERROR;
91 }
92 
CmdOpenFile(MessageParcel & data,MessageParcel & reply)93 ErrCode DataShareStub::CmdOpenFile(MessageParcel &data, MessageParcel &reply)
94 {
95     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
96     if (uri == nullptr) {
97         HILOG_ERROR("DataShareStub uri is nullptr");
98         return ERR_INVALID_VALUE;
99     }
100     std::string mode = data.ReadString();
101     if (mode.empty()) {
102         HILOG_ERROR("DataShareStub mode is nullptr");
103         return ERR_INVALID_VALUE;
104     }
105     int fd = OpenFile(*uri, mode);
106     if (fd < 0) {
107         HILOG_ERROR("OpenFile fail, fd is %{pubilc}d", fd);
108         return ERR_INVALID_VALUE;
109     }
110     if (!reply.WriteFileDescriptor(fd)) {
111         HILOG_ERROR("fail to WriteFileDescriptor fd");
112         return ERR_INVALID_VALUE;
113     }
114     return NO_ERROR;
115 }
116 
CmdOpenRawFile(MessageParcel & data,MessageParcel & reply)117 ErrCode DataShareStub::CmdOpenRawFile(MessageParcel &data, MessageParcel &reply)
118 {
119     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
120     if (uri == nullptr) {
121         HILOG_ERROR("DataShareStub uri is nullptr");
122         return ERR_INVALID_VALUE;
123     }
124     std::string mode = data.ReadString();
125     if (mode.empty()) {
126         HILOG_ERROR("DataShareStub mode is nullptr");
127         return ERR_INVALID_VALUE;
128     }
129     int fd = OpenRawFile(*uri, mode);
130     if (!reply.WriteInt32(fd)) {
131         HILOG_ERROR("fail to WriteInt32 fd");
132         return ERR_INVALID_VALUE;
133     }
134     return NO_ERROR;
135 }
136 
CmdInsert(MessageParcel & data,MessageParcel & reply)137 ErrCode DataShareStub::CmdInsert(MessageParcel &data, MessageParcel &reply)
138 {
139     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
140     if (uri == nullptr) {
141         HILOG_ERROR("DataShareStub uri is nullptr");
142         return ERR_INVALID_VALUE;
143     }
144     std::shared_ptr<NativeRdb::ValuesBucket> value(data.ReadParcelable<NativeRdb::ValuesBucket>());
145     if (value == nullptr) {
146         HILOG_ERROR("ReadParcelable value is nullptr");
147         return ERR_INVALID_VALUE;
148     }
149     int index = Insert(*uri, *value);
150     if (!reply.WriteInt32(index)) {
151         HILOG_ERROR("fail to WriteInt32 index");
152         return ERR_INVALID_VALUE;
153     }
154     HILOG_INFO("DataShareStub::CmdInsertInner end");
155     return NO_ERROR;
156 }
157 
CmdUpdate(MessageParcel & data,MessageParcel & reply)158 ErrCode DataShareStub::CmdUpdate(MessageParcel &data, MessageParcel &reply)
159 {
160     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
161     if (uri == nullptr) {
162         HILOG_ERROR("DataShareStub uri is nullptr");
163         return ERR_INVALID_VALUE;
164     }
165     std::shared_ptr<NativeRdb::ValuesBucket> value(data.ReadParcelable<NativeRdb::ValuesBucket>());
166     if (value == nullptr) {
167         HILOG_ERROR("ReadParcelable value is nullptr");
168         return ERR_INVALID_VALUE;
169     }
170     std::shared_ptr<NativeRdb::DataAbilityPredicates> predicates(
171         data.ReadParcelable<NativeRdb::DataAbilityPredicates>());
172     if (predicates == nullptr) {
173         HILOG_ERROR("ReadParcelable predicates is nullptr");
174         return ERR_INVALID_VALUE;
175     }
176     int index = Update(*uri, *value, *predicates);
177     if (!reply.WriteInt32(index)) {
178         HILOG_ERROR("fail to WriteInt32 index");
179         return ERR_INVALID_VALUE;
180     }
181     return NO_ERROR;
182 }
183 
CmdDelete(MessageParcel & data,MessageParcel & reply)184 ErrCode DataShareStub::CmdDelete(MessageParcel &data, MessageParcel &reply)
185 {
186     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
187     if (uri == nullptr) {
188         HILOG_ERROR("DataShareStub uri is nullptr");
189         return ERR_INVALID_VALUE;
190     }
191     std::shared_ptr<NativeRdb::DataAbilityPredicates> predicates(
192         data.ReadParcelable<NativeRdb::DataAbilityPredicates>());
193     if (predicates == nullptr) {
194         HILOG_ERROR("ReadParcelable predicates is nullptr");
195         return ERR_INVALID_VALUE;
196     }
197     int index = Delete(*uri, *predicates);
198     if (!reply.WriteInt32(index)) {
199         HILOG_ERROR("fail to WriteInt32 index");
200         return ERR_INVALID_VALUE;
201     }
202     return NO_ERROR;
203 }
204 
CmdQuery(MessageParcel & data,MessageParcel & reply)205 ErrCode DataShareStub::CmdQuery(MessageParcel &data, MessageParcel &reply)
206 {
207     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
208     if (uri == nullptr) {
209         HILOG_ERROR("DataShareStub uri is nullptr");
210         return ERR_INVALID_VALUE;
211     }
212     std::vector<std::string> columns;
213     if (!data.ReadStringVector(&columns)) {
214         HILOG_ERROR("fail to ReadStringVector columns");
215         return ERR_INVALID_VALUE;
216     }
217     std::shared_ptr<NativeRdb::DataAbilityPredicates> predicates(
218         data.ReadParcelable<NativeRdb::DataAbilityPredicates>());
219     if (predicates == nullptr) {
220         HILOG_ERROR("ReadParcelable predicates is nullptr");
221         return ERR_INVALID_VALUE;
222     }
223     auto resultSet = Query(*uri, columns, *predicates);
224     if (resultSet == nullptr) {
225         HILOG_ERROR("fail to WriteParcelable resultSet");
226         return ERR_INVALID_VALUE;
227     }
228     auto result = NativeRdb::ISharedResultSet::WriteToParcel(std::move(resultSet), reply);
229     if (result == nullptr) {
230         HILOG_ERROR("!resultSet->Marshalling(reply)");
231         return ERR_INVALID_VALUE;
232     }
233     HILOG_INFO("DataShareStub::CmdQueryInner end");
234     return NO_ERROR;
235 }
236 
CmdGetType(MessageParcel & data,MessageParcel & reply)237 ErrCode DataShareStub::CmdGetType(MessageParcel &data, MessageParcel &reply)
238 {
239     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
240     if (uri == nullptr) {
241         HILOG_ERROR("DataShareStub uri is nullptr");
242         return ERR_INVALID_VALUE;
243     }
244     std::string type = GetType(*uri);
245     if (!reply.WriteString(type)) {
246         HILOG_ERROR("fail to WriteString type");
247         return ERR_INVALID_VALUE;
248     }
249     return NO_ERROR;
250 }
251 
CmdBatchInsert(MessageParcel & data,MessageParcel & reply)252 ErrCode DataShareStub::CmdBatchInsert(MessageParcel &data, MessageParcel &reply)
253 {
254     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
255     if (uri == nullptr) {
256         HILOG_ERROR("DataShareStub uri is nullptr");
257         return ERR_INVALID_VALUE;
258     }
259 
260     int count = 0;
261     if (!data.ReadInt32(count)) {
262         HILOG_ERROR("fail to ReadInt32 index");
263         return ERR_INVALID_VALUE;
264     }
265 
266     std::vector<NativeRdb::ValuesBucket> values;
267     for (int i = 0; i < count; i++) {
268         std::unique_ptr<NativeRdb::ValuesBucket> value(data.ReadParcelable<NativeRdb::ValuesBucket>());
269         if (value == nullptr) {
270             HILOG_ERROR("DataShareStub value is nullptr, index = %{public}d", i);
271             return ERR_INVALID_VALUE;
272         }
273         values.emplace_back(*value);
274     }
275 
276     int ret = BatchInsert(*uri, values);
277     if (!reply.WriteInt32(ret)) {
278         HILOG_ERROR("fail to WriteInt32 ret");
279         return ERR_INVALID_VALUE;
280     }
281     return NO_ERROR;
282 }
283 
284 
CmdRegisterObserver(MessageParcel & data,MessageParcel & reply)285 ErrCode DataShareStub::CmdRegisterObserver(MessageParcel &data, MessageParcel &reply)
286 {
287     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
288     if (uri == nullptr) {
289         HILOG_ERROR("DataShareStub uri is nullptr");
290         return ERR_INVALID_VALUE;
291     }
292     auto remoteObject = data.ReadRemoteObject();
293     if (remoteObject == nullptr) {
294         HILOG_ERROR("remoteObject is nullptr");
295         return ERR_INVALID_VALUE;
296     }
297     auto obServer = iface_cast<AAFwk::IDataAbilityObserver>(remoteObject);
298     if (obServer == nullptr) {
299         HILOG_ERROR("the obServer creator is not exists");
300         return ERR_INVALID_VALUE;
301     }
302 
303     bool ret = RegisterObserver(*uri, obServer);
304     if (!reply.WriteInt32(ret)) {
305         HILOG_ERROR("fail to WriteInt32 ret");
306         return ERR_INVALID_VALUE;
307     }
308     return NO_ERROR;
309 }
310 
CmdUnregisterObserver(MessageParcel & data,MessageParcel & reply)311 ErrCode DataShareStub::CmdUnregisterObserver(MessageParcel &data, MessageParcel &reply)
312 {
313     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
314     if (uri == nullptr) {
315         HILOG_ERROR("DataShareStub uri is nullptr");
316         return ERR_INVALID_VALUE;
317     }
318     auto remoteObject = data.ReadRemoteObject();
319     if (remoteObject == nullptr) {
320         HILOG_ERROR("remoteObject is nullptr");
321         return ERR_INVALID_VALUE;
322     }
323     auto obServer = iface_cast<AAFwk::IDataAbilityObserver>(remoteObject);
324     if (obServer == nullptr) {
325         HILOG_ERROR("the obServer creator is not exists");
326         return ERR_INVALID_VALUE;
327     }
328 
329     bool ret = UnregisterObserver(*uri, obServer);
330     if (!reply.WriteInt32(ret)) {
331         HILOG_ERROR("fail to WriteInt32 ret");
332         return ERR_INVALID_VALUE;
333     }
334     return NO_ERROR;
335 }
336 
CmdNotifyChange(MessageParcel & data,MessageParcel & reply)337 ErrCode DataShareStub::CmdNotifyChange(MessageParcel &data, MessageParcel &reply)
338 {
339     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
340     if (uri == nullptr) {
341         HILOG_ERROR("DataShareStub uri is nullptr");
342         return ERR_INVALID_VALUE;
343     }
344 
345     bool ret = NotifyChange(*uri);
346     if (!reply.WriteInt32(ret)) {
347         HILOG_ERROR("fail to WriteInt32 ret");
348         return ERR_INVALID_VALUE;
349     }
350     return NO_ERROR;
351 }
352 
CmdNormalizeUri(MessageParcel & data,MessageParcel & reply)353 ErrCode DataShareStub::CmdNormalizeUri(MessageParcel &data, MessageParcel &reply)
354 {
355     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
356     if (uri == nullptr) {
357         HILOG_ERROR("DataShareStub uri is nullptr");
358         return ERR_INVALID_VALUE;
359     }
360 
361     Uri ret("");
362     ret = NormalizeUri(*uri);
363     if (!reply.WriteParcelable(&ret)) {
364         HILOG_ERROR("fail to WriteParcelable type");
365         return ERR_INVALID_VALUE;
366     }
367     return NO_ERROR;
368 }
369 
CmdDenormalizeUri(MessageParcel & data,MessageParcel & reply)370 ErrCode DataShareStub::CmdDenormalizeUri(MessageParcel &data, MessageParcel &reply)
371 {
372     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
373     if (uri == nullptr) {
374         HILOG_ERROR("DataShareStub uri is nullptr");
375         return ERR_INVALID_VALUE;
376     }
377 
378     Uri ret("");
379     ret = DenormalizeUri(*uri);
380     if (!reply.WriteParcelable(&ret)) {
381         HILOG_ERROR("fail to WriteParcelable type");
382         return ERR_INVALID_VALUE;
383     }
384     return NO_ERROR;
385 }
386 
CmdExecuteBatch(MessageParcel & data,MessageParcel & reply)387 ErrCode DataShareStub::CmdExecuteBatch(MessageParcel &data, MessageParcel &reply)
388 {
389     HILOG_INFO("DataShareStub::CmdExecuteBatchInner start");
390     int count = 0;
391     if (!data.ReadInt32(count)) {
392         HILOG_ERROR("DataShareStub::CmdExecuteBatchInner fail to ReadInt32 count");
393         return ERR_INVALID_VALUE;
394     }
395     HILOG_INFO("DataShareStub::CmdExecuteBatchInner count:%{public}d", count);
396     std::vector<std::shared_ptr<AppExecFwk::DataAbilityOperation>> operations;
397     for (int i = 0; i < count; i++) {
398         AppExecFwk::DataAbilityOperation *operation = data.ReadParcelable<AppExecFwk::DataAbilityOperation>();
399         if (operation == nullptr) {
400             HILOG_ERROR("DataShareStub::CmdExecuteBatchInner operation is nullptr, index = %{public}d", i);
401             return ERR_INVALID_VALUE;
402         }
403         std::shared_ptr<AppExecFwk::DataAbilityOperation> dataAbilityOperation(operation);
404         operations.push_back(dataAbilityOperation);
405     }
406 
407     std::vector<std::shared_ptr<AppExecFwk::DataAbilityResult>> results = ExecuteBatch(operations);
408     int total = (int)(results.size());
409     if (!reply.WriteInt32(total)) {
410         HILOG_ERROR("DataShareStub::CmdExecuteBatchInner fail to WriteInt32 ret");
411         return ERR_INVALID_VALUE;
412     }
413     HILOG_INFO("DataShareStub::CmdExecuteBatchInner total:%{public}d", total);
414     for (int i = 0; i < total; i++) {
415         if (results[i] == nullptr) {
416             HILOG_ERROR("DataShareStub::CmdExecuteBatchInner results[i] is nullptr, index = %{public}d", i);
417             return ERR_INVALID_VALUE;
418         }
419         if (!reply.WriteParcelable(results[i].get())) {
420             HILOG_ERROR(
421                 "DataShareStub::CmdExecuteBatchInner fail to WriteParcelable operation, index = %{public}d", i);
422             return ERR_INVALID_VALUE;
423         }
424     }
425     HILOG_INFO("DataShareStub::CmdExecuteBatchInner end");
426     return NO_ERROR;
427 }
428 } // namespace AAFwk
429 } // namespace OHOS
430