Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
stuartmorgan | 4733f55 | 2015-02-14 22:19:30 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_IOS_CRB_PROTOCOL_OBSERVERS_H_ |
| 6 | #define BASE_IOS_CRB_PROTOCOL_OBSERVERS_H_ |
| 7 | |
| 8 | #import <Foundation/Foundation.h> |
| 9 | |
| 10 | typedef void (^ExecutionWithObserverBlock)(id); |
| 11 | |
| 12 | // Implements a container for observers that implement a specific Objective-C |
| 13 | // protocol. The container forwards method invocations to its contained |
| 14 | // observers, so that sending a message to all the observers is as simple as |
| 15 | // sending the message to the container. |
jbbegue | 12d50e7 | 2015-06-05 08:12:43 | [diff] [blame] | 16 | // It is safe for an observer to remove itself or another observer while being |
| 17 | // notified. It is also safe to add an other observer while being notified but |
| 18 | // the newly added observer will not be notified as part of the current |
| 19 | // notification dispatch. |
stuartmorgan | 4733f55 | 2015-02-14 22:19:30 | [diff] [blame] | 20 | @interface CRBProtocolObservers : NSObject |
| 21 | |
| 22 | // The Objective-C protocol that the observers in this container conform to. |
| 23 | @property(nonatomic, readonly) Protocol* protocol; |
| 24 | |
| 25 | // Returns a CRBProtocolObservers container for observers that conform to |
| 26 | // |protocol|. |
jbbegue | 12d50e7 | 2015-06-05 08:12:43 | [diff] [blame] | 27 | + (instancetype)observersWithProtocol:(Protocol*)protocol; |
stuartmorgan | 4733f55 | 2015-02-14 22:19:30 | [diff] [blame] | 28 | |
| 29 | // Adds |observer| to this container. |
| 30 | - (void)addObserver:(id)observer; |
| 31 | |
| 32 | // Remove |observer| from this container. |
| 33 | - (void)removeObserver:(id)observer; |
| 34 | |
jbbegue | 12d50e7 | 2015-06-05 08:12:43 | [diff] [blame] | 35 | // Returns true if there are currently no observers. |
| 36 | - (BOOL)empty; |
| 37 | |
stuartmorgan | 4733f55 | 2015-02-14 22:19:30 | [diff] [blame] | 38 | // Executes callback on every observer. |callback| cannot be nil. |
| 39 | - (void)executeOnObservers:(ExecutionWithObserverBlock)callback; |
| 40 | |
| 41 | @end |
| 42 | |
| 43 | #endif // BASE_IOS_CRB_PROTOCOL_OBSERVERS_H_ |