crb_protocol_observers.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. #ifndef BASE_IOS_CRB_PROTOCOL_OBSERVERS_H_
  5. #define BASE_IOS_CRB_PROTOCOL_OBSERVERS_H_
  6. #import <Foundation/Foundation.h>
  7. typedef void (^ExecutionWithObserverBlock)(id);
  8. // Implements a container for observers that implement a specific Objective-C
  9. // protocol. The container forwards method invocations to its contained
  10. // observers, so that sending a message to all the observers is as simple as
  11. // sending the message to the container.
  12. // It is safe for an observer to remove itself or another observer while being
  13. // notified. It is also safe to add an other observer while being notified but
  14. // the newly added observer will not be notified as part of the current
  15. // notification dispatch.
  16. @interface CRBProtocolObservers : NSObject
  17. // The Objective-C protocol that the observers in this container conform to.
  18. @property(nonatomic, readonly) Protocol* protocol;
  19. // Returns a CRBProtocolObservers container for observers that conform to
  20. // |protocol|.
  21. + (instancetype)observersWithProtocol:(Protocol*)protocol;
  22. // Adds |observer| to this container.
  23. - (void)addObserver:(id)observer;
  24. // Remove |observer| from this container.
  25. - (void)removeObserver:(id)observer;
  26. // Returns true if there are currently no observers.
  27. - (BOOL)empty;
  28. // Executes callback on every observer. |callback| cannot be nil.
  29. - (void)executeOnObservers:(ExecutionWithObserverBlock)callback;
  30. @end
  31. #endif // BASE_IOS_CRB_PROTOCOL_OBSERVERS_H_