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