• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "cloud_sync_service_proxy.h"
16 
17 #include "cloud_file_sync_service_interface_code.h"
18 
19 #include <sstream>
20 
21 #include "dfs_error.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 #include "utils_log.h"
25 
26 namespace OHOS::FileManagement::CloudSync {
27 using namespace std;
28 
29 constexpr int LOAD_SA_TIMEOUT_MS = 2000;
30 static const int MAX_WRITE_DENTRY_FILE_SIZE = 500;
31 
UnRegisterCallbackInner(const std::string & bundleName)32 int32_t CloudSyncServiceProxy::UnRegisterCallbackInner(const std::string &bundleName)
33 {
34     LOGI("Start UnRegisterCallbackInner");
35     MessageParcel data;
36     MessageParcel reply;
37     MessageOption option;
38 
39     if (!data.WriteInterfaceToken(GetDescriptor())) {
40         LOGE("Failed to write interface token");
41         return E_BROKEN_IPC;
42     }
43 
44     auto remote = Remote();
45     if (!remote) {
46         LOGE("remote is nullptr");
47         return E_BROKEN_IPC;
48     }
49 
50     if (!data.WriteString(bundleName)) {
51         LOGE("Failed to send the bundle name");
52         return E_INVAL_ARG;
53     }
54 
55     int32_t ret = remote->SendRequest(
56         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UNREGISTER_CALLBACK), data, reply, option);
57     if (ret != E_OK) {
58         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
59         return E_BROKEN_IPC;
60     }
61     LOGI("UnRegisterCallbackInner Success");
62     return reply.ReadInt32();
63 }
64 
UnRegisterFileSyncCallbackInner(const std::string & bundleName)65 int32_t CloudSyncServiceProxy::UnRegisterFileSyncCallbackInner(const std::string &bundleName)
66 {
67     LOGI("Start UnRegisterFileSyncCallbackInner");
68     MessageParcel data;
69     MessageParcel reply;
70     MessageOption option;
71 
72     if (!data.WriteInterfaceToken(GetDescriptor())) {
73         LOGE("Failed to write interface token");
74         return E_BROKEN_IPC;
75     }
76 
77     auto remote = Remote();
78     if (!remote) {
79         LOGE("remote is nullptr");
80         return E_BROKEN_IPC;
81     }
82 
83     if (!data.WriteString(bundleName)) {
84         LOGE("Failed to send the bundle name");
85         return E_INVAL_ARG;
86     }
87 
88     int32_t ret = remote->SendRequest(
89         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UNREGISTER_FILE_SYNC_CALLBACK),
90         data, reply, option);
91     if (ret != E_OK) {
92         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
93         return ret;
94     }
95     LOGI("UnRegisterFileSyncCallbackInner Success");
96     return reply.ReadInt32();
97 }
98 
RegisterCallbackInner(const sptr<IRemoteObject> & remoteObject,const std::string & bundleName)99 int32_t CloudSyncServiceProxy::RegisterCallbackInner(const sptr<IRemoteObject> &remoteObject,
100                                                      const std::string &bundleName)
101 {
102     LOGI("Start RegisterCallbackInner");
103     MessageParcel data;
104     MessageParcel reply;
105     MessageOption option;
106 
107     if (!remoteObject) {
108         LOGI("Empty callback stub");
109         return E_INVAL_ARG;
110     }
111 
112     if (!data.WriteInterfaceToken(GetDescriptor())) {
113         LOGE("Failed to write interface token");
114         return E_BROKEN_IPC;
115     }
116 
117     if (!data.WriteRemoteObject(remoteObject)) {
118         LOGE("Failed to send the callback stub");
119         return E_INVAL_ARG;
120     }
121 
122     if (!data.WriteString(bundleName)) {
123         LOGE("Failed to send the bundle name");
124         return E_INVAL_ARG;
125     }
126 
127     auto remote = Remote();
128     if (!remote) {
129         LOGE("remote is nullptr");
130         return E_BROKEN_IPC;
131     }
132     int32_t ret = remote->SendRequest(
133         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_CALLBACK), data, reply, option);
134     if (ret != E_OK) {
135         LOGE("Failed to send out the request, errno: %{public}d", ret);
136         return E_BROKEN_IPC;
137     }
138     LOGI("RegisterCallbackInner Success");
139     return reply.ReadInt32();
140 }
141 
RegisterFileSyncCallbackInner(const sptr<IRemoteObject> & remoteObject,const std::string & bundleName)142 int32_t CloudSyncServiceProxy::RegisterFileSyncCallbackInner(const sptr<IRemoteObject> &remoteObject,
143     const std::string &bundleName)
144 {
145     LOGI("Start RegisterFileSyncCallbackInner");
146     MessageParcel data;
147     MessageParcel reply;
148     MessageOption option;
149 
150     if (!remoteObject) {
151         LOGI("Empty callback stub");
152         return E_INVAL_ARG;
153     }
154 
155     if (!data.WriteInterfaceToken(GetDescriptor())) {
156         LOGE("Failed to write interface token");
157         return E_BROKEN_IPC;
158     }
159 
160     if (!data.WriteRemoteObject(remoteObject)) {
161         LOGE("Failed to send the callback stub");
162         return E_INVAL_ARG;
163     }
164 
165     if (!data.WriteString(bundleName)) {
166         LOGE("Failed to send the bundle name");
167         return E_INVAL_ARG;
168     }
169 
170     auto remote = Remote();
171     if (!remote) {
172         LOGE("remote is nullptr");
173         return E_BROKEN_IPC;
174     }
175     int32_t ret = remote->SendRequest(
176         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_FILE_SYNC_CALLBACK),
177         data, reply, option);
178     if (ret != E_OK) {
179         LOGE("Failed to send out the request, errno: %{public}d", ret);
180         return ret;
181     }
182     LOGI("RegisterFileSyncCallbackInner Success");
183     return reply.ReadInt32();
184 }
185 
StartSyncInner(bool forceFlag,const std::string & bundleName)186 int32_t CloudSyncServiceProxy::StartSyncInner(bool forceFlag, const std::string &bundleName)
187 {
188     LOGI("Start Sync");
189     MessageParcel data;
190     MessageParcel reply;
191     MessageOption option;
192 
193     if (!data.WriteInterfaceToken(GetDescriptor())) {
194         LOGE("Failed to write interface token");
195         return E_BROKEN_IPC;
196     }
197 
198     if (!data.WriteBool(forceFlag)) {
199         LOGE("Failed to send the force flag");
200         return E_INVAL_ARG;
201     }
202 
203     if (!data.WriteString(bundleName)) {
204         LOGE("Failed to send the bundle name");
205         return E_INVAL_ARG;
206     }
207 
208     auto remote = Remote();
209     if (!remote) {
210         LOGE("remote is nullptr");
211         return E_BROKEN_IPC;
212     }
213     int32_t ret = remote->SendRequest(static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_SYNC),
214                                       data, reply, option);
215     if (ret != E_OK) {
216         LOGE("Failed to send out the request, errno: %{public}d", ret);
217         return E_BROKEN_IPC;
218     }
219     LOGI("StartSyncInner Success");
220     return reply.ReadInt32();
221 }
222 
StartFileSyncInner(bool forceFlag,const std::string & bundleName)223 int32_t CloudSyncServiceProxy::StartFileSyncInner(bool forceFlag, const std::string &bundleName)
224 {
225     LOGI("Start StartFileSyncInner");
226     MessageParcel data;
227     MessageParcel reply;
228     MessageOption option;
229 
230     if (!data.WriteInterfaceToken(GetDescriptor())) {
231         LOGE("Failed to write interface token");
232         return E_BROKEN_IPC;
233     }
234 
235     if (!data.WriteBool(forceFlag)) {
236         LOGE("Failed to send the force flag");
237         return E_INVAL_ARG;
238     }
239 
240     if (!data.WriteString(bundleName)) {
241         LOGE("Failed to send the bundle name");
242         return E_INVAL_ARG;
243     }
244 
245     auto remote = Remote();
246     if (!remote) {
247         LOGE("remote is nullptr");
248         return E_BROKEN_IPC;
249     }
250     int32_t ret = remote->SendRequest(static_cast<uint32_t>(
251         CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_FILE_SYNC), data, reply, option);
252     if (ret != E_OK) {
253         LOGE("Failed to send out the request, errno: %{public}d", ret);
254         return ret;
255     }
256     LOGI("StartFileSyncInner Success");
257     return reply.ReadInt32();
258 }
259 
260 
TriggerSyncInner(const std::string & bundleName,const int32_t & userId)261 int32_t CloudSyncServiceProxy::TriggerSyncInner(const std::string &bundleName, const int32_t &userId)
262 {
263     LOGI("Trigger Sync");
264     MessageParcel data;
265     MessageParcel reply;
266     MessageOption option;
267 
268     if (!data.WriteInterfaceToken(GetDescriptor())) {
269         LOGE("Failed to write interface token");
270         return E_BROKEN_IPC;
271     }
272 
273     if (!data.WriteString(bundleName)) {
274         LOGE("Failed to send the bundle name");
275         return E_INVAL_ARG;
276     }
277 
278     if (!data.WriteInt32(userId)) {
279         LOGE("Failed to send the user id");
280         return E_INVAL_ARG;
281     }
282 
283     auto remote = Remote();
284     if (!remote) {
285         LOGE("remote is nullptr");
286         return E_BROKEN_IPC;
287     }
288     int32_t ret = remote->SendRequest(
289         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_TRIGGER_SYNC), data, reply, option);
290     if (ret != E_OK) {
291         LOGE("Failed to send out the request, errno: %{public}d", ret);
292         return E_BROKEN_IPC;
293     }
294     LOGI("TriggerSyncInner Success");
295     return reply.ReadInt32();
296 }
297 
GetSyncTimeInner(int64_t & syncTime,const std::string & bundleName)298 int32_t CloudSyncServiceProxy::GetSyncTimeInner(int64_t &syncTime, const std::string &bundleName)
299 {
300     LOGI("Start GetSyncTimeInner");
301     MessageParcel data;
302     MessageParcel reply;
303     MessageOption option;
304 
305     if (!data.WriteInterfaceToken(GetDescriptor())) {
306         LOGE("Failed to write interface token");
307         return E_BROKEN_IPC;
308     }
309 
310     auto remote = Remote();
311     if (!remote) {
312         LOGE("remote is nullptr");
313         return E_BROKEN_IPC;
314     }
315 
316     if (!data.WriteString(bundleName)) {
317         LOGE("Failed to send the bundle name");
318         return E_INVAL_ARG;
319     }
320 
321     int32_t ret = remote->SendRequest(
322         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_GET_SYNC_TIME), data, reply, option);
323     if (ret != E_OK) {
324         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
325         return E_BROKEN_IPC;
326     }
327 
328     syncTime = reply.ReadInt64();
329 
330     return reply.ReadInt32();
331 }
332 
BatchDentryFileInsert(const std::vector<DentryFileInfoObj> & fileInfo,std::vector<std::string> & failCloudId)333 int32_t CloudSyncServiceProxy::BatchDentryFileInsert(const std::vector<DentryFileInfoObj> &fileInfo,
334     std::vector<std::string> &failCloudId)
335 {
336     LOGI("BatchDentryFileInsert");
337     MessageParcel data;
338     MessageParcel reply;
339     MessageOption option;
340 
341     if (!data.WriteInterfaceToken(GetDescriptor())) {
342         LOGE("Failed to write interface token");
343         return E_BROKEN_IPC;
344     }
345 
346     if (fileInfo.size() > MAX_WRITE_DENTRY_FILE_SIZE || fileInfo.size() == 0 || !data.WriteInt32(fileInfo.size())) {
347         LOGE("Failed to send the vector size");
348         return E_INVAL_ARG;
349     }
350 
351     for (const auto &obj : fileInfo) {
352         if (!data.WriteParcelable(&obj)) {
353             LOGE("Failed to send the fileInfo");
354             return E_INVAL_ARG;
355         }
356     }
357 
358     auto remote = Remote();
359     if (!remote) {
360         LOGE("remote is nullptr");
361         return E_BROKEN_IPC;
362     }
363     int32_t ret = remote->SendRequest(
364         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DENTRY_FILE_INSERT), data, reply, option);
365     if (ret != E_OK) {
366         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
367         return E_BROKEN_IPC;
368     }
369     LOGI("BatchDentryFileInsert Success");
370     reply.ReadStringVector(&failCloudId);
371     return reply.ReadInt32();
372 }
373 
CleanCacheInner(const std::string & uri)374 int32_t CloudSyncServiceProxy::CleanCacheInner(const std::string &uri)
375 {
376     LOGI("Start CleanCacheInner");
377     MessageParcel data;
378     MessageParcel reply;
379     MessageOption option;
380 
381     if (!data.WriteInterfaceToken(GetDescriptor())) {
382         LOGE("Failed to write interface token");
383         return E_BROKEN_IPC;
384     }
385 
386     if (!data.WriteString(uri)) {
387         LOGE("Failed to send the uri");
388         return E_BROKEN_IPC;
389     }
390 
391     auto remote = Remote();
392     if (!remote) {
393         LOGE("remote is nullptr");
394         return E_BROKEN_IPC;
395     }
396     int32_t ret = remote->SendRequest(static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_CLEAN_CACHE),
397                                       data, reply, option);
398     if (ret != E_OK) {
399         LOGE("Failed to send out the request, errno: %{public}d", ret);
400         return E_BROKEN_IPC;
401     }
402 
403     return reply.ReadInt32();
404 }
405 
StopSyncInner(const std::string & bundleName,bool forceFlag)406 int32_t CloudSyncServiceProxy::StopSyncInner(const std::string &bundleName, bool forceFlag)
407 {
408     LOGI("StopSync");
409     MessageParcel data;
410     MessageParcel reply;
411     MessageOption option;
412 
413     if (!data.WriteInterfaceToken(GetDescriptor())) {
414         LOGE("Failed to write interface token");
415         return E_BROKEN_IPC;
416     }
417 
418     auto remote = Remote();
419     if (!remote) {
420         LOGE("remote is nullptr");
421         return E_BROKEN_IPC;
422     }
423 
424     if (!data.WriteString(bundleName)) {
425         LOGE("Failed to send the bundle name");
426         return E_INVAL_ARG;
427     }
428 
429     if (!data.WriteBool(forceFlag)) {
430         LOGE("Failed to send the forceFlag");
431         return E_INVAL_ARG;
432     }
433 
434     int32_t ret = remote->SendRequest(static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_SYNC),
435                                       data, reply, option);
436     if (ret != E_OK) {
437         LOGE("Failed to send out the request, errno: %{public}d", ret);
438         return E_BROKEN_IPC;
439     }
440     LOGI("StopSyncInner Success");
441     return reply.ReadInt32();
442 }
443 
StopFileSyncInner(const std::string & bundleName,bool forceFlag)444 int32_t CloudSyncServiceProxy::StopFileSyncInner(const std::string &bundleName, bool forceFlag)
445 {
446     LOGI("StopFileSyncInner");
447     MessageParcel data;
448     MessageParcel reply;
449     MessageOption option;
450 
451     if (!data.WriteInterfaceToken(GetDescriptor())) {
452         LOGE("Failed to write interface token");
453         return E_BROKEN_IPC;
454     }
455 
456     auto remote = Remote();
457     if (!remote) {
458         LOGE("remote is nullptr");
459         return E_BROKEN_IPC;
460     }
461 
462     if (!data.WriteString(bundleName)) {
463         LOGE("Failed to send the bundle name");
464         return E_INVAL_ARG;
465     }
466 
467     if (!data.WriteBool(forceFlag)) {
468         LOGE("Failed to send the forceFlag");
469         return E_INVAL_ARG;
470     }
471 
472     int32_t ret = remote->SendRequest(
473         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_FILE_SYNC), data, reply, option);
474     if (ret != E_OK) {
475         LOGE("Failed to send out the request, errno: %{public}d", ret);
476         return ret;
477     }
478     LOGI("StopFileSyncInner Success");
479     return reply.ReadInt32();
480 }
481 
ResetCursor(const std::string & bundleName)482 int32_t CloudSyncServiceProxy::ResetCursor(const std::string &bundleName)
483 {
484     LOGI("ResetCursor");
485     MessageParcel data;
486     MessageParcel reply;
487     MessageOption option;
488 
489     if (!data.WriteInterfaceToken(GetDescriptor())) {
490         LOGE("Failed to write interface token");
491         return E_BROKEN_IPC;
492     }
493 
494     auto remote = Remote();
495     if (!remote) {
496         LOGE("remote is nullptr");
497         return E_BROKEN_IPC;
498     }
499 
500     if (!data.WriteString(bundleName)) {
501         LOGE("Failed to send the bundle name");
502         return E_INVAL_ARG;
503     }
504 
505     int32_t ret = remote->SendRequest(
506         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_RESET_CURSOR), data, reply, option);
507     if (ret != E_OK) {
508         LOGE("Failed to send out the request, errno: %{public}d", ret);
509         return E_BROKEN_IPC;
510     }
511     LOGI("ResetCursor Success");
512     return reply.ReadInt32();
513 }
514 
ChangeAppSwitch(const std::string & accoutId,const std::string & bundleName,bool status)515 int32_t CloudSyncServiceProxy::ChangeAppSwitch(const std::string &accoutId, const std::string &bundleName, bool status)
516 {
517     LOGI("ChangeAppSwitch");
518     MessageParcel data;
519     MessageParcel reply;
520     MessageOption option;
521 
522     if (!data.WriteInterfaceToken(GetDescriptor())) {
523         LOGE("Failed to write interface token");
524         return E_BROKEN_IPC;
525     }
526 
527     if (!data.WriteString(accoutId)) {
528         LOGE("Failed to send the account id");
529         return E_INVAL_ARG;
530     }
531 
532     if (!data.WriteString(bundleName)) {
533         LOGE("Failed to send the bundle name");
534         return E_INVAL_ARG;
535     }
536 
537     if (!data.WriteBool(status)) {
538         LOGE("Failed to send the switch status");
539         return E_INVAL_ARG;
540     }
541 
542     auto remote = Remote();
543     if (!remote) {
544         LOGE("remote is nullptr");
545         return E_BROKEN_IPC;
546     }
547     int32_t ret = remote->SendRequest(
548         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_CHANGE_APP_SWITCH), data, reply, option);
549     if (ret != E_OK) {
550         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
551         return E_BROKEN_IPC;
552     }
553     LOGI("ChangeAppSwitch Success");
554     return reply.ReadInt32();
555 }
556 
OptimizeStorage(const OptimizeSpaceOptions & optimizeOptions,bool isCallbackValid,const sptr<IRemoteObject> & optimizeCallback)557 int32_t CloudSyncServiceProxy::OptimizeStorage(const OptimizeSpaceOptions &optimizeOptions, bool isCallbackValid,
558     const sptr<IRemoteObject> &optimizeCallback)
559 {
560     LOGI("OptimizeStorage");
561     MessageParcel data;
562     MessageParcel reply;
563     MessageOption option;
564 
565     if (!data.WriteInterfaceToken(GetDescriptor())) {
566         LOGE("Failed to write interface token");
567         return E_BROKEN_IPC;
568     }
569 
570     if (!data.WriteParcelable(&optimizeOptions)) {
571         LOGE("failed to write cleanOptions");
572         return E_INVAL_ARG;
573     }
574 
575     if (!data.WriteBool(isCallbackValid)) {
576         LOGE("Failed to send the isCallbackValid flag");
577         return E_INVAL_ARG;
578     }
579 
580     if (isCallbackValid) {
581         if (!data.WriteRemoteObject(optimizeCallback)) {
582             LOGE("Failed to send the callback stub");
583             return E_INVAL_ARG;
584         }
585     }
586 
587     auto remote = Remote();
588     if (!remote) {
589         LOGE("remote is nullptr");
590         return E_BROKEN_IPC;
591     }
592     int32_t ret = remote->SendRequest(
593         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_OPTIMIZE_STORAGE), data, reply, option);
594     if (ret != E_OK) {
595         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
596         return E_BROKEN_IPC;
597     }
598     LOGI("OptimizeStorage Success");
599     return reply.ReadInt32();
600 }
601 
StopOptimizeStorage()602 int32_t CloudSyncServiceProxy::StopOptimizeStorage()
603 {
604     LOGI("StopOptimizeStorage Start");
605     MessageParcel data;
606     MessageParcel reply;
607     MessageOption option;
608 
609     if (!data.WriteInterfaceToken(GetDescriptor())) {
610         LOGE("Failed to write interface token");
611         return E_BROKEN_IPC;
612     }
613 
614     auto remote = Remote();
615     if (!remote) {
616         LOGE("remote is nullptr");
617         return E_BROKEN_IPC;
618     }
619     int32_t ret = remote->SendRequest(
620         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_OPTIMIZE_STORAGE), data,
621         reply, option);
622     if (ret != E_OK) {
623         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
624         return E_BROKEN_IPC;
625     }
626     LOGI("StopOptimizeStorage Success");
627     return reply.ReadInt32();
628 }
629 
Clean(const std::string & accountId,const CleanOptions & cleanOptions)630 int32_t CloudSyncServiceProxy::Clean(const std::string &accountId, const CleanOptions &cleanOptions)
631 {
632     LOGI("Clean");
633     MessageParcel data;
634     MessageParcel reply;
635     MessageOption option;
636 
637     if (!data.WriteInterfaceToken(GetDescriptor())) {
638         LOGE("Failed to write interface token");
639         return E_BROKEN_IPC;
640     }
641 
642     if (!data.WriteString(accountId)) {
643         LOGE("Failed to send the account id");
644         return E_INVAL_ARG;
645     }
646 
647     if (!data.WriteParcelable(&cleanOptions)) {
648         LOGE("failed to write cleanOptions");
649         return E_INVAL_ARG;
650     }
651 
652     auto remote = Remote();
653     if (!remote) {
654         LOGE("remote is nullptr");
655         return E_BROKEN_IPC;
656     }
657     int32_t ret = remote->SendRequest(static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_CLEAN), data,
658                                       reply, option);
659     if (ret != E_OK) {
660         LOGE("Failed to send out the request, errno: %{public}d", ret);
661         return E_BROKEN_IPC;
662     }
663     LOGI("Clean Success");
664     return reply.ReadInt32();
665 }
666 
EnableCloud(const std::string & accoutId,const SwitchDataObj & switchData)667 int32_t CloudSyncServiceProxy::EnableCloud(const std::string &accoutId, const SwitchDataObj &switchData)
668 {
669     LOGI("EnableCloud");
670     MessageParcel data;
671     MessageParcel reply;
672     MessageOption option;
673 
674     if (!data.WriteInterfaceToken(GetDescriptor())) {
675         LOGE("Failed to write interface token");
676         return E_BROKEN_IPC;
677     }
678 
679     if (!data.WriteString(accoutId)) {
680         LOGE("Failed to send the account id");
681         return E_INVAL_ARG;
682     }
683 
684     if (!data.WriteParcelable(&switchData)) {
685         LOGE("Failed to send the bundle switch");
686         return E_INVAL_ARG;
687     }
688 
689     auto remote = Remote();
690     if (!remote) {
691         LOGE("remote is nullptr");
692         return E_BROKEN_IPC;
693     }
694     int32_t ret = remote->SendRequest(
695         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_ENABLE_CLOUD), data, reply, option);
696     if (ret != E_OK) {
697         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
698         return E_BROKEN_IPC;
699     }
700     LOGI("EnableCloud Success");
701     return reply.ReadInt32();
702 }
703 
DisableCloud(const std::string & accoutId)704 int32_t CloudSyncServiceProxy::DisableCloud(const std::string &accoutId)
705 {
706     LOGI("DisableCloud");
707     MessageParcel data;
708     MessageParcel reply;
709     MessageOption option;
710 
711     if (!data.WriteInterfaceToken(GetDescriptor())) {
712         LOGE("Failed to write interface token");
713         return E_BROKEN_IPC;
714     }
715 
716     if (!data.WriteString(accoutId)) {
717         LOGE("Failed to send the account id");
718         return E_INVAL_ARG;
719     }
720 
721     auto remote = Remote();
722     if (!remote) {
723         LOGE("remote is nullptr");
724         return E_BROKEN_IPC;
725     }
726     int32_t ret = remote->SendRequest(
727         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DISABLE_CLOUD), data, reply, option);
728     if (ret != E_OK) {
729         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
730         return E_BROKEN_IPC;
731     }
732     LOGI("DisableCloud Success");
733     return reply.ReadInt32();
734 }
735 
NotifyDataChange(const std::string & accoutId,const std::string & bundleName)736 int32_t CloudSyncServiceProxy::NotifyDataChange(const std::string &accoutId, const std::string &bundleName)
737 {
738     LOGI("NotifyDataChange");
739     MessageParcel data;
740     MessageParcel reply;
741     MessageOption option;
742 
743     if (!data.WriteInterfaceToken(GetDescriptor())) {
744         LOGE("Failed to write interface token");
745         return E_BROKEN_IPC;
746     }
747 
748     if (!data.WriteString(accoutId)) {
749         LOGE("Failed to send the account id");
750         return E_INVAL_ARG;
751     }
752 
753     if (!data.WriteString(bundleName)) {
754         LOGE("Failed to send the bundle name");
755         return E_INVAL_ARG;
756     }
757 
758     auto remote = Remote();
759     if (!remote) {
760         LOGE("remote is nullptr");
761         return E_BROKEN_IPC;
762     }
763     int32_t ret = remote->SendRequest(
764         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_NOTIFY_DATA_CHANGE), data, reply, option);
765     if (ret != E_OK) {
766         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
767         return E_BROKEN_IPC;
768     }
769     LOGI("NotifyDataChange Success");
770     return reply.ReadInt32();
771 }
772 
NotifyEventChange(int32_t userId,const std::string & eventId,const std::string & extraData)773 int32_t CloudSyncServiceProxy::NotifyEventChange(
774     int32_t userId, const std::string &eventId, const std::string &extraData)
775 {
776     LOGI("NotifyEventChange");
777     MessageParcel data;
778     MessageParcel reply;
779     MessageOption option;
780 
781     if (!data.WriteInterfaceToken(GetDescriptor())) {
782         LOGE("Failed to write interface token");
783         return E_BROKEN_IPC;
784     }
785 
786     if (!data.WriteInt32(userId)) {
787         LOGE("Failed to send the user id");
788         return E_INVAL_ARG;
789     }
790 
791     if (!data.WriteString(eventId)) {
792         LOGE("Failed to send the event id");
793         return E_INVAL_ARG;
794     }
795 
796     if (!data.WriteString(extraData)) {
797         LOGE("Failed to send the extra data");
798         return E_INVAL_ARG;
799     }
800 
801     auto remote = Remote();
802     if (!remote) {
803         LOGE("remote is nullptr");
804         return E_BROKEN_IPC;
805     }
806     int32_t ret = remote->SendRequest(
807         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_NOTIFY_EVENT_CHANGE), data, reply, option);
808     if (ret != E_OK) {
809         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
810         return E_BROKEN_IPC;
811     }
812     LOGI("NotifyEventChange Success");
813     return reply.ReadInt32();
814 }
815 
StartDownloadFile(const std::string & uri)816 int32_t CloudSyncServiceProxy::StartDownloadFile(const std::string &uri)
817 {
818     if (uri.empty()) {
819         LOGE("Invalid argument for empty uri");
820         return E_INVAL_ARG;
821     }
822 #ifdef SUPPORT_MEDIA_LIBRARY
823     LOGI("StartDownloadFile Start, uri: %{public}s", GetAnonyString(uri).c_str());
824     MessageParcel data;
825     MessageParcel reply;
826     MessageOption option;
827 
828     if (!data.WriteInterfaceToken(GetDescriptor())) {
829         LOGE("Failed to write interface token");
830         return E_BROKEN_IPC;
831     }
832 
833     if (!data.WriteString(uri)) {
834         LOGE("Failed to send the cloud id");
835         return E_INVAL_ARG;
836     }
837 
838     auto remote = Remote();
839     if (!remote) {
840         LOGE("remote is nullptr");
841         return E_BROKEN_IPC;
842     }
843     int32_t ret = remote->SendRequest(
844         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_DOWNLOAD_FILE), data, reply, option);
845     if (ret != E_OK) {
846         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
847         return E_BROKEN_IPC;
848     }
849     LOGI("StartDownloadFile Success");
850     return reply.ReadInt32();
851 #else
852     LOGE("Function StartDownloadFile is undefined");
853     return 0;
854 #endif
855 }
856 
StartFileCacheWriteParcel(MessageParcel & data,const std::vector<std::string> & uriVec,std::bitset<FIELD_KEY_MAX_SIZE> & fieldkey,bool isCallbackValid,const sptr<IRemoteObject> & downloadCallback,int32_t timeout)857 int32_t CloudSyncServiceProxy::StartFileCacheWriteParcel(MessageParcel &data,
858                                                          const std::vector<std::string> &uriVec,
859                                                          std::bitset<FIELD_KEY_MAX_SIZE> &fieldkey,
860                                                          bool isCallbackValid,
861                                                          const sptr<IRemoteObject> &downloadCallback,
862                                                          int32_t timeout)
863 {
864     if (!data.WriteStringVector(uriVec)) {
865         LOGE("Failed to send the cloud id");
866         return E_INVAL_ARG;
867     }
868     int32_t intValue = static_cast<int32_t>(fieldkey.to_ulong());
869     if (!data.WriteInt32(intValue)) {
870         LOGE("Failed to send the fieldkey");
871         return E_INVAL_ARG;
872     }
873 
874     if (!data.WriteBool(isCallbackValid)) {
875         LOGE("Failed to send the isCallbackValid flag");
876         return E_INVAL_ARG;
877     }
878 
879     if (isCallbackValid) {
880         if (!data.WriteRemoteObject(downloadCallback)) {
881             LOGE("Failed to send the callback stub");
882             return E_INVAL_ARG;
883         }
884     }
885 
886     if (!data.WriteInt32(timeout)) {
887         LOGE("Faile to send the timeout");
888         return E_INVAL_ARG;
889     }
890     return E_OK;
891 }
892 
StartFileCache(const std::vector<std::string> & uriVec,int64_t & downloadId,std::bitset<FIELD_KEY_MAX_SIZE> fieldkey,bool isCallbackValid,const sptr<IRemoteObject> & downloadCallback,int32_t timeout)893 int32_t CloudSyncServiceProxy::StartFileCache(const std::vector<std::string> &uriVec,
894                                               int64_t &downloadId, std::bitset<FIELD_KEY_MAX_SIZE> fieldkey,
895                                               bool isCallbackValid,
896                                               const sptr<IRemoteObject> &downloadCallback,
897                                               int32_t timeout)
898 {
899     LOGI("StartFileCache Start");
900     MessageParcel data;
901     MessageParcel reply;
902     MessageOption option;
903 
904     if (!data.WriteInterfaceToken(GetDescriptor())) {
905         LOGE("Failed to write interface token");
906         return E_BROKEN_IPC;
907     }
908 
909     for (std::size_t i = 0; i < uriVec.size(); ++i) {
910         if (uriVec[i].empty()) {
911             LOGE("Invalid argument for empty uri, index %{public}zu", i);
912             return E_INVAL_ARG;
913         }
914     }
915 
916     auto retParcel = StartFileCacheWriteParcel(data, uriVec, fieldkey, isCallbackValid, downloadCallback, timeout);
917     if (retParcel != E_OK) {
918         LOGE("StartFileCacheWriteParcel failed");
919         return retParcel;
920     }
921 
922     auto remote = Remote();
923     if (!remote) {
924         LOGE("remote is nullptr");
925         return E_BROKEN_IPC;
926     }
927     int32_t ret = remote->SendRequest(
928         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_FILE_CACHE), data, reply, option);
929     if (ret != E_OK) {
930         LOGE("Failed to send out the request, errno %{public}d", ret);
931         return ret;
932     }
933 
934     downloadId = reply.ReadInt64();
935     LOGI("StartFileCache Success, downloadId: %{public}lld", static_cast<long long>(downloadId));
936 
937     return reply.ReadInt32();
938 }
939 
StopDownloadFile(const std::string & uri,bool needClean)940 int32_t CloudSyncServiceProxy::StopDownloadFile(const std::string &uri, bool needClean)
941 {
942     if (uri.empty()) {
943         LOGE("Invalid argument for empty uri");
944         return E_INVAL_ARG;
945     }
946     LOGI("StopDownloadFile Start, uri: %{public}s", GetAnonyString(uri).c_str());
947     MessageParcel data;
948     MessageParcel reply;
949     MessageOption option;
950 
951     if (!data.WriteInterfaceToken(GetDescriptor())) {
952         LOGE("Failed to write interface token");
953         return E_BROKEN_IPC;
954     }
955 
956     if (!data.WriteString(uri)) {
957         LOGE("Failed to send the cloud id");
958         return E_INVAL_ARG;
959     }
960 
961     if (!data.WriteBool(needClean)) {
962         LOGE("Failed to send the needClean flag");
963         return E_INVAL_ARG;
964     }
965 
966     auto remote = Remote();
967     if (!remote) {
968         LOGE("remote is nullptr");
969         return E_BROKEN_IPC;
970     }
971     int32_t ret = remote->SendRequest(
972         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_DOWNLOAD_FILE), data, reply, option);
973     if (ret != E_OK) {
974         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
975         return E_BROKEN_IPC;
976     }
977     LOGI("StopDownloadFile Success");
978     return reply.ReadInt32();
979 }
980 
StopFileCache(int64_t downloadId,bool needClean,int32_t timeout)981 int32_t CloudSyncServiceProxy::StopFileCache(int64_t downloadId, bool needClean, int32_t timeout)
982 {
983     LOGI("StopFileCache Start");
984     MessageParcel data;
985     MessageParcel reply;
986     MessageOption option;
987 
988     if (!data.WriteInterfaceToken(GetDescriptor())) {
989         LOGE("Failed to write interface token");
990         return E_BROKEN_IPC;
991     }
992 
993     LOGI("StopFileCache Start, downloadId: %{public}lld, needClean: %{public}d",
994          static_cast<long long>(downloadId), needClean);
995 
996     if (!data.WriteInt64(downloadId)) {
997         LOGE("Failed to send the cloud id");
998         return E_INVAL_ARG;
999     }
1000 
1001     if (!data.WriteBool(needClean)) {
1002         LOGE("Failed to send the needClean flag");
1003         return E_INVAL_ARG;
1004     }
1005 
1006     if (!data.WriteInt32(timeout)) {
1007         LOGE("Failed to send the timeout");
1008         return E_INVAL_ARG;
1009     }
1010 
1011     auto remote = Remote();
1012     if (!remote) {
1013         LOGE("remote is nullptr");
1014         return E_BROKEN_IPC;
1015     }
1016     int32_t ret = remote->SendRequest(
1017         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_FILE_CACHE), data, reply, option);
1018     if (ret != E_OK) {
1019         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
1020         return ret;
1021     }
1022     // if StopFileCache finished, we need call RemoveUri later
1023     LOGI("StopFileCache Success");
1024     return reply.ReadInt32();
1025 }
1026 
DownloadThumb()1027 int32_t CloudSyncServiceProxy::DownloadThumb()
1028 {
1029     LOGI("DownloadThumb Start");
1030     MessageParcel data;
1031     MessageParcel reply;
1032     MessageOption option;
1033 
1034     if (!data.WriteInterfaceToken(GetDescriptor())) {
1035         LOGE("Failed to write interface token");
1036         return E_BROKEN_IPC;
1037     }
1038     auto remote = Remote();
1039     if (!remote) {
1040         LOGE("remote is nullptr");
1041         return E_BROKEN_IPC;
1042     }
1043     int32_t ret = remote->SendRequest(
1044         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_THUMB), data, reply, option);
1045     if (ret != E_OK) {
1046         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
1047         return ret;
1048     }
1049     LOGI("DownloadThumb Success");
1050     return reply.ReadInt32();
1051 }
1052 
RegisterDownloadFileCallback(const sptr<IRemoteObject> & downloadCallback)1053 int32_t CloudSyncServiceProxy::RegisterDownloadFileCallback(const sptr<IRemoteObject> &downloadCallback)
1054 {
1055     LOGI("RegisterDownloadFileCallback Start");
1056     MessageParcel data;
1057     MessageParcel reply;
1058     MessageOption option;
1059 
1060     if (!downloadCallback) {
1061         LOGI("Empty callback stub");
1062         return E_INVAL_ARG;
1063     }
1064 
1065     if (!data.WriteInterfaceToken(GetDescriptor())) {
1066         LOGE("Failed to write interface token");
1067         return E_BROKEN_IPC;
1068     }
1069 
1070     if (!data.WriteRemoteObject(downloadCallback)) {
1071         LOGE("Failed to send the callback stub");
1072         return E_INVAL_ARG;
1073     }
1074 
1075     auto remote = Remote();
1076     if (!remote) {
1077         LOGE("remote is nullptr");
1078         return E_BROKEN_IPC;
1079     }
1080     int32_t ret = remote->SendRequest(
1081         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_DOWNLOAD_FILE_CALLBACK), data,
1082         reply, option);
1083     if (ret != E_OK) {
1084         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
1085         return E_BROKEN_IPC;
1086     }
1087     LOGI("RegisterDownloadFileCallback Success");
1088     return reply.ReadInt32();
1089 }
1090 
RegisterFileCacheCallback(const sptr<IRemoteObject> & downloadCallback)1091 int32_t CloudSyncServiceProxy::RegisterFileCacheCallback(const sptr<IRemoteObject> &downloadCallback)
1092 {
1093     LOGI("RegisterFileCacheCallback Start");
1094     MessageParcel data;
1095     MessageParcel reply;
1096     MessageOption option;
1097 
1098     if (!downloadCallback) {
1099         LOGI("Empty callback stub");
1100         return E_INVAL_ARG;
1101     }
1102 
1103     if (!data.WriteInterfaceToken(GetDescriptor())) {
1104         LOGE("Failed to write interface token");
1105         return E_BROKEN_IPC;
1106     }
1107 
1108     if (!data.WriteRemoteObject(downloadCallback)) {
1109         LOGE("Failed to send the callback stub");
1110         return E_INVAL_ARG;
1111     }
1112 
1113     auto remote = Remote();
1114     if (!remote) {
1115         LOGE("remote is nullptr");
1116         return E_BROKEN_IPC;
1117     }
1118     int32_t ret = remote->SendRequest(
1119         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_FILE_CACHE_CALLBACK), data,
1120         reply, option);
1121     if (ret != E_OK) {
1122         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
1123         return ret;
1124     }
1125     LOGI("RegisterFileCacheCallback Success");
1126     return reply.ReadInt32();
1127 }
1128 
UnregisterDownloadFileCallback()1129 int32_t CloudSyncServiceProxy::UnregisterDownloadFileCallback()
1130 {
1131     LOGI("UnregisterDownloadFileCallback Start");
1132     MessageParcel data;
1133     MessageParcel reply;
1134     MessageOption option;
1135 
1136     if (!data.WriteInterfaceToken(GetDescriptor())) {
1137         LOGE("Failed to write interface token");
1138         return E_BROKEN_IPC;
1139     }
1140 
1141     auto remote = Remote();
1142     if (!remote) {
1143         LOGE("remote is nullptr");
1144         return E_BROKEN_IPC;
1145     }
1146     int32_t ret = remote->SendRequest(
1147         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UNREGISTER_DOWNLOAD_FILE_CALLBACK), data,
1148         reply, option);
1149     if (ret != E_OK) {
1150         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
1151         return E_BROKEN_IPC;
1152     }
1153     LOGI("UnregisterDownloadFileCallback Success");
1154     return reply.ReadInt32();
1155 }
1156 
UnregisterFileCacheCallback()1157 int32_t CloudSyncServiceProxy::UnregisterFileCacheCallback()
1158 {
1159     LOGI("UnregisterFileCacheCallback Start");
1160     MessageParcel data;
1161     MessageParcel reply;
1162     MessageOption option;
1163 
1164     if (!data.WriteInterfaceToken(GetDescriptor())) {
1165         LOGE("Failed to write interface token");
1166         return E_BROKEN_IPC;
1167     }
1168 
1169     auto remote = Remote();
1170     if (!remote) {
1171         LOGE("remote is nullptr");
1172         return E_BROKEN_IPC;
1173     }
1174     int32_t ret = remote->SendRequest(
1175         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UNREGISTER_FILE_CACHE_CALLBACK), data,
1176         reply, option);
1177     if (ret != E_OK) {
1178         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
1179         return ret;
1180     }
1181     LOGI("UnregisterFileCacheCallback Success");
1182     return reply.ReadInt32();
1183 }
1184 
UploadAsset(const int32_t userId,const std::string & request,std::string & result)1185 int32_t CloudSyncServiceProxy::UploadAsset(const int32_t userId, const std::string &request, std::string &result)
1186 {
1187     LOGD("UploadAsset");
1188     MessageParcel data;
1189     MessageParcel reply;
1190     MessageOption option;
1191 
1192     if (!data.WriteInterfaceToken(GetDescriptor())) {
1193         LOGE("Failed to write interface token");
1194         return E_BROKEN_IPC;
1195     }
1196 
1197     if (!data.WriteInt32(userId)) {
1198         LOGE("Failed to send the user id");
1199         return E_INVAL_ARG;
1200     }
1201 
1202     if (!data.WriteString(request)) {
1203         LOGE("Failed to send the request");
1204         return E_INVAL_ARG;
1205     }
1206 
1207     auto remote = Remote();
1208     if (!remote) {
1209         LOGE("remote is nullptr");
1210         return E_BROKEN_IPC;
1211     }
1212     int32_t ret = remote->SendRequest(
1213         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UPLOAD_ASSET), data, reply, option);
1214     if (ret != E_OK) {
1215         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
1216         return E_BROKEN_IPC;
1217     }
1218     ret = reply.ReadInt32();
1219     result = reply.ReadString();
1220     LOGD("UploadAsset Success");
1221     return ret;
1222 }
1223 
DownloadFile(const int32_t userId,const std::string & bundleName,AssetInfoObj & assetInfoObj)1224 int32_t CloudSyncServiceProxy::DownloadFile(const int32_t userId,
1225                                             const std::string &bundleName,
1226                                             AssetInfoObj &assetInfoObj)
1227 {
1228     LOGI("DownloadFile");
1229     MessageParcel data;
1230     MessageParcel reply;
1231     MessageOption option;
1232 
1233     if (!data.WriteInterfaceToken(GetDescriptor())) {
1234         LOGE("Failed to write interface token");
1235         return E_BROKEN_IPC;
1236     }
1237 
1238     if (!data.WriteInt32(userId)) {
1239         LOGE("Failed to send the user id");
1240         return E_INVAL_ARG;
1241     }
1242 
1243     if (!data.WriteString(bundleName)) {
1244         LOGE("Failed to send the bundle name");
1245         return E_INVAL_ARG;
1246     }
1247 
1248     if (!data.WriteParcelable(&assetInfoObj)) {
1249         LOGE("Failed to send the bundle assetInfo");
1250         return E_INVAL_ARG;
1251     }
1252 
1253     auto remote = Remote();
1254     if (!remote) {
1255         LOGE("remote is nullptr");
1256         return E_BROKEN_IPC;
1257     }
1258     int32_t ret = remote->SendRequest(
1259         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_FILE), data, reply, option);
1260     if (ret != E_OK) {
1261         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
1262         return E_BROKEN_IPC;
1263     }
1264     LOGI("DownloadFile Success");
1265     return reply.ReadInt32();
1266 }
1267 
DownloadFiles(const int32_t userId,const std::string & bundleName,const std::vector<AssetInfoObj> & assetInfoObj,std::vector<bool> & assetResultMap)1268 int32_t CloudSyncServiceProxy::DownloadFiles(const int32_t userId,
1269                                              const std::string &bundleName,
1270                                              const std::vector<AssetInfoObj> &assetInfoObj,
1271                                              std::vector<bool> &assetResultMap)
1272 {
1273     LOGI("DownloadFiles");
1274     MessageParcel data;
1275     MessageParcel reply;
1276     MessageOption option;
1277 
1278     if (!data.WriteInterfaceToken(GetDescriptor())) {
1279         LOGE("Failed to write interface token");
1280         return E_BROKEN_IPC;
1281     }
1282 
1283     if (!data.WriteInt32(userId)) {
1284         LOGE("Failed to send the user id");
1285         return E_INVAL_ARG;
1286     }
1287 
1288     if (!data.WriteString(bundleName)) {
1289         LOGE("Failed to send the bundle name");
1290         return E_INVAL_ARG;
1291     }
1292 
1293     if (assetInfoObj.size() > INT_MAX ||
1294         assetInfoObj.size() == 0 ||
1295         !data.WriteInt32(assetInfoObj.size())) {
1296         LOGE("Failed to send the vector size");
1297         return E_INVAL_ARG;
1298     }
1299 
1300     for (const auto &obj : assetInfoObj) {
1301         if (!data.WriteParcelable(&obj)) {
1302             LOGE("Failed to send the assetInfoObj");
1303             return E_INVAL_ARG;
1304         }
1305     }
1306 
1307     auto remote = Remote();
1308     if (!remote) {
1309         LOGE("remote is nullptr");
1310         return E_BROKEN_IPC;
1311     }
1312     int32_t ret = remote->SendRequest(
1313         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_FILES), data, reply, option);
1314     if (ret != E_OK) {
1315         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
1316         return E_BROKEN_IPC;
1317     }
1318     LOGI("DownloadFile Success");
1319     bool readParcel = reply.ReadBoolVector(&assetResultMap);
1320     if (readParcel != true) {
1321         LOGE("Failed to ReadBoolVector");
1322         return E_INVAL_ARG;
1323     }
1324     return reply.ReadInt32();
1325 }
1326 
BatchCleanFile(const std::vector<CleanFileInfoObj> & fileInfo,std::vector<std::string> & failCloudId)1327 int32_t CloudSyncServiceProxy::BatchCleanFile(const std::vector<CleanFileInfoObj> &fileInfo,
1328     std::vector<std::string> &failCloudId)
1329 {
1330     LOGI("BatchCleanFile");
1331     MessageParcel data;
1332     MessageParcel reply;
1333     MessageOption option;
1334 
1335     if (!data.WriteInterfaceToken(GetDescriptor())) {
1336         LOGE("Failed to write interface token");
1337         return E_BROKEN_IPC;
1338     }
1339 
1340     if (fileInfo.size() > CLEAN_FILE_MAX_SIZE || fileInfo.size() == 0 || !data.WriteInt32(fileInfo.size())) {
1341         LOGE("Failed to send the vector size");
1342         return E_INVAL_ARG;
1343     }
1344 
1345     for (const auto &obj : fileInfo) {
1346         if (!data.WriteParcelable(&obj)) {
1347             LOGE("Failed to send the fileInfo");
1348             return E_INVAL_ARG;
1349         }
1350     }
1351 
1352     auto remote = Remote();
1353     if (!remote) {
1354         LOGE("remote is nullptr");
1355         return E_BROKEN_IPC;
1356     }
1357     int32_t ret = remote->SendRequest(
1358         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_BATCH_CLEAN_FILE), data, reply, option);
1359     if (ret != E_OK) {
1360         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
1361         return E_BROKEN_IPC;
1362     }
1363     LOGI("BatchCleanFile Success");
1364     reply.ReadStringVector(&failCloudId);
1365     return reply.ReadInt32();
1366 }
1367 
DownloadAsset(const uint64_t taskId,const int32_t userId,const std::string & bundleName,const std::string & networkId,AssetInfoObj & assetInfoObj)1368 int32_t CloudSyncServiceProxy::DownloadAsset(const uint64_t taskId,
1369                                              const int32_t userId,
1370                                              const std::string &bundleName,
1371                                              const std::string &networkId,
1372                                              AssetInfoObj &assetInfoObj)
1373 {
1374     LOGI("DownloadFile");
1375     MessageParcel data;
1376     MessageParcel reply;
1377     MessageOption option;
1378 
1379     if (!data.WriteInterfaceToken(GetDescriptor())) {
1380         LOGE("Failed to write interface token");
1381         return E_BROKEN_IPC;
1382     }
1383 
1384     if (!data.WriteUint64(taskId)) {
1385         LOGE("Failed to send the task id");
1386         return E_INVAL_ARG;
1387     }
1388 
1389     if (!data.WriteInt32(userId)) {
1390         LOGE("Failed to send the user id");
1391         return E_INVAL_ARG;
1392     }
1393 
1394     if (!data.WriteString(bundleName)) {
1395         LOGE("Failed to send the bundle name");
1396         return E_INVAL_ARG;
1397     }
1398 
1399     if (!data.WriteString(networkId)) {
1400         LOGE("Failed to send the bundle name");
1401         return E_INVAL_ARG;
1402     }
1403 
1404     if (!data.WriteParcelable(&assetInfoObj)) {
1405         LOGE("Failed to send the bundle assetInfo");
1406         return E_INVAL_ARG;
1407     }
1408 
1409     auto remote = Remote();
1410     if (!remote) {
1411         LOGE("remote is nullptr");
1412         return E_BROKEN_IPC;
1413     }
1414     int32_t ret = remote->SendRequest(
1415         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_ASSET), data, reply, option);
1416     if (ret != E_OK) {
1417         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
1418         return E_BROKEN_IPC;
1419     }
1420     LOGI("DownloadFile Success");
1421     return reply.ReadInt32();
1422 }
1423 
RegisterDownloadAssetCallback(const sptr<IRemoteObject> & remoteObject)1424 int32_t CloudSyncServiceProxy::RegisterDownloadAssetCallback(const sptr<IRemoteObject> &remoteObject)
1425 {
1426     LOGI("Start RegisterDownloadAssetCallback");
1427     MessageParcel data;
1428     MessageParcel reply;
1429     MessageOption option;
1430 
1431     if (!remoteObject) {
1432         LOGI("Empty callback stub");
1433         return E_INVAL_ARG;
1434     }
1435 
1436     if (!data.WriteInterfaceToken(GetDescriptor())) {
1437         LOGE("Failed to write interface token");
1438         return E_BROKEN_IPC;
1439     }
1440 
1441     if (!data.WriteRemoteObject(remoteObject)) {
1442         LOGE("Failed to send the callback stub");
1443         return E_INVAL_ARG;
1444     }
1445 
1446     auto remote = Remote();
1447     if (!remote) {
1448         LOGE("remote is nullptr");
1449         return E_BROKEN_IPC;
1450     }
1451     int32_t ret = remote->SendRequest(
1452         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_DOWNLOAD_ASSET_CALLBACK), data,
1453         reply, option);
1454     if (ret != E_OK) {
1455         stringstream ss;
1456         ss << "Failed to send out the requeset, errno:" << ret;
1457         LOGE("%{public}s", ss.str().c_str());
1458         return E_BROKEN_IPC;
1459     }
1460     LOGI("RegisterDownloadAssetCallback Success");
1461     return reply.ReadInt32();
1462 }
1463 
DeleteAsset(const int32_t userId,const std::string & uri)1464 int32_t CloudSyncServiceProxy::DeleteAsset(const int32_t userId, const std::string &uri)
1465 {
1466     LOGD("DeleteAsset");
1467     MessageParcel data;
1468     MessageParcel reply;
1469     MessageOption option;
1470 
1471     if (!data.WriteInterfaceToken(GetDescriptor())) {
1472         LOGE("Failed to write interface token");
1473         return E_BROKEN_IPC;
1474     }
1475 
1476     if (!data.WriteInt32(userId)) {
1477         LOGE("Failed to send the user id");
1478         return E_INVAL_ARG;
1479     }
1480 
1481     if (!data.WriteString(uri)) {
1482         LOGE("Failed to send the uri");
1483         return E_INVAL_ARG;
1484     }
1485 
1486     auto remote = Remote();
1487     if (!remote) {
1488         LOGE("remote is nullptr");
1489         return E_BROKEN_IPC;
1490     }
1491     int32_t ret = remote->SendRequest(
1492         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DELETE_ASSET), data, reply, option);
1493     if (ret != E_OK) {
1494         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
1495         return E_BROKEN_IPC;
1496     }
1497     ret = reply.ReadInt32();
1498     LOGI("DeleteAsset Success");
1499     return ret;
1500 }
1501 
GetInstance()1502 sptr<ICloudSyncService> CloudSyncServiceProxy::GetInstance()
1503 {
1504     LOGD("getinstance");
1505     unique_lock<mutex> lock(instanceMutex_);
1506     if (serviceProxy_ != nullptr) {
1507         return serviceProxy_;
1508     }
1509 
1510     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1511     if (samgr == nullptr) {
1512         LOGE("Samgr is nullptr");
1513         return nullptr;
1514     }
1515     sptr<ServiceProxyLoadCallback> cloudSyncLoadCallback = new ServiceProxyLoadCallback();
1516     if (cloudSyncLoadCallback == nullptr) {
1517         LOGE("cloudSyncLoadCallback is nullptr");
1518         return nullptr;
1519     }
1520     int32_t ret = samgr->LoadSystemAbility(FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, cloudSyncLoadCallback);
1521     if (ret != E_OK) {
1522         LOGE("Failed to Load systemAbility, systemAbilityId:%{public}d, ret code:%{public}d",
1523              FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, ret);
1524         return nullptr;
1525     }
1526     unique_lock<mutex> proxyLock(proxyMutex_);
1527     auto waitStatus = cloudSyncLoadCallback->proxyConVar_.wait_for(
1528         proxyLock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS),
1529         [cloudSyncLoadCallback]() { return cloudSyncLoadCallback->isLoadSuccess_.load(); });
1530     if (!waitStatus) {
1531         LOGE("Load CloudSynd SA timeout");
1532         return nullptr;
1533     }
1534     return serviceProxy_;
1535 }
1536 
InvaildInstance()1537 void CloudSyncServiceProxy::InvaildInstance()
1538 {
1539     LOGI("invalid instance");
1540     unique_lock<mutex> lock(instanceMutex_);
1541     serviceProxy_ = nullptr;
1542 }
1543 
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)1544 void CloudSyncServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilitySuccess(
1545     int32_t systemAbilityId,
1546     const sptr<IRemoteObject> &remoteObject)
1547 {
1548     LOGI("Load CloudSync SA success,systemAbilityId:%{public}d, remoteObj result:%{private}s", systemAbilityId,
1549          (remoteObject == nullptr ? "false" : "true"));
1550     unique_lock<mutex> lock(proxyMutex_);
1551     if (serviceProxy_ != nullptr) {
1552         LOGE("CloudSync SA proxy has been loaded");
1553     } else {
1554         serviceProxy_ = iface_cast<ICloudSyncService>(remoteObject);
1555     }
1556     isLoadSuccess_.store(true);
1557     proxyConVar_.notify_one();
1558 }
1559 
OnLoadSystemAbilityFail(int32_t systemAbilityId)1560 void CloudSyncServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
1561 {
1562     LOGI("Load CloudSync SA failed,systemAbilityId:%{public}d", systemAbilityId);
1563     unique_lock<mutex> lock(proxyMutex_);
1564     serviceProxy_ = nullptr;
1565     isLoadSuccess_.store(false);
1566     proxyConVar_.notify_one();
1567 }
1568 } // namespace OHOS::FileManagement::CloudSync
1569