Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame] | 1 | // 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 | |
| 16 | class CommandObserver; |
| 17 | class 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 | // |
| 27 | class 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 Joe | 6f6fc1e | 2019-02-27 20:00:37 | [diff] [blame] | 37 | bool ExecuteCommand( |
| 38 | int id, |
| 39 | base::TimeTicks time_stamp = base::TimeTicks::Now()) override; |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame] | 40 | bool ExecuteCommandWithDisposition( |
Edwin Joe | 6f6fc1e | 2019-02-27 20:00:37 | [diff] [blame] | 41 | int id, |
| 42 | WindowOpenDisposition disposition, |
| 43 | base::TimeTicks time_stamp = base::TimeTicks::Now()) override; |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame] | 44 | 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 Krcal | 8e7558a6 | 2021-07-21 12:24:21 | [diff] [blame] | 55 | struct Command; |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame] | 56 | |
| 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_ |