• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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_H
17 #define OHOS_FILEMGMT_BACKUP_SERVICE_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <mutex>
22 
23 #include "b_jsonutil/b_jsonutil.h"
24 #include "b_json/b_json_clear_data_config.h"
25 #include "b_json/b_json_entity_caps.h"
26 #include "b_json/b_json_service_disposal_config.h"
27 #include "b_radar/radar_total_statistic.h"
28 #include "b_radar/radar_app_statistic.h"
29 #include "iservice_reverse.h"
30 #include "iremote_stub.h"
31 #include "module_sched/sched_scheduler.h"
32 #include "service_stub.h"
33 #include "svc_session_manager.h"
34 #include "system_ability.h"
35 #include "thread_pool.h"
36 
37 namespace OHOS::FileManagement::Backup {
38 struct ExtensionMutexInfo {
39     std::string bundleName;
40     std::mutex callbackMutex;
ExtensionMutexInfoExtensionMutexInfo41     ExtensionMutexInfo(std::string bundleName_) : bundleName(bundleName_) {};
42 };
43 
44 struct BundleTaskInfo {
45     std::string reportTime;
46     ErrCode errCode;
47 };
48 const int INVALID_FD = -1;
49 constexpr const int32_t CONNECT_WAIT_TIME_S = 15;
50 
51 class Service : public SystemAbility, public ServiceStub, protected NoCopyable {
52     DECLARE_SYSTEM_ABILITY(Service);
53 
54     // 以下都是IPC接口
55 public:
56     ErrCode InitRestoreSession(const sptr<IServiceReverse>& remote) override;
57     ErrCode InitRestoreSessionWithErrMsg(const sptr<IServiceReverse>& reverseIpcRemoteObject,
58                                          int32_t &errCodeForMsg, std::string& errMsg) override;
59     ErrCode InitBackupSession(const sptr<IServiceReverse>& remote) override;
60     ErrCode InitBackupSessionWithErrMsg(const sptr<IServiceReverse>& remote,
61                                         int32_t &errCodeForMsg, std::string &errMsg) override;
62     ErrCode Start() override;
63     ErrCode GetLocalCapabilities(int& fd) override;
64 
65     ErrCode GetLocalCapabilitiesForBundleInfos(int& fd) override;
66     ErrCode PublishFile(const BFileInfo &fileInfo) override;
67     ErrCode AppFileReady(const std::string &fileName, int fd, int32_t errCode) override;
68     ErrCode AppFileReadyWithoutFd(const std::string &fileName, int32_t errCode) override;
69     ErrCode AppDone(ErrCode errCode) override;
70     ErrCode ServiceResultReport(const std::string& restoreRetInfo,
71         BackupRestoreScenario sennario, ErrCode errCode) override;
72     ErrCode GetFileHandle(const std::string &bundleName, const std::string &fileName) override;
73 
74     ErrCode AppendBundlesRestoreSessionDataByDetail(
75             int fd,
76             const std::vector<std::string>& bundleNames,
77             const std::vector<std::string>& detailInfos,
78             int32_t restoreType,
79             int32_t userId) override;
80     ErrCode AppendBundlesRestoreSessionData(int fd, const std::vector<std::string>& bundleNames,
81                                             int32_t restoreType, int32_t userId) override;
82 
83     ErrCode AppendBundlesBackupSession(const std::vector<BundleName> &bundleNames) override;
84     ErrCode AppendBundlesDetailsBackupSession(const std::vector<BundleName> &bundleNames,
85                                               const std::vector<std::string> &bundleInfos) override;
86     ErrCode Finish() override;
87     ErrCode Release() override;
88     ErrCode CancelForResult(const std::string& bundleName, int32_t &result) override;
89     ErrCode GetLocalCapabilitiesIncremental(const std::vector<BIncrementalData>& bundleNames, int& fd) override;
90     ErrCode GetAppLocalListAndDoIncrementalBackup() override;
91     ErrCode InitIncrementalBackupSession(const sptr<IServiceReverse>& remote) override;
92     ErrCode InitIncrementalBackupSessionWithErrMsg(const sptr<IServiceReverse>& remote, int32_t &errCodeForMsg,
93                                                    std::string &errMsg) override;
94     ErrCode AppendBundlesIncrementalBackupSession(const std::vector<BIncrementalData> &bundlesToBackup) override;
95     ErrCode AppendBundlesIncrementalBackupSessionWithBundleInfos(const std::vector<BIncrementalData>& bundlesToBackup,
96                                                                  const std::vector<std::string>& bundleInfos) override;
97     ErrCode PublishIncrementalFile(const BFileInfo &fileInfo) override;
98     ErrCode PublishSAIncrementalFile(const BFileInfo& fileInfo, int fd) override;
99     ErrCode PublishSAIncrementalFile(const BFileInfo &fileInfo, UniqueFd fd);
100     ErrCode AppIncrementalFileReady(const std::string& fileName, int fd, int manifestFd,
101                                     int32_t appIncrementalFileReadyErrCode) override;
102     ErrCode AppIncrementalFileReadyWithoutFd(const std::string& fileName,
103                                              int32_t appIncrementalFileReadyErrCode) override;
104     ErrCode AppIncrementalDone(ErrCode errCode) override;
105     ErrCode GetIncrementalFileHandle(const std::string &bundleName, const std::string &fileName) override;
106     ErrCode GetBackupInfo(const BundleName &bundleName, std::string &result) override;
107     ErrCode UpdateTimer(const BundleName &bundleName, uint32_t timeout, bool &result) override;
108     ErrCode UpdateSendRate(const std::string &bundleName, int32_t sendRate, bool &result) override;
109     ErrCode ReportAppProcessInfo(const std::string& processInfo,  BackupRestoreScenario sennario) override;
110     ErrCode StartExtTimer(bool &isExtStart) override;
111     ErrCode StartFwkTimer(bool &isFwkStart) override;
112     ErrCode StopExtTimer(bool &isExtStop) override;
113     ErrCode RefreshDataSize(int64_t totalDataSize) override;
114 
115     ErrCode SAResultReport(const std::string bundleName, const std::string resultInfo,
116                            const ErrCode errCode, const BackupRestoreScenario sennario);
117     void StartGetFdTask(std::string bundleName, wptr<Service> ptr);
118 
119     ErrCode GetBackupDataSize(bool isPreciseScan, const std::vector<BIncrementalData>& bundleNameList) override;
120     ErrCode CleanBundleTempDir(const std::string &bundleName) override;
121     ErrCode HandleExtDisconnect(BackupRestoreScenario scenario, bool isAppResultReport, ErrCode errCode) override;
122     ErrCode GetExtOnRelease(bool &isExtOnRelease) override;
123     ErrCode GetCompatibilityInfo(const std::string &bundleName, const std::string &extInfo,
124         std::string &compatInfo) override;
125 
126     // 以下都是非IPC接口
127 public:
128     void OnStart() override;
129     void OnStop() override;
130     void StopAll(const wptr<IRemoteObject> &obj, bool force = false);
131     int Dump(int fd, const std::vector<std::u16string> &args) override;
132 
133     /**
134      * @brief 执行启动 backup extension
135      *
136      * @param bundleName
137      * @return ErrCode
138      */
139     virtual ErrCode LaunchBackupExtension(const BundleName &bundleName);
140 
141     /**
142      * @brief 执行启动 backup sa extension
143      *
144      * @param bundleName
145      * @return ErrCode
146      */
147     ErrCode LaunchBackupSAExtension(const BundleName &bundleName);
148 
149     /**
150      * @brief backup extension died
151      *
152      * @param bundleName 应用名称
153      */
154     void OnBackupExtensionDied(const std::string &&bundleName, bool isCleanCalled = false);
155 
156     /**
157      * @brief extension启动连接成功
158      *
159      * @param bundleName 应用名称
160      */
161     void ExtConnectDone(std::string bundleName);
162 
163     /**
164      * @brief extension启动连接失败
165      *
166      * @param bundleName 应用名称
167      */
168     void ExtConnectFailed(const std::string &bundleName, ErrCode ret);
169 
170     /**
171      * @brief 执行backup extension 备份恢复流程
172      *
173      * @param bundleName 应用名称
174      */
175     virtual void ExtStart(const std::string &bundleName);
176 
177     /**
178      * @brief 备份恢复开始,设置处置位
179      *
180      * @param bundleName 应用名称
181      *
182      */
183     void SendStartAppGalleryNotify(const std::string &bundleName);
184 
185     /**
186      * @brief 备份恢复结束,清理处置位
187      *
188      * @param bundleName 应用名称
189      *
190      */
191     void SendEndAppGalleryNotify(const std::string &bundleName);
192 
193     /**
194      * @brief 备份恢复异常结束,清理处置位
195      *
196      */
197     void SendErrAppGalleryNotify();
198 
199     /**
200      * @brief SA开始时,清理配置文件中的处置位
201      *
202      */
203     void ClearDisposalOnSaStart();
204 
205     /**
206      * @brief 备份恢复全部结束,删除配置文件
207      *
208      *
209      */
210     void DeleteDisConfigFile();
211 
212     /**
213      * @brief 尝试清理处置
214      *
215      * @param bundleName 应用名称
216      *
217      */
218     void TryToClearDispose(const BundleName &bundleName);
219 
220     /**
221      * @brief 结束会话删除session,卸载服务
222      *
223      */
224     void SessionDeactive();
225 
226     /**
227      * @brief 构造拉起应用所需的want
228      *
229      * @param bundleName 应用名称
230      *
231      */
232     AAFwk::Want CreateConnectWant (const BundleName &bundleName);
233 
234     /**
235      * @brief SA backup回调
236      *
237      * @param bundleName 应用名称
238      * @param fd 备份数据
239      * @param result SA备份的结果(异常)
240      * @param errCode backup的错误
241      *
242      */
243     void OnSABackup(const std::string &bundleName, const int &fd, const std::string &result, const ErrCode &errCode);
244 
245     /**
246      * @brief SA restore回调
247      *
248      * @param bundleName 应用名称
249      * @param result SA恢复的结果(异常)
250      * @param errCode restore的错误
251      *
252      */
253     void OnSARestore(const std::string &bundleName, const std::string &result, const ErrCode &errCode);
254 
255     /**
256      * @brief GetBackupInfo extension成功回调
257      *
258      * @param obj 当前对象
259      * @param bundleName 应用名称
260      *
261      */
262     std::function<void(const std::string &&)> GetBackupInfoConnectDone(wptr<Service> obj,
263                                                                        const std::string &bundleName);
264 
265     /**
266      * @brief GetBackupInfo extension死亡回调
267      *
268      * @param obj 当前对象
269      * @param bundleName 应用名称
270      *
271      */
272     std::function<void(const std::string &&, bool)> GetBackupInfoConnectDied(
273         wptr<Service> obj, const std::string &bundleName);
274 
275     /**
276      * @brief timeout callback
277      *
278      * @param ptr 当前对象
279      * @param bundleName 应用名称
280      */
281     std::function<void()> TimeOutCallback(wptr<Service> ptr, std::string bundleName);
282 
283     /**
284      * @brief do timeout
285      *
286      * @param ptr 当前对象
287      * @param bundleName 应用名称
288      */
289     void DoTimeout(wptr<Service> ptr, std::string bundleName);
290 
291     /**
292      * @brief 清理残留数据
293      *
294      * @param bundleName 应用名称
295      *
296      */
297     ErrCode ClearResidualBundleData(const std::string &bundleName);
298 
299     /**
300      * @brief 添加清理记录
301      *
302      * @param bundleName 应用名称
303      *
304      */
305     void AddClearBundleRecord(const std::string &bundleName);
306 
307     /**
308      * @brief 删除清理记录
309      *
310      * @param bundleName 应用名称
311      *
312      */
313     void DelClearBundleRecord(const std::vector<std::string> &bundleNames);
314 
315     /**
316      * @brief 获取extension锁
317      *
318      * @param bundleName 应用名称
319      *
320      */
321     std::shared_ptr<ExtensionMutexInfo> GetExtensionMutex(const BundleName &bundleName);
322 
323     /**
324      * @brief 清理extension锁
325      *
326      * @param bundleName 应用名称
327      *
328      */
329     void RemoveExtensionMutex(const BundleName &bundleName);
330     void StartRunningTimer(const std::string &bundleName);
331     string BundleNameWithUserId(const string& bundleName, const int32_t userId);
332     std::tuple<std::string, int32_t> SplitBundleName(const string& bundleNameWithId);
333     void AppendBundles(const std::vector<std::string> &bundleNames);
334     void ReportOnBundleStarted(IServiceReverseType::Scenario scenario, const std::string &bundleName);
335     ErrCode AppIncrementalFileReady(const std::string &bundleName, const std::string &fileName, UniqueFd fd,
336         UniqueFd manifestFd, int32_t errCode);
337     ErrCode SendFileHandle(const std::string &bundleName, const std::string &fileName);
338     ErrCode SendIncrementalFileHandle(const std::string &bundleName, const std::string &fileName);
339     void SetExtOnRelease(const BundleName &bundleName, bool isOnRelease);
340     void RemoveExtOnRelease(const BundleName &bundleName);
341     void DoNoticeClientFinish(const std::string &bundleName, ErrCode errCode, bool isRestoreEnd);
342 public:
SystemAbility(saID,runOnCreate)343     explicit Service(int32_t saID, bool runOnCreate = false) : SystemAbility(saID, runOnCreate)
344     {
345         threadPool_.Start(BConstants::EXTENSION_THREAD_POOL_COUNT);
346         sendScannendResultThreadPool_.Start(BConstants::SA_THREAD_POOL_COUNT);
347         getDataSizeThreadPool_.Start(BConstants::SA_THREAD_POOL_COUNT);
348         callbackScannedInfoThreadPool_.Start(BConstants::SA_THREAD_POOL_COUNT);
349         session_ = sptr<SvcSessionManager>(new SvcSessionManager(wptr(this)));
350         disposal_ = make_shared<BJsonDisposalConfig>();
351         clearRecorder_ = make_shared<BJsonClearDataConfig>();
352         sched_ = sptr(new SchedScheduler(wptr(this), wptr(session_)));
353     };
~Service()354     ~Service() override
355     {
356         threadPool_.Stop();
357         sendScannendResultThreadPool_.Stop();
358         getDataSizeThreadPool_.Stop();
359         callbackScannedInfoThreadPool_.Stop();
360     };
361 
362 private:
363     /**
364      * @brief 验证调用者
365      *
366      */
367     ErrCode VerifyCaller();
368 
369     /**
370      * @brief 获取调用者名称
371      *
372      * @return std::string
373      */
374     std::string GetCallerName();
375 
376     /**
377      * @brief 获取用户id
378      *
379      * @return int32_t
380      */
381     int32_t GetUserIdDefault();
382 
383     /**
384      * @brief 验证调用者
385      *
386      * @param scenario Scenario状态
387      */
388     ErrCode VerifyCaller(IServiceReverseType::Scenario scenario);
389 
390     /**
391      * @brief 验证调用者并返回名称
392      *
393      * @return ErrCode
394      */
395     ErrCode VerifyCallerAndGetCallerName(std::string &bundleName);
396 
397     /**
398      * @brief 清除Session Sched相关资源
399      *
400      * @param bundleName 应用名称
401      */
402     void ClearSessionAndSchedInfo(const std::string &bundleName);
403 
404     /**
405      * @brief 上报总体统计打点
406      */
407     void TotalStatReport(ErrCode errCode);
408 
409     /**
410      * @brief 整个备份恢复流程结束
411      *
412      * @param errCode 错误码
413      */
414     void OnAllBundlesFinished(ErrCode errCode);
415 
416     /**
417      * @brief 执行调度器
418      *
419      */
420     void OnStartSched();
421 
422     /**
423      * @brief 通知客户端程序扩展能力处理结果
424      *
425      * @param bundleName 应用名称
426      *
427      */
428     void NoticeClientFinish(const std::string &bundleName, ErrCode errCode);
429 
430     /**
431      * @brief 处理有依赖的应用恢复
432      *
433      * @param bundleName 应用名称
434      *
435      */
436     void HandleRestoreDepsBundle(const std::string &bundleName);
437 
438     /**
439      * @brief 增量备份恢复逻辑处理
440      *
441      * @param bundleName
442      * @return true
443      * @return false
444      */
445     bool IncrementalBackup(const std::string &bundleName);
446 
447     /**
448      * @brief extension连接断开
449      *
450      * @param bundleName
451      */
452     void ExtConnectDied(const std::string &bundleName);
453 
454     /**
455      * @brief 设置当前session的关键信息
456      *
457      * @param restoreBundleInfos 待恢复的应用
458      * @param restoreBundleNames 待恢复的应用包信息
459      * @param bundleNameDetailMap bundle和detail的对应关系
460      * @param isClearDataFlags 清理数据标志集合
461      * @param restoreType 任务类型
462      * @param backupVersion 旧机backupVersion
463      */
464     void SetCurrentSessProperties(std::vector<BJsonEntityCaps::BundleInfo> &restoreBundleInfos,
465         std::vector<std::string> &restoreBundleNames,
466         std::map<std::string, std::vector<BJsonUtil::BundleDetailInfo>> &bundleNameDetailMap,
467         std::map<std::string, bool> &isClearDataFlags, RestoreTypeEnum restoreType, std::string &backupVersion);
468 
469     /**
470      * @brief set session info
471      *
472      * @param restoreBundleInfos: bundles to be restored
473      * @param restoreBundleNames: bundles info to be restored
474      * @param restoreType: retore type
475      * @param backupVersion backupVersion of old device
476      */
477     void SetCurrentSessProperties(std::vector<BJsonEntityCaps::BundleInfo> &restoreBundleInfos,
478         std::vector<std::string> &restoreBundleNames, RestoreTypeEnum restoreType, std::string &backupVersion);
479 
480     void SetCurrentSessProperties(BJsonEntityCaps::BundleInfo &info, std::map<std::string, bool> &isClearDataFlags,
481         const std::string &bundleNameIndexInfo);
482 
483     /**
484      * @brief add useridinfo to  current backup session
485      *
486      * @param bundleNames: bundleNames list
487      * @param userId: userId
488      * @param backupBundleInfos: backupBundleInfos
489      * @param isIncBackup: isIncBackup
490      *
491      */
492     void SetCurrentBackupSessProperties(const std::vector<std::string> &bundleNames, int32_t userId,
493         std::vector<BJsonEntityCaps::BundleInfo> &backupBundleInfos, bool isIncBackup);
494 
495     /**
496      * @brief send userid to app
497      *
498      * @param bundleName: bundleName
499      * @param userId: userId
500      *
501      */
502     void SendUserIdToApp(std::string &bundleName, int32_t userId);
503 
504     /**
505      * @brief 通知权限模块
506      *
507      * @param bundleName 包名称
508      *
509     */
510     void HandleCurBundleEndWork(std::string bundleName, const BackupRestoreScenario sennario);
511 
512     /**
513      * @brief SA 备份恢复结束
514      *
515      * @param bundleName SAID
516      *
517      * @return ErrCode OK if saDone, otherwise saDone failed.
518     */
519     ErrCode SADone(const ErrCode errCode, std::string bundleName);
520 
521     /**
522      * @brief SA备份恢复入口
523      *
524      * @param bundleName SAID
525      *
526      * @return ErrCode OK if backup sa, otherwise backup sa failed.
527     */
528     ErrCode BackupSA(std::string bundleName);
529 
530     /**
531      * @brief SA增量备份恢复入口
532      *
533      * @param bundleName SAID
534      *
535      * @return ErrCode OK if backup sa, otherwise backup sa failed.
536     */
537     ErrCode IncrementalBackupSA(std::string bundleName);
538 
539     /**
540      * @brief 执行通知调用方
541      *
542      * @param errCode 错误码
543      * @param callerName 业务调用方
544      *
545      */
546     void NotifyCallerCurAppDone(ErrCode errCode, const std::string &callerName);
547 
548     /**
549      * @brief 执行通知调用方
550      *
551      * @param errCode 错误码
552      * @param callerName 业务调用方
553      *
554      */
555     void NotifyCallerCurAppIncrementDone(ErrCode errCode, const std::string &callerName);
556 
557     void SetWant(AAFwk::Want &want, const BundleName &bundleName, const BConstants::ExtensionAction &action);
558 
559     /**
560      * @brief GetBackupInfo 任务执行
561      *
562      * @param bundleName 应用名称
563      * @param result 业务结果出参
564      *
565      */
566     ErrCode GetBackupInfoCmdHandle(const BundleName &bundleName, std::string &result);
567 
568     /**
569      * @brief 添加需要清理的Session
570      *
571      * @param bundleNames 需要清理的应用包信息
572      *
573      */
574     ErrCode AppendBundlesClearSession(const std::vector<BundleName> &bundleNames);
575 
576     /**
577      * @brief 设置是否自占用Session
578      *
579      * @param isOccupyingSession 框架是否自占用session
580      *
581      */
582     void SetOccupySession(bool isOccupyingSession);
583 
584     /**
585      * @brief 尝试拉起某个应用的extension
586      *
587      * @param bundleName 目标应用
588      * @param extConnection 框架和应用的连接
589      *
590      */
591     ErrCode TryToConnectExt(const std::string& bundleName, sptr<SvcBackupConnection>& extConnection);
592 
593     void ReportOnExtConnectFailed(const IServiceReverseType::Scenario scenario,
594         const std::string &bundleName, const ErrCode ret);
595 
596     void ReleaseOnException();
597 
598     vector<BIncrementalData> MakeDetailList(const vector<BundleName> &bundleNames);
599 
600     vector<string> GetBundleNameByDetails(const std::vector<BIncrementalData> &bundlesToBackup);
601 
602     void HandleCurGroupBackupInfos(vector<BJsonEntityCaps::BundleInfo> &bundleInfos,
603         std::map<std::string, std::vector<BJsonUtil::BundleDetailInfo>> &bundleNameDetailMap,
604         std::map<std::string, bool> &isClearDataFlags);
605 
606     void HandleCurGroupIncBackupInfos(vector<BJsonEntityCaps::BundleInfo> &bundleInfos,
607         std::map<std::string, std::vector<BJsonUtil::BundleDetailInfo>> &bundleNameDetailMap,
608         std::map<std::string, bool> &isClearDataFlags);
609 
610     void TimeoutRadarReport(IServiceReverseType::Scenario scenario, std::string &bundleName);
611 
612     void OnBundleStarted(BError error, sptr<SvcSessionManager> session, const BundleName &bundleName);
613 
614     void HandleExceptionOnAppendBundles(sptr<SvcSessionManager> session, const vector<BundleName> &appendBundleNames,
615         const vector<BundleName> &restoreBundleNames);
616 
617     void BundleBeginRadarReport(const std::string &bundleName, const ErrCode errCode,
618         const IServiceReverseType::Scenario scenario);
619 
620     void BundleEndRadarReport(const std::string &bundleName, ErrCode errCode,
621         const IServiceReverseType::Scenario scenario);
622 
623     void FileReadyRadarReport(const std::string &bundleName, const std::string &fileName, const ErrCode errCode,
624         const IServiceReverseType::Scenario scenario);
625 
626     void ExtensionConnectFailRadarReport(const std::string &bundleName, const ErrCode errCode,
627         const IServiceReverseType::Scenario scenario);
628 
629     void OnStartResRadarReport(const std::vector<std::string> &bundleNameList, int32_t stage);
630 
631     void PermissionCheckFailRadar(const std::string &info, const std::string &func);
632 
633     bool IsReportBundleExecFail(const std::string &bundleName);
634 
635     void ClearBundleRadarReport();
636 
637     void UpdateBundleRadarReport(const std::string &bundleName);
638 
639     bool IsReportFileReadyFail(const std::string &bundleName);
640 
641     void ClearFileReadyRadarReport();
642 
643     void UpdateFailedBundles(const std::string &bundleName, BundleTaskInfo taskInfo);
644 
645     void ClearFailedBundles();
646     void CreateDirIfNotExist(const std::string &path);
647 
648     void GetOldDeviceBackupVersion();
649 
650     std::vector<std::string> GetSupportBackupBundleNames(vector<BJsonEntityCaps::BundleInfo> &bundleInfos,
651         bool isIncBackup, const vector<std::string> &srcBundleNames);
652 
653     void RefreshBundleDataSize(const vector<BJsonEntityCaps::BundleInfo> &newBundleInfos,
654         std::string bundleName, wptr<Service> ptr);
655 
656     void HandleNotSupportBundleNames(const std::vector<std::string> &srcBundleNames,
657         std::vector<std::string> &supportBundleNames, bool isIncBackup);
658 
659     void SetBundleIncDataInfo(const std::vector<BIncrementalData> &bundlesToBackup,
660         std::vector<std::string> &supportBundleNames);
661 
662     void CancelTask(std::string bundleName, wptr<Service> ptr);
663 
664     bool CancelSessionClean(sptr<SvcSessionManager> session, std::string bundleName);
665 
666     void SetUserIdAndRestoreType(RestoreTypeEnum restoreType, int32_t userId);
667 
668     ErrCode VerifySendRateParam();
669 
670     ErrCode HandleCurBundleFileReady(const std::string &bundleName, const std::string &fileName, bool isIncBackup);
671 
672     ErrCode HandleCurAppDone(ErrCode errCode, const std::string &bundleName, bool isIncBackup);
673 
674     void StartCurBundleBackupOrRestore(const std::string &bundleName);
675 
676     void CallOnBundleEndByScenario(const std::string &bundleName, BackupRestoreScenario scenario, ErrCode errCode);
677 
678     void GetDataSizeStepByStep(bool isPreciseScan, vector<BIncrementalData> bundleNameList, string &scanning);
679 
680     void GetPresumablySize(vector<BIncrementalData> bundleNameList, string &scanning);
681 
682     void GetPrecisesSize(vector<BIncrementalData> bundleNameList, string &scanning);
683 
684     void WriteToList(BJsonUtil::BundleDataSize bundleDataSize);
685 
686     void DeleteFromList(size_t scannedSize);
687 
688     void WriteScannedInfoToList(const string &bundleName, int64_t dataSize, int64_t incDataSize);
689 
690     void SendScannedInfo(const string &scannendInfos, sptr<SvcSessionManager> session);
691 
692     void CyclicSendScannedInfo(bool isPreciseScan, vector<BIncrementalData> bundleNameList);
693 
694     bool GetScanningInfo(wptr<Service> obj, size_t scannedSize, string &scanning);
695 
696     void SetScanningInfo(string &scanning, string name);
697 
698     ErrCode Cancel(const std::string& bundleName, int32_t &result);
699     void HandleOnReleaseAndDisconnect(sptr<SvcSessionManager> sessionPtr, const std::string &bundleName);
700 
701     ErrCode InitRestoreSession(const sptr<IServiceReverse>& remote, std::string &errMsg);
702     ErrCode InitBackupSession(const sptr<IServiceReverse>& remote, std::string &errMsg);
703     ErrCode InitIncrementalBackupSession(const sptr<IServiceReverse>& remote, std::string &errMsg);
704     UniqueFd GetLocalCapabilities();
705     UniqueFd GetLocalCapabilitiesForBundleInfos();
706     ErrCode AppFileReady(const std::string &fileName, UniqueFd fd, int32_t errCode);
707     ErrCode AppIncrementalFileReady(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd, int32_t errCode);
708     ErrCode AppendBundlesRestoreSession(UniqueFd fd, const std::vector<BundleName> &bundleNames,
709                                         const std::vector<std::string> &bundleInfos,
710                                         RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND,
711                                         int32_t userId = DEFAULT_INVAL_VALUE);
712     ErrCode AppendBundlesRestoreSession(UniqueFd fd, const std::vector<BundleName> &bundleNames,
713                                         RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND,
714                                         int32_t userId = DEFAULT_INVAL_VALUE);
715     UniqueFd GetLocalCapabilitiesIncremental(const std::vector<BIncrementalData> &bundleNames);
716     ErrCode AppendBundlesIncrementalBackupSession(const std::vector<BIncrementalData> &bundlesToBackup,
717                                                   const std::vector<std::string> &infos);
718 
719     ErrCode HelpToAppIncrementalFileReady(const string &bundleName, const string &fileName, sptr<IExtension> proxy);
720     vector<BJsonEntityCaps::BundleInfo> GetRestoreBundleNames(UniqueFd fd, sptr<SvcSessionManager> session,
721         const vector<BundleName> &bundleNames, std::string &oldBackupVersion);
722     void AppStatReportErr(const string &bundleName, const string &func, RadarError err);
723     void SaStatReport(const string &bundleName, const string &func, RadarError err);
724     void ClearIncrementalStatFile(int32_t userId, const string &bundleName);
725     BJsonCachedEntity<BJsonEntityCaps> CreateJsonEntity(UniqueFd &fd,
726         vector<BJsonEntityCaps::BundleInfo> &bundleInfos, const std::vector<BIncrementalData> &bundleNames);
TotalStart()727     void TotalStart()
728     {
729         if (totalStatistic_ != nullptr) {
730             totalStatistic_->totalSpendTime_.Start();
731         }
732     }
733 
GetBundleInfoStart()734     void GetBundleInfoStart()
735     {
736         if (totalStatistic_ != nullptr) {
737             totalStatistic_->getBundleInfoSpend_.Start();
738         }
739     }
740 
GetBundleInfoEnd()741     void GetBundleInfoEnd()
742     {
743         if (totalStatistic_ != nullptr) {
744             totalStatistic_->getBundleInfoSpend_.End();
745         }
746     }
747 
UpdateHandleCnt(ErrCode errCode)748     void UpdateHandleCnt(ErrCode errCode)
749     {
750         if (totalStatistic_ != nullptr) {
751             if (errCode == ERR_OK) {
752                 totalStatistic_->succBundleCount_.fetch_add(1);
753             } else {
754                 totalStatistic_->failBundleCount_.fetch_add(1);
755             }
756         }
757     }
758 private:
759     static sptr<Service> instance_;
760     static std::mutex instanceLock_;
761     std::mutex getBackupInfoProcLock_;
762     std::mutex getBackupInfoSyncLock_;
763     std::condition_variable getBackupInfoCondition_;
764     static inline std::atomic<uint32_t> seed {1};
765     std::atomic<bool> isConnectDied_ {false};
766     std::atomic<bool> isOccupyingSession_ {false};
767     std::atomic<int> isCreatingIncreaseFile_ {0};
768 
769     sptr<SvcSessionManager> session_;
770     sptr<SchedScheduler> sched_;
771     std::shared_ptr<BJsonDisposalConfig> disposal_;
772     std::shared_ptr<BJsonClearDataConfig> clearRecorder_;
773     std::atomic<bool> isInRelease_ {false};
774     std::atomic<bool> isRmConfigFile_ {true};
775     friend class ServiceTest;
776 
777     OHOS::ThreadPool threadPool_;
778     std::mutex bundleExecRadarLock_;
779     std::set<std::string> bundleExecRadarSet_;
780     std::mutex fileReadyRadarLock_;
781     std::map<BundleName, int> fileReadyRadarMap_;
782     std::mutex extensionMutexLock_;
783     std::mutex failedBundlesLock_;
784 
785     std::mutex scannedListLock_;
786     std::mutex getDataSizeLock_;
787     OHOS::ThreadPool callbackScannedInfoThreadPool_;
788     OHOS::ThreadPool getDataSizeThreadPool_;
789     OHOS::ThreadPool sendScannendResultThreadPool_;
790     std::condition_variable getDataSizeCon_;
791     std::atomic<bool> isScannedEnd_ {false};
792     std::atomic<bool> onScanning_ {false};
793     std::shared_ptr<RadarTotalStatistic> totalStatistic_ = nullptr;
794     std::shared_mutex statMapMutex_;
795     std::map<std::string, std::shared_ptr<RadarAppStatistic>> saStatisticMap_;
796     std::map<BundleName, std::atomic<bool>> backupExtOnReleaseMap_;
797 public:
798     std::map<BundleName, std::shared_ptr<ExtensionMutexInfo>> backupExtMutexMap_;
799     std::map<BundleName, BundleTaskInfo> failedBundles_;
800     std::atomic<uint32_t> successBundlesNum_ {0};
801     std::vector<BJsonUtil::BundleDataSize> bundleDataSizeList_;
802     std::string scannedInfo_;
803 };
804 } // namespace OHOS::FileManagement::Backup
805 
806 #endif // OHOS_FILEMGMT_BACKUP_SERVICE_H