• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "cloud_download_callback_client.h"
17 #include "cloud_sync_manager_impl.h"
18 #include "cloud_sync_callback_client.h"
19 #include "cloud_sync_service_proxy.h"
20 #include "dfs_error.h"
21 #include "system_ability_definition.h"
22 #include "iservice_registry.h"
23 #include "utils_directory.h"
24 #include "utils_log.h"
25 
26 namespace OHOS::FileManagement::CloudSync {
27 using namespace std;
28 constexpr int32_t MIN_USER_ID = 100;
29 constexpr int32_t MAX_FILE_CACHE_NUM = 400;
30 constexpr int32_t MAX_DENTRY_FILE_SIZE = 500;
GetInstance()31 CloudSyncManagerImpl &CloudSyncManagerImpl::GetInstance()
32 {
33     static CloudSyncManagerImpl instance;
34     return instance;
35 }
36 
RegisterCallback(const std::shared_ptr<CloudSyncCallback> callback,const std::string & bundleName)37 int32_t CloudSyncManagerImpl::RegisterCallback(const std::shared_ptr<CloudSyncCallback> callback,
38                                                const std::string &bundleName)
39 {
40     if (!callback) {
41         LOGE("callback is null");
42         return E_INVAL_ARG;
43     }
44     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
45     if (!CloudSyncServiceProxy) {
46         LOGE("proxy is null");
47         return E_SA_LOAD_FAILED;
48     }
49     auto ret = CloudSyncServiceProxy->RegisterCallbackInner(sptr(new (std::nothrow) CloudSyncCallbackClient(callback)),
50                                                             bundleName);
51     {
52         unique_lock<mutex> lock(callbackMutex_);
53         callback_ = callback;
54     }
55     SubscribeListener(bundleName);
56     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
57     LOGI("RegisterCallback ret %{public}d", ret);
58     return ret;
59 }
60 
RegisterFileSyncCallback(const std::shared_ptr<CloudSyncCallback> callback,const std::string & bundleName)61 int32_t CloudSyncManagerImpl::RegisterFileSyncCallback(const std::shared_ptr<CloudSyncCallback> callback,
62     const std::string &bundleName)
63 {
64     if (!callback) {
65         LOGE("callback is null");
66         return E_INVAL_ARG;
67     }
68     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
69     if (!CloudSyncServiceProxy) {
70         LOGE("proxy is null");
71         return E_SA_LOAD_FAILED;
72     }
73     auto ret = CloudSyncServiceProxy->RegisterFileSyncCallbackInner(
74         sptr(new (std::nothrow) CloudSyncCallbackClient(callback)), bundleName);
75     {
76         unique_lock<mutex> lock(callbackMutex_);
77         callback_ = callback;
78     }
79     SubscribeListener(bundleName);
80     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
81     LOGI("RegisterFileSyncCallback ret %{public}d", ret);
82     return ret;
83 }
84 
UnRegisterCallback(const std::string & bundleName)85 int32_t CloudSyncManagerImpl::UnRegisterCallback(const std::string &bundleName)
86 {
87     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
88     if (!CloudSyncServiceProxy) {
89         LOGE("proxy is null");
90         return E_SA_LOAD_FAILED;
91     }
92 
93     auto ret = CloudSyncServiceProxy->UnRegisterCallbackInner(bundleName);
94     if (!ret) {
95         {
96             unique_lock<mutex> lock(callbackMutex_);
97             callback_ = nullptr;
98         }
99         SubscribeListener();
100     }
101     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
102     LOGI("UnRegisterCallback ret %{public}d", ret);
103     return ret;
104 }
105 
UnRegisterFileSyncCallback(const std::string & bundleName)106 int32_t CloudSyncManagerImpl::UnRegisterFileSyncCallback(const std::string &bundleName)
107 {
108     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
109     if (!CloudSyncServiceProxy) {
110         LOGE("proxy is null");
111         return E_SA_LOAD_FAILED;
112     }
113 
114     auto ret = CloudSyncServiceProxy->UnRegisterFileSyncCallbackInner(bundleName);
115     if (!ret) {
116         {
117             unique_lock<mutex> lock(callbackMutex_);
118             callback_ = nullptr;
119         }
120         SubscribeListener();
121     }
122     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
123     LOGI("UnRegisterFileSyncCallback ret %{public}d", ret);
124     return ret;
125 }
126 
StartSync(const std::string & bundleName)127 int32_t CloudSyncManagerImpl::StartSync(const std::string &bundleName)
128 {
129     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
130     if (!CloudSyncServiceProxy) {
131         LOGE("proxy is null");
132         return E_SA_LOAD_FAILED;
133     }
134     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
135     return CloudSyncServiceProxy->StartSyncInner(true, bundleName);
136 }
137 
StartFileSync(const std::string & bundleName)138 int32_t CloudSyncManagerImpl::StartFileSync(const std::string &bundleName)
139 {
140     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
141     if (!CloudSyncServiceProxy) {
142         LOGE("proxy is null");
143         return E_SA_LOAD_FAILED;
144     }
145     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
146     return CloudSyncServiceProxy->StartFileSyncInner(true, bundleName);
147 }
148 
GetSyncTime(int64_t & syncTime,const std::string & bundleName)149 int32_t CloudSyncManagerImpl::GetSyncTime(int64_t &syncTime, const std::string &bundleName)
150 {
151     LOGI("GetSyncTime Start");
152     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
153     if (!CloudSyncServiceProxy) {
154         LOGE("proxy is null");
155         return E_SA_LOAD_FAILED;
156     }
157     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
158     return CloudSyncServiceProxy->GetSyncTimeInner(syncTime, bundleName);
159 }
160 
OptimizeStorage(const OptimizeSpaceOptions & optimizeOptions,const std::shared_ptr<CloudOptimizeCallback> optimizeCallback)161 int32_t CloudSyncManagerImpl::OptimizeStorage(const OptimizeSpaceOptions &optimizeOptions,
162     const std::shared_ptr<CloudOptimizeCallback> optimizeCallback)
163 {
164     LOGI("OptimizeStorage Start");
165     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
166     if (!CloudSyncServiceProxy) {
167         LOGE("proxy is null");
168         return E_SA_LOAD_FAILED;
169     }
170     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
171 
172     bool isCallbackValid = false;
173     sptr<CloudOptimizeCallbackClient> opCallback = nullptr;
174     if (optimizeCallback != nullptr) {
175         opCallback = sptr(new (std::nothrow) CloudOptimizeCallbackClient(optimizeCallback));
176         isCallbackValid = true;
177         if (opCallback == nullptr) {
178             isCallbackValid = false;
179             LOGE("OptimizeStorage callback failed");
180         }
181     }
182 
183     return CloudSyncServiceProxy->OptimizeStorage(optimizeOptions, isCallbackValid, opCallback);
184 }
185 
StopOptimizeStorage()186 int32_t CloudSyncManagerImpl::StopOptimizeStorage()
187 {
188     LOGI("StopOptimizeStorage Start");
189     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
190     if (!CloudSyncServiceProxy) {
191         LOGE("proxy is null");
192         return E_SA_LOAD_FAILED;
193     }
194     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
195 
196     return CloudSyncServiceProxy->StopOptimizeStorage();
197 }
198 
StartSync(bool forceFlag,const std::shared_ptr<CloudSyncCallback> callback)199 int32_t CloudSyncManagerImpl::StartSync(bool forceFlag, const std::shared_ptr<CloudSyncCallback> callback)
200 {
201     if (!callback) {
202         LOGE("callback is null");
203         return E_INVAL_ARG;
204     }
205     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
206     if (!CloudSyncServiceProxy) {
207         LOGE("proxy is null");
208         return E_SA_LOAD_FAILED;
209     }
210 
211     if (!isFirstCall_.test()) {
212         LOGI("Register callback");
213         auto ret =
214             CloudSyncServiceProxy->RegisterCallbackInner(sptr(new (std::nothrow) CloudSyncCallbackClient(callback)));
215         if (ret) {
216             LOGE("Register callback failed");
217             isFirstCall_.clear();
218             return ret;
219         }
220         callback_ = callback;
221         SubscribeListener();
222         SetDeathRecipient(CloudSyncServiceProxy->AsObject());
223     }
224 
225     return CloudSyncServiceProxy->StartSyncInner(forceFlag);
226 }
227 
TriggerSync(const std::string & bundleName,const int32_t & userId)228 int32_t CloudSyncManagerImpl::TriggerSync(const std::string &bundleName, const int32_t &userId)
229 {
230     if (bundleName.empty() || userId < MIN_USER_ID) {
231         LOGE("Trigger Sync parameter is invalid");
232         return E_INVAL_ARG;
233     }
234     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
235     if (!CloudSyncServiceProxy) {
236         LOGE("proxy is null");
237         return E_SA_LOAD_FAILED;
238     }
239     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
240     return CloudSyncServiceProxy->TriggerSyncInner(bundleName, userId);
241 }
242 
StopSync(const std::string & bundleName,bool forceFlag)243 int32_t CloudSyncManagerImpl::StopSync(const std::string &bundleName, bool forceFlag)
244 {
245     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
246     if (!CloudSyncServiceProxy) {
247         LOGE("proxy is null");
248         return E_SA_LOAD_FAILED;
249     }
250     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
251     return CloudSyncServiceProxy->StopSyncInner(bundleName, forceFlag);
252 }
253 
StopFileSync(const std::string & bundleName,bool forceFlag)254 int32_t CloudSyncManagerImpl::StopFileSync(const std::string &bundleName, bool forceFlag)
255 {
256     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
257     if (!CloudSyncServiceProxy) {
258         LOGE("proxy is null");
259         return E_SA_LOAD_FAILED;
260     }
261     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
262     return CloudSyncServiceProxy->StopFileSyncInner(bundleName, forceFlag);
263 }
264 
ResetCursor(const std::string & bundleName)265 int32_t CloudSyncManagerImpl::ResetCursor(const std::string &bundleName)
266 {
267     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
268     if (!CloudSyncServiceProxy) {
269         LOGE("proxy is null");
270         return E_SA_LOAD_FAILED;
271     }
272     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
273     return CloudSyncServiceProxy->ResetCursor(bundleName);
274 }
275 
ChangeAppSwitch(const std::string & accoutId,const std::string & bundleName,bool status)276 int32_t CloudSyncManagerImpl::ChangeAppSwitch(const std::string &accoutId, const std::string &bundleName, bool status)
277 {
278     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
279     if (!CloudSyncServiceProxy) {
280         LOGE("proxy is null");
281         return E_SA_LOAD_FAILED;
282     }
283 
284     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
285 
286     int32_t ret = CloudSyncServiceProxy->ChangeAppSwitch(accoutId, bundleName, status);
287     LOGI("ChangeAppSwitch ret %{public}d", ret);
288     return ret;
289 }
290 
NotifyDataChange(const std::string & accoutId,const std::string & bundleName)291 int32_t CloudSyncManagerImpl::NotifyDataChange(const std::string &accoutId, const std::string &bundleName)
292 {
293     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
294     if (!CloudSyncServiceProxy) {
295         LOGE("proxy is null");
296         return E_SA_LOAD_FAILED;
297     }
298 
299     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
300 
301     int32_t ret = CloudSyncServiceProxy->NotifyDataChange(accoutId, bundleName);
302     LOGI("NotifyDataChange ret %{public}d", ret);
303     return ret;
304 }
305 
NotifyEventChange(int32_t userId,const std::string & eventId,const std::string & extraData)306 int32_t CloudSyncManagerImpl::NotifyEventChange(
307     int32_t userId, const std::string &eventId, const std::string &extraData)
308 {
309     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
310     if (!CloudSyncServiceProxy) {
311         LOGE("proxy is null");
312         return E_SA_LOAD_FAILED;
313     }
314 
315     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
316 
317     int32_t ret = CloudSyncServiceProxy->NotifyEventChange(userId, eventId, extraData);
318     LOGI("NotifyDataChange ret %{public}d", ret);
319     return ret;
320 }
321 
StartDownloadFile(const std::string & uri)322 int32_t CloudSyncManagerImpl::StartDownloadFile(const std::string &uri)
323 {
324     LOGI("StartDownloadFile start");
325     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
326     if (!CloudSyncServiceProxy) {
327         LOGE("proxy is null");
328         return E_SA_LOAD_FAILED;
329     }
330     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
331     int32_t ret = CloudSyncServiceProxy->StartDownloadFile(uri);
332     LOGI("StartDownloadFile ret %{public}d", ret);
333     return ret;
334 }
335 
BatchCleanFile(const std::vector<CleanFileInfo> & fileInfo,std::vector<std::string> & failCloudId)336 int32_t CloudSyncManagerImpl::BatchCleanFile(const std::vector<CleanFileInfo> &fileInfo,
337     std::vector<std::string> &failCloudId)
338 {
339     if (fileInfo.size() > CLEAN_FILE_MAX_SIZE) {
340         LOGE("BatchCleanFile size over max limit");
341         return E_INVAL_ARG;
342     }
343     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
344     if (!CloudSyncServiceProxy) {
345         LOGE("proxy is null");
346         return E_SA_LOAD_FAILED;
347     }
348 
349     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
350     std::vector<CleanFileInfoObj> fileInfoObj;
351     for (const auto &info : fileInfo) {
352         CleanFileInfoObj obj(info);
353         fileInfoObj.emplace_back(obj);
354     }
355 
356     return CloudSyncServiceProxy->BatchCleanFile(fileInfoObj, failCloudId);
357 }
358 
StartFileCache(const std::string & uri)359 int32_t CloudSyncManagerImpl::StartFileCache(const std::string &uri)
360 {
361     LOGI("StartFileCache start");
362     int64_t downloadId = 0;
363     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
364     if (!CloudSyncServiceProxy) {
365         LOGE("proxy is null");
366         return E_SA_LOAD_FAILED;
367     }
368     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
369     std::vector<std::string> uriVec;
370     uriVec.push_back(uri);
371     int32_t ret = CloudSyncServiceProxy->StartFileCache(uriVec, downloadId);
372     LOGI("StartFileCache ret %{public}d", ret);
373     return ret;
374 }
375 
StartFileCache(const std::vector<std::string> & uriVec,int64_t & downloadId,std::bitset<FIELD_KEY_MAX_SIZE> fieldkey,const std::shared_ptr<CloudDownloadCallback> downloadCallback,int32_t timeout)376 int32_t CloudSyncManagerImpl::StartFileCache(const std::vector<std::string> &uriVec,
377                                              int64_t &downloadId, std::bitset<FIELD_KEY_MAX_SIZE> fieldkey,
378                                              const std::shared_ptr<CloudDownloadCallback> downloadCallback,
379                                              int32_t timeout)
380 {
381     LOGI("StartFileCache batch start, uriVec size: %{public}zu, fieldKey: %{public}llu, Callback is null: %{public}d",
382          uriVec.size(), static_cast<unsigned long long>(fieldkey.to_ulong()), (downloadCallback == nullptr));
383     if (uriVec.empty()) {
384         LOGE("StartFileCache, uri list is empty");
385         return E_INVAL_ARG;
386     }
387     if (uriVec.size() > MAX_FILE_CACHE_NUM) {
388         LOGE("StartFileCache, the size of uri list exceeded the maximum limit, size: %{public}zu", uriVec.size());
389         return E_EXCEED_MAX_SIZE;
390     }
391     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
392     if (!CloudSyncServiceProxy) {
393         LOGE("proxy is null");
394         return E_SA_LOAD_FAILED;
395     }
396     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
397 
398     bool isCallbackValid = false;
399     sptr<CloudDownloadCallbackClient> dlCallback = nullptr;
400     if (downloadCallback != nullptr) {
401         dlCallback = sptr(new (std::nothrow) CloudDownloadCallbackClient(downloadCallback));
402         isCallbackValid = true;
403         if (dlCallback == nullptr) {
404             LOGE("register download callback failed");
405             isCallbackValid = false;
406         }
407     }
408 
409     int32_t ret = CloudSyncServiceProxy->StartFileCache(uriVec, downloadId, fieldkey,
410         isCallbackValid, dlCallback, timeout);
411     LOGI("StartFileCache batch ret %{public}d", ret);
412     return ret;
413 }
414 
StopDownloadFile(const std::string & uri,bool needClean)415 int32_t CloudSyncManagerImpl::StopDownloadFile(const std::string &uri, bool needClean)
416 {
417     LOGI("StopDownloadFile start");
418     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
419     if (!CloudSyncServiceProxy) {
420         LOGE("proxy is null");
421         return E_SA_LOAD_FAILED;
422     }
423     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
424     int32_t ret = CloudSyncServiceProxy->StopDownloadFile(uri, needClean);
425     LOGI("StopDownloadFile ret %{public}d", ret);
426     return ret;
427 }
428 
StopFileCache(int64_t downloadId,bool needClean,int32_t timeout)429 int32_t CloudSyncManagerImpl::StopFileCache(int64_t downloadId, bool needClean, int32_t timeout)
430 {
431     LOGI("StopFileCache start");
432     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
433     if (!CloudSyncServiceProxy) {
434         LOGE("proxy is null");
435         return E_SA_LOAD_FAILED;
436     }
437     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
438     int32_t ret = CloudSyncServiceProxy->StopFileCache(downloadId, needClean, timeout);
439     LOGI("StopFileCache ret %{public}d", ret);
440     return ret;
441 }
442 
DownloadThumb()443 int32_t CloudSyncManagerImpl::DownloadThumb()
444 {
445     LOGI("DownloadThumb start");
446     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
447     if (!CloudSyncServiceProxy) {
448         LOGE("proxy is null");
449         return E_SA_LOAD_FAILED;
450     }
451     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
452     int32_t ret = CloudSyncServiceProxy->DownloadThumb();
453     LOGI("DownloadThumb ret %{public}d", ret);
454     return ret;
455 }
456 
RegisterDownloadFileCallback(const std::shared_ptr<CloudDownloadCallback> downloadCallback)457 int32_t CloudSyncManagerImpl::RegisterDownloadFileCallback(
458     const std::shared_ptr<CloudDownloadCallback> downloadCallback)
459 {
460     LOGI("RegisterDownloadFileCallback start");
461     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
462     if (!CloudSyncServiceProxy) {
463         LOGE("proxy is null");
464         return E_SA_LOAD_FAILED;
465     }
466     {
467         unique_lock<mutex> lock(downloadMutex_);
468         auto dlCallback = sptr(new (std::nothrow) CloudDownloadCallbackClient(downloadCallback));
469         if (dlCallback == nullptr ||
470             CloudSyncServiceProxy->RegisterDownloadFileCallback(dlCallback) != E_OK) {
471             LOGE("register download callback failed");
472         } else {
473             downloadCallback_ = downloadCallback;
474         }
475     }
476     SubscribeListener();
477     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
478     return E_OK;
479 }
480 
RegisterFileCacheCallback(const std::shared_ptr<CloudDownloadCallback> downloadCallback)481 int32_t CloudSyncManagerImpl::RegisterFileCacheCallback(
482     const std::shared_ptr<CloudDownloadCallback> downloadCallback)
483 {
484     LOGI("RegisterFileCacheCallback start");
485     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
486     if (!CloudSyncServiceProxy) {
487         LOGE("proxy is null");
488         return E_SA_LOAD_FAILED;
489     }
490     {
491         unique_lock<mutex> lock(downloadMutex_);
492         auto dlCallback = sptr(new (std::nothrow) CloudDownloadCallbackClient(downloadCallback));
493         if (dlCallback == nullptr ||
494             CloudSyncServiceProxy->RegisterFileCacheCallback(dlCallback) != E_OK) {
495             LOGE("register download callback failed");
496         } else {
497             downloadCallback_ = downloadCallback;
498         }
499     }
500     SubscribeListener();
501     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
502     return E_OK;
503 }
504 
UnregisterDownloadFileCallback()505 int32_t CloudSyncManagerImpl::UnregisterDownloadFileCallback()
506 {
507     LOGI("UnregisterDownloadFileCallback start");
508     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
509     if (!CloudSyncServiceProxy) {
510         LOGE("proxy is null");
511         return E_SA_LOAD_FAILED;
512     }
513     int32_t ret = E_OK;
514     {
515         unique_lock<mutex> lock(downloadMutex_);
516         ret = CloudSyncServiceProxy->UnregisterDownloadFileCallback();
517         LOGI("UnregisterDownloadFileCallback ret %{public}d", ret);
518         if (ret == E_OK) {
519             downloadCallback_ = nullptr;
520         }
521     }
522     SubscribeListener();
523     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
524     return ret;
525 }
526 
UnregisterFileCacheCallback()527 int32_t CloudSyncManagerImpl::UnregisterFileCacheCallback()
528 {
529     LOGI("UnregisterFileCacheCallback start");
530     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
531     if (!CloudSyncServiceProxy) {
532         LOGE("proxy is null");
533         return E_SA_LOAD_FAILED;
534     }
535     int32_t ret = E_OK;
536     {
537         unique_lock<mutex> lock(downloadMutex_);
538         ret = CloudSyncServiceProxy->UnregisterFileCacheCallback();
539         LOGI("UnregisterFileCacheCallback ret %{public}d", ret);
540         if (ret == E_OK) {
541             downloadCallback_ = nullptr;
542         }
543     }
544     SubscribeListener();
545     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
546     return ret;
547 }
548 
SetDeathRecipient(const sptr<IRemoteObject> & remoteObject)549 void CloudSyncManagerImpl::SetDeathRecipient(const sptr<IRemoteObject> &remoteObject)
550 {
551     if (!isFirstCall_.test_and_set()) {
552         auto deathCallback = [this](const wptr<IRemoteObject> &obj) {
553             LOGE("service died.");
554             CloudSyncServiceProxy::InvaildInstance();
555             if (callback_) {
556                 callback_->OnDeathRecipient();
557             }
558             isFirstCall_.clear();
559         };
560         deathRecipient_ = sptr(new SvcDeathRecipient(deathCallback));
561         if (!remoteObject->AddDeathRecipient(deathRecipient_)) {
562             LOGE("add death recipient failed");
563             isFirstCall_.clear();
564         }
565     }
566 }
567 
EnableCloud(const std::string & accoutId,const SwitchDataObj & switchData)568 int32_t CloudSyncManagerImpl::EnableCloud(const std::string &accoutId,
569                                           const SwitchDataObj &switchData)
570 {
571     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
572     if (!CloudSyncServiceProxy) {
573         LOGE("proxy is null");
574         return E_SA_LOAD_FAILED;
575     }
576 
577     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
578 
579     return CloudSyncServiceProxy->EnableCloud(accoutId, switchData);
580 }
581 
DisableCloud(const std::string & accoutId)582 int32_t CloudSyncManagerImpl::DisableCloud(const std::string &accoutId)
583 {
584     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
585     if (!CloudSyncServiceProxy) {
586         LOGE("proxy is null");
587         return E_SA_LOAD_FAILED;
588     }
589 
590     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
591     return CloudSyncServiceProxy->DisableCloud(accoutId);
592 }
593 
Clean(const std::string & accountId,const CleanOptions & cleanOptions)594 int32_t CloudSyncManagerImpl::Clean(const std::string &accountId, const CleanOptions &cleanOptions)
595 {
596     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
597     if (!CloudSyncServiceProxy) {
598         LOGE("proxy is null");
599         return E_SA_LOAD_FAILED;
600     }
601 
602     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
603 
604     return CloudSyncServiceProxy->Clean(accountId, cleanOptions);
605 }
606 
CleanCache(const std::string & uri)607 int32_t CloudSyncManagerImpl::CleanCache(const std::string &uri)
608 {
609     LOGI("CleanCache Start");
610     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
611     if (!CloudSyncServiceProxy) {
612         LOGE("proxy is null");
613         return E_SA_LOAD_FAILED;
614     }
615     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
616     return CloudSyncServiceProxy->CleanCacheInner(uri);
617 }
618 
BatchDentryFileInsert(const std::vector<DentryFileInfo> & fileInfo,std::vector<std::string> & failCloudId)619 int32_t CloudSyncManagerImpl::BatchDentryFileInsert(const std::vector<DentryFileInfo> &fileInfo,
620     std::vector<std::string> &failCloudId)
621 {
622     if (fileInfo.size() > MAX_DENTRY_FILE_SIZE) {
623         LOGE("BatchDentryFileInsert parameter is invalid");
624         return E_INVAL_ARG;
625     }
626     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
627     if (!CloudSyncServiceProxy) {
628         LOGE("proxy is null");
629         return E_SA_LOAD_FAILED;
630     }
631 
632     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
633     std::vector<DentryFileInfoObj> fileInfoObj;
634     for (const auto &info : fileInfo) {
635         DentryFileInfoObj obj(info);
636         fileInfoObj.emplace_back(obj);
637     }
638 
639     return CloudSyncServiceProxy->BatchDentryFileInsert(fileInfoObj, failCloudId);
640 }
641 
SubscribeListener(std::string bundleName)642 void CloudSyncManagerImpl::SubscribeListener(std::string bundleName)
643 {
644     unique_lock<mutex> lock(subscribeMutex_);
645     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
646     if (samgr == nullptr) {
647         LOGE("Samgr is nullptr");
648         return;
649     }
650     if (listener_ != nullptr) {
651         auto ret = samgr->UnSubscribeSystemAbility(FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, listener_);
652         LOGI("unsubscribed to systemAbility ret %{public}d", ret);
653     }
654     if (callback_ != nullptr || downloadCallback_ != nullptr) {
655         listener_ = new SystemAbilityStatusChange(bundleName);
656         auto ret = samgr->SubscribeSystemAbility(FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, listener_);
657         LOGI("subscribed to systemAbility ret %{public}d", ret);
658     } else {
659         listener_ = nullptr;
660     }
661 }
662 
ResetProxyCallback(uint32_t retryCount,const string & bundleName)663 bool CloudSyncManagerImpl::ResetProxyCallback(uint32_t retryCount, const string &bundleName)
664 {
665     auto cloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
666     if (cloudSyncServiceProxy == nullptr) {
667         LOGE("proxy is null");
668         return false;
669     }
670     bool hasCallback = false;
671     {
672         unique_lock<mutex> downloadLock(downloadMutex_);
673         if (downloadCallback_ != nullptr) {
674             auto dlCallback = sptr(new (std::nothrow) CloudDownloadCallbackClient(downloadCallback_));
675             if (dlCallback == nullptr ||
676                 cloudSyncServiceProxy->RegisterFileCacheCallback(dlCallback) != E_OK) {
677                 LOGW("register download callback failed, try time is %{public}d", retryCount);
678             } else {
679                 hasCallback = true;
680             }
681         }
682     }
683     if (callback_ != nullptr) {
684         auto callback = sptr(new (std::nothrow) CloudSyncCallbackClient(callback_));
685         if (callback == nullptr ||
686             cloudSyncServiceProxy->RegisterCallbackInner(callback, bundleName) != E_OK) {
687             LOGW("register callback failed, try time is %{public}d", retryCount);
688         } else {
689             hasCallback = true;
690         }
691     }
692     if (hasCallback) {
693         CloudSyncManagerImpl::GetInstance().SetDeathRecipient(cloudSyncServiceProxy->AsObject());
694     }
695     return true;
696 }
697 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)698 void CloudSyncManagerImpl::SystemAbilityStatusChange::OnAddSystemAbility(int32_t systemAbilityId,
699     const std::string &deviceId)
700 {
701     const uint32_t RETRY_TIMES = 3;
702     const uint32_t SLEEP_TIME = 20 * 1000;
703     uint32_t retryCount = 0;
704     LOGI("saId %{public}d loaded", systemAbilityId);
705     do {
706         usleep(SLEEP_TIME);
707         if (!CloudSyncManagerImpl::GetInstance().ResetProxyCallback(retryCount, bundleName_)) {
708             continue;
709         }
710         return;
711     } while (++retryCount < RETRY_TIMES);
712     LOGE("register callback failed, try too many times");
713 }
714 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)715 void CloudSyncManagerImpl::SystemAbilityStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId,
716     const std::string &deviceId)
717 {
718     return;
719 }
720 
CleanGalleryDentryFile()721 void CloudSyncManagerImpl::CleanGalleryDentryFile()
722 {
723     LOGI("CleanGalleryDentryFile start");
724     const std::string photoDir = "/storage/media/cloud/files/Photo";
725     const std::string thumbsDir = "/storage/media/cloud/files/.thumbs/Photo";
726     if (!OHOS::Storage::DistributedFile::Utils::ForceRemoveDirectoryDeepFirst(photoDir)) {
727         LOGW("remove photo dentry dir failed, errno: %{public}d", errno);
728     }
729     if (!OHOS::Storage::DistributedFile::Utils::ForceRemoveDirectoryDeepFirst(thumbsDir)) {
730         LOGW("remove thumbs dentry dir failed, errno: %{public}d", errno);
731     }
732 }
733 } // namespace OHOS::FileManagement::CloudSync
734