blob: 1671de36eb22a0e6048b6257572bf6a98c6f2a38 [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;
Edwin Joe6f6fc1e2019-02-27 20:00:3737 bool ExecuteCommand(
38 int id,
39 base::TimeTicks time_stamp = base::TimeTicks::Now()) override;
Ivan Sandrk9669d0e2017-12-15 23:50:2040 bool ExecuteCommandWithDisposition(
Edwin Joe6f6fc1e2019-02-27 20:00:3741 int id,
42 WindowOpenDisposition disposition,
43 base::TimeTicks time_stamp = base::TimeTicks::Now()) override;
Ivan Sandrk9669d0e2017-12-15 23:50:2044 void AddCommandObserver(int id, CommandObserver* observer) override;
45 void RemoveCommandObserver(int id, CommandObserver* observer) override;
46 void RemoveCommandObserver(CommandObserver* observer) override;
47 bool UpdateCommandEnabled(int id, bool state) override;
48
49 void DisableAllCommands();
50 std::vector<int> GetAllIds();
51
52 private:
53 // A piece of data about a command - whether or not it is enabled, and a list
54 // of objects that observe the enabled state of this command.
Jan Krcal8e7558a62021-07-21 12:24:2155 struct Command;
Ivan Sandrk9669d0e2017-12-15 23:50:2056
57 // Get a Command node for a given command ID, creating an entry if it doesn't
58 // exist if desired.
59 Command* GetCommand(int id, bool create);
60
61 // The delegate is responsible for executing commands.
62 CommandUpdaterDelegate* delegate_;
63
64 // This is a map of command IDs to states and observer lists
65 std::unordered_map<int, std::unique_ptr<Command>> commands_;
66
67 DISALLOW_COPY_AND_ASSIGN(CommandUpdaterImpl);
68};
69
70#endif // CHROME_BROWSER_COMMAND_UPDATER_IMPL_H_