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