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_EXT_BACKUP_H 17 #define OHOS_FILEMGMT_BACKUP_EXT_BACKUP_H 18 19 #include "b_resources/b_constants.h" 20 #include "ext_backup_context_js.h" 21 #include "extension_base.h" 22 #include "runtime.h" 23 #include "want.h" 24 25 namespace OHOS::FileManagement::Backup { 26 27 class ExtBackup; 28 using CreatorFunc = std::function<ExtBackup *(const std::unique_ptr<AbilityRuntime::Runtime> &runtime)>; 29 class BackupExtExtension; 30 31 class ExtBackup : public AbilityRuntime::ExtensionBase<ExtBackupContext> { 32 public: 33 /** 34 * @brief Called when this extension is started. You must override this function if you want to perform some 35 * initialization operations during extension startup. 36 * 37 * This function can be called only once in the entire lifecycle of an extension. 38 * @param Want Indicates the {@link Want} structure containing startup information about the extension. 39 */ 40 void OnStart(const AAFwk::Want &want) override; 41 42 /** 43 * @brief Init the extension. 44 * 45 * @param record the extension record. 46 * @param application the application info. 47 * @param handler the extension handler. 48 * @param token the remote token. 49 */ 50 void Init(const std::shared_ptr<AbilityRuntime::AbilityLocalRecord> &record, 51 const std::shared_ptr<AbilityRuntime::OHOSApplication> &application, 52 std::shared_ptr<AbilityRuntime::AbilityHandler> &handler, 53 const sptr<IRemoteObject> &token) override; 54 55 /** 56 * @brief Called back when Service is started. 57 * This method can be called only by Service. You can use the StartAbility(ohos.aafwk.content.Want) method to start 58 * Service. Then the system calls back the current method to use the transferred want parameter to execute its own 59 * logic. 60 * 61 * @param want Indicates the want of Service to start. 62 * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being 63 * destroyed, and the value false indicates a normal startup. 64 * @param startId Indicates the number of times the Service extension has been started. The startId is 65 * incremented by 1 every time the extension is started. For example, if the extension has been started 66 * for six times, the value of startId is 6. 67 */ 68 void OnCommand(const AAFwk::Want &want, bool restart, int startId) override; 69 70 /** 71 * @brief Called when this backup extension ability is connected for the first time. 72 * 73 * You can override this function to implement your own processing logic. 74 * 75 * @param want Indicates the {@link Want} structure containing connection information about the backup 76 * extension. 77 * @return Returns a pointer to the <b>sid</b> of the connected backup extension ability. 78 */ 79 sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override; 80 81 /** 82 * @brief Called when all abilities connected to this Wallpaper extension are disconnected. 83 * 84 * You can override this function to implement your own processing logic. 85 * 86 */ 87 void OnDisconnect(const AAFwk::Want &want) override; 88 89 public: 90 /** 91 * @brief Create Extension. 92 * 93 * @param runtime The runtime. 94 * @return The ServiceExtension instance. 95 */ 96 static ExtBackup *Create(const std::unique_ptr<AbilityRuntime::Runtime> &runtime); 97 98 /** 99 * @brief Get the Extension Action object 100 * 101 * @return BConstants::ExtensionAction 102 */ 103 virtual BConstants::ExtensionAction GetExtensionAction() const; 104 105 /** 106 * @brief Get the User Config, then check if 107 * 108 * @return allowed ro not 109 */ 110 virtual bool AllowToBackupRestore(); 111 112 /** 113 * @brief Get whether FullBackupOnly or not 114 * 115 * @return FullBackupOnly ro not 116 */ 117 virtual bool UseFullBackupOnly(void) const; 118 119 /** 120 * @brief Get the user configure 121 * 122 * @return user configure 123 */ 124 virtual std::string GetUsrConfig() const; 125 126 /** 127 * @brief do backup. Subclasses can inherit to implement their own custom functionality. 128 */ 129 virtual ErrCode OnBackup(std::function<void(ErrCode, std::string)> callback); 130 131 virtual ErrCode OnBackup(std::function<void(ErrCode, std::string)> callback, 132 std::function<void(ErrCode, const std::string)> callbackEx); 133 134 /** 135 * @brief Called do restore. 136 */ 137 virtual ErrCode OnRestore(std::function<void(ErrCode, std::string)> callback, 138 std::function<void(ErrCode, const std::string)> callbackEx); 139 140 /** 141 * @brief Called do restore. 142 */ 143 virtual ErrCode OnRestore(std::function<void(ErrCode, std::string)> callback); 144 145 /** 146 * @brief Called do GetBackupInfo. 147 */ 148 virtual ErrCode GetBackupInfo(std::function<void(ErrCode, std::string)> callback); 149 150 /** 151 * @brief Called do OnProcess 152 */ 153 virtual ErrCode OnProcess(std::function<void(ErrCode, std::string)> callback); 154 155 /* 156 * @brief Called do onRelease. 157 */ 158 virtual ErrCode OnRelease(std::function<void(ErrCode, std::string)> callback, int32_t scenario); 159 160 /** 161 * @brief 数据迁移判断 162 * 163 * @return true 164 * @return false 165 */ 166 bool WasFromSpecialVersion(void); 167 168 /** 169 * @brief Version for clone and cloud 170 * 171 * @return true 172 * @return false 173 */ 174 bool SpecialVersionForCloneAndCloud(void); 175 176 /** 177 * @brief 数据以准备就绪 178 * 179 * @return true 180 * @return false 181 */ 182 bool RestoreDataReady(); 183 184 /** 185 * @brief Invoke the extended function of the APP 186 */ 187 virtual ErrCode InvokeAppExtMethod(ErrCode, const std::string); 188 189 /** 190 * @brief Called do getBackupCompatibilityInfo. 191 */ 192 virtual ErrCode GetBackupCompatibilityInfo(std::function<void(ErrCode, const std::string)> callbackEx, 193 std::string extInfo); 194 195 /** 196 * @brief Called do getRestoreCompatibilityInfo. 197 */ 198 virtual ErrCode GetRestoreCompatibilityInfo(std::function<void(ErrCode, const std::string)> callbackEx, 199 std::string extInfo); 200 201 public: 202 ExtBackup() = default; 203 ~ExtBackup() override = default; 204 205 static void SetCreator(const CreatorFunc &creator); 206 207 void SetBackupExtExtension(const wptr<BackupExtExtension> &extExtension); 208 209 protected: 210 std::string appVersionStr_; 211 std::string restoreRetInfo_; 212 std::string restoreExtInfo_; 213 std::string backupExtInfo_; 214 int64_t appVersionCode_; 215 int restoreType_; 216 std::string oldBackupVersion_; 217 218 private: 219 BConstants::ExtensionAction VerifyAndGetAction(const AAFwk::Want &want, 220 std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo); 221 BConstants::ExtensionAction extAction_ {BConstants::ExtensionAction::INVALID}; 222 ErrCode GetParament(const AAFwk::Want &want); 223 static CreatorFunc creator_; 224 wptr<BackupExtExtension> bakExtExtension_; 225 }; 226 } // namespace OHOS::FileManagement::Backup 227 228 #endif // OHOS_FILEMGMT_BACKUP_EXT_BACKUP_H 229