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