blob: 59ff772446cde621289fb2828d102bd32412c707 [file] [log] [blame]
Ivan Sandrk9669d0e2017-12-15 23:50:201// Copyright 2017 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_IMPL_H_
6#define CHROME_BROWSER_COMMAND_UPDATER_IMPL_H_
7
8#include <memory>
9#include <unordered_map>
10#include <vector>
11
12#include "chrome/browser/command_updater.h"
13#include "base/macros.h"
14#include "ui/base/window_open_disposition.h"
15
16class CommandObserver;
17class CommandUpdaterDelegate;
18
19////////////////////////////////////////////////////////////////////////////////
20//
21// CommandUpdaterImpl class
22//
23// This object manages the enabled state of a set of commands. Observers
24// register to listen to changes in this state so they can update their
25// presentation.
26//
27class CommandUpdaterImpl : public CommandUpdater {
28 public:
29 // Create a CommandUpdaterImpl with |delegate| to handle the execution of
30 // specific commands.
31 explicit CommandUpdaterImpl(CommandUpdaterDelegate* delegate);
32 ~CommandUpdaterImpl() override;
33
34 // Overriden from CommandUpdater:
35 bool SupportsCommand(int id) const override;
36 bool IsCommandEnabled(int id) const override;
37 bool ExecuteCommand(int id) override;
38 bool ExecuteCommandWithDisposition(
39 int id, WindowOpenDisposition disposition) override;
40 void AddCommandObserver(int id, CommandObserver* observer) override;
41 void RemoveCommandObserver(int id, CommandObserver* observer) override;
42 void RemoveCommandObserver(CommandObserver* observer) override;
43 bool UpdateCommandEnabled(int id, bool state) override;
44
45 void DisableAllCommands();
46 std::vector<int> GetAllIds();
47
48 private:
49 // A piece of data about a command - whether or not it is enabled, and a list
50 // of objects that observe the enabled state of this command.
51 class Command;
52
53 // Get a Command node for a given command ID, creating an entry if it doesn't
54 // exist if desired.
55 Command* GetCommand(int id, bool create);
56
57 // The delegate is responsible for executing commands.
58 CommandUpdaterDelegate* delegate_;
59
60 // This is a map of command IDs to states and observer lists
61 std::unordered_map<int, std::unique_ptr<Command>> commands_;
62
63 DISALLOW_COPY_AND_ASSIGN(CommandUpdaterImpl);
64};
65
66#endif // CHROME_BROWSER_COMMAND_UPDATER_IMPL_H_