Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/public/web_state/crw_web_view_scroll_view_proxy.h" | 5 #import "ios/web/public/web_state/crw_web_view_scroll_view_proxy.h" |
| 6 | 6 |
| 7 #import <objc/runtime.h> | |
|
marq (ping after 24h)
2016/10/04 12:49:05
Is this needed?
stkhapugin
2016/10/04 13:02:50
Yes, before it was transitevly included from base/
| |
| 8 | |
| 7 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
| 8 #import "base/ios/crb_protocol_observers.h" | 10 #import "base/ios/crb_protocol_observers.h" |
| 9 #import "base/ios/weak_nsobject.h" | |
| 10 #include "base/mac/foundation_util.h" | 11 #include "base/mac/foundation_util.h" |
| 11 #import "base/mac/scoped_nsobject.h" | 12 #import "base/mac/scoped_nsobject.h" |
| 12 | 13 |
| 14 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 15 #error "This file requires ARC support." | |
| 16 #endif | |
| 17 | |
| 13 @interface CRWWebViewScrollViewProxy () { | 18 @interface CRWWebViewScrollViewProxy () { |
| 14 base::WeakNSObject<UIScrollView> _scrollView; | 19 __weak UIScrollView* _scrollView; |
| 15 base::scoped_nsobject<id> _observers; | 20 base::scoped_nsobject<id> _observers; |
|
marq (ping after 24h)
2016/10/04 12:49:05
Why not convert this to a strong property as well?
stkhapugin
2016/10/04 13:02:50
The plan is to remove scoped_nsobject boilerplate
| |
| 16 // When |_ignoreScroll| is set to YES, do not pass on -scrollViewDidScroll | 21 // When |_ignoreScroll| is set to YES, do not pass on -scrollViewDidScroll |
| 17 // calls to observers. This is used by -setContentInsetFast, which needs to | 22 // calls to observers. This is used by -setContentInsetFast, which needs to |
| 18 // update and reset the contentOffset to force a fast update. These updates | 23 // update and reset the contentOffset to force a fast update. These updates |
| 19 // should be a no-op for the contentOffset, so the callbacks can be ignored. | 24 // should be a no-op for the contentOffset, so the callbacks can be ignored. |
| 20 BOOL _ignoreScroll; | 25 BOOL _ignoreScroll; |
| 21 } | 26 } |
| 22 | 27 |
| 23 // Returns the key paths that need to be observed for UIScrollView. | 28 // Returns the key paths that need to be observed for UIScrollView. |
| 24 + (NSArray*)scrollViewObserverKeyPaths; | 29 + (NSArray*)scrollViewObserverKeyPaths; |
| 25 | 30 |
| 26 // Adds and removes |self| as an observer for |scrollView| with key paths | 31 // Adds and removes |self| as an observer for |scrollView| with key paths |
| 27 // returned by |+scrollViewObserverKeyPaths|. | 32 // returned by |+scrollViewObserverKeyPaths|. |
| 28 - (void)startObservingScrollView:(UIScrollView*)scrollView; | 33 - (void)startObservingScrollView:(UIScrollView*)scrollView; |
| 29 - (void)stopObservingScrollView:(UIScrollView*)scrollView; | 34 - (void)stopObservingScrollView:(UIScrollView*)scrollView; |
| 30 | 35 |
| 31 @end | 36 @end |
| 32 | 37 |
| 33 @implementation CRWWebViewScrollViewProxy | 38 @implementation CRWWebViewScrollViewProxy |
| 34 | 39 |
| 35 - (instancetype)init { | 40 - (instancetype)init { |
| 36 self = [super init]; | 41 self = [super init]; |
| 37 if (self) { | 42 if (self) { |
| 38 Protocol* protocol = @protocol(CRWWebViewScrollViewProxyObserver); | 43 Protocol* protocol = @protocol(CRWWebViewScrollViewProxyObserver); |
| 39 _observers.reset( | 44 _observers.reset([CRBProtocolObservers observersWithProtocol:protocol]); |
| 40 [[CRBProtocolObservers observersWithProtocol:protocol] retain]); | |
| 41 } | 45 } |
| 42 return self; | 46 return self; |
| 43 } | 47 } |
| 44 | 48 |
| 45 - (void)dealloc { | 49 - (void)dealloc { |
| 46 [self stopObservingScrollView:_scrollView]; | 50 [self stopObservingScrollView:_scrollView]; |
| 47 [super dealloc]; | |
| 48 } | 51 } |
| 49 | 52 |
| 50 - (void)addGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer { | 53 - (void)addGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer { |
| 51 [_scrollView addGestureRecognizer:gestureRecognizer]; | 54 [_scrollView addGestureRecognizer:gestureRecognizer]; |
| 52 } | 55 } |
| 53 | 56 |
| 54 - (void)removeGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer { | 57 - (void)removeGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer { |
| 55 [_scrollView removeGestureRecognizer:gestureRecognizer]; | 58 [_scrollView removeGestureRecognizer:gestureRecognizer]; |
| 56 } | 59 } |
| 57 | 60 |
| 58 - (void)addObserver:(id<CRWWebViewScrollViewProxyObserver>)observer { | 61 - (void)addObserver:(id<CRWWebViewScrollViewProxyObserver>)observer { |
| 59 [_observers addObserver:observer]; | 62 [_observers addObserver:observer]; |
| 60 } | 63 } |
| 61 | 64 |
| 62 - (void)removeObserver:(id<CRWWebViewScrollViewProxyObserver>)observer { | 65 - (void)removeObserver:(id<CRWWebViewScrollViewProxyObserver>)observer { |
| 63 [_observers removeObserver:observer]; | 66 [_observers removeObserver:observer]; |
| 64 } | 67 } |
| 65 | 68 |
| 66 - (void)setScrollView:(UIScrollView*)scrollView { | 69 - (void)setScrollView:(UIScrollView*)scrollView { |
| 67 if (_scrollView == scrollView) | 70 if (_scrollView == scrollView) |
| 68 return; | 71 return; |
| 69 [_scrollView setDelegate:nil]; | 72 [_scrollView setDelegate:nil]; |
| 70 [self stopObservingScrollView:_scrollView]; | 73 [self stopObservingScrollView:_scrollView]; |
| 71 DCHECK(!scrollView.delegate); | 74 DCHECK(!scrollView.delegate); |
| 72 scrollView.delegate = self; | 75 scrollView.delegate = self; |
| 73 [self startObservingScrollView:scrollView]; | 76 [self startObservingScrollView:scrollView]; |
| 74 _scrollView.reset(scrollView); | 77 _scrollView = scrollView; |
| 75 [_observers webViewScrollViewProxyDidSetScrollView:self]; | 78 [_observers webViewScrollViewProxyDidSetScrollView:self]; |
| 76 } | 79 } |
| 77 | 80 |
| 78 - (CGRect)frame { | 81 - (CGRect)frame { |
| 79 return _scrollView ? [_scrollView frame] : CGRectZero; | 82 return _scrollView ? [_scrollView frame] : CGRectZero; |
| 80 } | 83 } |
| 81 | 84 |
| 82 - (BOOL)isScrollEnabled { | 85 - (BOOL)isScrollEnabled { |
| 83 return [_scrollView isScrollEnabled]; | 86 return [_scrollView isScrollEnabled]; |
| 84 } | 87 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 }); | 137 }); |
| 135 scrollViewSetContentInsetImpl(_scrollView, setContentInset, contentInset); | 138 scrollViewSetContentInsetImpl(_scrollView, setContentInset, contentInset); |
| 136 | 139 |
| 137 // Change and then reset the contentOffset to force the view into updating the | 140 // Change and then reset the contentOffset to force the view into updating the |
| 138 // absolute position of elements and content frame. Updating the | 141 // absolute position of elements and content frame. Updating the |
| 139 // contentOffset will cause the -scrollViewDidScroll callback to fire. | 142 // contentOffset will cause the -scrollViewDidScroll callback to fire. |
| 140 // Because we are eventually setting the contentOffset back to it's original | 143 // Because we are eventually setting the contentOffset back to it's original |
| 141 // position, we can ignore these calls. | 144 // position, we can ignore these calls. |
| 142 base::AutoReset<BOOL> autoReset(&_ignoreScroll, YES); | 145 base::AutoReset<BOOL> autoReset(&_ignoreScroll, YES); |
| 143 CGPoint contentOffset = [_scrollView contentOffset]; | 146 CGPoint contentOffset = [_scrollView contentOffset]; |
| 144 _scrollView.get().contentOffset = | 147 _scrollView.contentOffset = CGPointMake(contentOffset.x, contentOffset.y + 1); |
| 145 CGPointMake(contentOffset.x, contentOffset.y + 1); | 148 _scrollView.contentOffset = contentOffset; |
| 146 _scrollView.get().contentOffset = contentOffset; | |
| 147 } | 149 } |
| 148 | 150 |
| 149 - (void)setContentInset:(UIEdgeInsets)contentInset { | 151 - (void)setContentInset:(UIEdgeInsets)contentInset { |
| 150 [_scrollView setContentInset:contentInset]; | 152 [_scrollView setContentInset:contentInset]; |
| 151 } | 153 } |
| 152 | 154 |
| 153 - (UIEdgeInsets)contentInset { | 155 - (UIEdgeInsets)contentInset { |
| 154 return _scrollView ? [_scrollView contentInset] : UIEdgeInsetsZero; | 156 return _scrollView ? [_scrollView contentInset] : UIEdgeInsetsZero; |
| 155 } | 157 } |
| 156 | 158 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 | 259 |
| 258 - (void)stopObservingScrollView:(UIScrollView*)scrollView { | 260 - (void)stopObservingScrollView:(UIScrollView*)scrollView { |
| 259 for (NSString* keyPath in [[self class] scrollViewObserverKeyPaths]) | 261 for (NSString* keyPath in [[self class] scrollViewObserverKeyPaths]) |
| 260 [scrollView removeObserver:self forKeyPath:keyPath]; | 262 [scrollView removeObserver:self forKeyPath:keyPath]; |
| 261 } | 263 } |
| 262 | 264 |
| 263 - (void)observeValueForKeyPath:(NSString*)keyPath | 265 - (void)observeValueForKeyPath:(NSString*)keyPath |
| 264 ofObject:(id)object | 266 ofObject:(id)object |
| 265 change:(NSDictionary*)change | 267 change:(NSDictionary*)change |
| 266 context:(void*)context { | 268 context:(void*)context { |
| 267 DCHECK_EQ(object, _scrollView.get()); | 269 DCHECK_EQ(object, _scrollView); |
| 268 if ([keyPath isEqualToString:@"contentSize"]) | 270 if ([keyPath isEqualToString:@"contentSize"]) |
| 269 [_observers webViewScrollViewDidResetContentSize:self]; | 271 [_observers webViewScrollViewDidResetContentSize:self]; |
| 270 } | 272 } |
| 271 | 273 |
| 272 @end | 274 @end |
| OLD | NEW |