|
3 | 3 |
|
4 | 4 | #include <Ecore_IMF_Evas.h> |
5 | 5 | #include <Ecore_Input_Evas.h> |
6 | | -#include <flutter/method_channel.h> |
7 | 6 | #include <flutter_platform_view.h> |
8 | 7 | #include <flutter_texture_registrar.h> |
9 | 8 |
|
@@ -58,27 +57,72 @@ double ExtractDoubleFromMap(const flutter::EncodableValue& arguments, |
58 | 57 |
|
59 | 58 | WebView::WebView(flutter::PluginRegistrar* registrar, int viewId, |
60 | 59 | FlutterTextureRegistrar* textureRegistrar, double width, |
61 | | - double height, const std::string initialUrl) |
| 60 | + double height, flutter::EncodableMap& params) |
62 | 61 | : PlatformView(registrar, viewId), |
63 | 62 | textureRegistrar_(textureRegistrar), |
64 | 63 | webViewInstance_(nullptr), |
65 | | - currentUrl_(initialUrl), |
66 | 64 | width_(width), |
67 | 65 | height_(height), |
68 | 66 | tbmSurface_(nullptr) { |
69 | 67 | SetTextureId(FlutterRegisterExternalTexture(textureRegistrar_)); |
70 | 68 | InitWebView(); |
71 | | - auto channel = |
72 | | - std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>( |
73 | | - GetPluginRegistrar()->messenger(), GetChannelName(), |
74 | | - &flutter::StandardMethodCodec::GetInstance()); |
75 | | - channel->SetMethodCallHandler( |
| 69 | + |
| 70 | + channel_ = std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>( |
| 71 | + GetPluginRegistrar()->messenger(), GetChannelName(), |
| 72 | + &flutter::StandardMethodCodec::GetInstance()); |
| 73 | + channel_->SetMethodCallHandler( |
76 | 74 | [webview = this](const auto& call, auto result) { |
77 | 75 | webview->HandleMethodCall(call, std::move(result)); |
78 | 76 | }); |
| 77 | + |
| 78 | + auto initialUrl = params[flutter::EncodableValue("initialUrl")]; |
| 79 | + if (std::holds_alternative<std::string>(initialUrl)) { |
| 80 | + currentUrl_ = std::get<std::string>(initialUrl); |
| 81 | + } else { |
| 82 | + currentUrl_ = "about:blank"; |
| 83 | + } |
| 84 | + |
| 85 | + auto names = params[flutter::EncodableValue("javascriptChannelNames")]; |
| 86 | + if (std::holds_alternative<flutter::EncodableList>(names)) { |
| 87 | + auto nameList = std::get<flutter::EncodableList>(names); |
| 88 | + for (size_t i = 0; i < nameList.size(); i++) { |
| 89 | + if (std::holds_alternative<std::string>(nameList[i])) { |
| 90 | + RegisterJavaScriptChannelName(std::get<std::string>(nameList[i])); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
79 | 95 | webViewInstance_->LoadURL(currentUrl_); |
80 | 96 | } |
81 | 97 |
|
| 98 | +/** |
| 99 | + * Added as a JavaScript interface to the WebView for any JavaScript channel |
| 100 | + * that the Dart code sets up. |
| 101 | + * |
| 102 | + * Exposes a single method named `postMessage` to JavaScript, which sends a |
| 103 | + * message over a method channel to the Dart code. |
| 104 | + */ |
| 105 | +void WebView::RegisterJavaScriptChannelName(const std::string& name) { |
| 106 | + LOG_DEBUG("RegisterJavaScriptChannelName(channelName: %s)\n", name.c_str()); |
| 107 | + |
| 108 | + std::function<std::string(const std::string&)> cb = |
| 109 | + [this, name](const std::string& message) -> std::string { |
| 110 | + LOG_DEBUG("Invoke JavaScriptChannel(message: %s)\n", message.c_str()); |
| 111 | + flutter::EncodableMap map; |
| 112 | + map.insert(std::make_pair<flutter::EncodableValue, flutter::EncodableValue>( |
| 113 | + flutter::EncodableValue("channel"), flutter::EncodableValue(name))); |
| 114 | + map.insert(std::make_pair<flutter::EncodableValue, flutter::EncodableValue>( |
| 115 | + flutter::EncodableValue("message"), flutter::EncodableValue(message))); |
| 116 | + |
| 117 | + std::unique_ptr<flutter::EncodableValue> args = |
| 118 | + std::make_unique<flutter::EncodableValue>(map); |
| 119 | + channel_->InvokeMethod("javascriptChannelMessage", std::move(args)); |
| 120 | + return "success"; |
| 121 | + }; |
| 122 | + |
| 123 | + webViewInstance_->AddJavaScriptInterface(name, "postMessage", cb); |
| 124 | +} |
| 125 | + |
82 | 126 | WebView::~WebView() { Dispose(); } |
83 | 127 |
|
84 | 128 | std::string WebView::GetChannelName() { |
@@ -543,7 +587,15 @@ void WebView::HandleMethodCall( |
543 | 587 | result->Error("Invalid Arguments", "Invalid Arguments"); |
544 | 588 | } |
545 | 589 | } else if (methodName.compare("addJavascriptChannels") == 0) { |
546 | | - result->NotImplemented(); |
| 590 | + if (std::holds_alternative<flutter::EncodableList>(arguments)) { |
| 591 | + auto nameList = std::get<flutter::EncodableList>(arguments); |
| 592 | + for (size_t i = 0; i < nameList.size(); i++) { |
| 593 | + if (std::holds_alternative<std::string>(nameList[i])) { |
| 594 | + RegisterJavaScriptChannelName(std::get<std::string>(nameList[i])); |
| 595 | + } |
| 596 | + } |
| 597 | + } |
| 598 | + result->Success(); |
547 | 599 | } else if (methodName.compare("removeJavascriptChannels") == 0) { |
548 | 600 | result->NotImplemented(); |
549 | 601 | } else if (methodName.compare("clearCache") == 0) { |
|
0 commit comments