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_RESTORE_H
17 #define OHOS_FILEMGMT_BACKUP_B_SESSION_RESTORE_H
18 
19 #include <functional>
20 #include <memory>
21 #include <vector>
22 
23 #include "b_file_info.h"
24 #include "errors.h"
25 #include "svc_death_recipient.h"
26 #include "unique_fd.h"
27 
28 namespace OHOS::FileManagement::Backup {
29 class BSessionRestore {
30 public:
31     struct Callbacks {
32         std::function<void(const BFileInfo &, UniqueFd, ErrCode)> onFileReady;    // 当备份服务有文件待发送时执行的回调
33         std::function<void(ErrCode, const BundleName)> onBundleStarted;  // 当启动某个应用的恢复流程结束时执行的回调函数
34         std::function<void(ErrCode, const BundleName)> onBundleFinished; // 当某个应用的恢复流程结束或意外中止时执行的回调函数
35         std::function<void(ErrCode)> onAllBundlesFinished;               // 当整个恢复流程结束或意外中止时执行的回调函数
36         std::function<void(const std::string, const std::string)> onResultReport; // 某个应用恢复流程中自定义错误信息的上报的回调函数
37         std::function<void()> onBackupServiceDied;                       // 当备份服务意外死亡时执行的回调函数
38         std::function<void(const std::string, const std::string)> onProcess; // 上报备份恢复过程中的进度和异常
39     };
40 
41 public:
42     /**
43      * @brief 获取一个用于控制恢复流程的会话
44      *
45      * @param callbacks 注册的回调函数
46      * @return std::unique_ptr<BRestoreSession> 指向BRestoreSession的智能指针。失败时为空指针
47      */
48     static std::unique_ptr<BSessionRestore> Init(Callbacks callbacks);
49 
50     /**
51      * @brief 通知备份服务文件内容已就绪
52      *
53      * @param fileInfo 文件描述信息
54      * @return ErrCode 规范错误码
55      * @see GetFileHandle
56      */
57     ErrCode PublishFile(BFileInfo fileInfo);
58 
59     /**
60      * @brief 请求恢复流程所需的真实文件
61      *
62      * @param bundleName 应用名称
63      * @param fileName   文件名称
64      */
65     ErrCode GetFileHandle(const std::string &bundleName, const std::string &fileName);
66 
67      /**
68      * @brief 用于追加应用,现阶段仅支持在Start之前调用
69      *
70      * @param remoteCap 已打开的保存远端设备能力的Json文件。可使用GetLocalCapabilities方法获取
71      * @param bundlesToRestore 待恢复的应用清单
72      * @param detailInfos bundle对应的单双映射关系json
73      * @return ErrCode 规范错误码
74      */
75     ErrCode AppendBundles(UniqueFd remoteCap, std::vector<BundleName> bundlesToRestore,
76         std::vector<std::string> detailInfos);
77 
78     /**
79      * @brief 用于追加应用,现阶段仅支持在Start之前调用
80      *
81      * @param remoteCap 已打开的保存远端设备能力的Json文件。可使用GetLocalCapabilities方法获取
82      * @param bundlesToRestore 待恢复的应用清单
83      * @return ErrCode 规范错误码
84      */
85     ErrCode AppendBundles(UniqueFd remoteCap, std::vector<BundleName> bundlesToRestore);
86 
87     /**
88      * @brief 用于结束追加应用,结束后不可在调用AppendBundles
89      *
90      * @return ErrCode 规范错误码
91      */
92     ErrCode Finish();
93 
94     /**
95      * @brief 用于启动恢复流程
96      *
97      * @return ErrCode 规范错误码
98      */
99     ErrCode Start();
100 
101     /**
102      * @brief 用于结束服务
103      *
104      * @return ErrCode 规范错误码
105      */
106     ErrCode Release();
107 
108     /**
109      * @brief 注册备份服务意外死亡时执行的回调函数
110      *
111      * @param functor 回调函数
112      */
113     void RegisterBackupServiceDied(std::function<void()> functor);
114 
115 public:
116     ~BSessionRestore();
117 
118 private:
119     sptr<SvcDeathRecipient> deathRecipient_;
120 };
121 } // namespace OHOS::FileManagement::Backup
122 
123 #endif // OHOS_FILEMGMT_BACKUP_B_SESSION_RESTORE_H