Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1293)

Side by Side Diff: ios/chrome/browser/find_in_page/find_in_page_controller.mm

Issue 1071723002: [Find in Page] Prevent scrolling past the page edge. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Ascii sort cmath Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #import <cmath>
8 9
9 #include "base/ios/ios_util.h" 10 #include "base/ios/ios_util.h"
10 #include "base/logging.h" 11 #include "base/logging.h"
11 #include "base/mac/foundation_util.h" 12 #include "base/mac/foundation_util.h"
12 #include "base/mac/scoped_nsobject.h" 13 #include "base/mac/scoped_nsobject.h"
13 #import "ios/chrome/browser/find_in_page/find_in_page_model.h" 14 #import "ios/chrome/browser/find_in_page/find_in_page_model.h"
14 #import "ios/chrome/browser/find_in_page/js_findinpage_manager.h" 15 #import "ios/chrome/browser/find_in_page/js_findinpage_manager.h"
15 #import "ios/chrome/browser/web/dom_altering_lock.h" 16 #import "ios/chrome/browser/web/dom_altering_lock.h"
16 #import "ios/web/public/web_state/crw_web_view_proxy.h" 17 #import "ios/web/public/web_state/crw_web_view_proxy.h"
17 #import "ios/web/public/web_state/crw_web_view_scroll_view_proxy.h" 18 #import "ios/web/public/web_state/crw_web_view_scroll_view_proxy.h"
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 // Gives find in page more time to complete. Calls |completionHandler| with 52 // Gives find in page more time to complete. Calls |completionHandler| with
52 // a BOOL indicating if the find operation was successfull. |completionHandler| 53 // a BOOL indicating if the find operation was successfull. |completionHandler|
53 // can be nil. 54 // can be nil.
54 - (void)pumpFindStringInPageWithCompletionHandler: 55 - (void)pumpFindStringInPageWithCompletionHandler:
55 (void (^)(BOOL))completionHandler; 56 (void (^)(BOOL))completionHandler;
56 // Processes the result of a single find in page pump. Calls |completionHandler| 57 // Processes the result of a single find in page pump. Calls |completionHandler|
57 // if pumping is done. Re-pumps if necessary. 58 // if pumping is done. Re-pumps if necessary.
58 - (void)processPumpResult:(BOOL)finished 59 - (void)processPumpResult:(BOOL)finished
59 scrollPoint:(CGPoint)scrollPoint 60 scrollPoint:(CGPoint)scrollPoint
60 completionHandler:(ProceduralBlock)completionHandler; 61 completionHandler:(ProceduralBlock)completionHandler;
62 // Prevent scrolling past the end of the page.
63 - (CGPoint)limitOverscroll:(CRWWebViewScrollViewProxy*)scrollViewProxy
64 atPoint:(CGPoint)point;
61 // Returns the associated web state. May be null. 65 // Returns the associated web state. May be null.
62 - (web::WebState*)webState; 66 - (web::WebState*)webState;
63 @end 67 @end
64 68
65 @implementation FindInPageController { 69 @implementation FindInPageController {
66 @private 70 @private
67 // Object that manages find_in_page.js injection into the web view. 71 // Object that manages find_in_page.js injection into the web view.
68 JsFindinpageManager* _findInPageJsManager; 72 JsFindinpageManager* _findInPageJsManager;
69 id<FindInPageControllerDelegate> _delegate; 73 id<FindInPageControllerDelegate> _delegate;
70 74
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 130
127 // Initialize the module with our frame size. 131 // Initialize the module with our frame size.
128 CGRect frame = [_webViewProxy bounds]; 132 CGRect frame = [_webViewProxy bounds];
129 [_findInPageJsManager setWidth:frame.size.width height:frame.size.height]; 133 [_findInPageJsManager setWidth:frame.size.width height:frame.size.height];
130 } 134 }
131 135
132 - (CRWWebViewScrollViewProxy*)webViewScrollView { 136 - (CRWWebViewScrollViewProxy*)webViewScrollView {
133 return [_webViewProxy scrollViewProxy]; 137 return [_webViewProxy scrollViewProxy];
134 } 138 }
135 139
140 - (CGPoint)limitOverscroll:(CRWWebViewScrollViewProxy*)scrollViewProxy
141 atPoint:(CGPoint)point {
142 CGFloat contentHeight = scrollViewProxy.contentSize.height;
143 CGFloat frameHeight = scrollViewProxy.frame.size.height;
144 CGFloat maxScroll = std::max((CGFloat)0, contentHeight - frameHeight);
sdefresne 2015/05/19 13:30:27 C-style cast are forbidden (and sadly casting cons
justincohen 2015/05/19 13:57:09 Done.
145 if (point.y > maxScroll) {
146 point.y = maxScroll;
147 }
148 return point;
149 }
150
136 - (void)processPumpResult:(BOOL)finished 151 - (void)processPumpResult:(BOOL)finished
137 scrollPoint:(CGPoint)scrollPoint 152 scrollPoint:(CGPoint)scrollPoint
138 completionHandler:(ProceduralBlock)completionHandler { 153 completionHandler:(ProceduralBlock)completionHandler {
139 if (finished) { 154 if (finished) {
140 [_delegate willAdjustScrollPosition]; 155 [_delegate willAdjustScrollPosition];
156 scrollPoint = [self limitOverscroll:[_webViewProxy scrollViewProxy]
157 atPoint:scrollPoint];
141 [[_webViewProxy scrollViewProxy] setContentOffset:scrollPoint animated:YES]; 158 [[_webViewProxy scrollViewProxy] setContentOffset:scrollPoint animated:YES];
142 if (completionHandler) 159 if (completionHandler)
143 completionHandler(); 160 completionHandler();
144 } else { 161 } else {
145 [self performSelector:@selector(startPumpingWithCompletionHandler:) 162 [self performSelector:@selector(startPumpingWithCompletionHandler:)
146 withObject:completionHandler 163 withObject:completionHandler
147 afterDelay:kRecurringPumpDelay]; 164 afterDelay:kRecurringPumpDelay];
148 } 165 }
149 } 166 }
150 167
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 } 209 }
193 210
194 - (void)pumpFindStringInPageWithCompletionHandler: 211 - (void)pumpFindStringInPageWithCompletionHandler:
195 (void (^)(BOOL))completionHandler { 212 (void (^)(BOOL))completionHandler {
196 base::WeakNSObject<FindInPageController> weakSelf(self); 213 base::WeakNSObject<FindInPageController> weakSelf(self);
197 [_findInPageJsManager pumpWithCompletionHandler:^(BOOL finished, 214 [_findInPageJsManager pumpWithCompletionHandler:^(BOOL finished,
198 CGPoint point) { 215 CGPoint point) {
199 base::scoped_nsobject<FindInPageController> strongSelf([weakSelf retain]); 216 base::scoped_nsobject<FindInPageController> strongSelf([weakSelf retain]);
200 if (finished) { 217 if (finished) {
201 [[strongSelf delegate] willAdjustScrollPosition]; 218 [[strongSelf delegate] willAdjustScrollPosition];
219 point = [strongSelf limitOverscroll:[strongSelf webViewScrollView]
220 atPoint:point];
202 [[strongSelf webViewScrollView] setContentOffset:point animated:YES]; 221 [[strongSelf webViewScrollView] setContentOffset:point animated:YES];
203 } 222 }
204 completionHandler(finished); 223 completionHandler(finished);
205 }]; 224 }];
206 } 225 }
207 226
208 - (void)findNextStringInPageWithCompletionHandler: 227 - (void)findNextStringInPageWithCompletionHandler:
209 (ProceduralBlock)completionHandler { 228 (ProceduralBlock)completionHandler {
210 [self initFindInPage]; 229 [self initFindInPage];
211 base::WeakNSObject<FindInPageController> weakSelf(self); 230 base::WeakNSObject<FindInPageController> weakSelf(self);
212 [_findInPageJsManager nextMatchWithCompletionHandler:^(CGPoint point) { 231 [_findInPageJsManager nextMatchWithCompletionHandler:^(CGPoint point) {
213 base::scoped_nsobject<FindInPageController> strongSelf([weakSelf retain]); 232 base::scoped_nsobject<FindInPageController> strongSelf([weakSelf retain]);
214 [[strongSelf delegate] willAdjustScrollPosition]; 233 [[strongSelf delegate] willAdjustScrollPosition];
215 CGFloat contentHeight = [strongSelf webViewScrollView].contentSize.height; 234 point = [strongSelf limitOverscroll:[strongSelf webViewScrollView]
216 CGFloat frameHeight = [strongSelf webViewScrollView].frame.size.height; 235 atPoint:point];
217 CGFloat maxScroll = fmax(0, contentHeight - frameHeight);
218 if (point.y > maxScroll) {
219 point.y = maxScroll;
220 }
221 [[strongSelf webViewScrollView] setContentOffset:point animated:YES]; 236 [[strongSelf webViewScrollView] setContentOffset:point animated:YES];
222 if (completionHandler) 237 if (completionHandler)
223 completionHandler(); 238 completionHandler();
224 }]; 239 }];
225 } 240 }
226 241
227 // Highlight the previous search match, update model and scroll to match. 242 // Highlight the previous search match, update model and scroll to match.
228 - (void)findPreviousStringInPageWithCompletionHandler: 243 - (void)findPreviousStringInPageWithCompletionHandler:
229 (ProceduralBlock)completionHandler { 244 (ProceduralBlock)completionHandler {
230 [self initFindInPage]; 245 [self initFindInPage];
231 base::WeakNSObject<FindInPageController> weakSelf(self); 246 base::WeakNSObject<FindInPageController> weakSelf(self);
232 [_findInPageJsManager previousMatchWithCompletionHandler:^(CGPoint point) { 247 [_findInPageJsManager previousMatchWithCompletionHandler:^(CGPoint point) {
233 base::scoped_nsobject<FindInPageController> strongSelf([weakSelf retain]); 248 base::scoped_nsobject<FindInPageController> strongSelf([weakSelf retain]);
234 [[strongSelf delegate] willAdjustScrollPosition]; 249 [[strongSelf delegate] willAdjustScrollPosition];
250 point = [strongSelf limitOverscroll:[strongSelf webViewScrollView]
251 atPoint:point];
235 [[strongSelf webViewScrollView] setContentOffset:point animated:YES]; 252 [[strongSelf webViewScrollView] setContentOffset:point animated:YES];
236 if (completionHandler) 253 if (completionHandler)
237 completionHandler(); 254 completionHandler();
238 }]; 255 }];
239 } 256 }
240 257
241 // Remove highlights from the page and disable the model. 258 // Remove highlights from the page and disable the model.
242 - (void)disableFindInPageWithCompletionHandler: 259 - (void)disableFindInPageWithCompletionHandler:
243 (ProceduralBlock)completionHandler { 260 (ProceduralBlock)completionHandler {
244 if (![self canFindInPage]) 261 if (![self canFindInPage])
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 365
349 - (BOOL)canReleaseDOMLock { 366 - (BOOL)canReleaseDOMLock {
350 return NO; 367 return NO;
351 } 368 }
352 369
353 - (void)releaseDOMLockWithCompletionHandler:(ProceduralBlock)completionHandler { 370 - (void)releaseDOMLockWithCompletionHandler:(ProceduralBlock)completionHandler {
354 NOTREACHED(); 371 NOTREACHED();
355 } 372 }
356 373
357 @end 374 @end
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698