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 #include "ext_backup.h"
17
18 #include <algorithm>
19 #include <cstdio>
20 #include <sstream>
21
22 #include <sys/stat.h>
23 #include <sys/types.h>
24
25 #include "bundle_mgr_client.h"
26 #include "unique_fd.h"
27
28 #include "b_anony/b_anony.h"
29 #include "b_error/b_error.h"
30 #include "b_error/b_excep_utils.h"
31 #include "b_json/b_json_cached_entity.h"
32 #include "b_json/b_json_entity_extension_config.h"
33 #include "b_resources/b_constants.h"
34 #include "ext_backup_ani.h"
35 #include "ext_backup_js.h"
36 #include "ext_extension.h"
37 #include "filemgmt_libhilog.h"
38 #include "service_common.h"
39 #include "iservice.h"
40
41 namespace OHOS::FileManagement::Backup {
42 using namespace std;
43
44 CreatorFunc ExtBackup::creator_ = nullptr;
SetCreator(const CreatorFunc & creator)45 void ExtBackup::SetCreator(const CreatorFunc &creator)
46 {
47 creator_ = creator;
48 }
49
SetBackupExtExtension(const wptr<BackupExtExtension> & extExtension)50 void ExtBackup::SetBackupExtExtension(const wptr<BackupExtExtension> &extExtension)
51 {
52 bakExtExtension_ = extExtension;
53 }
54
Init(const shared_ptr<AbilityRuntime::AbilityLocalRecord> & record,const shared_ptr<AbilityRuntime::OHOSApplication> & application,shared_ptr<AbilityRuntime::AbilityHandler> & handler,const sptr<IRemoteObject> & token)55 void ExtBackup::Init(const shared_ptr<AbilityRuntime::AbilityLocalRecord> &record,
56 const shared_ptr<AbilityRuntime::OHOSApplication> &application,
57 shared_ptr<AbilityRuntime::AbilityHandler> &handler,
58 const sptr<IRemoteObject> &token)
59 {
60 HILOGI("Init the BackupExtensionAbility(Base)");
61 AbilityRuntime::ExtensionBase<ExtBackupContext>::Init(record, application, handler, token);
62 }
63
Create(const unique_ptr<AbilityRuntime::Runtime> & runtime)64 ExtBackup *ExtBackup::Create(const unique_ptr<AbilityRuntime::Runtime> &runtime)
65 {
66 if (!runtime) {
67 HILOGD("Create as BackupExtensionAbility(base)");
68 return new ExtBackup();
69 }
70
71 if (creator_) {
72 HILOGD("Create as BackupExtensionAbility(creater)");
73 return creator_(runtime);
74 }
75
76 switch (runtime->GetLanguage()) {
77 case AbilityRuntime::Runtime::Language::JS:
78 HILOGD("Create as BackupExtensionAbility(JS)");
79 return ExtBackupJs::Create(runtime);
80 case AbilityRuntime::Runtime::Language::ETS:
81 HILOGD("Create as BackupExtensionAbility(ETS)");
82 return ExtBackupAni::Create(runtime);
83 default:
84 HILOGD("Create as BackupExtensionAbility(base)");
85 return new ExtBackup();
86 }
87 }
88
OnStart(const AAFwk::Want & want)89 void ExtBackup::OnStart(const AAFwk::Want &want)
90 {
91 HILOGI("BackupExtensionAbility was started");
92 Extension::OnStart(want);
93 }
94
OnCommand(const AAFwk::Want & want,bool restart,int startId)95 void ExtBackup::OnCommand(const AAFwk::Want &want, bool restart, int startId)
96 {
97 HILOGI("BackupExtensionAbility was invoked. restart=%{public}d, startId=%{public}d", restart, startId);
98
99 // REM: 处理返回结果 ret
100 // REM: 通过杀死进程实现 Stop
101 }
102
GetUsrConfig() const103 string ExtBackup::GetUsrConfig() const
104 {
105 vector<string> config;
106 AppExecFwk::BundleMgrClient client;
107 BExcepUltils::BAssert(abilityInfo_, BError::Codes::EXT_BROKEN_FRAMEWORK, "Invalid abilityInfo_");
108 const AppExecFwk::AbilityInfo &info = *abilityInfo_;
109 if (!client.GetProfileFromAbility(info, "ohos.extension.backup", config)) {
110 throw BError(BError::Codes::EXT_INVAL_ARG, "Failed to invoke the GetProfileFromAbility method.");
111 }
112 if (config.empty()) {
113 HILOGE("GetUsrConfig empty.");
114 } else {
115 HILOGI("GetUsrConfig:%{public}s", config[0].c_str());
116 }
117
118 return config.empty() ? "" : config[0];
119 }
120
AllowToBackupRestore()121 bool ExtBackup::AllowToBackupRestore()
122 {
123 string usrConfig = GetUsrConfig();
124 BJsonCachedEntity<BJsonEntityExtensionConfig> cachedEntity(usrConfig);
125 auto cache = cachedEntity.Structuralize();
126 if (cache.GetAllowToBackupRestore() || WasFromSpecialVersion() || SpecialVersionForCloneAndCloud()) {
127 return true;
128 }
129 return false;
130 }
131
UseFullBackupOnly(void) const132 bool ExtBackup::UseFullBackupOnly(void) const
133 {
134 string usrConfig = GetUsrConfig();
135 BJsonCachedEntity<BJsonEntityExtensionConfig> cachedEntity(usrConfig);
136 auto cache = cachedEntity.Structuralize();
137 if (cache.GetFullBackupOnly()) {
138 HILOGI("backup use fullBackupOnly.");
139 return true;
140 }
141 HILOGI("backup not use fullBackupOnly.");
142 return false;
143 }
144
GetExtensionAction() const145 BConstants::ExtensionAction ExtBackup::GetExtensionAction() const
146 {
147 return extAction_;
148 }
149
VerifyAndGetAction(const AAFwk::Want & want,std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo)150 BConstants::ExtensionAction ExtBackup::VerifyAndGetAction(const AAFwk::Want &want,
151 std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo)
152 {
153 string pendingMsg = "Received an empty ability. You must missed the init proc";
154 BExcepUltils::BAssert(abilityInfo, BError::Codes::EXT_INVAL_ARG, pendingMsg);
155 using namespace BConstants;
156 ExtensionAction extAction {want.GetIntParam(EXTENSION_ACTION_PARA, static_cast<int>(ExtensionAction::INVALID))};
157 if (extAction == ExtensionAction::INVALID) {
158 int extActionInt = static_cast<int>(extAction);
159 pendingMsg = string("Want must specify a valid action instead of ").append(to_string(extActionInt));
160 throw BError(BError::Codes::EXT_INVAL_ARG, pendingMsg);
161 }
162 return extAction;
163 }
164
GetParament(const AAFwk::Want & want)165 ErrCode ExtBackup::GetParament(const AAFwk::Want &want)
166 {
167 if (extAction_ == BConstants::ExtensionAction::RESTORE) {
168 appVersionStr_ = want.GetStringParam(BConstants::EXTENSION_VERSION_NAME_PARA);
169 appVersionCode_ = want.GetLongParam(BConstants::EXTENSION_VERSION_CODE_PARA, 0);
170 restoreType_ = want.GetIntParam(BConstants::EXTENSION_RESTORE_TYPE_PARA, 0);
171 restoreExtInfo_ = want.GetStringParam(BConstants::EXTENSION_RESTORE_EXT_INFO_PARA);
172 oldBackupVersion_ = want.GetStringParam(BConstants::EXTENSION_OLD_BACKUP_VERSION_PARA);
173 HILOGI("restoreExtInfo_ is %{public}s", GetAnonyString(restoreExtInfo_).c_str());
174 HILOGI("Get version %{public}s type %{public}d from want when restore.", appVersionStr_.c_str(), restoreType_);
175 HILOGI("oldBackupVersion_ is %{public}s", oldBackupVersion_.c_str());
176 } else if (extAction_ == BConstants::ExtensionAction::BACKUP) {
177 backupExtInfo_ = want.GetStringParam(BConstants::EXTENSION_BACKUP_EXT_INFO_PARA);
178 HILOGI("backupExtInfo_ is %{public}s", backupExtInfo_.c_str());
179 }
180 /* backup don't need parament. */
181 return ERR_OK;
182 }
183
OnConnect(const AAFwk::Want & want)184 sptr<IRemoteObject> ExtBackup::OnConnect(const AAFwk::Want &want)
185 {
186 try {
187 HILOGI("begin connect");
188 BExcepUltils::BAssert(abilityInfo_, BError::Codes::EXT_BROKEN_FRAMEWORK, "Invalid abilityInfo_");
189 // 发起者必须是备份服务
190 auto extAction = VerifyAndGetAction(want, abilityInfo_);
191 if (extAction_ != BConstants::ExtensionAction::INVALID && extAction == BConstants::ExtensionAction::INVALID &&
192 extAction_ != extAction) {
193 HILOGE("Verification action failed.");
194 return nullptr;
195 }
196 extAction_ = extAction;
197 GetParament(want);
198 // 应用必须配置支持备份恢复
199 if (!AllowToBackupRestore()) {
200 HILOGE("The application does not allow to backup and restore.");
201 return nullptr;
202 }
203
204 Extension::OnConnect(want);
205
206 auto remoteObject =
207 sptr<BackupExtExtension>(new BackupExtExtension(std::static_pointer_cast<ExtBackup>(shared_from_this()),
208 want.GetBundle()));
209 return remoteObject->AsObject();
210 } catch (const BError &e) {
211 return nullptr;
212 } catch (const exception &e) {
213 HILOGE("%{public}s", e.what());
214 return nullptr;
215 } catch (...) {
216 HILOGE("");
217 return nullptr;
218 }
219 }
220
OnDisconnect(const AAFwk::Want & want)221 void ExtBackup::OnDisconnect(const AAFwk::Want &want)
222 {
223 try {
224 HILOGI("begin disconnect");
225 sptr<BackupExtExtension> extExtension = bakExtExtension_.promote();
226 if (extExtension != nullptr) {
227 extExtension->ExtClear();
228 }
229 Extension::OnDisconnect(want);
230 extAction_ = BConstants::ExtensionAction::INVALID;
231 HILOGI("end");
232 } catch (const BError &e) {
233 return;
234 } catch (const exception &e) {
235 HILOGE("%{public}s", e.what());
236 return;
237 } catch (...) {
238 HILOGE("");
239 return;
240 }
241 }
242
WasFromSpecialVersion(void)243 bool ExtBackup::WasFromSpecialVersion(void)
244 {
245 if (appVersionStr_.empty()) {
246 HILOGE("App version name is empty");
247 return false;
248 }
249 std::string appVersionFlag_ =
250 appVersionStr_.substr(0, appVersionStr_.find_first_of(BConstants::VERSION_NAME_SEPARATOR_CHAR));
251 if (appVersionFlag_ == BConstants::DEFAULT_VERSION_NAME) {
252 return true;
253 }
254 return false;
255 }
256
SpecialVersionForCloneAndCloud(void)257 bool ExtBackup::SpecialVersionForCloneAndCloud(void)
258 {
259 if (appVersionStr_.empty()) {
260 HILOGE("App version name is empty");
261 return false;
262 }
263 std::string appVersionFlag_ =
264 appVersionStr_.substr(0, appVersionStr_.find_first_of(BConstants::VERSION_NAME_SEPARATOR_CHAR));
265 auto iter =
266 find_if(BConstants::DEFAULT_VERSION_NAMES_VEC.begin(), BConstants::DEFAULT_VERSION_NAMES_VEC.end(),
267 [appVersionFlag {appVersionFlag_}](const auto &versionName) { return versionName == appVersionFlag; });
268 if (iter != BConstants::DEFAULT_VERSION_NAMES_VEC.end()) {
269 return true;
270 }
271 return false;
272 }
273
RestoreDataReady()274 bool ExtBackup::RestoreDataReady()
275 {
276 return restoreType_ == RestoreTypeEnum::RESTORE_DATA_READDY;
277 }
278
OnBackup(function<void (ErrCode,std::string)> callback)279 ErrCode ExtBackup::OnBackup(function<void(ErrCode, std::string)> callback)
280 {
281 HILOGI("BackupExtensionAbility(base) OnBackup.");
282 return ERR_OK;
283 }
284
OnBackup(std::function<void (ErrCode,std::string)> callback,std::function<void (ErrCode,const std::string)> callbackEx)285 ErrCode ExtBackup::OnBackup(std::function<void(ErrCode, std::string)> callback,
286 std::function<void(ErrCode, const std::string)> callbackEx)
287 {
288 HILOGI("BackupExtensionAbility(base) OnBackup with Ex");
289 return ERR_OK;
290 }
291
OnRestore(function<void (ErrCode,std::string)> callback,std::function<void (ErrCode,const std::string)> callbackEx)292 ErrCode ExtBackup::OnRestore(function<void(ErrCode, std::string)> callback,
293 std::function<void(ErrCode, const std::string)> callbackEx)
294 {
295 HILOGI("BackupExtensionAbility(base) OnRestore with Ex.");
296 return ERR_OK;
297 }
298
OnRestore(function<void (ErrCode,std::string)> callback)299 ErrCode ExtBackup::OnRestore(function<void(ErrCode, std::string)> callback)
300 {
301 HILOGI("BackupExtensionAbility(base) OnRestore.");
302 return ERR_OK;
303 }
304
GetBackupInfo(function<void (ErrCode,std::string)> callback)305 ErrCode ExtBackup::GetBackupInfo(function<void(ErrCode, std::string)> callback)
306 {
307 HILOGI("BackupExtensionAbility(base) GetBackupInfo.");
308 return ERR_OK;
309 }
310
InvokeAppExtMethod(ErrCode errCode,const std::string result)311 ErrCode ExtBackup::InvokeAppExtMethod(ErrCode errCode, const std::string result)
312 {
313 HILOGI("BackupExtensionAbility(base) InvokeAppExtMethod.");
314 return ERR_OK;
315 }
316
OnProcess(std::function<void (ErrCode,std::string)> callback)317 ErrCode ExtBackup::OnProcess(std::function<void(ErrCode, std::string)> callback)
318 {
319 HILOGI("BackupExtensionAbility(base) OnProcess.");
320 return ERR_OK;
321 }
322
OnRelease(std::function<void (ErrCode,std::string)> callback,int32_t scenario)323 ErrCode ExtBackup::OnRelease(std::function<void(ErrCode, std::string)> callback, int32_t scenario)
324 {
325 HILOGI("BackupExtensionAbility(base) OnRelease.");
326 return ERR_OK;
327 }
328
GetBackupCompatibilityInfo(std::function<void (ErrCode,const std::string)> callbackEx,std::string extInfo)329 ErrCode ExtBackup::GetBackupCompatibilityInfo(std::function<void(ErrCode, const std::string)> callbackEx,
330 std::string extInfo)
331 {
332 HILOGI("BackupExtensionAbility(base) GetBackupCompatibilityInfo.");
333 return ERR_OK;
334 }
335
GetRestoreCompatibilityInfo(std::function<void (ErrCode,const std::string)> callbackEx,std::string extInfo)336 ErrCode ExtBackup::GetRestoreCompatibilityInfo(std::function<void(ErrCode, const std::string)> callbackEx,
337 std::string extInfo)
338 {
339 HILOGI("BackupExtensionAbility(base) GetRestoreCompatibilityInfo.");
340 return ERR_OK;
341 }
342 } // namespace OHOS::FileManagement::Backup
343