Chromium Code Reviews| Index: ios/web_view/test/boolean_observer.mm |
| diff --git a/ios/web_view/test/boolean_observer.mm b/ios/web_view/test/boolean_observer.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f1b75b83aa8839ed58a7b610bc966a4e424844f3 |
| --- /dev/null |
| +++ b/ios/web_view/test/boolean_observer.mm |
| @@ -0,0 +1,48 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "ios/web_view/test/boolean_observer.h" |
| + |
| +#if !defined(__has_feature) || !__has_feature(objc_arc) |
| +#error "This file requires ARC support." |
| +#endif |
| + |
| +@implementation BooleanObserver |
| + |
| +@synthesize keyPath = _keyPath; |
| +@synthesize lastValue = _lastValue; |
| +@synthesize object = _object; |
| + |
| +- (void)setObservedObject:(NSObject*)object keyPath:(NSString*)keyPath { |
| + if (_object) { |
|
Eugene But (OOO till 7-30)
2017/05/23 19:49:33
No need for this check, it's perfectly fine to cal
michaeldo
2017/05/24 15:02:34
Done.
|
| + [_object removeObserver:self forKeyPath:_keyPath]; |
| + } |
| + |
| + _lastValue = NO; |
| + _keyPath = [keyPath copy]; |
| + _object = object; |
| + if (keyPath) { |
|
Eugene But (OOO till 7-30)
2017/05/23 19:49:33
Is is there a value in passing nil keyPath? Should
michaeldo
2017/05/24 15:02:34
It's the only current way to stop this object from
Eugene But (OOO till 7-30)
2017/05/24 15:32:14
I think if we need API for stopping the observing,
|
| + [_object addObserver:self |
| + forKeyPath:_keyPath |
| + options:NSKeyValueObservingOptionNew |
| + context:nil]; |
| + } |
| +} |
| + |
| +- (void)observeValueForKeyPath:(NSString*)keyPath |
| + ofObject:(id)object |
| + change:(NSDictionary<NSKeyValueChangeKey, id>*)change |
| + context:(void*)context { |
| + if (object != _object || keyPath != _keyPath) { |
| + // Ignore extraneous call from previous |_object| or |_keyPath|. |
| + return; |
| + } |
| + _lastValue = [change[NSKeyValueChangeNewKey] boolValue]; |
| +} |
| + |
| +- (void)dealloc { |
| + [_object removeObserver:self forKeyPath:_keyPath]; |
| +} |
| + |
| +@end |