• 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 #define LOG_TAG "RdbServiceStub"
17 
18 #include "rdb_service_stub.h"
19 #include <ipc_skeleton.h>
20 #include "log_print.h"
21 #include "itypes_util.h"
22 
23 namespace OHOS::DistributedRdb {
OnRemoteObtainDistributedTableName(MessageParcel & data,MessageParcel & reply)24 int32_t RdbServiceStub::OnRemoteObtainDistributedTableName(MessageParcel &data, MessageParcel &reply)
25 {
26     std::string device;
27     std::string table;
28     if (!DistributedKv::ITypesUtil::Unmarshal(data, device, table)) {
29         ZLOGE("read from message parcel failed");
30         reply.WriteString("");
31         return RDB_OK;
32     }
33 
34     reply.WriteString(ObtainDistributedTableName(device, table));
35     return RDB_OK;
36 }
37 
OnRemoteInitNotifier(MessageParcel & data,MessageParcel & reply)38 int32_t RdbServiceStub::OnRemoteInitNotifier(MessageParcel &data, MessageParcel &reply)
39 {
40     RdbSyncerParam param;
41     sptr<IRemoteObject> notifier;
42     if (!DistributedKv::ITypesUtil::Unmarshal(data, param, notifier)) {
43         ZLOGE("read from message parcel failed");
44         reply.WriteInt32(RDB_ERROR);
45         return RDB_OK;
46     }
47     if (notifier == nullptr) {
48         ZLOGE("notifier is null");
49         reply.WriteInt32(RDB_ERROR);
50         return RDB_OK;
51     }
52     if (InitNotifier(param, notifier) != RDB_OK) {
53         ZLOGE("init notifier failed");
54         reply.WriteInt32(RDB_ERROR);
55         return RDB_OK;
56     }
57     ZLOGI("success");
58     reply.WriteInt32(RDB_OK);
59     return RDB_OK;
60 }
61 
OnRemoteSetDistributedTables(MessageParcel & data,MessageParcel & reply)62 int32_t RdbServiceStub::OnRemoteSetDistributedTables(MessageParcel &data, MessageParcel &reply)
63 {
64     RdbSyncerParam param;
65     std::vector<std::string> tables;
66     if (!DistributedKv::ITypesUtil::Unmarshal(data, param, tables)) {
67         ZLOGE("read from message parcel failed");
68         reply.WriteInt32(RDB_ERROR);
69         return RDB_OK;
70     }
71 
72     reply.WriteInt32(SetDistributedTables(param, tables));
73     return RDB_OK;
74 }
75 
OnRemoteDoSync(MessageParcel & data,MessageParcel & reply)76 int32_t RdbServiceStub::OnRemoteDoSync(MessageParcel &data, MessageParcel &reply)
77 {
78     RdbSyncerParam param;
79     SyncOption option {};
80     RdbPredicates predicates;
81     if (!DistributedKv::ITypesUtil::Unmarshal(data, param, option, predicates)) {
82         ZLOGE("read from message parcel failed");
83         reply.WriteInt32(RDB_ERROR);
84         return RDB_OK;
85     }
86 
87     SyncResult result;
88     if (DoSync(param, option, predicates, result) != RDB_OK) {
89         reply.WriteInt32(RDB_ERROR);
90         return RDB_OK;
91     }
92     if (!DistributedKv::ITypesUtil::Marshalling(result, reply)) {
93         reply.WriteInt32(RDB_ERROR);
94         return RDB_OK;
95     }
96     return RDB_OK;
97 }
98 
OnRemoteDoAsync(MessageParcel & data,MessageParcel & reply)99 int32_t RdbServiceStub::OnRemoteDoAsync(MessageParcel &data, MessageParcel &reply)
100 {
101     RdbSyncerParam param;
102     uint32_t seqNum;
103     SyncOption option {};
104     RdbPredicates predicates;
105     if (!DistributedKv::ITypesUtil::Unmarshal(data, param, seqNum, option, predicates)) {
106         ZLOGE("read from message parcel failed");
107         reply.WriteInt32(RDB_ERROR);
108         return RDB_OK;
109     }
110 
111     reply.WriteInt32(DoAsync(param, seqNum, option, predicates));
112     return RDB_OK;
113 }
114 
OnRemoteDoSubscribe(MessageParcel & data,MessageParcel & reply)115 int32_t RdbServiceStub::OnRemoteDoSubscribe(MessageParcel &data, MessageParcel &reply)
116 {
117     RdbSyncerParam param;
118     if (!DistributedKv::ITypesUtil::Unmarshal(data, param)) {
119         ZLOGE("read from message parcel failed");
120         reply.WriteInt32(RDB_ERROR);
121         return RDB_OK;
122     }
123     reply.WriteInt32(DoSubscribe(param));
124     return RDB_OK;
125 }
126 
OnRemoteDoUnSubscribe(MessageParcel & data,MessageParcel & reply)127 int32_t RdbServiceStub::OnRemoteDoUnSubscribe(MessageParcel &data, MessageParcel &reply)
128 {
129     RdbSyncerParam param;
130     if (!DistributedKv::ITypesUtil::Unmarshal(data, param)) {
131         ZLOGE("read from message parcel failed");
132         reply.WriteInt32(RDB_ERROR);
133         return RDB_OK;
134     }
135     reply.WriteInt32(DoUnSubscribe(param));
136     return RDB_OK;
137 }
138 
OnRemoteDoRemoteQuery(MessageParcel & data,MessageParcel & reply)139 int32_t RdbServiceStub::OnRemoteDoRemoteQuery(MessageParcel& data, MessageParcel& reply)
140 {
141     RdbSyncerParam param;
142     std::string device;
143     std::string sql;
144     std::vector<std::string> selectionArgs;
145     if (!DistributedKv::ITypesUtil::Unmarshal(data, param, device, sql, selectionArgs)) {
146         ZLOGE("read from message parcel failed");
147         reply.WriteInt32(RDB_ERROR);
148         return RDB_OK;
149     }
150 
151     sptr<IRemoteObject> resultSet;
152     int32_t status = RemoteQuery(param, device, sql, selectionArgs, resultSet);
153     if (status != RDB_OK) {
154         reply.WriteInt32(RDB_ERROR);
155         return RDB_OK;
156     }
157     reply.WriteInt32(RDB_OK);
158     reply.WriteRemoteObject(resultSet);
159     return RDB_OK;
160 }
161 
CheckInterfaceToken(MessageParcel & data)162 bool RdbServiceStub::CheckInterfaceToken(MessageParcel& data)
163 {
164     auto localDescriptor = GetDescriptor();
165     auto remoteDescriptor = data.ReadInterfaceToken();
166     if (remoteDescriptor != localDescriptor) {
167         ZLOGE("interface token is not equal");
168         return false;
169     }
170     return true;
171 }
172 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)173 int RdbServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply)
174 {
175     ZLOGD("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
176     if (!CheckInterfaceToken(data)) {
177         return RDB_ERROR;
178     }
179     if (code >= 0 && code < RDB_SERVICE_CMD_MAX) {
180         return (this->*HANDLERS[code])(data, reply);
181     }
182     return RDB_ERROR;
183 }
184 
OnRemoteDoCreateTable(MessageParcel & data,MessageParcel & reply)185 int32_t RdbServiceStub::OnRemoteDoCreateTable(MessageParcel &data, MessageParcel &reply)
186 {
187     RdbSyncerParam param;
188     std::string writePermission;
189     std::string readPermission;
190     if (!DistributedKv::ITypesUtil::Unmarshal(data, param, writePermission, readPermission)) {
191         ZLOGE("read from message parcel failed");
192         reply.WriteInt32(RDB_ERROR);
193         return RDB_OK;
194     }
195 
196     int32_t status = CreateRDBTable(param, writePermission, readPermission);
197     if (status != RDB_OK) {
198         reply.WriteInt32(RDB_ERROR);
199         return RDB_OK;
200     }
201     reply.WriteInt32(RDB_OK);
202     return RDB_OK;
203 }
204 
OnRemoteDoDestroyTable(MessageParcel & data,MessageParcel & reply)205 int32_t RdbServiceStub::OnRemoteDoDestroyTable(MessageParcel &data, MessageParcel &reply)
206 {
207     RdbSyncerParam param;
208     if (!DistributedKv::ITypesUtil::Unmarshal(data, param)) {
209         ZLOGE("read from message parcel failed");
210         reply.WriteInt32(RDB_ERROR);
211         return RDB_OK;
212     }
213 
214     int32_t status = DestroyRDBTable(param);
215     if (status != RDB_OK) {
216         reply.WriteInt32(RDB_ERROR);
217         return RDB_OK;
218     }
219     reply.WriteInt32(RDB_OK);
220     return RDB_OK;
221 }
222 } // namespace OHOS::DistributedRdb
223