[email protected] | ef55702 | 2012-03-16 10:05:33 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 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 | |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 8 | #include "base/basictypes.h" |
| 9 | #include "base/hash_tables.h" |
[email protected] | f47621b | 2013-01-22 20:50:33 | [diff] [blame] | 10 | #include "ui/base/window_open_disposition.h" |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 11 | |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame] | 12 | class CommandObserver; |
[email protected] | de0d0f4 | 2012-12-06 21:27:11 | [diff] [blame] | 13 | class CommandUpdaterDelegate; |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame] | 14 | |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 15 | //////////////////////////////////////////////////////////////////////////////// |
| 16 | // |
| 17 | // CommandUpdater class |
| 18 | // |
| 19 | // This object manages the enabled state of a set of commands. Observers |
| 20 | // register to listen to changes in this state so they can update their |
| 21 | // presentation. |
| 22 | // |
| 23 | class CommandUpdater { |
| 24 | public: |
[email protected] | de0d0f4 | 2012-12-06 21:27:11 | [diff] [blame] | 25 | // Create a CommandUpdater with |delegate| to handle the execution of specific |
| 26 | // commands. |
| 27 | explicit CommandUpdater(CommandUpdaterDelegate* delegate); |
| 28 | ~CommandUpdater(); |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 29 | |
| 30 | // Returns true if the specified command ID is supported. |
| 31 | bool SupportsCommand(int id) const; |
| 32 | |
| 33 | // Returns true if the specified command ID is enabled. The command ID must be |
| 34 | // supported by this updater. |
| 35 | bool IsCommandEnabled(int id) const; |
| 36 | |
[email protected] | ef55702 | 2012-03-16 10:05:33 | [diff] [blame] | 37 | // Performs the action associated with this command ID using CURRENT_TAB |
| 38 | // disposition. |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame] | 39 | // Returns true if the command was executed (i.e. it is supported and is |
| 40 | // enabled). |
| 41 | bool ExecuteCommand(int id); |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 42 | |
[email protected] | ef55702 | 2012-03-16 10:05:33 | [diff] [blame] | 43 | // Performs the action associated with this command ID using the given |
| 44 | // disposition. |
[email protected] | 5d9829491 | 2012-06-27 22:57:40 | [diff] [blame] | 45 | // Returns true if the command was executed (i.e. it is supported and is |
| 46 | // enabled). |
| 47 | bool ExecuteCommandWithDisposition(int id, WindowOpenDisposition disposition); |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 48 | |
| 49 | // Adds an observer to the state of a particular command. If the command does |
| 50 | // not exist, it is created, initialized to false. |
| 51 | void AddCommandObserver(int id, CommandObserver* observer); |
| 52 | |
| 53 | // Removes an observer to the state of a particular command. |
| 54 | void RemoveCommandObserver(int id, CommandObserver* observer); |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 55 | |
[email protected] | a80edd4 | 2009-02-06 22:37:13 | [diff] [blame] | 56 | // Removes |observer| for all commands on which it's registered. |
| 57 | void RemoveCommandObserver(CommandObserver* observer); |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 58 | |
| 59 | // Notify all observers of a particular command that the command has been |
| 60 | // enabled or disabled. If the command does not exist, it is created and |
| 61 | // initialized to |state|. This function is very lightweight if the command |
| 62 | // state has not changed. |
| 63 | void UpdateCommandEnabled(int id, bool state); |
| 64 | |
| 65 | private: |
| 66 | // A piece of data about a command - whether or not it is enabled, and a list |
| 67 | // of objects that observe the enabled state of this command. |
[email protected] | 5c23875 | 2009-06-13 10:29:07 | [diff] [blame] | 68 | class Command; |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 69 | |
| 70 | // Get a Command node for a given command ID, creating an entry if it doesn't |
| 71 | // exist if desired. |
| 72 | Command* GetCommand(int id, bool create); |
| 73 | |
| 74 | // The delegate is responsible for executing commands. |
| 75 | CommandUpdaterDelegate* delegate_; |
| 76 | |
| 77 | // This is a map of command IDs to states and observer lists |
| 78 | typedef base::hash_map<int, Command*> CommandMap; |
| 79 | CommandMap commands_; |
| 80 | |
[email protected] | 4d818fee | 2010-06-06 13:32:27 | [diff] [blame] | 81 | DISALLOW_COPY_AND_ASSIGN(CommandUpdater); |
[email protected] | 1fc02520 | 2009-01-20 23:03:14 | [diff] [blame] | 82 | }; |
| 83 | |
[email protected] | 11f485728 | 2009-11-13 19:56:17 | [diff] [blame] | 84 | #endif // CHROME_BROWSER_COMMAND_UPDATER_H_ |