1 /*
2  * Copyright (c) 2022-2024 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_BACKUP_SERVICE_PROXY_H
17 #define OHOS_FILEMGMT_BACKUP_SERVICE_PROXY_H
18 
19 #include <condition_variable>
20 #include <cstddef>
21 #include <cstdint>
22 #include <mutex>
23 
24 #include "i_service.h"
25 #include "iremote_proxy.h"
26 #include "system_ability_load_callback_stub.h"
27 
28 namespace OHOS::FileManagement::Backup {
29 class ServiceProxy : public IRemoteProxy<IService> {
30 public:
31     ErrCode InitRestoreSession(sptr<IServiceReverse> remote) override;
32     ErrCode InitBackupSession(sptr<IServiceReverse> remote) override;
33     ErrCode Start() override;
34     UniqueFd GetLocalCapabilities() override;
35     ErrCode PublishFile(const BFileInfo &fileInfo) override;
36     ErrCode AppFileReady(const std::string &fileName, UniqueFd fd, int32_t errCode) override;
37     ErrCode AppDone(ErrCode errCode) override;
38     ErrCode ServiceResultReport(const std::string restoreRetInfo,
39         BackupRestoreScenario scenario, ErrCode errCode) override;
40     ErrCode GetFileHandle(const std::string &bundleName, const std::string &fileName) override;
41     ErrCode AppendBundlesRestoreSession(UniqueFd fd,
42                                         const std::vector<BundleName> &bundleNames,
43                                         const std::vector<std::string> &detailInfos,
44                                         RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND,
45                                         int32_t userId = DEFAULT_INVAL_VALUE) override;
46     ErrCode AppendBundlesRestoreSession(UniqueFd fd,
47                                         const std::vector<BundleName> &bundleNames,
48                                         RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND,
49                                         int32_t userId = DEFAULT_INVAL_VALUE) override;
50     ErrCode AppendBundlesBackupSession(const std::vector<BundleName> &bundleNames) override;
51     ErrCode AppendBundlesDetailsBackupSession(const std::vector<BundleName> &bundleNames,
52                                               const std::vector<std::string> &detailInfos) override;
53     ErrCode Finish() override;
54     ErrCode Release() override;
55     UniqueFd GetLocalCapabilitiesIncremental(const std::vector<BIncrementalData> &bundleNames) override;
56     ErrCode GetAppLocalListAndDoIncrementalBackup() override;
57     ErrCode InitIncrementalBackupSession(sptr<IServiceReverse> remote) override;
58     ErrCode AppendBundlesIncrementalBackupSession(const std::vector<BIncrementalData> &bundlesToBackup) override;
59     ErrCode AppendBundlesIncrementalBackupSession(const std::vector<BIncrementalData> &bundlesToBackup,
60         const std::vector<std::string> &infos) override;
61 
62     ErrCode PublishIncrementalFile(const BFileInfo &fileInfo) override;
63     ErrCode PublishSAIncrementalFile(const BFileInfo &fileInfo, UniqueFd fd) override;
64     ErrCode AppIncrementalFileReady(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd,
65         int32_t errCode) override;
66     ErrCode AppIncrementalDone(ErrCode errCode) override;
67     ErrCode GetIncrementalFileHandle(const std::string &bundleName, const std::string &fileName) override;
68     ErrCode GetBackupInfo(BundleName &bundleName, std::string &result) override;
69     ErrCode UpdateTimer(BundleName &bundleName, uint32_t timeout, bool &result) override;
70     ErrCode UpdateSendRate(std::string &bundleName, int32_t sendRate, bool &result) override;
71     ErrCode ReportAppProcessInfo(const std::string processInfo, const BackupRestoreScenario sennario) override;
72     ErrCode StartExtTimer(bool &isExtStart) override;
73     ErrCode StartFwkTimer(bool &isFwkStart) override;
74 
75 public:
ServiceProxy(const sptr<IRemoteObject> & impl)76     explicit ServiceProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IService>(impl) {}
~ServiceProxy()77     ~ServiceProxy() override {}
78 
79 public:
80     template <typename T>
81     bool WriteParcelableVector(const std::vector<T> &parcelableVector, Parcel &data);
82     static sptr<IService> GetServiceProxyPointer();
83     static sptr<IService> GetInstance();
84     static void InvaildInstance();
85 
86 public:
87     class ServiceProxyLoadCallback : public SystemAbilityLoadCallbackStub {
88     public:
89         void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject) override;
90         void OnLoadSystemAbilityFail(int32_t systemAbilityId) override;
91 
92     public:
93         std::condition_variable proxyConVar_;
94         std::atomic<bool> isLoadSuccess_ = {false};
95     };
96 
97 private:
98     static inline std::mutex proxyMutex_;
99     static inline std::mutex getInstanceMutex_;
100     static inline sptr<IService> serviceProxy_ = nullptr;
101     static inline BrokerDelegator<ServiceProxy> delegator_;
102 };
103 } // namespace OHOS::FileManagement::Backup
104 
105 #endif // OHOS_FILEMGMT_BACKUP_SERVICE_PROXY_H