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/chrome/browser/find_in_page/find_in_page_controller.h" | 5 #import "ios/chrome/browser/find_in_page/find_in_page_controller.h" |
6 | 6 |
7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
8 | 8 |
9 #import <cmath> | 9 #import <cmath> |
10 #include <memory> | 10 #include <memory> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/mac/foundation_util.h" | 13 #include "base/mac/foundation_util.h" |
14 #include "base/mac/scoped_nsobject.h" | 14 #include "base/mac/scoped_nsobject.h" |
15 #import "ios/chrome/browser/find_in_page/find_in_page_model.h" | 15 #import "ios/chrome/browser/find_in_page/find_in_page_model.h" |
16 #import "ios/chrome/browser/find_in_page/js_findinpage_manager.h" | 16 #import "ios/chrome/browser/find_in_page/js_findinpage_manager.h" |
17 #import "ios/chrome/browser/web/dom_altering_lock.h" | 17 #import "ios/chrome/browser/web/dom_altering_lock.h" |
18 #import "ios/web/public/web_state/crw_web_view_proxy.h" | 18 #import "ios/web/public/web_state/crw_web_view_proxy.h" |
19 #import "ios/web/public/web_state/crw_web_view_scroll_view_proxy.h" | 19 #import "ios/web/public/web_state/crw_web_view_scroll_view_proxy.h" |
20 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" | 20 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
21 #import "ios/web/public/web_state/web_state.h" | 21 #import "ios/web/public/web_state/web_state.h" |
22 #import "ios/web/public/web_state/web_state_observer_bridge.h" | 22 #import "ios/web/public/web_state/web_state_observer_bridge.h" |
23 | 23 |
| 24 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 25 #error "This file requires ARC support." |
| 26 #endif |
| 27 |
24 NSString* const kFindBarTextFieldWillBecomeFirstResponderNotification = | 28 NSString* const kFindBarTextFieldWillBecomeFirstResponderNotification = |
25 @"kFindBarTextFieldWillBecomeFirstResponderNotification"; | 29 @"kFindBarTextFieldWillBecomeFirstResponderNotification"; |
26 NSString* const kFindBarTextFieldDidResignFirstResponderNotification = | 30 NSString* const kFindBarTextFieldDidResignFirstResponderNotification = |
27 @"kFindBarTextFieldDidResignFirstResponderNotification"; | 31 @"kFindBarTextFieldDidResignFirstResponderNotification"; |
28 | 32 |
29 namespace { | 33 namespace { |
30 // The delay (in secs) after which the find in page string will be pumped again. | 34 // The delay (in secs) after which the find in page string will be pumped again. |
31 const NSTimeInterval kRecurringPumpDelay = .01; | 35 const NSTimeInterval kRecurringPumpDelay = .01; |
32 | 36 |
33 // Keeps find in page search term to be shared between different tabs. Never | 37 // Keeps find in page search term to be shared between different tabs. Never |
34 // reset, not stored on disk. | 38 // reset, not stored on disk. |
35 static NSString* gSearchTerm; | 39 static NSString* gSearchTerm; |
36 } | 40 } |
37 | 41 |
38 @interface FindInPageController () <DOMAltering, CRWWebStateObserver> | 42 @interface FindInPageController () <DOMAltering, CRWWebStateObserver> |
39 // The find in page controller delegate. | 43 // The find in page controller delegate. |
40 @property(nonatomic, readonly) id<FindInPageControllerDelegate> delegate; | 44 @property(nonatomic, readonly) id<FindInPageControllerDelegate> delegate; |
41 // The web view's scroll view. | 45 // The web view's scroll view. |
42 @property(nonatomic, readonly) CRWWebViewScrollViewProxy* webViewScrollView; | 46 @property(weak, nonatomic, readonly) |
| 47 CRWWebViewScrollViewProxy* webViewScrollView; |
43 | 48 |
44 // Find in Page text field listeners. | 49 // Find in Page text field listeners. |
45 - (void)findBarTextFieldWillBecomeFirstResponder:(NSNotification*)note; | 50 - (void)findBarTextFieldWillBecomeFirstResponder:(NSNotification*)note; |
46 - (void)findBarTextFieldDidResignFirstResponder:(NSNotification*)note; | 51 - (void)findBarTextFieldDidResignFirstResponder:(NSNotification*)note; |
47 // Keyboard listeners. | 52 // Keyboard listeners. |
48 - (void)keyboardDidShow:(NSNotification*)note; | 53 - (void)keyboardDidShow:(NSNotification*)note; |
49 - (void)keyboardWillHide:(NSNotification*)note; | 54 - (void)keyboardWillHide:(NSNotification*)note; |
50 // Constantly injects the find string in page until | 55 // Constantly injects the find string in page until |
51 // |disableFindInPageWithCompletionHandler:| is called or the find operation is | 56 // |disableFindInPageWithCompletionHandler:| is called or the find operation is |
52 // complete. Calls |completionHandler| if the find operation is complete. | 57 // complete. Calls |completionHandler| if the find operation is complete. |
(...skipping 29 matching lines...) Expand all Loading... |
82 // disable when there is nothing to clear. | 87 // disable when there is nothing to clear. |
83 BOOL _findStringStarted; | 88 BOOL _findStringStarted; |
84 | 89 |
85 // Bridge to observe the web state from Objective-C. | 90 // Bridge to observe the web state from Objective-C. |
86 std::unique_ptr<web::WebStateObserverBridge> _webStateObserverBridge; | 91 std::unique_ptr<web::WebStateObserverBridge> _webStateObserverBridge; |
87 } | 92 } |
88 | 93 |
89 @synthesize delegate = _delegate; | 94 @synthesize delegate = _delegate; |
90 | 95 |
91 + (void)setSearchTerm:(NSString*)string { | 96 + (void)setSearchTerm:(NSString*)string { |
92 [gSearchTerm release]; | |
93 gSearchTerm = [string copy]; | 97 gSearchTerm = [string copy]; |
94 } | 98 } |
95 | 99 |
96 + (NSString*)searchTerm { | 100 + (NSString*)searchTerm { |
97 return gSearchTerm; | 101 return gSearchTerm; |
98 } | 102 } |
99 | 103 |
100 - (id)initWithWebState:(web::WebState*)webState | 104 - (id)initWithWebState:(web::WebState*)webState |
101 delegate:(id<FindInPageControllerDelegate>)delegate { | 105 delegate:(id<FindInPageControllerDelegate>)delegate { |
102 self = [super init]; | 106 self = [super init]; |
103 if (self) { | 107 if (self) { |
104 DCHECK(delegate); | 108 DCHECK(delegate); |
105 _findInPageJsManager = base::mac::ObjCCastStrict<JsFindinpageManager>( | 109 _findInPageJsManager = base::mac::ObjCCastStrict<JsFindinpageManager>( |
106 [webState->GetJSInjectionReceiver() | 110 [webState->GetJSInjectionReceiver() |
107 instanceOfClass:[JsFindinpageManager class]]); | 111 instanceOfClass:[JsFindinpageManager class]]); |
108 _delegate = delegate; | 112 _delegate = delegate; |
109 _webStateObserverBridge.reset( | 113 _webStateObserverBridge.reset( |
110 new web::WebStateObserverBridge(webState, self)); | 114 new web::WebStateObserverBridge(webState, self)); |
111 _webViewProxy.reset([webState->GetWebViewProxy() retain]); | 115 _webViewProxy.reset(webState->GetWebViewProxy()); |
112 [[NSNotificationCenter defaultCenter] | 116 [[NSNotificationCenter defaultCenter] |
113 addObserver:self | 117 addObserver:self |
114 selector:@selector(findBarTextFieldWillBecomeFirstResponder:) | 118 selector:@selector(findBarTextFieldWillBecomeFirstResponder:) |
115 name:kFindBarTextFieldWillBecomeFirstResponderNotification | 119 name:kFindBarTextFieldWillBecomeFirstResponderNotification |
116 object:nil]; | 120 object:nil]; |
117 [[NSNotificationCenter defaultCenter] | 121 [[NSNotificationCenter defaultCenter] |
118 addObserver:self | 122 addObserver:self |
119 selector:@selector(findBarTextFieldDidResignFirstResponder:) | 123 selector:@selector(findBarTextFieldDidResignFirstResponder:) |
120 name:kFindBarTextFieldDidResignFirstResponderNotification | 124 name:kFindBarTextFieldDidResignFirstResponderNotification |
121 object:nil]; | 125 object:nil]; |
122 DOMAlteringLock::CreateForWebState(webState); | 126 DOMAlteringLock::CreateForWebState(webState); |
123 } | 127 } |
124 return self; | 128 return self; |
125 } | 129 } |
126 | 130 |
127 - (void)dealloc { | 131 - (void)dealloc { |
128 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 132 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
129 [super dealloc]; | |
130 } | 133 } |
131 | 134 |
132 - (FindInPageModel*)findInPageModel { | 135 - (FindInPageModel*)findInPageModel { |
133 return [_findInPageJsManager findInPageModel]; | 136 return [_findInPageJsManager findInPageModel]; |
134 } | 137 } |
135 | 138 |
136 - (BOOL)canFindInPage { | 139 - (BOOL)canFindInPage { |
137 return [_webViewProxy hasSearchableTextContent]; | 140 return [_webViewProxy hasSearchableTextContent]; |
138 } | 141 } |
139 | 142 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 afterDelay:kRecurringPumpDelay]; | 221 afterDelay:kRecurringPumpDelay]; |
219 }; | 222 }; |
220 [self pumpFindStringInPageWithCompletionHandler:completionHandlerBlock]; | 223 [self pumpFindStringInPageWithCompletionHandler:completionHandlerBlock]; |
221 } | 224 } |
222 | 225 |
223 - (void)pumpFindStringInPageWithCompletionHandler: | 226 - (void)pumpFindStringInPageWithCompletionHandler: |
224 (void (^)(BOOL))completionHandler { | 227 (void (^)(BOOL))completionHandler { |
225 base::WeakNSObject<FindInPageController> weakSelf(self); | 228 base::WeakNSObject<FindInPageController> weakSelf(self); |
226 [_findInPageJsManager pumpWithCompletionHandler:^(BOOL finished, | 229 [_findInPageJsManager pumpWithCompletionHandler:^(BOOL finished, |
227 CGPoint point) { | 230 CGPoint point) { |
228 base::scoped_nsobject<FindInPageController> strongSelf([weakSelf retain]); | 231 base::scoped_nsobject<FindInPageController> strongSelf(weakSelf); |
229 if (finished) { | 232 if (finished) { |
230 [[strongSelf delegate] willAdjustScrollPosition]; | 233 [[strongSelf delegate] willAdjustScrollPosition]; |
231 point = [strongSelf limitOverscroll:[strongSelf webViewScrollView] | 234 point = [strongSelf limitOverscroll:[strongSelf webViewScrollView] |
232 atPoint:point]; | 235 atPoint:point]; |
233 [[strongSelf webViewScrollView] setContentOffset:point animated:YES]; | 236 [[strongSelf webViewScrollView] setContentOffset:point animated:YES]; |
234 } | 237 } |
235 completionHandler(finished); | 238 completionHandler(finished); |
236 }]; | 239 }]; |
237 } | 240 } |
238 | 241 |
239 - (void)findNextStringInPageWithCompletionHandler: | 242 - (void)findNextStringInPageWithCompletionHandler: |
240 (ProceduralBlock)completionHandler { | 243 (ProceduralBlock)completionHandler { |
241 [self initFindInPage]; | 244 [self initFindInPage]; |
242 base::WeakNSObject<FindInPageController> weakSelf(self); | 245 base::WeakNSObject<FindInPageController> weakSelf(self); |
243 [_findInPageJsManager nextMatchWithCompletionHandler:^(CGPoint point) { | 246 [_findInPageJsManager nextMatchWithCompletionHandler:^(CGPoint point) { |
244 base::scoped_nsobject<FindInPageController> strongSelf([weakSelf retain]); | 247 base::scoped_nsobject<FindInPageController> strongSelf(weakSelf); |
245 [[strongSelf delegate] willAdjustScrollPosition]; | 248 [[strongSelf delegate] willAdjustScrollPosition]; |
246 point = [strongSelf limitOverscroll:[strongSelf webViewScrollView] | 249 point = [strongSelf limitOverscroll:[strongSelf webViewScrollView] |
247 atPoint:point]; | 250 atPoint:point]; |
248 [[strongSelf webViewScrollView] setContentOffset:point animated:YES]; | 251 [[strongSelf webViewScrollView] setContentOffset:point animated:YES]; |
249 if (completionHandler) | 252 if (completionHandler) |
250 completionHandler(); | 253 completionHandler(); |
251 }]; | 254 }]; |
252 } | 255 } |
253 | 256 |
254 // Highlight the previous search match, update model and scroll to match. | 257 // Highlight the previous search match, update model and scroll to match. |
255 - (void)findPreviousStringInPageWithCompletionHandler: | 258 - (void)findPreviousStringInPageWithCompletionHandler: |
256 (ProceduralBlock)completionHandler { | 259 (ProceduralBlock)completionHandler { |
257 [self initFindInPage]; | 260 [self initFindInPage]; |
258 base::WeakNSObject<FindInPageController> weakSelf(self); | 261 base::WeakNSObject<FindInPageController> weakSelf(self); |
259 [_findInPageJsManager previousMatchWithCompletionHandler:^(CGPoint point) { | 262 [_findInPageJsManager previousMatchWithCompletionHandler:^(CGPoint point) { |
260 base::scoped_nsobject<FindInPageController> strongSelf([weakSelf retain]); | 263 base::scoped_nsobject<FindInPageController> strongSelf(weakSelf); |
261 [[strongSelf delegate] willAdjustScrollPosition]; | 264 [[strongSelf delegate] willAdjustScrollPosition]; |
262 point = [strongSelf limitOverscroll:[strongSelf webViewScrollView] | 265 point = [strongSelf limitOverscroll:[strongSelf webViewScrollView] |
263 atPoint:point]; | 266 atPoint:point]; |
264 [[strongSelf webViewScrollView] setContentOffset:point animated:YES]; | 267 [[strongSelf webViewScrollView] setContentOffset:point animated:YES]; |
265 if (completionHandler) | 268 if (completionHandler) |
266 completionHandler(); | 269 completionHandler(); |
267 }]; | 270 }]; |
268 } | 271 } |
269 | 272 |
270 // Remove highlights from the page and disable the model. | 273 // Remove highlights from the page and disable the model. |
271 - (void)disableFindInPageWithCompletionHandler: | 274 - (void)disableFindInPageWithCompletionHandler: |
272 (ProceduralBlock)completionHandler { | 275 (ProceduralBlock)completionHandler { |
273 if (![self canFindInPage]) | 276 if (![self canFindInPage]) |
274 return; | 277 return; |
275 // Cancel any queued calls to |recurringPumpWithCompletionHandler|. | 278 // Cancel any queued calls to |recurringPumpWithCompletionHandler|. |
276 [NSObject cancelPreviousPerformRequestsWithTarget:self]; | 279 [NSObject cancelPreviousPerformRequestsWithTarget:self]; |
277 base::WeakNSObject<FindInPageController> weakSelf(self); | 280 base::WeakNSObject<FindInPageController> weakSelf(self); |
278 ProceduralBlock handler = ^{ | 281 ProceduralBlock handler = ^{ |
279 base::scoped_nsobject<FindInPageController> strongSelf([weakSelf retain]); | 282 base::scoped_nsobject<FindInPageController> strongSelf(weakSelf); |
280 if (strongSelf) { | 283 if (strongSelf) { |
281 [strongSelf.get().findInPageModel setEnabled:NO]; | 284 [strongSelf.get().findInPageModel setEnabled:NO]; |
282 web::WebState* webState = [strongSelf webState]; | 285 web::WebState* webState = [strongSelf webState]; |
283 if (webState) | 286 if (webState) |
284 DOMAlteringLock::FromWebState(webState)->Release(strongSelf); | 287 DOMAlteringLock::FromWebState(webState)->Release(strongSelf); |
285 } | 288 } |
286 if (completionHandler) | 289 if (completionHandler) |
287 completionHandler(); | 290 completionHandler(); |
288 }; | 291 }; |
289 // Only run JSFindInPageManager disable if there is a string in progress to | 292 // Only run JSFindInPageManager disable if there is a string in progress to |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 | 377 |
375 - (BOOL)canReleaseDOMLock { | 378 - (BOOL)canReleaseDOMLock { |
376 return NO; | 379 return NO; |
377 } | 380 } |
378 | 381 |
379 - (void)releaseDOMLockWithCompletionHandler:(ProceduralBlock)completionHandler { | 382 - (void)releaseDOMLockWithCompletionHandler:(ProceduralBlock)completionHandler { |
380 NOTREACHED(); | 383 NOTREACHED(); |
381 } | 384 } |
382 | 385 |
383 @end | 386 @end |
OLD | NEW |