Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/command_updater_impl.h" |
| 6 | |
| 7 | #include <algorithm> |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 8 | #include <optional> |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame] | 9 | |
Hans Wennborg | 1790e6b | 2020-04-24 19:10:33 | [diff] [blame] | 10 | #include "base/check.h" |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame] | 11 | #include "base/observer_list.h" |
| 12 | #include "chrome/browser/command_observer.h" |
| 13 | #include "chrome/browser/command_updater_delegate.h" |
| 14 | |
Jan Krcal | 8e7558a6 | 2021-07-21 12:24:21 | [diff] [blame] | 15 | struct CommandUpdaterImpl::Command { |
| 16 | // Empty optional means not specified yet and thus implicitly disabled. |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 17 | std::optional<bool> enabled; |
mikt | bacc1fc | 2024-03-07 16:38:20 | [diff] [blame] | 18 | base::ObserverList<CommandObserver>::UncheckedAndDanglingUntriaged observers; |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame] | 19 | }; |
| 20 | |
| 21 | CommandUpdaterImpl::CommandUpdaterImpl(CommandUpdaterDelegate* delegate) |
| 22 | : delegate_(delegate) { |
| 23 | } |
| 24 | |
Sorin Jianu | 6e1c3b1c | 2024-12-19 22:11:20 | [diff] [blame] | 25 | CommandUpdaterImpl::~CommandUpdaterImpl() = default; |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame] | 26 | |
| 27 | bool CommandUpdaterImpl::SupportsCommand(int id) const { |
| 28 | return commands_.find(id) != commands_.end(); |
| 29 | } |
| 30 | |
| 31 | bool CommandUpdaterImpl::IsCommandEnabled(int id) const { |
| 32 | auto command = commands_.find(id); |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 33 | if (command == commands_.end() || command->second->enabled == std::nullopt) { |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame] | 34 | return false; |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 35 | } |
Jan Krcal | 8e7558a6 | 2021-07-21 12:24:21 | [diff] [blame] | 36 | return *command->second->enabled; |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame] | 37 | } |
| 38 | |
Edwin Joe | 6f6fc1e | 2019-02-27 20:00:37 | [diff] [blame] | 39 | bool CommandUpdaterImpl::ExecuteCommand(int id, base::TimeTicks time_stamp) { |
| 40 | return ExecuteCommandWithDisposition(id, WindowOpenDisposition::CURRENT_TAB, |
| 41 | time_stamp); |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | bool CommandUpdaterImpl::ExecuteCommandWithDisposition( |
| 45 | int id, |
Edwin Joe | 6f6fc1e | 2019-02-27 20:00:37 | [diff] [blame] | 46 | WindowOpenDisposition disposition, |
| 47 | base::TimeTicks time_stamp) { |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame] | 48 | if (SupportsCommand(id) && IsCommandEnabled(id)) { |
| 49 | delegate_->ExecuteCommandWithDisposition(id, disposition); |
| 50 | return true; |
| 51 | } |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | void CommandUpdaterImpl::AddCommandObserver(int id, CommandObserver* observer) { |
| 56 | GetCommand(id, true)->observers.AddObserver(observer); |
| 57 | } |
| 58 | |
| 59 | void CommandUpdaterImpl::RemoveCommandObserver( |
| 60 | int id, CommandObserver* observer) { |
| 61 | GetCommand(id, false)->observers.RemoveObserver(observer); |
| 62 | } |
| 63 | |
| 64 | void CommandUpdaterImpl::RemoveCommandObserver(CommandObserver* observer) { |
| 65 | for (const auto& command_pair : commands_) { |
| 66 | Command* command = command_pair.second.get(); |
| 67 | if (command) |
| 68 | command->observers.RemoveObserver(observer); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | bool CommandUpdaterImpl::UpdateCommandEnabled(int id, bool enabled) { |
| 73 | Command* command = GetCommand(id, true); |
Jan Krcal | 8e7558a6 | 2021-07-21 12:24:21 | [diff] [blame] | 74 | if (command->enabled.has_value() && *command->enabled == enabled) |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame] | 75 | return true; // Nothing to do. |
| 76 | command->enabled = enabled; |
| 77 | for (auto& observer : command->observers) |
| 78 | observer.EnabledStateChangedForCommand(id, enabled); |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | void CommandUpdaterImpl::DisableAllCommands() { |
| 83 | for (const auto& command_pair : commands_) |
| 84 | UpdateCommandEnabled(command_pair.first, false); |
| 85 | } |
| 86 | |
| 87 | std::vector<int> CommandUpdaterImpl::GetAllIds() { |
| 88 | std::vector<int> result; |
| 89 | for (const auto& command_pair : commands_) |
| 90 | result.push_back(command_pair.first); |
| 91 | return result; |
| 92 | } |
| 93 | |
| 94 | CommandUpdaterImpl::Command* |
| 95 | CommandUpdaterImpl::GetCommand(int id, bool create) { |
| 96 | bool supported = SupportsCommand(id); |
| 97 | if (supported) |
| 98 | return commands_[id].get(); |
| 99 | |
| 100 | DCHECK(create); |
| 101 | std::unique_ptr<Command>& entry = commands_[id]; |
Jeremy Roman | ec48d7a | 2018-03-01 17:35:09 | [diff] [blame] | 102 | entry = std::make_unique<Command>(); |
Ivan Sandrk | 9669d0e | 2017-12-15 23:50:20 | [diff] [blame] | 103 | return entry.get(); |
| 104 | } |