Index: ios/web/web_state/ui/crw_web_controller.mm |
diff --git a/ios/web/web_state/ui/crw_web_controller.mm b/ios/web/web_state/ui/crw_web_controller.mm |
index 4d87b2c3da8df85af85d482cbd6a74a833044682..1f186d549373f07690ce3f2293f29be3e1d858cd 100644 |
--- a/ios/web/web_state/ui/crw_web_controller.mm |
+++ b/ios/web/web_state/ui/crw_web_controller.mm |
@@ -459,8 +459,10 @@ @interface CRWWebController ()<CRWContextMenuDelegate, |
// 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; |
@@ -477,10 +479,18 @@ @interface CRWWebController ()<CRWContextMenuDelegate, |
// unless the request was a POST. |
@property(nonatomic, readonly) NSDictionary* currentHTTPHeaders; |
-// 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; |
+// Updates web view's user agent according to |userAgentType|. It is no-op if |
+// |userAgentType| is NONE. |
+- (void)updateWebViewUserAgentFromUserAgentType: |
+ (web::UserAgentType)userAgentType; |
+ |
+// 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 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 |
+// fundamentally the same purpose. |
+- (void)requirePageReconstruction; |
// Removes the container view from the hierarchy and resets the ivar. |
- (void)resetContainerView; |
@@ -1616,6 +1626,13 @@ - (void)loadCurrentURLInWebView { |
[self ensureWebViewCreated]; |
+ // Update web view's user agent is called for every load, which may have |
+ // performance implications because update web view's user agent may |
+ // potentially send a message to a separate thread. However, this is not an |
+ // issue for WKWebView because WKWebView's |setCustomUserAgent| is non-op if |
+ // user agent stays thesame. |
+ [self updateWebViewUserAgentFromUserAgentType:self.userAgentType]; |
+ |
[self loadRequestForCurrentNavigationItem]; |
} |
@@ -2026,15 +2043,11 @@ - (void)goToItemAtIndex:(int)index { |
// Update the user agent before attempting the navigation. |
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 updateWebViewUserAgentFromUserAgentType:toItem->GetUserAgentType()]; |
+ web::NavigationItem* fromItem = sessionController.currentItem; |
BOOL sameDocumentNavigation = |
- [sessionController isSameDocumentNavigationBetweenItem:previousItem |
+ [sessionController isSameDocumentNavigationBetweenItem:fromItem |
andItem:toItem]; |
if (sameDocumentNavigation) { |
[sessionController goToItemAtIndex:index]; |
@@ -2189,9 +2202,9 @@ - (BOOL)shouldClosePageOnNativeApplicationLoad { |
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 { |
@@ -2230,15 +2243,14 @@ - (CRWPassKitDownloader*)passKitDownloader { |
return _passKitDownloader.get(); |
} |
-- (void)updateDesktopUserAgentForItem:(web::NavigationItem*)item |
- previousUserAgentType:(web::UserAgentType)userAgentType { |
- if (!item) |
+- (void)updateWebViewUserAgentFromUserAgentType: |
+ (web::UserAgentType)userAgentType { |
+ if (userAgentType == web::UserAgentType::NONE) |
return; |
- web::UserAgentType itemUserAgentType = item->GetUserAgentType(); |
- if (itemUserAgentType == web::UserAgentType::NONE) |
- return; |
- if (itemUserAgentType != userAgentType) |
- [self requirePageReconstruction]; |
+ |
+ NSString* userAgent = |
+ base::SysUTF8ToNSString(web::GetWebClient()->GetUserAgent(userAgentType)); |
+ [_webView setCustomUserAgent:userAgent]; |
} |
#pragma mark - |
@@ -4040,7 +4052,7 @@ - (WKWebView*)webViewWithConfiguration:(WKWebViewConfiguration*)config { |
// delegate must be specified. |
return web::BuildWKWebView(CGRectZero, config, |
self.webStateImpl->GetBrowserState(), |
- self.usesDesktopUserAgent); |
+ self.userAgentType); |
} |
- (void)setWebView:(WKWebView*)webView { |