Chromium Code Reviews| 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..d4fd8af0b207436bfa91e4c5d36240c2cf7e23f2 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,17 @@ @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|. Is no-op if |
| +// |userAgentType| is NONE or web view's user agent will not change. |
| +- (void)updateWebViewUserAgentIfChanged:(web::UserAgentType)userAgentType; |
|
Eugene But (OOO till 7-30)
2017/04/13 00:34:06
From "Coding Guidelines for Cocoa": Make the word
liaoyuke
2017/04/13 16:43:13
Done.
|
| + |
| +// 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; |
| @@ -1615,6 +1624,7 @@ - (void)loadCurrentURLInWebView { |
| DCHECK(!targetURL.SchemeIs(url::kJavaScriptScheme)); |
| [self ensureWebViewCreated]; |
| + [self updateWebViewUserAgentIfChanged:self.userAgentType]; |
| [self loadRequestForCurrentNavigationItem]; |
| } |
| @@ -2026,15 +2036,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 updateWebViewUserAgentIfChanged:toItem->GetUserAgentType()]; |
| + web::NavigationItem* fromItem = sessionController.currentItem; |
| BOOL sameDocumentNavigation = |
| - [sessionController isSameDocumentNavigationBetweenItem:previousItem |
| + [sessionController isSameDocumentNavigationBetweenItem:fromItem |
| andItem:toItem]; |
| if (sameDocumentNavigation) { |
| [sessionController goToItemAtIndex:index]; |
| @@ -2189,9 +2195,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 +2236,14 @@ - (CRWPassKitDownloader*)passKitDownloader { |
| return _passKitDownloader.get(); |
| } |
| -- (void)updateDesktopUserAgentForItem:(web::NavigationItem*)item |
| - previousUserAgentType:(web::UserAgentType)userAgentType { |
| - if (!item) |
| +- (void)updateWebViewUserAgentIfChanged:(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)); |
| + if (![userAgent isEqualToString:[_webView customUserAgent]]) |
|
Eugene But (OOO till 7-30)
2017/04/13 00:34:06
Is there guarantee that customUserAgent is not nil
liaoyuke
2017/04/13 16:43:13
Yes, in BuildWKWebView, we always set the customUs
|
| + [_webView setCustomUserAgent:userAgent]; |
| } |
| #pragma mark - |
| @@ -4040,7 +4045,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 { |