Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[webview_flutter_lwe] Fix "channel sent a message from native to Flutter on a non-platform thread" error #790

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/webview_flutter_lwe/tizen/src/message_dispatcher.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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 <Ecore.h>

MessageDispatcher::MessageDispatcher() { ecore_init(); }
MessageDispatcher::~MessageDispatcher() { ecore_shutdown(); }

void MessageDispatcher::dispatchTaskOnMainThread(std::function<void()> fn) {
struct Param {
std::function<void()> fn;
};

Param* p = new Param;
p->fn = fn;

ecore_main_loop_thread_safe_call_sync(
[](void* data) -> void* {
Param* p = (Param*)data;
p->fn();
delete p;
return nullptr;
},
p);
JSUYA marked this conversation as resolved.
Show resolved Hide resolved
}
18 changes: 18 additions & 0 deletions packages/webview_flutter_lwe/tizen/src/message_dispatcher.h
Original file line number Diff line number Diff line change
@@ -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 <functional>

class MessageDispatcher {
public:
MessageDispatcher();
~MessageDispatcher();

void dispatchTaskOnMainThread(std::function<void()> fn);
};

#endif // FLUTTER_PLUGIN_MESSAGE_DISPATCHER_H_
93 changes: 58 additions & 35 deletions packages/webview_flutter_lwe/tizen/src/webview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ WebView::WebView(flutter::PluginRegistrar* registrar, int view_id,

InitWebView();

dispatcher = new MessageDispatcher();

webview_channel_ = std::make_unique<FlMethodChannel>(
GetPluginRegistrar()->messenger(), GetWebViewChannelName(),
&flutter::StandardMethodCodec::GetInstance());
Expand All @@ -148,53 +150,65 @@ WebView::WebView(flutter::PluginRegistrar* registrar, int view_id,

webview_instance_->RegisterOnPageStartedHandler(
[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<flutter::EncodableValue>(args));
dispatcher->dispatchTaskOnMainThread([&]() {
flutter::EncodableMap args = {
{flutter::EncodableValue("url"), flutter::EncodableValue(url)}};
navigation_delegate_channel_->InvokeMethod(
"onPageStarted", std::make_unique<flutter::EncodableValue>(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<flutter::EncodableValue>(args));
dispatcher->dispatchTaskOnMainThread([&]() {
flutter::EncodableMap args = {
{flutter::EncodableValue("url"), flutter::EncodableValue(url)}};
navigation_delegate_channel_->InvokeMethod(
"onPageFinished",
std::make_unique<flutter::EncodableValue>(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<flutter::EncodableValue>(args));
dispatcher->dispatchTaskOnMainThread([&]() {
flutter::EncodableMap args = {{flutter::EncodableValue("progress"),
flutter::EncodableValue(progress)}};
navigation_delegate_channel_->InvokeMethod(
"onProgress", std::make_unique<flutter::EncodableValue>(args));
});
});
webview_instance_->RegisterOnReceivedErrorHandler(
[this](LWE::WebContainer* container, LWE::ResourceError error) {
flutter::EncodableMap args = {
{flutter::EncodableValue("errorCode"),
flutter::EncodableValue(error.GetErrorCode())},
{flutter::EncodableValue("description"),
flutter::EncodableValue(error.GetDescription())},
{flutter::EncodableValue("failingUrl"),
flutter::EncodableValue(error.GetUrl())},
};
navigation_delegate_channel_->InvokeMethod(
"onWebResourceError",
std::make_unique<flutter::EncodableValue>(args));
dispatcher->dispatchTaskOnMainThread([&]() {
flutter::EncodableMap args = {
{flutter::EncodableValue("errorCode"),
flutter::EncodableValue(error.GetErrorCode())},
{flutter::EncodableValue("description"),
flutter::EncodableValue(error.GetDescription())},
{flutter::EncodableValue("failingUrl"),
flutter::EncodableValue(error.GetUrl())},
};
navigation_delegate_channel_->InvokeMethod(
"onWebResourceError",
std::make_unique<flutter::EncodableValue>(args));
});
});
webview_instance_->RegisterShouldOverrideUrlLoadingHandler(
[this](LWE::WebContainer* view, const std::string& url) -> bool {
if (!has_navigation_delegate_) {
return false;
}
flutter::EncodableMap args = {
{flutter::EncodableValue("url"), flutter::EncodableValue(url)},
{flutter::EncodableValue("isForMainFrame"),
flutter::EncodableValue(true)},
};
auto result = std::make_unique<NavigationRequestResult>(url, this);
navigation_delegate_channel_->InvokeMethod(
"navigationRequest",
std::make_unique<flutter::EncodableValue>(args), std::move(result));
dispatcher->dispatchTaskOnMainThread([&]() {
flutter::EncodableMap args = {
{flutter::EncodableValue("url"), flutter::EncodableValue(url)},
{flutter::EncodableValue("isForMainFrame"),
flutter::EncodableValue(true)},
};
auto result = std::make_unique<NavigationRequestResult>(url, this);
navigation_delegate_channel_->InvokeMethod(
"navigationRequest",
std::make_unique<flutter::EncodableValue>(args),
std::move(result));
});
return true;
});
}
Expand All @@ -212,9 +226,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<flutter::EncodableValue>(args));
dispatcher->dispatchTaskOnMainThread([&]() {
JSUYA marked this conversation as resolved.
Show resolved Hide resolved
webview_channel_->InvokeMethod(
"javaScriptChannelMessage",
std::make_unique<flutter::EncodableValue>(args));
});
return "success";
};
webview_instance_->AddJavaScriptInterface(name, "postMessage", on_message);
Expand All @@ -238,6 +254,11 @@ void WebView::Dispose() {
webview_instance_->Destroy();
webview_instance_ = nullptr;
}

if (dispatcher) {
delete dispatcher;
dispatcher = nullptr;
}
}

void WebView::Resize(double width, double height) {
Expand Down Expand Up @@ -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.");
}
Expand Down
3 changes: 3 additions & 0 deletions packages/webview_flutter_lwe/tizen/src/webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <mutex>
#include <string>

#include "message_dispatcher.h"

typedef flutter::MethodCall<flutter::EncodableValue> FlMethodCall;
typedef flutter::MethodResult<flutter::EncodableValue> FlMethodResult;
typedef flutter::MethodChannel<flutter::EncodableValue> FlMethodChannel;
Expand Down Expand Up @@ -66,6 +68,7 @@ class WebView : public PlatformView {
void InitWebView();

LWE::WebContainer* webview_instance_ = nullptr;
MessageDispatcher* dispatcher = nullptr;
JSUYA marked this conversation as resolved.
Show resolved Hide resolved
JSUYA marked this conversation as resolved.
Show resolved Hide resolved
flutter::TextureRegistrar* texture_registrar_;
double width_;
double height_;
Expand Down
Loading