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()35 int32_t CloudSyncServiceProxy::UnRegisterCallbackInner()
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 int32_t ret = remote->SendRequest(
53 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UNREGISTER_CALLBACK), data, reply, option);
54 if (ret != E_OK) {
55 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
56 return E_BROKEN_IPC;
57 }
58 LOGI("UnRegisterCallbackInner Success");
59 return reply.ReadInt32();
60 }
61
RegisterCallbackInner(const sptr<IRemoteObject> & remoteObject)62 int32_t CloudSyncServiceProxy::RegisterCallbackInner(const sptr<IRemoteObject> &remoteObject)
63 {
64 LOGI("Start RegisterCallbackInner");
65 MessageParcel data;
66 MessageParcel reply;
67 MessageOption option;
68
69 if (!remoteObject) {
70 LOGI("Empty callback stub");
71 return E_INVAL_ARG;
72 }
73
74 if (!data.WriteInterfaceToken(GetDescriptor())) {
75 LOGE("Failed to write interface token");
76 return E_BROKEN_IPC;
77 }
78
79 if (!data.WriteRemoteObject(remoteObject)) {
80 LOGE("Failed to send the callback stub");
81 return E_INVAL_ARG;
82 }
83
84 auto remote = Remote();
85 if (!remote) {
86 LOGE("remote is nullptr");
87 return E_BROKEN_IPC;
88 }
89 int32_t ret = remote->SendRequest(
90 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_CALLBACK), data, reply, option);
91 if (ret != E_OK) {
92 LOGE("Failed to send out the request, errno: %{public}d", ret);
93 return E_BROKEN_IPC;
94 }
95 LOGI("RegisterCallbackInner Success");
96 return reply.ReadInt32();
97 }
98
StartSyncInner(bool forceFlag)99 int32_t CloudSyncServiceProxy::StartSyncInner(bool forceFlag)
100 {
101 LOGI("Start Sync");
102 MessageParcel data;
103 MessageParcel reply;
104 MessageOption option;
105
106 if (!data.WriteInterfaceToken(GetDescriptor())) {
107 LOGE("Failed to write interface token");
108 return E_BROKEN_IPC;
109 }
110
111 if (!data.WriteBool(forceFlag)) {
112 LOGE("Failed to send the force flag");
113 return E_INVAL_ARG;
114 }
115
116 auto remote = Remote();
117 if (!remote) {
118 LOGE("remote is nullptr");
119 return E_BROKEN_IPC;
120 }
121 int32_t ret = remote->SendRequest(static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_SYNC),
122 data, reply, option);
123 if (ret != E_OK) {
124 LOGE("Failed to send out the request, errno: %{public}d", ret);
125 return E_BROKEN_IPC;
126 }
127 LOGI("StartSyncInner Success");
128 return reply.ReadInt32();
129 }
130
TriggerSyncInner(const std::string & bundleName,const int32_t & userId)131 int32_t CloudSyncServiceProxy::TriggerSyncInner(const std::string &bundleName, const int32_t &userId)
132 {
133 LOGI("Trigger Sync");
134 MessageParcel data;
135 MessageParcel reply;
136 MessageOption option;
137
138 if (!data.WriteInterfaceToken(GetDescriptor())) {
139 LOGE("Failed to write interface token");
140 return E_BROKEN_IPC;
141 }
142
143 if (!data.WriteString(bundleName)) {
144 LOGE("Failed to send the bundle name");
145 return E_INVAL_ARG;
146 }
147
148 if (!data.WriteInt32(userId)) {
149 LOGE("Failed to send the user id");
150 return E_INVAL_ARG;
151 }
152
153 auto remote = Remote();
154 if (!remote) {
155 LOGE("remote is nullptr");
156 return E_BROKEN_IPC;
157 }
158 int32_t ret = remote->SendRequest(
159 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_TRIGGER_SYNC), data, reply, option);
160 if (ret != E_OK) {
161 LOGE("Failed to send out the request, errno: %{public}d", ret);
162 return E_BROKEN_IPC;
163 }
164 LOGI("TriggerSyncInner Success");
165 return reply.ReadInt32();
166 }
167
GetSyncTimeInner(int64_t & syncTime)168 int32_t CloudSyncServiceProxy::GetSyncTimeInner(int64_t &syncTime)
169 {
170 LOGI("Start GetSyncTimeInner");
171 LOGI("Start Sync");
172 MessageParcel data;
173 MessageParcel reply;
174 MessageOption option;
175
176 if (!data.WriteInterfaceToken(GetDescriptor())) {
177 LOGE("Failed to write interface token");
178 return E_BROKEN_IPC;
179 }
180
181 auto remote = Remote();
182 if (!remote) {
183 LOGE("remote is nullptr");
184 return E_BROKEN_IPC;
185 }
186
187 int32_t ret = remote->SendRequest(
188 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_GET_SYNC_TIME), data, reply, option);
189 if (ret != E_OK) {
190 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
191 return E_BROKEN_IPC;
192 }
193
194 syncTime = reply.ReadInt64();
195
196 return reply.ReadInt32();
197 }
198
CleanCacheInner(const std::string & uri)199 int32_t CloudSyncServiceProxy::CleanCacheInner(const std::string &uri)
200 {
201 LOGI("Start CleanCacheInner");
202 MessageParcel data;
203 MessageParcel reply;
204 MessageOption option;
205
206 if (!data.WriteInterfaceToken(GetDescriptor())) {
207 LOGE("Failed to write interface token");
208 return E_BROKEN_IPC;
209 }
210
211 if (!data.WriteString(uri)) {
212 LOGE("Failed to send the uri");
213 return E_BROKEN_IPC;
214 }
215
216 auto remote = Remote();
217 if (!remote) {
218 LOGE("remote is nullptr");
219 return E_BROKEN_IPC;
220 }
221 int32_t ret = remote->SendRequest(static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_CLEAN_CACHE),
222 data, reply, option);
223 if (ret != E_OK) {
224 LOGE("Failed to send out the request, errno: %{public}d", ret);
225 return E_BROKEN_IPC;
226 }
227
228 return reply.ReadInt32();
229 }
230
StopSyncInner()231 int32_t CloudSyncServiceProxy::StopSyncInner()
232 {
233 LOGI("StopSync");
234 MessageParcel data;
235 MessageParcel reply;
236 MessageOption option;
237
238 if (!data.WriteInterfaceToken(GetDescriptor())) {
239 LOGE("Failed to write interface token");
240 return E_BROKEN_IPC;
241 }
242
243 auto remote = Remote();
244 if (!remote) {
245 LOGE("remote is nullptr");
246 return E_BROKEN_IPC;
247 }
248 int32_t ret = remote->SendRequest(static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_SYNC),
249 data, reply, option);
250 if (ret != E_OK) {
251 LOGE("Failed to send out the request, errno: %{public}d", ret);
252 return E_BROKEN_IPC;
253 }
254 LOGI("StopSyncInner Success");
255 return reply.ReadInt32();
256 }
257
ChangeAppSwitch(const std::string & accoutId,const std::string & bundleName,bool status)258 int32_t CloudSyncServiceProxy::ChangeAppSwitch(const std::string &accoutId, const std::string &bundleName, bool status)
259 {
260 LOGI("ChangeAppSwitch");
261 MessageParcel data;
262 MessageParcel reply;
263 MessageOption option;
264
265 if (!data.WriteInterfaceToken(GetDescriptor())) {
266 LOGE("Failed to write interface token");
267 return E_BROKEN_IPC;
268 }
269
270 if (!data.WriteString(accoutId)) {
271 LOGE("Failed to send the account id");
272 return E_INVAL_ARG;
273 }
274
275 if (!data.WriteString(bundleName)) {
276 LOGE("Failed to send the bundle name");
277 return E_INVAL_ARG;
278 }
279
280 if (!data.WriteBool(status)) {
281 LOGE("Failed to send the switch status");
282 return E_INVAL_ARG;
283 }
284
285 auto remote = Remote();
286 if (!remote) {
287 LOGE("remote is nullptr");
288 return E_BROKEN_IPC;
289 }
290 int32_t ret = remote->SendRequest(
291 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_CHANGE_APP_SWITCH), data, reply, option);
292 if (ret != E_OK) {
293 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
294 return E_BROKEN_IPC;
295 }
296 LOGI("ChangeAppSwitch Success");
297 return reply.ReadInt32();
298 }
299
Clean(const std::string & accountId,const CleanOptions & cleanOptions)300 int32_t CloudSyncServiceProxy::Clean(const std::string &accountId, const CleanOptions &cleanOptions)
301 {
302 LOGI("Clean");
303 MessageParcel data;
304 MessageParcel reply;
305 MessageOption option;
306
307 if (!data.WriteInterfaceToken(GetDescriptor())) {
308 LOGE("Failed to write interface token");
309 return E_BROKEN_IPC;
310 }
311
312 if (!data.WriteString(accountId)) {
313 LOGE("Failed to send the account id");
314 return E_INVAL_ARG;
315 }
316
317 if (!data.WriteParcelable(&cleanOptions)) {
318 LOGE("failed to write cleanOptions");
319 return E_INVAL_ARG;
320 }
321
322 auto remote = Remote();
323 if (!remote) {
324 LOGE("remote is nullptr");
325 return E_BROKEN_IPC;
326 }
327 int32_t ret = remote->SendRequest(static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_CLEAN), data,
328 reply, option);
329 if (ret != E_OK) {
330 LOGE("Failed to send out the request, errno: %{public}d", ret);
331 return E_BROKEN_IPC;
332 }
333 LOGI("Clean Success");
334 return reply.ReadInt32();
335 }
336
EnableCloud(const std::string & accoutId,const SwitchDataObj & switchData)337 int32_t CloudSyncServiceProxy::EnableCloud(const std::string &accoutId, const SwitchDataObj &switchData)
338 {
339 LOGI("EnableCloud");
340 MessageParcel data;
341 MessageParcel reply;
342 MessageOption option;
343
344 if (!data.WriteInterfaceToken(GetDescriptor())) {
345 LOGE("Failed to write interface token");
346 return E_BROKEN_IPC;
347 }
348
349 if (!data.WriteString(accoutId)) {
350 LOGE("Failed to send the account id");
351 return E_INVAL_ARG;
352 }
353
354 if (!data.WriteParcelable(&switchData)) {
355 LOGE("Failed to send the bundle switch");
356 return E_INVAL_ARG;
357 }
358
359 auto remote = Remote();
360 if (!remote) {
361 LOGE("remote is nullptr");
362 return E_BROKEN_IPC;
363 }
364 int32_t ret = remote->SendRequest(
365 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_ENABLE_CLOUD), data, reply, option);
366 if (ret != E_OK) {
367 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
368 return E_BROKEN_IPC;
369 }
370 LOGI("EnableCloud Success");
371 return reply.ReadInt32();
372 }
373
DisableCloud(const std::string & accoutId)374 int32_t CloudSyncServiceProxy::DisableCloud(const std::string &accoutId)
375 {
376 LOGI("DisableCloud");
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(accoutId)) {
387 LOGE("Failed to send the account id");
388 return E_INVAL_ARG;
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(
397 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DISABLE_CLOUD), data, reply, option);
398 if (ret != E_OK) {
399 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
400 return E_BROKEN_IPC;
401 }
402 LOGI("DisableCloud Success");
403 return reply.ReadInt32();
404 }
405
NotifyDataChange(const std::string & accoutId,const std::string & bundleName)406 int32_t CloudSyncServiceProxy::NotifyDataChange(const std::string &accoutId, const std::string &bundleName)
407 {
408 LOGI("NotifyDataChange");
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 if (!data.WriteString(accoutId)) {
419 LOGE("Failed to send the account id");
420 return E_INVAL_ARG;
421 }
422
423 if (!data.WriteString(bundleName)) {
424 LOGE("Failed to send the bundle name");
425 return E_INVAL_ARG;
426 }
427
428 auto remote = Remote();
429 if (!remote) {
430 LOGE("remote is nullptr");
431 return E_BROKEN_IPC;
432 }
433 int32_t ret = remote->SendRequest(
434 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_NOTIFY_DATA_CHANGE), data, reply, option);
435 if (ret != E_OK) {
436 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
437 return E_BROKEN_IPC;
438 }
439 LOGI("NotifyDataChange Success");
440 return reply.ReadInt32();
441 }
442
NotifyEventChange(int32_t userId,const std::string & eventId,const std::string & extraData)443 int32_t CloudSyncServiceProxy::NotifyEventChange(
444 int32_t userId, const std::string &eventId, const std::string &extraData)
445 {
446 LOGI("NotifyEventChange");
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 if (!data.WriteInt32(userId)) {
457 LOGE("Failed to send the user id");
458 return E_INVAL_ARG;
459 }
460
461 if (!data.WriteString(eventId)) {
462 LOGE("Failed to send the event id");
463 return E_INVAL_ARG;
464 }
465
466 if (!data.WriteString(extraData)) {
467 LOGE("Failed to send the extra data");
468 return E_INVAL_ARG;
469 }
470
471 auto remote = Remote();
472 if (!remote) {
473 LOGE("remote is nullptr");
474 return E_BROKEN_IPC;
475 }
476 int32_t ret = remote->SendRequest(
477 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_NOTIFY_EVENT_CHANGE), data, reply, option);
478 if (ret != E_OK) {
479 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
480 return E_BROKEN_IPC;
481 }
482 LOGI("NotifyEventChange Success");
483 return reply.ReadInt32();
484 }
485
StartDownloadFile(const std::string & uri)486 int32_t CloudSyncServiceProxy::StartDownloadFile(const std::string &uri)
487 {
488 #ifdef SUPPORT_MEDIA_LIBRARY
489 LOGI("StartDownloadFile Start");
490 MessageParcel data;
491 MessageParcel reply;
492 MessageOption option;
493
494 if (!data.WriteInterfaceToken(GetDescriptor())) {
495 LOGE("Failed to write interface token");
496 return E_BROKEN_IPC;
497 }
498
499 string path = uri;
500 if (uri.find("file://media") == 0) {
501 OHOS::Media::MediaFileUri mediaUri(uri);
502 path = mediaUri.GetFilePath();
503 }
504
505 LOGI("StartDownloadFile Start, uri: %{public}s, path: %{public}s", uri.c_str(), path.c_str());
506
507 CloudDownloadUriManager &uriMgr = CloudDownloadUriManager::GetInstance();
508 if (uriMgr.AddPathToUri(path, uri) == E_STOP) {
509 return E_OK;
510 }
511
512 if (!data.WriteString(path)) {
513 LOGE("Failed to send the cloud id");
514 return E_INVAL_ARG;
515 }
516
517 auto remote = Remote();
518 if (!remote) {
519 LOGE("remote is nullptr");
520 return E_BROKEN_IPC;
521 }
522 int32_t ret = remote->SendRequest(
523 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_DOWNLOAD_FILE), data, reply, option);
524 if (ret != E_OK) {
525 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
526 return E_BROKEN_IPC;
527 }
528 LOGI("StartDownloadFile Success");
529 return reply.ReadInt32();
530 #else
531 LOGE("Function StartDownloadFile is undefined");
532 return 0;
533 #endif
534 }
535
StartFileCache(const std::string & uri)536 int32_t CloudSyncServiceProxy::StartFileCache(const std::string &uri)
537 {
538 LOGI("StartFileCache Start");
539 MessageParcel data;
540 MessageParcel reply;
541 MessageOption option;
542
543 if (!data.WriteInterfaceToken(GetDescriptor())) {
544 LOGE("Failed to write interface token");
545 return E_BROKEN_IPC;
546 }
547
548 if (!data.WriteString(uri)) {
549 LOGE("Failed to send the cloud id");
550 return E_INVAL_ARG;
551 }
552
553 CloudDownloadUriManager &uriMgr = CloudDownloadUriManager::GetInstance();
554 if (uriMgr.AddPathToUri(uri, uri) == E_STOP) {
555 return E_OK;
556 }
557 auto remote = Remote();
558 if (!remote) {
559 LOGE("remote is nullptr");
560 return E_BROKEN_IPC;
561 }
562 int32_t ret = remote->SendRequest(
563 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_FILE_CACHE), data, reply, option);
564 if (ret != E_OK) {
565 LOGE("Failed to send out the request, errno %{public}d", ret);
566 return E_BROKEN_IPC;
567 }
568 LOGI("StartFileCache Success");
569 return reply.ReadInt32();
570 }
571
StopDownloadFile(const std::string & uri)572 int32_t CloudSyncServiceProxy::StopDownloadFile(const std::string &uri)
573 {
574 LOGI("StopDownloadFile Start");
575 MessageParcel data;
576 MessageParcel reply;
577 MessageOption option;
578
579 if (!data.WriteInterfaceToken(GetDescriptor())) {
580 LOGE("Failed to write interface token");
581 return E_BROKEN_IPC;
582 }
583
584 string path = uri;
585 #ifdef SUPPORT_MEDIA_LIBRARY
586 if (uri.find("file://media") == 0) {
587 OHOS::Media::MediaFileUri Muri(uri);
588 string path = Muri.GetFilePath();
589 }
590 #endif
591 LOGI("StopDownloadFile Start, uri: %{public}s, path: %{public}s", uri.c_str(), path.c_str());
592
593 if (!data.WriteString(path)) {
594 LOGE("Failed to send the cloud id");
595 return E_INVAL_ARG;
596 }
597
598 auto remote = Remote();
599 if (!remote) {
600 LOGE("remote is nullptr");
601 return E_BROKEN_IPC;
602 }
603 int32_t ret = remote->SendRequest(
604 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_DOWNLOAD_FILE), data, reply, option);
605 if (ret != E_OK) {
606 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
607 return E_BROKEN_IPC;
608 }
609 LOGI("StopDownloadFile Success");
610 return reply.ReadInt32();
611 }
612
RegisterDownloadFileCallback(const sptr<IRemoteObject> & downloadCallback)613 int32_t CloudSyncServiceProxy::RegisterDownloadFileCallback(const sptr<IRemoteObject> &downloadCallback)
614 {
615 LOGI("RegisterDownloadFileCallback Start");
616 MessageParcel data;
617 MessageParcel reply;
618 MessageOption option;
619
620 if (!downloadCallback) {
621 LOGI("Empty callback stub");
622 return E_INVAL_ARG;
623 }
624
625 if (!data.WriteInterfaceToken(GetDescriptor())) {
626 LOGE("Failed to write interface token");
627 return E_BROKEN_IPC;
628 }
629
630 if (!data.WriteRemoteObject(downloadCallback)) {
631 LOGE("Failed to send the callback stub");
632 return E_INVAL_ARG;
633 }
634
635 auto remote = Remote();
636 if (!remote) {
637 LOGE("remote is nullptr");
638 return E_BROKEN_IPC;
639 }
640 int32_t ret = remote->SendRequest(
641 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_DOWNLOAD_FILE_CALLBACK), data,
642 reply, option);
643 if (ret != E_OK) {
644 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
645 return E_BROKEN_IPC;
646 }
647 LOGI("RegisterDownloadFileCallback Success");
648 return reply.ReadInt32();
649 }
650
UnregisterDownloadFileCallback()651 int32_t CloudSyncServiceProxy::UnregisterDownloadFileCallback()
652 {
653 LOGI("UnregisterDownloadFileCallback Start");
654 MessageParcel data;
655 MessageParcel reply;
656 MessageOption option;
657
658 if (!data.WriteInterfaceToken(GetDescriptor())) {
659 LOGE("Failed to write interface token");
660 return E_BROKEN_IPC;
661 }
662
663 auto remote = Remote();
664 if (!remote) {
665 LOGE("remote is nullptr");
666 return E_BROKEN_IPC;
667 }
668 int32_t ret = remote->SendRequest(
669 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UNREGISTER_DOWNLOAD_FILE_CALLBACK), data,
670 reply, option);
671 if (ret != E_OK) {
672 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
673 return E_BROKEN_IPC;
674 }
675 LOGI("UnregisterDownloadFileCallback Success");
676 return reply.ReadInt32();
677 }
678
UploadAsset(const int32_t userId,const std::string & request,std::string & result)679 int32_t CloudSyncServiceProxy::UploadAsset(const int32_t userId, const std::string &request, std::string &result)
680 {
681 LOGI("UploadAsset");
682 MessageParcel data;
683 MessageParcel reply;
684 MessageOption option;
685
686 if (!data.WriteInterfaceToken(GetDescriptor())) {
687 LOGE("Failed to write interface token");
688 return E_BROKEN_IPC;
689 }
690
691 if (!data.WriteInt32(userId)) {
692 LOGE("Failed to send the user id");
693 return E_INVAL_ARG;
694 }
695
696 if (!data.WriteString(request)) {
697 LOGE("Failed to send the request");
698 return E_INVAL_ARG;
699 }
700
701 auto remote = Remote();
702 if (!remote) {
703 LOGE("remote is nullptr");
704 return E_BROKEN_IPC;
705 }
706 int32_t ret = remote->SendRequest(
707 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UPLOAD_ASSET), data, reply, option);
708 if (ret != E_OK) {
709 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
710 return E_BROKEN_IPC;
711 }
712 ret = reply.ReadInt32();
713 result = reply.ReadString();
714 LOGI("UploadAsset Success");
715 return ret;
716 }
717
DownloadFile(const int32_t userId,const std::string & bundleName,AssetInfoObj & assetInfoObj)718 int32_t CloudSyncServiceProxy::DownloadFile(const int32_t userId,
719 const std::string &bundleName,
720 AssetInfoObj &assetInfoObj)
721 {
722 LOGI("DownloadFile");
723 MessageParcel data;
724 MessageParcel reply;
725 MessageOption option;
726
727 if (!data.WriteInterfaceToken(GetDescriptor())) {
728 LOGE("Failed to write interface token");
729 return E_BROKEN_IPC;
730 }
731
732 if (!data.WriteInt32(userId)) {
733 LOGE("Failed to send the user id");
734 return E_INVAL_ARG;
735 }
736
737 if (!data.WriteString(bundleName)) {
738 LOGE("Failed to send the bundle name");
739 return E_INVAL_ARG;
740 }
741
742 if (!data.WriteParcelable(&assetInfoObj)) {
743 LOGE("Failed to send the bundle assetInfo");
744 return E_INVAL_ARG;
745 }
746
747 auto remote = Remote();
748 if (!remote) {
749 LOGE("remote is nullptr");
750 return E_BROKEN_IPC;
751 }
752 int32_t ret = remote->SendRequest(
753 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_FILE), data, reply, option);
754 if (ret != E_OK) {
755 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
756 return E_BROKEN_IPC;
757 }
758 LOGI("DownloadFile Success");
759 return reply.ReadInt32();
760 }
761
DownloadAsset(const uint64_t taskId,const int32_t userId,const std::string & bundleName,const std::string & networkId,AssetInfoObj & assetInfoObj)762 int32_t CloudSyncServiceProxy::DownloadAsset(const uint64_t taskId,
763 const int32_t userId,
764 const std::string &bundleName,
765 const std::string &networkId,
766 AssetInfoObj &assetInfoObj)
767 {
768 LOGI("DownloadFile");
769 MessageParcel data;
770 MessageParcel reply;
771 MessageOption option;
772
773 if (!data.WriteInterfaceToken(GetDescriptor())) {
774 LOGE("Failed to write interface token");
775 return E_BROKEN_IPC;
776 }
777
778 if (!data.WriteUint64(taskId)) {
779 LOGE("Failed to send the task id");
780 return E_INVAL_ARG;
781 }
782
783 if (!data.WriteInt32(userId)) {
784 LOGE("Failed to send the user id");
785 return E_INVAL_ARG;
786 }
787
788 if (!data.WriteString(bundleName)) {
789 LOGE("Failed to send the bundle name");
790 return E_INVAL_ARG;
791 }
792
793 if (!data.WriteString(networkId)) {
794 LOGE("Failed to send the bundle name");
795 return E_INVAL_ARG;
796 }
797
798 if (!data.WriteParcelable(&assetInfoObj)) {
799 LOGE("Failed to send the bundle assetInfo");
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_DOWNLOAD_ASSET), data, reply, option);
810 if (ret != E_OK) {
811 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
812 return E_BROKEN_IPC;
813 }
814 LOGI("DownloadFile Success");
815 return reply.ReadInt32();
816 }
817
RegisterDownloadAssetCallback(const sptr<IRemoteObject> & remoteObject)818 int32_t CloudSyncServiceProxy::RegisterDownloadAssetCallback(const sptr<IRemoteObject> &remoteObject)
819 {
820 LOGI("Start RegisterDownloadAssetCallback");
821 MessageParcel data;
822 MessageParcel reply;
823 MessageOption option;
824
825 if (!remoteObject) {
826 LOGI("Empty callback stub");
827 return E_INVAL_ARG;
828 }
829
830 if (!data.WriteInterfaceToken(GetDescriptor())) {
831 LOGE("Failed to write interface token");
832 return E_BROKEN_IPC;
833 }
834
835 if (!data.WriteRemoteObject(remoteObject)) {
836 LOGE("Failed to send the callback stub");
837 return E_INVAL_ARG;
838 }
839
840 auto remote = Remote();
841 if (!remote) {
842 LOGE("remote is nullptr");
843 return E_BROKEN_IPC;
844 }
845 int32_t ret = remote->SendRequest(
846 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_DOWNLOAD_ASSET_CALLBACK), data,
847 reply, option);
848 if (ret != E_OK) {
849 stringstream ss;
850 ss << "Failed to send out the requeset, errno:" << ret;
851 LOGE("%{public}s", ss.str().c_str());
852 return E_BROKEN_IPC;
853 }
854 LOGI("RegisterDownloadAssetCallback Success");
855 return reply.ReadInt32();
856 }
857
DeleteAsset(const int32_t userId,const std::string & uri)858 int32_t CloudSyncServiceProxy::DeleteAsset(const int32_t userId, const std::string &uri)
859 {
860 LOGD("DeleteAsset");
861 MessageParcel data;
862 MessageParcel reply;
863 MessageOption option;
864
865 if (!data.WriteInterfaceToken(GetDescriptor())) {
866 LOGE("Failed to write interface token");
867 return E_BROKEN_IPC;
868 }
869
870 if (!data.WriteInt32(userId)) {
871 LOGE("Failed to send the user id");
872 return E_INVAL_ARG;
873 }
874
875 if (!data.WriteString(uri)) {
876 LOGE("Failed to send the uri");
877 return E_INVAL_ARG;
878 }
879
880 auto remote = Remote();
881 if (!remote) {
882 LOGE("remote is nullptr");
883 return E_BROKEN_IPC;
884 }
885 int32_t ret = remote->SendRequest(
886 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DELETE_ASSET), data, reply, option);
887 if (ret != E_OK) {
888 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
889 return E_BROKEN_IPC;
890 }
891 ret = reply.ReadInt32();
892 LOGI("DeleteAsset Success");
893 return ret;
894 }
895
GetInstance()896 sptr<ICloudSyncService> CloudSyncServiceProxy::GetInstance()
897 {
898 LOGI("getinstance");
899 unique_lock<mutex> lock(proxyMutex_);
900 if (serviceProxy_ != nullptr) {
901 return serviceProxy_;
902 }
903
904 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
905 if (samgr == nullptr) {
906 LOGE("Samgr is nullptr");
907 return nullptr;
908 }
909 sptr<ServiceProxyLoadCallback> cloudSyncLoadCallback = new ServiceProxyLoadCallback();
910 if (cloudSyncLoadCallback == nullptr) {
911 LOGE("cloudSyncLoadCallback is nullptr");
912 return nullptr;
913 }
914 int32_t ret = samgr->LoadSystemAbility(FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, cloudSyncLoadCallback);
915 if (ret != E_OK) {
916 LOGE("Failed to Load systemAbility, systemAbilityId:%{pulbic}d, ret code:%{pulbic}d",
917 FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, ret);
918 return nullptr;
919 }
920 auto waitStatus = cloudSyncLoadCallback->proxyConVar_.wait_for(
921 lock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS),
922 [cloudSyncLoadCallback]() { return cloudSyncLoadCallback->isLoadSuccess_.load(); });
923 if (!waitStatus) {
924 LOGE("Load CloudSynd SA timeout");
925 return nullptr;
926 }
927 return serviceProxy_;
928 }
929
InvaildInstance()930 void CloudSyncServiceProxy::InvaildInstance()
931 {
932 LOGI("invalid instance");
933 unique_lock<mutex> lock(proxyMutex_);
934 serviceProxy_ = nullptr;
935 }
936
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)937 void CloudSyncServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilitySuccess(
938 int32_t systemAbilityId,
939 const sptr<IRemoteObject> &remoteObject)
940 {
941 LOGI("Load CloudSync SA success,systemAbilityId:%{public}d, remoteObj result:%{private}s", systemAbilityId,
942 (remoteObject == nullptr ? "false" : "true"));
943 unique_lock<mutex> lock(proxyMutex_);
944 if (serviceProxy_ != nullptr) {
945 LOGE("CloudSync SA proxy has been loaded");
946 } else {
947 serviceProxy_ = iface_cast<ICloudSyncService>(remoteObject);
948 }
949 isLoadSuccess_.store(true);
950 proxyConVar_.notify_one();
951 }
952
OnLoadSystemAbilityFail(int32_t systemAbilityId)953 void CloudSyncServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
954 {
955 LOGI("Load CloudSync SA failed,systemAbilityId:%{public}d", systemAbilityId);
956 unique_lock<mutex> lock(proxyMutex_);
957 serviceProxy_ = nullptr;
958 isLoadSuccess_.store(false);
959 proxyConVar_.notify_one();
960 }
961 } // namespace OHOS::FileManagement::CloudSync
962