• 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/screen_status_listener.h"
33 #include "svc_death_recipient.h"
34 
35 namespace OHOS::FileManagement::CloudSync {
36 class CloudSyncService final : public SystemAbility, public CloudSyncServiceStub, protected NoCopyable {
37     DECLARE_SYSTEM_ABILITY(CloudSyncService);
38 
39 public:
40     explicit CloudSyncService(int32_t saID, bool runOnCreate = true);
41     virtual ~CloudSyncService() = default;
42     int32_t UnRegisterCallbackInner(const std::string &bundleName = "") override;
43     int32_t RegisterCallbackInner(const sptr<IRemoteObject> &remoteObject, const std::string &bundleName = "") override;
44     int32_t StartSyncInner(bool forceFlag, const std::string &bundleName = "") override;
45     int32_t TriggerSyncInner(const std::string &bundleName, const int32_t &userId) override;
46     int32_t StopSyncInner(const std::string &bundleName = "", bool forceFlag = false) override;
47     int32_t ResetCursor(const std::string &bundleName = "") override;
48     int32_t OptimizeStorage(const int32_t agingDays) override;
49     int32_t ChangeAppSwitch(const std::string &accoutId, const std::string &bundleName, bool status) override;
50     int32_t Clean(const std::string &accountId, const CleanOptions &cleanOptions) override;
51     int32_t NotifyDataChange(const std::string &accoutId, const std::string &bundleName) override;
52     int32_t NotifyEventChange(int32_t userId, const std::string &eventId, const std::string &extraData) override;
53     int32_t EnableCloud(const std::string &accoutId, const SwitchDataObj &switchData) override;
54     int32_t DisableCloud(const std::string &accoutId) override;
55     int32_t StartDownloadFile(const std::string &path) override;
56     int32_t StartFileCache(const std::vector<std::string> &uriVec,
57                            int64_t &downloadId,
58                            bool isCallbackValid,
59                            const sptr<IRemoteObject> &downloadCallback) override;
60     int32_t StopDownloadFile(const std::string &path, bool needClean = false) override;
61     int32_t StopFileCache(const int64_t &downloadId,  bool needClean = false) override;
62     int32_t RegisterDownloadFileCallback(const sptr<IRemoteObject> &downloadCallback) override;
63     int32_t UnregisterDownloadFileCallback() override;
64     int32_t DownloadThumb() override;
65     int32_t UploadAsset(const int32_t userId, const std::string &request, std::string &result) override;
66     int32_t DownloadFile(const int32_t userId, const std::string &bundleName, AssetInfoObj &assetInfoObj) override;
67     int32_t DownloadFiles(const int32_t userId,
68                           const std::string &bundleName,
69                           const std::vector<AssetInfoObj> &assetInfoObj,
70                           std::vector<bool> &assetResultMap) override;
71     int32_t DownloadAsset(const uint64_t taskId,
72                           const int32_t userId,
73                           const std::string &bundleName,
74                           const std::string &networkId,
75                           AssetInfoObj &assetInfoObj) override;
76     int32_t RegisterDownloadAssetCallback(const sptr<IRemoteObject> &remoteObject) override;
77     int32_t DeleteAsset(const int32_t userId, const std::string &uri) override;
78     int32_t GetSyncTimeInner(int64_t &syncTime, const std::string &bundleName = "") override;
79     int32_t CleanCacheInner(const std::string &uri) override;
80     void SetDeathRecipient(const sptr<IRemoteObject> &remoteObject);
81     int32_t BatchDentryFileInsert(const std::vector<DentryFileInfoObj> &fileInfo,
82         std::vector<std::string> &failCloudId) override;
83 
84 private:
85     std::string GetHmdfsPath(const std::string &uri, int32_t userId);
86     void OnStart(const SystemAbilityOnDemandReason& startReason) override;
87     void OnActive(const SystemAbilityOnDemandReason& startReason) override;
88     void OnStop() override;
89     void PublishSA();
90     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
91     void PreInit();
92     void Init();
93     void HandleStartReason(const SystemAbilityOnDemandReason &startReason);
94     int32_t GetBundleNameUserInfo(BundleNameUserInfo &bundleNameUserInfo);
95     void GetBundleNameUserInfo(const std::vector<std::string> &uriVec, BundleNameUserInfo &bundleNameUserInfo);
96 
97     class LoadRemoteSACallback : public SystemAbilityLoadCallbackStub {
98     public:
99         void OnLoadSACompleteForRemote(const std::string &deviceId,
100                                        int32_t systemAbilityId,
101                                        const sptr<IRemoteObject> &remoteObject);
102         std::condition_variable proxyConVar_;
103         std::atomic<bool> isLoadSuccess_{false};
104     };
105 
106     int32_t LoadRemoteSA(const std::string &deviceId);
107 
108     static inline std::mutex loadRemoteSAMutex_;
109 
110     std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager_;
111     std::shared_ptr<UserStatusListener> userStatusListener_;
112     std::shared_ptr<BatteryStatusListener> batteryStatusListener_;
113     std::shared_ptr<ScreenStatusListener> screenStatusListener_;
114     std::shared_ptr<FileTransferManager> fileTransferManager_;
115     sptr<SvcDeathRecipient> deathRecipient_;
116     static inline std::map<std::string, sptr<OHOS::IRemoteObject>> remoteObjectMap_;
117 };
118 } // namespace OHOS::FileManagement::CloudSync
119 
120 #endif // OHOS_FILEMGMT_CLOUD_SYNC_SERVICE_H
121