• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_INCREMENTAL_RESTORE_RESTORE_H
17 #define OHOS_FILEMGMT_BACKUP_B_INCREMENTAL_RESTORE_RESTORE_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 "svc_death_recipient.h"
27 #include "unique_fd.h"
28 
29 namespace OHOS::FileManagement::Backup {
30 class BIncrementalRestoreSession {
31 public:
32     struct Callbacks {
33         std::function<void(const BFileInfo &, UniqueFd, UniqueFd, ErrCode)> onFileReady; // 当备份服务有文件待发送时执行的回调
34         std::function<void(ErrCode, const BundleName)> onBundleStarted; // 当启动某个应用的恢复流程结束时执行的回调函数
35         std::function<void(ErrCode, const BundleName)>
36             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     };
42 
43 public:
44     /**
45      * @brief 获取一个用于控制恢复流程的会话
46      *
47      * @param callbacks 注册的回调函数
48      * @return std::unique_ptr<BRestoreSession> 指向BRestoreSession的智能指针。失败时为空指针
49      */
50     static std::unique_ptr<BIncrementalRestoreSession> Init(Callbacks callbacks);
51 
52     /**
53      * @brief 获取一个用于控制恢复流程的会话
54      *
55      * @param callbacks 注册的回调函数
56      * @param errMsg 失败信息
57      * @param errCode 错误码
58      * @return std::unique_ptr<BRestoreSession> 指向BRestoreSession的智能指针。失败时为空指针
59      */
60     static std::unique_ptr<BIncrementalRestoreSession> Init(Callbacks callbacks,
61                                                             std::string &errMsg, ErrCode &errCode);
62 
63     /**
64      * @brief 获取支持备份的应用信息(不包含数据量大小)
65      *
66      * @return UniqueFd 包含应用信息的文件描述符
67      */
68     UniqueFd GetLocalCapabilities();
69 
70     /**
71      * @brief 通知备份服务文件内容已就绪
72      *
73      * @param fileInfo 文件描述信息
74      * @return ErrCode 规范错误码
75      * @see GetFileHandle
76      */
77     ErrCode PublishFile(BFileInfo fileInfo);
78 
79     /**
80      * @brief 通知备份服务SA文件内容已就绪
81      *
82      * @param fileInfo 文件描述信息
83      * @param fd 文件描述符
84      * @return ErrCode 规范错误码
85      * @see GetFileHandle
86      */
87     ErrCode PublishSAFile(BFileInfo fileInfo, UniqueFd fd);
88 
89     /**
90      * @brief 请求恢复流程所需的真实文件
91      *
92      * @param bundleName 应用名称
93      * @param fileName   文件名称
94      */
95     ErrCode GetFileHandle(const std::string &bundleName, const std::string &fileName);
96 
97     /**
98      * @brief 用于追加应用,现阶段仅支持在Start之前调用
99      *
100      * @param remoteCap 已打开的保存远端设备能力的Json文件。可使用GetLocalCapabilities方法获取
101      * @param bundlesToRestore 待恢复的应用清单
102      * @param detailInfos bundle对应的单双映射关系json串
103      *
104      * @return ErrCode 规范错误码
105      */
106     ErrCode AppendBundles(UniqueFd remoteCap, std::vector<BundleName> bundlesToRestore,
107         std::vector<std::string> detailInfos);
108 
109     /**
110      * @brief 用于追加应用,现阶段仅支持在Start之前调用
111      *
112      * @param remoteCap 已打开的保存远端设备能力的Json文件。可使用GetLocalCapabilities方法获取
113      * @param bundlesToRestore 待恢复的应用清单
114      * @return ErrCode 规范错误码
115      */
116     ErrCode AppendBundles(UniqueFd remoteCap, std::vector<BundleName> bundlesToRestore);
117 
118     /**
119      * @brief 用于结束服务
120      *
121      * @return ErrCode 规范错误码
122      */
123     ErrCode Release();
124 
125     /**
126      * @brief 用于结束应用的备份恢复任务
127      *
128      * @param bundleName 要取消的应用包名
129      * @return ErrCode 规范错误码
130      */
131     ErrCode Cancel(std::string bundleName);
132 
133     /**
134      * @brief 注册备份服务意外死亡时执行的回调函数
135      *
136      * @param functor 回调函数
137      */
138     void RegisterBackupServiceDied(std::function<void()> functor);
139 
140     /**
141      * @brief 备份或者恢复任务结束后,用于清理当前应用的临时目录(./backup目录下的backup和restore目录)的数据
142      *
143      * @param bundleName 应用名称
144      * @return ErrCode 规范错误码
145      */
146     ErrCode CleanBundleTempDir(const std::string &bundleName);
147 
148     /**
149      * @brief 获取备份或恢复的应用的兼容性信息
150      *
151      * @param bundleName 应用名称
152      * @param extInfo 导入给应用的信息
153      * @param compatInfo 应用返回的兼容信息
154      * @return ErrCode 规范错误码
155      */
156     ErrCode GetCompatibilityInfo(const std::string &bundleName, const std::string &extInfo, std::string &compatInfo);
157 
158 public:
159     ~BIncrementalRestoreSession();
160 
161 private:
162     sptr<SvcDeathRecipient> deathRecipient_;
163 };
164 } // namespace OHOS::FileManagement::Backup
165 
166 #endif // OHOS_FILEMGMT_BACKUP_B_INCREMENTAL_RESTORE_RESTORE_H