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