OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/contextual_search/js_contextual_search_manager.h" |
| 6 |
| 7 #include "base/ios/ios_util.h" |
| 8 #include "base/mac/foundation_util.h" |
| 9 #include "base/mac/scoped_block.h" |
| 10 #include "base/mac/scoped_nsobject.h" |
| 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/sys_string_conversions.h" |
| 13 |
| 14 static NSString* const kEnableSelectionChangeListener = |
| 15 @"__gCrWeb.contextualSearch.enableSelectionChangeListener(%@);"; |
| 16 |
| 17 static NSString* const kSetMutationObserverDelay = |
| 18 @"__gCrWeb.contextualSearch.setMutationObserverDelay(%f);"; |
| 19 |
| 20 static NSString* const kDisableMutationObserver = |
| 21 @"__gCrWeb.contextualSearch.disableMutationObserver();"; |
| 22 |
| 23 static NSString* const kSetBodyTouchListenerDelay = |
| 24 @"__gCrWeb.contextualSearch.setBodyTouchListenerDelay(%f);"; |
| 25 |
| 26 static NSString* const kDisableBodyTouchListener = |
| 27 @"__gCrWeb.contextualSearch.disableBodyTouchListener();"; |
| 28 |
| 29 static NSString* const kHandleCtxSearch = |
| 30 @"__gCrWeb.contextualSearch.handleTapAtPoint(%f, %f);"; |
| 31 |
| 32 static NSString* const kExpandHighlight = |
| 33 @"__gCrWeb.contextualSearch.expandHighlight(%d, %d);"; |
| 34 |
| 35 static NSString* const kHighlightRects = |
| 36 @"__gCrWeb.contextualSearch.highlightRects();"; |
| 37 |
| 38 static NSString* const kClearHighlight = |
| 39 @"__gCrWeb.contextualSearch.clearHighlight();"; |
| 40 |
| 41 @implementation JsContextualSearchManager |
| 42 |
| 43 #pragma mark - Protected methods |
| 44 |
| 45 - (NSString*)scriptPath { |
| 46 return @"contextualsearch"; |
| 47 } |
| 48 |
| 49 #pragma mark - Public methods |
| 50 |
| 51 - (void)fetchContextFromSelectionAtPoint:(CGPoint)point |
| 52 completionHandler:(void (^)(NSString*))handler { |
| 53 NSString* handleContextualSearch = |
| 54 [NSString stringWithFormat:kHandleCtxSearch, point.x, point.y]; |
| 55 |
| 56 web::JavaScriptResultBlock resultHandler = ^(id result, NSError* error) { |
| 57 if (error) { |
| 58 DLOG(ERROR) << "Error evaluating contextual search javascript: " |
| 59 << base::SysNSStringToUTF8([error description]); |
| 60 } |
| 61 handler(base::mac::ObjCCastStrict<NSString>(result)); |
| 62 }; |
| 63 |
| 64 [self executeJavaScript:handleContextualSearch |
| 65 completionHandler:resultHandler]; |
| 66 } |
| 67 |
| 68 - (void)enableEventListenersWithMutationDelay:(CGFloat)mutationDelay |
| 69 bodyTouchDelay:(CGFloat)bodyTouchDelay { |
| 70 NSString* jsForwardString = |
| 71 [NSString stringWithFormat:kEnableSelectionChangeListener, @"true"]; |
| 72 jsForwardString = [jsForwardString |
| 73 stringByAppendingFormat:kSetMutationObserverDelay, mutationDelay]; |
| 74 if (bodyTouchDelay == 0) { |
| 75 jsForwardString = |
| 76 [jsForwardString stringByAppendingString:kDisableBodyTouchListener]; |
| 77 } else { |
| 78 jsForwardString = [jsForwardString |
| 79 stringByAppendingFormat:kSetBodyTouchListenerDelay, bodyTouchDelay]; |
| 80 } |
| 81 [self executeJavaScript:jsForwardString completionHandler:nil]; |
| 82 } |
| 83 |
| 84 - (void)highlightRectsWithCompletionHandler: |
| 85 (web::JavaScriptResultBlock)completion { |
| 86 [self executeJavaScript:kHighlightRects completionHandler:completion]; |
| 87 } |
| 88 |
| 89 - (void)clearHighlight { |
| 90 [self executeJavaScript:kClearHighlight completionHandler:nil]; |
| 91 } |
| 92 |
| 93 - (void)disableListeners { |
| 94 NSString* jsForwardString = |
| 95 [NSString stringWithFormat:kEnableSelectionChangeListener, @"false"]; |
| 96 |
| 97 jsForwardString = |
| 98 [jsForwardString stringByAppendingString:kDisableMutationObserver]; |
| 99 jsForwardString = |
| 100 [jsForwardString stringByAppendingString:kDisableBodyTouchListener]; |
| 101 [self executeJavaScript:jsForwardString completionHandler:nil]; |
| 102 } |
| 103 |
| 104 - (void)expandHighlightToStartOffset:(int)startOffset |
| 105 endOffset:(int)endOffset |
| 106 completionHandler:(web::JavaScriptResultBlock)completion { |
| 107 if (startOffset < 0 || endOffset < 0) |
| 108 return; |
| 109 NSString* expandHightlightString = |
| 110 [NSString stringWithFormat:kExpandHighlight, startOffset, endOffset]; |
| 111 |
| 112 [self executeJavaScript:expandHightlightString completionHandler:completion]; |
| 113 } |
| 114 |
| 115 @end |
OLD | NEW |