OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_COMMAND_UPDATER_H_ | 5 #ifndef CHROME_BROWSER_COMMAND_UPDATER_H_ |
6 #define CHROME_BROWSER_COMMAND_UPDATER_H_ | 6 #define CHROME_BROWSER_COMMAND_UPDATER_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/hash_tables.h" | 9 #include "base/hash_tables.h" |
10 | 10 |
11 //////////////////////////////////////////////////////////////////////////////// | 11 //////////////////////////////////////////////////////////////////////////////// |
12 // | 12 // |
13 // CommandUpdater class | 13 // CommandUpdater class |
14 // | 14 // |
15 // This object manages the enabled state of a set of commands. Observers | 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 | 16 // register to listen to changes in this state so they can update their |
17 // presentation. | 17 // presentation. |
18 // | 18 // |
19 class CommandUpdater { | 19 class CommandUpdater { |
20 public: | 20 public: |
21 // A Delegate object implements this interface so that it can execute commands | 21 // A Delegate object implements this interface so that it can execute commands |
22 // when needed. | 22 // when needed. |
23 class CommandUpdaterDelegate { | 23 class CommandUpdaterDelegate { |
24 public: | 24 public: |
25 // Perform the action associated with the command with the specified ID. | 25 // Perform the action associated with the command with the specified ID. |
26 virtual void ExecuteCommand(int id) = 0; | 26 virtual void ExecuteCommand(int id) = 0; |
27 | 27 |
28 protected: | 28 protected: |
29 virtual ~CommandUpdaterDelegate() {} | 29 virtual ~CommandUpdaterDelegate(); |
30 }; | 30 }; |
31 | 31 |
32 // Create a CommandUpdater with a CommandUpdaterDelegate to handle execution | 32 // Create a CommandUpdater with a CommandUpdaterDelegate to handle execution |
33 // of specific commands. | 33 // of specific commands. |
34 explicit CommandUpdater(CommandUpdaterDelegate* handler); | 34 explicit CommandUpdater(CommandUpdaterDelegate* handler); |
35 virtual ~CommandUpdater(); | 35 virtual ~CommandUpdater(); |
36 | 36 |
37 // Returns true if the specified command ID is supported. | 37 // Returns true if the specified command ID is supported. |
38 bool SupportsCommand(int id) const; | 38 bool SupportsCommand(int id) const; |
39 | 39 |
40 // Returns true if the specified command ID is enabled. The command ID must be | 40 // Returns true if the specified command ID is enabled. The command ID must be |
41 // supported by this updater. | 41 // supported by this updater. |
42 bool IsCommandEnabled(int id) const; | 42 bool IsCommandEnabled(int id) const; |
43 | 43 |
44 // Performs the action associated with this command ID. | 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 | 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. | 46 // call sites would be better off using more well defined delegate interfaces. |
47 void ExecuteCommand(int id); | 47 void ExecuteCommand(int id); |
48 | 48 |
49 // An Observer interface implemented by objects that want to be informed when | 49 // An Observer interface implemented by objects that want to be informed when |
50 // the state of a particular command ID is modified. | 50 // the state of a particular command ID is modified. |
51 class CommandObserver { | 51 class CommandObserver { |
52 public: | 52 public: |
53 // Notifies the observer that the enabled state has changed for the | 53 // Notifies the observer that the enabled state has changed for the |
54 // specified command id. | 54 // specified command id. |
55 virtual void EnabledStateChangedForCommand(int id, bool enabled) = 0; | 55 virtual void EnabledStateChangedForCommand(int id, bool enabled) = 0; |
56 | 56 |
57 protected: | 57 protected: |
58 virtual ~CommandObserver() {} | 58 virtual ~CommandObserver(); |
59 }; | 59 }; |
60 | 60 |
61 // Adds an observer to the state of a particular command. If the command does | 61 // Adds an observer to the state of a particular command. If the command does |
62 // not exist, it is created, initialized to false. | 62 // not exist, it is created, initialized to false. |
63 void AddCommandObserver(int id, CommandObserver* observer); | 63 void AddCommandObserver(int id, CommandObserver* observer); |
64 | 64 |
65 // Removes an observer to the state of a particular command. | 65 // Removes an observer to the state of a particular command. |
66 void RemoveCommandObserver(int id, CommandObserver* observer); | 66 void RemoveCommandObserver(int id, CommandObserver* observer); |
67 | 67 |
68 // Removes |observer| for all commands on which it's registered. | 68 // Removes |observer| for all commands on which it's registered. |
(...skipping 19 matching lines...) Expand all Loading... |
88 | 88 |
89 // This is a map of command IDs to states and observer lists | 89 // This is a map of command IDs to states and observer lists |
90 typedef base::hash_map<int, Command*> CommandMap; | 90 typedef base::hash_map<int, Command*> CommandMap; |
91 CommandMap commands_; | 91 CommandMap commands_; |
92 | 92 |
93 CommandUpdater(); | 93 CommandUpdater(); |
94 DISALLOW_COPY_AND_ASSIGN(CommandUpdater); | 94 DISALLOW_COPY_AND_ASSIGN(CommandUpdater); |
95 }; | 95 }; |
96 | 96 |
97 #endif // CHROME_BROWSER_COMMAND_UPDATER_H_ | 97 #endif // CHROME_BROWSER_COMMAND_UPDATER_H_ |
OLD | NEW |