diff --git a/frameworks/native/backup_ext/include/ext_backup_js.h b/frameworks/native/backup_ext/include/ext_backup_js.h index 2d41756ce25f6bfc985006080f3916a92d2b8f29..37bdc9c1f26444c39f3692b93acf2c39b861ad58 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 cf34fa574bac9894566aa81f87ea27e614e545b8..4b5e57a3315e5207bf659f0b6e46407585c32cd0 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) {