From b6124c1b016764b58869026483ab33bf71eabb7e Mon Sep 17 00:00:00 2001 From: fengjituo111 Date: Tue, 12 Nov 2024 11:30:04 +0000 Subject: [PATCH] =?UTF-8?q?=E3=80=90mediaQuery=E3=80=91=E3=80=90=E8=A1=A5?= =?UTF-8?q?=E5=85=85=E3=80=91=E5=A4=9A=E5=AE=9E=E4=BE=8B=E5=9C=BA=E6=99=AF?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0ContainerScope=E4=BF=9D=E6=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fengjituo111 Change-Id: Ie17c2dde63c352c211a39431d7a362f5f077ea5e --- .../napi/kits/mediaquery/js_media_query.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/interfaces/napi/kits/mediaquery/js_media_query.cpp b/interfaces/napi/kits/mediaquery/js_media_query.cpp index 664f19efd42..df61d8c391b 100644 --- a/interfaces/napi/kits/mediaquery/js_media_query.cpp +++ b/interfaces/napi/kits/mediaquery/js_media_query.cpp @@ -25,6 +25,7 @@ namespace OHOS::Ace::Napi { namespace { constexpr size_t STR_BUFFER_SIZE = 1024; constexpr int32_t TWO_ARGS = 2; +constexpr int32_t DEFAULT_INSTANCE_ID = -1; } using namespace OHOS::Ace::Framework; @@ -168,7 +169,7 @@ public: napi_close_handle_scope(env, scope); return nullptr; } - auto jsEngine = EngineHelper::GetEngine(listener->GetInstanceId()); + auto jsEngine = listener->GetJsEngine(); if (!jsEngine) { return nullptr; } @@ -308,7 +309,7 @@ private: env_ = env; } napi_close_handle_scope(env, scope); - auto jsEngine = EngineHelper::GetCurrentEngineSafely(); + auto jsEngine = GetJsEngine(); if (!jsEngine) { return; } @@ -327,6 +328,16 @@ private: return listener; } + RefPtr GetJsEngine() + { + if (GetInstanceId() == DEFAULT_INSTANCE_ID) { + TAG_LOGW(AceLogTag::ACE_MEDIA_QUERY, "matchMediaSync executes in non-UI context"); + return EngineHelper::GetCurrentEngineSafely(); + } else { + return EngineHelper::GetEngine(GetInstanceId()); + } + } + static size_t ParseArgs(napi_env& env, napi_callback_info& info, napi_value& thisVar, napi_value& cb) { const size_t argNum = 2; @@ -357,7 +368,7 @@ private: napi_env env_ = nullptr; std::list cbList_; - int32_t instanceId_; + int32_t instanceId_ = DEFAULT_INSTANCE_ID; static std::set>* delayDeleteListenerSets_; static napi_env delayDeleteEnv_; static std::set* delayDeleteCallbacks_;