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 #include "ipc/cloud_sync_service_stub.h"
16 #include "cloud_file_sync_service_interface_code.h"
17 #include "dfs_error.h"
18 #include "dfsu_access_token_helper.h"
19 #include "utils_log.h"
20
21 namespace OHOS::FileManagement::CloudSync {
22 using namespace std;
23
CloudSyncServiceStub()24 CloudSyncServiceStub::CloudSyncServiceStub()
25 {
26 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UNREGISTER_CALLBACK)] =
27 &CloudSyncServiceStub::HandleUnRegisterCallbackInner;
28 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_CALLBACK)] =
29 &CloudSyncServiceStub::HandleRegisterCallbackInner;
30 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_SYNC)] =
31 &CloudSyncServiceStub::HandleStartSyncInner;
32 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_SYNC)] =
33 &CloudSyncServiceStub::HandleStopSyncInner;
34 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_CHANGE_APP_SWITCH)] =
35 &CloudSyncServiceStub::HandleChangeAppSwitch;
36 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_CLEAN)] =
37 &CloudSyncServiceStub::HandleClean;
38 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_NOTIFY_DATA_CHANGE)] =
39 &CloudSyncServiceStub::HandleNotifyDataChange;
40 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_ENABLE_CLOUD)] =
41 &CloudSyncServiceStub::HandleEnableCloud;
42 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DISABLE_CLOUD)] =
43 &CloudSyncServiceStub::HandleDisableCloud;
44 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_DOWNLOAD_FILE)] =
45 &CloudSyncServiceStub::HandleStartDownloadFile;
46 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_DOWNLOAD_FILE)] =
47 &CloudSyncServiceStub::HandleStopDownloadFile;
48 opToInterfaceMap_[static_cast<uint32_t>(
49 CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_DOWNLOAD_FILE_CALLBACK)] =
50 &CloudSyncServiceStub::HandleRegisterDownloadFileCallback;
51 opToInterfaceMap_[static_cast<uint32_t>(
52 CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UNREGISTER_DOWNLOAD_FILE_CALLBACK)] =
53 &CloudSyncServiceStub::HandleUnregisterDownloadFileCallback;
54 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UPLOAD_ASSET)] =
55 &CloudSyncServiceStub::HandleUploadAsset;
56 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_FILE)] =
57 &CloudSyncServiceStub::HandleDownloadFile;
58 }
59
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)60 int32_t CloudSyncServiceStub::OnRemoteRequest(uint32_t code,
61 MessageParcel &data,
62 MessageParcel &reply,
63 MessageOption &option)
64 {
65 if (data.ReadInterfaceToken() != GetDescriptor()) {
66 return E_SERVICE_DESCRIPTOR_IS_EMPTY;
67 }
68 auto interfaceIndex = opToInterfaceMap_.find(code);
69 if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) {
70 LOGE("Cannot response request %d: unknown tranction", code);
71 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
72 }
73 return (this->*(interfaceIndex->second))(data, reply);
74 }
75
HandleUnRegisterCallbackInner(MessageParcel & data,MessageParcel & reply)76 int32_t CloudSyncServiceStub::HandleUnRegisterCallbackInner(MessageParcel &data, MessageParcel &reply)
77 {
78 LOGI("Begin UnRegisterCallbackInner");
79 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
80 LOGE("permission denied");
81 return E_PERMISSION_DENIED;
82 }
83 if (!DfsuAccessTokenHelper::IsSystemApp()) {
84 LOGE("caller hap is not system hap");
85 return E_PERMISSION_SYSTEM;
86 }
87
88 int32_t res = UnRegisterCallbackInner();
89 reply.WriteInt32(res);
90 LOGI("End UnRegisterCallbackInner");
91 return res;
92 }
93
HandleRegisterCallbackInner(MessageParcel & data,MessageParcel & reply)94 int32_t CloudSyncServiceStub::HandleRegisterCallbackInner(MessageParcel &data, MessageParcel &reply)
95 {
96 LOGI("Begin RegisterCallbackInner");
97 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
98 LOGE("permission denied");
99 return E_PERMISSION_DENIED;
100 }
101 if (!DfsuAccessTokenHelper::IsSystemApp()) {
102 LOGE("caller hap is not system hap");
103 return E_PERMISSION_SYSTEM;
104 }
105 auto remoteObj = data.ReadRemoteObject();
106 int32_t res = RegisterCallbackInner(remoteObj);
107 reply.WriteInt32(res);
108 LOGI("End RegisterCallbackInner");
109 return res;
110 }
111
HandleStartSyncInner(MessageParcel & data,MessageParcel & reply)112 int32_t CloudSyncServiceStub::HandleStartSyncInner(MessageParcel &data, MessageParcel &reply)
113 {
114 LOGI("Begin StartSyncInner");
115 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
116 LOGE("permission denied");
117 return E_PERMISSION_DENIED;
118 }
119 if (!DfsuAccessTokenHelper::IsSystemApp()) {
120 LOGE("caller hap is not system hap");
121 return E_PERMISSION_SYSTEM;
122 }
123 auto forceFlag = data.ReadBool();
124 int32_t res = StartSyncInner(forceFlag);
125 reply.WriteInt32(res);
126 LOGI("End StartSyncInner");
127 return res;
128 }
129
HandleStopSyncInner(MessageParcel & data,MessageParcel & reply)130 int32_t CloudSyncServiceStub::HandleStopSyncInner(MessageParcel &data, MessageParcel &reply)
131 {
132 LOGI("Begin StopSyncInner");
133 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
134 LOGE("permission denied");
135 return E_PERMISSION_DENIED;
136 }
137 if (!DfsuAccessTokenHelper::IsSystemApp()) {
138 LOGE("caller hap is not system hap");
139 return E_PERMISSION_SYSTEM;
140 }
141 int32_t res = StopSyncInner();
142 reply.WriteInt32(res);
143 LOGI("End StopSyncInner");
144 return res;
145 }
146
HandleChangeAppSwitch(MessageParcel & data,MessageParcel & reply)147 int32_t CloudSyncServiceStub::HandleChangeAppSwitch(MessageParcel &data, MessageParcel &reply)
148 {
149 LOGI("Begin ChangeAppSwitch");
150 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC_MANAGER)) {
151 LOGE("permission denied");
152 return E_PERMISSION_DENIED;
153 }
154 if (!DfsuAccessTokenHelper::IsSystemApp()) {
155 LOGE("caller hap is not system hap");
156 return E_PERMISSION_SYSTEM;
157 }
158 string accountId = data.ReadString();
159 string bundleName = data.ReadString();
160 bool status = data.ReadBool();
161 int32_t res = ChangeAppSwitch(accountId, bundleName, status);
162 reply.WriteInt32(res);
163 LOGI("End ChangeAppSwitch");
164 return res;
165 }
166
HandleClean(MessageParcel & data,MessageParcel & reply)167 int32_t CloudSyncServiceStub::HandleClean(MessageParcel &data, MessageParcel &reply)
168 {
169 LOGI("Begin Clean");
170 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC_MANAGER)) {
171 LOGE("permission denied");
172 return E_PERMISSION_DENIED;
173 }
174 if (!DfsuAccessTokenHelper::IsSystemApp()) {
175 LOGE("caller hap is not system hap");
176 return E_PERMISSION_SYSTEM;
177 }
178 string accountId = data.ReadString();
179 sptr<CleanOptions> options = data.ReadParcelable<CleanOptions>();
180 if (!options) {
181 LOGE("object of CleanOptions is nullptr");
182 return E_INVAL_ARG;
183 }
184 int32_t res = Clean(accountId, *options);
185 reply.WriteInt32(res);
186 LOGI("End Clean");
187 return res;
188 }
189
HandleNotifyDataChange(MessageParcel & data,MessageParcel & reply)190 int32_t CloudSyncServiceStub::HandleNotifyDataChange(MessageParcel &data, MessageParcel &reply)
191 {
192 LOGI("Begin NotifyDataChange");
193 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC_MANAGER)) {
194 LOGE("permission denied");
195 return E_PERMISSION_DENIED;
196 }
197 if (!DfsuAccessTokenHelper::IsSystemApp()) {
198 LOGE("caller hap is not system hap");
199 return E_PERMISSION_SYSTEM;
200 }
201 string accountId = data.ReadString();
202 string bundleName = data.ReadString();
203 int32_t res = NotifyDataChange(accountId, bundleName);
204 reply.WriteInt32(res);
205 LOGI("End NotifyDataChange");
206 return res;
207 }
HandleDisableCloud(MessageParcel & data,MessageParcel & reply)208 int32_t CloudSyncServiceStub::HandleDisableCloud(MessageParcel &data, MessageParcel &reply)
209 {
210 LOGI("Begin DisableCloud");
211 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC_MANAGER)) {
212 LOGE("permission denied");
213 return E_PERMISSION_DENIED;
214 }
215 if (!DfsuAccessTokenHelper::IsSystemApp()) {
216 LOGE("caller hap is not system hap");
217 return E_PERMISSION_SYSTEM;
218 }
219 string accountId = data.ReadString();
220 int32_t res = DisableCloud(accountId);
221 reply.WriteInt32(res);
222 LOGI("End DisableCloud");
223 return res;
224 }
HandleEnableCloud(MessageParcel & data,MessageParcel & reply)225 int32_t CloudSyncServiceStub::HandleEnableCloud(MessageParcel &data, MessageParcel &reply)
226 {
227 LOGI("Begin EnableCloud");
228 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC_MANAGER)) {
229 LOGE("permission denied");
230 return E_PERMISSION_DENIED;
231 }
232 if (!DfsuAccessTokenHelper::IsSystemApp()) {
233 LOGE("caller hap is not system hap");
234 return E_PERMISSION_SYSTEM;
235 }
236 string accountId = data.ReadString();
237 sptr<SwitchDataObj> switchObj = data.ReadParcelable<SwitchDataObj>();
238 if (!switchObj) {
239 LOGE("object of SwitchDataObj is nullptr");
240 return E_INVAL_ARG;
241 }
242 int32_t res = EnableCloud(accountId, *switchObj);
243 reply.WriteInt32(res);
244 LOGI("End EnableCloud");
245 return res;
246 }
247
HandleStartDownloadFile(MessageParcel & data,MessageParcel & reply)248 int32_t CloudSyncServiceStub::HandleStartDownloadFile(MessageParcel &data, MessageParcel &reply)
249 {
250 LOGI("Begin HandleStartDownloadFile");
251 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
252 LOGE("permission denied");
253 return E_PERMISSION_DENIED;
254 }
255 if (!DfsuAccessTokenHelper::IsSystemApp()) {
256 LOGE("caller hap is not system hap");
257 return E_PERMISSION_SYSTEM;
258 }
259 string path = data.ReadString();
260
261 int32_t res = StartDownloadFile(path);
262 reply.WriteInt32(res);
263 LOGI("End HandleStartDownloadFile");
264 return res;
265 }
266
HandleStopDownloadFile(MessageParcel & data,MessageParcel & reply)267 int32_t CloudSyncServiceStub::HandleStopDownloadFile(MessageParcel &data, MessageParcel &reply)
268 {
269 LOGI("Begin HandleStopDownloadFile");
270 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
271 LOGE("permission denied");
272 return E_PERMISSION_DENIED;
273 }
274 if (!DfsuAccessTokenHelper::IsSystemApp()) {
275 LOGE("caller hap is not system hap");
276 return E_PERMISSION_SYSTEM;
277 }
278 string path = data.ReadString();
279
280 int32_t res = StopDownloadFile(path);
281 reply.WriteInt32(res);
282 LOGI("End HandleStopDownloadFile");
283 return res;
284 }
285
HandleRegisterDownloadFileCallback(MessageParcel & data,MessageParcel & reply)286 int32_t CloudSyncServiceStub::HandleRegisterDownloadFileCallback(MessageParcel &data, MessageParcel &reply)
287 {
288 LOGI("Begin HandleRegisterDownloadFileCallback");
289 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
290 LOGE("permission denied");
291 return E_PERMISSION_DENIED;
292 }
293 if (!DfsuAccessTokenHelper::IsSystemApp()) {
294 LOGE("caller hap is not system hap");
295 return E_PERMISSION_SYSTEM;
296 }
297
298 auto downloadCallback = data.ReadRemoteObject();
299
300 int32_t res = RegisterDownloadFileCallback(downloadCallback);
301 reply.WriteInt32(res);
302 LOGI("End HandleRegisterDownloadFileCallback");
303 return res;
304 }
305
HandleUnregisterDownloadFileCallback(MessageParcel & data,MessageParcel & reply)306 int32_t CloudSyncServiceStub::HandleUnregisterDownloadFileCallback(MessageParcel &data, MessageParcel &reply)
307 {
308 LOGI("Begin HandleUnregisterDownloadFileCallback");
309 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
310 LOGE("permission denied");
311 return E_PERMISSION_DENIED;
312 }
313 if (!DfsuAccessTokenHelper::IsSystemApp()) {
314 LOGE("caller hap is not system hap");
315 return E_PERMISSION_SYSTEM;
316 }
317
318 int32_t res = UnregisterDownloadFileCallback();
319 reply.WriteInt32(res);
320 LOGI("End HandleUnregisterDownloadFileCallback");
321 return res;
322 }
323
HandleUploadAsset(MessageParcel & data,MessageParcel & reply)324 int32_t CloudSyncServiceStub::HandleUploadAsset(MessageParcel &data, MessageParcel &reply)
325 {
326 LOGI("Begin UploadAsset");
327 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
328 LOGE("permission denied");
329 return E_PERMISSION_DENIED;
330 }
331 if (!DfsuAccessTokenHelper::IsSystemApp()) {
332 LOGE("caller hap is not system hap");
333 return E_PERMISSION_SYSTEM;
334 }
335 int32_t userId = data.ReadInt32();
336 string request = data.ReadString();
337 string result;
338 int32_t res = UploadAsset(userId, request, result);
339 reply.WriteInt32(res);
340 reply.WriteString(result);
341 LOGI("End UploadAsset");
342 return res;
343 }
344
HandleDownloadFile(MessageParcel & data,MessageParcel & reply)345 int32_t CloudSyncServiceStub::HandleDownloadFile(MessageParcel &data, MessageParcel &reply)
346 {
347 LOGI("Begin DownloadFile");
348 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
349 LOGE("permission denied");
350 return E_PERMISSION_DENIED;
351 }
352 if (!DfsuAccessTokenHelper::IsSystemApp()) {
353 LOGE("caller hap is not system hap");
354 return E_PERMISSION_SYSTEM;
355 }
356 int32_t userId = data.ReadInt32();
357 string bundleName = data.ReadString();
358 sptr<AssetInfoObj> assetInfoObj = data.ReadParcelable<AssetInfoObj>();
359 if (!assetInfoObj) {
360 LOGE("object of AssetInfoObj is nullptr");
361 return E_INVAL_ARG;
362 }
363 int32_t res = DownloadFile(userId, bundleName, *assetInfoObj);
364 reply.WriteInt32(res);
365 LOGI("End DownloadFile");
366 return res;
367 }
368 } // namespace OHOS::FileManagement::CloudSync
369