• 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 #ifndef OHOS_FILEMGMT_CLOUD_SYNC_MANAGER_IMPL_H
17 #define OHOS_FILEMGMT_CLOUD_SYNC_MANAGER_IMPL_H
18 
19 #include <atomic>
20 
21 #include "nocopyable.h"
22 
23 #include "cloud_optimize_callback_client.h"
24 #include "cloud_sync_callback_client.h"
25 #include "cloud_sync_common.h"
26 #include "cloud_sync_manager.h"
27 #include "svc_death_recipient.h"
28 #include "system_ability_status_change_stub.h"
29 
30 namespace OHOS::FileManagement::CloudSync {
31 class CloudSyncManagerImpl final : public CloudSyncManager, public NoCopyable {
32 public:
33     static CloudSyncManagerImpl &GetInstance();
34 
35     int32_t RegisterCallback(const std::shared_ptr<CloudSyncCallback> callback,
36                              const std::string &bundleName = "") override;
37     int32_t RegisterFileSyncCallback(const std::shared_ptr<CloudSyncCallback> callback,
38                              const std::string &bundleName = "") override;
39     int32_t UnRegisterCallback(const std::string &bundleName = "") override;
40     int32_t UnRegisterFileSyncCallback(const std::string &bundleName = "") override;
41     int32_t StartSync(const std::string &bundleName = "") override;
42     int32_t StartFileSync(const std::string &bundleName = "") override;
43     int32_t StartSync(bool forceFlag, const std::shared_ptr<CloudSyncCallback> callback) override;
44     int32_t TriggerSync(const std::string &bundleName, const int32_t &userId) override;
45     int32_t StopSync(const std::string &bundleName = "", bool forceFlag = false) override;
46     int32_t StopFileSync(const std::string &bundleName = "", bool forceFlag = false) override;
47     int32_t ResetCursor(const std::string &bundleName = "") override;
48     int32_t ChangeAppSwitch(const std::string &accoutId, const std::string &bundleName, bool status) override;
49     int32_t OptimizeStorage(const OptimizeSpaceOptions &optimizeOptions,
50         const std::shared_ptr<CloudOptimizeCallback> optimizeCallback = nullptr) override;
51     int32_t StopOptimizeStorage() override;
52     int32_t Clean(const std::string &accountId, const CleanOptions &cleanOptions) override;
53     int32_t NotifyDataChange(const std::string &accoutId, const std::string &bundleName) override;
54     int32_t NotifyEventChange(int32_t userId, const std::string &eventId, const std::string &extraData) override;
55     int32_t EnableCloud(const std::string &accoutId, const SwitchDataObj &switchData) override;
56     int32_t DisableCloud(const std::string &accoutId) override;
57     int32_t StartDownloadFile(const std::string &uri,
58                               const std::shared_ptr<CloudDownloadCallback> downloadCallback,
59                               int64_t &downloadId) override;
60     int32_t StartFileCache(const std::vector<std::string> &uriVec,
61                            int64_t &downloadId,
62                            int32_t fieldkey = FieldKey::FIELDKEY_CONTENT,
63                            const std::shared_ptr<CloudDownloadCallback> downloadCallback = nullptr,
64                            int32_t timeout = -1) override;
65     int32_t StopDownloadFile(int64_t downloadId, bool needClean = false) override;
66     int32_t StopFileCache(int64_t downloadId, bool needClean = false, int32_t timeout = -1) override;
67     int32_t DownloadThumb() override;
68     int32_t GetSyncTime(int64_t &syncTime, const std::string &bundleName = "") override;
69     int32_t CleanCache(const std::string &uri) override;
70     int32_t CleanFileCache(const std::string &uri) override;
71     void CleanGalleryDentryFile() override;
72     void CleanGalleryDentryFile(const std::string path) override;
73     int32_t BatchCleanFile(const std::vector<CleanFileInfo> &fileInfo, std::vector<std::string> &failCloudId) override;
74     int32_t BatchDentryFileInsert(const std::vector<DentryFileInfo> &fileInfo,
75         std::vector<std::string> &failCloudId) override;
76     int32_t StartDowngrade(const std::string &bundleName,
77                            const std::shared_ptr<DowngradeDlCallback> downloadCallback) override;
78     int32_t StopDowngrade(const std::string &bundleName) override;
79     int32_t GetCloudFileInfo(const std::string &bundleName, CloudFileInfo &cloudFileInfo) override;
80     // file version
81     int32_t GetHistoryVersionList(const std::string &uri, const int32_t versionNumLimit,
82                                   std::vector<CloudSync::HistoryVersion> &historyVersionList) override;
83     int32_t DownloadHistoryVersion(const std::string &uri, int64_t &downloadId, const uint64_t versionId,
84                                    const std::shared_ptr<CloudDownloadCallback> downloadCallback,
85                                    std::string &versionUri) override;
86     int32_t ReplaceFileWithHistoryVersion(const std::string &uri, const std::string &versionUri) override;
87     int32_t IsFileConflict(const std::string &uri, bool &isConflict) override;
88     int32_t ClearFileConflict(const std::string &uri) override;
89 
90     class SystemAbilityStatusChange : public SystemAbilityStatusChangeStub {
91     public:
SystemAbilityStatusChange(const std::string & bundleName)92         SystemAbilityStatusChange(const std::string &bundleName) : bundleName_(bundleName) {};
93         void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId);
94         void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId);
95     private:
96         std::string bundleName_ = "";
97     };
98 private:
99     CloudSyncManagerImpl() = default;
100     void SetDeathRecipient(const sptr<IRemoteObject> &remoteObject);
101     bool ResetProxyCallback(uint32_t retryCount, const std::string &bundleName);
102 
103     std::atomic_flag isFirstCall_{false};
104     sptr<SvcDeathRecipient> deathRecipient_;
105     std::shared_ptr<CloudSyncCallback> callback_;
106     sptr<CloudSyncManagerImpl::SystemAbilityStatusChange> listener_;
107     std::mutex subscribeMutex_;
108     std::mutex callbackMutex_;
109     void SubscribeListener(std::string bundleName = "");
110 };
111 } // namespace OHOS::FileManagement::CloudSync
112 
113 #endif // OHOS_FILEMGMT_CLOUD_SYNC_MANAGER_IMPL_H
114