From 3708f768cd784c2678cfc5b71c881b175446dce7 Mon Sep 17 00:00:00 2001 From: huaqingsimeng Date: Tue, 19 Sep 2023 12:07:24 +0800 Subject: [PATCH] =?UTF-8?q?backupextension=E4=B8=BAcontext=E6=8F=90?= =?UTF-8?q?=E4=BE=9B=E5=BA=8F=E5=88=97=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huaqingsimeng --- .../native/backup_ext/src/ext_backup_js.cpp | 49 ++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/frameworks/native/backup_ext/src/ext_backup_js.cpp b/frameworks/native/backup_ext/src/ext_backup_js.cpp index d2e58a09a..afdb53d0b 100644 --- a/frameworks/native/backup_ext/src/ext_backup_js.cpp +++ b/frameworks/native/backup_ext/src/ext_backup_js.cpp @@ -22,6 +22,7 @@ #include #include "bundle_mgr_client.h" +#include "ext_backup_context.h" #include "js_extension_context.h" #include "js_runtime.h" #include "js_runtime_utils.h" @@ -142,6 +143,47 @@ void ExtBackupJs::Init(const shared_ptr &record, } } +NativeValue *AttachBackupExtensionContext(NativeEngine *engine, void *value, void *) +{ + HILOGI("AttachBackupExtensionContext"); + if (value == nullptr) { + HILOGE("invalid parameter."); + return nullptr; + } + auto ptr = reinterpret_cast *>(value)->lock(); + if (ptr == nullptr) { + HILOGE("invalid context."); + return nullptr; + } + NativeValue *object = CreateJsExtensionContext(*engine, ptr); + if (object == nullptr) { + HILOGE("Failed to get js backup extension context"); + return nullptr; + } + auto contextObj = + AbilityRuntime::JsRuntime::LoadSystemModuleByEngine(engine, "application.ExtensionContext", &object, 1)->Get(); + NativeObject *nObject = AbilityRuntime::ConvertNativeValueTo(contextObj); + if (nObject == nullptr) { + HILOGE("Failed to get context native object"); + return nullptr; + } + nObject->ConvertToNativeBindingObject(engine, AbilityRuntime::DetachCallbackFunc, AttachBackupExtensionContext, + value, nullptr); + auto workContext = new (std::nothrow) std::weak_ptr(ptr); + if (workContext == nullptr) { + HILOGE("Failed to get backup extension context"); + return nullptr; + } + nObject->SetNativePointer( + workContext, + [](NativeEngine *, void *data, void *) { + HILOGE("Finalizer for weak_ptr backup extension context is called"); + delete static_cast *>(data); + }, + nullptr); + return contextObj; +} + void ExtBackupJs::ExportJsContext(void) { auto &engine = jsRuntime_.GetNativeEngine(); @@ -170,13 +212,16 @@ void ExtBackupJs::ExportJsContext(void) HILOGE("Failed to get backup extension ability native object"); return; } + auto workContext = new (std::nothrow) std::weak_ptr(context); + nativeObj->ConvertToNativeBindingObject(&engine, AbilityRuntime::DetachCallbackFunc, AttachBackupExtensionContext, + workContext, nullptr); 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); + delete static_cast *>(data); }; - nativeObj->SetNativePointer(new std::weak_ptr(context), releaseContext, nullptr); + nativeObj->SetNativePointer(workContext, releaseContext, nullptr); } [[maybe_unused]] tuple ExtBackupJs::CallObjectMethod(string_view name, -- Gitee