Commit 82dc73e8 authored by Gauthier Ambard's avatar Gauthier Ambard Committed by Commit Bot

[iOS] Fix the UserAgent used for creating web view

This CL makes sure that the user agent used at the creation of the
WebView is the correct one when the default user agent is automatic.

Bug: 1025225
Change-Id: I9e4408b0ff0fb36a2904f9f9304480656b60ab7f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2047189
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#740334}
parent f28594e3
......@@ -61,6 +61,7 @@ class TestWebClient : public web::WebClient {
int64_t navigation_id,
base::OnceCallback<void(NSString*)> callback) override;
UIView* GetWindowedContainer() override;
UserAgentType GetDefaultUserAgent(UIView* web_view, const GURL& url) override;
// Sets |plugin_not_supported_text_|.
void SetPluginNotSupportedText(const base::string16& text);
......@@ -81,6 +82,8 @@ class TestWebClient : public web::WebClient {
}
bool last_cert_error_overridable() { return last_cert_error_overridable_; }
void SetDefaultUserAgent(UserAgentType type) { default_user_agent_ = type; }
private:
base::string16 plugin_not_supported_text_;
NSString* early_page_script_ = nil;
......@@ -90,6 +93,7 @@ class TestWebClient : public web::WebClient {
GURL last_cert_error_request_url_;
bool last_cert_error_overridable_ = true;
bool allow_certificate_errors_ = false;
UserAgentType default_user_agent_ = UserAgentType::MOBILE;
};
} // namespace web
......
......@@ -49,7 +49,9 @@ base::string16 TestWebClient::GetPluginNotSupportedText() const {
}
std::string TestWebClient::GetUserAgent(UserAgentType type) const {
return "Chromium/66.0.3333.0 CFNetwork/893.14 Darwin/16.7.0";
if (type == UserAgentType::DESKTOP)
return "Chromium/66.0.3333.0 CFNetwork/893.14 Darwin/16.7.0 Desktop";
return "Chromium/66.0.3333.0 CFNetwork/893.14 Darwin/16.7.0 Mobile";
}
base::RefCountedMemory* TestWebClient::GetDataResourceBytes(
......@@ -114,4 +116,9 @@ UIView* TestWebClient::GetWindowedContainer() {
return UIApplication.sharedApplication.keyWindow.rootViewController.view;
}
UserAgentType TestWebClient::GetDefaultUserAgent(UIView* web_view,
const GURL& url) {
return default_user_agent_;
}
} // namespace web
......@@ -187,11 +187,6 @@ NSString* const kScriptMessageName = @"crwebinvoke";
// it's needed multiple times in a method.
@property(nonatomic, readonly) GURL currentURL;
// User agent type of the transient item if any, the pending item if a
// navigation is in progress or the last committed item otherwise.
// Returns MOBILE, the default type, if navigation manager is nullptr or empty.
@property(nonatomic, readonly) web::UserAgentType userAgentType;
@property(nonatomic, readonly) web::WebState* webState;
// WebStateImpl instance associated with this CRWWebController, web controller
// does not own this pointer.
......@@ -494,11 +489,6 @@ typedef void (^ViewportStateCompletion)(const web::PageViewportState*);
return [self currentURLWithTrustLevel:&trustLevel];
}
- (web::UserAgentType)userAgentType {
web::NavigationItem* item = self.currentNavItem;
return item ? item->GetUserAgentType() : web::UserAgentType::MOBILE;
}
- (WebState*)webState {
return _webStateImpl;
}
......@@ -1407,9 +1397,21 @@ typedef void (^ViewportStateCompletion)(const web::PageViewportState*);
- (WKWebView*)webViewWithConfiguration:(WKWebViewConfiguration*)config {
// Do not attach the context menu controller immediately as the JavaScript
// delegate must be specified.
return web::BuildWKWebView(CGRectZero, config,
self.webStateImpl->GetBrowserState(),
[self userAgentType]);
web::UserAgentType defaultUserAgent =
base::FeatureList::IsEnabled(
web::features::kUseDefaultUserAgentInWebClient)
? web::UserAgentType::AUTOMATIC
: web::UserAgentType::MOBILE;
web::NavigationItem* item = self.currentNavItem;
web::UserAgentType userAgentType =
item ? item->GetUserAgentType() : defaultUserAgent;
if (userAgentType == web::UserAgentType::AUTOMATIC) {
userAgentType =
web::GetWebClient()->GetDefaultUserAgent(_containerView, GURL());
}
return web::BuildWKWebView(
CGRectZero, config, self.webStateImpl->GetBrowserState(), userAgentType);
}
// Wraps the web view in a CRWWebViewContentView and adds it to the container
......
......@@ -12,6 +12,7 @@
#include "base/mac/foundation_util.h"
#include "base/scoped_observer.h"
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#import "base/test/ios/wait_util.h"
#include "base/test/scoped_feature_list.h"
......@@ -347,12 +348,29 @@ TEST_F(CRWWebControllerTest, BackForwardWithPendingNavigation) {
EXPECT_EQ(web::WKNavigationState::FINISHED, web_controller().navigationState);
}
// Tests that a web view is created after calling -[ensureWebViewCreated].
// Tests that a web view is created after calling -[ensureWebViewCreated] and
// check its user agent.
TEST_F(CRWWebControllerTest, WebViewCreatedAfterEnsureWebViewCreated) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(
features::kUseDefaultUserAgentInWebClient);
TestWebClient* web_client = static_cast<TestWebClient*>(GetWebClient());
[web_controller() removeWebView];
WKWebView* web_view = [web_controller() ensureWebViewCreated];
EXPECT_TRUE(web_view);
EXPECT_NSEQ(web_view, web_controller().jsInjector.webView);
EXPECT_NSEQ(
base::SysUTF8ToNSString(web_client->GetUserAgent(UserAgentType::MOBILE)),
web_view.customUserAgent);
web_client->SetDefaultUserAgent(UserAgentType::DESKTOP);
[web_controller() removeWebView];
web_view = [web_controller() ensureWebViewCreated];
EXPECT_NSEQ(
base::SysUTF8ToNSString(web_client->GetUserAgent(UserAgentType::DESKTOP)),
web_view.customUserAgent);
}
// Test fixture to test JavaScriptDialogPresenter.
......
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