• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_ani.h"
17 
18 #include "ani_utils.h"
19 #include "b_error/b_error.h"
20 #include "b_error/b_excep_utils.h"
21 #include "filemgmt_libhilog.h"
22 
23 namespace OHOS::FileManagement::Backup {
24 using namespace std;
25 
ExtBackupAni(AbilityRuntime::Runtime & runtime)26 ExtBackupAni::ExtBackupAni(AbilityRuntime::Runtime &runtime)
27     : stsRuntime_(static_cast<AbilityRuntime::ETSRuntime &>(runtime))
28 {
29     HILOGI("Create as an BackupExtensionAbility");
30 }
31 
~ExtBackupAni()32 ExtBackupAni::~ExtBackupAni()
33 {
34     HILOGI("ExtBackupAni::~ExtBackupAni.");
35 }
36 
Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> & record,const std::shared_ptr<AppExecFwk::OHOSApplication> & application,std::shared_ptr<AppExecFwk::AbilityHandler> & handler,const sptr<IRemoteObject> & token)37 void ExtBackupAni::Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
38                         const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
39                         std::shared_ptr<AppExecFwk::AbilityHandler> &handler,
40                         const sptr<IRemoteObject> &token)
41 {
42     HILOGI("Init the BackupExtensionAbility");
43     try {
44         if (record == nullptr) {
45             HILOGE("record null");
46             return;
47         }
48         Extension::Init(record, application, handler, token);
49         if (Extension::abilityInfo_ == nullptr || Extension::abilityInfo_->srcEntrance.empty()) {
50             HILOGE("BackupExtensionAbility Init abilityInfo error");
51             return;
52         }
53         std::string srcPath(Extension::abilityInfo_->moduleName + "/");
54         srcPath.append(Extension::abilityInfo_->srcEntrance);
55         auto pos = srcPath.rfind(".");
56         if (pos != std::string::npos) {
57             srcPath.erase(pos);
58             srcPath.append(".abc");
59         }
60         std::string moduleName(Extension::abilityInfo_->moduleName);
61         moduleName.append("::").append(abilityInfo_->name);
62         etsObj_ = stsRuntime_.LoadModule(moduleName, srcPath, abilityInfo_->hapPath,
63                                          abilityInfo_->compileMode == AppExecFwk::CompileMode::ES_MODULE, false,
64                                          abilityInfo_->srcEntrance);
65         if (etsObj_ == nullptr) {
66             HILOGE("Failed to get etsObj");
67             return;
68         }
69     } catch (const BError &e) {
70         HILOGE("%{public}s", e.what());
71     } catch (const exception &e) {
72         HILOGE("%{public}s", e.what());
73     }
74 }
75 
CallEtsOnBackup()76 ErrCode ExtBackupAni::CallEtsOnBackup()
77 {
78     ani_vm *vm = nullptr;
79     auto aniEnv = stsRuntime_.GetAniEnv();
80     if (aniEnv == nullptr) {
81         HILOGE("aniEnv null");
82         return EINVAL;
83     }
84     if (ANI_OK != aniEnv->GetVM(&vm)) {
85         return EINVAL;
86     }
87     ani_env *env = nullptr;
88     ani_options aniArgs {0, nullptr};
89     if (ANI_OK != vm->AttachCurrentThread(&aniArgs, ANI_VERSION_1, &env)) {
90         if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) {
91             return EINVAL;
92         }
93     }
94     if (env == nullptr) {
95         HILOGE("Failed to get environment");
96         vm->DetachCurrentThread();
97         return EINVAL;
98     }
99     if (ANI_OK != env->Object_CallMethodByName_Void(etsObj_->aniObj, "onBackup", nullptr)) {
100         HILOGE("Failed to call the method: onBackup");
101         vm->DetachCurrentThread();
102         return EINVAL;
103     }
104     vm->DetachCurrentThread();
105     return ERR_OK;
106 }
107 
CallEtsOnRestore()108 ErrCode ExtBackupAni::CallEtsOnRestore()
109 {
110     ani_vm *vm = nullptr;
111     auto aniEnv = stsRuntime_.GetAniEnv();
112     if (aniEnv == nullptr) {
113         HILOGE("aniEnv null");
114         return EINVAL;
115     }
116     if (ANI_OK != aniEnv->GetVM(&vm)) {
117         return EINVAL;
118     }
119     ani_env *env = nullptr;
120     ani_options aniArgs {0, nullptr};
121     if (ANI_OK != vm->AttachCurrentThread(&aniArgs, ANI_VERSION_1, &env)) {
122         if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) {
123             return EINVAL;
124         }
125     }
126     const std::string className = "L@ohos/application/BackupExtensionAbility/BundleVersionInner;";
127     ani_object bundleVersionObj = AniObjectUtils::Create(env, className.c_str());
128     if (nullptr == bundleVersionObj) {
129         HILOGE("Failed to Create the BundleVersionInner");
130         return EINVAL;
131     }
132     if (ANI_OK != env->Object_CallMethodByName_Void(etsObj_->aniObj, "onRestore", nullptr, bundleVersionObj)) {
133         HILOGE("Failed to call the method: onRestore");
134         return EINVAL;
135     }
136     vm->DetachCurrentThread();
137     return ERR_OK;
138 }
139 
Create(const std::unique_ptr<AbilityRuntime::Runtime> & runtime)140 ExtBackupAni *ExtBackupAni::Create(const std::unique_ptr<AbilityRuntime::Runtime> &runtime)
141 {
142     HILOGI("Create as an BackupExtensionAbility");
143     return new ExtBackupAni(*runtime);
144 }
145 
OnBackup(function<void (ErrCode,std::string)> callback,std::function<void (ErrCode,const std::string)> callbackEx)146 ErrCode ExtBackupAni::OnBackup(function<void(ErrCode, std::string)> callback,
147                                std::function<void(ErrCode, const std::string)> callbackEx)
148 {
149     HILOGI("ExtBackupAni OnBackup");
150     BExcepUltils::BAssert(etsObj_, BError::Codes::EXT_BROKEN_FRAMEWORK,
151                           "The app does not provide the onBackup interface.");
152     return CallEtsOnBackup();
153 }
154 
OnRestore(std::function<void (ErrCode,std::string)> callback,std::function<void (ErrCode,const std::string)> callbackEx)155 ErrCode ExtBackupAni::OnRestore(std::function<void(ErrCode, std::string)> callback,
156                                 std::function<void(ErrCode, const std::string)> callbackEx)
157 {
158     HILOGI("ExtBackupAni OnRestore");
159     BExcepUltils::BAssert(etsObj_, BError::Codes::EXT_BROKEN_FRAMEWORK,
160                           "The app does not provide the OnRestore interface.");
161     return CallEtsOnRestore();
162 }
163 
164 } // namespace OHOS::FileManagement::Backup