diff --git a/packages/webview_flutter_lwe/CHANGELOG.md b/packages/webview_flutter_lwe/CHANGELOG.md index 9c852f592..18315103a 100644 --- a/packages/webview_flutter_lwe/CHANGELOG.md +++ b/packages/webview_flutter_lwe/CHANGELOG.md @@ -1,3 +1,7 @@ +## NEXT + +* Fix an issue where platform channel isn't called on the main thread. + ## 0.3.4 * Update lightweight web engine(1.3.3). diff --git a/packages/webview_flutter_lwe/tizen/src/message_dispatcher.cc b/packages/webview_flutter_lwe/tizen/src/message_dispatcher.cc new file mode 100644 index 000000000..2d29d05b7 --- /dev/null +++ b/packages/webview_flutter_lwe/tizen/src/message_dispatcher.cc @@ -0,0 +1,20 @@ +// Copyright 2025 Samsung Electronics Co., Ltd. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "message_dispatcher.h" + +#include + +MessageDispatcher::MessageDispatcher() { ecore_init(); } +MessageDispatcher::~MessageDispatcher() { ecore_shutdown(); } + +void MessageDispatcher::dispatchTaskOnMainThread(std::function&& fn) { + ecore_main_loop_thread_safe_call_sync( + [](void* data) -> void* { + auto fn = static_cast*>(data); + if (fn) (*fn)(); + return nullptr; + }, + &fn); +} diff --git a/packages/webview_flutter_lwe/tizen/src/message_dispatcher.h b/packages/webview_flutter_lwe/tizen/src/message_dispatcher.h new file mode 100644 index 000000000..1e9980a01 --- /dev/null +++ b/packages/webview_flutter_lwe/tizen/src/message_dispatcher.h @@ -0,0 +1,18 @@ +// Copyright 2025 Samsung Electronics Co., Ltd. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_PLUGIN_MESSAGE_DISPATCHER_H_ +#define FLUTTER_PLUGIN_MESSAGE_DISPATCHER_H_ + +#include + +class MessageDispatcher { + public: + MessageDispatcher(); + ~MessageDispatcher(); + + void dispatchTaskOnMainThread(std::function&& fn); +}; + +#endif // FLUTTER_PLUGIN_MESSAGE_DISPATCHER_H_ diff --git a/packages/webview_flutter_lwe/tizen/src/webview.cc b/packages/webview_flutter_lwe/tizen/src/webview.cc index 7b9e7b3f7..118f82bbd 100644 --- a/packages/webview_flutter_lwe/tizen/src/webview.cc +++ b/packages/webview_flutter_lwe/tizen/src/webview.cc @@ -125,6 +125,8 @@ WebView::WebView(flutter::PluginRegistrar* registrar, int view_id, InitWebView(); + dispatcher_ = std::make_unique(); + webview_channel_ = std::make_unique( GetPluginRegistrar()->messenger(), GetWebViewChannelName(), &flutter::StandardMethodCodec::GetInstance()); @@ -150,22 +152,32 @@ WebView::WebView(flutter::PluginRegistrar* registrar, int view_id, [this](LWE::WebContainer* container, const std::string& url) { flutter::EncodableMap args = { {flutter::EncodableValue("url"), flutter::EncodableValue(url)}}; - navigation_delegate_channel_->InvokeMethod( - "onPageStarted", std::make_unique(args)); + + dispatcher_->dispatchTaskOnMainThread([this, &args]() { + navigation_delegate_channel_->InvokeMethod( + "onPageStarted", std::make_unique(args)); + }); }); webview_instance_->RegisterOnPageLoadedHandler( [this](LWE::WebContainer* container, const std::string& url) { flutter::EncodableMap args = { {flutter::EncodableValue("url"), flutter::EncodableValue(url)}}; - navigation_delegate_channel_->InvokeMethod( - "onPageFinished", std::make_unique(args)); + + dispatcher_->dispatchTaskOnMainThread([this, &args]() { + navigation_delegate_channel_->InvokeMethod( + "onPageFinished", + std::make_unique(args)); + }); }); webview_instance_->RegisterOnProgressChangedHandler( [this](LWE::WebContainer* container, int progress) { flutter::EncodableMap args = {{flutter::EncodableValue("progress"), flutter::EncodableValue(progress)}}; - navigation_delegate_channel_->InvokeMethod( - "onProgress", std::make_unique(args)); + + dispatcher_->dispatchTaskOnMainThread([this, &args]() { + navigation_delegate_channel_->InvokeMethod( + "onProgress", std::make_unique(args)); + }); }); webview_instance_->RegisterOnReceivedErrorHandler( [this](LWE::WebContainer* container, LWE::ResourceError error) { @@ -177,9 +189,12 @@ WebView::WebView(flutter::PluginRegistrar* registrar, int view_id, {flutter::EncodableValue("failingUrl"), flutter::EncodableValue(error.GetUrl())}, }; - navigation_delegate_channel_->InvokeMethod( - "onWebResourceError", - std::make_unique(args)); + + dispatcher_->dispatchTaskOnMainThread([this, &args]() { + navigation_delegate_channel_->InvokeMethod( + "onWebResourceError", + std::make_unique(args)); + }); }); webview_instance_->RegisterShouldOverrideUrlLoadingHandler( [this](LWE::WebContainer* view, const std::string& url) -> bool { @@ -191,10 +206,14 @@ WebView::WebView(flutter::PluginRegistrar* registrar, int view_id, {flutter::EncodableValue("isForMainFrame"), flutter::EncodableValue(true)}, }; - auto result = std::make_unique(url, this); - navigation_delegate_channel_->InvokeMethod( - "navigationRequest", - std::make_unique(args), std::move(result)); + + dispatcher_->dispatchTaskOnMainThread([this, &args, url]() { + auto result = std::make_unique(url, this); + navigation_delegate_channel_->InvokeMethod( + "navigationRequest", + std::make_unique(args), + std::move(result)); + }); return true; }); } @@ -212,9 +231,11 @@ void WebView::RegisterJavaScriptChannelName(const std::string& name) { {flutter::EncodableValue("channel"), flutter::EncodableValue(name)}, {flutter::EncodableValue("message"), flutter::EncodableValue(message)}, }; - webview_channel_->InvokeMethod( - "javaScriptChannelMessage", - std::make_unique(args)); + dispatcher_->dispatchTaskOnMainThread([this, &args]() { + webview_channel_->InvokeMethod( + "javaScriptChannelMessage", + std::make_unique(args)); + }); return "success"; }; webview_instance_->AddJavaScriptInterface(name, "postMessage", on_message); @@ -622,7 +643,9 @@ void WebView::HandleWebViewMethodCall(const FlMethodCall& method_call, delete result; } }; + webview_instance_->EvaluateJavaScript(*javascript, on_result); + } else { result->Error("Invalid argument", "The argument must be a string."); } diff --git a/packages/webview_flutter_lwe/tizen/src/webview.h b/packages/webview_flutter_lwe/tizen/src/webview.h index 19cbe703e..0f2064c87 100644 --- a/packages/webview_flutter_lwe/tizen/src/webview.h +++ b/packages/webview_flutter_lwe/tizen/src/webview.h @@ -17,6 +17,8 @@ #include #include +#include "message_dispatcher.h" + typedef flutter::MethodCall FlMethodCall; typedef flutter::MethodResult FlMethodResult; typedef flutter::MethodChannel FlMethodChannel; @@ -74,6 +76,7 @@ class WebView : public PlatformView { BufferUnit* rendered_surface_ = nullptr; bool is_mouse_lbutton_down_ = false; bool has_navigation_delegate_ = false; + std::unique_ptr dispatcher_; std::unique_ptr webview_channel_; std::unique_ptr navigation_delegate_channel_; std::unique_ptr texture_variant_;