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

Side by Side Diff: base/ios/crb_protocol_observers.mm

Issue 935333002: Update from https://crrev.com/316786 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 10 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 | « base/ios/crb_protocol_observers.h ('k') | base/ios/crb_protocol_observers_unittest.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #import "base/ios/crb_protocol_observers.h"
6
7 #include <objc/runtime.h>
8
9 #include "base/logging.h"
10 #include "base/mac/scoped_nsobject.h"
11
12 @interface CRBProtocolObservers ()
13
14 // Designated initializer.
15 - (id)initWithProtocol:(Protocol*)protocol;
16
17 @end
18
19 @implementation CRBProtocolObservers {
20 base::scoped_nsobject<Protocol> _protocol;
21 base::scoped_nsobject<NSHashTable> _observers;
22 }
23
24 + (CRBProtocolObservers*)observersWithProtocol:(Protocol*)protocol {
25 return [[[self alloc] initWithProtocol:protocol] autorelease];
26 }
27
28 - (id)init {
29 NOTREACHED();
30 return nil;
31 }
32
33 - (id)initWithProtocol:(Protocol*)protocol {
34 self = [super init];
35 if (self) {
36 _protocol.reset([protocol retain]);
37 _observers.reset([[NSHashTable weakObjectsHashTable] retain]);
38 }
39 return self;
40 }
41
42 - (Protocol*)protocol {
43 return _protocol.get();
44 }
45
46 - (void)addObserver:(id)observer {
47 DCHECK([observer conformsToProtocol:self.protocol]);
48 [_observers addObject:observer];
49 }
50
51 - (void)removeObserver:(id)observer {
52 [_observers removeObject:observer];
53 }
54
55 #pragma mark - NSObject
56
57 - (NSMethodSignature*)methodSignatureForSelector:(SEL)selector {
58 NSMethodSignature* signature = [super methodSignatureForSelector:selector];
59 if (signature)
60 return signature;
61
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
64 // not found.
65 struct objc_method_description description =
66 protocol_getMethodDescription(self.protocol, selector, YES, YES);
67 if (description.types)
68 return [NSMethodSignature signatureWithObjCTypes:description.types];
69
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];
74
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];
79 return nil;
80 }
81
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];
88 }
89 }
90
91 - (void)executeOnObservers:(ExecutionWithObserverBlock)callback {
92 DCHECK(callback);
93 base::scoped_nsobject<NSArray> observers([[_observers allObjects] retain]);
94 for (id observer in observers.get()) {
95 callback(observer);
96 }
97 }
98
99 @end
OLDNEW
« no previous file with comments | « base/ios/crb_protocol_observers.h ('k') | base/ios/crb_protocol_observers_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698