• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 /*
17  * 说明:
18  * 备份服务同一时间只接受一个唯一的会话。在会话期间,服务只与一个备份应用通信
19  * 且只响应备份或恢复接口中的一种。
20  */
21 
22 #ifndef OHOS_FILEMGMT_BACKUP_SVC_SESSION_MANAGER_H
23 #define OHOS_FILEMGMT_BACKUP_SVC_SESSION_MANAGER_H
24 
25 #include <map>
26 #include <memory>
27 #include <shared_mutex>
28 #include <vector>
29 
30 #include <refbase.h>
31 
32 #include "b_file_info.h"
33 #include "b_resources/b_constants.h"
34 #include "i_service_reverse.h"
35 #include "i_service.h"
36 #include "module_ipc/svc_backup_connection.h"
37 #include "svc_death_recipient.h"
38 
39 namespace OHOS::FileManagement::Backup {
40 struct BackupExtInfo {
41     bool receExtManageJson {false};
42     bool receExtAppDone {false};
43     bool isBundleFinished {false};
44     std::string backupExtName;
45     sptr<SvcBackupConnection> backUpConnection;
46     std::set<std::string> fileNameInfo;
47     BConstants::ServiceSchedAction schedAction {BConstants::ServiceSchedAction::WAIT};
48     bool bNeedToInstall {false};
49     std::string installState;
50     /* [RESTORE] Record whether data backup is required during the app exec restore proceess. */
51     RestoreTypeEnum restoreType;
52     /* Clone App: old device app versionCode */
53     uint32_t versionCode;
54     /* Clone App: old device app versionCode */
55     std::string versionName;
56 };
57 
58 class Service;
59 class SvcSessionManager : public virtual RefBase {
60 public:
61     struct Impl {
62         uint32_t clientToken {0};
63         IServiceReverse::Scenario scenario {IServiceReverse::Scenario::UNDEFINED};
64         std::map<BundleName, BackupExtInfo> backupExtNameMap;
65         sptr<IServiceReverse> clientProxy;
66         bool isBackupStart {false};
67         bool isAppendFinish {false};
68         /* Note: Multi user scenario: <System Update Upgrade>
69             Caller must complete all processes before next user tigger.<Session>
70             [RESTORE] Support for multiple users, incoming during restore process.
71         */
72         int32_t userId {100};
73     };
74 
75 public:
76     /**
77      * @brief 检验调用者是否是会话所有者,且当前命令是否与当前场景相匹配
78      *
79      * @param clientToken 调用者TOKEN
80      * @param scenario 给定场景
81      * @throw BError::Codes::SA_REFUSED_ACT 调用者不是会话所有者
82      * @throw BError::Codes::SDK_MIXED_SCENARIO 调用者在备份/恢复场景使用了不匹配的函数
83      */
84     void VerifyCallerAndScenario(uint32_t clientToken, IServiceReverse::Scenario scenario) const;
85 
86     /**
87      * @brief 激活会话
88      *
89      * @param impl 客户端信息
90      */
91     void Active(Impl newImpl);
92 
93     /**
94      * @brief 关闭会话
95      *
96      * @param remoteInAction 尝试关闭会话的客户端代理。只有激活会话的客户端代理有权关闭会话
97      * @param force 强制关闭
98      */
99     void Deactive(const wptr<IRemoteObject> &remoteInAction, bool force = false);
100 
101     /**
102      * @brief 检验调用者给定的bundleName是否是有效的
103      *
104      * @param bundleName 调用者名称
105      * @throw BError::Codes::SA_REFUSED_ACT 调用者不是会话所有者
106      */
107     void VerifyBundleName(std::string &bundleName);
108 
109     /**
110      * @brief 获取IServiceReverse
111      *
112      * @return sptr<IServiceReverse> 返回clientProxy
113      * @throw BError::Codes::SA_REFUSED_ACT 调用者不是会话所有者
114      */
115     sptr<IServiceReverse> GetServiceReverseProxy();
116 
117     /**
118      * @brief 获取Scenario
119      *
120      * @return IServiceReverse::Scenario 返回scenario
121      * @throw BError::Codes::SA_INVAL_ARG 获取异常
122      */
123     IServiceReverse::Scenario GetScenario();
124 
125     /**
126      * @brief 获取当前处理事务会话对应的userId
127      *
128      * @return int32_t
129      */
130     int32_t GetSessionUserId();
131 
132     /**
133      * @brief 设置当前处理事务会话对应的userId
134      *
135      * @param userid 相关会话对应的userId
136      */
137     void SetSessionUserId(int32_t userId);
138 
139     /**
140      * @brief 更新backupExtNameMap并判断是否完成分发
141      *
142      * @param bundleName 客户端信息
143      * @param fileName 文件名称
144      * @throw BError::Codes::SA_INVAL_ARG 获取异常
145      * @return true 分发已完成
146      * @return false 分发未完成
147      */
148     bool OnBunleFileReady(const std::string &bundleName, const std::string &fileName = "");
149 
150     /**
151      * @brief 设置backup manage.json 信息
152      *
153      * @param bundleName 客户端信息
154      * @param fd manage.json 文件描述符
155      * @return UniqueFd 返回manage.json 文件描述符
156      * @throw BError::Codes::SA_INVAL_ARG 获取异常
157      */
158     UniqueFd OnBunleExtManageInfo(const std::string &bundleName, UniqueFd fd);
159 
160     /**
161      * @brief Remove backup extension info
162      *
163      * @param bundleName 应用名称
164      */
165     void RemoveExtInfo(const std::string &bundleName);
166 
167     /**
168      * @brief get extension connection info
169      *
170      * @param bundleName
171      * @return wptr<SvcBackupConnection>
172      */
173     wptr<SvcBackupConnection> GetExtConnection(const BundleName &bundleName);
174 
175     /**
176      * @brief HiDumper dump info
177      *
178      * @param fd 对端dump句柄
179      * @param args 服务参数
180      */
181     void DumpInfo(const int fd, const std::vector<std::u16string> &args);
182 
183     /**
184      * @brief 暂存restore流程真实文件请求
185      *
186      * @param bundleName 应用名称
187      * @param fileName  文件名称
188      */
189     void SetExtFileNameRequest(const std::string &bundleName, const std::string &fileName);
190 
191     /**
192      * @brief 获取restore流程真实文件请求
193      *
194      * @param bundleName 应用名称
195      * @return std::set<std::string> 返回真实文件vec
196      */
197     std::set<std::string> GetExtFileNameRequest(const std::string &bundleName);
198 
199     /**
200      * @brief 获取ServiceSchedAction状态
201      *
202      * @param bundleName 应用名称
203      * @return BConstants::ServiceSchedAction
204      */
205     BConstants::ServiceSchedAction GetServiceSchedAction(const std::string &bundleName);
206 
207     /**
208      * @brief 设置ServiceSchedAction状态
209      *
210      * @param bundleName 应用名称
211      * @param action 状态
212      */
213     void SetServiceSchedAction(const std::string &bundleName, BConstants::ServiceSchedAction action);
214 
215     /**
216      * @brief 获取所需要的调度信息
217      *
218      * @return std::string 返回bundleName
219      */
220     bool GetSchedBundleName(std::string &bundleName);
221 
222     /**
223      * @brief 获取backup extension name
224      *
225      * @param bundleName 应用名称
226      * @return std::string
227      * @return std::string extension name
228      */
229     std::string GetBackupExtName(const std::string &bundleName);
230 
231     /**
232      * @brief 追加应用
233      *
234      * @param bundleNames 应用名称
235      */
236     void AppendBundles(const std::vector<std::string> &bundleNames);
237 
238     /**
239      * @brief 开始备份
240      *
241      * @return ErrCode
242      */
243     void Start();
244 
245     /**
246      * @brief 结束追加应用
247      *
248      * @return ErrCode
249      */
250     void Finish();
251 
252     /**
253      * @brief 整个备份恢复流程是否结束
254      *
255      * @return true 备份恢复流程结束
256      * @return false 备份恢复流程未结束
257      */
258     bool IsOnAllBundlesFinished();
259 
260     /**
261      * @brief 是否启动调度器
262      *
263      * @return true 启动调度器
264      * @return false 不启动调度器
265      */
266     bool IsOnOnStartSched();
267 
268     /**
269      * @brief Set the Install State object
270      *
271      * @param bundleName
272      * @param state
273      */
274     void SetInstallState(const std::string &bundleName, const std::string &state);
275 
276     /**
277      * @brief Get the Install State object
278      *
279      * @param bundleName
280      * @return std::string
281      */
282     std::string GetInstallState(const std::string &bundleName);
283 
284     /**
285      * @brief Set the Need To Install object
286      *
287      * @param bundleName
288      * @param needToInstall
289      */
290     void SetNeedToInstall(const std::string &bundleName, bool needToInstall);
291 
292     /**
293      * @brief Get the Need To Install object
294      *
295      * @param bundleName
296      * @return true
297      * @return false
298      */
299     bool GetNeedToInstall(const std::string &bundleName);
300 
301     /**
302      * @brief Whether to unload the service
303      *
304      * @return true
305      * @return false
306      */
307     bool NeedToUnloadService();
308 
309 /**
310      * @brief Set the bundle restore type object
311      *
312      * @param bundleName
313      * @param restoreType
314      */
315     void SetBundleRestoreType(const std::string &bundleName, RestoreTypeEnum restoreType);
316 
317     /**
318      * @brief Get the bundle restore type object
319      *
320      * @param  bundleName
321      * @return restoreType
322      */
323     RestoreTypeEnum GetBundleRestoreType(const std::string &bundleName);
324 
325     /**
326      * @brief Set the bundle version code object
327      *
328      * @param bundleName
329      * @param versionCode
330      */
331     void SetBundleVersionCode(const std::string &bundleName, uint32_t versionCode);
332 
333     /**
334      * @brief Get the bundle version code object
335      *
336      * @param  bundleName
337      * @return versionCode
338      */
339     uint32_t GetBundleVersionCode(const std::string &bundleName);
340 
341     /**
342      * @brief Set the bundle version name object
343      *
344      * @param bundleName
345      * @param versionName
346      */
347     void SetBundleVersionName(const std::string &bundleName, std::string versionName);
348 
349     /**
350      * @brief Get the bundle version name object
351      *
352      * @param  bundleName
353      * @return versionName
354      */
355     std::string GetBundleVersionName(const std::string &bundleName);
356 
357 private:
358     /**
359      * @brief 获取backup extension ability
360      *
361      * @param bundleName 应用名称
362      */
363     sptr<SvcBackupConnection> GetBackupExtAbility(const std::string &bundleName);
364 
365     /**
366      * @brief 初始化 clientProxy
367      *
368      * @param newImpl
369      */
370     void InitClient(Impl &newImpl);
371 
372     /**
373      * @brief 获取BackupExtNameMap iterator
374      *
375      * @param bundleName 应用名称
376      * @return std::map<BundleName, BackupExtInfo>::iterator
377      */
378     std::map<BundleName, BackupExtInfo>::iterator GetBackupExtNameMap(const std::string &bundleName);
379 
380 public:
381     /**
382      * @brief Construct a new Svc Session object
383      *
384      * @param reversePtr 指向Service的反向指针,使用wptr避免循环引用
385      */
SvcSessionManager(wptr<Service> reversePtr)386     explicit SvcSessionManager(wptr<Service> reversePtr) : reversePtr_(reversePtr) {}
387     ~SvcSessionManager() override = default;
388 
389 private:
390     mutable std::shared_mutex lock_;
391     wptr<Service> reversePtr_;
392     sptr<SvcDeathRecipient> deathRecipient_;
393     Impl impl_;
394     uint32_t extConnectNum_ {0};
395 };
396 } // namespace OHOS::FileManagement::Backup
397 
398 #endif // OHOS_FILEMGMT_BACKUP_SVC_SESSION_MANAGER_H
399