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 19 matching lines...) Expand all Loading... | |
30 #import "base/mac/scoped_nsobject.h" | 30 #import "base/mac/scoped_nsobject.h" |
31 #include "base/memory/ptr_util.h" | 31 #include "base/memory/ptr_util.h" |
32 #include "base/metrics/histogram.h" | 32 #include "base/metrics/histogram.h" |
33 #include "base/metrics/user_metrics.h" | 33 #include "base/metrics/user_metrics.h" |
34 #include "base/metrics/user_metrics_action.h" | 34 #include "base/metrics/user_metrics_action.h" |
35 #include "base/strings/string_util.h" | 35 #include "base/strings/string_util.h" |
36 #include "base/strings/sys_string_conversions.h" | 36 #include "base/strings/sys_string_conversions.h" |
37 #include "base/strings/utf_string_conversions.h" | 37 #include "base/strings/utf_string_conversions.h" |
38 #include "base/time/time.h" | 38 #include "base/time/time.h" |
39 #include "base/values.h" | 39 #include "base/values.h" |
40 #include "components/url_formatter/url_formatter.h" | |
41 #import "ios/net/http_response_headers_util.h" | 40 #import "ios/net/http_response_headers_util.h" |
42 #import "ios/net/nsurlrequest_util.h" | 41 #import "ios/net/nsurlrequest_util.h" |
43 #include "ios/web/history_state_util.h" | 42 #include "ios/web/history_state_util.h" |
44 #import "ios/web/interstitials/web_interstitial_impl.h" | 43 #import "ios/web/interstitials/web_interstitial_impl.h" |
45 #import "ios/web/navigation/crw_session_certificate_policy_manager.h" | 44 #import "ios/web/navigation/crw_session_certificate_policy_manager.h" |
46 #import "ios/web/navigation/crw_session_controller.h" | 45 #import "ios/web/navigation/crw_session_controller.h" |
47 #import "ios/web/navigation/crw_session_entry.h" | 46 #import "ios/web/navigation/crw_session_entry.h" |
48 #import "ios/web/navigation/navigation_item_impl.h" | 47 #import "ios/web/navigation/navigation_item_impl.h" |
49 #import "ios/web/navigation/navigation_manager_impl.h" | 48 #import "ios/web/navigation/navigation_manager_impl.h" |
50 #include "ios/web/net/cert_host_pair.h" | 49 #include "ios/web/net/cert_host_pair.h" |
(...skipping 15 matching lines...) Expand all Loading... | |
66 #include "ios/web/public/web_kit_constants.h" | 65 #include "ios/web/public/web_kit_constants.h" |
67 #import "ios/web/public/web_state/context_menu_params.h" | 66 #import "ios/web/public/web_state/context_menu_params.h" |
68 #include "ios/web/public/web_state/credential.h" | 67 #include "ios/web/public/web_state/credential.h" |
69 #import "ios/web/public/web_state/crw_web_controller_observer.h" | 68 #import "ios/web/public/web_state/crw_web_controller_observer.h" |
70 #import "ios/web/public/web_state/crw_web_view_scroll_view_proxy.h" | 69 #import "ios/web/public/web_state/crw_web_view_scroll_view_proxy.h" |
71 #include "ios/web/public/web_state/js/credential_util.h" | 70 #include "ios/web/public/web_state/js/credential_util.h" |
72 #import "ios/web/public/web_state/js/crw_js_injection_manager.h" | 71 #import "ios/web/public/web_state/js/crw_js_injection_manager.h" |
73 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" | 72 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
74 #include "ios/web/public/web_state/page_display_state.h" | 73 #include "ios/web/public/web_state/page_display_state.h" |
75 #import "ios/web/public/web_state/ui/crw_content_view.h" | 74 #import "ios/web/public/web_state/ui/crw_content_view.h" |
75 #import "ios/web/public/web_state/ui/crw_context_menu_delegate.h" | |
76 #import "ios/web/public/web_state/ui/crw_native_content.h" | 76 #import "ios/web/public/web_state/ui/crw_native_content.h" |
77 #import "ios/web/public/web_state/ui/crw_native_content_provider.h" | 77 #import "ios/web/public/web_state/ui/crw_native_content_provider.h" |
78 #import "ios/web/public/web_state/ui/crw_web_view_content_view.h" | 78 #import "ios/web/public/web_state/ui/crw_web_view_content_view.h" |
79 #include "ios/web/public/web_state/url_verification_constants.h" | 79 #include "ios/web/public/web_state/url_verification_constants.h" |
80 #import "ios/web/public/web_state/web_state.h" | 80 #import "ios/web/public/web_state/web_state.h" |
81 #include "ios/web/public/webui/web_ui_ios.h" | 81 #include "ios/web/public/webui/web_ui_ios.h" |
82 #import "ios/web/web_state/blocked_popup_info.h" | 82 #import "ios/web/web_state/blocked_popup_info.h" |
83 #import "ios/web/web_state/crw_pass_kit_downloader.h" | 83 #import "ios/web/web_state/crw_pass_kit_downloader.h" |
84 #import "ios/web/web_state/crw_web_view_proxy_impl.h" | 84 #import "ios/web/web_state/crw_web_view_proxy_impl.h" |
85 #import "ios/web/web_state/error_translation_util.h" | 85 #import "ios/web/web_state/error_translation_util.h" |
86 #import "ios/web/web_state/js/crw_js_plugin_placeholder_manager.h" | 86 #import "ios/web/web_state/js/crw_js_plugin_placeholder_manager.h" |
87 #import "ios/web/web_state/js/crw_js_post_request_loader.h" | 87 #import "ios/web/web_state/js/crw_js_post_request_loader.h" |
88 #import "ios/web/web_state/js/crw_js_window_id_manager.h" | 88 #import "ios/web/web_state/js/crw_js_window_id_manager.h" |
89 #import "ios/web/web_state/page_viewport_state.h" | 89 #import "ios/web/web_state/page_viewport_state.h" |
90 #import "ios/web/web_state/ui/crw_context_menu_controller.h" | |
90 #import "ios/web/web_state/ui/crw_swipe_recognizer_provider.h" | 91 #import "ios/web/web_state/ui/crw_swipe_recognizer_provider.h" |
91 #import "ios/web/web_state/ui/crw_web_controller.h" | 92 #import "ios/web/web_state/ui/crw_web_controller.h" |
92 #import "ios/web/web_state/ui/crw_web_controller_container_view.h" | 93 #import "ios/web/web_state/ui/crw_web_controller_container_view.h" |
93 #import "ios/web/web_state/ui/crw_wk_navigation_states.h" | 94 #import "ios/web/web_state/ui/crw_wk_navigation_states.h" |
94 #import "ios/web/web_state/ui/crw_wk_script_message_router.h" | 95 #import "ios/web/web_state/ui/crw_wk_script_message_router.h" |
95 #import "ios/web/web_state/ui/wk_back_forward_list_item_holder.h" | 96 #import "ios/web/web_state/ui/wk_back_forward_list_item_holder.h" |
96 #import "ios/web/web_state/ui/wk_web_view_configuration_provider.h" | 97 #import "ios/web/web_state/ui/wk_web_view_configuration_provider.h" |
97 #import "ios/web/web_state/web_controller_observer_bridge.h" | 98 #import "ios/web/web_state/web_controller_observer_bridge.h" |
98 #include "ios/web/web_state/web_state_facade_delegate.h" | 99 #include "ios/web/web_state/web_state_facade_delegate.h" |
99 #import "ios/web/web_state/web_state_impl.h" | 100 #import "ios/web/web_state/web_state_impl.h" |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
210 | 211 |
211 // States for external URL requests. This enum is used in UMA and | 212 // States for external URL requests. This enum is used in UMA and |
212 // entries should not be re-ordered or deleted. | 213 // entries should not be re-ordered or deleted. |
213 enum ExternalURLRequestStatus { | 214 enum ExternalURLRequestStatus { |
214 MAIN_FRAME_ALLOWED = 0, | 215 MAIN_FRAME_ALLOWED = 0, |
215 SUBFRAME_ALLOWED, | 216 SUBFRAME_ALLOWED, |
216 SUBFRAME_BLOCKED, | 217 SUBFRAME_BLOCKED, |
217 NUM_EXTERNAL_URL_REQUEST_STATUS | 218 NUM_EXTERNAL_URL_REQUEST_STATUS |
218 }; | 219 }; |
219 | 220 |
220 // Cancels touch events for the given gesture recognizer. | |
221 void CancelTouches(UIGestureRecognizer* gesture_recognizer) { | |
222 if (gesture_recognizer.enabled) { | |
223 gesture_recognizer.enabled = NO; | |
224 gesture_recognizer.enabled = YES; | |
225 } | |
226 } | |
227 | |
228 // Utility function for getting the source of NSErrors received by WKWebViews. | 221 // Utility function for getting the source of NSErrors received by WKWebViews. |
229 WKWebViewErrorSource WKWebViewErrorSourceFromError(NSError* error) { | 222 WKWebViewErrorSource WKWebViewErrorSourceFromError(NSError* error) { |
230 DCHECK(error); | 223 DCHECK(error); |
231 return static_cast<WKWebViewErrorSource>( | 224 return static_cast<WKWebViewErrorSource>( |
232 [error.userInfo[kWKWebViewErrorSourceKey] integerValue]); | 225 [error.userInfo[kWKWebViewErrorSourceKey] integerValue]); |
233 } | 226 } |
234 // Utility function for converting the WKWebViewErrorSource to the NSError | 227 // Utility function for converting the WKWebViewErrorSource to the NSError |
235 // received by WKWebViews. | 228 // received by WKWebViews. |
236 NSError* WKWebViewErrorWithSource(NSError* error, WKWebViewErrorSource source) { | 229 NSError* WKWebViewErrorWithSource(NSError* error, WKWebViewErrorSource source) { |
237 DCHECK(error); | 230 DCHECK(error); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
277 - (instancetype)init { | 270 - (instancetype)init { |
278 if ((self = [super init])) { | 271 if ((self = [super init])) { |
279 _propertyReleaser_CRWWebControllerPendingNavigationInfo.Init( | 272 _propertyReleaser_CRWWebControllerPendingNavigationInfo.Init( |
280 self, [CRWWebControllerPendingNavigationInfo class]); | 273 self, [CRWWebControllerPendingNavigationInfo class]); |
281 _navigationType = WKNavigationTypeOther; | 274 _navigationType = WKNavigationTypeOther; |
282 } | 275 } |
283 return self; | 276 return self; |
284 } | 277 } |
285 @end | 278 @end |
286 | 279 |
287 @interface CRWWebController ()<CRWNativeContentDelegate, | 280 @interface CRWWebController ()<CRWContextMenuDelegate, |
281 CRWContextMenuJavaScriptDelegate, | |
Eugene But (OOO till 7-30)
2017/01/13 18:23:42
Do you need this?
Olivier
2017/01/13 18:49:55
Done.
| |
282 CRWNativeContentDelegate, | |
288 CRWSSLStatusUpdaterDataSource, | 283 CRWSSLStatusUpdaterDataSource, |
289 CRWSSLStatusUpdaterDelegate, | 284 CRWSSLStatusUpdaterDelegate, |
290 CRWWebControllerContainerViewDelegate, | 285 CRWWebControllerContainerViewDelegate, |
291 CRWWebViewScrollViewProxyObserver, | 286 CRWWebViewScrollViewProxyObserver, |
292 WKNavigationDelegate, | 287 WKNavigationDelegate, |
293 WKUIDelegate> { | 288 WKUIDelegate> { |
294 base::WeakNSProtocol<id<CRWWebDelegate>> _delegate; | 289 base::WeakNSProtocol<id<CRWWebDelegate>> _delegate; |
295 base::WeakNSProtocol<id<CRWWebUserInterfaceDelegate>> _UIDelegate; | 290 base::WeakNSProtocol<id<CRWWebUserInterfaceDelegate>> _UIDelegate; |
296 base::WeakNSProtocol<id<CRWNativeContentProvider>> _nativeProvider; | 291 base::WeakNSProtocol<id<CRWNativeContentProvider>> _nativeProvider; |
297 base::WeakNSProtocol<id<CRWSwipeRecognizerProvider>> _swipeRecognizerProvider; | 292 base::WeakNSProtocol<id<CRWSwipeRecognizerProvider>> _swipeRecognizerProvider; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
356 // next creation. | 351 // next creation. |
357 BOOL _usePlaceholderOverlay; | 352 BOOL _usePlaceholderOverlay; |
358 // The next time the view is requested, reload the page (using the placeholder | 353 // The next time the view is requested, reload the page (using the placeholder |
359 // overlay until it's loaded). | 354 // overlay until it's loaded). |
360 BOOL _requireReloadOnDisplay; | 355 BOOL _requireReloadOnDisplay; |
361 // Overlay view used instead of webView. | 356 // Overlay view used instead of webView. |
362 base::scoped_nsobject<UIImageView> _placeholderOverlayView; | 357 base::scoped_nsobject<UIImageView> _placeholderOverlayView; |
363 // The touch tracking recognizer allowing us to decide if a navigation is | 358 // The touch tracking recognizer allowing us to decide if a navigation is |
364 // started by the user. | 359 // started by the user. |
365 base::scoped_nsobject<CRWTouchTrackingRecognizer> _touchTrackingRecognizer; | 360 base::scoped_nsobject<CRWTouchTrackingRecognizer> _touchTrackingRecognizer; |
366 // Long press recognizer that allows showing context menus. | 361 // The controller that tracks long press and check context menu trigger. |
367 base::scoped_nsobject<UILongPressGestureRecognizer> _contextMenuRecognizer; | 362 base::scoped_nsobject<CRWContextMenuController> _contextMenuController; |
368 // DOM element information for the point where the user made the last touch. | |
369 // Can be nil if has not been calculated yet. Precalculation is necessary | |
370 // because retreiving DOM element relies on async API so element info can not | |
371 // be built on demand. May contain the following keys: @"href", @"src", | |
372 // @"title", @"referrerPolicy". All values are strings. Used for showing | |
373 // context menus. | |
374 base::scoped_nsobject<NSDictionary> _DOMElementForLastTouch; | |
375 // Whether a click is in progress. | 363 // Whether a click is in progress. |
376 BOOL _clickInProgress; | 364 BOOL _clickInProgress; |
377 // Data on the recorded last user interaction. | 365 // Data on the recorded last user interaction. |
378 std::unique_ptr<web::UserInteractionEvent> _lastUserInteraction; | 366 std::unique_ptr<web::UserInteractionEvent> _lastUserInteraction; |
379 // YES if there has been user interaction with views owned by this controller. | 367 // YES if there has been user interaction with views owned by this controller. |
380 BOOL _userInteractedWithWebController; | 368 BOOL _userInteractedWithWebController; |
381 // The time of the last page transfer start, measured in seconds since Jan 1 | 369 // The time of the last page transfer start, measured in seconds since Jan 1 |
382 // 2001. | 370 // 2001. |
383 CFAbsoluteTime _lastTransferTimeInSeconds; | 371 CFAbsoluteTime _lastTransferTimeInSeconds; |
384 // Default URL (about:blank). | 372 // Default URL (about:blank). |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
728 - (void)prepareToApplyWebViewScrollZoomScale; | 716 - (void)prepareToApplyWebViewScrollZoomScale; |
729 // Calls the zoom-completion UIScrollViewDelegate callbacks on the web view. | 717 // Calls the zoom-completion UIScrollViewDelegate callbacks on the web view. |
730 // This is called after |-applyWebViewScrollZoomScaleFromScrollState:|. | 718 // This is called after |-applyWebViewScrollZoomScaleFromScrollState:|. |
731 - (void)finishApplyingWebViewScrollZoomScale; | 719 - (void)finishApplyingWebViewScrollZoomScale; |
732 // Sets zoom scale value for webview scroll view from |zoomState|. | 720 // Sets zoom scale value for webview scroll view from |zoomState|. |
733 - (void)applyWebViewScrollZoomScaleFromZoomState: | 721 - (void)applyWebViewScrollZoomScaleFromZoomState: |
734 (const web::PageZoomState&)zoomState; | 722 (const web::PageZoomState&)zoomState; |
735 // Sets scroll offset value for webview scroll view from |scrollState|. | 723 // Sets scroll offset value for webview scroll view from |scrollState|. |
736 - (void)applyWebViewScrollOffsetFromScrollState: | 724 - (void)applyWebViewScrollOffsetFromScrollState: |
737 (const web::PageScrollState&)scrollState; | 725 (const web::PageScrollState&)scrollState; |
738 // Asynchronously fetches full width of the rendered web page. | |
739 - (void)fetchWebPageWidthWithCompletionHandler:(void (^)(CGFloat))handler; | |
740 // Asynchronously fetches information about DOM element for the given point (in | |
741 // UIView coordinates). |handler| can not be nil. See |_DOMElementForLastTouch| | |
742 // for element format description. | |
743 - (void)fetchDOMElementAtPoint:(CGPoint)point | |
744 completionHandler:(void (^)(NSDictionary*))handler; | |
745 // Extracts context menu information from the given DOM element. | |
746 - (web::ContextMenuParams)contextMenuParamsForElement:(NSDictionary*)element; | |
747 // Sets the value of |_DOMElementForLastTouch|. | |
748 - (void)setDOMElementForLastTouch:(NSDictionary*)element; | |
749 // Called when the window has determined there was a long-press and context menu | |
750 // must be shown. | |
751 - (void)showContextMenu:(UIGestureRecognizer*)gestureRecognizer; | |
752 // Cancels all touch events in the web view (long presses, tapping, scrolling). | |
753 - (void)cancelAllTouches; | |
754 // Returns the referrer for the current page. | 726 // Returns the referrer for the current page. |
755 - (web::Referrer)currentReferrer; | 727 - (web::Referrer)currentReferrer; |
756 // Asynchronously returns the referrer policy for the current page. | 728 // Asynchronously returns the referrer policy for the current page. |
757 - (void)queryPageReferrerPolicy:(void (^)(NSString*))responseHandler; | 729 - (void)queryPageReferrerPolicy:(void (^)(NSString*))responseHandler; |
758 // Adds a new CRWSessionEntry with the given URL and state object to the history | 730 // Adds a new CRWSessionEntry with the given URL and state object to the history |
759 // stack. A state object is a serialized generic JavaScript object that contains | 731 // stack. A state object is a serialized generic JavaScript object that contains |
760 // details of the UI's state for a given CRWSessionEntry/URL. | 732 // details of the UI's state for a given CRWSessionEntry/URL. |
761 // TODO(stuartmorgan): Move the pushState/replaceState logic into | 733 // TODO(stuartmorgan): Move the pushState/replaceState logic into |
762 // NavigationManager. | 734 // NavigationManager. |
763 - (void)pushStateWithPageURL:(const GURL&)pageURL | 735 - (void)pushStateWithPageURL:(const GURL&)pageURL |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
982 - (void)createWebUIForURL:(const GURL&)URL; | 954 - (void)createWebUIForURL:(const GURL&)URL; |
983 // Clears WebUI, if one exists. | 955 // Clears WebUI, if one exists. |
984 - (void)clearWebUI; | 956 - (void)clearWebUI; |
985 | 957 |
986 @end | 958 @end |
987 | 959 |
988 namespace { | 960 namespace { |
989 | 961 |
990 NSString* const kReferrerHeaderName = @"Referer"; // [sic] | 962 NSString* const kReferrerHeaderName = @"Referer"; // [sic] |
991 | 963 |
992 // The long press detection duration must be shorter than the UIWebView's | |
993 // long click gesture recognizer's minimum duration. That is 0.55s. | |
994 // If our detection duration is shorter, our gesture recognizer will fire | |
995 // first, and if it fails the long click gesture (processed simultaneously) | |
996 // still is able to complete. | |
997 const NSTimeInterval kLongPressDurationSeconds = 0.55 - 0.1; | |
998 const CGFloat kLongPressMoveDeltaPixels = 10.0; | |
999 | |
1000 // The duration of the period following a screen touch during which the user is | 964 // The duration of the period following a screen touch during which the user is |
1001 // still considered to be interacting with the page. | 965 // still considered to be interacting with the page. |
1002 const NSTimeInterval kMaximumDelayForUserInteractionInSeconds = 2; | 966 const NSTimeInterval kMaximumDelayForUserInteractionInSeconds = 2; |
1003 | 967 |
1004 // URLs that are fed into UIWebView as history push/replace get escaped, | 968 // URLs that are fed into UIWebView as history push/replace get escaped, |
1005 // potentially changing their format. Code that attempts to determine whether a | 969 // potentially changing their format. Code that attempts to determine whether a |
1006 // URL hasn't changed can be confused by those differences though, so method | 970 // URL hasn't changed can be confused by those differences though, so method |
1007 // will round-trip a URL through the escaping process so that it can be adjusted | 971 // will round-trip a URL through the escaping process so that it can be adjusted |
1008 // pre-storing, to allow later comparisons to work as expected. | 972 // pre-storing, to allow later comparisons to work as expected. |
1009 GURL URLEscapedForHistory(const GURL& url) { | 973 GURL URLEscapedForHistory(const GURL& url) { |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1297 // Mark the destruction sequence has started, in case someone else holds a | 1261 // Mark the destruction sequence has started, in case someone else holds a |
1298 // strong reference and tries to continue using the tab. | 1262 // strong reference and tries to continue using the tab. |
1299 _isBeingDestroyed = YES; | 1263 _isBeingDestroyed = YES; |
1300 | 1264 |
1301 // Remove the web view now. Otherwise, delegate callbacks occur. | 1265 // Remove the web view now. Otherwise, delegate callbacks occur. |
1302 [self removeWebViewAllowingCachedReconstruction:NO]; | 1266 [self removeWebViewAllowingCachedReconstruction:NO]; |
1303 | 1267 |
1304 _webStateImpl = nullptr; | 1268 _webStateImpl = nullptr; |
1305 } | 1269 } |
1306 | 1270 |
1307 - (void)setDOMElementForLastTouch:(NSDictionary*)element { | |
1308 _DOMElementForLastTouch.reset([element copy]); | |
1309 } | |
1310 | |
1311 - (void)showContextMenu:(UIGestureRecognizer*)gestureRecognizer { | |
1312 // We don't want ongoing notification that the long press is held. | |
1313 if ([gestureRecognizer state] != UIGestureRecognizerStateBegan) | |
1314 return; | |
1315 | |
1316 if (![_DOMElementForLastTouch count]) | |
1317 return; | |
1318 | |
1319 web::ContextMenuParams params = | |
1320 [self contextMenuParamsForElement:_DOMElementForLastTouch.get()]; | |
1321 params.view.reset([_webView retain]); | |
1322 params.location = [gestureRecognizer locationInView:_webView]; | |
1323 if (self.webStateImpl->HandleContextMenu(params)) { | |
1324 // Cancelling all touches has the intended side effect of suppressing the | |
1325 // system's context menu. | |
1326 [self cancelAllTouches]; | |
1327 } | |
1328 } | |
1329 | |
1330 - (void)cancelAllTouches { | |
1331 // Disable web view scrolling. | |
1332 CancelTouches(self.webScrollView.panGestureRecognizer); | |
1333 | |
1334 // All user gestures are handled by a subview of web view scroll view | |
1335 // (WKContentView). | |
1336 for (UIView* subview in self.webScrollView.subviews) { | |
1337 for (UIGestureRecognizer* recognizer in subview.gestureRecognizers) { | |
1338 CancelTouches(recognizer); | |
1339 } | |
1340 } | |
1341 | |
1342 // Just disabling/enabling the gesture recognizers is not enough to suppress | |
1343 // the click handlers on the JS side. This JS performs the function of | |
1344 // suppressing these handlers on the JS side. | |
1345 NSString* suppressNextClick = @"__gCrWeb.suppressNextClick()"; | |
1346 [self executeJavaScript:suppressNextClick completionHandler:nil]; | |
1347 } | |
1348 | |
1349 // TODO(shreyasv): This code is shared with SnapshotManager. Remove this and add | 1271 // TODO(shreyasv): This code is shared with SnapshotManager. Remove this and add |
1350 // it as part of WebDelegate delegate API such that a default image is returned | 1272 // it as part of WebDelegate delegate API such that a default image is returned |
1351 // immediately. | 1273 // immediately. |
1352 + (UIImage*)defaultSnapshotImage { | 1274 + (UIImage*)defaultSnapshotImage { |
1353 static UIImage* defaultImage = nil; | 1275 static UIImage* defaultImage = nil; |
1354 | 1276 |
1355 if (!defaultImage) { | 1277 if (!defaultImage) { |
1356 CGRect frame = CGRectMake(0, 0, 2, 2); | 1278 CGRect frame = CGRectMake(0, 0, 2, 2); |
1357 UIGraphicsBeginImageContext(frame.size); | 1279 UIGraphicsBeginImageContext(frame.size); |
1358 [[UIColor whiteColor] setFill]; | 1280 [[UIColor whiteColor] setFill]; |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1576 // Default value for shouldSuppressDialogs is NO, so updating them only | 1498 // Default value for shouldSuppressDialogs is NO, so updating them only |
1577 // when necessary is a good optimization. | 1499 // when necessary is a good optimization. |
1578 if (_shouldSuppressDialogsOnWindowIDInjection) { | 1500 if (_shouldSuppressDialogsOnWindowIDInjection) { |
1579 self.shouldSuppressDialogs = YES; | 1501 self.shouldSuppressDialogs = YES; |
1580 _shouldSuppressDialogsOnWindowIDInjection = NO; | 1502 _shouldSuppressDialogsOnWindowIDInjection = NO; |
1581 } | 1503 } |
1582 | 1504 |
1583 [_windowIDJSManager inject]; | 1505 [_windowIDJSManager inject]; |
1584 } | 1506 } |
1585 | 1507 |
1586 // Set the specified recognizer to take priority over any recognizers in the | |
1587 // view that have a description containing the specified text fragment. | |
1588 + (void)requireGestureRecognizerToFail:(UIGestureRecognizer*)recognizer | |
1589 inView:(UIView*)view | |
1590 containingDescription:(NSString*)fragment { | |
1591 for (UIGestureRecognizer* iRecognizer in [view gestureRecognizers]) { | |
1592 if (iRecognizer != recognizer) { | |
1593 NSString* description = [iRecognizer description]; | |
1594 if ([description rangeOfString:fragment].length) { | |
1595 [iRecognizer requireGestureRecognizerToFail:recognizer]; | |
1596 // requireGestureRecognizerToFail: doesn't retain the recognizer, so it | |
1597 // is possible for |iRecognizer| to outlive |recognizer| and end up with | |
1598 // a dangling pointer. Add a retaining associative reference to ensure | |
1599 // that the lifetimes work out. | |
1600 // Note that normally using the value as the key wouldn't make any | |
1601 // sense, but here it's fine since nothing needs to look up the value. | |
1602 objc_setAssociatedObject(view, recognizer, recognizer, | |
1603 OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
1604 } | |
1605 } | |
1606 } | |
1607 } | |
1608 | |
1609 - (CRWWebController*)createChildWebController { | 1508 - (CRWWebController*)createChildWebController { |
1610 CRWWebController* result = [self.delegate webPageOrderedOpen]; | 1509 CRWWebController* result = [self.delegate webPageOrderedOpen]; |
1611 DCHECK(!result || result.sessionController.openedByDOM); | 1510 DCHECK(!result || result.sessionController.openedByDOM); |
1612 return result; | 1511 return result; |
1613 } | 1512 } |
1614 | 1513 |
1615 - (BOOL)canUseViewForGeneratingOverlayPlaceholderView { | 1514 - (BOOL)canUseViewForGeneratingOverlayPlaceholderView { |
1616 return _containerView != nil; | 1515 return _containerView != nil; |
1617 } | 1516 } |
1618 | 1517 |
(...skipping 1980 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3599 _webUIManager.reset( | 3498 _webUIManager.reset( |
3600 [[CRWWebUIManager alloc] initWithWebState:self.webStateImpl]); | 3499 [[CRWWebUIManager alloc] initWithWebState:self.webStateImpl]); |
3601 } | 3500 } |
3602 | 3501 |
3603 - (void)clearWebUI { | 3502 - (void)clearWebUI { |
3604 _webStateImpl->ClearWebUI(); | 3503 _webStateImpl->ClearWebUI(); |
3605 _webUIManager.reset(); | 3504 _webUIManager.reset(); |
3606 } | 3505 } |
3607 | 3506 |
3608 #pragma mark - | 3507 #pragma mark - |
3609 #pragma mark UIGestureRecognizerDelegate | |
3610 | |
3611 - (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer | |
3612 shouldRecognizeSimultaneouslyWithGestureRecognizer: | |
3613 (UIGestureRecognizer*)otherGestureRecognizer { | |
3614 // Allows the custom UILongPressGestureRecognizer to fire simultaneously with | |
3615 // other recognizers. | |
3616 return YES; | |
3617 } | |
3618 | |
3619 - (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer | |
3620 shouldReceiveTouch:(UITouch*)touch { | |
3621 // Expect only _contextMenuRecognizer. | |
3622 DCHECK([gestureRecognizer isEqual:_contextMenuRecognizer]); | |
3623 | |
3624 // This is custom long press gesture recognizer. By the time the gesture is | |
3625 // recognized the web controller needs to know if there is a link under the | |
3626 // touch. If there a link, the web controller will reject system's context | |
3627 // menu and show another one. If for some reason context menu info is not | |
3628 // fetched - system context menu will be shown. | |
3629 [self setDOMElementForLastTouch:nil]; | |
3630 base::WeakNSObject<CRWWebController> weakSelf(self); | |
3631 [self fetchDOMElementAtPoint:[touch locationInView:_webView] | |
3632 completionHandler:^(NSDictionary* element) { | |
3633 [weakSelf setDOMElementForLastTouch:element]; | |
3634 }]; | |
3635 return YES; | |
3636 } | |
3637 | |
3638 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer { | |
3639 // Expect only _contextMenuRecognizer. | |
3640 DCHECK([gestureRecognizer isEqual:_contextMenuRecognizer]); | |
3641 if (!_webView) { | |
3642 // Show the context menu iff currently displaying a web view. | |
3643 // Do nothing for native views. | |
3644 return NO; | |
3645 } | |
3646 | |
3647 // Fetching is considered as successful even if |_DOMElementForLastTouch| is | |
3648 // empty. However if |_DOMElementForLastTouch| is empty then custom context | |
3649 // menu should not be shown. | |
3650 UMA_HISTOGRAM_BOOLEAN("WebController.FetchContextMenuInfoAsyncSucceeded", | |
3651 !!_DOMElementForLastTouch); | |
3652 return [_DOMElementForLastTouch count]; | |
3653 } | |
3654 | |
3655 #pragma mark - | |
3656 #pragma mark CRWRequestTrackerDelegate | 3508 #pragma mark CRWRequestTrackerDelegate |
3657 | 3509 |
3658 - (BOOL)isForStaticFileRequests { | 3510 - (BOOL)isForStaticFileRequests { |
3659 return NO; | 3511 return NO; |
3660 } | 3512 } |
3661 | 3513 |
3662 - (void)updatedSSLStatus:(const web::SSLStatus&)sslStatus | 3514 - (void)updatedSSLStatus:(const web::SSLStatus&)sslStatus |
3663 forPageUrl:(const GURL&)url | 3515 forPageUrl:(const GURL&)url |
3664 userInfo:(id)userInfo { | 3516 userInfo:(id)userInfo { |
3665 // |userInfo| is a CRWSessionEntry. | 3517 // |userInfo| is a CRWSessionEntry. |
(...skipping 643 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4309 // when the page finishes loading. | 4161 // when the page finishes loading. |
4310 base::WeakNSObject<UIScrollView> weakScrollView(self.webScrollView); | 4162 base::WeakNSObject<UIScrollView> weakScrollView(self.webScrollView); |
4311 base::scoped_nsprotocol<ProceduralBlock> action([^{ | 4163 base::scoped_nsprotocol<ProceduralBlock> action([^{ |
4312 [weakScrollView setContentOffset:scrollOffset]; | 4164 [weakScrollView setContentOffset:scrollOffset]; |
4313 } copy]); | 4165 } copy]); |
4314 [_pendingLoadCompleteActions addObject:action]; | 4166 [_pendingLoadCompleteActions addObject:action]; |
4315 } | 4167 } |
4316 } | 4168 } |
4317 | 4169 |
4318 #pragma mark - | 4170 #pragma mark - |
4319 #pragma mark Web Page Features | |
4320 | |
4321 - (void)fetchWebPageWidthWithCompletionHandler:(void (^)(CGFloat))handler { | |
4322 if (!_webView) { | |
4323 handler(0); | |
4324 return; | |
4325 } | |
4326 | |
4327 [self executeJavaScript:@"__gCrWeb.getPageWidth();" | |
4328 completionHandler:^(id pageWidth, NSError*) { | |
4329 handler([base::mac::ObjCCastStrict<NSNumber>(pageWidth) floatValue]); | |
4330 }]; | |
4331 } | |
4332 | |
4333 - (void)fetchDOMElementAtPoint:(CGPoint)point | |
4334 completionHandler:(void (^)(NSDictionary*))handler { | |
4335 DCHECK(handler); | |
4336 // Convert point into web page's coordinate system (which may be scaled and/or | |
4337 // scrolled). | |
4338 CGPoint scrollOffset = self.scrollPosition; | |
4339 CGFloat webViewContentWidth = self.webScrollView.contentSize.width; | |
4340 base::WeakNSObject<CRWWebController> weakSelf(self); | |
4341 [self fetchWebPageWidthWithCompletionHandler:^(CGFloat pageWidth) { | |
4342 CGFloat scale = pageWidth / webViewContentWidth; | |
4343 CGPoint localPoint = CGPointMake((point.x + scrollOffset.x) * scale, | |
4344 (point.y + scrollOffset.y) * scale); | |
4345 NSString* const kGetElementScript = | |
4346 [NSString stringWithFormat:@"__gCrWeb.getElementFromPoint(%g, %g);", | |
4347 localPoint.x, localPoint.y]; | |
4348 [weakSelf executeJavaScript:kGetElementScript | |
4349 completionHandler:^(id element, NSError*) { | |
4350 handler(base::mac::ObjCCastStrict<NSDictionary>(element)); | |
4351 }]; | |
4352 }]; | |
4353 } | |
4354 | |
4355 - (web::ContextMenuParams)contextMenuParamsForElement:(NSDictionary*)element { | |
4356 web::ContextMenuParams params; | |
4357 NSString* title = nil; | |
4358 NSString* href = element[@"href"]; | |
4359 if (href) { | |
4360 params.link_url = GURL(base::SysNSStringToUTF8(href)); | |
4361 if (params.link_url.SchemeIs(url::kJavaScriptScheme)) { | |
4362 title = @"JavaScript"; | |
4363 } else { | |
4364 base::string16 URLText = url_formatter::FormatUrl(params.link_url); | |
4365 title = base::SysUTF16ToNSString(URLText); | |
4366 } | |
4367 } | |
4368 NSString* src = element[@"src"]; | |
4369 if (src) { | |
4370 params.src_url = GURL(base::SysNSStringToUTF8(src)); | |
4371 if (!title) | |
4372 title = [[src copy] autorelease]; | |
4373 if ([title hasPrefix:base::SysUTF8ToNSString(url::kDataScheme)]) | |
4374 title = nil; | |
4375 } | |
4376 NSString* titleAttribute = element[@"title"]; | |
4377 if (titleAttribute) | |
4378 title = titleAttribute; | |
4379 if (title) { | |
4380 params.menu_title.reset([title copy]); | |
4381 } | |
4382 NSString* referrerPolicy = element[@"referrerPolicy"]; | |
4383 if (referrerPolicy) { | |
4384 params.referrer_policy = | |
4385 web::ReferrerPolicyFromString(base::SysNSStringToUTF8(referrerPolicy)); | |
4386 } | |
4387 NSString* innerText = element[@"innerText"]; | |
4388 if ([innerText length] > 0) { | |
4389 params.link_text.reset([innerText copy]); | |
4390 } | |
4391 return params; | |
4392 } | |
4393 | |
4394 #pragma mark - | |
4395 #pragma mark Fullscreen | 4171 #pragma mark Fullscreen |
4396 | 4172 |
4397 - (CGRect)visibleFrame { | 4173 - (CGRect)visibleFrame { |
4398 CGRect frame = [_containerView bounds]; | 4174 CGRect frame = [_containerView bounds]; |
4399 CGFloat headerHeight = [self headerHeight]; | 4175 CGFloat headerHeight = [self headerHeight]; |
4400 frame.origin.y = headerHeight; | 4176 frame.origin.y = headerHeight; |
4401 frame.size.height -= headerHeight; | 4177 frame.size.height -= headerHeight; |
4402 return frame; | 4178 return frame; |
4403 } | 4179 } |
4404 | 4180 |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4667 // Create a dependency between the |webView| pan gesture and BVC side swipe | 4443 // Create a dependency between the |webView| pan gesture and BVC side swipe |
4668 // gestures. Note: This needs to be added before the longPress recognizers | 4444 // gestures. Note: This needs to be added before the longPress recognizers |
4669 // below, or the longPress appears to deadlock the remaining recognizers, | 4445 // below, or the longPress appears to deadlock the remaining recognizers, |
4670 // thereby breaking scroll. | 4446 // thereby breaking scroll. |
4671 NSSet* recognizers = [_swipeRecognizerProvider swipeRecognizers]; | 4447 NSSet* recognizers = [_swipeRecognizerProvider swipeRecognizers]; |
4672 for (UISwipeGestureRecognizer* swipeRecognizer in recognizers) { | 4448 for (UISwipeGestureRecognizer* swipeRecognizer in recognizers) { |
4673 [self.webScrollView.panGestureRecognizer | 4449 [self.webScrollView.panGestureRecognizer |
4674 requireGestureRecognizerToFail:swipeRecognizer]; | 4450 requireGestureRecognizerToFail:swipeRecognizer]; |
4675 } | 4451 } |
4676 | 4452 |
4677 // On iOS 4.x, there are two gesture recognizers on the UIWebView | 4453 _contextMenuController.reset([[CRWContextMenuController alloc] |
4678 // subclasses, | 4454 initWithWebView:_webView |
4679 // that have a minimum tap threshold of 0.12s and 0.75s. | 4455 javaScriptDelegate:self |
4680 // | 4456 delegate:self]); |
4681 // My theory is that the shorter threshold recognizer performs the link | |
4682 // highlight (grey highlight around links when it is tapped and held) while | |
4683 // the longer threshold one pops up the context menu. | |
4684 // | |
4685 // To override the context menu, this recognizer needs to react faster than | |
4686 // the 0.75s one. The below gesture recognizer is initialized with a | |
4687 // detection duration a little lower than that (see | |
4688 // kLongPressDurationSeconds). It also points the delegate to this class | |
4689 // that | |
4690 // allows simultaneously operate along with the other recognizers. | |
4691 _contextMenuRecognizer.reset([[UILongPressGestureRecognizer alloc] | |
4692 initWithTarget:self | |
4693 action:@selector(showContextMenu:)]); | |
4694 [_contextMenuRecognizer setMinimumPressDuration:kLongPressDurationSeconds]; | |
4695 [_contextMenuRecognizer setAllowableMovement:kLongPressMoveDeltaPixels]; | |
4696 [_contextMenuRecognizer setDelegate:self]; | |
4697 [_webView addGestureRecognizer:_contextMenuRecognizer]; | |
4698 // Certain system gesture handlers are known to conflict with our context | |
4699 // menu handler, causing extra events to fire when the context menu is | |
4700 // active. | |
4701 | |
4702 // A number of solutions have been investigated. The lowest-risk solution | |
4703 // appears to be to recurse through the web controller's recognizers, | |
4704 // looking | |
4705 // for fingerprints of the recognizers known to cause problems, which are | |
4706 // then | |
4707 // de-prioritized (below our own long click handler). | |
4708 // Hunting for description fragments of system recognizers is undeniably | |
4709 // brittle for future versions of iOS. If it does break the context menu | |
4710 // events may leak (regressing b/5310177), but the app will otherwise work. | |
4711 [CRWWebController requireGestureRecognizerToFail:_contextMenuRecognizer | |
4712 inView:_webView | |
4713 containingDescription: | |
4714 @"action=_highlightLongPressRecognized:"]; | |
4715 | 4457 |
4716 // Add all additional gesture recognizers to the web view. | 4458 // Add all additional gesture recognizers to the web view. |
4717 for (UIGestureRecognizer* recognizer in _gestureRecognizers.get()) { | 4459 for (UIGestureRecognizer* recognizer in _gestureRecognizers.get()) { |
4718 [_webView addGestureRecognizer:recognizer]; | 4460 [_webView addGestureRecognizer:recognizer]; |
4719 } | 4461 } |
4720 | 4462 |
4721 _URLOnStartLoading = _defaultURL; | 4463 _URLOnStartLoading = _defaultURL; |
4722 | 4464 |
4723 // Add the web toolbars. | 4465 // Add the web toolbars. |
4724 [_containerView addToolbars:_webViewToolbars]; | 4466 [_containerView addToolbars:_webViewToolbars]; |
4725 | 4467 |
4726 base::scoped_nsobject<CRWWebViewContentView> webViewContentView( | 4468 base::scoped_nsobject<CRWWebViewContentView> webViewContentView( |
4727 [[CRWWebViewContentView alloc] initWithWebView:_webView | 4469 [[CRWWebViewContentView alloc] initWithWebView:_webView |
4728 scrollView:self.webScrollView]); | 4470 scrollView:self.webScrollView]); |
4729 [_containerView displayWebViewContentView:webViewContentView]; | 4471 [_containerView displayWebViewContentView:webViewContentView]; |
4730 } | 4472 } |
4731 } | 4473 } |
4732 | 4474 |
4733 - (WKWebView*)webViewWithConfiguration:(WKWebViewConfiguration*)config { | 4475 - (WKWebView*)webViewWithConfiguration:(WKWebViewConfiguration*)config { |
4476 // Do not attach the context menu controller immediately as the JavaScript | |
4477 // delegate must be specified. | |
4734 return web::BuildWKWebView(CGRectZero, config, | 4478 return web::BuildWKWebView(CGRectZero, config, |
4735 self.webStateImpl->GetBrowserState(), | 4479 self.webStateImpl->GetBrowserState(), |
4736 [self useDesktopUserAgent]); | 4480 [self useDesktopUserAgent]); |
4737 } | 4481 } |
4738 | 4482 |
4739 - (void)setWebView:(WKWebView*)webView { | 4483 - (void)setWebView:(WKWebView*)webView { |
4740 DCHECK_NE(_webView.get(), webView); | 4484 DCHECK_NE(_webView.get(), webView); |
4741 | 4485 |
4742 // Unwind the old web view. | 4486 // Unwind the old web view. |
4743 // TODO(eugenebut): Remove CRWWKScriptMessageRouter once crbug.com/543374 is | 4487 // TODO(eugenebut): Remove CRWWKScriptMessageRouter once crbug.com/543374 is |
(...skipping 614 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5358 - (void)SSLStatusUpdater:(CRWSSLStatusUpdater*)SSLStatusUpdater | 5102 - (void)SSLStatusUpdater:(CRWSSLStatusUpdater*)SSLStatusUpdater |
5359 didChangeSSLStatusForNavigationItem:(web::NavigationItem*)navigationItem { | 5103 didChangeSSLStatusForNavigationItem:(web::NavigationItem*)navigationItem { |
5360 web::NavigationItem* currentNavigationItem = | 5104 web::NavigationItem* currentNavigationItem = |
5361 self.webState->GetNavigationManager()->GetLastCommittedItem(); | 5105 self.webState->GetNavigationManager()->GetLastCommittedItem(); |
5362 if (navigationItem == currentNavigationItem) { | 5106 if (navigationItem == currentNavigationItem) { |
5363 [self didUpdateSSLStatusForCurrentNavigationItem]; | 5107 [self didUpdateSSLStatusForCurrentNavigationItem]; |
5364 } | 5108 } |
5365 } | 5109 } |
5366 | 5110 |
5367 #pragma mark - | 5111 #pragma mark - |
5112 #pragma mark CRWWebContextMenuControllerDelegate methods | |
5113 | |
5114 - (BOOL)webView:(WKWebView*)webView | |
5115 handleContextMenu:(const web::ContextMenuParams&)params { | |
5116 DCHECK(webView == _webView); | |
5117 if (_isBeingDestroyed) { | |
5118 return NO; | |
5119 } | |
5120 return self.webStateImpl->HandleContextMenu(params); | |
5121 } | |
5122 | |
5123 #pragma mark - | |
5124 #pragma mark CRWNativeContentDelegate methods | |
5125 | |
5126 - (BOOL)nativeContent:(id)content | |
5127 handleContextMenu:(const web::ContextMenuParams&)params { | |
5128 if (_isBeingDestroyed) { | |
5129 return NO; | |
5130 } | |
5131 return self.webStateImpl->HandleContextMenu(params); | |
5132 } | |
5133 | |
5134 #pragma mark - | |
5368 #pragma mark KVO Observation | 5135 #pragma mark KVO Observation |
5369 | 5136 |
5370 - (void)observeValueForKeyPath:(NSString*)keyPath | 5137 - (void)observeValueForKeyPath:(NSString*)keyPath |
5371 ofObject:(id)object | 5138 ofObject:(id)object |
5372 change:(NSDictionary*)change | 5139 change:(NSDictionary*)change |
5373 context:(void*)context { | 5140 context:(void*)context { |
5374 NSString* dispatcherSelectorName = self.WKWebViewObservers[keyPath]; | 5141 NSString* dispatcherSelectorName = self.WKWebViewObservers[keyPath]; |
5375 DCHECK(dispatcherSelectorName); | 5142 DCHECK(dispatcherSelectorName); |
5376 if (dispatcherSelectorName) | 5143 if (dispatcherSelectorName) |
5377 [self performSelector:NSSelectorFromString(dispatcherSelectorName)]; | 5144 [self performSelector:NSSelectorFromString(dispatcherSelectorName)]; |
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5767 } | 5534 } |
5768 | 5535 |
5769 return web::WEB_VIEW_DOCUMENT_TYPE_GENERIC; | 5536 return web::WEB_VIEW_DOCUMENT_TYPE_GENERIC; |
5770 } | 5537 } |
5771 | 5538 |
5772 - (NSString*)refererFromNavigationAction:(WKNavigationAction*)action { | 5539 - (NSString*)refererFromNavigationAction:(WKNavigationAction*)action { |
5773 return [action.request valueForHTTPHeaderField:@"Referer"]; | 5540 return [action.request valueForHTTPHeaderField:@"Referer"]; |
5774 } | 5541 } |
5775 | 5542 |
5776 @end | 5543 @end |
OLD | NEW |