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 #ifndef OHOS_FILEMGMT_BACKUP_SERVICE_H 17 #define OHOS_FILEMGMT_BACKUP_SERVICE_H 18 19 #include <cstdint> 20 #include <mutex> 21 22 #include "i_service_reverse.h" 23 #include "iremote_stub.h" 24 #include "module_sched/sched_scheduler.h" 25 #include "service_stub.h" 26 #include "svc_session_manager.h" 27 #include "system_ability.h" 28 29 namespace OHOS::FileManagement::Backup { 30 class Service : public SystemAbility, public ServiceStub, protected NoCopyable { 31 DECLARE_SYSTEM_ABILITY(Service); 32 33 // 以下都是IPC接口 34 public: 35 ErrCode InitRestoreSession(sptr<IServiceReverse> remote) override; 36 ErrCode InitBackupSession(sptr<IServiceReverse> remote) override; 37 ErrCode Start() override; 38 UniqueFd GetLocalCapabilities() override; 39 ErrCode PublishFile(const BFileInfo &fileInfo) override; 40 ErrCode AppFileReady(const std::string &fileName, UniqueFd fd) override; 41 ErrCode AppDone(ErrCode errCode) override; 42 ErrCode GetFileHandle(const std::string &bundleName, const std::string &fileName) override; 43 ErrCode AppendBundlesRestoreSession(UniqueFd fd, 44 const std::vector<BundleName> &bundleNames, 45 RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND, 46 int32_t userId = DEFAULT_INVAL_VALUE) override; 47 ErrCode AppendBundlesBackupSession(const std::vector<BundleName> &bundleNames) override; 48 ErrCode Finish() override; 49 50 // 以下都是非IPC接口 51 public: 52 void OnStart() override; 53 void OnStop() override; 54 void StopAll(const wptr<IRemoteObject> &obj, bool force = false); 55 int Dump(int fd, const std::vector<std::u16string> &args) override; 56 57 /** 58 * @brief 执行启动 backup extension 59 * 60 * @param bundleName 61 * @return ErrCode 62 */ 63 virtual ErrCode LaunchBackupExtension(const BundleName &bundleName); 64 65 /** 66 * @brief backup extension died 67 * 68 * @param bundleName 应用名称 69 */ 70 void OnBackupExtensionDied(const std::string &&bundleName, ErrCode ret); 71 72 /** 73 * @brief extension启动连接成功 74 * 75 * @param bundleName 应用名称 76 */ 77 void ExtConnectDone(std::string bundleName); 78 79 /** 80 * @brief extension启动连接失败 81 * 82 * @param bundleName 应用名称 83 */ 84 void ExtConnectFailed(const std::string &bundleName, ErrCode ret); 85 86 /** 87 * @brief 执行backup extension 备份恢复流程 88 * 89 * @param bundleName 应用名称 90 */ 91 virtual void ExtStart(const std::string &bundleName); 92 93 public: SystemAbility(saID,runOnCreate)94 explicit Service(int32_t saID, bool runOnCreate = false) : SystemAbility(saID, runOnCreate) 95 { 96 session_ = sptr<SvcSessionManager>(new SvcSessionManager(wptr(this))); 97 }; 98 ~Service() override = default; 99 100 private: 101 /** 102 * @brief 验证调用者 103 * 104 */ 105 void VerifyCaller(); 106 107 /** 108 * @brief 验证调用者 109 * 110 * @param scenario Scenario状态 111 */ 112 void VerifyCaller(IServiceReverse::Scenario scenario); 113 114 /** 115 * @brief 验证调用者并返回名称 116 * 117 * @return std::string 118 */ 119 std::string VerifyCallerAndGetCallerName(); 120 121 /** 122 * @brief 清除Session Sched相关资源 123 * 124 * @param bundleName 应用名称 125 */ 126 void ClearSessionAndSchedInfo(const std::string &bundleName); 127 128 /** 129 * @brief 整个备份恢复流程结束 130 * 131 * @param errCode 错误码 132 */ 133 void OnAllBundlesFinished(ErrCode errCode); 134 135 /** 136 * @brief 执行调度器 137 * 138 */ 139 void OnStartSched(); 140 141 private: 142 static sptr<Service> instance_; 143 static std::mutex instanceLock_; 144 static inline std::atomic<uint32_t> seed {1}; 145 146 sptr<SvcSessionManager> session_; 147 sptr<SchedScheduler> sched_; 148 149 friend class ServiceTest; 150 }; 151 } // namespace OHOS::FileManagement::Backup 152 153 #endif // OHOS_FILEMGMT_BACKUP_SERVICE_H