[M85 Dev][EFL] Fix errors to generate ninja files
[platform/framework/web/chromium-efl.git] / chrome / browser / command_updater_impl.cc
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 #include "chrome/browser/command_updater_impl.h"
6
7 #include <algorithm>
8
9 #include "base/check.h"
10 #include "base/observer_list.h"
11 #include "chrome/browser/command_observer.h"
12 #include "chrome/browser/command_updater_delegate.h"
13
14 class CommandUpdaterImpl::Command {
15  public:
16   bool enabled;
17   base::ObserverList<CommandObserver>::Unchecked observers;
18
19   Command() : enabled(true) {}
20 };
21
22 CommandUpdaterImpl::CommandUpdaterImpl(CommandUpdaterDelegate* delegate)
23     : delegate_(delegate) {
24 }
25
26 CommandUpdaterImpl::~CommandUpdaterImpl() {
27 }
28
29 bool CommandUpdaterImpl::SupportsCommand(int id) const {
30   return commands_.find(id) != commands_.end();
31 }
32
33 bool 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
40 bool CommandUpdaterImpl::ExecuteCommand(int id, base::TimeTicks time_stamp) {
41   return ExecuteCommandWithDisposition(id, WindowOpenDisposition::CURRENT_TAB,
42                                        time_stamp);
43 }
44
45 bool CommandUpdaterImpl::ExecuteCommandWithDisposition(
46     int id,
47     WindowOpenDisposition disposition,
48     base::TimeTicks time_stamp) {
49   if (SupportsCommand(id) && IsCommandEnabled(id)) {
50     delegate_->ExecuteCommandWithDisposition(id, disposition);
51     return true;
52   }
53   return false;
54 }
55
56 void CommandUpdaterImpl::AddCommandObserver(int id, CommandObserver* observer) {
57   GetCommand(id, true)->observers.AddObserver(observer);
58 }
59
60 void CommandUpdaterImpl::RemoveCommandObserver(
61     int id, CommandObserver* observer) {
62   GetCommand(id, false)->observers.RemoveObserver(observer);
63 }
64
65 void 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
73 bool 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
83 void CommandUpdaterImpl::DisableAllCommands() {
84   for (const auto& command_pair : commands_)
85     UpdateCommandEnabled(command_pair.first, false);
86 }
87
88 std::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
95 CommandUpdaterImpl::Command*
96 CommandUpdaterImpl::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];
103   entry = std::make_unique<Command>();
104   return entry.get();
105 }