Move AppMenuTest from ChromeShellTest to ChromePublicTest
[chromium-blink-merge.git] / chrome / browser / command_updater.cc
blobeae13ce041470b7e127515c9ba0f2bfa2efe937e
1 // Copyright (c) 2012 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.
5 #include "chrome/browser/command_updater.h"
7 #include <algorithm>
9 #include "base/logging.h"
10 #include "base/observer_list.h"
11 #include "base/stl_util.h"
12 #include "chrome/browser/command_observer.h"
13 #include "chrome/browser/command_updater_delegate.h"
15 class CommandUpdater::Command {
16 public:
17 bool enabled;
18 base::ObserverList<CommandObserver> observers;
20 Command() : enabled(true) {}
23 CommandUpdater::CommandUpdater(CommandUpdaterDelegate* delegate)
24 : delegate_(delegate) {
27 CommandUpdater::~CommandUpdater() {
28 STLDeleteContainerPairSecondPointers(commands_.begin(), commands_.end());
31 bool CommandUpdater::SupportsCommand(int id) const {
32 return commands_.find(id) != commands_.end();
35 bool CommandUpdater::IsCommandEnabled(int id) const {
36 const CommandMap::const_iterator command(commands_.find(id));
37 if (command == commands_.end())
38 return false;
39 return command->second->enabled;
42 bool CommandUpdater::ExecuteCommand(int id) {
43 return ExecuteCommandWithDisposition(id, CURRENT_TAB);
46 bool CommandUpdater::ExecuteCommandWithDisposition(
47 int id,
48 WindowOpenDisposition disposition) {
49 if (SupportsCommand(id) && IsCommandEnabled(id)) {
50 delegate_->ExecuteCommandWithDisposition(id, disposition);
51 return true;
53 return false;
56 void CommandUpdater::AddCommandObserver(int id, CommandObserver* observer) {
57 GetCommand(id, true)->observers.AddObserver(observer);
60 void CommandUpdater::RemoveCommandObserver(int id, CommandObserver* observer) {
61 GetCommand(id, false)->observers.RemoveObserver(observer);
64 void CommandUpdater::RemoveCommandObserver(CommandObserver* observer) {
65 for (CommandMap::const_iterator it = commands_.begin();
66 it != commands_.end();
67 ++it) {
68 Command* command = it->second;
69 if (command)
70 command->observers.RemoveObserver(observer);
74 void CommandUpdater::UpdateCommandEnabled(int id, bool enabled) {
75 Command* command = GetCommand(id, true);
76 if (command->enabled == enabled)
77 return; // Nothing to do.
78 command->enabled = enabled;
79 FOR_EACH_OBSERVER(CommandObserver, command->observers,
80 EnabledStateChangedForCommand(id, enabled));
83 CommandUpdater::Command* CommandUpdater::GetCommand(int id, bool create) {
84 bool supported = SupportsCommand(id);
85 if (supported)
86 return commands_[id];
87 DCHECK(create);
88 Command* command = new Command;
89 commands_[id] = command;
90 return command;