1 /*
2  * Copyright (c) 2023 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 #define LOG_TAG "CloudServiceProxy"
16 #include "cloud_service_proxy.h"
17 
18 #include "itypes_util.h"
19 #include "logger.h"
20 
21 namespace OHOS::CloudData {
22 using namespace OHOS::Rdb;
23 
24 #define IPC_SEND(code, reply, ...)                                          \
25 ({                                                                          \
26     int32_t __status = SUCCESS;                                             \
27     do {                                                                    \
28         MessageParcel request;                                              \
29         if (!request.WriteInterfaceToken(GetDescriptor())) {                \
30             __status = IPC_PARCEL_ERROR;                                    \
31             break;                                                          \
32         }                                                                   \
33         if (!ITypesUtil::Marshal(request, ##__VA_ARGS__)) {                 \
34             __status = IPC_PARCEL_ERROR;                                    \
35             break;                                                          \
36         }                                                                   \
37         MessageOption option;                                               \
38         auto result = remote_->SendRequest((code), request, reply, option); \
39         if (result != 0) {                                                  \
40             __status = IPC_ERROR;                                           \
41             break;                                                          \
42         }                                                                   \
43                                                                             \
44         ITypesUtil::Unmarshal(reply, __status);                             \
45     } while (0);                                                            \
46     __status;                                                               \
47 })
48 
CloudServiceProxy(const sptr<IRemoteObject> & object)49 CloudServiceProxy::CloudServiceProxy(const sptr<IRemoteObject> &object)
50     : IRemoteProxy<ICloudService>(object)
51 {
52     remote_ = Remote();
53 }
54 
EnableCloud(const std::string & id,const std::map<std::string,int32_t> & switches)55 int32_t CloudServiceProxy::EnableCloud(const std::string &id, const std::map<std::string, int32_t> &switches)
56 {
57     MessageParcel reply;
58     int32_t status = IPC_SEND(TRANS_ENABLE_CLOUD, reply, id, switches);
59     if (status != SUCCESS) {
60         LOG_ERROR("status:0x%{public}x id:%{public}.6s size:%{public}zu", status, id.c_str(), switches.size());
61     }
62     return static_cast<Status>(status);
63 }
64 
DisableCloud(const std::string & id)65 int32_t CloudServiceProxy::DisableCloud(const std::string &id)
66 {
67     MessageParcel reply;
68     int32_t status = IPC_SEND(TRANS_DISABLE_CLOUD, reply, id);
69     if (status != SUCCESS) {
70         LOG_ERROR("status:0x%{public}x id:%{public}.6s", status, id.c_str());
71     }
72     return static_cast<Status>(status);
73 }
74 
ChangeAppSwitch(const std::string & id,const std::string & bundleName,int32_t appSwitch)75 int32_t CloudServiceProxy::ChangeAppSwitch(const std::string &id, const std::string &bundleName, int32_t appSwitch)
76 {
77     MessageParcel reply;
78     int32_t status = IPC_SEND(TRANS_CHANGE_APP_SWITCH, reply, id, bundleName, appSwitch);
79     if (status != SUCCESS) {
80         LOG_ERROR("status:0x%{public}x id:%{public}.6s bundleName:%{public}s switch:%{public}d",
81             status, id.c_str(), bundleName.c_str(), appSwitch);
82     }
83     return static_cast<Status>(status);
84 }
85 
Clean(const std::string & id,const std::map<std::string,int32_t> & actions)86 int32_t CloudServiceProxy::Clean(const std::string &id, const std::map<std::string, int32_t> &actions)
87 {
88     MessageParcel reply;
89     int32_t status = IPC_SEND(TRANS_CLEAN, reply, id, actions);
90     if (status != SUCCESS) {
91         LOG_ERROR("status:0x%{public}x id:%{public}.6s size:%{public}zu", status, id.c_str(), actions.size());
92     }
93     return static_cast<Status>(status);
94 }
95 
NotifyDataChange(const std::string & id,const std::string & bundleName)96 int32_t CloudServiceProxy::NotifyDataChange(const std::string &id, const std::string &bundleName)
97 {
98     MessageParcel reply;
99     int32_t status = IPC_SEND(TRANS_NOTIFY_DATA_CHANGE, reply, id, bundleName);
100     if (status != SUCCESS) {
101         LOG_ERROR("status:0x%{public}x id:%{public}.6s bundleName:%{public}s", status, id.c_str(), bundleName.c_str());
102     }
103     return static_cast<Status>(status);
104 }
105 
SetGlobalCloudStrategy(Strategy strategy,const std::vector<CommonType::Value> & values)106 int32_t CloudServiceProxy::SetGlobalCloudStrategy(Strategy strategy, const std::vector<CommonType::Value> &values)
107 {
108     MessageParcel reply;
109     int32_t status = IPC_SEND(TRANS_SET_GLOBAL_CLOUD_STRATEGY, reply, strategy, values);
110     if (status != SUCCESS) {
111         LOG_ERROR("status:0x%{public}x strategy:%{public}d values size:%{public}zu", status,
112             static_cast<uint32_t>(strategy), values.size());
113     }
114     return static_cast<Status>(status);
115 }
116 
AllocResourceAndShare(const std::string & storeId,const DistributedRdb::PredicatesMemo & predicates,const std::vector<std::string> & columns,const std::vector<Participant> & participants)117 std::pair<int32_t, std::vector<NativeRdb::ValuesBucket>> CloudServiceProxy::AllocResourceAndShare(
118     const std::string &storeId, const DistributedRdb::PredicatesMemo &predicates,
119     const std::vector<std::string> &columns, const std::vector<Participant> &participants)
120 {
121     MessageParcel reply;
122     int32_t status = IPC_SEND(TRANS_ALLOC_RESOURCE_AND_SHARE, reply, storeId, predicates, columns, participants);
123     if (status != SUCCESS) {
124         LOG_ERROR("status:0x%{public}x storeName:%{public}.6s", status, storeId.c_str());
125     }
126     std::vector<NativeRdb::ValuesBucket> valueBuckets;
127     ITypesUtil::Unmarshal(reply, valueBuckets);
128     return { static_cast<Status>(status), valueBuckets };
129 }
130 
NotifyDataChange(const std::string & eventId,const std::string & extraData,int32_t userId)131 int32_t CloudServiceProxy::NotifyDataChange(const std::string &eventId, const std::string &extraData, int32_t userId)
132 {
133     MessageParcel reply;
134     int32_t status = IPC_SEND(TRANS_NOTIFY_DATA_CHANGE_EXT, reply, eventId, extraData, userId);
135     if (status != SUCCESS) {
136         LOG_ERROR("status:0x%{public}x eventId:%{public}.6s extraData:%{public}.6s", status, eventId.c_str(),
137             extraData.c_str());
138     }
139     return static_cast<Status>(status);
140 }
141 
QueryStatistics(const std::string & id,const std::string & bundleName,const std::string & storeId)142 std::pair<int32_t, std::map<std::string, StatisticInfos>> CloudServiceProxy::QueryStatistics(const std::string &id,
143     const std::string &bundleName, const std::string &storeId)
144 {
145     MessageParcel reply;
146     int32_t status = IPC_SEND(TRANS_QUERY_STATISTICS, reply, id, bundleName, storeId);
147     if (status != SUCCESS) {
148         LOG_ERROR("status:0x%{public}x bundleName:%{public}.6s storeId:%{public}.6s", status, id.c_str(),
149             storeId.c_str());
150     }
151     std::map<std::string, StatisticInfos> infos;
152     ITypesUtil::Unmarshal(reply, infos);
153     return { status, infos };
154 }
155 
Share(const std::string & sharingRes,const Participants & participants,Results & results)156 int32_t CloudServiceProxy::Share(
157     const std::string &sharingRes, const Participants &participants, Results &results)
158 {
159     MessageParcel reply;
160     int32_t status = IPC_SEND(TRANS_SHARE, reply, sharingRes, participants);
161     if (status != SUCCESS) {
162         LOG_ERROR("status:0x%{public}x sharingRes:%{public}.6s participants:%{public}zu",
163             status, sharingRes.c_str(), participants.size());
164     }
165     ITypesUtil::Unmarshal(reply, results);
166     return static_cast<Status>(status);
167 }
168 
Unshare(const std::string & sharingRes,const Participants & participants,Results & results)169 int32_t CloudServiceProxy::Unshare(
170     const std::string &sharingRes, const Participants &participants, Results &results)
171 {
172     MessageParcel reply;
173     int32_t status = IPC_SEND(TRANS_UNSHARE, reply, sharingRes, participants);
174     if (status != SUCCESS) {
175         LOG_ERROR("status:0x%{public}x sharingRes:%{public}.6s participants:%{public}zu",
176             status, sharingRes.c_str(), participants.size());
177     }
178     ITypesUtil::Unmarshal(reply, results);
179     return static_cast<Status>(status);
180 }
181 
Exit(const std::string & sharingRes,std::pair<int32_t,std::string> & result)182 int32_t CloudServiceProxy::Exit(const std::string &sharingRes, std::pair<int32_t, std::string> &result)
183 {
184     MessageParcel reply;
185     int32_t status = IPC_SEND(TRANS_EXIT, reply, sharingRes);
186     if (status != SUCCESS) {
187         LOG_ERROR("status:0x%{public}x sharingRes:%{public}.6s", status, sharingRes.c_str());
188     }
189     ITypesUtil::Unmarshal(reply, result);
190     return static_cast<Status>(status);
191 }
192 
ChangePrivilege(const std::string & sharingRes,const Participants & participants,Results & results)193 int32_t CloudServiceProxy::ChangePrivilege(
194     const std::string &sharingRes, const Participants &participants, Results &results)
195 {
196     MessageParcel reply;
197     int32_t status = IPC_SEND(TRANS_CHANGE_PRIVILEGE, reply, sharingRes, participants);
198     if (status != SUCCESS) {
199         LOG_ERROR("status:0x%{public}x sharingRes:%{public}.6s participants:%{public}zu",
200             status, sharingRes.c_str(), participants.size());
201     }
202     ITypesUtil::Unmarshal(reply, results);
203     return static_cast<Status>(status);
204 }
205 
Query(const std::string & sharingRes,QueryResults & results)206 int32_t CloudServiceProxy::Query(const std::string &sharingRes, QueryResults &results)
207 {
208     MessageParcel reply;
209     int32_t status = IPC_SEND(TRANS_QUERY, reply, sharingRes);
210     if (status != SUCCESS) {
211         LOG_ERROR("status:0x%{public}x sharingRes:%{public}.6s", status, sharingRes.c_str());
212     }
213     ITypesUtil::Unmarshal(reply, results);
214     return static_cast<Status>(status);
215 }
216 
QueryByInvitation(const std::string & invitation,QueryResults & results)217 int32_t CloudServiceProxy::QueryByInvitation(
218     const std::string &invitation, QueryResults &results)
219 {
220     MessageParcel reply;
221     int32_t status = IPC_SEND(TRANS_QUERY_BY_INVITATION, reply, invitation);
222     if (status != SUCCESS) {
223         LOG_ERROR("status:0x%{public}x invitation:%{public}.6s", status, invitation.c_str());
224     }
225     ITypesUtil::Unmarshal(reply, results);
226     return static_cast<Status>(status);
227 }
228 
ConfirmInvitation(const std::string & invitation,int32_t confirmation,std::tuple<int32_t,std::string,std::string> & result)229 int32_t CloudServiceProxy::ConfirmInvitation(const std::string &invitation,
230     int32_t confirmation, std::tuple<int32_t, std::string, std::string> &result)
231 {
232     MessageParcel reply;
233     int32_t status = IPC_SEND(TRANS_CONFIRM_INVITATION, reply, invitation, confirmation);
234     if (status != SUCCESS) {
235         LOG_ERROR("status:0x%{public}x invitation:%{public}.6s", status, invitation.c_str());
236     }
237     ITypesUtil::Unmarshal(reply, result);
238     return static_cast<Status>(status);
239 }
240 
ChangeConfirmation(const std::string & sharingRes,int32_t confirmation,std::pair<int32_t,std::string> & result)241 int32_t CloudServiceProxy::ChangeConfirmation(const std::string &sharingRes,
242     int32_t confirmation, std::pair<int32_t, std::string> &result)
243 {
244     MessageParcel reply;
245     int32_t status = IPC_SEND(TRANS_CHANGE_CONFIRMATION, reply, sharingRes, confirmation);
246     if (status != SUCCESS) {
247         LOG_ERROR("status:0x%{public}x sharingRes:%{public}.6s", status, sharingRes.c_str());
248     }
249     ITypesUtil::Unmarshal(reply, result);
250     return static_cast<Status>(status);
251 }
252 
SetCloudStrategy(Strategy strategy,const std::vector<CommonType::Value> & values)253 int32_t CloudServiceProxy::SetCloudStrategy(Strategy strategy, const std::vector<CommonType::Value> &values)
254 {
255     MessageParcel reply;
256     int32_t status = IPC_SEND(TRANS_SET_CLOUD_STRATEGY, reply, strategy, values);
257     if (status != SUCCESS) {
258         LOG_ERROR("status:0x%{public}x strategy:%{public}d values size:%{public}zu", status,
259             static_cast<uint32_t>(strategy), values.size());
260     }
261     return static_cast<Status>(status);
262 }
263 
QueryLastSyncInfo(const std::string & id,const std::string & bundleName,const std::string & storeId)264 std::pair<int32_t, QueryLastResults> CloudServiceProxy::QueryLastSyncInfo(
265     const std::string &id, const std::string &bundleName, const std::string &storeId)
266 {
267     MessageParcel reply;
268     int32_t status = IPC_SEND(TRANS_QUERY_LAST_SYNC_INFO, reply, id, bundleName, storeId);
269     if (status != SUCCESS) {
270         LOG_ERROR("status:0x%{public}x id:%{public}.6s bundleName:%{public}s storeId:%{public}.3s", status, id.c_str(),
271             bundleName.c_str(), storeId.c_str());
272     }
273     QueryLastResults results;
274     ITypesUtil::Unmarshal(reply, results);
275     return { status, results };
276 }
277 } // namespace OHOS::CloudData