Skip to content

Commit 0b75f65

Browse files
windows: updated where the onLoadStart and onProgressChanged events are called on native side
1 parent 1747f3d commit 0b75f65

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

flutter_inappwebview_windows/windows/in_app_webview/in_app_webview.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,15 @@ namespace flutter_inappwebview_plugin
244244
auto isResponseStage = requestPausedData.contains("responseStatusCode");
245245
auto frameId = requestPausedData.at("frameId").get<std::string>();
246246

247-
auto allowRequest = [this, requestId]()
247+
auto request = requestPausedData.at("request").get<nlohmann::json>();
248+
std::optional<std::string> url = request.at("url").is_string() ? request.at("url").get<std::string>() : std::optional<std::string>{};
249+
std::optional<std::string> urlFragment = request.contains("urlFragment") && request.at("urlFragment").is_string() ? request.at("urlFragment").get<std::string>() : std::optional<std::string>{};
250+
if (url.has_value() && urlFragment.has_value()) {
251+
url = url.value() + urlFragment.value();
252+
}
253+
auto isForMainFrame = pageFrameId_.empty() || string_equals(pageFrameId_, frameId);
254+
255+
auto allowRequest = [this, requestId, url, isForMainFrame]()
248256
{
249257
failedAndLog(webView->CallDevToolsProtocolMethod(L"Fetch.continueRequest",
250258
utf8_to_wide("{\"requestId\":\"" + requestId + "\"}").c_str(),
@@ -255,6 +263,13 @@ namespace flutter_inappwebview_plugin
255263
return S_OK;
256264
}
257265
).Get()));
266+
267+
if (channelDelegate && isForMainFrame) {
268+
// if shouldOverrideUrlLoading is used, then call onLoadStart and onProgressChanged here
269+
// to match the behaviour of the other platforms
270+
channelDelegate->onLoadStart(url);
271+
channelDelegate->onProgressChanged(0);
272+
}
258273
};
259274

260275
auto cancelRequest = [this, requestId]()
@@ -271,12 +286,6 @@ namespace flutter_inappwebview_plugin
271286
};
272287

273288
if (!isResponseStage && channelDelegate && settings->useShouldOverrideUrlLoading && string_equals(resourceType, "Document")) {
274-
auto request = requestPausedData.at("request").get<nlohmann::json>();
275-
std::optional<std::string> url = request.at("url").is_string() ? request.at("url").get<std::string>() : std::optional<std::string>{};
276-
std::optional<std::string> urlFragment = request.contains("urlFragment") && request.at("urlFragment").is_string() ? request.at("urlFragment").get<std::string>() : std::optional<std::string>{};
277-
if (url.has_value() && urlFragment.has_value()) {
278-
url = url.value() + urlFragment.value();
279-
}
280289
std::optional<std::string> method = request.at("method").is_string() ? request.at("method").get<std::string>() : std::optional<std::string>{};
281290
std::optional<std::map<std::string, std::string>> headers = request.at("headers").is_object() ? request.at("headers").get<std::map<std::string, std::string>>() : std::optional<std::map<std::string, std::string>>{};
282291
std::optional<std::string> redirectedRequestId = request.contains("redirectedRequestId") && request.at("redirectedRequestId").is_string() ? request.at("redirectedRequestId").get<std::string>() : std::optional<std::string>{};
@@ -311,8 +320,6 @@ namespace flutter_inappwebview_plugin
311320

312321
std::optional<NavigationActionType> navigationType = isRedirect ? NavigationActionType::other : std::optional<NavigationActionType>{};
313322

314-
auto isForMainFrame = pageFrameId_.empty() || string_equals(pageFrameId_, frameId);
315-
316323
auto urlRequest = std::make_shared<URLRequest>(url, method, headers, body);
317324
auto navigationAction = std::make_shared<NavigationAction>(
318325
urlRequest,
@@ -345,7 +352,8 @@ namespace flutter_inappwebview_plugin
345352
channelDelegate->shouldOverrideUrlLoading(std::move(navigationAction), std::move(callback));
346353
}
347354
else {
348-
// check if a custom event listener is found and give the opportunity to it to handle the request
355+
// check if a custom event listener is found and give back the opportunity to it to handle the request
356+
// through the Chrome Dev Protocol API
349357
if (!map_contains(devToolsProtocolEventListener_, std::string("Fetch.requestPaused"))) {
350358
// if a custom event listener is not found, continue the request
351359
allowRequest();
@@ -449,8 +457,11 @@ namespace flutter_inappwebview_plugin
449457
navigationActions_.insert({ navigationId, navigationAction });
450458
}
451459

452-
channelDelegate->onLoadStart(url);
453-
channelDelegate->onProgressChanged(0);
460+
// if shouldOverrideUrlLoading is not used, then call onLoadStart and onProgressChanged here
461+
if (!settings->useShouldOverrideUrlLoading) {
462+
channelDelegate->onLoadStart(url);
463+
channelDelegate->onProgressChanged(0);
464+
}
454465
args->put_Cancel(false);
455466

456467
return S_OK;

0 commit comments

Comments
 (0)