Commit f30e5800 authored by Gauthier Ambard's avatar Gauthier Ambard Committed by Commit Bot

[iOS] Adjust the UserAgent in decidePolicyForNavigationAction

This CL modifies the place where the customUserAgent is updated,
changing it from the request to -decidePolicyForNavigationAction:.
This prevents issues where the page is loaded but not from a request
(for example after a back/forward navigation).

Fixed: 1059235
Change-Id: I83205c80a49e51ebfa194d12bae016bf48b52b4d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2107555
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarAli Juma <ajuma@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#754442}
parent e4ac4a0c
...@@ -151,6 +151,7 @@ source_set("eg2_tests") { ...@@ -151,6 +151,7 @@ source_set("eg2_tests") {
] ]
deps = [ deps = [
":constants", ":constants",
"//base/test:test_support",
"//components/strings", "//components/strings",
"//ios/chrome/app/strings", "//ios/chrome/app/strings",
"//ios/chrome/test/earl_grey:eg_test_support+eg2", "//ios/chrome/test/earl_grey:eg_test_support+eg2",
...@@ -174,6 +175,7 @@ source_set("eg_tests") { ...@@ -174,6 +175,7 @@ source_set("eg_tests") {
] ]
deps = [ deps = [
"//base", "//base",
"//base/test:test_support",
"//components/strings", "//components/strings",
"//ios/chrome/app/strings", "//ios/chrome/app/strings",
"//ios/chrome/browser/ui/popup_menu:constants", "//ios/chrome/browser/ui/popup_menu:constants",
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#import "base/test/ios/wait_util.h"
#include "components/strings/grit/components_strings.h" #include "components/strings/grit/components_strings.h"
#import "ios/chrome/browser/ui/popup_menu/popup_menu_constants.h" #import "ios/chrome/browser/ui/popup_menu/popup_menu_constants.h"
#include "ios/chrome/grit/ios_strings.h" #include "ios/chrome/grit/ios_strings.h"
...@@ -31,6 +32,17 @@ const char kMobileSiteLabel[] = "Mobile"; ...@@ -31,6 +32,17 @@ const char kMobileSiteLabel[] = "Mobile";
const char kDesktopSiteLabel[] = "Desktop"; const char kDesktopSiteLabel[] = "Desktop";
const char kDesktopPlatformLabel[] = "MacIntel"; const char kDesktopPlatformLabel[] = "MacIntel";
// URL to be used when the page needs to be reloaded on back/forward
// navigations.
const char kPurgeURL[] = "url-purge.com";
// JavaScript used to reload the page on back/forward navigations.
const char kJavaScriptReload[] =
"<script>window.onpageshow = function(event) {"
" if (event.persisted) {"
" window.location.href = window.location.href + \"?reloaded\""
" }"
"};</script>";
// Custom timeout used when waiting for a web state after requesting desktop // Custom timeout used when waiting for a web state after requesting desktop
// or mobile mode. // or mobile mode.
const NSTimeInterval kWaitForUserAgentChangeTimeout = 15.0; const NSTimeInterval kWaitForUserAgentChangeTimeout = 15.0;
...@@ -79,15 +91,20 @@ class UserAgentResponseProvider : public web::DataResponseProvider { ...@@ -79,15 +91,20 @@ class UserAgentResponseProvider : public web::DataResponseProvider {
return; return;
} }
std::string purge_additions = "";
if (request.url.path().find(kPurgeURL) != std::string::npos) {
purge_additions = kJavaScriptReload;
}
*headers = web::ResponseProvider::GetDefaultResponseHeaders(); *headers = web::ResponseProvider::GetDefaultResponseHeaders();
std::string userAgent; std::string userAgent;
std::string desktop_user_agent = std::string desktop_user_agent =
web::BuildUserAgentFromProduct(web::UserAgentType::DESKTOP, ""); web::BuildUserAgentFromProduct(web::UserAgentType::DESKTOP, "");
if (request.headers.GetHeader("User-Agent", &userAgent) && if (request.headers.GetHeader("User-Agent", &userAgent) &&
userAgent == desktop_user_agent) { userAgent == desktop_user_agent) {
response_body->assign("Desktop"); response_body->assign(std::string("Desktop\n") + purge_additions);
} else { } else {
response_body->assign("Mobile"); response_body->assign(std::string("Mobile\n") + purge_additions);
} }
} }
}; };
...@@ -168,6 +185,45 @@ class UserAgentResponseProvider : public web::DataResponseProvider { ...@@ -168,6 +185,45 @@ class UserAgentResponseProvider : public web::DataResponseProvider {
[ChromeEarlGrey waitForWebStateContainingText:kMobileSiteLabel]; [ChromeEarlGrey waitForWebStateContainingText:kMobileSiteLabel];
} }
// Tests that when requesting desktop on another page and coming back to a page
// that has been purged from memory, we still display the mobile page.
- (void)testRequestDesktopSiteGoBackToMobilePurged {
if (@available(iOS 13, *)) {
} else {
EARL_GREY_TEST_DISABLED(@"On iOS 12, the User Agent can be wrong when "
@"doing back/forward navigations");
}
std::unique_ptr<web::DataResponseProvider> provider(
new UserAgentResponseProvider());
web::test::SetUpHttpServer(std::move(provider));
[ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(
"http://" + std::string(kPurgeURL))];
// Verify initial reception of the mobile site.
[ChromeEarlGrey waitForWebStateContainingText:kMobileSiteLabel];
[ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl("http://2.com")];
// Request and verify reception of the desktop site.
[ChromeEarlGreyUI openToolsMenu];
[RequestDesktopButton() performAction:grey_tap()];
[ChromeEarlGrey waitForWebStateContainingText:kDesktopSiteLabel
timeout:kWaitForUserAgentChangeTimeout];
// Verify that going back returns to the mobile site.
[[EarlGrey selectElementWithMatcher:chrome_test_util::BackButton()]
performAction:grey_tap()];
GREYAssert(base::test::ios::WaitUntilConditionOrTimeout(
base::test::ios::kWaitForPageLoadTimeout,
^bool {
return [ChromeEarlGrey webStateVisibleURL].query() ==
"reloaded";
}),
@"Page did not reload");
[ChromeEarlGrey waitForWebStateContainingText:kMobileSiteLabel];
}
// Tests that requesting mobile site of a page works and the user agent // Tests that requesting mobile site of a page works and the user agent
// propagates to the next navigations in the same tab. // propagates to the next navigations in the same tab.
- (void)testRequestMobileSitePropagatesToNextNavigations { - (void)testRequestMobileSitePropagatesToNextNavigations {
......
...@@ -181,6 +181,21 @@ void ReportOutOfSyncURLInDidStartProvisionalNavigation( ...@@ -181,6 +181,21 @@ void ReportOutOfSyncURLInDidStartProvisionalNavigation(
} }
} }
if (item && userAgentType == web::UserAgentType::NONE &&
web::GetWebClient()->IsAppSpecificURL(item->GetVirtualURL())) {
// In case the URL to be loaded is a WebUI URL and the user agent is nil,
// get the mobile user agent.
userAgentType = web::UserAgentType::MOBILE;
}
if (userAgentType != web::UserAgentType::NONE) {
NSString* userAgentString = base::SysUTF8ToNSString(
web::GetWebClient()->GetUserAgent(userAgentType));
if (![webView.customUserAgent isEqualToString:userAgentString]) {
webView.customUserAgent = userAgentString;
}
}
WKContentMode contentMode = userAgentType == web::UserAgentType::DESKTOP WKContentMode contentMode = userAgentType == web::UserAgentType::DESKTOP
? WKContentModeDesktop ? WKContentModeDesktop
: WKContentModeMobile; : WKContentModeMobile;
......
...@@ -573,11 +573,18 @@ enum class BackForwardNavigationType { ...@@ -573,11 +573,18 @@ enum class BackForwardNavigationType {
itemUserAgentType = web::UserAgentType::MOBILE; itemUserAgentType = web::UserAgentType::MOBILE;
} }
if (itemUserAgentType != web::UserAgentType::NONE) { if (@available(iOS 13, *)) {
NSString* userAgentString = base::SysUTF8ToNSString( } else {
web::GetWebClient()->GetUserAgent(itemUserAgentType)); // On iOS 13, this is done in
if (![self.webView.customUserAgent isEqualToString:userAgentString]) { // webView:decidePolicyForNavigationAction:preferences:decisionHandler:. As
self.webView.customUserAgent = userAgentString; // the method only exists for iOS 13, this check still need to be there for
// iOS 12.
if (itemUserAgentType != web::UserAgentType::NONE) {
NSString* userAgentString = base::SysUTF8ToNSString(
web::GetWebClient()->GetUserAgent(itemUserAgentType));
if (![self.webView.customUserAgent isEqualToString:userAgentString]) {
self.webView.customUserAgent = userAgentString;
}
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment