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 ChangeAppSwitch(const std::string &accoutId, const std::string &bundleName, bool status) override;
49     int32_t Clean(const std::string &accountId, const CleanOptions &cleanOptions) override;
50     int32_t NotifyDataChange(const std::string &accoutId, const std::string &bundleName) override;
51     int32_t NotifyEventChange(int32_t userId, const std::string &eventId, const std::string &extraData) override;
52     int32_t EnableCloud(const std::string &accoutId, const SwitchDataObj &switchData) override;
53     int32_t DisableCloud(const std::string &accoutId) override;
54     int32_t StartDownloadFile(const std::string &path) override;
55     int32_t StartFileCache(const std::vector<std::string> &uriVec,
56                            int64_t &downloadId, std::bitset<FIELD_KEY_MAX_SIZE> fieldkey,
57                            bool isCallbackValid,
58                            const sptr<IRemoteObject> &downloadCallback) override;
59     int32_t StopDownloadFile(const std::string &path, bool needClean = false) override;
60     int32_t StopFileCache(const int64_t &downloadId,  bool needClean = false) override;
61     int32_t RegisterDownloadFileCallback(const sptr<IRemoteObject> &downloadCallback) override;
62     int32_t UnregisterDownloadFileCallback() override;
63     int32_t UploadAsset(const int32_t userId, const std::string &request, std::string &result) override;
64     int32_t DownloadFile(const int32_t userId, const std::string &bundleName, AssetInfoObj &assetInfoObj) override;
65     int32_t DownloadFiles(const int32_t userId,
66                           const std::string &bundleName,
67                           const std::vector<AssetInfoObj> &assetInfoObj,
68                           std::vector<bool> &assetResultMap) override;
69     int32_t DownloadAsset(const uint64_t taskId,
70                           const int32_t userId,
71                           const std::string &bundleName,
72                           const std::string &networkId,
73                           AssetInfoObj &assetInfoObj) override;
74     int32_t RegisterDownloadAssetCallback(const sptr<IRemoteObject> &remoteObject) override;
75     int32_t DeleteAsset(const int32_t userId, const std::string &uri) override;
76     int32_t GetSyncTimeInner(int64_t &syncTime, const std::string &bundleName = "") override;
77     int32_t CleanCacheInner(const std::string &uri) override;
78     void SetDeathRecipient(const sptr<IRemoteObject> &remoteObject);
79 
80 private:
81     std::string GetHmdfsPath(const std::string &uri, int32_t userId);
82     void OnStart(const SystemAbilityOnDemandReason& startReason) override;
83     void OnActive(const SystemAbilityOnDemandReason& startReason) override;
84     void OnStop() override;
85     void PublishSA();
86     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
87     void PreInit();
88     void Init();
89     void HandleStartReason(const SystemAbilityOnDemandReason &startReason);
90     int32_t GetBundleNameUserInfo(BundleNameUserInfo &bundleNameUserInfo);
91     void GetBundleNameUserInfo(const std::vector<std::string> &uriVec, BundleNameUserInfo &bundleNameUserInfo);
92 
93     class LoadRemoteSACallback : public SystemAbilityLoadCallbackStub {
94     public:
95         void OnLoadSACompleteForRemote(const std::string &deviceId,
96                                        int32_t systemAbilityId,
97                                        const sptr<IRemoteObject> &remoteObject);
98         std::condition_variable proxyConVar_;
99         std::atomic<bool> isLoadSuccess_{false};
100     };
101 
102     int32_t LoadRemoteSA(const std::string &deviceId);
103 
104     static inline std::mutex loadRemoteSAMutex_;
105 
106     std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager_;
107     std::shared_ptr<UserStatusListener> userStatusListener_;
108     std::shared_ptr<BatteryStatusListener> batteryStatusListener_;
109     std::shared_ptr<ScreenStatusListener> screenStatusListener_;
110     std::shared_ptr<FileTransferManager> fileTransferManager_;
111     sptr<SvcDeathRecipient> deathRecipient_;
112     static inline std::map<std::string, sptr<OHOS::IRemoteObject>> remoteObjectMap_;
113 };
114 } // namespace OHOS::FileManagement::CloudSync
115 
116 #endif // OHOS_FILEMGMT_CLOUD_SYNC_SERVICE_H
117