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