From 51411df9740e3039726e4bc7858f38998cd7d4da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=A1=A2?= Date: Sat, 19 Aug 2023 18:04:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D=E6=A1=86?= =?UTF-8?q?=E6=9E=B6=EF=BC=8C=E5=AF=BC=E5=87=BAcontext=E4=B8=8A=E4=B8=8B?= =?UTF-8?q?=E6=96=87=E3=80=82=E6=94=AF=E6=8C=81=E5=85=AC=E5=85=B1=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王桢 --- .../native/backup_ext/include/ext_backup_js.h | 4 +- .../native/backup_ext/src/ext_backup_js.cpp | 45 +++++++++++++++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/frameworks/native/backup_ext/include/ext_backup_js.h b/frameworks/native/backup_ext/include/ext_backup_js.h index 2d41756ce..37bdc9c1f 100644 --- a/frameworks/native/backup_ext/include/ext_backup_js.h +++ b/frameworks/native/backup_ext/include/ext_backup_js.h @@ -102,9 +102,11 @@ private: std::tuple CallObjectMethod(std::string_view name, const std::vector &argv = {}); + void ExportJsContext(void); + AbilityRuntime::JsRuntime &jsRuntime_; std::unique_ptr jsObj_; }; } // namespace OHOS::FileManagement::Backup -#endif // OHOS_FILEMGMT_BACKUP_EXT_BACKUP_JS_H \ No newline at end of file +#endif // OHOS_FILEMGMT_BACKUP_EXT_BACKUP_JS_H diff --git a/frameworks/native/backup_ext/src/ext_backup_js.cpp b/frameworks/native/backup_ext/src/ext_backup_js.cpp index cf34fa574..4b5e57a33 100644 --- a/frameworks/native/backup_ext/src/ext_backup_js.cpp +++ b/frameworks/native/backup_ext/src/ext_backup_js.cpp @@ -24,6 +24,7 @@ #include "bundle_mgr_client.h" #include "js_runtime.h" #include "js_runtime_utils.h" +#include "js_extension_context.h" #include "napi/native_api.h" #include "napi/native_node_api.h" #include "napi_common_util.h" @@ -77,11 +78,12 @@ void ExtBackupJs::Init(const shared_ptr &record, AbilityRuntime::HandleScope handleScope(jsRuntime_); jsObj_ = jsRuntime_.LoadModule(moduleName, modulePath, info.hapPath, abilityInfo_->compileMode == AbilityRuntime::CompileMode::ES_MODULE); - if (jsObj_) { - HILOGI("Wow! Here's a custsom BackupExtensionAbility"); - } else { + if (jsObj_ == nullptr) { HILOGW("Oops! There's no custom BackupExtensionAbility"); + return; } + HILOGI("Wow! Here's a custsom BackupExtensionAbility"); + ExportJsContext(); } catch (const BError &e) { HILOGE("%{public}s", e.what()); } catch (const exception &e) { @@ -89,6 +91,43 @@ void ExtBackupJs::Init(const shared_ptr &record, } } +void ExtBackupJs::ExportJsContext(void) +{ + auto& engine = jsRuntime_.GetNativeEngine(); + NativeObject* obj = AbilityRuntime::ConvertNativeValueTo(jsObj_->Get()); + if (obj == nullptr) { + HILOGE("Failed to get BackupExtAbility object"); + return; + } + + auto context = GetContext(); + if (context == nullptr) { + HILOGE("Failed to get context"); + return; + } + + HILOGI("CreateBackupExtAbilityContext"); + NativeValue* contextObj = CreateJsExtensionContext(engine, context); + auto contextRef = jsRuntime_.LoadSystemModule("application.ExtensionContext", &contextObj, 1); + contextObj = contextRef->Get(); + HILOGI("Bind context"); + context->Bind(jsRuntime_, contextRef.release()); + obj->SetProperty("context", contextObj); + + auto nativeObj = AbilityRuntime::ConvertNativeValueTo(contextObj); + if (nativeObj == nullptr) { + HILOGE("Failed to get backup extension ability native object"); + return; + } + + HILOGI("Set backup extension ability context pointer is nullptr: %{public}d", context.get() == nullptr); + auto releaseContext = [](NativeEngine*, void* data, void*) { + HILOGI("Finalizer for weak_ptr backup extension ability context is called"); + delete static_cast*>(data); + }; + nativeObj->SetNativePointer(new std::weak_ptr(context), releaseContext, nullptr); +} + [[maybe_unused]] tuple ExtBackupJs::CallObjectMethod(string_view name, const vector &argv) { -- Gitee