command_updater.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2010 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_COMMAND_UPDATER_H_
6#define CHROME_BROWSER_COMMAND_UPDATER_H_
7
8#include "base/basictypes.h"
9#include "base/hash_tables.h"
10
11////////////////////////////////////////////////////////////////////////////////
12//
13// CommandUpdater class
14//
15//   This object manages the enabled state of a set of commands. Observers
16//   register to listen to changes in this state so they can update their
17//   presentation.
18//
19class CommandUpdater {
20 public:
21  // A Delegate object implements this interface so that it can execute commands
22  // when needed.
23  class CommandUpdaterDelegate {
24   public:
25    // Perform the action associated with the command with the specified ID.
26    virtual void ExecuteCommand(int id) = 0;
27
28   protected:
29    virtual ~CommandUpdaterDelegate();
30  };
31
32  // Create a CommandUpdater with a CommandUpdaterDelegate to handle execution
33  // of specific commands.
34  explicit CommandUpdater(CommandUpdaterDelegate* handler);
35  virtual ~CommandUpdater();
36
37  // Returns true if the specified command ID is supported.
38  bool SupportsCommand(int id) const;
39
40  // Returns true if the specified command ID is enabled. The command ID must be
41  // supported by this updater.
42  bool IsCommandEnabled(int id) const;
43
44  // Performs the action associated with this command ID.
45  // TODO(beng): get rid of this since it's effectively just a pass-thru and the
46  // call sites would be better off using more well defined delegate interfaces.
47  void ExecuteCommand(int id);
48
49  // An Observer interface implemented by objects that want to be informed when
50  // the state of a particular command ID is modified.
51  class CommandObserver {
52   public:
53    // Notifies the observer that the enabled state has changed for the
54    // specified command id.
55    virtual void EnabledStateChangedForCommand(int id, bool enabled) = 0;
56
57   protected:
58    virtual ~CommandObserver();
59  };
60
61  // Adds an observer to the state of a particular command. If the command does
62  // not exist, it is created, initialized to false.
63  void AddCommandObserver(int id, CommandObserver* observer);
64
65  // Removes an observer to the state of a particular command.
66  void RemoveCommandObserver(int id, CommandObserver* observer);
67
68  // Removes |observer| for all commands on which it's registered.
69  void RemoveCommandObserver(CommandObserver* observer);
70
71  // Notify all observers of a particular command that the command has been
72  // enabled or disabled. If the command does not exist, it is created and
73  // initialized to |state|. This function is very lightweight if the command
74  // state has not changed.
75  void UpdateCommandEnabled(int id, bool state);
76
77 private:
78  // A piece of data about a command - whether or not it is enabled, and a list
79  // of objects that observe the enabled state of this command.
80  class Command;
81
82  // Get a Command node for a given command ID, creating an entry if it doesn't
83  // exist if desired.
84  Command* GetCommand(int id, bool create);
85
86  // The delegate is responsible for executing commands.
87  CommandUpdaterDelegate* delegate_;
88
89  // This is a map of command IDs to states and observer lists
90  typedef base::hash_map<int, Command*> CommandMap;
91  CommandMap commands_;
92
93  CommandUpdater();
94  DISALLOW_COPY_AND_ASSIGN(CommandUpdater);
95};
96
97#endif  // CHROME_BROWSER_COMMAND_UPDATER_H_
98