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
3 // found in the LICENSE file.
5 #import "base/ios/crb_protocol_observers.h"
7 #include <objc/runtime.h>
9 #include "base/logging.h"
10 #include "base/mac/scoped_nsobject.h"
12 @interface CRBProtocolObservers ()
14 // Designated initializer.
15 - (id)initWithProtocol:(Protocol*)protocol;
19 @implementation CRBProtocolObservers {
20 base::scoped_nsobject<Protocol> _protocol;
21 base::scoped_nsobject<NSHashTable> _observers;
24 + (CRBProtocolObservers*)observersWithProtocol:(Protocol*)protocol {
25 return [[[self alloc] initWithProtocol:protocol] autorelease];
33 - (id)initWithProtocol:(Protocol*)protocol {
36 _protocol.reset([protocol retain]);
37 _observers.reset([[NSHashTable weakObjectsHashTable] retain]);
42 - (Protocol*)protocol {
43 return _protocol.get();
46 - (void)addObserver:(id)observer {
47 DCHECK([observer conformsToProtocol:self.protocol]);
48 [_observers addObject:observer];
51 - (void)removeObserver:(id)observer {
52 [_observers removeObject:observer];
55 #pragma mark - NSObject
57 - (NSMethodSignature*)methodSignatureForSelector:(SEL)selector {
58 NSMethodSignature* signature = [super methodSignatureForSelector:selector];
62 // Look for a required method in the protocol. protocol_getMethodDescription
63 // returns a struct whose fields are null if a method for the selector was
65 struct objc_method_description description =
66 protocol_getMethodDescription(self.protocol, selector, YES, YES);
67 if (description.types)
68 return [NSMethodSignature signatureWithObjCTypes:description.types];
70 // Look for an optional method in the protocol.
71 description = protocol_getMethodDescription(self.protocol, selector, NO, YES);
72 if (description.types)
73 return [NSMethodSignature signatureWithObjCTypes:description.types];
75 // There is neither a required nor optional method with this selector in the
76 // protocol, so invoke -[NSObject doesNotRecognizeSelector:] to raise
77 // NSInvalidArgumentException.
78 [self doesNotRecognizeSelector:selector];
82 - (void)forwardInvocation:(NSInvocation*)invocation {
83 SEL selector = [invocation selector];
84 base::scoped_nsobject<NSArray> observers([[_observers allObjects] retain]);
85 for (id observer in observers.get()) {
86 if ([observer respondsToSelector:selector])
87 [invocation invokeWithTarget:observer];
91 - (void)executeOnObservers:(ExecutionWithObserverBlock)callback {
93 base::scoped_nsobject<NSArray> observers([[_observers allObjects] retain]);
94 for (id observer in observers.get()) {