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