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_download_uri_manager.h"
18 #include "cloud_file_sync_service_interface_code.h"
19 
20 #include <sstream>
21 
22 #include "dfs_error.h"
23 #include "iservice_registry.h"
24 #ifdef SUPPORT_MEDIA_LIBRARY
25 #include "media_file_uri.h"
26 #endif
27 #include "system_ability_definition.h"
28 #include "utils_log.h"
29 
30 namespace OHOS::FileManagement::CloudSync {
31 using namespace std;
32 
33 constexpr int LOAD_SA_TIMEOUT_MS = 4000;
34 
UnRegisterCallbackInner(const std::string & bundleName)35 int32_t CloudSyncServiceProxy::UnRegisterCallbackInner(const std::string &bundleName)
36 {
37     LOGI("Start UnRegisterCallbackInner");
38     MessageParcel data;
39     MessageParcel reply;
40     MessageOption option;
41 
42     if (!data.WriteInterfaceToken(GetDescriptor())) {
43         LOGE("Failed to write interface token");
44         return E_BROKEN_IPC;
45     }
46 
47     auto remote = Remote();
48     if (!remote) {
49         LOGE("remote is nullptr");
50         return E_BROKEN_IPC;
51     }
52 
53     if (!data.WriteString(bundleName)) {
54         LOGE("Failed to send the bundle name");
55         return E_INVAL_ARG;
56     }
57 
58     int32_t ret = remote->SendRequest(
59         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UNREGISTER_CALLBACK), data, reply, option);
60     if (ret != E_OK) {
61         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
62         return E_BROKEN_IPC;
63     }
64     LOGI("UnRegisterCallbackInner Success");
65     return reply.ReadInt32();
66 }
67 
RegisterCallbackInner(const sptr<IRemoteObject> & remoteObject,const std::string & bundleName)68 int32_t CloudSyncServiceProxy::RegisterCallbackInner(const sptr<IRemoteObject> &remoteObject,
69                                                      const std::string &bundleName)
70 {
71     LOGI("Start RegisterCallbackInner");
72     MessageParcel data;
73     MessageParcel reply;
74     MessageOption option;
75 
76     if (!remoteObject) {
77         LOGI("Empty callback stub");
78         return E_INVAL_ARG;
79     }
80 
81     if (!data.WriteInterfaceToken(GetDescriptor())) {
82         LOGE("Failed to write interface token");
83         return E_BROKEN_IPC;
84     }
85 
86     if (!data.WriteRemoteObject(remoteObject)) {
87         LOGE("Failed to send the callback stub");
88         return E_INVAL_ARG;
89     }
90 
91     if (!data.WriteString(bundleName)) {
92         LOGE("Failed to send the bundle name");
93         return E_INVAL_ARG;
94     }
95 
96     auto remote = Remote();
97     if (!remote) {
98         LOGE("remote is nullptr");
99         return E_BROKEN_IPC;
100     }
101     int32_t ret = remote->SendRequest(
102         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_CALLBACK), data, reply, option);
103     if (ret != E_OK) {
104         LOGE("Failed to send out the request, errno: %{public}d", ret);
105         return E_BROKEN_IPC;
106     }
107     LOGI("RegisterCallbackInner Success");
108     return reply.ReadInt32();
109 }
110 
StartSyncInner(bool forceFlag,const std::string & bundleName)111 int32_t CloudSyncServiceProxy::StartSyncInner(bool forceFlag, const std::string &bundleName)
112 {
113     LOGI("Start Sync");
114     MessageParcel data;
115     MessageParcel reply;
116     MessageOption option;
117 
118     if (!data.WriteInterfaceToken(GetDescriptor())) {
119         LOGE("Failed to write interface token");
120         return E_BROKEN_IPC;
121     }
122 
123     if (!data.WriteBool(forceFlag)) {
124         LOGE("Failed to send the force flag");
125         return E_INVAL_ARG;
126     }
127 
128     if (!data.WriteString(bundleName)) {
129         LOGE("Failed to send the bundle name");
130         return E_INVAL_ARG;
131     }
132 
133     auto remote = Remote();
134     if (!remote) {
135         LOGE("remote is nullptr");
136         return E_BROKEN_IPC;
137     }
138     int32_t ret = remote->SendRequest(static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_SYNC),
139                                       data, reply, option);
140     if (ret != E_OK) {
141         LOGE("Failed to send out the request, errno: %{public}d", ret);
142         return E_BROKEN_IPC;
143     }
144     LOGI("StartSyncInner Success");
145     return reply.ReadInt32();
146 }
147 
TriggerSyncInner(const std::string & bundleName,const int32_t & userId)148 int32_t CloudSyncServiceProxy::TriggerSyncInner(const std::string &bundleName, const int32_t &userId)
149 {
150     LOGI("Trigger Sync");
151     MessageParcel data;
152     MessageParcel reply;
153     MessageOption option;
154 
155     if (!data.WriteInterfaceToken(GetDescriptor())) {
156         LOGE("Failed to write interface token");
157         return E_BROKEN_IPC;
158     }
159 
160     if (!data.WriteString(bundleName)) {
161         LOGE("Failed to send the bundle name");
162         return E_INVAL_ARG;
163     }
164 
165     if (!data.WriteInt32(userId)) {
166         LOGE("Failed to send the user id");
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_TRIGGER_SYNC), data, reply, option);
177     if (ret != E_OK) {
178         LOGE("Failed to send out the request, errno: %{public}d", ret);
179         return E_BROKEN_IPC;
180     }
181     LOGI("TriggerSyncInner Success");
182     return reply.ReadInt32();
183 }
184 
GetSyncTimeInner(int64_t & syncTime,const std::string & bundleName)185 int32_t CloudSyncServiceProxy::GetSyncTimeInner(int64_t &syncTime, const std::string &bundleName)
186 {
187     LOGI("Start GetSyncTimeInner");
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     auto remote = Remote();
199     if (!remote) {
200         LOGE("remote is nullptr");
201         return E_BROKEN_IPC;
202     }
203 
204     if (!data.WriteString(bundleName)) {
205         LOGE("Failed to send the bundle name");
206         return E_INVAL_ARG;
207     }
208 
209     int32_t ret = remote->SendRequest(
210         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_GET_SYNC_TIME), data, reply, option);
211     if (ret != E_OK) {
212         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
213         return E_BROKEN_IPC;
214     }
215 
216     syncTime = reply.ReadInt64();
217 
218     return reply.ReadInt32();
219 }
220 
CleanCacheInner(const std::string & uri)221 int32_t CloudSyncServiceProxy::CleanCacheInner(const std::string &uri)
222 {
223     LOGI("Start CleanCacheInner");
224     MessageParcel data;
225     MessageParcel reply;
226     MessageOption option;
227 
228     if (!data.WriteInterfaceToken(GetDescriptor())) {
229         LOGE("Failed to write interface token");
230         return E_BROKEN_IPC;
231     }
232 
233     if (!data.WriteString(uri)) {
234         LOGE("Failed to send the uri");
235         return E_BROKEN_IPC;
236     }
237 
238     auto remote = Remote();
239     if (!remote) {
240         LOGE("remote is nullptr");
241         return E_BROKEN_IPC;
242     }
243     int32_t ret = remote->SendRequest(static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_CLEAN_CACHE),
244                                       data, reply, option);
245     if (ret != E_OK) {
246         LOGE("Failed to send out the request, errno: %{public}d", ret);
247         return E_BROKEN_IPC;
248     }
249 
250     return reply.ReadInt32();
251 }
252 
StopSyncInner(const std::string & bundleName,bool forceFlag)253 int32_t CloudSyncServiceProxy::StopSyncInner(const std::string &bundleName, bool forceFlag)
254 {
255     LOGI("StopSync");
256     MessageParcel data;
257     MessageParcel reply;
258     MessageOption option;
259 
260     if (!data.WriteInterfaceToken(GetDescriptor())) {
261         LOGE("Failed to write interface token");
262         return E_BROKEN_IPC;
263     }
264 
265     auto remote = Remote();
266     if (!remote) {
267         LOGE("remote is nullptr");
268         return E_BROKEN_IPC;
269     }
270 
271     if (!data.WriteString(bundleName)) {
272         LOGE("Failed to send the bundle name");
273         return E_INVAL_ARG;
274     }
275 
276     if (!data.WriteBool(forceFlag)) {
277         LOGE("Failed to send the forceFlag");
278         return E_INVAL_ARG;
279     }
280 
281     int32_t ret = remote->SendRequest(static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_SYNC),
282                                       data, reply, option);
283     if (ret != E_OK) {
284         LOGE("Failed to send out the request, errno: %{public}d", ret);
285         return E_BROKEN_IPC;
286     }
287     LOGI("StopSyncInner Success");
288     return reply.ReadInt32();
289 }
290 
ResetCursor(const std::string & bundleName)291 int32_t CloudSyncServiceProxy::ResetCursor(const std::string &bundleName)
292 {
293     LOGI("ResetCursor");
294     MessageParcel data;
295     MessageParcel reply;
296     MessageOption option;
297 
298     if (!data.WriteInterfaceToken(GetDescriptor())) {
299         LOGE("Failed to write interface token");
300         return E_BROKEN_IPC;
301     }
302 
303     auto remote = Remote();
304     if (!remote) {
305         LOGE("remote is nullptr");
306         return E_BROKEN_IPC;
307     }
308 
309     if (!data.WriteString(bundleName)) {
310         LOGE("Failed to send the bundle name");
311         return E_INVAL_ARG;
312     }
313 
314     int32_t ret = remote->SendRequest(
315         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_RESET_CURSOR), data, reply, option);
316     if (ret != E_OK) {
317         LOGE("Failed to send out the request, errno: %{public}d", ret);
318         return E_BROKEN_IPC;
319     }
320     LOGI("ResetCursor Success");
321     return reply.ReadInt32();
322 }
323 
ChangeAppSwitch(const std::string & accoutId,const std::string & bundleName,bool status)324 int32_t CloudSyncServiceProxy::ChangeAppSwitch(const std::string &accoutId, const std::string &bundleName, bool status)
325 {
326     LOGI("ChangeAppSwitch");
327     MessageParcel data;
328     MessageParcel reply;
329     MessageOption option;
330 
331     if (!data.WriteInterfaceToken(GetDescriptor())) {
332         LOGE("Failed to write interface token");
333         return E_BROKEN_IPC;
334     }
335 
336     if (!data.WriteString(accoutId)) {
337         LOGE("Failed to send the account id");
338         return E_INVAL_ARG;
339     }
340 
341     if (!data.WriteString(bundleName)) {
342         LOGE("Failed to send the bundle name");
343         return E_INVAL_ARG;
344     }
345 
346     if (!data.WriteBool(status)) {
347         LOGE("Failed to send the switch status");
348         return E_INVAL_ARG;
349     }
350 
351     auto remote = Remote();
352     if (!remote) {
353         LOGE("remote is nullptr");
354         return E_BROKEN_IPC;
355     }
356     int32_t ret = remote->SendRequest(
357         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_CHANGE_APP_SWITCH), data, reply, option);
358     if (ret != E_OK) {
359         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
360         return E_BROKEN_IPC;
361     }
362     LOGI("ChangeAppSwitch Success");
363     return reply.ReadInt32();
364 }
365 
Clean(const std::string & accountId,const CleanOptions & cleanOptions)366 int32_t CloudSyncServiceProxy::Clean(const std::string &accountId, const CleanOptions &cleanOptions)
367 {
368     LOGI("Clean");
369     MessageParcel data;
370     MessageParcel reply;
371     MessageOption option;
372 
373     if (!data.WriteInterfaceToken(GetDescriptor())) {
374         LOGE("Failed to write interface token");
375         return E_BROKEN_IPC;
376     }
377 
378     if (!data.WriteString(accountId)) {
379         LOGE("Failed to send the account id");
380         return E_INVAL_ARG;
381     }
382 
383     if (!data.WriteParcelable(&cleanOptions)) {
384         LOGE("failed to write cleanOptions");
385         return E_INVAL_ARG;
386     }
387 
388     auto remote = Remote();
389     if (!remote) {
390         LOGE("remote is nullptr");
391         return E_BROKEN_IPC;
392     }
393     int32_t ret = remote->SendRequest(static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_CLEAN), data,
394                                       reply, option);
395     if (ret != E_OK) {
396         LOGE("Failed to send out the request, errno: %{public}d", ret);
397         return E_BROKEN_IPC;
398     }
399     LOGI("Clean Success");
400     return reply.ReadInt32();
401 }
402 
EnableCloud(const std::string & accoutId,const SwitchDataObj & switchData)403 int32_t CloudSyncServiceProxy::EnableCloud(const std::string &accoutId, const SwitchDataObj &switchData)
404 {
405     LOGI("EnableCloud");
406     MessageParcel data;
407     MessageParcel reply;
408     MessageOption option;
409 
410     if (!data.WriteInterfaceToken(GetDescriptor())) {
411         LOGE("Failed to write interface token");
412         return E_BROKEN_IPC;
413     }
414 
415     if (!data.WriteString(accoutId)) {
416         LOGE("Failed to send the account id");
417         return E_INVAL_ARG;
418     }
419 
420     if (!data.WriteParcelable(&switchData)) {
421         LOGE("Failed to send the bundle switch");
422         return E_INVAL_ARG;
423     }
424 
425     auto remote = Remote();
426     if (!remote) {
427         LOGE("remote is nullptr");
428         return E_BROKEN_IPC;
429     }
430     int32_t ret = remote->SendRequest(
431         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_ENABLE_CLOUD), data, reply, option);
432     if (ret != E_OK) {
433         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
434         return E_BROKEN_IPC;
435     }
436     LOGI("EnableCloud Success");
437     return reply.ReadInt32();
438 }
439 
DisableCloud(const std::string & accoutId)440 int32_t CloudSyncServiceProxy::DisableCloud(const std::string &accoutId)
441 {
442     LOGI("DisableCloud");
443     MessageParcel data;
444     MessageParcel reply;
445     MessageOption option;
446 
447     if (!data.WriteInterfaceToken(GetDescriptor())) {
448         LOGE("Failed to write interface token");
449         return E_BROKEN_IPC;
450     }
451 
452     if (!data.WriteString(accoutId)) {
453         LOGE("Failed to send the account id");
454         return E_INVAL_ARG;
455     }
456 
457     auto remote = Remote();
458     if (!remote) {
459         LOGE("remote is nullptr");
460         return E_BROKEN_IPC;
461     }
462     int32_t ret = remote->SendRequest(
463         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DISABLE_CLOUD), data, reply, option);
464     if (ret != E_OK) {
465         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
466         return E_BROKEN_IPC;
467     }
468     LOGI("DisableCloud Success");
469     return reply.ReadInt32();
470 }
471 
NotifyDataChange(const std::string & accoutId,const std::string & bundleName)472 int32_t CloudSyncServiceProxy::NotifyDataChange(const std::string &accoutId, const std::string &bundleName)
473 {
474     LOGI("NotifyDataChange");
475     MessageParcel data;
476     MessageParcel reply;
477     MessageOption option;
478 
479     if (!data.WriteInterfaceToken(GetDescriptor())) {
480         LOGE("Failed to write interface token");
481         return E_BROKEN_IPC;
482     }
483 
484     if (!data.WriteString(accoutId)) {
485         LOGE("Failed to send the account id");
486         return E_INVAL_ARG;
487     }
488 
489     if (!data.WriteString(bundleName)) {
490         LOGE("Failed to send the bundle name");
491         return E_INVAL_ARG;
492     }
493 
494     auto remote = Remote();
495     if (!remote) {
496         LOGE("remote is nullptr");
497         return E_BROKEN_IPC;
498     }
499     int32_t ret = remote->SendRequest(
500         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_NOTIFY_DATA_CHANGE), data, reply, option);
501     if (ret != E_OK) {
502         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
503         return E_BROKEN_IPC;
504     }
505     LOGI("NotifyDataChange Success");
506     return reply.ReadInt32();
507 }
508 
NotifyEventChange(int32_t userId,const std::string & eventId,const std::string & extraData)509 int32_t CloudSyncServiceProxy::NotifyEventChange(
510     int32_t userId, const std::string &eventId, const std::string &extraData)
511 {
512     LOGI("NotifyEventChange");
513     MessageParcel data;
514     MessageParcel reply;
515     MessageOption option;
516 
517     if (!data.WriteInterfaceToken(GetDescriptor())) {
518         LOGE("Failed to write interface token");
519         return E_BROKEN_IPC;
520     }
521 
522     if (!data.WriteInt32(userId)) {
523         LOGE("Failed to send the user id");
524         return E_INVAL_ARG;
525     }
526 
527     if (!data.WriteString(eventId)) {
528         LOGE("Failed to send the event id");
529         return E_INVAL_ARG;
530     }
531 
532     if (!data.WriteString(extraData)) {
533         LOGE("Failed to send the extra data");
534         return E_INVAL_ARG;
535     }
536 
537     auto remote = Remote();
538     if (!remote) {
539         LOGE("remote is nullptr");
540         return E_BROKEN_IPC;
541     }
542     int32_t ret = remote->SendRequest(
543         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_NOTIFY_EVENT_CHANGE), data, reply, option);
544     if (ret != E_OK) {
545         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
546         return E_BROKEN_IPC;
547     }
548     LOGI("NotifyEventChange Success");
549     return reply.ReadInt32();
550 }
551 
StartDownloadFile(const std::string & uri)552 int32_t CloudSyncServiceProxy::StartDownloadFile(const std::string &uri)
553 {
554 #ifdef SUPPORT_MEDIA_LIBRARY
555     LOGI("StartDownloadFile Start");
556     MessageParcel data;
557     MessageParcel reply;
558     MessageOption option;
559 
560     if (!data.WriteInterfaceToken(GetDescriptor())) {
561         LOGE("Failed to write interface token");
562         return E_BROKEN_IPC;
563     }
564 
565     string path = uri;
566     if (uri.find("file://media") == 0) {
567         OHOS::Media::MediaFileUri mediaUri(uri);
568         path = mediaUri.GetFilePath();
569 
570         CloudDownloadUriManager &uriMgr = CloudDownloadUriManager::GetInstance();
571         uriMgr.AddPathToUri(path, uri);
572     }
573 
574     LOGI("StartDownloadFile Start, uri: %{public}s, path: %{public}s",
575          GetAnonyString(uri).c_str(), GetAnonyString(path).c_str());
576 
577     if (!data.WriteString(path)) {
578         LOGE("Failed to send the cloud id");
579         return E_INVAL_ARG;
580     }
581 
582     auto remote = Remote();
583     if (!remote) {
584         LOGE("remote is nullptr");
585         return E_BROKEN_IPC;
586     }
587     int32_t ret = remote->SendRequest(
588         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_DOWNLOAD_FILE), data, reply, option);
589     if (ret != E_OK) {
590         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
591         return E_BROKEN_IPC;
592     }
593     LOGI("StartDownloadFile Success");
594     return reply.ReadInt32();
595 #else
596     LOGE("Function StartDownloadFile is undefined");
597     return 0;
598 #endif
599 }
600 
StartFileCacheWriteParcel(MessageParcel & data,const std::vector<std::string> & pathVec,std::bitset<FIELD_KEY_MAX_SIZE> & fieldkey,bool & isCallbackValid,const sptr<IRemoteObject> & downloadCallback)601 int32_t CloudSyncServiceProxy::StartFileCacheWriteParcel(MessageParcel &data,
602                                                          const std::vector<std::string> &pathVec,
603                                                          std::bitset<FIELD_KEY_MAX_SIZE> &fieldkey,
604                                                          bool &isCallbackValid,
605                                                          const sptr<IRemoteObject> &downloadCallback)
606 {
607     if (!data.WriteStringVector(pathVec)) {
608         LOGE("Failed to send the cloud id");
609         return E_INVAL_ARG;
610     }
611     int32_t intValue = static_cast<int32_t>(fieldkey.to_ulong());
612     if (!data.WriteInt32(intValue)) {
613         LOGE("Failed to send the fieldkey");
614         return E_INVAL_ARG;
615     }
616 
617     if (!data.WriteBool(isCallbackValid)) {
618         LOGE("Failed to send the isCallbackValid flag");
619         return E_INVAL_ARG;
620     }
621 
622     if (isCallbackValid) {
623         if (!data.WriteRemoteObject(downloadCallback)) {
624             LOGE("Failed to send the callback stub");
625             return E_INVAL_ARG;
626         }
627     }
628     return E_OK;
629 }
630 
StartFileCache(const std::vector<std::string> & uriVec,int64_t & downloadId,std::bitset<FIELD_KEY_MAX_SIZE> fieldkey,bool isCallbackValid,const sptr<IRemoteObject> & downloadCallback)631 int32_t CloudSyncServiceProxy::StartFileCache(const std::vector<std::string> &uriVec,
632                                               int64_t &downloadId, std::bitset<FIELD_KEY_MAX_SIZE> fieldkey,
633                                               bool isCallbackValid,
634                                               const sptr<IRemoteObject> &downloadCallback)
635 {
636     LOGI("StartFileCache Start");
637     MessageParcel data;
638     MessageParcel reply;
639     MessageOption option;
640 
641     if (!data.WriteInterfaceToken(GetDescriptor())) {
642         LOGE("Failed to write interface token");
643         return E_BROKEN_IPC;
644     }
645 
646     std::vector<std::string> pathVec;
647     for (unsigned long i = 0; i < uriVec.size(); i++) {
648         string path = uriVec[i];
649 #ifdef SUPPORT_MEDIA_LIBRARY
650         if (uriVec[i].find("file://media") == 0) {
651             OHOS::Media::MediaFileUri mediaUri(uriVec[i]);
652             path = mediaUri.GetFilePath();
653         }
654 #endif
655         CloudDownloadUriManager &uriMgr = CloudDownloadUriManager::GetInstance();
656         uriMgr.AddPathToUri(path, uriVec[i]);
657         pathVec.push_back(path);
658 
659         LOGI("StartFileCache Start, uriVec[%{public}ld]: %{public}s, path: %{public}s",
660              i, GetAnonyString(uriVec[i]).c_str(), GetAnonyString(path).c_str());
661     }
662 
663     auto retParcel = StartFileCacheWriteParcel(data, pathVec, fieldkey, isCallbackValid, downloadCallback);
664     if (retParcel != E_OK) {
665         LOGE("StartFileCacheWriteParcel failed");
666         return retParcel;
667     }
668 
669     auto remote = Remote();
670     if (!remote) {
671         LOGE("remote is nullptr");
672         return E_BROKEN_IPC;
673     }
674     int32_t ret = remote->SendRequest(
675         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_FILE_CACHE), data, reply, option);
676     if (ret != E_OK) {
677         LOGE("Failed to send out the request, errno %{public}d", ret);
678         return ret;
679     }
680 
681     downloadId = reply.ReadInt64();
682 
683     CloudDownloadUriManager &uriMgr = CloudDownloadUriManager::GetInstance();
684     uriMgr.AddDownloadIdToPath(downloadId, pathVec);
685 
686     LOGI("StartFileCache Success, downloadId: %{public}lld", static_cast<long long>(downloadId));
687 
688     return reply.ReadInt32();
689 }
690 
StopDownloadFile(const std::string & uri,bool needClean)691 int32_t CloudSyncServiceProxy::StopDownloadFile(const std::string &uri, bool needClean)
692 {
693     LOGI("StopDownloadFile Start");
694     MessageParcel data;
695     MessageParcel reply;
696     MessageOption option;
697 
698     if (!data.WriteInterfaceToken(GetDescriptor())) {
699         LOGE("Failed to write interface token");
700         return E_BROKEN_IPC;
701     }
702 
703     string path = uri;
704 #ifdef SUPPORT_MEDIA_LIBRARY
705     if (uri.find("file://media") == 0) {
706         OHOS::Media::MediaFileUri Muri(uri);
707         path = Muri.GetFilePath();
708     }
709 #endif
710     LOGI("StopDownloadFile Start, uri: %{public}s, path: %{public}s",
711          GetAnonyString(uri).c_str(), GetAnonyString(path).c_str());
712 
713     if (!data.WriteString(path)) {
714         LOGE("Failed to send the cloud id");
715         return E_INVAL_ARG;
716     }
717 
718     if (!data.WriteBool(needClean)) {
719         LOGE("Failed to send the needClean flag");
720         return E_INVAL_ARG;
721     }
722 
723     auto remote = Remote();
724     if (!remote) {
725         LOGE("remote is nullptr");
726         return E_BROKEN_IPC;
727     }
728     int32_t ret = remote->SendRequest(
729         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_DOWNLOAD_FILE), data, reply, option);
730     if (ret != E_OK) {
731         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
732         return E_BROKEN_IPC;
733     }
734     CloudDownloadUriManager& uriMgr = CloudDownloadUriManager::GetInstance();
735     uriMgr.RemoveUri(path);
736     LOGI("StopDownloadFile Success");
737     return reply.ReadInt32();
738 }
739 
StopFileCache(const int64_t & downloadId,bool needClean)740 int32_t CloudSyncServiceProxy::StopFileCache(const int64_t &downloadId,  bool needClean)
741 {
742     LOGI("StopFileCache Start");
743     MessageParcel data;
744     MessageParcel reply;
745     MessageOption option;
746 
747     if (!data.WriteInterfaceToken(GetDescriptor())) {
748         LOGE("Failed to write interface token");
749         return E_BROKEN_IPC;
750     }
751 
752     LOGI("StopFileCache Start, downloadId: %{public}lld, needClean: %{public}d",
753          static_cast<long long>(downloadId), needClean);
754 
755     if (!data.WriteInt64(downloadId)) {
756         LOGE("Failed to send the cloud id");
757         return E_INVAL_ARG;
758     }
759 
760     if (!data.WriteBool(needClean)) {
761         LOGE("Failed to send the needClean flag");
762         return E_INVAL_ARG;
763     }
764 
765     auto remote = Remote();
766     if (!remote) {
767         LOGE("remote is nullptr");
768         return E_BROKEN_IPC;
769     }
770     int32_t ret = remote->SendRequest(
771         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_FILE_CACHE), data, reply, option);
772     if (ret != E_OK) {
773         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
774         return ret;
775     }
776     // if StopFileCache finished, we need call RemoveUri later
777     LOGI("StopFileCache Success");
778     return reply.ReadInt32();
779 }
780 
RegisterDownloadFileCallback(const sptr<IRemoteObject> & downloadCallback)781 int32_t CloudSyncServiceProxy::RegisterDownloadFileCallback(const sptr<IRemoteObject> &downloadCallback)
782 {
783     LOGI("RegisterDownloadFileCallback Start");
784     MessageParcel data;
785     MessageParcel reply;
786     MessageOption option;
787 
788     if (!downloadCallback) {
789         LOGI("Empty callback stub");
790         return E_INVAL_ARG;
791     }
792 
793     if (!data.WriteInterfaceToken(GetDescriptor())) {
794         LOGE("Failed to write interface token");
795         return E_BROKEN_IPC;
796     }
797 
798     if (!data.WriteRemoteObject(downloadCallback)) {
799         LOGE("Failed to send the callback stub");
800         return E_INVAL_ARG;
801     }
802 
803     auto remote = Remote();
804     if (!remote) {
805         LOGE("remote is nullptr");
806         return E_BROKEN_IPC;
807     }
808     int32_t ret = remote->SendRequest(
809         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_DOWNLOAD_FILE_CALLBACK), data,
810         reply, option);
811     if (ret != E_OK) {
812         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
813         return E_BROKEN_IPC;
814     }
815     LOGI("RegisterDownloadFileCallback Success");
816     return reply.ReadInt32();
817 }
818 
UnregisterDownloadFileCallback()819 int32_t CloudSyncServiceProxy::UnregisterDownloadFileCallback()
820 {
821     LOGI("UnregisterDownloadFileCallback Start");
822     MessageParcel data;
823     MessageParcel reply;
824     MessageOption option;
825 
826     if (!data.WriteInterfaceToken(GetDescriptor())) {
827         LOGE("Failed to write interface token");
828         return E_BROKEN_IPC;
829     }
830 
831     auto remote = Remote();
832     if (!remote) {
833         LOGE("remote is nullptr");
834         return E_BROKEN_IPC;
835     }
836     int32_t ret = remote->SendRequest(
837         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UNREGISTER_DOWNLOAD_FILE_CALLBACK), data,
838         reply, option);
839     if (ret != E_OK) {
840         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
841         return E_BROKEN_IPC;
842     }
843     LOGI("UnregisterDownloadFileCallback Success");
844     return reply.ReadInt32();
845 }
846 
UploadAsset(const int32_t userId,const std::string & request,std::string & result)847 int32_t CloudSyncServiceProxy::UploadAsset(const int32_t userId, const std::string &request, std::string &result)
848 {
849     LOGD("UploadAsset");
850     MessageParcel data;
851     MessageParcel reply;
852     MessageOption option;
853 
854     if (!data.WriteInterfaceToken(GetDescriptor())) {
855         LOGE("Failed to write interface token");
856         return E_BROKEN_IPC;
857     }
858 
859     if (!data.WriteInt32(userId)) {
860         LOGE("Failed to send the user id");
861         return E_INVAL_ARG;
862     }
863 
864     if (!data.WriteString(request)) {
865         LOGE("Failed to send the request");
866         return E_INVAL_ARG;
867     }
868 
869     auto remote = Remote();
870     if (!remote) {
871         LOGE("remote is nullptr");
872         return E_BROKEN_IPC;
873     }
874     int32_t ret = remote->SendRequest(
875         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UPLOAD_ASSET), data, reply, option);
876     if (ret != E_OK) {
877         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
878         return E_BROKEN_IPC;
879     }
880     ret = reply.ReadInt32();
881     result = reply.ReadString();
882     LOGD("UploadAsset Success");
883     return ret;
884 }
885 
DownloadFile(const int32_t userId,const std::string & bundleName,AssetInfoObj & assetInfoObj)886 int32_t CloudSyncServiceProxy::DownloadFile(const int32_t userId,
887                                             const std::string &bundleName,
888                                             AssetInfoObj &assetInfoObj)
889 {
890     LOGI("DownloadFile");
891     MessageParcel data;
892     MessageParcel reply;
893     MessageOption option;
894 
895     if (!data.WriteInterfaceToken(GetDescriptor())) {
896         LOGE("Failed to write interface token");
897         return E_BROKEN_IPC;
898     }
899 
900     if (!data.WriteInt32(userId)) {
901         LOGE("Failed to send the user id");
902         return E_INVAL_ARG;
903     }
904 
905     if (!data.WriteString(bundleName)) {
906         LOGE("Failed to send the bundle name");
907         return E_INVAL_ARG;
908     }
909 
910     if (!data.WriteParcelable(&assetInfoObj)) {
911         LOGE("Failed to send the bundle assetInfo");
912         return E_INVAL_ARG;
913     }
914 
915     auto remote = Remote();
916     if (!remote) {
917         LOGE("remote is nullptr");
918         return E_BROKEN_IPC;
919     }
920     int32_t ret = remote->SendRequest(
921         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_FILE), data, reply, option);
922     if (ret != E_OK) {
923         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
924         return E_BROKEN_IPC;
925     }
926     LOGI("DownloadFile Success");
927     return reply.ReadInt32();
928 }
929 
DownloadFiles(const int32_t userId,const std::string & bundleName,const std::vector<AssetInfoObj> & assetInfoObj,std::vector<bool> & assetResultMap)930 int32_t CloudSyncServiceProxy::DownloadFiles(const int32_t userId,
931                                              const std::string &bundleName,
932                                              const std::vector<AssetInfoObj> &assetInfoObj,
933                                              std::vector<bool> &assetResultMap)
934 {
935     LOGI("DownloadFiles");
936     MessageParcel data;
937     MessageParcel reply;
938     MessageOption option;
939 
940     if (!data.WriteInterfaceToken(GetDescriptor())) {
941         LOGE("Failed to write interface token");
942         return E_BROKEN_IPC;
943     }
944 
945     if (!data.WriteInt32(userId)) {
946         LOGE("Failed to send the user id");
947         return E_INVAL_ARG;
948     }
949 
950     if (!data.WriteString(bundleName)) {
951         LOGE("Failed to send the bundle name");
952         return E_INVAL_ARG;
953     }
954 
955     if (assetInfoObj.size() > INT_MAX ||
956         assetInfoObj.size() == 0 ||
957         !data.WriteInt32(assetInfoObj.size())) {
958         LOGE("Failed to send the vector size");
959         return E_INVAL_ARG;
960     }
961 
962     for (const auto &obj : assetInfoObj) {
963         if (!data.WriteParcelable(&obj)) {
964             LOGE("Failed to send the assetInfoObj");
965             return E_INVAL_ARG;
966         }
967     }
968 
969     auto remote = Remote();
970     if (!remote) {
971         LOGE("remote is nullptr");
972         return E_BROKEN_IPC;
973     }
974     int32_t ret = remote->SendRequest(
975         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_FILES), data, reply, option);
976     if (ret != E_OK) {
977         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
978         return E_BROKEN_IPC;
979     }
980     LOGI("DownloadFile Success");
981     bool readParcel = reply.ReadBoolVector(&assetResultMap);
982     if (readParcel != true) {
983         LOGE("Failed to ReadBoolVector");
984         return E_INVAL_ARG;
985     }
986     return reply.ReadInt32();
987 }
988 
DownloadAsset(const uint64_t taskId,const int32_t userId,const std::string & bundleName,const std::string & networkId,AssetInfoObj & assetInfoObj)989 int32_t CloudSyncServiceProxy::DownloadAsset(const uint64_t taskId,
990                                              const int32_t userId,
991                                              const std::string &bundleName,
992                                              const std::string &networkId,
993                                              AssetInfoObj &assetInfoObj)
994 {
995     LOGI("DownloadFile");
996     MessageParcel data;
997     MessageParcel reply;
998     MessageOption option;
999 
1000     if (!data.WriteInterfaceToken(GetDescriptor())) {
1001         LOGE("Failed to write interface token");
1002         return E_BROKEN_IPC;
1003     }
1004 
1005     if (!data.WriteUint64(taskId)) {
1006         LOGE("Failed to send the task id");
1007         return E_INVAL_ARG;
1008     }
1009 
1010     if (!data.WriteInt32(userId)) {
1011         LOGE("Failed to send the user id");
1012         return E_INVAL_ARG;
1013     }
1014 
1015     if (!data.WriteString(bundleName)) {
1016         LOGE("Failed to send the bundle name");
1017         return E_INVAL_ARG;
1018     }
1019 
1020     if (!data.WriteString(networkId)) {
1021         LOGE("Failed to send the bundle name");
1022         return E_INVAL_ARG;
1023     }
1024 
1025     if (!data.WriteParcelable(&assetInfoObj)) {
1026         LOGE("Failed to send the bundle assetInfo");
1027         return E_INVAL_ARG;
1028     }
1029 
1030     auto remote = Remote();
1031     if (!remote) {
1032         LOGE("remote is nullptr");
1033         return E_BROKEN_IPC;
1034     }
1035     int32_t ret = remote->SendRequest(
1036         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_ASSET), data, reply, option);
1037     if (ret != E_OK) {
1038         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
1039         return E_BROKEN_IPC;
1040     }
1041     LOGI("DownloadFile Success");
1042     return reply.ReadInt32();
1043 }
1044 
RegisterDownloadAssetCallback(const sptr<IRemoteObject> & remoteObject)1045 int32_t CloudSyncServiceProxy::RegisterDownloadAssetCallback(const sptr<IRemoteObject> &remoteObject)
1046 {
1047     LOGI("Start RegisterDownloadAssetCallback");
1048     MessageParcel data;
1049     MessageParcel reply;
1050     MessageOption option;
1051 
1052     if (!remoteObject) {
1053         LOGI("Empty callback stub");
1054         return E_INVAL_ARG;
1055     }
1056 
1057     if (!data.WriteInterfaceToken(GetDescriptor())) {
1058         LOGE("Failed to write interface token");
1059         return E_BROKEN_IPC;
1060     }
1061 
1062     if (!data.WriteRemoteObject(remoteObject)) {
1063         LOGE("Failed to send the callback stub");
1064         return E_INVAL_ARG;
1065     }
1066 
1067     auto remote = Remote();
1068     if (!remote) {
1069         LOGE("remote is nullptr");
1070         return E_BROKEN_IPC;
1071     }
1072     int32_t ret = remote->SendRequest(
1073         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_DOWNLOAD_ASSET_CALLBACK), data,
1074         reply, option);
1075     if (ret != E_OK) {
1076         stringstream ss;
1077         ss << "Failed to send out the requeset, errno:" << ret;
1078         LOGE("%{public}s", ss.str().c_str());
1079         return E_BROKEN_IPC;
1080     }
1081     LOGI("RegisterDownloadAssetCallback Success");
1082     return reply.ReadInt32();
1083 }
1084 
DeleteAsset(const int32_t userId,const std::string & uri)1085 int32_t CloudSyncServiceProxy::DeleteAsset(const int32_t userId, const std::string &uri)
1086 {
1087     LOGD("DeleteAsset");
1088     MessageParcel data;
1089     MessageParcel reply;
1090     MessageOption option;
1091 
1092     if (!data.WriteInterfaceToken(GetDescriptor())) {
1093         LOGE("Failed to write interface token");
1094         return E_BROKEN_IPC;
1095     }
1096 
1097     if (!data.WriteInt32(userId)) {
1098         LOGE("Failed to send the user id");
1099         return E_INVAL_ARG;
1100     }
1101 
1102     if (!data.WriteString(uri)) {
1103         LOGE("Failed to send the uri");
1104         return E_INVAL_ARG;
1105     }
1106 
1107     auto remote = Remote();
1108     if (!remote) {
1109         LOGE("remote is nullptr");
1110         return E_BROKEN_IPC;
1111     }
1112     int32_t ret = remote->SendRequest(
1113         static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DELETE_ASSET), data, reply, option);
1114     if (ret != E_OK) {
1115         LOGE("Failed to send out the requeset, errno: %{public}d", ret);
1116         return E_BROKEN_IPC;
1117     }
1118     ret = reply.ReadInt32();
1119     LOGI("DeleteAsset Success");
1120     return ret;
1121 }
1122 
GetInstance()1123 sptr<ICloudSyncService> CloudSyncServiceProxy::GetInstance()
1124 {
1125     LOGD("getinstance");
1126     unique_lock<mutex> lock(instanceMutex_);
1127     if (serviceProxy_ != nullptr) {
1128         return serviceProxy_;
1129     }
1130 
1131     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1132     if (samgr == nullptr) {
1133         LOGE("Samgr is nullptr");
1134         return nullptr;
1135     }
1136     sptr<ServiceProxyLoadCallback> cloudSyncLoadCallback = new ServiceProxyLoadCallback();
1137     if (cloudSyncLoadCallback == nullptr) {
1138         LOGE("cloudSyncLoadCallback is nullptr");
1139         return nullptr;
1140     }
1141     int32_t ret = samgr->LoadSystemAbility(FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, cloudSyncLoadCallback);
1142     if (ret != E_OK) {
1143         LOGE("Failed to Load systemAbility, systemAbilityId:%{pulbic}d, ret code:%{pulbic}d",
1144              FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, ret);
1145         return nullptr;
1146     }
1147     unique_lock<mutex> proxyLock(proxyMutex_);
1148     auto waitStatus = cloudSyncLoadCallback->proxyConVar_.wait_for(
1149         proxyLock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS),
1150         [cloudSyncLoadCallback]() { return cloudSyncLoadCallback->isLoadSuccess_.load(); });
1151     if (!waitStatus) {
1152         LOGE("Load CloudSynd SA timeout");
1153         return nullptr;
1154     }
1155     return serviceProxy_;
1156 }
1157 
InvaildInstance()1158 void CloudSyncServiceProxy::InvaildInstance()
1159 {
1160     LOGI("invalid instance");
1161     unique_lock<mutex> lock(instanceMutex_);
1162     serviceProxy_ = nullptr;
1163     CloudDownloadUriManager::GetInstance().Reset();
1164 }
1165 
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)1166 void CloudSyncServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilitySuccess(
1167     int32_t systemAbilityId,
1168     const sptr<IRemoteObject> &remoteObject)
1169 {
1170     LOGI("Load CloudSync SA success,systemAbilityId:%{public}d, remoteObj result:%{private}s", systemAbilityId,
1171          (remoteObject == nullptr ? "false" : "true"));
1172     unique_lock<mutex> lock(proxyMutex_);
1173     if (serviceProxy_ != nullptr) {
1174         LOGE("CloudSync SA proxy has been loaded");
1175     } else {
1176         serviceProxy_ = iface_cast<ICloudSyncService>(remoteObject);
1177     }
1178     isLoadSuccess_.store(true);
1179     proxyConVar_.notify_one();
1180 }
1181 
OnLoadSystemAbilityFail(int32_t systemAbilityId)1182 void CloudSyncServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
1183 {
1184     LOGI("Load CloudSync SA failed,systemAbilityId:%{public}d", systemAbilityId);
1185     unique_lock<mutex> lock(proxyMutex_);
1186     serviceProxy_ = nullptr;
1187     isLoadSuccess_.store(false);
1188     proxyConVar_.notify_one();
1189 }
1190 } // namespace OHOS::FileManagement::CloudSync
1191