Home | History | Annotate | Download | only in cocoa
      1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_UI_COCOA_COMMAND_OBSERVER_BRIDGE
      6 #define CHROME_BROWSER_UI_COCOA_COMMAND_OBSERVER_BRIDGE
      7 
      8 #import <Cocoa/Cocoa.h>
      9 
     10 #include "base/compiler_specific.h"
     11 #include "chrome/browser/command_observer.h"
     12 
     13 @protocol CommandObserverProtocol;
     14 
     15 class CommandUpdater;
     16 
     17 // A C++ bridge class that handles listening for updates to commands and
     18 // passing them back to an object that supports the protocol delcared below.
     19 // The observer will create one of these bridges, call ObserveCommand() on the
     20 // command ids it cares about, and then wait for update notifications,
     21 // delivered via -enabledStateChangedForCommand:enabled:. Destroying this
     22 // bridge will handle automatically unregistering for updates, so there's no
     23 // need to do that manually.
     24 
     25 class CommandObserverBridge : public CommandObserver {
     26  public:
     27   CommandObserverBridge(id<CommandObserverProtocol> observer,
     28                         CommandUpdater* commands);
     29   virtual ~CommandObserverBridge();
     30 
     31   // Register for updates about |command|.
     32   void ObserveCommand(int command);
     33 
     34  protected:
     35   // Overridden from CommandObserver
     36   virtual void EnabledStateChangedForCommand(int command,
     37                                              bool enabled) OVERRIDE;
     38 
     39  private:
     40   id<CommandObserverProtocol> observer_;  // weak, owns me
     41   CommandUpdater* commands_;  // weak
     42 };
     43 
     44 // Implemented by the observing Objective-C object, called when there is a
     45 // state change for the given command.
     46 @protocol CommandObserverProtocol
     47 - (void)enabledStateChangedForCommand:(NSInteger)command enabled:(BOOL)enabled;
     48 @end
     49 
     50 #endif  // CHROME_BROWSER_UI_COCOA_COMMAND_OBSERVER_BRIDGE
     51