Skip to content

Commit 952aa78

Browse files
windows: Updated code to support multiple flutter windows
1 parent 683d1ca commit 952aa78

14 files changed

+106
-40
lines changed

flutter_inappwebview_windows/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.6.0
2+
3+
- Updated code to support multiple flutter windows
4+
15
## 0.5.0+2
26

37
- Fixed `InAppWebViewController.callAsyncJavaScript` not working with JSON objects

flutter_inappwebview_windows/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_inappwebview_windows
22
description: Windows implementation of the flutter_inappwebview plugin.
3-
version: 0.5.0+2
3+
version: 0.6.0
44
homepage: https://inappwebview.dev/
55
repository: https://github.com/pichillilorenzo/flutter_inappwebview/tree/master/flutter_inappwebview_windows
66
issue_tracker: https://github.com/pichillilorenzo/flutter_inappwebview/issues

flutter_inappwebview_windows/windows/cookie_manager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace flutter_inappwebview_plugin
2525

2626
auto webViewEnvironmentId = get_optional_fl_map_value<std::string>(arguments, "webViewEnvironmentId");
2727

28-
auto webViewEnvironment = webViewEnvironmentId.has_value() && map_contains(plugin->webViewEnvironmentManager->webViewEnvironments, webViewEnvironmentId.value())
28+
auto webViewEnvironment = plugin && webViewEnvironmentId.has_value() && map_contains(plugin->webViewEnvironmentManager->webViewEnvironments, webViewEnvironmentId.value())
2929
? plugin->webViewEnvironmentManager->webViewEnvironments.at(webViewEnvironmentId.value()).get() : nullptr;
3030

3131
auto result_ = std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>>(std::move(result));

flutter_inappwebview_windows/windows/custom_platform_view/custom_platform_view.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,21 @@ namespace flutter_inappwebview_plugin
186186
event_channel_->SetStreamHandler(std::move(handler));
187187
}
188188

189+
void CustomPlatformView::UnregisterMethodCallHandler() const
190+
{
191+
if (method_channel_) {
192+
method_channel_->SetMethodCallHandler(nullptr);
193+
if (view && view->channelDelegate) {
194+
view->channelDelegate->UnregisterMethodCallHandler();
195+
}
196+
}
197+
}
198+
189199
CustomPlatformView::~CustomPlatformView()
190200
{
191201
debugLog("dealloc CustomPlatformView");
192-
method_channel_->SetMethodCallHandler(nullptr);
193202
event_sink_ = nullptr;
194-
texture_registrar_->UnregisterTexture(texture_id_);
203+
texture_registrar_->UnregisterTexture(texture_id_, nullptr);
195204
}
196205

197206
void CustomPlatformView::RegisterEventHandlers()

flutter_inappwebview_windows/windows/custom_platform_view/custom_platform_view.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ namespace flutter_inappwebview_plugin
2929
TextureBridge* texture_bridge() const { return texture_bridge_.get(); }
3030

3131
int64_t texture_id() const { return texture_id_; }
32+
33+
void UnregisterMethodCallHandler() const;
3234
private:
3335
HWND hwnd_;
3436
std::unique_ptr<flutter::TextureVariant> flutter_texture_;

flutter_inappwebview_windows/windows/flutter_inappwebview_windows_plugin.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,11 @@ namespace flutter_inappwebview_plugin
3333
}
3434

3535
FlutterInappwebviewWindowsPlugin::~FlutterInappwebviewWindowsPlugin()
36-
{}
36+
{
37+
webViewEnvironmentManager = nullptr;
38+
inAppWebViewManager = nullptr;
39+
inAppBrowserManager = nullptr;
40+
headlessInAppWebViewManager = nullptr;
41+
cookieManager = nullptr;
42+
}
3743
}

flutter_inappwebview_windows/windows/headless_in_app_webview/headless_in_app_webview_manager.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ namespace flutter_inappwebview_plugin
4646
{
4747
auto result_ = std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>>(std::move(result));
4848

49+
if (!plugin) {
50+
result_->Error("0", "Cannot create the HeadlessInAppWebView instance!");
51+
return;
52+
}
53+
4954
auto id = get_fl_map_value<std::string>(*arguments, "id");
5055
auto params = get_fl_map_value<flutter::EncodableMap>(*arguments, "params");
5156

@@ -80,7 +85,7 @@ namespace flutter_inappwebview_plugin
8085
wil::com_ptr<ICoreWebView2Controller> webViewController,
8186
wil::com_ptr<ICoreWebView2CompositionController> webViewCompositionController)
8287
{
83-
if (webViewEnv && webViewController) {
88+
if (plugin && webViewEnv && webViewController) {
8489
std::optional<std::vector<std::shared_ptr<UserScript>>> initialUserScripts = initialUserScriptList.has_value() ?
8590
functional_map(initialUserScriptList.value(), [](const flutter::EncodableValue& map) { return std::make_shared<UserScript>(std::get<flutter::EncodableMap>(map)); }) :
8691
std::optional<std::vector<std::shared_ptr<UserScript>>>{};

flutter_inappwebview_windows/windows/headless_in_app_webview/headless_webview_channel_delegate.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ namespace flutter_inappwebview_plugin
2929
std::map<std::string, std::unique_ptr<HeadlessInAppWebView>>& webViews = webView->plugin->headlessInAppWebViewManager->webViews;
3030
auto& id = webView->id;
3131
if (map_contains(webViews, id)) {
32+
if (webView->channelDelegate) {
33+
webView->channelDelegate->UnregisterMethodCallHandler();
34+
if (webView->webView && webView->webView->channelDelegate) {
35+
webView->webView->channelDelegate->UnregisterMethodCallHandler();
36+
}
37+
}
3238
webViews.erase(id);
3339
}
3440
}

flutter_inappwebview_windows/windows/in_app_browser/in_app_browser.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,18 @@ namespace flutter_inappwebview_plugin
200200
if (!destroyed_) {
201201
destroyed_ = true;
202202

203-
webView.reset();
204-
205203
if (channelDelegate) {
206204
channelDelegate->onExit();
207205
}
208206

207+
if (channelDelegate) {
208+
channelDelegate->UnregisterMethodCallHandler();
209+
if (webView && webView->channelDelegate) {
210+
webView->channelDelegate->UnregisterMethodCallHandler();
211+
}
212+
}
213+
webView.reset();
214+
209215
if (plugin && plugin->inAppBrowserManager) {
210216
plugin->inAppBrowserManager->browsers.erase(id);
211217
}

flutter_inappwebview_windows/windows/in_app_browser/in_app_browser_manager.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ namespace flutter_inappwebview_plugin
2525
auto& methodName = method_call.method_name();
2626

2727
if (string_equals(methodName, "open")) {
28-
createInAppBrowser(arguments);
29-
result->Success(true);
28+
if (plugin) {
29+
createInAppBrowser(arguments);
30+
result->Success(true);
31+
}
32+
else {
33+
result->Error("0", "Cannot create the InAppBrowser instance!");
34+
35+
}
3036
}
3137
else if (string_equals(methodName, "openWithSystemBrowser")) {
3238
auto url = get_fl_map_value<std::string>(*arguments, "url");

0 commit comments

Comments
 (0)