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_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 ErrCode Release() override; 50 51 UniqueFd GetLocalCapabilitiesIncremental(const std::vector<BIncrementalData> &bundleNames) override; 52 ErrCode InitIncrementalBackupSession(sptr<IServiceReverse> remote) override; 53 ErrCode AppendBundlesIncrementalBackupSession(const std::vector<BIncrementalData> &bundlesToBackup) override; 54 55 ErrCode PublishIncrementalFile(const BFileInfo &fileInfo) override; 56 ErrCode AppIncrementalFileReady(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd) override; 57 ErrCode AppIncrementalDone(ErrCode errCode) override; 58 ErrCode GetIncrementalFileHandle(const std::string &bundleName, const std::string &fileName) override; 59 60 // 以下都是非IPC接口 61 public: 62 void OnStart() override; 63 void OnStop() override; 64 void StopAll(const wptr<IRemoteObject> &obj, bool force = false); 65 int Dump(int fd, const std::vector<std::u16string> &args) override; 66 67 /** 68 * @brief 执行启动 backup extension 69 * 70 * @param bundleName 71 * @return ErrCode 72 */ 73 virtual ErrCode LaunchBackupExtension(const BundleName &bundleName); 74 75 /** 76 * @brief backup extension died 77 * 78 * @param bundleName 应用名称 79 */ 80 void OnBackupExtensionDied(const std::string &&bundleName); 81 82 /** 83 * @brief extension启动连接成功 84 * 85 * @param bundleName 应用名称 86 */ 87 void ExtConnectDone(std::string bundleName); 88 89 /** 90 * @brief extension启动连接失败 91 * 92 * @param bundleName 应用名称 93 */ 94 void ExtConnectFailed(const std::string &bundleName, ErrCode ret); 95 96 /** 97 * @brief 执行backup extension 备份恢复流程 98 * 99 * @param bundleName 应用名称 100 */ 101 virtual void ExtStart(const std::string &bundleName); 102 103 /** 104 * @brief 发送备份恢复通知到应用市场 105 * 106 * @param bundleName 应用名称 107 * 108 */ 109 void SendAppGalleryNotify(const std::string &bundleName); 110 111 /** 112 * @brief 结束会话删除session,卸载服务 113 * 114 */ 115 void SessionDeactive(); 116 117 public: SystemAbility(saID,runOnCreate)118 explicit Service(int32_t saID, bool runOnCreate = false) : SystemAbility(saID, runOnCreate) 119 { 120 session_ = sptr<SvcSessionManager>(new SvcSessionManager(wptr(this))); 121 }; 122 ~Service() override = default; 123 124 private: 125 /** 126 * @brief 验证调用者 127 * 128 */ 129 void VerifyCaller(); 130 131 /** 132 * @brief 验证调用者 133 * 134 * @param scenario Scenario状态 135 */ 136 void VerifyCaller(IServiceReverse::Scenario scenario); 137 138 /** 139 * @brief 验证调用者并返回名称 140 * 141 * @return std::string 142 */ 143 std::string VerifyCallerAndGetCallerName(); 144 145 /** 146 * @brief 清除Session Sched相关资源 147 * 148 * @param bundleName 应用名称 149 */ 150 void ClearSessionAndSchedInfo(const std::string &bundleName); 151 152 /** 153 * @brief 整个备份恢复流程结束 154 * 155 * @param errCode 错误码 156 */ 157 void OnAllBundlesFinished(ErrCode errCode); 158 159 /** 160 * @brief 执行调度器 161 * 162 */ 163 void OnStartSched(); 164 165 /** 166 * @brief 通知客户端程序扩展能力处理结果 167 * 168 * @param bundleName 应用名称 169 * 170 */ 171 void NoticeClientFinish(const std::string &bundleName, ErrCode errCode); 172 173 /** 174 * @brief 处理有依赖的应用恢复 175 * 176 * @param bundleName 应用名称 177 * 178 */ 179 void HandleRestoreDepsBundle(const std::string &bundleName); 180 181 /** 182 * @brief 增量备份恢复逻辑处理 183 * 184 * @param bundleName 185 * @return true 186 * @return false 187 */ 188 bool IncrementalBackup(const std::string &bundleName); 189 190 /** 191 * @brief extension连接断开 192 * 193 * @param bundleName 194 */ 195 void ExtConnectDied(const std::string &bundleName); 196 197 private: 198 static sptr<Service> instance_; 199 static std::mutex instanceLock_; 200 static inline std::atomic<uint32_t> seed {1}; 201 202 sptr<SvcSessionManager> session_; 203 sptr<SchedScheduler> sched_; 204 205 friend class ServiceTest; 206 }; 207 } // namespace OHOS::FileManagement::Backup 208 209 #endif // OHOS_FILEMGMT_BACKUP_SERVICE_H