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
StartFileCache(const std::vector<std::string> & uriVec,int64_t & downloadId)601 int32_t CloudSyncServiceProxy::StartFileCache(const std::vector<std::string> &uriVec,
602 int64_t &downloadId)
603 {
604 LOGI("StartFileCache 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 std::vector<std::string> pathVec;
615 for (unsigned long i = 0; i < uriVec.size(); i++) {
616 string path = uriVec[i];
617 #ifdef SUPPORT_MEDIA_LIBRARY
618 if (uriVec[i].find("file://media") == 0) {
619 OHOS::Media::MediaFileUri mediaUri(uriVec[i]);
620 path = mediaUri.GetFilePath();
621 }
622 #endif
623 CloudDownloadUriManager &uriMgr = CloudDownloadUriManager::GetInstance();
624 uriMgr.AddPathToUri(path, uriVec[i]);
625 pathVec.push_back(path);
626
627 LOGI("StartFileCache Start, uriVec[%{public}ld]: %{public}s, path: %{public}s",
628 i, GetAnonyString(uriVec[i]).c_str(), GetAnonyString(path).c_str());
629 }
630
631 if (!data.WriteStringVector(pathVec)) {
632 LOGE("Failed to send the cloud id");
633 return E_INVAL_ARG;
634 }
635
636 auto remote = Remote();
637 if (!remote) {
638 LOGE("remote is nullptr");
639 return E_BROKEN_IPC;
640 }
641 int32_t ret = remote->SendRequest(
642 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_FILE_CACHE), data, reply, option);
643 if (ret != E_OK) {
644 LOGE("Failed to send out the request, errno %{public}d", ret);
645 return ret;
646 }
647
648 downloadId = reply.ReadInt64();
649
650 CloudDownloadUriManager &uriMgr = CloudDownloadUriManager::GetInstance();
651 uriMgr.AddDownloadIdToPath(downloadId, pathVec);
652
653 LOGI("StartFileCache Success, downloadId: %{public}lld", static_cast<long long>(downloadId));
654
655 return reply.ReadInt32();
656 }
657
StopDownloadFile(const std::string & uri,bool needClean)658 int32_t CloudSyncServiceProxy::StopDownloadFile(const std::string &uri, bool needClean)
659 {
660 LOGI("StopDownloadFile Start");
661 MessageParcel data;
662 MessageParcel reply;
663 MessageOption option;
664
665 if (!data.WriteInterfaceToken(GetDescriptor())) {
666 LOGE("Failed to write interface token");
667 return E_BROKEN_IPC;
668 }
669
670 string path = uri;
671 #ifdef SUPPORT_MEDIA_LIBRARY
672 if (uri.find("file://media") == 0) {
673 OHOS::Media::MediaFileUri Muri(uri);
674 path = Muri.GetFilePath();
675 }
676 #endif
677 LOGI("StopDownloadFile Start, uri: %{public}s, path: %{public}s",
678 GetAnonyString(uri).c_str(), GetAnonyString(path).c_str());
679
680 if (!data.WriteString(path)) {
681 LOGE("Failed to send the cloud id");
682 return E_INVAL_ARG;
683 }
684
685 if (!data.WriteBool(needClean)) {
686 LOGE("Failed to send the needClean flag");
687 return E_INVAL_ARG;
688 }
689
690 auto remote = Remote();
691 if (!remote) {
692 LOGE("remote is nullptr");
693 return E_BROKEN_IPC;
694 }
695 int32_t ret = remote->SendRequest(
696 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_DOWNLOAD_FILE), data, reply, option);
697 if (ret != E_OK) {
698 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
699 return E_BROKEN_IPC;
700 }
701 CloudDownloadUriManager& uriMgr = CloudDownloadUriManager::GetInstance();
702 uriMgr.RemoveUri(path);
703 LOGI("StopDownloadFile Success");
704 return reply.ReadInt32();
705 }
706
StopFileCache(const int64_t & downloadId,bool needClean)707 int32_t CloudSyncServiceProxy::StopFileCache(const int64_t &downloadId, bool needClean)
708 {
709 LOGI("StopFileCache Start");
710 MessageParcel data;
711 MessageParcel reply;
712 MessageOption option;
713
714 if (!data.WriteInterfaceToken(GetDescriptor())) {
715 LOGE("Failed to write interface token");
716 return E_BROKEN_IPC;
717 }
718
719 LOGI("StopFileCache Start, downloadId: %{public}lld, needClean: %{public}d",
720 static_cast<long long>(downloadId), needClean);
721
722 if (!data.WriteInt64(downloadId)) {
723 LOGE("Failed to send the cloud id");
724 return E_INVAL_ARG;
725 }
726
727 if (!data.WriteBool(needClean)) {
728 LOGE("Failed to send the needClean flag");
729 return E_INVAL_ARG;
730 }
731
732 auto remote = Remote();
733 if (!remote) {
734 LOGE("remote is nullptr");
735 return E_BROKEN_IPC;
736 }
737 int32_t ret = remote->SendRequest(
738 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_FILE_CACHE), data, reply, option);
739 if (ret != E_OK) {
740 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
741 return ret;
742 }
743 // if StopFileCache finished, we need call RemoveUri later
744 LOGI("StopFileCache Success");
745 return reply.ReadInt32();
746 }
747
RegisterDownloadFileCallback(const sptr<IRemoteObject> & downloadCallback)748 int32_t CloudSyncServiceProxy::RegisterDownloadFileCallback(const sptr<IRemoteObject> &downloadCallback)
749 {
750 LOGI("RegisterDownloadFileCallback Start");
751 MessageParcel data;
752 MessageParcel reply;
753 MessageOption option;
754
755 if (!downloadCallback) {
756 LOGI("Empty callback stub");
757 return E_INVAL_ARG;
758 }
759
760 if (!data.WriteInterfaceToken(GetDescriptor())) {
761 LOGE("Failed to write interface token");
762 return E_BROKEN_IPC;
763 }
764
765 if (!data.WriteRemoteObject(downloadCallback)) {
766 LOGE("Failed to send the callback stub");
767 return E_INVAL_ARG;
768 }
769
770 auto remote = Remote();
771 if (!remote) {
772 LOGE("remote is nullptr");
773 return E_BROKEN_IPC;
774 }
775 int32_t ret = remote->SendRequest(
776 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_DOWNLOAD_FILE_CALLBACK), data,
777 reply, option);
778 if (ret != E_OK) {
779 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
780 return E_BROKEN_IPC;
781 }
782 LOGI("RegisterDownloadFileCallback Success");
783 return reply.ReadInt32();
784 }
785
UnregisterDownloadFileCallback()786 int32_t CloudSyncServiceProxy::UnregisterDownloadFileCallback()
787 {
788 LOGI("UnregisterDownloadFileCallback Start");
789 MessageParcel data;
790 MessageParcel reply;
791 MessageOption option;
792
793 if (!data.WriteInterfaceToken(GetDescriptor())) {
794 LOGE("Failed to write interface token");
795 return E_BROKEN_IPC;
796 }
797
798 auto remote = Remote();
799 if (!remote) {
800 LOGE("remote is nullptr");
801 return E_BROKEN_IPC;
802 }
803 int32_t ret = remote->SendRequest(
804 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UNREGISTER_DOWNLOAD_FILE_CALLBACK), data,
805 reply, option);
806 if (ret != E_OK) {
807 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
808 return E_BROKEN_IPC;
809 }
810 LOGI("UnregisterDownloadFileCallback Success");
811 return reply.ReadInt32();
812 }
813
UploadAsset(const int32_t userId,const std::string & request,std::string & result)814 int32_t CloudSyncServiceProxy::UploadAsset(const int32_t userId, const std::string &request, std::string &result)
815 {
816 LOGD("UploadAsset");
817 MessageParcel data;
818 MessageParcel reply;
819 MessageOption option;
820
821 if (!data.WriteInterfaceToken(GetDescriptor())) {
822 LOGE("Failed to write interface token");
823 return E_BROKEN_IPC;
824 }
825
826 if (!data.WriteInt32(userId)) {
827 LOGE("Failed to send the user id");
828 return E_INVAL_ARG;
829 }
830
831 if (!data.WriteString(request)) {
832 LOGE("Failed to send the request");
833 return E_INVAL_ARG;
834 }
835
836 auto remote = Remote();
837 if (!remote) {
838 LOGE("remote is nullptr");
839 return E_BROKEN_IPC;
840 }
841 int32_t ret = remote->SendRequest(
842 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UPLOAD_ASSET), data, reply, option);
843 if (ret != E_OK) {
844 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
845 return E_BROKEN_IPC;
846 }
847 ret = reply.ReadInt32();
848 result = reply.ReadString();
849 LOGD("UploadAsset Success");
850 return ret;
851 }
852
DownloadFile(const int32_t userId,const std::string & bundleName,AssetInfoObj & assetInfoObj)853 int32_t CloudSyncServiceProxy::DownloadFile(const int32_t userId,
854 const std::string &bundleName,
855 AssetInfoObj &assetInfoObj)
856 {
857 LOGI("DownloadFile");
858 MessageParcel data;
859 MessageParcel reply;
860 MessageOption option;
861
862 if (!data.WriteInterfaceToken(GetDescriptor())) {
863 LOGE("Failed to write interface token");
864 return E_BROKEN_IPC;
865 }
866
867 if (!data.WriteInt32(userId)) {
868 LOGE("Failed to send the user id");
869 return E_INVAL_ARG;
870 }
871
872 if (!data.WriteString(bundleName)) {
873 LOGE("Failed to send the bundle name");
874 return E_INVAL_ARG;
875 }
876
877 if (!data.WriteParcelable(&assetInfoObj)) {
878 LOGE("Failed to send the bundle assetInfo");
879 return E_INVAL_ARG;
880 }
881
882 auto remote = Remote();
883 if (!remote) {
884 LOGE("remote is nullptr");
885 return E_BROKEN_IPC;
886 }
887 int32_t ret = remote->SendRequest(
888 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_FILE), data, reply, option);
889 if (ret != E_OK) {
890 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
891 return E_BROKEN_IPC;
892 }
893 LOGI("DownloadFile Success");
894 return reply.ReadInt32();
895 }
896
DownloadFiles(const int32_t userId,const std::string & bundleName,const std::vector<AssetInfoObj> & assetInfoObj,std::vector<bool> & assetResultMap)897 int32_t CloudSyncServiceProxy::DownloadFiles(const int32_t userId,
898 const std::string &bundleName,
899 const std::vector<AssetInfoObj> &assetInfoObj,
900 std::vector<bool> &assetResultMap)
901 {
902 LOGI("DownloadFiles");
903 MessageParcel data;
904 MessageParcel reply;
905 MessageOption option;
906
907 if (!data.WriteInterfaceToken(GetDescriptor())) {
908 LOGE("Failed to write interface token");
909 return E_BROKEN_IPC;
910 }
911
912 if (!data.WriteInt32(userId)) {
913 LOGE("Failed to send the user id");
914 return E_INVAL_ARG;
915 }
916
917 if (!data.WriteString(bundleName)) {
918 LOGE("Failed to send the bundle name");
919 return E_INVAL_ARG;
920 }
921
922 if (assetInfoObj.size() > INT_MAX ||
923 assetInfoObj.size() == 0 ||
924 !data.WriteInt32(assetInfoObj.size())) {
925 LOGE("Failed to send the vector size");
926 return E_INVAL_ARG;
927 }
928
929 for (const auto &obj : assetInfoObj) {
930 if (!data.WriteParcelable(&obj)) {
931 LOGE("Failed to send the assetInfoObj");
932 return E_INVAL_ARG;
933 }
934 }
935
936 auto remote = Remote();
937 if (!remote) {
938 LOGE("remote is nullptr");
939 return E_BROKEN_IPC;
940 }
941 int32_t ret = remote->SendRequest(
942 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_FILES), data, reply, option);
943 if (ret != E_OK) {
944 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
945 return E_BROKEN_IPC;
946 }
947 LOGI("DownloadFile Success");
948 bool readParcel = reply.ReadBoolVector(&assetResultMap);
949 if (readParcel != true) {
950 LOGE("Failed to ReadBoolVector");
951 return E_INVAL_ARG;
952 }
953 return reply.ReadInt32();
954 }
955
DownloadAsset(const uint64_t taskId,const int32_t userId,const std::string & bundleName,const std::string & networkId,AssetInfoObj & assetInfoObj)956 int32_t CloudSyncServiceProxy::DownloadAsset(const uint64_t taskId,
957 const int32_t userId,
958 const std::string &bundleName,
959 const std::string &networkId,
960 AssetInfoObj &assetInfoObj)
961 {
962 LOGI("DownloadFile");
963 MessageParcel data;
964 MessageParcel reply;
965 MessageOption option;
966
967 if (!data.WriteInterfaceToken(GetDescriptor())) {
968 LOGE("Failed to write interface token");
969 return E_BROKEN_IPC;
970 }
971
972 if (!data.WriteUint64(taskId)) {
973 LOGE("Failed to send the task id");
974 return E_INVAL_ARG;
975 }
976
977 if (!data.WriteInt32(userId)) {
978 LOGE("Failed to send the user id");
979 return E_INVAL_ARG;
980 }
981
982 if (!data.WriteString(bundleName)) {
983 LOGE("Failed to send the bundle name");
984 return E_INVAL_ARG;
985 }
986
987 if (!data.WriteString(networkId)) {
988 LOGE("Failed to send the bundle name");
989 return E_INVAL_ARG;
990 }
991
992 if (!data.WriteParcelable(&assetInfoObj)) {
993 LOGE("Failed to send the bundle assetInfo");
994 return E_INVAL_ARG;
995 }
996
997 auto remote = Remote();
998 if (!remote) {
999 LOGE("remote is nullptr");
1000 return E_BROKEN_IPC;
1001 }
1002 int32_t ret = remote->SendRequest(
1003 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_ASSET), data, reply, option);
1004 if (ret != E_OK) {
1005 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
1006 return E_BROKEN_IPC;
1007 }
1008 LOGI("DownloadFile Success");
1009 return reply.ReadInt32();
1010 }
1011
RegisterDownloadAssetCallback(const sptr<IRemoteObject> & remoteObject)1012 int32_t CloudSyncServiceProxy::RegisterDownloadAssetCallback(const sptr<IRemoteObject> &remoteObject)
1013 {
1014 LOGI("Start RegisterDownloadAssetCallback");
1015 MessageParcel data;
1016 MessageParcel reply;
1017 MessageOption option;
1018
1019 if (!remoteObject) {
1020 LOGI("Empty callback stub");
1021 return E_INVAL_ARG;
1022 }
1023
1024 if (!data.WriteInterfaceToken(GetDescriptor())) {
1025 LOGE("Failed to write interface token");
1026 return E_BROKEN_IPC;
1027 }
1028
1029 if (!data.WriteRemoteObject(remoteObject)) {
1030 LOGE("Failed to send the callback stub");
1031 return E_INVAL_ARG;
1032 }
1033
1034 auto remote = Remote();
1035 if (!remote) {
1036 LOGE("remote is nullptr");
1037 return E_BROKEN_IPC;
1038 }
1039 int32_t ret = remote->SendRequest(
1040 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_DOWNLOAD_ASSET_CALLBACK), data,
1041 reply, option);
1042 if (ret != E_OK) {
1043 stringstream ss;
1044 ss << "Failed to send out the requeset, errno:" << ret;
1045 LOGE("%{public}s", ss.str().c_str());
1046 return E_BROKEN_IPC;
1047 }
1048 LOGI("RegisterDownloadAssetCallback Success");
1049 return reply.ReadInt32();
1050 }
1051
DeleteAsset(const int32_t userId,const std::string & uri)1052 int32_t CloudSyncServiceProxy::DeleteAsset(const int32_t userId, const std::string &uri)
1053 {
1054 LOGD("DeleteAsset");
1055 MessageParcel data;
1056 MessageParcel reply;
1057 MessageOption option;
1058
1059 if (!data.WriteInterfaceToken(GetDescriptor())) {
1060 LOGE("Failed to write interface token");
1061 return E_BROKEN_IPC;
1062 }
1063
1064 if (!data.WriteInt32(userId)) {
1065 LOGE("Failed to send the user id");
1066 return E_INVAL_ARG;
1067 }
1068
1069 if (!data.WriteString(uri)) {
1070 LOGE("Failed to send the uri");
1071 return E_INVAL_ARG;
1072 }
1073
1074 auto remote = Remote();
1075 if (!remote) {
1076 LOGE("remote is nullptr");
1077 return E_BROKEN_IPC;
1078 }
1079 int32_t ret = remote->SendRequest(
1080 static_cast<uint32_t>(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DELETE_ASSET), data, reply, option);
1081 if (ret != E_OK) {
1082 LOGE("Failed to send out the requeset, errno: %{public}d", ret);
1083 return E_BROKEN_IPC;
1084 }
1085 ret = reply.ReadInt32();
1086 LOGI("DeleteAsset Success");
1087 return ret;
1088 }
1089
GetInstance()1090 sptr<ICloudSyncService> CloudSyncServiceProxy::GetInstance()
1091 {
1092 LOGD("getinstance");
1093 unique_lock<mutex> lock(instanceMutex_);
1094 if (serviceProxy_ != nullptr) {
1095 return serviceProxy_;
1096 }
1097
1098 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1099 if (samgr == nullptr) {
1100 LOGE("Samgr is nullptr");
1101 return nullptr;
1102 }
1103 sptr<ServiceProxyLoadCallback> cloudSyncLoadCallback = new ServiceProxyLoadCallback();
1104 if (cloudSyncLoadCallback == nullptr) {
1105 LOGE("cloudSyncLoadCallback is nullptr");
1106 return nullptr;
1107 }
1108 int32_t ret = samgr->LoadSystemAbility(FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, cloudSyncLoadCallback);
1109 if (ret != E_OK) {
1110 LOGE("Failed to Load systemAbility, systemAbilityId:%{pulbic}d, ret code:%{pulbic}d",
1111 FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, ret);
1112 return nullptr;
1113 }
1114 unique_lock<mutex> proxyLock(proxyMutex_);
1115 auto waitStatus = cloudSyncLoadCallback->proxyConVar_.wait_for(
1116 proxyLock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS),
1117 [cloudSyncLoadCallback]() { return cloudSyncLoadCallback->isLoadSuccess_.load(); });
1118 if (!waitStatus) {
1119 LOGE("Load CloudSynd SA timeout");
1120 return nullptr;
1121 }
1122 return serviceProxy_;
1123 }
1124
InvaildInstance()1125 void CloudSyncServiceProxy::InvaildInstance()
1126 {
1127 LOGI("invalid instance");
1128 unique_lock<mutex> lock(instanceMutex_);
1129 serviceProxy_ = nullptr;
1130 CloudDownloadUriManager::GetInstance().Reset();
1131 }
1132
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)1133 void CloudSyncServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilitySuccess(
1134 int32_t systemAbilityId,
1135 const sptr<IRemoteObject> &remoteObject)
1136 {
1137 LOGI("Load CloudSync SA success,systemAbilityId:%{public}d, remoteObj result:%{private}s", systemAbilityId,
1138 (remoteObject == nullptr ? "false" : "true"));
1139 unique_lock<mutex> lock(proxyMutex_);
1140 if (serviceProxy_ != nullptr) {
1141 LOGE("CloudSync SA proxy has been loaded");
1142 } else {
1143 serviceProxy_ = iface_cast<ICloudSyncService>(remoteObject);
1144 }
1145 isLoadSuccess_.store(true);
1146 proxyConVar_.notify_one();
1147 }
1148
OnLoadSystemAbilityFail(int32_t systemAbilityId)1149 void CloudSyncServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
1150 {
1151 LOGI("Load CloudSync SA failed,systemAbilityId:%{public}d", systemAbilityId);
1152 unique_lock<mutex> lock(proxyMutex_);
1153 serviceProxy_ = nullptr;
1154 isLoadSuccess_.store(false);
1155 proxyConVar_.notify_one();
1156 }
1157 } // namespace OHOS::FileManagement::CloudSync
1158