blob: c0b83b3536d9dd53f83cdb3eca78f772f4270c07 [file] [log] [blame]
[email protected]ef557022012-03-16 10:05:331// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]1fc025202009-01-20 23:03:142// 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]1fc025202009-01-20 23:03:148#include "base/basictypes.h"
9#include "base/hash_tables.h"
[email protected]f47621b2013-01-22 20:50:3310#include "ui/base/window_open_disposition.h"
[email protected]1fc025202009-01-20 23:03:1411
[email protected]5d98294912012-06-27 22:57:4012class CommandObserver;
[email protected]de0d0f42012-12-06 21:27:1113class CommandUpdaterDelegate;
[email protected]5d98294912012-06-27 22:57:4014
[email protected]1fc025202009-01-20 23:03:1415////////////////////////////////////////////////////////////////////////////////
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//
23class CommandUpdater {
24 public:
[email protected]de0d0f42012-12-06 21:27:1125 // Create a CommandUpdater with |delegate| to handle the execution of specific
26 // commands.
27 explicit CommandUpdater(CommandUpdaterDelegate* delegate);
28 ~CommandUpdater();
[email protected]1fc025202009-01-20 23:03:1429
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]ef557022012-03-16 10:05:3337 // Performs the action associated with this command ID using CURRENT_TAB
38 // disposition.
[email protected]5d98294912012-06-27 22:57:4039 // Returns true if the command was executed (i.e. it is supported and is
40 // enabled).
41 bool ExecuteCommand(int id);
[email protected]1fc025202009-01-20 23:03:1442
[email protected]ef557022012-03-16 10:05:3343 // Performs the action associated with this command ID using the given
44 // disposition.
[email protected]5d98294912012-06-27 22:57:4045 // 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]1fc025202009-01-20 23:03:1448
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]f0a51fb52009-03-05 12:46:3855
[email protected]a80edd42009-02-06 22:37:1356 // Removes |observer| for all commands on which it's registered.
57 void RemoveCommandObserver(CommandObserver* observer);
[email protected]1fc025202009-01-20 23:03:1458
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]5c238752009-06-13 10:29:0768 class Command;
[email protected]1fc025202009-01-20 23:03:1469
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]4d818fee2010-06-06 13:32:2781 DISALLOW_COPY_AND_ASSIGN(CommandUpdater);
[email protected]1fc025202009-01-20 23:03:1482};
83
[email protected]11f4857282009-11-13 19:56:1784#endif // CHROME_BROWSER_COMMAND_UPDATER_H_