1 /*
2  * Copyright (c) 2022 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_PROXY_H
17 #define OHOS_FILEMGMT_CLOUD_SYNC_SERVICE_PROXY_H
18 
19 #include "iremote_proxy.h"
20 #include "system_ability_load_callback_stub.h"
21 
22 #include "i_cloud_sync_service.h"
23 
24 namespace OHOS::FileManagement::CloudSync {
25 class CloudSyncServiceProxy : public IRemoteProxy<ICloudSyncService> {
26 public:
CloudSyncServiceProxy(const sptr<IRemoteObject> & impl)27     explicit CloudSyncServiceProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<ICloudSyncService>(impl) {}
~CloudSyncServiceProxy()28     ~CloudSyncServiceProxy() override {}
29 
30     static sptr<ICloudSyncService> GetInstance();
31     static void InvaildInstance();
32     int32_t UnRegisterCallbackInner(const std::string &bundleName = "") override;
33     int32_t RegisterCallbackInner(const sptr<IRemoteObject> &remoteObject, const std::string &bundleName = "") override;
34     int32_t StartSyncInner(bool forceFlag, const std::string &bundleName = "") override;
35     int32_t TriggerSyncInner(const std::string &bundleName, const int32_t &userId) override;
36     int32_t StopSyncInner(const std::string &bundleName = "", bool forceFlag = false) override;
37     int32_t ResetCursor(const std::string &bundleName = "") override;
38     int32_t ChangeAppSwitch(const std::string &accoutId, const std::string &bundleName, bool status) override;
39     int32_t Clean(const std::string &accountId, const CleanOptions &cleanOptions) override;
40     int32_t NotifyDataChange(const std::string &accoutId, const std::string &bundleName) override;
41     int32_t NotifyEventChange(int32_t userId, const std::string &eventId, const std::string &extraData) override;
42     int32_t EnableCloud(const std::string &accoutId, const SwitchDataObj &switchData) override;
43     int32_t DisableCloud(const std::string &accoutId) override;
44     int32_t StartDownloadFile(const std::string &uri) override;
45     int32_t StartFileCacheWriteParcel(MessageParcel &data,
46                                       const std::vector<std::string> &pathVec,
47                                       std::bitset<FIELD_KEY_MAX_SIZE> &fieldkey,
48                                       bool &isCallbackValid,
49                                       const sptr<IRemoteObject> &downloadCallback);
50     int32_t StartFileCache(const std::vector<std::string> &uriVec,
51                            int64_t &downloadId,
52                            std::bitset<FIELD_KEY_MAX_SIZE> fieldkey = FIELDKEY_CONTENT,
53                            bool isCallbackValid = false,
54                            const sptr<IRemoteObject> &downloadCallback = nullptr) override;
55     int32_t StopDownloadFile(const std::string &uri, bool needClean = false) override;
56     int32_t StopFileCache(const int64_t &downloadId,  bool needClean = false) override;
57     int32_t RegisterDownloadFileCallback(const sptr<IRemoteObject> &downloadCallback) override;
58     int32_t UnregisterDownloadFileCallback() override;
59     int32_t UploadAsset(const int32_t userId, const std::string &request, std::string &result) override;
60     int32_t DownloadFile(const int32_t userId, const std::string &bundleName, AssetInfoObj &assetInfoObj) override;
61     int32_t DownloadFiles(const int32_t userId,
62                           const std::string &bundleName,
63                           const std::vector<AssetInfoObj> &assetInfoObj,
64                           std::vector<bool> &assetResultMap) override;
65     int32_t DownloadAsset(const uint64_t taskId, const int32_t userId,
66                           const std::string &bundleName,
67                           const std::string &networkId,
68                           AssetInfoObj &assetInfoObj) override;
69     int32_t RegisterDownloadAssetCallback(const sptr<IRemoteObject> &remoteObject) override;
70     int32_t DeleteAsset(const int32_t userId, const std::string &uri) override;
71     int32_t GetSyncTimeInner(int64_t &syncTime, const std::string &bundleName = "") override;
72     int32_t CleanCacheInner(const std::string &uri) override;
73 
74     class ServiceProxyLoadCallback : public SystemAbilityLoadCallbackStub {
75     public:
76         void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject) override;
77         void OnLoadSystemAbilityFail(int32_t systemAbilityId) override;
78 
79         std::condition_variable proxyConVar_;
80         std::atomic<bool> isLoadSuccess_{false};
81     };
82 
83 private:
84     static inline std::mutex proxyMutex_;
85     static inline std::mutex instanceMutex_;
86     static inline std::mutex downloadMutex_;
87     static inline sptr<ICloudSyncService> serviceProxy_;
88     static inline BrokerDelegator<CloudSyncServiceProxy> delegator_;
89 };
90 } // namespace OHOS::FileManagement::CloudSync
91 
92 #endif // OHOS_FILEMGMT_CLOUD_SYNC_SERVICE_PROXY_H
93