OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_context_menu_controller.h" | 5 #import "ios/web/web_state/ui/crw_context_menu_controller.h" |
6 | 6 |
7 #import <objc/runtime.h> | 7 #import <objc/runtime.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 // The scroll view of |webView|. | 51 // The scroll view of |webView|. |
52 @property(nonatomic, readonly, weak) id<CRWContextMenuDelegate> delegate; | 52 @property(nonatomic, readonly, weak) id<CRWContextMenuDelegate> delegate; |
53 // Returns the x, y offset the content has been scrolled. | 53 // Returns the x, y offset the content has been scrolled. |
54 @property(nonatomic, readonly) CGPoint scrollPosition; | 54 @property(nonatomic, readonly) CGPoint scrollPosition; |
55 | 55 |
56 // Called when the window has determined there was a long-press and context menu | 56 // Called when the window has determined there was a long-press and context menu |
57 // must be shown. | 57 // must be shown. |
58 - (void)showContextMenu:(UIGestureRecognizer*)gestureRecognizer; | 58 - (void)showContextMenu:(UIGestureRecognizer*)gestureRecognizer; |
59 // Cancels all touch events in the web view (long presses, tapping, scrolling). | 59 // Cancels all touch events in the web view (long presses, tapping, scrolling). |
60 - (void)cancelAllTouches; | 60 - (void)cancelAllTouches; |
61 // Asynchronously fetches full width of the rendered web page. | |
62 - (void)fetchWebPageWidthWithCompletionHandler:(void (^)(CGFloat))handler; | |
63 // Asynchronously fetches information about DOM element for the given point (in | 61 // Asynchronously fetches information about DOM element for the given point (in |
64 // UIView coordinates). |handler| can not be nil. See |_DOMElementForLastTouch| | 62 // UIView coordinates). |handler| can not be nil. See |_DOMElementForLastTouch| |
65 // for element format description. | 63 // for element format description. |
66 - (void)fetchDOMElementAtPoint:(CGPoint)point | 64 - (void)fetchDOMElementAtPoint:(CGPoint)point |
67 completionHandler:(void (^)(NSDictionary*))handler; | 65 completionHandler:(void (^)(NSDictionary*))handler; |
68 // Sets the value of |_DOMElementForLastTouch|. | 66 // Sets the value of |_DOMElementForLastTouch|. |
69 - (void)setDOMElementForLastTouch:(NSDictionary*)element; | 67 - (void)setDOMElementForLastTouch:(NSDictionary*)element; |
70 // Forwards the execution of |script| to |javaScriptDelegate| and if it is nil, | 68 // Forwards the execution of |script| to |javaScriptDelegate| and if it is nil, |
71 // to |webView|. | 69 // to |webView|. |
72 - (void)executeJavaScript:(NSString*)script | 70 - (void)executeJavaScript:(NSString*)script |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 // empty. However if |_DOMElementForLastTouch| is empty then custom context | 216 // empty. However if |_DOMElementForLastTouch| is empty then custom context |
219 // menu should not be shown. | 217 // menu should not be shown. |
220 UMA_HISTOGRAM_BOOLEAN("WebController.FetchContextMenuInfoAsyncSucceeded", | 218 UMA_HISTOGRAM_BOOLEAN("WebController.FetchContextMenuInfoAsyncSucceeded", |
221 !!_DOMElementForLastTouch); | 219 !!_DOMElementForLastTouch); |
222 return [_DOMElementForLastTouch count]; | 220 return [_DOMElementForLastTouch count]; |
223 } | 221 } |
224 | 222 |
225 #pragma mark - | 223 #pragma mark - |
226 #pragma mark Web Page Features | 224 #pragma mark Web Page Features |
227 | 225 |
228 - (void)fetchWebPageWidthWithCompletionHandler:(void (^)(CGFloat))handler { | |
229 [self executeJavaScript:@"__gCrWeb.getPageWidth();" | |
230 completionHandler:^(id pageWidth, NSError*) { | |
231 handler([base::mac::ObjCCastStrict<NSNumber>(pageWidth) floatValue]); | |
232 }]; | |
233 } | |
234 | |
235 - (void)fetchDOMElementAtPoint:(CGPoint)point | 226 - (void)fetchDOMElementAtPoint:(CGPoint)point |
236 completionHandler:(void (^)(NSDictionary*))handler { | 227 completionHandler:(void (^)(NSDictionary*))handler { |
237 DCHECK(handler); | 228 DCHECK(handler); |
238 // Convert point into web page's coordinate system (which may be scaled and/or | |
239 // scrolled). | |
240 CGPoint scrollOffset = self.scrollPosition; | 229 CGPoint scrollOffset = self.scrollPosition; |
241 CGFloat webViewContentWidth = self.webScrollView.contentSize.width; | 230 CGSize webViewContentSize = self.webScrollView.contentSize; |
242 CRWContextMenuController* weakSelf = self; | 231 CGFloat webViewContentWidth = webViewContentSize.width; |
243 [self fetchWebPageWidthWithCompletionHandler:^(CGFloat pageWidth) { | 232 CGFloat webViewContentHeight = webViewContentSize.height; |
244 CGFloat scale = pageWidth / webViewContentWidth; | 233 NSString* getElementScript = [NSString |
245 CGPoint localPoint = CGPointMake((point.x + scrollOffset.x) * scale, | 234 stringWithFormat:@"__gCrWeb.getElementFromPoint(%g, %g, %g, %g);", |
246 (point.y + scrollOffset.y) * scale); | 235 point.x + scrollOffset.x, point.y + scrollOffset.y, |
247 NSString* const kGetElementScript = | 236 webViewContentWidth, webViewContentHeight]; |
248 [NSString stringWithFormat:@"__gCrWeb.getElementFromPoint(%g, %g);", | 237 [self executeJavaScript:getElementScript |
249 localPoint.x, localPoint.y]; | 238 completionHandler:^(id element, NSError*) { |
250 [weakSelf executeJavaScript:kGetElementScript | 239 handler(base::mac::ObjCCastStrict<NSDictionary>(element)); |
251 completionHandler:^(id element, NSError*) { | 240 }]; |
252 handler(base::mac::ObjCCastStrict<NSDictionary>(element)); | |
253 }]; | |
254 }]; | |
255 } | 241 } |
256 | 242 |
257 @end | 243 @end |
OLD | NEW |