• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_SERVICE_H
17 #define OHOS_FILEMGMT_CLOUD_SYNC_SERVICE_H
18 
19 #include <map>
20 
21 #include "iservice_registry.h"
22 #include "nocopyable.h"
23 #include "system_ability.h"
24 #include "system_ability_load_callback_stub.h"
25 
26 #include "cloud_sync_service_stub.h"
27 #include "file_transfer_manager.h"
28 #include "i_cloud_download_callback.h"
29 #include "i_cloud_sync_callback.h"
30 #include "svc_death_recipient.h"
31 #include "sync_rule/battery_status_listener.h"
32 #include "sync_rule/package_status_listener.h"
33 #include "sync_rule/screen_status_listener.h"
34 #include "sync_rule/user_status_listener.h"
35 
36 namespace OHOS::FileManagement::CloudSync {
37 class CloudSyncService final : public SystemAbility, public CloudSyncServiceStub, protected NoCopyable {
38     DECLARE_SYSTEM_ABILITY(CloudSyncService);
39 
40 public:
41     explicit CloudSyncService(int32_t saID, bool runOnCreate = true);
42     virtual ~CloudSyncService() = default;
43     int32_t CallbackEnter(uint32_t code) override;
44     int32_t CallbackExit(uint32_t code, int32_t result) override;
45     ErrCode UnRegisterCallbackInner(const std::string &bundleName = "") override;
46     ErrCode UnRegisterFileSyncCallbackInner(const std::string &bundleName = "") override;
47     ErrCode RegisterCallbackInner(const sptr<IRemoteObject> &remoteObject, const std::string &bundleName = "") override;
48     ErrCode RegisterFileSyncCallbackInner(const sptr<IRemoteObject> &remoteObject,
49                                           const std::string &bundleName = "") override;
50     ErrCode StartSyncInner(bool forceFlag, const std::string &bundleName = "") override;
51     ErrCode StartFileSyncInner(bool forceFlag, const std::string &bundleName = "") override;
52     ErrCode TriggerSyncInner(const std::string &bundleName, int32_t userId) override;
53     ErrCode StopSyncInner(const std::string &bundleName = "", bool forceFlag = false) override;
54     ErrCode StopFileSyncInner(const std::string &bundleName = "", bool forceFlag = false) override;
55     ErrCode ResetCursor(const std::string &bundleName = "") override;
56     ErrCode OptimizeStorage(const OptimizeSpaceOptions &optimizeOptions,
57                             bool isCallbackValid,
58                             const sptr<IRemoteObject> &optimizeCallback) override;
59     ErrCode StopOptimizeStorage() override;
60     ErrCode ChangeAppSwitch(const std::string &accoutId, const std::string &bundleName, bool status) override;
61     ErrCode Clean(const std::string &accountId, const CleanOptions &cleanOptions) override;
62     ErrCode NotifyDataChange(const std::string &accoutId, const std::string &bundleName) override;
63     ErrCode NotifyEventChange(int32_t userId, const std::string &eventId, const std::string &extraData) override;
64     ErrCode EnableCloud(const std::string &accoutId, const SwitchDataObj &switchData) override;
65     ErrCode DisableCloud(const std::string &accoutId) override;
66     ErrCode StartDownloadFile(const std::string &uri,
67                               const sptr<IRemoteObject> &downloadCallback,
68                               int64_t &downloadId) override;
69     ErrCode StartFileCache(const std::vector<std::string> &uriVec,
70                            int64_t &downloadId,
71                            int32_t fieldkey,
72                            const sptr<IRemoteObject> &downloadCallback,
73                            int32_t timeout = -1) override;
74     ErrCode StopDownloadFile(int64_t downloadId, bool needClean = false) override;
75     ErrCode StopFileCache(int64_t downloadId, bool needClean = false, int32_t timeout = -1) override;
76     ErrCode DownloadThumb() override;
77     ErrCode UploadAsset(const int32_t userId, const std::string &request, std::string &result) override;
78     ErrCode DownloadFile(const int32_t userId, const std::string &bundleName,
79                          const AssetInfoObj &assetInfoObj) override;
80     ErrCode DownloadFiles(const int32_t userId,
81                           const std::string &bundleName,
82                           const std::vector<AssetInfoObj> &assetInfoObj,
83                           std::vector<bool> &assetResultMap,
84                           int32_t connectTime) override;
85     ErrCode DownloadAsset(const uint64_t taskId,
86                           const int32_t userId,
87                           const std::string &bundleName,
88                           const std::string &networkId,
89                           const AssetInfoObj &assetInfoObj) override;
90     ErrCode RegisterDownloadAssetCallback(const sptr<IRemoteObject> &remoteObject) override;
91     ErrCode DeleteAsset(const int32_t userId, const std::string &uri) override;
92     ErrCode GetSyncTimeInner(int64_t &syncTime, const std::string &bundleName = "") override;
93     ErrCode CleanCacheInner(const std::string &uri) override;
94     ErrCode CleanFileCacheInner(const std::string &uri) override;
95     void SetDeathRecipient(const sptr<IRemoteObject> &remoteObject);
96     ErrCode BatchCleanFile(const std::vector<CleanFileInfoObj> &fileInfo,
97                            std::vector<std::string> &failCloudId) override;
98     ErrCode BatchDentryFileInsert(const std::vector<DentryFileInfoObj> &fileInfo,
99                                   std::vector<std::string> &failCloudId) override;
100     ErrCode StartDowngrade(const std::string &bundleName, const sptr<IRemoteObject> &downloadCallback) override;
101     ErrCode StopDowngrade(const std::string &bundleName) override;
102     ErrCode GetCloudFileInfo(const std::string &bundleName, CloudFileInfo &cloudFileInfo) override;
103     // file version
104     ErrCode GetHistoryVersionList(const std::string &uri, const int32_t versionNumLimit,
105                                           std::vector<CloudSync::HistoryVersion> &historyVersionList) override;
106     ErrCode DownloadHistoryVersion(const std::string &uri, int64_t &downloadId, const uint64_t versionId,
107                                            const sptr<IRemoteObject> &downloadCallback,
108                                            std::string &versionUri) override;
109     ErrCode ReplaceFileWithHistoryVersion(const std::string &uri, const std::string &versionUri) override;
110     ErrCode IsFileConflict(const std::string &uri, bool &isConflict) override;
111     ErrCode ClearFileConflict(const std::string &uri) override;
112 
113 private:
114     std::string GetHmdfsPath(const std::string &uri, int32_t userId);
115     void OnStart(const SystemAbilityOnDemandReason &startReason) override;
116     void OnActive(const SystemAbilityOnDemandReason &startReason) override;
117     void OnStop() override;
118     void PublishSA();
119     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
120     void PreInit();
121     void Init();
122     void HandleStartReason(const SystemAbilityOnDemandReason &startReason);
123     void HandlePackageRemoved(const SystemAbilityOnDemandReason &startReason);
124     int32_t GetBundleNameUserInfo(BundleNameUserInfo &bundleNameUserInfo);
125     void GetBundleNameUserInfo(const std::vector<std::string> &uriVec, BundleNameUserInfo &bundleNameUserInfo);
126     void CovertBundleName(std::string &bundleName);
127 
128     class LoadRemoteSACallback : public SystemAbilityLoadCallbackStub {
129     public:
130         void OnLoadSACompleteForRemote(const std::string &deviceId,
131                                        int32_t systemAbilityId,
132                                        const sptr<IRemoteObject> &remoteObject);
133         std::condition_variable proxyConVar_;
134         std::atomic<bool> isLoadSuccess_{false};
135     };
136 
137     int32_t LoadRemoteSA(const std::string &deviceId);
138 
139     static inline std::mutex loadRemoteSAMutex_;
140 
141     std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager_;
142     std::shared_ptr<UserStatusListener> userStatusListener_;
143     std::shared_ptr<BatteryStatusListener> batteryStatusListener_;
144     std::shared_ptr<PackageStatusListener> packageStatusListener_;
145     std::shared_ptr<ScreenStatusListener> screenStatusListener_;
146     std::shared_ptr<FileTransferManager> fileTransferManager_;
147     sptr<SvcDeathRecipient> deathRecipient_;
148     static inline std::map<std::string, sptr<OHOS::IRemoteObject>> remoteObjectMap_;
149 };
150 } // namespace OHOS::FileManagement::CloudSync
151 
152 #endif // OHOS_FILEMGMT_CLOUD_SYNC_SERVICE_H
153