Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1084)

Side by Side Diff: ios/web/web_state/ui/crw_web_controller.mm

Issue 2811073005: Replace requirePageReconstruction with setCustomUserAgent (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "ios/web/web_state/ui/crw_web_controller.h" 5 #import "ios/web/web_state/ui/crw_web_controller.h"
6 6
7 #import <WebKit/WebKit.h> 7 #import <WebKit/WebKit.h>
8 8
9 #import <objc/runtime.h> 9 #import <objc/runtime.h>
10 #include <stddef.h> 10 #include <stddef.h>
(...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 // an error if the returned URL is not reliable from a security point of view. 452 // an error if the returned URL is not reliable from a security point of view.
453 // Note that this method is expensive, so it should always be cached locally if 453 // Note that this method is expensive, so it should always be cached locally if
454 // it's needed multiple times in a method. 454 // it's needed multiple times in a method.
455 @property(nonatomic, readonly) GURL currentURL; 455 @property(nonatomic, readonly) GURL currentURL;
456 // Returns the referrer for the current page. 456 // Returns the referrer for the current page.
457 @property(nonatomic, readonly) web::Referrer currentReferrer; 457 @property(nonatomic, readonly) web::Referrer currentReferrer;
458 458
459 // Returns YES if the user interacted with the page recently. 459 // Returns YES if the user interacted with the page recently.
460 @property(nonatomic, readonly) BOOL userClickedRecently; 460 @property(nonatomic, readonly) BOOL userClickedRecently;
461 461
462 // Whether or not desktop user agent is used for the currentItem. 462 // User agent type of the transient item if any, the pending item if a
463 @property(nonatomic, readonly) BOOL usesDesktopUserAgent; 463 // navigation is in progress or the last committed item otherwise.
464 // Returns MOBILE, the default type, if navigation manager is nullptr or empty.
465 @property(nonatomic, readonly) web::UserAgentType userAgentType;
464 466
465 // Facade for Mojo API. 467 // Facade for Mojo API.
466 @property(nonatomic, readonly) web::MojoFacade* mojoFacade; 468 @property(nonatomic, readonly) web::MojoFacade* mojoFacade;
467 469
468 // TODO(crbug.com/692871): Remove these functions and replace with more 470 // TODO(crbug.com/692871): Remove these functions and replace with more
469 // appropriate NavigationItem getters. 471 // appropriate NavigationItem getters.
470 // Returns the navigation item for the current page. 472 // Returns the navigation item for the current page.
471 @property(nonatomic, readonly) web::NavigationItemImpl* currentNavItem; 473 @property(nonatomic, readonly) web::NavigationItemImpl* currentNavItem;
472 // Returns the current transition type. 474 // Returns the current transition type.
473 @property(nonatomic, readonly) ui::PageTransition currentTransition; 475 @property(nonatomic, readonly) ui::PageTransition currentTransition;
474 // Returns the referrer for current navigation item. May be empty. 476 // Returns the referrer for current navigation item. May be empty.
475 @property(nonatomic, readonly) web::Referrer currentNavItemReferrer; 477 @property(nonatomic, readonly) web::Referrer currentNavItemReferrer;
476 // The HTTP headers associated with the current navigation item. These are nil 478 // The HTTP headers associated with the current navigation item. These are nil
477 // unless the request was a POST. 479 // unless the request was a POST.
478 @property(nonatomic, readonly) NSDictionary* currentHTTPHeaders; 480 @property(nonatomic, readonly) NSDictionary* currentHTTPHeaders;
479 481
480 // Requires page reconstruction if |item| has a non-NONE UserAgentType and it 482 // Updates the user agent used to make HTTP request if |item|'s user agent type
481 // differs from that of |fromItem|. 483 // is not NONE and has changed.
482 - (void)updateDesktopUserAgentForItem:(web::NavigationItem*)item 484 - (void)updateUserAgentForItem:(web::NavigationItem*)item
Eugene But (OOO till 7-30) 2017/04/12 18:22:02 updateWebViewUserAgentFromItem: ?
liaoyuke 2017/04/12 19:17:59 Thanks! that's even better, and after looking at t
483 previousUserAgentType:(web::UserAgentType)userAgentType; 485 previousUserAgentType:(web::UserAgentType)previousUserAgentType;
484 486
485 // Removes the container view from the hierarchy and resets the ivar. 487 // Removes the container view from the hierarchy and resets the ivar.
486 - (void)resetContainerView; 488 - (void)resetContainerView;
487 // Called when the web page has changed document and/or URL, and so the page 489 // Called when the web page has changed document and/or URL, and so the page
488 // navigation should be reported to the delegate, and internal state updated to 490 // navigation should be reported to the delegate, and internal state updated to
489 // reflect the fact that the navigation has occurred. 491 // reflect the fact that the navigation has occurred.
490 // TODO(stuartmorgan): The code conflates URL changes and document object 492 // TODO(stuartmorgan): The code conflates URL changes and document object
491 // changes; the two need to be separated and handled differently. 493 // changes; the two need to be separated and handled differently.
492 - (void)webPageChanged; 494 - (void)webPageChanged;
493 // Resets any state that is associated with a specific document object (e.g., 495 // Resets any state that is associated with a specific document object (e.g.,
(...skipping 1115 matching lines...) Expand 10 before | Expand all | Expand 10 after
1609 [self didFinishWithURL:targetURL loadSuccess:NO]; 1611 [self didFinishWithURL:targetURL loadSuccess:NO];
1610 return; 1612 return;
1611 } 1613 }
1612 1614
1613 // JavaScript should never be evaluated here. User-entered JS should be 1615 // JavaScript should never be evaluated here. User-entered JS should be
1614 // evaluated via stringByEvaluatingUserJavaScriptFromString. 1616 // evaluated via stringByEvaluatingUserJavaScriptFromString.
1615 DCHECK(!targetURL.SchemeIs(url::kJavaScriptScheme)); 1617 DCHECK(!targetURL.SchemeIs(url::kJavaScriptScheme));
1616 1618
1617 [self ensureWebViewCreated]; 1619 [self ensureWebViewCreated];
1618 1620
1621 NSString* userAgent = base::SysUTF8ToNSString(
1622 web::GetWebClient()->GetUserAgent(self.userAgentType));
1623 [_webView setCustomUserAgent:userAgent];
Eugene But (OOO till 7-30) 2017/04/12 18:22:02 Are there any performance implications in calling
liaoyuke 2017/04/12 19:17:59 That is a very good question. I'm not sure how WKW
Eugene But (OOO till 7-30) 2017/04/13 00:34:06 WKWebView code is open source, so it should not be
liaoyuke 2017/04/13 23:03:53 Sorry, forgot to reply. I was only able to find th
Eugene But (OOO till 7-30) 2017/04/14 00:19:53 WKWebView source code: https://trac.webkit.org/bro
1624
1619 [self loadRequestForCurrentNavigationItem]; 1625 [self loadRequestForCurrentNavigationItem];
1620 } 1626 }
1621 1627
1622 - (void)updatePendingNavigationInfoFromNavigationAction: 1628 - (void)updatePendingNavigationInfoFromNavigationAction:
1623 (WKNavigationAction*)action { 1629 (WKNavigationAction*)action {
1624 if (action.targetFrame.mainFrame) { 1630 if (action.targetFrame.mainFrame) {
1625 _pendingNavigationInfo.reset( 1631 _pendingNavigationInfo.reset(
1626 [[CRWWebControllerPendingNavigationInfo alloc] init]); 1632 [[CRWWebControllerPendingNavigationInfo alloc] init]);
1627 [_pendingNavigationInfo 1633 [_pendingNavigationInfo
1628 setReferrer:[self referrerFromNavigationAction:action]]; 1634 setReferrer:[self referrerFromNavigationAction:action]];
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after
2023 [self recordStateInHistory]; 2029 [self recordStateInHistory];
2024 2030
2025 [self clearTransientContentView]; 2031 [self clearTransientContentView];
2026 2032
2027 // Update the user agent before attempting the navigation. 2033 // Update the user agent before attempting the navigation.
2028 web::NavigationItem* toItem = items[index].get(); 2034 web::NavigationItem* toItem = items[index].get();
2029 web::NavigationItem* previousItem = sessionController.currentItem; 2035 web::NavigationItem* previousItem = sessionController.currentItem;
2030 web::UserAgentType previousUserAgentType = 2036 web::UserAgentType previousUserAgentType =
2031 previousItem ? previousItem->GetUserAgentType() 2037 previousItem ? previousItem->GetUserAgentType()
2032 : web::UserAgentType::NONE; 2038 : web::UserAgentType::NONE;
2033 [self updateDesktopUserAgentForItem:toItem 2039 [self updateUserAgentForItem:toItem
2034 previousUserAgentType:previousUserAgentType]; 2040 previousUserAgentType:previousUserAgentType];
2035 2041
2036 BOOL sameDocumentNavigation = 2042 BOOL sameDocumentNavigation =
2037 [sessionController isSameDocumentNavigationBetweenItem:previousItem 2043 [sessionController isSameDocumentNavigationBetweenItem:previousItem
2038 andItem:toItem]; 2044 andItem:toItem];
2039 if (sameDocumentNavigation) { 2045 if (sameDocumentNavigation) {
2040 [sessionController goToItemAtIndex:index]; 2046 [sessionController goToItemAtIndex:index];
2041 [self updateHTML5HistoryState]; 2047 [self updateHTML5HistoryState];
2042 } else { 2048 } else {
2043 [sessionController discardNonCommittedItems]; 2049 [sessionController discardNonCommittedItems];
2044 [sessionController setPendingItemIndex:index]; 2050 [sessionController setPendingItemIndex:index];
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
2182 // The page should be closed if it was initiated by the DOM and there has been 2188 // The page should be closed if it was initiated by the DOM and there has been
2183 // no user interaction with the page since the web view was created, or if 2189 // no user interaction with the page since the web view was created, or if
2184 // the page has no navigation items, as occurs when an App Store link is 2190 // the page has no navigation items, as occurs when an App Store link is
2185 // opened from another application. 2191 // opened from another application.
2186 BOOL rendererInitiatedWithoutInteraction = 2192 BOOL rendererInitiatedWithoutInteraction =
2187 self.hasOpener && !_userInteractedWithWebController; 2193 self.hasOpener && !_userInteractedWithWebController;
2188 BOOL noNavigationItems = !(self.navigationManagerImpl->GetItemCount()); 2194 BOOL noNavigationItems = !(self.navigationManagerImpl->GetItemCount());
2189 return rendererInitiatedWithoutInteraction || noNavigationItems; 2195 return rendererInitiatedWithoutInteraction || noNavigationItems;
2190 } 2196 }
2191 2197
2192 - (BOOL)usesDesktopUserAgent { 2198 - (web::UserAgentType)userAgentType {
2193 web::NavigationItem* item = self.currentNavItem; 2199 if (!self.currentNavItem)
Eugene But (OOO till 7-30) 2017/04/12 18:22:02 Old code with ternary could be faster
liaoyuke 2017/04/12 19:17:59 Done.
2194 return item && item->GetUserAgentType() == web::UserAgentType::DESKTOP; 2200 return web::UserAgentType::MOBILE;
2201
2202 return self.currentNavItem->GetUserAgentType();
2195 } 2203 }
2196 2204
2197 - (web::MojoFacade*)mojoFacade { 2205 - (web::MojoFacade*)mojoFacade {
2198 if (!_mojoFacade) { 2206 if (!_mojoFacade) {
2199 service_manager::mojom::InterfaceProvider* interfaceProvider = 2207 service_manager::mojom::InterfaceProvider* interfaceProvider =
2200 _webStateImpl->GetMojoInterfaceRegistry(); 2208 _webStateImpl->GetMojoInterfaceRegistry();
2201 _mojoFacade.reset(new web::MojoFacade(interfaceProvider, self)); 2209 _mojoFacade.reset(new web::MojoFacade(interfaceProvider, self));
2202 } 2210 }
2203 return _mojoFacade.get(); 2211 return _mojoFacade.get();
2204 } 2212 }
(...skipping 18 matching lines...) Expand all
2223 [delegate webController:strongSelf didLoadPassKitObject:data]; 2231 [delegate webController:strongSelf didLoadPassKitObject:data];
2224 } 2232 }
2225 }; 2233 };
2226 web::BrowserState* browserState = self.webStateImpl->GetBrowserState(); 2234 web::BrowserState* browserState = self.webStateImpl->GetBrowserState();
2227 _passKitDownloader.reset([[CRWPassKitDownloader alloc] 2235 _passKitDownloader.reset([[CRWPassKitDownloader alloc]
2228 initWithContextGetter:browserState->GetRequestContext() 2236 initWithContextGetter:browserState->GetRequestContext()
2229 completionHandler:passKitCompletion]); 2237 completionHandler:passKitCompletion]);
2230 return _passKitDownloader.get(); 2238 return _passKitDownloader.get();
2231 } 2239 }
2232 2240
2233 - (void)updateDesktopUserAgentForItem:(web::NavigationItem*)item 2241 - (void)updateUserAgentForItem:(web::NavigationItem*)item
2234 previousUserAgentType:(web::UserAgentType)userAgentType { 2242 previousUserAgentType:(web::UserAgentType)previousUserAgentType {
2235 if (!item) 2243 if (!item)
2236 return; 2244 return;
2237 web::UserAgentType itemUserAgentType = item->GetUserAgentType(); 2245 web::UserAgentType currentUserAgentType = item->GetUserAgentType();
Eugene But (OOO till 7-30) 2017/04/12 18:22:02 This isn't "current", right? Current is one that i
liaoyuke 2017/04/12 19:17:59 Done.
2238 if (itemUserAgentType == web::UserAgentType::NONE) 2246 if (currentUserAgentType == web::UserAgentType::NONE)
2239 return; 2247 return;
2240 if (itemUserAgentType != userAgentType) 2248
2241 [self requirePageReconstruction]; 2249 if (currentUserAgentType != previousUserAgentType) {
2250 NSString* currentUserAgent = base::SysUTF8ToNSString(
Eugene But (OOO till 7-30) 2017/04/12 18:22:02 ditto
liaoyuke 2017/04/12 19:17:59 Done.
2251 web::GetWebClient()->GetUserAgent(currentUserAgentType));
2252 [_webView setCustomUserAgent:currentUserAgent];
2253 }
2242 } 2254 }
2243 2255
2244 #pragma mark - 2256 #pragma mark -
2245 #pragma mark CRWWebControllerContainerViewDelegate 2257 #pragma mark CRWWebControllerContainerViewDelegate
2246 2258
2247 - (CRWWebViewProxyImpl*)contentViewProxyForContainerView: 2259 - (CRWWebViewProxyImpl*)contentViewProxyForContainerView:
2248 (CRWWebControllerContainerView*)containerView { 2260 (CRWWebControllerContainerView*)containerView {
2249 return _webViewProxy.get(); 2261 return _webViewProxy.get();
2250 } 2262 }
2251 2263
(...skipping 1781 matching lines...) Expand 10 before | Expand all | Expand 10 after
4033 scrollView:self.webScrollView]); 4045 scrollView:self.webScrollView]);
4034 [_containerView displayWebViewContentView:webViewContentView]; 4046 [_containerView displayWebViewContentView:webViewContentView];
4035 } 4047 }
4036 } 4048 }
4037 4049
4038 - (WKWebView*)webViewWithConfiguration:(WKWebViewConfiguration*)config { 4050 - (WKWebView*)webViewWithConfiguration:(WKWebViewConfiguration*)config {
4039 // Do not attach the context menu controller immediately as the JavaScript 4051 // Do not attach the context menu controller immediately as the JavaScript
4040 // delegate must be specified. 4052 // delegate must be specified.
4041 return web::BuildWKWebView(CGRectZero, config, 4053 return web::BuildWKWebView(CGRectZero, config,
4042 self.webStateImpl->GetBrowserState(), 4054 self.webStateImpl->GetBrowserState(),
4043 self.usesDesktopUserAgent); 4055 self.userAgentType);
4044 } 4056 }
4045 4057
4046 - (void)setWebView:(WKWebView*)webView { 4058 - (void)setWebView:(WKWebView*)webView {
4047 DCHECK_NE(_webView.get(), webView); 4059 DCHECK_NE(_webView.get(), webView);
4048 4060
4049 // Unwind the old web view. 4061 // Unwind the old web view.
4050 // TODO(eugenebut): Remove CRWWKScriptMessageRouter once crbug.com/543374 is 4062 // TODO(eugenebut): Remove CRWWKScriptMessageRouter once crbug.com/543374 is
4051 // fixed. 4063 // fixed.
4052 CRWWKScriptMessageRouter* messageRouter = 4064 CRWWKScriptMessageRouter* messageRouter =
4053 [self webViewConfigurationProvider].GetScriptMessageRouter(); 4065 [self webViewConfigurationProvider].GetScriptMessageRouter();
(...skipping 1016 matching lines...) Expand 10 before | Expand all | Expand 10 after
5070 - (void)simulateLoadRequestWithURL:(const GURL&)URL { 5082 - (void)simulateLoadRequestWithURL:(const GURL&)URL {
5071 _lastRegisteredRequestURL = URL; 5083 _lastRegisteredRequestURL = URL;
5072 _loadPhase = web::LOAD_REQUESTED; 5084 _loadPhase = web::LOAD_REQUESTED;
5073 } 5085 }
5074 5086
5075 - (NSString*)referrerFromNavigationAction:(WKNavigationAction*)action { 5087 - (NSString*)referrerFromNavigationAction:(WKNavigationAction*)action {
5076 return [action.request valueForHTTPHeaderField:kReferrerHeaderName]; 5088 return [action.request valueForHTTPHeaderField:kReferrerHeaderName];
5077 } 5089 }
5078 5090
5079 @end 5091 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698