Commit 3f22b496 authored by Yuke Liao's avatar Yuke Liao Committed by Commit Bot

Use UserAgentType for CRWWebController and web view creation utils

https://chromium-review.googlesource.com/c/542962/ was reverted due to
a bug, however, part of the changes are still useful, so restore them
in this CL. And specifically, this CL refactors CRWWebController and
web view creation utils to use UserAgentType.

Bug: 
Change-Id: Iafaca12bd30d1b84b315295ee090e7ee68161bfe
Reviewed-on: https://chromium-review.googlesource.com/545112
Commit-Queue: Yuke Liao <liaoyuke@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#481922}
parent cb868403
......@@ -190,11 +190,11 @@ class WebStateImpl;
// Dismisses the soft keyboard.
- (void)dismissKeyboard;
// Requires that the next load rebuild the UIWebView. This is expensive, and
// should be used only in the case where something has changed that UIWebView
// Requires that the next load rebuild the web view. This is expensive, and
// should be used only in the case where something has changed that the web view
// only checks on creation, such that the whole object needs to be rebuilt.
// TODO(stuartmorgan): Merge this and reinitializeWebViewAndReload:. They are
// currently subtly different in terms of implementation, but are for
// TODO(crbug.com/736102): Merge this and reinitializeWebViewAndReload:. They
// are currently subtly different in terms of implementation, but are for
// fundamentally the same purpose.
- (void)requirePageReconstruction;
......
......@@ -471,8 +471,10 @@ NSError* WKWebViewErrorWithSource(NSError* error, WKWebViewErrorSource source) {
// Returns YES if the user interacted with the page recently.
@property(nonatomic, readonly) BOOL userClickedRecently;
// Whether or not desktop user agent is used for the currentItem.
@property(nonatomic, readonly) BOOL usesDesktopUserAgent;
// 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;
// Facade for Mojo API.
@property(nonatomic, readonly) web::MojoFacade* mojoFacade;
......@@ -493,10 +495,11 @@ NSError* WKWebViewErrorWithSource(NSError* error, WKWebViewErrorSource source) {
// loaded.
@property(nonatomic, readwrite) BOOL userInteractionRegistered;
// Requires page reconstruction if |item| has a non-NONE UserAgentType and it
// differs from that of |fromItem|.
- (void)updateDesktopUserAgentForItem:(web::NavigationItem*)item
previousUserAgentType:(web::UserAgentType)userAgentType;
// Requires page reconstruction if |userAgentType| is a non-NONE and it differs
// from that of |previousUserAgentType|.
- (void)updateWebViewFromUserAgentType:(web::UserAgentType)userAgentType
previousUserAgentType:
(web::UserAgentType)previousUserAgentType;
// Removes the container view from the hierarchy and resets the ivar.
- (void)resetContainerView;
......@@ -2055,13 +2058,16 @@ registerLoadRequestForURL:(const GURL&)requestURL
[self clearTransientContentView];
// Update the user agent before attempting the navigation.
// TODO(crbug.com/736103): due to the bug, updating the user agent of web view
// requires reconstructing the while web view, change the behavior to call
// [WKWebView setCustomUserAgent] once the bug is fixed.
web::NavigationItem* toItem = items[index].get();
web::NavigationItem* previousItem = sessionController.currentItem;
web::UserAgentType previousUserAgentType =
previousItem ? previousItem->GetUserAgentType()
: web::UserAgentType::NONE;
[self updateDesktopUserAgentForItem:toItem
previousUserAgentType:previousUserAgentType];
[self updateWebViewFromUserAgentType:toItem->GetUserAgentType()
previousUserAgentType:previousUserAgentType];
BOOL sameDocumentNavigation =
[sessionController isSameDocumentNavigationBetweenItem:previousItem
......@@ -2221,9 +2227,9 @@ registerLoadRequestForURL:(const GURL&)requestURL
return rendererInitiatedWithoutInteraction || noNavigationItems;
}
- (BOOL)usesDesktopUserAgent {
- (web::UserAgentType)userAgentType {
web::NavigationItem* item = self.currentNavItem;
return item && item->GetUserAgentType() == web::UserAgentType::DESKTOP;
return item ? item->GetUserAgentType() : web::UserAgentType::MOBILE;
}
- (web::MojoFacade*)mojoFacade {
......@@ -2262,6 +2268,15 @@ registerLoadRequestForURL:(const GURL&)requestURL
return _passKitDownloader.get();
}
- (void)updateWebViewFromUserAgentType:(web::UserAgentType)userAgentType
previousUserAgentType:
(web::UserAgentType)previousUserAgentType {
if (userAgentType != web::UserAgentType::NONE &&
userAgentType != previousUserAgentType) {
[self requirePageReconstruction];
}
}
- (void)updateDesktopUserAgentForItem:(web::NavigationItem*)item
previousUserAgentType:(web::UserAgentType)userAgentType {
if (!item)
......@@ -3973,7 +3988,7 @@ registerLoadRequestForURL:(const GURL&)requestURL
// delegate must be specified.
return web::BuildWKWebView(CGRectZero, config,
self.webStateImpl->GetBrowserState(),
self.usesDesktopUserAgent);
self.userAgentType);
}
- (void)setWebView:(WKWebView*)webView {
......
......@@ -8,6 +8,8 @@
#import <CoreGraphics/CoreGraphics.h>
#import <WebKit/WebKit.h>
#include "ios/web/public/user_agent.h"
@protocol CRWContextMenuDelegate;
// This file is a collection of functions that vend web views.
......@@ -26,7 +28,7 @@ class BrowserState;
WKWebView* BuildWKWebView(CGRect frame,
WKWebViewConfiguration* configuration,
BrowserState* browser_state,
BOOL use_desktop_user_agent,
UserAgentType user_agent_type,
id<CRWContextMenuDelegate> context_menu_delegate);
// Creates and returns a new WKWebView for displaying regular web content.
......@@ -35,7 +37,7 @@ WKWebView* BuildWKWebView(CGRect frame,
WKWebView* BuildWKWebView(CGRect frame,
WKWebViewConfiguration* configuration,
BrowserState* browser_state,
BOOL use_desktop_user_agent);
UserAgentType user_agent_type);
// Creates and returns a new WKWebView for displaying regular web content.
// The preconditions for the creation of a WKWebView are the same as the
......
......@@ -36,7 +36,7 @@ void VerifyWKWebViewCreationPreConditions(
WKWebView* BuildWKWebView(CGRect frame,
WKWebViewConfiguration* configuration,
BrowserState* browser_state,
BOOL use_desktop_user_agent,
UserAgentType user_agent_type,
id<CRWContextMenuDelegate> context_menu_delegate) {
VerifyWKWebViewCreationPreConditions(browser_state, configuration);
......@@ -44,11 +44,11 @@ WKWebView* BuildWKWebView(CGRect frame,
WKWebView* web_view =
[[WKWebView alloc] initWithFrame:frame configuration:configuration];
// Set the user agent.
UserAgentType user_agent_type =
use_desktop_user_agent ? UserAgentType::DESKTOP : UserAgentType::MOBILE;
web_view.customUserAgent = base::SysUTF8ToNSString(
web::GetWebClient()->GetUserAgent(user_agent_type));
// Set the user agent type.
if (user_agent_type != web::UserAgentType::NONE) {
web_view.customUserAgent = base::SysUTF8ToNSString(
web::GetWebClient()->GetUserAgent(user_agent_type));
}
// By default the web view uses a very sluggish scroll speed. Set it to a more
// reasonable value.
......@@ -77,17 +77,16 @@ WKWebView* BuildWKWebView(CGRect frame,
WKWebView* BuildWKWebView(CGRect frame,
WKWebViewConfiguration* configuration,
BrowserState* browser_state,
BOOL use_desktop_user_agent) {
return BuildWKWebView(frame, configuration, browser_state,
use_desktop_user_agent, nil);
UserAgentType user_agent_type) {
return BuildWKWebView(frame, configuration, browser_state, user_agent_type,
nil);
}
WKWebView* BuildWKWebView(CGRect frame,
WKWebViewConfiguration* configuration,
BrowserState* browser_state) {
BOOL use_desktop_user_agent = NO;
return BuildWKWebView(frame, configuration, browser_state,
use_desktop_user_agent);
UserAgentType::MOBILE);
}
} // namespace web
......@@ -5,6 +5,7 @@
#import "ios/web/public/web_view_creation_util.h"
#include "base/logging.h"
#include "ios/web/public/user_agent.h"
#import "ios/web/web_state/ui/wk_web_view_configuration_provider.h"
#import "ios/web/web_state/web_view_internal_creation_util.h"
......@@ -27,7 +28,8 @@ WKWebView* BuildWKWebViewWithCustomContextMenu(
WKWebViewConfigurationProvider& config_provider =
WKWebViewConfigurationProvider::FromBrowserState(browser_state);
return BuildWKWebView(frame, config_provider.GetWebViewConfiguration(),
browser_state, NO, context_menu_delegate);
browser_state, UserAgentType::MOBILE,
context_menu_delegate);
}
} // namespace web
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