blob: 73c5a5f5ffb6e2f9ca134144d92c105e942238f0 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2017 The Chromium Authors
Ivan Sandrk9669d0e2017-12-15 23:50:202// 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 Sonzognife132ee2024-01-15 11:01:048#include <optional>
Ivan Sandrk9669d0e2017-12-15 23:50:209
Hans Wennborg1790e6b2020-04-24 19:10:3310#include "base/check.h"
Ivan Sandrk9669d0e2017-12-15 23:50:2011#include "base/observer_list.h"
12#include "chrome/browser/command_observer.h"
13#include "chrome/browser/command_updater_delegate.h"
14
Jan Krcal8e7558a62021-07-21 12:24:2115struct CommandUpdaterImpl::Command {
16 // Empty optional means not specified yet and thus implicitly disabled.
Arthur Sonzognife132ee2024-01-15 11:01:0417 std::optional<bool> enabled;
miktbacc1fc2024-03-07 16:38:2018 base::ObserverList<CommandObserver>::UncheckedAndDanglingUntriaged observers;
Ivan Sandrk9669d0e2017-12-15 23:50:2019};
20
21CommandUpdaterImpl::CommandUpdaterImpl(CommandUpdaterDelegate* delegate)
22 : delegate_(delegate) {
23}
24
Sorin Jianu6e1c3b1c2024-12-19 22:11:2025CommandUpdaterImpl::~CommandUpdaterImpl() = default;
Ivan Sandrk9669d0e2017-12-15 23:50:2026
27bool CommandUpdaterImpl::SupportsCommand(int id) const {
28 return commands_.find(id) != commands_.end();
29}
30
31bool CommandUpdaterImpl::IsCommandEnabled(int id) const {
32 auto command = commands_.find(id);
Arthur Sonzognife132ee2024-01-15 11:01:0433 if (command == commands_.end() || command->second->enabled == std::nullopt) {
Ivan Sandrk9669d0e2017-12-15 23:50:2034 return false;
Arthur Sonzognife132ee2024-01-15 11:01:0435 }
Jan Krcal8e7558a62021-07-21 12:24:2136 return *command->second->enabled;
Ivan Sandrk9669d0e2017-12-15 23:50:2037}
38
Edwin Joe6f6fc1e2019-02-27 20:00:3739bool CommandUpdaterImpl::ExecuteCommand(int id, base::TimeTicks time_stamp) {
40 return ExecuteCommandWithDisposition(id, WindowOpenDisposition::CURRENT_TAB,
41 time_stamp);
Ivan Sandrk9669d0e2017-12-15 23:50:2042}
43
44bool CommandUpdaterImpl::ExecuteCommandWithDisposition(
45 int id,
Edwin Joe6f6fc1e2019-02-27 20:00:3746 WindowOpenDisposition disposition,
47 base::TimeTicks time_stamp) {
Ivan Sandrk9669d0e2017-12-15 23:50:2048 if (SupportsCommand(id) && IsCommandEnabled(id)) {
49 delegate_->ExecuteCommandWithDisposition(id, disposition);
50 return true;
51 }
52 return false;
53}
54
55void CommandUpdaterImpl::AddCommandObserver(int id, CommandObserver* observer) {
56 GetCommand(id, true)->observers.AddObserver(observer);
57}
58
59void CommandUpdaterImpl::RemoveCommandObserver(
60 int id, CommandObserver* observer) {
61 GetCommand(id, false)->observers.RemoveObserver(observer);
62}
63
64void 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
72bool CommandUpdaterImpl::UpdateCommandEnabled(int id, bool enabled) {
73 Command* command = GetCommand(id, true);
Jan Krcal8e7558a62021-07-21 12:24:2174 if (command->enabled.has_value() && *command->enabled == enabled)
Ivan Sandrk9669d0e2017-12-15 23:50:2075 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
82void CommandUpdaterImpl::DisableAllCommands() {
83 for (const auto& command_pair : commands_)
84 UpdateCommandEnabled(command_pair.first, false);
85}
86
87std::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
94CommandUpdaterImpl::Command*
95CommandUpdaterImpl::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 Romanec48d7a2018-03-01 17:35:09102 entry = std::make_unique<Command>();
Ivan Sandrk9669d0e2017-12-15 23:50:20103 return entry.get();
104}