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

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

Issue 2811073005: Replace requirePageReconstruction with setCustomUserAgent (Closed)
Patch Set: Address comments 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 when navigating from
481 // differs from that of |fromItem|. 483 // |fromItem| to |toItem|. |toItem| must not be nullptr.
482 - (void)updateDesktopUserAgentForItem:(web::NavigationItem*)item 484 - (void)updateWebViewUserAgentFromItem:(web::NavigationItem*)fromItem
483 previousUserAgentType:(web::UserAgentType)userAgentType; 485 toItem:(web::NavigationItem*)toItem;
486
487 // Requires that the next load rebuild the UIWebView. This is expensive, and
kkhorimoto 2017/04/12 19:42:28 s/UIWebView/WKWebView.
liaoyuke 2017/04/12 22:45:33 maybe web view?
488 // should be used only in the case where something has changed that UIWebView
489 // only checks on creation, such that the whole object needs to be rebuilt.
490 // TODO(stuartmorgan): Merge this and reinitializeWebViewAndReload:. They are
491 // currently subtly different in terms of implementation, but are for
492 // fundamentally the same purpose.
493 - (void)requirePageReconstruction;
484 494
485 // Removes the container view from the hierarchy and resets the ivar. 495 // Removes the container view from the hierarchy and resets the ivar.
486 - (void)resetContainerView; 496 - (void)resetContainerView;
487 // Called when the web page has changed document and/or URL, and so the page 497 // 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 498 // navigation should be reported to the delegate, and internal state updated to
489 // reflect the fact that the navigation has occurred. 499 // reflect the fact that the navigation has occurred.
490 // TODO(stuartmorgan): The code conflates URL changes and document object 500 // TODO(stuartmorgan): The code conflates URL changes and document object
491 // changes; the two need to be separated and handled differently. 501 // changes; the two need to be separated and handled differently.
492 - (void)webPageChanged; 502 - (void)webPageChanged;
493 // Resets any state that is associated with a specific document object (e.g., 503 // 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]; 1619 [self didFinishWithURL:targetURL loadSuccess:NO];
1610 return; 1620 return;
1611 } 1621 }
1612 1622
1613 // JavaScript should never be evaluated here. User-entered JS should be 1623 // JavaScript should never be evaluated here. User-entered JS should be
1614 // evaluated via stringByEvaluatingUserJavaScriptFromString. 1624 // evaluated via stringByEvaluatingUserJavaScriptFromString.
1615 DCHECK(!targetURL.SchemeIs(url::kJavaScriptScheme)); 1625 DCHECK(!targetURL.SchemeIs(url::kJavaScriptScheme));
1616 1626
1617 [self ensureWebViewCreated]; 1627 [self ensureWebViewCreated];
1618 1628
1629 NSString* userAgent = base::SysUTF8ToNSString(
kkhorimoto 2017/04/12 19:42:27 Can we check whether the current NavigationItem ha
liaoyuke 2017/04/12 22:45:33 How about we directly compare web view's current c
1630 web::GetWebClient()->GetUserAgent(self.userAgentType));
1631 [_webView setCustomUserAgent:userAgent];
1632
1619 [self loadRequestForCurrentNavigationItem]; 1633 [self loadRequestForCurrentNavigationItem];
1620 } 1634 }
1621 1635
1622 - (void)updatePendingNavigationInfoFromNavigationAction: 1636 - (void)updatePendingNavigationInfoFromNavigationAction:
1623 (WKNavigationAction*)action { 1637 (WKNavigationAction*)action {
1624 if (action.targetFrame.mainFrame) { 1638 if (action.targetFrame.mainFrame) {
1625 _pendingNavigationInfo.reset( 1639 _pendingNavigationInfo.reset(
1626 [[CRWWebControllerPendingNavigationInfo alloc] init]); 1640 [[CRWWebControllerPendingNavigationInfo alloc] init]);
1627 [_pendingNavigationInfo 1641 [_pendingNavigationInfo
1628 setReferrer:[self referrerFromNavigationAction:action]]; 1642 setReferrer:[self referrerFromNavigationAction:action]];
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after
2019 return; 2033 return;
2020 } 2034 }
2021 2035
2022 if (!_webStateImpl->IsShowingWebInterstitial()) 2036 if (!_webStateImpl->IsShowingWebInterstitial())
2023 [self recordStateInHistory]; 2037 [self recordStateInHistory];
2024 2038
2025 [self clearTransientContentView]; 2039 [self clearTransientContentView];
2026 2040
2027 // Update the user agent before attempting the navigation. 2041 // Update the user agent before attempting the navigation.
2028 web::NavigationItem* toItem = items[index].get(); 2042 web::NavigationItem* toItem = items[index].get();
2029 web::NavigationItem* previousItem = sessionController.currentItem; 2043 web::NavigationItem* fromItem = sessionController.currentItem;
2030 web::UserAgentType previousUserAgentType = 2044 [self updateWebViewUserAgentFromItem:fromItem toItem:toItem];
2031 previousItem ? previousItem->GetUserAgentType()
2032 : web::UserAgentType::NONE;
2033 [self updateDesktopUserAgentForItem:toItem
2034 previousUserAgentType:previousUserAgentType];
2035 2045
2036 BOOL sameDocumentNavigation = 2046 BOOL sameDocumentNavigation =
2037 [sessionController isSameDocumentNavigationBetweenItem:previousItem 2047 [sessionController isSameDocumentNavigationBetweenItem:fromItem
2038 andItem:toItem]; 2048 andItem:toItem];
2039 if (sameDocumentNavigation) { 2049 if (sameDocumentNavigation) {
2040 [sessionController goToItemAtIndex:index]; 2050 [sessionController goToItemAtIndex:index];
2041 [self updateHTML5HistoryState]; 2051 [self updateHTML5HistoryState];
2042 } else { 2052 } else {
2043 [sessionController discardNonCommittedItems]; 2053 [sessionController discardNonCommittedItems];
2044 [sessionController setPendingItemIndex:index]; 2054 [sessionController setPendingItemIndex:index];
2045 2055
2046 web::NavigationItemImpl* pendingItem = sessionController.pendingItem; 2056 web::NavigationItemImpl* pendingItem = sessionController.pendingItem;
2047 pendingItem->SetTransitionType(ui::PageTransitionFromInt( 2057 pendingItem->SetTransitionType(ui::PageTransitionFromInt(
(...skipping 134 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 2192 // 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 2193 // 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 2194 // the page has no navigation items, as occurs when an App Store link is
2185 // opened from another application. 2195 // opened from another application.
2186 BOOL rendererInitiatedWithoutInteraction = 2196 BOOL rendererInitiatedWithoutInteraction =
2187 self.hasOpener && !_userInteractedWithWebController; 2197 self.hasOpener && !_userInteractedWithWebController;
2188 BOOL noNavigationItems = !(self.navigationManagerImpl->GetItemCount()); 2198 BOOL noNavigationItems = !(self.navigationManagerImpl->GetItemCount());
2189 return rendererInitiatedWithoutInteraction || noNavigationItems; 2199 return rendererInitiatedWithoutInteraction || noNavigationItems;
2190 } 2200 }
2191 2201
2192 - (BOOL)usesDesktopUserAgent { 2202 - (web::UserAgentType)userAgentType {
2193 web::NavigationItem* item = self.currentNavItem; 2203 web::NavigationItem* item = self.currentNavItem;
2194 return item && item->GetUserAgentType() == web::UserAgentType::DESKTOP; 2204 return item ? item->GetUserAgentType() : web::UserAgentType::MOBILE;
2195 } 2205 }
2196 2206
2197 - (web::MojoFacade*)mojoFacade { 2207 - (web::MojoFacade*)mojoFacade {
2198 if (!_mojoFacade) { 2208 if (!_mojoFacade) {
2199 service_manager::mojom::InterfaceProvider* interfaceProvider = 2209 service_manager::mojom::InterfaceProvider* interfaceProvider =
2200 _webStateImpl->GetMojoInterfaceRegistry(); 2210 _webStateImpl->GetMojoInterfaceRegistry();
2201 _mojoFacade.reset(new web::MojoFacade(interfaceProvider, self)); 2211 _mojoFacade.reset(new web::MojoFacade(interfaceProvider, self));
2202 } 2212 }
2203 return _mojoFacade.get(); 2213 return _mojoFacade.get();
2204 } 2214 }
(...skipping 18 matching lines...) Expand all
2223 [delegate webController:strongSelf didLoadPassKitObject:data]; 2233 [delegate webController:strongSelf didLoadPassKitObject:data];
2224 } 2234 }
2225 }; 2235 };
2226 web::BrowserState* browserState = self.webStateImpl->GetBrowserState(); 2236 web::BrowserState* browserState = self.webStateImpl->GetBrowserState();
2227 _passKitDownloader.reset([[CRWPassKitDownloader alloc] 2237 _passKitDownloader.reset([[CRWPassKitDownloader alloc]
2228 initWithContextGetter:browserState->GetRequestContext() 2238 initWithContextGetter:browserState->GetRequestContext()
2229 completionHandler:passKitCompletion]); 2239 completionHandler:passKitCompletion]);
2230 return _passKitDownloader.get(); 2240 return _passKitDownloader.get();
2231 } 2241 }
2232 2242
2233 - (void)updateDesktopUserAgentForItem:(web::NavigationItem*)item 2243 - (void)updateWebViewUserAgentFromItem:(web::NavigationItem*)fromItem
2234 previousUserAgentType:(web::UserAgentType)userAgentType { 2244 toItem:(web::NavigationItem*)toItem {
2235 if (!item) 2245 DCHECK(toItem);
2246
2247 web::UserAgentType toUserAgentType = toItem->GetUserAgentType();
2248 if (toUserAgentType == web::UserAgentType::NONE)
2236 return; 2249 return;
2237 web::UserAgentType itemUserAgentType = item->GetUserAgentType(); 2250
2238 if (itemUserAgentType == web::UserAgentType::NONE) 2251 if (!fromItem || fromItem->GetUserAgentType() != toUserAgentType) {
2239 return; 2252 NSString* toUserAgent = base::SysUTF8ToNSString(
2240 if (itemUserAgentType != userAgentType) 2253 web::GetWebClient()->GetUserAgent(toUserAgentType));
2241 [self requirePageReconstruction]; 2254 [_webView setCustomUserAgent:toUserAgent];
2255 }
2242 } 2256 }
2243 2257
2244 #pragma mark - 2258 #pragma mark -
2245 #pragma mark CRWWebControllerContainerViewDelegate 2259 #pragma mark CRWWebControllerContainerViewDelegate
2246 2260
2247 - (CRWWebViewProxyImpl*)contentViewProxyForContainerView: 2261 - (CRWWebViewProxyImpl*)contentViewProxyForContainerView:
2248 (CRWWebControllerContainerView*)containerView { 2262 (CRWWebControllerContainerView*)containerView {
2249 return _webViewProxy.get(); 2263 return _webViewProxy.get();
2250 } 2264 }
2251 2265
(...skipping 1781 matching lines...) Expand 10 before | Expand all | Expand 10 after
4033 scrollView:self.webScrollView]); 4047 scrollView:self.webScrollView]);
4034 [_containerView displayWebViewContentView:webViewContentView]; 4048 [_containerView displayWebViewContentView:webViewContentView];
4035 } 4049 }
4036 } 4050 }
4037 4051
4038 - (WKWebView*)webViewWithConfiguration:(WKWebViewConfiguration*)config { 4052 - (WKWebView*)webViewWithConfiguration:(WKWebViewConfiguration*)config {
4039 // Do not attach the context menu controller immediately as the JavaScript 4053 // Do not attach the context menu controller immediately as the JavaScript
4040 // delegate must be specified. 4054 // delegate must be specified.
4041 return web::BuildWKWebView(CGRectZero, config, 4055 return web::BuildWKWebView(CGRectZero, config,
4042 self.webStateImpl->GetBrowserState(), 4056 self.webStateImpl->GetBrowserState(),
4043 self.usesDesktopUserAgent); 4057 self.userAgentType);
4044 } 4058 }
4045 4059
4046 - (void)setWebView:(WKWebView*)webView { 4060 - (void)setWebView:(WKWebView*)webView {
4047 DCHECK_NE(_webView.get(), webView); 4061 DCHECK_NE(_webView.get(), webView);
4048 4062
4049 // Unwind the old web view. 4063 // Unwind the old web view.
4050 // TODO(eugenebut): Remove CRWWKScriptMessageRouter once crbug.com/543374 is 4064 // TODO(eugenebut): Remove CRWWKScriptMessageRouter once crbug.com/543374 is
4051 // fixed. 4065 // fixed.
4052 CRWWKScriptMessageRouter* messageRouter = 4066 CRWWKScriptMessageRouter* messageRouter =
4053 [self webViewConfigurationProvider].GetScriptMessageRouter(); 4067 [self webViewConfigurationProvider].GetScriptMessageRouter();
(...skipping 1016 matching lines...) Expand 10 before | Expand all | Expand 10 after
5070 - (void)simulateLoadRequestWithURL:(const GURL&)URL { 5084 - (void)simulateLoadRequestWithURL:(const GURL&)URL {
5071 _lastRegisteredRequestURL = URL; 5085 _lastRegisteredRequestURL = URL;
5072 _loadPhase = web::LOAD_REQUESTED; 5086 _loadPhase = web::LOAD_REQUESTED;
5073 } 5087 }
5074 5088
5075 - (NSString*)referrerFromNavigationAction:(WKNavigationAction*)action { 5089 - (NSString*)referrerFromNavigationAction:(WKNavigationAction*)action {
5076 return [action.request valueForHTTPHeaderField:kReferrerHeaderName]; 5090 return [action.request valueForHTTPHeaderField:kReferrerHeaderName];
5077 } 5091 }
5078 5092
5079 @end 5093 @end
OLDNEW
« no previous file with comments | « ios/web/web_state/ui/crw_web_controller.h ('k') | ios/web/web_state/web_view_internal_creation_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698