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 "dfsu_memory_guard.h"
20 #include "task_state_manager.h"
21 #include "utils_log.h"
22
23 namespace OHOS::FileManagement::CloudSync {
24 using namespace std;
25
26 static const int READ_SIZE = 100;
27 static const int MAX_READ_DENTRY_FILE_SIZE = 500;
28
CloudSyncServiceStub()29 CloudSyncServiceStub::CloudSyncServiceStub()
30 {
31 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UNREGISTER_CALLBACK)] =
32 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleUnRegisterCallbackInner(data, reply); };
33 opToInterfaceMap_[static_cast<uint32_t>
34 (CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UNREGISTER_FILE_SYNC_CALLBACK)] = [this](MessageParcel &data,
35 MessageParcel &reply) { return this->HandleUnRegisterFileSyncCallbackInner(data, reply); };
36 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_CALLBACK)] =
37 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleRegisterCallbackInner(data, reply); };
38 opToInterfaceMap_[static_cast<uint32_t>
39 (CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_FILE_SYNC_CALLBACK)] = [this](MessageParcel &data,
40 MessageParcel &reply) { return this->HandleRegisterFileSyncCallbackInner(data, reply); };
41 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_SYNC)] =
42 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleStartSyncInner(data, reply); };
43 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_FILE_SYNC)] =
44 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleStartFileSyncInner(data, reply); };
45 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_TRIGGER_SYNC)] =
46 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleTriggerSyncInner(data, reply); };
47 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_SYNC)] =
48 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleStopSyncInner(data, reply); };
49 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_FILE_SYNC)] =
50 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleStopFileSyncInner(data, reply); };
51 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_CHANGE_APP_SWITCH)] =
52 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleChangeAppSwitch(data, reply); };
53 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_CLEAN)] =
54 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleClean(data, reply); };
55 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_NOTIFY_DATA_CHANGE)] =
56 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleNotifyDataChange(data, reply); };
57 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_NOTIFY_EVENT_CHANGE)] =
58 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleNotifyEventChange(data, reply); };
59 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_ENABLE_CLOUD)] =
60 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleEnableCloud(data, reply); };
61 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DISABLE_CLOUD)] =
62 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleDisableCloud(data, reply); };
63 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_DOWNLOAD_FILE)] =
64 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleStartDownloadFile(data, reply); };
65 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_DOWNLOAD_FILE)] =
66 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleStopDownloadFile(data, reply); };
67 opToInterfaceMap_[static_cast<uint32_t>(
68 CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_DOWNLOAD_FILE_CALLBACK)] =
69 [this](MessageParcel &data, MessageParcel &reply) {
70 return this->HandleRegisterDownloadFileCallback(data, reply);
71 };
72 opToInterfaceMap_[static_cast<uint32_t>(
73 CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_FILE_CACHE_CALLBACK)] =
74 [this](MessageParcel &data, MessageParcel &reply) {
75 return this->HandleRegisterFileCacheCallback(data, reply);
76 };
77 opToInterfaceMap_[static_cast<uint32_t>(
78 CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UNREGISTER_DOWNLOAD_FILE_CALLBACK)] =
79 [this](MessageParcel &data, MessageParcel &reply) {
80 return this->HandleUnregisterDownloadFileCallback(data, reply);
81 };
82 opToInterfaceMap_[static_cast<uint32_t>(
83 CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UNREGISTER_FILE_CACHE_CALLBACK)] =
84 [this](MessageParcel &data, MessageParcel &reply) {
85 return this->HandleUnregisterFileCacheCallback(data, reply);
86 };
87 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UPLOAD_ASSET)] =
88 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleUploadAsset(data, reply); };
89 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_FILE)] =
90 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleDownloadFile(data, reply); };
91 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_FILES)] =
92 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleDownloadFiles(data, reply); };
93 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_ASSET)] =
94 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleDownloadAsset(data, reply); };
95 opToInterfaceMap_[static_cast<uint32_t>(
96 CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_DOWNLOAD_ASSET_CALLBACK)] =
97 [this](MessageParcel &data, MessageParcel &reply) {
98 return this->HandleRegisterDownloadAssetCallback(data, reply);
99 };
100 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DELETE_ASSET)] =
101 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleDeleteAsset(data, reply); };
102 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_GET_SYNC_TIME)] =
103 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleGetSyncTime(data, reply); };
104 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_CLEAN_CACHE)] =
105 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleCleanCache(data, reply); };
106 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_FILE_CACHE)] =
107 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleStartFileCache(data, reply); };
108 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_RESET_CURSOR)] =
109 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleResetCursor(data, reply); };
110 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_FILE_CACHE)] =
111 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleStopFileCache(data, reply); };
112 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_THUMB)] =
113 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleDownloadThumb(data, reply); };
114 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_BATCH_CLEAN_FILE)] =
115 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleBatchCleanFile(data, reply); };
116 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_OPTIMIZE_STORAGE)] =
117 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleOptimizeStorage(data, reply); };
118 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DENTRY_FILE_INSERT)] =
119 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleBatchDentryFileInsert(data, reply); };
120 opToInterfaceMap_[static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_OPTIMIZE_STORAGE)] =
121 [this](MessageParcel &data, MessageParcel &reply) { return this->HandleStopOptimizeStorage(data, reply); };
122 }
123
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)124 int32_t CloudSyncServiceStub::OnRemoteRequest(uint32_t code,
125 MessageParcel &data,
126 MessageParcel &reply,
127 MessageOption &option)
128 {
129 DfsuMemoryGuard cacheGuard;
130 TaskStateManager::GetInstance().StartTask();
131 if (data.ReadInterfaceToken() != GetDescriptor()) {
132 return E_SERVICE_DESCRIPTOR_IS_EMPTY;
133 }
134 auto interfaceIndex = opToInterfaceMap_.find(code);
135 if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) {
136 LOGE("Cannot response request %d: unknown tranction", code);
137 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
138 }
139 auto memberFunc = interfaceIndex->second;
140 return memberFunc(data, reply);
141 }
142
HandleUnRegisterCallbackInner(MessageParcel & data,MessageParcel & reply)143 int32_t CloudSyncServiceStub::HandleUnRegisterCallbackInner(MessageParcel &data, MessageParcel &reply)
144 {
145 LOGI("Begin UnRegisterCallbackInner");
146 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
147 LOGE("permission denied");
148 return E_PERMISSION_DENIED;
149 }
150 if (!DfsuAccessTokenHelper::IsSystemApp()) {
151 LOGE("caller hap is not system hap");
152 return E_PERMISSION_SYSTEM;
153 }
154
155 string bundleName = data.ReadString();
156 int32_t res = UnRegisterCallbackInner(bundleName);
157 reply.WriteInt32(res);
158 LOGI("End UnRegisterCallbackInner");
159 return E_OK;
160 }
161
HandleUnRegisterFileSyncCallbackInner(MessageParcel & data,MessageParcel & reply)162 int32_t CloudSyncServiceStub::HandleUnRegisterFileSyncCallbackInner(MessageParcel &data, MessageParcel &reply)
163 {
164 LOGI("Begin HandleUnRegisterFileSyncCallbackInner");
165 string bundleName = data.ReadString();
166 int32_t res = UnRegisterFileSyncCallbackInner(bundleName);
167 reply.WriteInt32(res);
168 LOGI("End HandleUnRegisterFileSyncCallbackInner");
169 return E_OK;
170 }
171
HandleRegisterCallbackInner(MessageParcel & data,MessageParcel & reply)172 int32_t CloudSyncServiceStub::HandleRegisterCallbackInner(MessageParcel &data, MessageParcel &reply)
173 {
174 LOGI("Begin RegisterCallbackInner");
175 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
176 LOGE("permission denied");
177 return E_PERMISSION_DENIED;
178 }
179 if (!DfsuAccessTokenHelper::IsSystemApp()) {
180 LOGE("caller hap is not system hap");
181 return E_PERMISSION_SYSTEM;
182 }
183 auto remoteObj = data.ReadRemoteObject();
184 string bundleName = data.ReadString();
185 int32_t res = RegisterCallbackInner(remoteObj, bundleName);
186 reply.WriteInt32(res);
187 LOGI("End RegisterCallbackInner");
188 return E_OK;
189 }
190
HandleRegisterFileSyncCallbackInner(MessageParcel & data,MessageParcel & reply)191 int32_t CloudSyncServiceStub::HandleRegisterFileSyncCallbackInner(MessageParcel &data, MessageParcel &reply)
192 {
193 LOGI("Begin HandleRegisterFileSyncCallbackInner");
194 auto remoteObj = data.ReadRemoteObject();
195 string bundleName = data.ReadString();
196 int32_t res = RegisterFileSyncCallbackInner(remoteObj, bundleName);
197 reply.WriteInt32(res);
198 LOGI("End HandleRegisterFileSyncCallbackInner");
199 return E_OK;
200 }
201
HandleStartSyncInner(MessageParcel & data,MessageParcel & reply)202 int32_t CloudSyncServiceStub::HandleStartSyncInner(MessageParcel &data, MessageParcel &reply)
203 {
204 LOGI("Begin StartSyncInner");
205 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
206 LOGE("permission denied");
207 return E_PERMISSION_DENIED;
208 }
209 if (!DfsuAccessTokenHelper::IsSystemApp()) {
210 LOGE("caller hap is not system hap");
211 return E_PERMISSION_SYSTEM;
212 }
213 auto forceFlag = data.ReadBool();
214 string bundleName = data.ReadString();
215 int32_t res = StartSyncInner(forceFlag, bundleName);
216 reply.WriteInt32(res);
217 LOGI("End StartSyncInner");
218 return E_OK;
219 }
220
HandleStartFileSyncInner(MessageParcel & data,MessageParcel & reply)221 int32_t CloudSyncServiceStub::HandleStartFileSyncInner(MessageParcel &data, MessageParcel &reply)
222 {
223 LOGI("Begin StartFileSyncInner");
224 auto forceFlag = data.ReadBool();
225 string bundleName = data.ReadString();
226 int32_t res = StartFileSyncInner(forceFlag, bundleName);
227 reply.WriteInt32(res);
228 LOGI("End StartFileSyncInner");
229 return E_OK;
230 }
231
HandleTriggerSyncInner(MessageParcel & data,MessageParcel & reply)232 int32_t CloudSyncServiceStub::HandleTriggerSyncInner(MessageParcel &data, MessageParcel &reply)
233 {
234 LOGI("Begin TriggerSyncInner");
235 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
236 LOGE("permission denied");
237 return E_PERMISSION_DENIED;
238 }
239 if (!DfsuAccessTokenHelper::IsSystemApp()) {
240 LOGE("caller hap is not system hap");
241 return E_PERMISSION_SYSTEM;
242 }
243 string bundleName = data.ReadString();
244 int32_t userId = data.ReadInt32();
245 int32_t res = TriggerSyncInner(bundleName, userId);
246 reply.WriteInt32(res);
247 LOGI("End TriggerSyncInner");
248 return E_OK;
249 }
250
HandleStopSyncInner(MessageParcel & data,MessageParcel & reply)251 int32_t CloudSyncServiceStub::HandleStopSyncInner(MessageParcel &data, MessageParcel &reply)
252 {
253 LOGI("Begin StopSyncInner");
254 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
255 LOGE("permission denied");
256 return E_PERMISSION_DENIED;
257 }
258 if (!DfsuAccessTokenHelper::IsSystemApp()) {
259 LOGE("caller hap is not system hap");
260 return E_PERMISSION_SYSTEM;
261 }
262
263 string bundleName = data.ReadString();
264 bool forceFlag = data.ReadBool();
265 int32_t res = StopSyncInner(bundleName, forceFlag);
266 reply.WriteInt32(res);
267 LOGI("End StopSyncInner");
268 return E_OK;
269 }
270
HandleStopFileSyncInner(MessageParcel & data,MessageParcel & reply)271 int32_t CloudSyncServiceStub::HandleStopFileSyncInner(MessageParcel &data, MessageParcel &reply)
272 {
273 LOGI("Begin StopFileSyncInner");
274 string bundleName = data.ReadString();
275 bool forceFlag = data.ReadBool();
276 int32_t res = StopFileSyncInner(bundleName, forceFlag);
277 reply.WriteInt32(res);
278 LOGI("End StopFileSyncInner");
279 return E_OK;
280 }
281
HandleResetCursor(MessageParcel & data,MessageParcel & reply)282 int32_t CloudSyncServiceStub::HandleResetCursor(MessageParcel &data, MessageParcel &reply)
283 {
284 LOGI("Begin ResetCursor");
285 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
286 LOGE("permission denied");
287 return E_PERMISSION_DENIED;
288 }
289 if (!DfsuAccessTokenHelper::IsSystemApp()) {
290 LOGE("caller hap is not system hap");
291 return E_PERMISSION_SYSTEM;
292 }
293
294 string bundleName = data.ReadString();
295 int32_t res = ResetCursor(bundleName);
296 reply.WriteInt32(res);
297 LOGI("End ResetCursor");
298 return E_OK;
299 }
300
HandleOptimizeStorage(MessageParcel & data,MessageParcel & reply)301 int32_t CloudSyncServiceStub::HandleOptimizeStorage(MessageParcel &data, MessageParcel &reply)
302 {
303 LOGI("Begin HandleOptimizeStorage");
304 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
305 LOGE("permission denied");
306 return E_PERMISSION_DENIED;
307 }
308 if (!DfsuAccessTokenHelper::IsSystemApp()) {
309 LOGE("caller hap is not system hap");
310 return E_PERMISSION_SYSTEM;
311 }
312
313 sptr<OptimizeSpaceOptions> options = data.ReadParcelable<OptimizeSpaceOptions>();
314 if (!options) {
315 LOGE("object of OptimizeSpaceOptions is nullptr");
316 return E_INVAL_ARG;
317 }
318
319 bool isCallbackValid = data.ReadBool();
320 sptr<IRemoteObject> optimizeCallback = nullptr;
321 if (isCallbackValid) {
322 optimizeCallback = data.ReadRemoteObject();
323 }
324
325 int32_t res = OptimizeStorage(*options, isCallbackValid, optimizeCallback);
326 reply.WriteInt32(res);
327 LOGI("End HandleOptimizeStorage");
328 return E_OK;
329 }
330
HandleStopOptimizeStorage(MessageParcel & data,MessageParcel & reply)331 int32_t CloudSyncServiceStub::HandleStopOptimizeStorage(MessageParcel &data, MessageParcel &reply)
332 {
333 LOGI("Begin HandleStopOptimizeStorage");
334 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
335 LOGE("permission denied");
336 return E_PERMISSION_DENIED;
337 }
338
339 if (!DfsuAccessTokenHelper::IsSystemApp()) {
340 LOGE("caller hap is not system hap");
341 return E_PERMISSION_SYSTEM;
342 }
343
344 int32_t res = StopOptimizeStorage();
345 reply.WriteInt32(res);
346 LOGI("End HandleStopOptimizeStorage");
347 return E_OK;
348 }
349
HandleChangeAppSwitch(MessageParcel & data,MessageParcel & reply)350 int32_t CloudSyncServiceStub::HandleChangeAppSwitch(MessageParcel &data, MessageParcel &reply)
351 {
352 LOGI("Begin ChangeAppSwitch");
353 if (!DfsuAccessTokenHelper::IsSystemApp()) {
354 LOGE("caller hap is not system hap");
355 return E_PERMISSION_SYSTEM;
356 }
357 string accountId = data.ReadString();
358 string bundleName = data.ReadString();
359 bool status = data.ReadBool();
360 int32_t res = ChangeAppSwitch(accountId, bundleName, status);
361 reply.WriteInt32(res);
362 LOGI("End ChangeAppSwitch");
363 return E_OK;
364 }
365
HandleClean(MessageParcel & data,MessageParcel & reply)366 int32_t CloudSyncServiceStub::HandleClean(MessageParcel &data, MessageParcel &reply)
367 {
368 LOGI("Begin Clean");
369 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC_MANAGER)) {
370 LOGE("permission denied");
371 return E_PERMISSION_DENIED;
372 }
373 if (!DfsuAccessTokenHelper::IsSystemApp()) {
374 LOGE("caller hap is not system hap");
375 return E_PERMISSION_SYSTEM;
376 }
377 string accountId = data.ReadString();
378 sptr<CleanOptions> options = data.ReadParcelable<CleanOptions>();
379 if (!options) {
380 LOGE("object of CleanOptions is nullptr");
381 return E_INVAL_ARG;
382 }
383 int32_t res = Clean(accountId, *options);
384 reply.WriteInt32(res);
385 LOGI("End Clean");
386 return E_OK;
387 }
388
HandleNotifyDataChange(MessageParcel & data,MessageParcel & reply)389 int32_t CloudSyncServiceStub::HandleNotifyDataChange(MessageParcel &data, MessageParcel &reply)
390 {
391 LOGI("Begin NotifyDataChange");
392 if (!DfsuAccessTokenHelper::IsSystemApp()) {
393 LOGE("caller hap is not system hap");
394 return E_PERMISSION_SYSTEM;
395 }
396 string accountId = data.ReadString();
397 string bundleName = data.ReadString();
398 int32_t res = NotifyDataChange(accountId, bundleName);
399 reply.WriteInt32(res);
400 LOGI("End NotifyDataChange");
401 return E_OK;
402 }
403
HandleNotifyEventChange(MessageParcel & data,MessageParcel & reply)404 int32_t CloudSyncServiceStub::HandleNotifyEventChange(MessageParcel &data, MessageParcel &reply)
405 {
406 LOGI("Begin NotifyEventChange");
407 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC_MANAGER)) {
408 LOGE("permission denied");
409 return E_PERMISSION_DENIED;
410 }
411 if (!DfsuAccessTokenHelper::IsSystemApp()) {
412 LOGE("caller hap is not system hap");
413 return E_PERMISSION_SYSTEM;
414 }
415 int32_t userId = data.ReadInt32();
416 string eventIdStr = data.ReadString();
417 string extraDataStr = data.ReadString();
418
419 int32_t res = NotifyEventChange(userId, eventIdStr, extraDataStr);
420 reply.WriteInt32(res);
421 LOGI("End NotifyEventChange");
422 return E_OK;
423 }
424
HandleDisableCloud(MessageParcel & data,MessageParcel & reply)425 int32_t CloudSyncServiceStub::HandleDisableCloud(MessageParcel &data, MessageParcel &reply)
426 {
427 LOGI("Begin DisableCloud");
428 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC_MANAGER)) {
429 LOGE("permission denied");
430 return E_PERMISSION_DENIED;
431 }
432 if (!DfsuAccessTokenHelper::IsSystemApp()) {
433 LOGE("caller hap is not system hap");
434 return E_PERMISSION_SYSTEM;
435 }
436 string accountId = data.ReadString();
437 int32_t res = DisableCloud(accountId);
438 reply.WriteInt32(res);
439 LOGI("End DisableCloud");
440 return E_OK;
441 }
HandleEnableCloud(MessageParcel & data,MessageParcel & reply)442 int32_t CloudSyncServiceStub::HandleEnableCloud(MessageParcel &data, MessageParcel &reply)
443 {
444 LOGI("Begin EnableCloud");
445 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC_MANAGER)) {
446 LOGE("permission denied");
447 return E_PERMISSION_DENIED;
448 }
449 if (!DfsuAccessTokenHelper::IsSystemApp()) {
450 LOGE("caller hap is not system hap");
451 return E_PERMISSION_SYSTEM;
452 }
453 string accountId = data.ReadString();
454 sptr<SwitchDataObj> switchObj = data.ReadParcelable<SwitchDataObj>();
455 if (!switchObj) {
456 LOGE("object of SwitchDataObj is nullptr");
457 return E_INVAL_ARG;
458 }
459 int32_t res = EnableCloud(accountId, *switchObj);
460 reply.WriteInt32(res);
461 LOGI("End EnableCloud");
462 return E_OK;
463 }
464
HandleStartDownloadFile(MessageParcel & data,MessageParcel & reply)465 int32_t CloudSyncServiceStub::HandleStartDownloadFile(MessageParcel &data, MessageParcel &reply)
466 {
467 LOGI("Begin HandleStartDownloadFile");
468 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
469 LOGE("permission denied");
470 return E_PERMISSION_DENIED;
471 }
472 if (!DfsuAccessTokenHelper::IsSystemApp()) {
473 LOGE("caller hap is not system hap");
474 return E_PERMISSION_SYSTEM;
475 }
476 string path = data.ReadString();
477
478 int32_t res = StartDownloadFile(path);
479 reply.WriteInt32(res);
480 LOGI("End HandleStartDownloadFile");
481 return E_OK;
482 }
483
HandleStartFileCache(MessageParcel & data,MessageParcel & reply)484 int32_t CloudSyncServiceStub::HandleStartFileCache(MessageParcel &data, MessageParcel &reply)
485 {
486 LOGI("Begin HandleStartFileCache");
487 std::vector<std::string> pathVec;
488 if (!data.ReadStringVector(&pathVec)) {
489 LOGE("Failed to get the cloud id.");
490 return E_INVAL_ARG;
491 }
492 int32_t fieldkey = data.ReadInt32();
493
494 bool isCallbackValid = data.ReadBool();
495
496 sptr<IRemoteObject> downloadCallback = nullptr;
497 if (isCallbackValid) {
498 downloadCallback = data.ReadRemoteObject();
499 }
500
501 int32_t timeout = data.ReadInt32();
502
503 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_AUTH_URI)) {
504 for (auto &uri : pathVec) {
505 if (!DfsuAccessTokenHelper::CheckUriPermission(uri)) {
506 LOGE("permission denied");
507 return E_PERMISSION_DENIED;
508 }
509 }
510 }
511 int64_t downloadId = 0;
512 int32_t res = StartFileCache(pathVec, downloadId, fieldkey, isCallbackValid, downloadCallback, timeout);
513 reply.WriteInt64(downloadId);
514 reply.WriteInt32(res);
515 LOGI("End HandleStartFileCache");
516 return E_OK;
517 }
518
HandleStopDownloadFile(MessageParcel & data,MessageParcel & reply)519 int32_t CloudSyncServiceStub::HandleStopDownloadFile(MessageParcel &data, MessageParcel &reply)
520 {
521 LOGI("Begin HandleStopDownloadFile");
522 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
523 LOGE("permission denied");
524 return E_PERMISSION_DENIED;
525 }
526 if (!DfsuAccessTokenHelper::IsSystemApp()) {
527 LOGE("caller hap is not system hap");
528 return E_PERMISSION_SYSTEM;
529 }
530 string path = data.ReadString();
531 bool needClean = data.ReadBool();
532
533 int32_t res = StopDownloadFile(path, needClean);
534 reply.WriteInt32(res);
535 LOGI("End HandleStopDownloadFile");
536 return E_OK;
537 }
538
HandleStopFileCache(MessageParcel & data,MessageParcel & reply)539 int32_t CloudSyncServiceStub::HandleStopFileCache(MessageParcel &data, MessageParcel &reply)
540 {
541 LOGI("Begin HandleStopFileCache");
542 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_AUTH_URI)) {
543 LOGE("permission denied");
544 return E_PERMISSION_DENIED;
545 }
546 int64_t downloadId = data.ReadInt64();
547 bool needClean = data.ReadBool();
548 int32_t timeout = data.ReadInt32();
549
550 int32_t res = StopFileCache(downloadId, needClean, timeout);
551 reply.WriteInt32(res);
552 LOGI("End HandleStopFileCache");
553 return E_OK;
554 }
555
HandleDownloadThumb(MessageParcel & data,MessageParcel & reply)556 int32_t CloudSyncServiceStub::HandleDownloadThumb(MessageParcel &data, MessageParcel &reply)
557 {
558 LOGI("Begin HandleDownloadThumb");
559 if (!DfsuAccessTokenHelper::IsSystemApp()) {
560 LOGE("caller hap is not system hap");
561 return E_PERMISSION_SYSTEM;
562 }
563 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
564 LOGE("permission denied");
565 return E_PERMISSION_DENIED;
566 }
567 int32_t res = DownloadThumb();
568 reply.WriteInt32(res);
569 LOGI("End HandleDownloadThumb");
570 return E_OK;
571 }
572
HandleRegisterDownloadFileCallback(MessageParcel & data,MessageParcel & reply)573 int32_t CloudSyncServiceStub::HandleRegisterDownloadFileCallback(MessageParcel &data, MessageParcel &reply)
574 {
575 LOGI("Begin HandleRegisterDownloadFileCallback");
576 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
577 LOGE("permission denied");
578 return E_PERMISSION_DENIED;
579 }
580 if (!DfsuAccessTokenHelper::IsSystemApp()) {
581 LOGE("caller hap is not system hap");
582 return E_PERMISSION_SYSTEM;
583 }
584
585 auto downloadCallback = data.ReadRemoteObject();
586
587 int32_t res = RegisterDownloadFileCallback(downloadCallback);
588 reply.WriteInt32(res);
589 LOGI("End HandleRegisterDownloadFileCallback");
590 return E_OK;
591 }
592
HandleRegisterFileCacheCallback(MessageParcel & data,MessageParcel & reply)593 int32_t CloudSyncServiceStub::HandleRegisterFileCacheCallback(MessageParcel &data, MessageParcel &reply)
594 {
595 LOGI("Begin HandleRegisterFileCacheCallback");
596 auto downloadCallback = data.ReadRemoteObject();
597
598 int32_t res = RegisterFileCacheCallback(downloadCallback);
599 reply.WriteInt32(res);
600 LOGI("End HandleRegisterFileCacheCallback");
601 return E_OK;
602 }
603
HandleUnregisterDownloadFileCallback(MessageParcel & data,MessageParcel & reply)604 int32_t CloudSyncServiceStub::HandleUnregisterDownloadFileCallback(MessageParcel &data, MessageParcel &reply)
605 {
606 LOGI("Begin HandleUnregisterDownloadFileCallback");
607 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
608 LOGE("permission denied");
609 return E_PERMISSION_DENIED;
610 }
611 if (!DfsuAccessTokenHelper::IsSystemApp()) {
612 LOGE("caller hap is not system hap");
613 return E_PERMISSION_SYSTEM;
614 }
615
616 int32_t res = UnregisterDownloadFileCallback();
617 reply.WriteInt32(res);
618 LOGI("End HandleUnregisterDownloadFileCallback");
619 return E_OK;
620 }
621
HandleUnregisterFileCacheCallback(MessageParcel & data,MessageParcel & reply)622 int32_t CloudSyncServiceStub::HandleUnregisterFileCacheCallback(MessageParcel &data, MessageParcel &reply)
623 {
624 LOGI("Begin HandleUnregisterFileCacheCallback");
625 int32_t res = UnregisterFileCacheCallback();
626 reply.WriteInt32(res);
627 LOGI("End HandleUnregisterFileCacheCallback");
628 return E_OK;
629 }
630
HandleUploadAsset(MessageParcel & data,MessageParcel & reply)631 int32_t CloudSyncServiceStub::HandleUploadAsset(MessageParcel &data, MessageParcel &reply)
632 {
633 LOGI("Begin UploadAsset");
634 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
635 LOGE("permission denied");
636 return E_PERMISSION_DENIED;
637 }
638 if (!DfsuAccessTokenHelper::IsSystemApp()) {
639 LOGE("caller hap is not system hap");
640 return E_PERMISSION_SYSTEM;
641 }
642 int32_t userId = data.ReadInt32();
643 string request = data.ReadString();
644 string result;
645 int32_t res = UploadAsset(userId, request, result);
646 reply.WriteInt32(res);
647 reply.WriteString(result);
648 LOGI("End UploadAsset");
649 return E_OK;
650 }
651
HandleDownloadFile(MessageParcel & data,MessageParcel & reply)652 int32_t CloudSyncServiceStub::HandleDownloadFile(MessageParcel &data, MessageParcel &reply)
653 {
654 LOGI("Begin DownloadFile");
655 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
656 LOGE("permission denied");
657 return E_PERMISSION_DENIED;
658 }
659 if (!DfsuAccessTokenHelper::IsSystemApp()) {
660 LOGE("caller hap is not system hap");
661 return E_PERMISSION_SYSTEM;
662 }
663 int32_t userId = data.ReadInt32();
664 string bundleName = data.ReadString();
665 sptr<AssetInfoObj> assetInfoObj = data.ReadParcelable<AssetInfoObj>();
666 if (!assetInfoObj) {
667 LOGE("object of AssetInfoObj is nullptr");
668 return E_INVAL_ARG;
669 }
670 int32_t res = DownloadFile(userId, bundleName, *assetInfoObj);
671 reply.WriteInt32(res);
672 LOGI("End DownloadFile");
673 return E_OK;
674 }
675
HandleDownloadFiles(MessageParcel & data,MessageParcel & reply)676 int32_t CloudSyncServiceStub::HandleDownloadFiles(MessageParcel &data, MessageParcel &reply)
677 {
678 LOGI("Begin DownloadFile");
679 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
680 LOGE("permission denied");
681 return E_PERMISSION_DENIED;
682 }
683 if (!DfsuAccessTokenHelper::IsSystemApp()) {
684 LOGE("caller hap is not system hap");
685 return E_PERMISSION_SYSTEM;
686 }
687 int32_t userId = data.ReadInt32();
688 string bundleName = data.ReadString();
689 int32_t size = data.ReadInt32();
690 std::vector<AssetInfoObj> assetInfoObj;
691 if (size > READ_SIZE) {
692 return E_INVAL_ARG;
693 }
694 for (int i = 0; i < size; i++) {
695 sptr<AssetInfoObj> obj = data.ReadParcelable<AssetInfoObj>();
696 if (!obj) {
697 LOGE("object of obj is nullptr");
698 return E_INVAL_ARG;
699 }
700 assetInfoObj.emplace_back(*obj);
701 }
702
703 std::vector<bool> assetResultMap;
704 int32_t res = DownloadFiles(userId, bundleName, assetInfoObj, assetResultMap);
705 reply.WriteBoolVector(assetResultMap);
706 reply.WriteInt32(res);
707 LOGI("End DownloadFiles");
708 return E_OK;
709 }
710
HandleBatchCleanFile(MessageParcel & data,MessageParcel & reply)711 int32_t CloudSyncServiceStub::HandleBatchCleanFile(MessageParcel &data, MessageParcel &reply)
712 {
713 LOGI("Begin HandleBatchCleanFile");
714 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
715 LOGE("permission denied");
716 return E_PERMISSION_DENIED;
717 }
718 if (!DfsuAccessTokenHelper::IsSystemApp()) {
719 LOGE("caller hap is not system hap");
720 return E_PERMISSION_SYSTEM;
721 }
722
723 int32_t size = data.ReadInt32();
724 if (size > CLEAN_FILE_MAX_SIZE) {
725 LOGE("size over max limit");
726 return E_INVAL_ARG;
727 }
728 std::vector<CleanFileInfoObj> deleteInfoObj;
729 for (int i = 0; i < size; i++) {
730 sptr<CleanFileInfoObj> obj = data.ReadParcelable<CleanFileInfoObj>();
731 if (!obj) {
732 LOGE("object of obj is nullptr");
733 return E_INVAL_ARG;
734 }
735 deleteInfoObj.emplace_back(*obj);
736 }
737
738 std::vector<std::string> failCloudId;
739 int32_t res = BatchCleanFile(deleteInfoObj, failCloudId);
740 reply.WriteStringVector(failCloudId);
741 reply.WriteInt32(res);
742 LOGI("End HandleBatchCleanFile");
743 return E_OK;
744 }
745
HandleDownloadAsset(MessageParcel & data,MessageParcel & reply)746 int32_t CloudSyncServiceStub::HandleDownloadAsset(MessageParcel &data, MessageParcel &reply)
747 {
748 LOGI("Begin DownloadAsset");
749 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
750 LOGE("permission denied");
751 return E_PERMISSION_DENIED;
752 }
753 if (!DfsuAccessTokenHelper::IsSystemApp()) {
754 LOGE("caller hap is not system hap");
755 return E_PERMISSION_SYSTEM;
756 }
757 uint64_t taskId = data.ReadUint64();
758 int32_t userId = data.ReadInt32();
759 string bundleName = data.ReadString();
760 string networkId = data.ReadString();
761 sptr<AssetInfoObj> assetInfoObj = data.ReadParcelable<AssetInfoObj>();
762 if (!assetInfoObj) {
763 LOGE("object of AssetInfoObj is nullptr");
764 return E_INVAL_ARG;
765 }
766 int32_t res = DownloadAsset(taskId, userId, bundleName, networkId, *assetInfoObj);
767 reply.WriteInt32(res);
768 LOGI("End DownloadAsset");
769 return E_OK;
770 }
771
HandleRegisterDownloadAssetCallback(MessageParcel & data,MessageParcel & reply)772 int32_t CloudSyncServiceStub::HandleRegisterDownloadAssetCallback(MessageParcel &data, MessageParcel &reply)
773 {
774 LOGI("Begin RegisterDownloadAssetCallback");
775 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
776 LOGE("permission denied");
777 return E_PERMISSION_DENIED;
778 }
779 if (!DfsuAccessTokenHelper::IsSystemApp()) {
780 LOGE("caller hap is not system hap");
781 return E_PERMISSION_SYSTEM;
782 }
783 auto remoteObj = data.ReadRemoteObject();
784 int32_t res = RegisterDownloadAssetCallback(remoteObj);
785 reply.WriteInt32(res);
786 LOGI("End RegisterDownloadAssetCallback");
787 return E_OK;
788 }
789
HandleDeleteAsset(MessageParcel & data,MessageParcel & reply)790 int32_t CloudSyncServiceStub::HandleDeleteAsset(MessageParcel &data, MessageParcel &reply)
791 {
792 LOGI("Begin DeleteAsset");
793 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
794 LOGE("permission denied");
795 return E_PERMISSION_DENIED;
796 }
797 if (!DfsuAccessTokenHelper::IsSystemApp()) {
798 LOGE("caller hap is not system hap");
799 return E_PERMISSION_SYSTEM;
800 }
801 int32_t userId = data.ReadInt32();
802 string uri = data.ReadString();
803 int32_t res = DeleteAsset(userId, uri);
804 reply.WriteInt32(res);
805 reply.WriteString(uri);
806 LOGI("End DeleteAsset");
807 return E_OK;
808 }
809
HandleGetSyncTime(MessageParcel & data,MessageParcel & reply)810 int32_t CloudSyncServiceStub::HandleGetSyncTime(MessageParcel &data, MessageParcel &reply)
811 {
812 LOGI("Begin GetSyncTime");
813 int64_t syncTime = 0;
814 string bundleName = data.ReadString();
815 int32_t res = GetSyncTimeInner(syncTime, bundleName);
816 reply.WriteInt64(syncTime);
817 reply.WriteInt32(res);
818 return E_OK;
819 }
820
HandleBatchDentryFileInsert(MessageParcel & data,MessageParcel & reply)821 int32_t CloudSyncServiceStub::HandleBatchDentryFileInsert(MessageParcel &data, MessageParcel &reply)
822 {
823 LOGI("Begin HandleBatchDentryFileInsert");
824 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
825 LOGE("permission denied");
826 return E_PERMISSION_DENIED;
827 }
828 if (!DfsuAccessTokenHelper::IsSystemApp()) {
829 LOGE("caller hap is not system hap");
830 return E_PERMISSION_SYSTEM;
831 }
832
833 int32_t size = data.ReadInt32();
834 std::vector<DentryFileInfoObj> dentryFileInfoObj;
835 if (size > MAX_READ_DENTRY_FILE_SIZE) {
836 return E_INVAL_ARG;
837 }
838 for (int i = 0; i < size; i++) {
839 sptr<DentryFileInfoObj> obj = data.ReadParcelable<DentryFileInfoObj>();
840 if (!obj) {
841 LOGE("object of obj is nullptr");
842 return E_INVAL_ARG;
843 }
844 dentryFileInfoObj.emplace_back(*obj);
845 }
846
847 std::vector<std::string> failCloudId;
848 int32_t res = BatchDentryFileInsert(dentryFileInfoObj, failCloudId);
849 reply.WriteStringVector(failCloudId);
850 reply.WriteInt32(res);
851 LOGI("End HandleBatchDentryFileInsert");
852 return E_OK;
853 }
854
HandleCleanCache(MessageParcel & data,MessageParcel & reply)855 int32_t CloudSyncServiceStub::HandleCleanCache(MessageParcel &data, MessageParcel &reply)
856 {
857 LOGI("Begin HandleCleanCache");
858 if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) {
859 LOGE("permission denied");
860 return E_PERMISSION_DENIED;
861 }
862 if (!DfsuAccessTokenHelper::IsSystemApp()) {
863 LOGE("caller hap is not system hap");
864 return E_PERMISSION_SYSTEM;
865 }
866
867 string uri = data.ReadString();
868 int32_t res = CleanCacheInner(uri);
869
870 reply.WriteInt32(res);
871 return E_OK;
872 }
873 } // namespace OHOS::FileManagement::CloudSync
874