Skip to content

Commit fddb6d8

Browse files
windows: Fixed onLoadResourceWithCustomScheme WebView event called every time
1 parent 226ebc5 commit fddb6d8

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
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.5.0+1
2+
3+
- Fixed `onLoadResourceWithCustomScheme` WebView event called every time
4+
15
## 0.5.0
26

37
- Implemented `shouldInterceptRequest`, `onLoadResourceWithCustomScheme` WebView events

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
3+
version: 0.5.0+1
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/in_app_webview/in_app_webview.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,12 @@ namespace flutter_inappwebview_plugin
835835
if (channelDelegate && succeededOrLog(args->get_Request(&webResourceRequest)) && succeededOrLog(args->GetDeferral(&deferral))) {
836836
auto request = std::make_shared<WebResourceRequest>(webResourceRequest);
837837

838+
// The add_WebResourceRequested event is by default raised for file, http, and https URI schemes.
839+
// This is also raised for registered custom URI schemes.
840+
// https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2?view=webview2-1.0.2792.45#add_webresourcerequested
841+
auto url = request->url.has_value() ? request->url.value() : "";
842+
auto isCustomScheme = !url.empty() && !starts_with(url, std::string{ "file://" }) && !starts_with(url, std::string{ "http://" }) && !starts_with(url, std::string{ "https://" });
843+
838844
auto onLoadResourceWithCustomSchemeCallback = [this, deferral, request, args]()
839845
{
840846
if (channelDelegate) {
@@ -874,9 +880,14 @@ namespace flutter_inappwebview_plugin
874880
failedLog(deferral->Complete());
875881
return false;
876882
};
877-
callback->nullSuccess = [this, deferral, args, onLoadResourceWithCustomSchemeCallback]()
883+
callback->nullSuccess = [this, deferral, args, isCustomScheme, onLoadResourceWithCustomSchemeCallback]()
878884
{
879-
onLoadResourceWithCustomSchemeCallback();
885+
if (isCustomScheme) {
886+
onLoadResourceWithCustomSchemeCallback();
887+
}
888+
else {
889+
failedLog(deferral->Complete());
890+
}
880891
return false;
881892
};
882893
callback->defaultBehaviour = defaultBehaviour;
@@ -887,9 +898,12 @@ namespace flutter_inappwebview_plugin
887898
};
888899
channelDelegate->shouldInterceptRequest(request, std::move(callback));
889900
}
890-
else {
901+
else if (isCustomScheme) {
891902
onLoadResourceWithCustomSchemeCallback();
892903
}
904+
else {
905+
failedLog(deferral->Complete());
906+
}
893907
}
894908
return S_OK;
895909
}

flutter_inappwebview_windows/windows/types/custom_scheme_registration.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <flutter/standard_method_codec.h>
55
#include <optional>
66
#include <WebView2EnvironmentOptions.h>
7-
#include <wil/com.h>
87

98
namespace flutter_inappwebview_plugin
109
{

flutter_inappwebview_windows/windows/utils/string.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ namespace flutter_inappwebview_plugin
176176
[](const T v) { return static_cast<T>(std::toupper(v)); });
177177
return s2;
178178
}
179+
180+
template <typename T>
181+
bool starts_with(const std::basic_string<T>& str, const std::basic_string<T>& prefix)
182+
{
183+
return str.size() >= prefix.size() && str.compare(0, prefix.size(), prefix) == 0;
184+
}
185+
186+
template <typename T>
187+
bool ends_with(const std::basic_string<T>& str, const std::basic_string<T>& suffix)
188+
{
189+
return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
190+
}
179191
}
180192

181193
#endif //FLUTTER_INAPPWEBVIEW_PLUGIN_UTIL_STRING_H_

0 commit comments

Comments
 (0)