blob: f1014fcbdedba2dff1946372dca36cd5114e592e [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#include "chrome/browser/command_updater_impl.h"
6
7#include <algorithm>
8
Hans Wennborg1790e6b2020-04-24 19:10:339#include "base/check.h"
Ivan Sandrk9669d0e2017-12-15 23:50:2010#include "base/observer_list.h"
11#include "chrome/browser/command_observer.h"
12#include "chrome/browser/command_updater_delegate.h"
13
14class CommandUpdaterImpl::Command {
15 public:
16 bool enabled;
Trent Apteda250ec3ab2018-08-19 08:52:1917 base::ObserverList<CommandObserver>::Unchecked observers;
Ivan Sandrk9669d0e2017-12-15 23:50:2018
19 Command() : enabled(true) {}
20};
21
22CommandUpdaterImpl::CommandUpdaterImpl(CommandUpdaterDelegate* delegate)
23 : delegate_(delegate) {
24}
25
26CommandUpdaterImpl::~CommandUpdaterImpl() {
27}
28
29bool CommandUpdaterImpl::SupportsCommand(int id) const {
30 return commands_.find(id) != commands_.end();
31}
32
33bool CommandUpdaterImpl::IsCommandEnabled(int id) const {
34 auto command = commands_.find(id);
35 if (command == commands_.end())
36 return false;
37 return command->second->enabled;
38}
39
Edwin Joe6f6fc1e2019-02-27 20:00:3740bool CommandUpdaterImpl::ExecuteCommand(int id, base::TimeTicks time_stamp) {
41 return ExecuteCommandWithDisposition(id, WindowOpenDisposition::CURRENT_TAB,
42 time_stamp);
Ivan Sandrk9669d0e2017-12-15 23:50:2043}
44
45bool CommandUpdaterImpl::ExecuteCommandWithDisposition(
46 int id,
Edwin Joe6f6fc1e2019-02-27 20:00:3747 WindowOpenDisposition disposition,
48 base::TimeTicks time_stamp) {
Ivan Sandrk9669d0e2017-12-15 23:50:2049 if (SupportsCommand(id) && IsCommandEnabled(id)) {
50 delegate_->ExecuteCommandWithDisposition(id, disposition);
51 return true;
52 }
53 return false;
54}
55
56void CommandUpdaterImpl::AddCommandObserver(int id, CommandObserver* observer) {
57 GetCommand(id, true)->observers.AddObserver(observer);
58}
59
60void CommandUpdaterImpl::RemoveCommandObserver(
61 int id, CommandObserver* observer) {
62 GetCommand(id, false)->observers.RemoveObserver(observer);
63}
64
65void CommandUpdaterImpl::RemoveCommandObserver(CommandObserver* observer) {
66 for (const auto& command_pair : commands_) {
67 Command* command = command_pair.second.get();
68 if (command)
69 command->observers.RemoveObserver(observer);
70 }
71}
72
73bool CommandUpdaterImpl::UpdateCommandEnabled(int id, bool enabled) {
74 Command* command = GetCommand(id, true);
75 if (command->enabled == enabled)
76 return true; // Nothing to do.
77 command->enabled = enabled;
78 for (auto& observer : command->observers)
79 observer.EnabledStateChangedForCommand(id, enabled);
80 return true;
81}
82
83void CommandUpdaterImpl::DisableAllCommands() {
84 for (const auto& command_pair : commands_)
85 UpdateCommandEnabled(command_pair.first, false);
86}
87
88std::vector<int> CommandUpdaterImpl::GetAllIds() {
89 std::vector<int> result;
90 for (const auto& command_pair : commands_)
91 result.push_back(command_pair.first);
92 return result;
93}
94
95CommandUpdaterImpl::Command*
96CommandUpdaterImpl::GetCommand(int id, bool create) {
97 bool supported = SupportsCommand(id);
98 if (supported)
99 return commands_[id].get();
100
101 DCHECK(create);
102 std::unique_ptr<Command>& entry = commands_[id];
Jeremy Romanec48d7a2018-03-01 17:35:09103 entry = std::make_unique<Command>();
Ivan Sandrk9669d0e2017-12-15 23:50:20104 return entry.get();
105}