• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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/containers/hash_tables.h"
10 #include "ui/base/window_open_disposition.h"
11 
12 class CommandObserver;
13 class CommandUpdaterDelegate;
14 
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:
25   // Create a CommandUpdater with |delegate| to handle the execution of specific
26   // commands.
27   explicit CommandUpdater(CommandUpdaterDelegate* delegate);
28   ~CommandUpdater();
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 
37   // Performs the action associated with this command ID using CURRENT_TAB
38   // disposition.
39   // Returns true if the command was executed (i.e. it is supported and is
40   // enabled).
41   bool ExecuteCommand(int id);
42 
43   // Performs the action associated with this command ID using the given
44   // disposition.
45   // Returns true if the command was executed (i.e. it is supported and is
46   // enabled).
47   bool ExecuteCommandWithDisposition(int id, WindowOpenDisposition disposition);
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);
55 
56   // Removes |observer| for all commands on which it's registered.
57   void RemoveCommandObserver(CommandObserver* observer);
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.
68   class Command;
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 
81   DISALLOW_COPY_AND_ASSIGN(CommandUpdater);
82 };
83 
84 #endif  // CHROME_BROWSER_COMMAND_UPDATER_H_
85