Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 web view's user agent according to |userAgentType|. Is no-op if |
| 481 // differs from that of |fromItem|. | 483 // |userAgentType| is NONE or web view's user agent will not change. |
| 482 - (void)updateDesktopUserAgentForItem:(web::NavigationItem*)item | 484 - (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.
| |
| 483 previousUserAgentType:(web::UserAgentType)userAgentType; | 485 |
| 486 // Requires that the next load rebuild the web view. This is expensive, and | |
| 487 // should be used only in the case where something has changed that web view | |
| 488 // only checks on creation, such that the whole object needs to be rebuilt. | |
| 489 // TODO(stuartmorgan): Merge this and reinitializeWebViewAndReload:. They are | |
| 490 // currently subtly different in terms of implementation, but are for | |
| 491 // fundamentally the same purpose. | |
| 492 - (void)requirePageReconstruction; | |
| 484 | 493 |
| 485 // Removes the container view from the hierarchy and resets the ivar. | 494 // Removes the container view from the hierarchy and resets the ivar. |
| 486 - (void)resetContainerView; | 495 - (void)resetContainerView; |
| 487 // Called when the web page has changed document and/or URL, and so the page | 496 // 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 | 497 // navigation should be reported to the delegate, and internal state updated to |
| 489 // reflect the fact that the navigation has occurred. | 498 // reflect the fact that the navigation has occurred. |
| 490 // TODO(stuartmorgan): The code conflates URL changes and document object | 499 // TODO(stuartmorgan): The code conflates URL changes and document object |
| 491 // changes; the two need to be separated and handled differently. | 500 // changes; the two need to be separated and handled differently. |
| 492 - (void)webPageChanged; | 501 - (void)webPageChanged; |
| 493 // Resets any state that is associated with a specific document object (e.g., | 502 // Resets any state that is associated with a specific document object (e.g., |
| (...skipping 1114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1608 if (!targetURL.is_valid()) { | 1617 if (!targetURL.is_valid()) { |
| 1609 [self didFinishWithURL:targetURL loadSuccess:NO]; | 1618 [self didFinishWithURL:targetURL loadSuccess:NO]; |
| 1610 return; | 1619 return; |
| 1611 } | 1620 } |
| 1612 | 1621 |
| 1613 // JavaScript should never be evaluated here. User-entered JS should be | 1622 // JavaScript should never be evaluated here. User-entered JS should be |
| 1614 // evaluated via stringByEvaluatingUserJavaScriptFromString. | 1623 // evaluated via stringByEvaluatingUserJavaScriptFromString. |
| 1615 DCHECK(!targetURL.SchemeIs(url::kJavaScriptScheme)); | 1624 DCHECK(!targetURL.SchemeIs(url::kJavaScriptScheme)); |
| 1616 | 1625 |
| 1617 [self ensureWebViewCreated]; | 1626 [self ensureWebViewCreated]; |
| 1627 [self updateWebViewUserAgentIfChanged:self.userAgentType]; | |
| 1618 | 1628 |
| 1619 [self loadRequestForCurrentNavigationItem]; | 1629 [self loadRequestForCurrentNavigationItem]; |
| 1620 } | 1630 } |
| 1621 | 1631 |
| 1622 - (void)updatePendingNavigationInfoFromNavigationAction: | 1632 - (void)updatePendingNavigationInfoFromNavigationAction: |
| 1623 (WKNavigationAction*)action { | 1633 (WKNavigationAction*)action { |
| 1624 if (action.targetFrame.mainFrame) { | 1634 if (action.targetFrame.mainFrame) { |
| 1625 _pendingNavigationInfo.reset( | 1635 _pendingNavigationInfo.reset( |
| 1626 [[CRWWebControllerPendingNavigationInfo alloc] init]); | 1636 [[CRWWebControllerPendingNavigationInfo alloc] init]); |
| 1627 [_pendingNavigationInfo | 1637 [_pendingNavigationInfo |
| (...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2019 return; | 2029 return; |
| 2020 } | 2030 } |
| 2021 | 2031 |
| 2022 if (!_webStateImpl->IsShowingWebInterstitial()) | 2032 if (!_webStateImpl->IsShowingWebInterstitial()) |
| 2023 [self recordStateInHistory]; | 2033 [self recordStateInHistory]; |
| 2024 | 2034 |
| 2025 [self clearTransientContentView]; | 2035 [self clearTransientContentView]; |
| 2026 | 2036 |
| 2027 // Update the user agent before attempting the navigation. | 2037 // Update the user agent before attempting the navigation. |
| 2028 web::NavigationItem* toItem = items[index].get(); | 2038 web::NavigationItem* toItem = items[index].get(); |
| 2029 web::NavigationItem* previousItem = sessionController.currentItem; | 2039 [self updateWebViewUserAgentIfChanged:toItem->GetUserAgentType()]; |
| 2030 web::UserAgentType previousUserAgentType = | |
| 2031 previousItem ? previousItem->GetUserAgentType() | |
| 2032 : web::UserAgentType::NONE; | |
| 2033 [self updateDesktopUserAgentForItem:toItem | |
| 2034 previousUserAgentType:previousUserAgentType]; | |
| 2035 | 2040 |
| 2041 web::NavigationItem* fromItem = sessionController.currentItem; | |
| 2036 BOOL sameDocumentNavigation = | 2042 BOOL sameDocumentNavigation = |
| 2037 [sessionController isSameDocumentNavigationBetweenItem:previousItem | 2043 [sessionController isSameDocumentNavigationBetweenItem:fromItem |
| 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]; |
| 2045 | 2051 |
| 2046 web::NavigationItemImpl* pendingItem = sessionController.pendingItem; | 2052 web::NavigationItemImpl* pendingItem = sessionController.pendingItem; |
| 2047 pendingItem->SetTransitionType(ui::PageTransitionFromInt( | 2053 pendingItem->SetTransitionType(ui::PageTransitionFromInt( |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 web::NavigationItem* item = self.currentNavItem; |
| 2194 return item && item->GetUserAgentType() == web::UserAgentType::DESKTOP; | 2200 return item ? item->GetUserAgentType() : web::UserAgentType::MOBILE; |
| 2195 } | 2201 } |
| 2196 | 2202 |
| 2197 - (web::MojoFacade*)mojoFacade { | 2203 - (web::MojoFacade*)mojoFacade { |
| 2198 if (!_mojoFacade) { | 2204 if (!_mojoFacade) { |
| 2199 service_manager::mojom::InterfaceProvider* interfaceProvider = | 2205 service_manager::mojom::InterfaceProvider* interfaceProvider = |
| 2200 _webStateImpl->GetMojoInterfaceRegistry(); | 2206 _webStateImpl->GetMojoInterfaceRegistry(); |
| 2201 _mojoFacade.reset(new web::MojoFacade(interfaceProvider, self)); | 2207 _mojoFacade.reset(new web::MojoFacade(interfaceProvider, self)); |
| 2202 } | 2208 } |
| 2203 return _mojoFacade.get(); | 2209 return _mojoFacade.get(); |
| 2204 } | 2210 } |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 2223 [delegate webController:strongSelf didLoadPassKitObject:data]; | 2229 [delegate webController:strongSelf didLoadPassKitObject:data]; |
| 2224 } | 2230 } |
| 2225 }; | 2231 }; |
| 2226 web::BrowserState* browserState = self.webStateImpl->GetBrowserState(); | 2232 web::BrowserState* browserState = self.webStateImpl->GetBrowserState(); |
| 2227 _passKitDownloader.reset([[CRWPassKitDownloader alloc] | 2233 _passKitDownloader.reset([[CRWPassKitDownloader alloc] |
| 2228 initWithContextGetter:browserState->GetRequestContext() | 2234 initWithContextGetter:browserState->GetRequestContext() |
| 2229 completionHandler:passKitCompletion]); | 2235 completionHandler:passKitCompletion]); |
| 2230 return _passKitDownloader.get(); | 2236 return _passKitDownloader.get(); |
| 2231 } | 2237 } |
| 2232 | 2238 |
| 2233 - (void)updateDesktopUserAgentForItem:(web::NavigationItem*)item | 2239 - (void)updateWebViewUserAgentIfChanged:(web::UserAgentType)userAgentType { |
| 2234 previousUserAgentType:(web::UserAgentType)userAgentType { | 2240 if (userAgentType == web::UserAgentType::NONE) |
| 2235 if (!item) | |
| 2236 return; | 2241 return; |
| 2237 web::UserAgentType itemUserAgentType = item->GetUserAgentType(); | 2242 |
| 2238 if (itemUserAgentType == web::UserAgentType::NONE) | 2243 NSString* userAgent = |
| 2239 return; | 2244 base::SysUTF8ToNSString(web::GetWebClient()->GetUserAgent(userAgentType)); |
| 2240 if (itemUserAgentType != userAgentType) | 2245 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
| |
| 2241 [self requirePageReconstruction]; | 2246 [_webView setCustomUserAgent:userAgent]; |
| 2242 } | 2247 } |
| 2243 | 2248 |
| 2244 #pragma mark - | 2249 #pragma mark - |
| 2245 #pragma mark CRWWebControllerContainerViewDelegate | 2250 #pragma mark CRWWebControllerContainerViewDelegate |
| 2246 | 2251 |
| 2247 - (CRWWebViewProxyImpl*)contentViewProxyForContainerView: | 2252 - (CRWWebViewProxyImpl*)contentViewProxyForContainerView: |
| 2248 (CRWWebControllerContainerView*)containerView { | 2253 (CRWWebControllerContainerView*)containerView { |
| 2249 return _webViewProxy.get(); | 2254 return _webViewProxy.get(); |
| 2250 } | 2255 } |
| 2251 | 2256 |
| (...skipping 1781 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4033 scrollView:self.webScrollView]); | 4038 scrollView:self.webScrollView]); |
| 4034 [_containerView displayWebViewContentView:webViewContentView]; | 4039 [_containerView displayWebViewContentView:webViewContentView]; |
| 4035 } | 4040 } |
| 4036 } | 4041 } |
| 4037 | 4042 |
| 4038 - (WKWebView*)webViewWithConfiguration:(WKWebViewConfiguration*)config { | 4043 - (WKWebView*)webViewWithConfiguration:(WKWebViewConfiguration*)config { |
| 4039 // Do not attach the context menu controller immediately as the JavaScript | 4044 // Do not attach the context menu controller immediately as the JavaScript |
| 4040 // delegate must be specified. | 4045 // delegate must be specified. |
| 4041 return web::BuildWKWebView(CGRectZero, config, | 4046 return web::BuildWKWebView(CGRectZero, config, |
| 4042 self.webStateImpl->GetBrowserState(), | 4047 self.webStateImpl->GetBrowserState(), |
| 4043 self.usesDesktopUserAgent); | 4048 self.userAgentType); |
| 4044 } | 4049 } |
| 4045 | 4050 |
| 4046 - (void)setWebView:(WKWebView*)webView { | 4051 - (void)setWebView:(WKWebView*)webView { |
| 4047 DCHECK_NE(_webView.get(), webView); | 4052 DCHECK_NE(_webView.get(), webView); |
| 4048 | 4053 |
| 4049 // Unwind the old web view. | 4054 // Unwind the old web view. |
| 4050 // TODO(eugenebut): Remove CRWWKScriptMessageRouter once crbug.com/543374 is | 4055 // TODO(eugenebut): Remove CRWWKScriptMessageRouter once crbug.com/543374 is |
| 4051 // fixed. | 4056 // fixed. |
| 4052 CRWWKScriptMessageRouter* messageRouter = | 4057 CRWWKScriptMessageRouter* messageRouter = |
| 4053 [self webViewConfigurationProvider].GetScriptMessageRouter(); | 4058 [self webViewConfigurationProvider].GetScriptMessageRouter(); |
| (...skipping 1016 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 5070 - (void)simulateLoadRequestWithURL:(const GURL&)URL { | 5075 - (void)simulateLoadRequestWithURL:(const GURL&)URL { |
| 5071 _lastRegisteredRequestURL = URL; | 5076 _lastRegisteredRequestURL = URL; |
| 5072 _loadPhase = web::LOAD_REQUESTED; | 5077 _loadPhase = web::LOAD_REQUESTED; |
| 5073 } | 5078 } |
| 5074 | 5079 |
| 5075 - (NSString*)referrerFromNavigationAction:(WKNavigationAction*)action { | 5080 - (NSString*)referrerFromNavigationAction:(WKNavigationAction*)action { |
| 5076 return [action.request valueForHTTPHeaderField:kReferrerHeaderName]; | 5081 return [action.request valueForHTTPHeaderField:kReferrerHeaderName]; |
| 5077 } | 5082 } |
| 5078 | 5083 |
| 5079 @end | 5084 @end |
| OLD | NEW |