• 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 "sync_rule/user_status_listener.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 "svc_death_recipient.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 UnRegisterCallbackInner(const std::string &bundleName = "") override;
44     int32_t UnRegisterFileSyncCallbackInner(const std::string &bundleName = "") override;
45     int32_t RegisterCallbackInner(const sptr<IRemoteObject> &remoteObject, const std::string &bundleName = "") override;
46     int32_t RegisterFileSyncCallbackInner(const sptr<IRemoteObject> &remoteObject,
47         const std::string &bundleName = "") override;
48     int32_t StartSyncInner(bool forceFlag, const std::string &bundleName = "") override;
49     int32_t StartFileSyncInner(bool forceFlag, const std::string &bundleName = "") override;
50     int32_t TriggerSyncInner(const std::string &bundleName, const int32_t &userId) override;
51     int32_t StopSyncInner(const std::string &bundleName = "", bool forceFlag = false) override;
52     int32_t StopFileSyncInner(const std::string &bundleName = "", bool forceFlag = false) override;
53     int32_t ResetCursor(const std::string &bundleName = "") override;
54     int32_t OptimizeStorage(const OptimizeSpaceOptions &optimizeOptions, bool isCallbackValid,
55         const sptr<IRemoteObject> &optimizeCallback) override;
56     int32_t StopOptimizeStorage() override;
57     int32_t ChangeAppSwitch(const std::string &accoutId, const std::string &bundleName, bool status) override;
58     int32_t Clean(const std::string &accountId, const CleanOptions &cleanOptions) override;
59     int32_t NotifyDataChange(const std::string &accoutId, const std::string &bundleName) override;
60     int32_t NotifyEventChange(int32_t userId, const std::string &eventId, const std::string &extraData) override;
61     int32_t EnableCloud(const std::string &accoutId, const SwitchDataObj &switchData) override;
62     int32_t DisableCloud(const std::string &accoutId) override;
63     int32_t StartDownloadFile(const std::string &path) override;
64     int32_t StartFileCache(const std::vector<std::string> &uriVec,
65                            int64_t &downloadId, std::bitset<FIELD_KEY_MAX_SIZE> fieldkey,
66                            bool isCallbackValid,
67                            const sptr<IRemoteObject> &downloadCallback,
68                            int32_t timeout = -1) override;
69     int32_t StopDownloadFile(const std::string &path, bool needClean = false) override;
70     int32_t StopFileCache(int64_t downloadId, bool needClean = false, int32_t timeout = -1) override;
71     int32_t DownloadThumb() override;
72     int32_t RegisterDownloadFileCallback(const sptr<IRemoteObject> &downloadCallback) override;
73     int32_t RegisterFileCacheCallback(const sptr<IRemoteObject> &downloadCallback) override;
74     int32_t UnregisterDownloadFileCallback() override;
75     int32_t UnregisterFileCacheCallback() override;
76     int32_t UploadAsset(const int32_t userId, const std::string &request, std::string &result) override;
77     int32_t DownloadFile(const int32_t userId, const std::string &bundleName, AssetInfoObj &assetInfoObj) override;
78     int32_t DownloadFiles(const int32_t userId,
79                           const std::string &bundleName,
80                           const std::vector<AssetInfoObj> &assetInfoObj,
81                           std::vector<bool> &assetResultMap) override;
82     int32_t DownloadAsset(const uint64_t taskId,
83                           const int32_t userId,
84                           const std::string &bundleName,
85                           const std::string &networkId,
86                           AssetInfoObj &assetInfoObj) override;
87     int32_t RegisterDownloadAssetCallback(const sptr<IRemoteObject> &remoteObject) override;
88     int32_t DeleteAsset(const int32_t userId, const std::string &uri) override;
89     int32_t GetSyncTimeInner(int64_t &syncTime, const std::string &bundleName = "") override;
90     int32_t CleanCacheInner(const std::string &uri) override;
91     void SetDeathRecipient(const sptr<IRemoteObject> &remoteObject);
92     int32_t BatchCleanFile(const std::vector<CleanFileInfoObj> &fileInfo,
93         std::vector<std::string> &failCloudId) override;
94     int32_t BatchDentryFileInsert(const std::vector<DentryFileInfoObj> &fileInfo,
95         std::vector<std::string> &failCloudId) override;
96 
97 private:
98     std::string GetHmdfsPath(const std::string &uri, int32_t userId);
99     void OnStart(const SystemAbilityOnDemandReason& startReason) override;
100     void OnActive(const SystemAbilityOnDemandReason& startReason) override;
101     void OnStop() override;
102     void PublishSA();
103     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
104     void PreInit();
105     void Init();
106     void HandleStartReason(const SystemAbilityOnDemandReason &startReason);
107     void HandlePackageRemoved(const SystemAbilityOnDemandReason &startReason);
108     int32_t GetBundleNameUserInfo(BundleNameUserInfo &bundleNameUserInfo);
109     void GetBundleNameUserInfo(const std::vector<std::string> &uriVec, BundleNameUserInfo &bundleNameUserInfo);
110 
111     class LoadRemoteSACallback : public SystemAbilityLoadCallbackStub {
112     public:
113         void OnLoadSACompleteForRemote(const std::string &deviceId,
114                                        int32_t systemAbilityId,
115                                        const sptr<IRemoteObject> &remoteObject);
116         std::condition_variable proxyConVar_;
117         std::atomic<bool> isLoadSuccess_{false};
118     };
119 
120     int32_t LoadRemoteSA(const std::string &deviceId);
121 
122     static inline std::mutex loadRemoteSAMutex_;
123 
124     std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager_;
125     std::shared_ptr<UserStatusListener> userStatusListener_;
126     std::shared_ptr<BatteryStatusListener> batteryStatusListener_;
127     std::shared_ptr<PackageStatusListener> packageStatusListener_;
128     std::shared_ptr<ScreenStatusListener> screenStatusListener_;
129     std::shared_ptr<FileTransferManager> fileTransferManager_;
130     sptr<SvcDeathRecipient> deathRecipient_;
131     static inline std::map<std::string, sptr<OHOS::IRemoteObject>> remoteObjectMap_;
132 };
133 } // namespace OHOS::FileManagement::CloudSync
134 
135 #endif // OHOS_FILEMGMT_CLOUD_SYNC_SERVICE_H
136