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