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..023f3173d06c480c5fcfd63a09deb1619dc7270a |
| --- /dev/null |
| +++ b/ios/web_view/test/boolean_observer.mm |
| @@ -0,0 +1,54 @@ |
| +// 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" |
| + |
| +#include "base/logging.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) { |
| + [_object removeObserver:self forKeyPath:_keyPath]; |
| + } |
| + |
| + _lastValue = NO; |
| + _keyPath = [keyPath copy]; |
| + _object = object; |
| + [_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; |
| + } |
| + |
| + SEL selector = NSSelectorFromString(keyPath); |
| + DCHECK([_object respondsToSelector:selector]); |
| +#pragma clang diagnostic push |
| +#pragma clang diagnostic ignored "-Warc-performSelector-leaks" |
| + _lastValue = [object performSelector:selector]; |
|
Eugene But (OOO till 7-30)
2017/05/19 18:39:48
Is this something you can get from |change| dictio
michaeldo
2017/05/23 16:20:51
Yes, it is cleaner to get out of |change|. Done.
|
| +#pragma clang diagnostic pop |
| +} |
| + |
| +- (void)dealloc { |
| + [_object removeObserver:self forKeyPath:_keyPath]; |
| +} |
| + |
| +@end |