• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_B_SESSION_BACKUP_H
17 #define OHOS_FILEMGMT_BACKUP_B_SESSION_BACKUP_H
18 
19 #include <functional>
20 #include <memory>
21 #include <vector>
22 
23 #include "b_file_info.h"
24 #include "b_incremental_data.h"
25 #include "errors.h"
26 #include "errors.h"
27 #include "svc_death_recipient.h"
28 #include "unique_fd.h"
29 
30 namespace OHOS::FileManagement::Backup {
31 class BSessionBackup {
32 public:
33     struct Callbacks {
34         std::function<void(const BFileInfo &, UniqueFd, ErrCode)> onFileReady;    // 当备份服务有文件待发送时执行的回调
35         std::function<void(ErrCode, const BundleName)> onBundleStarted;  // 当启动某个应用的备份流程结束时执行的回调函数
36         std::function<void(ErrCode, const BundleName)> onBundleFinished; // 当某个应用的备份流程结束或意外中止时执行的回调函数
37         std::function<void(ErrCode)> onAllBundlesFinished;               // 当整个备份流程结束或意外中止时执行的回调函数
38         std::function<void(const std::string, const std::string)> onResultReport; // 某个应用备份流程中自定义错误信息的上报的回调函数
39         std::function<void()> onBackupServiceDied;                       // 当备份服务意外死亡时执行的回调函数
40         std::function<void(const std::string, const std::string)> onProcess; // 上报备份恢复过程中的进度和异常
41         std::function<void(const std::string)> onBackupSizeReport; // 返回已获取待备份数据量的信息
42     };
43 
44 public:
45     /**
46      * @brief 获取一个用于控制备份流程的会话
47      *
48      * @param callbacks 注册回调
49      * @return std::unique_ptr<BSessionBackup> 指向会话的智能指针。失败时为空指针
50      */
51     static std::unique_ptr<BSessionBackup> Init(Callbacks callbacks);
52 
53     /**
54      * @brief 获取一个用于控制备份流程的会话
55      *
56      * @param callbacks 注册回调
57      * @param errMsg 失败信息
58      * @param errCode 错误码
59      * @return std::unique_ptr<BSessionBackup> 指向会话的智能指针。失败时为空指针
60      */
61     static std::unique_ptr<BSessionBackup> Init(Callbacks callbacks, std::string &errMsg, ErrCode &errCode);
62 
63     /**
64      * @brief 获取支持备份的应用信息
65      *
66      * @return ErrCode 规范错误码
67      */
68     UniqueFd GetLocalCapabilities();
69 
70     /**
71      * @brief 获取需要备份的应用数据量大小
72      * @param isPreciseScan 是否获取精确大小
73      * @param bundleNameList 需要获取数据量大小的包名列表,包含时间戳信息
74      * @return ErrCode 规范错误码
75      */
76     ErrCode GetBackupDataSize(bool isPreciseScan, std::vector<BIncrementalData> bundleNameList);
77 
78     /**
79      * @brief 用于追加应用,现阶段仅支持在Start之前调用
80      *
81      * @param bundlesToBackup 待备份的应用清单
82      *
83      * @return ErrCode 规范错误码
84      */
85     ErrCode AppendBundles(std::vector<BundleName> bundlesToBackup);
86 
87     /**
88      * @brief 用于追加应用,现阶段仅支持在Start之前调用
89      *
90      * @param bundlesToBackup 待备份的应用清单
91      * @param detailInfos 备份所需信息
92      *
93      * @return ErrCode 规范错误码
94      */
95     ErrCode AppendBundles(std::vector<BundleName> bundlesToBackup, std::vector<std::string> detailInfos);
96 
97     /**
98      * @brief 用于结束追加应用,结束后不可在调用AppendBundles
99      *
100      * @return ErrCode 规范错误码
101      */
102     ErrCode Finish();
103 
104     /**
105      * @brief 用于启动备份流程
106      *
107      * @return ErrCode 规范错误码
108      */
109     ErrCode Start();
110 
111     /**
112      * @brief 用于结束服务
113      *
114      * @return ErrCode 规范错误码
115      */
116     ErrCode Release();
117 
118     /**
119      * @brief 用于结束应用的备份恢复任务
120      *
121      * @param bundleName 要取消的应用包名
122      * @return ErrCode 规范错误码
123      */
124     ErrCode Cancel(std::string bundleName);
125 
126     /**
127      * @brief 注册备份服务意外死亡时执行的回调函数
128      *
129      * @param functor 回调函数
130      */
131     void RegisterBackupServiceDied(std::function<void()> functor);
132 
133     /**
134      * @brief 备份或者恢复任务结束后,用于清理当前应用的临时目录(./backup目录下的backup和restore目录)的数据
135      *
136      * @param bundleName 应用名称
137      * @return ErrCode 规范错误码
138      */
139     ErrCode CleanBundleTempDir(const std::string &bundleName);
140 
141     /**
142      * @brief 获取备份或恢复的应用的兼容性信息
143      *
144      * @param bundleName 应用名称
145      * @param extInfo 导入给应用的信息
146      * @param compatInfo 应用返回的兼容信息
147      * @return ErrCode 规范错误码
148      */
149     ErrCode GetCompatibilityInfo(const std::string &bundleName, const std::string &extInfo, std::string &compatInfo);
150 
151 public:
152     ~BSessionBackup();
153 
154 private:
155     sptr<SvcDeathRecipient> deathRecipient_;
156 };
157 } // namespace OHOS::FileManagement::Backup
158 
159 #endif // OHOS_FILEMGMT_BACKUP_B_SESSION_BACKUP_H