• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ext_backup.h"
17 
18 #include <cstdio>
19 #include <sstream>
20 
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 
24 #include "bundle_mgr_client.h"
25 #include "unique_fd.h"
26 
27 #include "b_error/b_error.h"
28 #include "b_error/b_excep_utils.h"
29 #include "b_json/b_json_cached_entity.h"
30 #include "b_json/b_json_entity_extension_config.h"
31 #include "b_resources/b_constants.h"
32 #include "ext_backup_js.h"
33 #include "ext_extension.h"
34 #include "filemgmt_libhilog.h"
35 #include "i_service.h"
36 
37 namespace OHOS::FileManagement::Backup {
38 using namespace std;
39 
40 CreatorFunc ExtBackup::creator_ = nullptr;
SetCreator(const CreatorFunc & creator)41 void ExtBackup::SetCreator(const CreatorFunc &creator)
42 {
43     creator_ = creator;
44 }
45 
Init(const shared_ptr<AbilityRuntime::AbilityLocalRecord> & record,const shared_ptr<AbilityRuntime::OHOSApplication> & application,shared_ptr<AbilityRuntime::AbilityHandler> & handler,const sptr<IRemoteObject> & token)46 void ExtBackup::Init(const shared_ptr<AbilityRuntime::AbilityLocalRecord> &record,
47                      const shared_ptr<AbilityRuntime::OHOSApplication> &application,
48                      shared_ptr<AbilityRuntime::AbilityHandler> &handler,
49                      const sptr<IRemoteObject> &token)
50 {
51     HILOGI("Init the BackupExtensionAbility(Base)");
52     AbilityRuntime::ExtensionBase<ExtBackupContext>::Init(record, application, handler, token);
53 }
54 
Create(const unique_ptr<AbilityRuntime::Runtime> & runtime)55 ExtBackup *ExtBackup::Create(const unique_ptr<AbilityRuntime::Runtime> &runtime)
56 {
57     if (!runtime) {
58         HILOGD("Create as BackupExtensionAbility(base)");
59         return new ExtBackup();
60     }
61 
62     if (creator_) {
63         HILOGD("Create as BackupExtensionAbility(creater)");
64         return creator_(runtime);
65     }
66 
67     switch (runtime->GetLanguage()) {
68         case AbilityRuntime::Runtime::Language::JS:
69             HILOGD("Create as BackupExtensionAbility(JS)");
70             return ExtBackupJs::Create(runtime);
71 
72         default:
73             HILOGD("Create as BackupExtensionAbility(base)");
74             return new ExtBackup();
75     }
76 }
77 
OnStart(const AAFwk::Want & want)78 void ExtBackup::OnStart(const AAFwk::Want &want)
79 {
80     HILOGI("BackupExtensionAbility was started");
81     Extension::OnStart(want);
82 }
83 
OnCommand(const AAFwk::Want & want,bool restart,int startId)84 void ExtBackup::OnCommand(const AAFwk::Want &want, bool restart, int startId)
85 {
86     HILOGI("BackupExtensionAbility was invoked. restart=%{public}d, startId=%{public}d", restart, startId);
87 
88     // REM: 处理返回结果 ret
89     // REM: 通过杀死进程实现 Stop
90 }
91 
GetUsrConfig() const92 string ExtBackup::GetUsrConfig() const
93 {
94     vector<string> config;
95     AppExecFwk::BundleMgrClient client;
96     BExcepUltils::BAssert(abilityInfo_, BError::Codes::EXT_BROKEN_FRAMEWORK, "Invalid abilityInfo_");
97     const AppExecFwk::AbilityInfo &info = *abilityInfo_;
98     if (!client.GetProfileFromAbility(info, "ohos.extension.backup", config)) {
99         throw BError(BError::Codes::EXT_INVAL_ARG, "Failed to invoke the GetProfileFromAbility method.");
100     }
101 
102     return config.empty() ? "" : config[0];
103 }
104 
AllowToBackupRestore() const105 bool ExtBackup::AllowToBackupRestore() const
106 {
107     string usrConfig = GetUsrConfig();
108     BJsonCachedEntity<BJsonEntityExtensionConfig> cachedEntity(usrConfig);
109     auto cache = cachedEntity.Structuralize();
110     if (cache.GetAllowToBackupRestore()) {
111         return true;
112     }
113     return false;
114 }
115 
GetExtensionAction() const116 BConstants::ExtensionAction ExtBackup::GetExtensionAction() const
117 {
118     return extAction_;
119 }
120 
VerifyAndGetAction(const AAFwk::Want & want,std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo)121 BConstants::ExtensionAction ExtBackup::VerifyAndGetAction(const AAFwk::Want &want,
122                                                           std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo)
123 {
124     string pendingMsg = "Received an empty ability. You must missed the init proc";
125     BExcepUltils::BAssert(abilityInfo, BError::Codes::EXT_INVAL_ARG, pendingMsg);
126     using namespace BConstants;
127     ExtensionAction extAction {want.GetIntParam(EXTENSION_ACTION_PARA, static_cast<int>(ExtensionAction::INVALID))};
128     if (extAction == ExtensionAction::INVALID) {
129         int extActionInt = static_cast<int>(extAction);
130         pendingMsg = string("Want must specify a valid action instead of ").append(to_string(extActionInt));
131         throw BError(BError::Codes::EXT_INVAL_ARG, pendingMsg);
132     }
133     return extAction;
134 }
135 
GetParament(const AAFwk::Want & want)136 ErrCode ExtBackup::GetParament(const AAFwk::Want &want)
137 {
138     if (extAction_ == BConstants::ExtensionAction::RESTORE) {
139         appVersionStr_ = want.GetStringParam(BConstants::EXTENSION_VERSION_NAME_PARA);
140         appVersionCode_ = want.GetIntParam(BConstants::EXTENSION_VERSION_CODE_PARA, 0);
141         restoreType_ = want.GetIntParam(BConstants::EXTENSION_RESTORE_TYPE_PARA, 0);
142 
143         HILOGI("Get version %{public}s type %{public}d from want when restore.", appVersionStr_.c_str(), restoreType_);
144     }
145     /* backup don't need parament. */
146     return ERR_OK;
147 }
148 
OnConnect(const AAFwk::Want & want)149 sptr<IRemoteObject> ExtBackup::OnConnect(const AAFwk::Want &want)
150 {
151     try {
152         HILOGI("begin connect");
153         BExcepUltils::BAssert(abilityInfo_, BError::Codes::EXT_BROKEN_FRAMEWORK, "Invalid abilityInfo_");
154         // 发起者必须是备份服务
155         auto extAction = VerifyAndGetAction(want, abilityInfo_);
156         if (extAction_ != BConstants::ExtensionAction::INVALID && extAction == BConstants::ExtensionAction::INVALID &&
157             extAction_ != extAction) {
158             HILOGE("Verification action failed.");
159             return nullptr;
160         }
161         // 应用必须配置支持备份恢复
162         if (!AllowToBackupRestore()) {
163             HILOGE("The application does not allow to backup and restore.");
164             return nullptr;
165         }
166         extAction_ = extAction;
167         GetParament(want);
168 
169         Extension::OnConnect(want);
170 
171         auto remoteObject =
172             sptr<BackupExtExtension>(new BackupExtExtension(std::static_pointer_cast<ExtBackup>(shared_from_this())));
173 
174         // async do restore.
175         if (extAction_ == BConstants::ExtensionAction::RESTORE &&
176                 restoreType_ == RestoreTypeEnum::RESTORE_DATA_READDY &&
177                 WasFromSpeicalVersion()) {
178             HILOGI("Restore directly when upgrading.");
179             remoteObject->AsyncTaskRestoreForUpgrade();
180         }
181 
182         return remoteObject->AsObject();
183     } catch (const BError &e) {
184         return nullptr;
185     } catch (const exception &e) {
186         HILOGE("%{public}s", e.what());
187         return nullptr;
188     } catch (...) {
189         HILOGE("");
190         return nullptr;
191     }
192 }
193 
OnDisconnect(const AAFwk::Want & want)194 void ExtBackup::OnDisconnect(const AAFwk::Want &want)
195 {
196     try {
197         HILOGI("begin disconnect");
198         Extension::OnDisconnect(want);
199         extAction_ = BConstants::ExtensionAction::INVALID;
200         HILOGI("end");
201     } catch (const BError &e) {
202         return;
203     } catch (const exception &e) {
204         HILOGE("%{public}s", e.what());
205         return;
206     } catch (...) {
207         HILOGE("");
208         return;
209     }
210 }
211 
WasFromSpeicalVersion(void)212 bool ExtBackup::WasFromSpeicalVersion(void)
213 {
214     if (appVersionCode_ == 0 && appVersionStr_ == "0.0.0.0") {
215         return true;
216     }
217     return false;
218 }
219 
OnBackup(void)220 ErrCode ExtBackup::OnBackup(void)
221 {
222     HILOGI("BackupExtensionAbility(base) OnBackup.");
223     return ERR_OK;
224 }
225 
OnRestore(void)226 ErrCode ExtBackup::OnRestore(void)
227 {
228     HILOGI("BackupExtensionAbility(base) OnRestore.");
229     return ERR_OK;
230 }
231 
232 } // namespace OHOS::FileManagement::Backup
233