1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <algorithm>
#include "chrome/browser/controller.h"
#include "base/logging.h"
CommandController::CommandController(CommandHandler* handler) :
handler_(handler) {
}
CommandController::~CommandController() {
// The button controllers are command observers hence this list
// must be deleted before the commands below.
std::vector<ButtonController*>::iterator bc_iter;
for (bc_iter = managed_button_controllers_.begin();
bc_iter != managed_button_controllers_.end();
++bc_iter)
delete *bc_iter;
CommandMap::iterator command;
for (command = commands_.begin(); command != commands_.end(); ++command) {
DCHECK(command->second);
delete command->second->observers;
delete command->second;
}
}
bool CommandController::IsCommandEnabled(int id) const {
const CommandMap::const_iterator command(commands_.find(id));
if (command == commands_.end())
return false;
DCHECK(command->second);
return command->second->enabled;
}
bool CommandController::SupportsCommand(int id) const {
return commands_.find(id) != commands_.end();
}
bool CommandController::GetContextualLabel(int id, std::wstring* out) const {
return handler_->GetContextualLabel(id, out);
}
void CommandController::ExecuteCommand(int id) {
if (IsCommandEnabled(id))
handler_->ExecuteCommand(id);
}
void CommandController::UpdateCommandEnabled(int id, bool enabled) {
Command* command = GetCommand(id, true);
if (command->enabled == enabled)
return; // Nothing to do.
command->enabled = enabled;
CommandObserverList* list = command->observers;
// FIXME: If Update calls RemoveCommandObserver, the iterator will be
// invalidated. Darin says he's working on a new ObserverList
// class that will handle this properly. For right now, don't
// do that!
CommandObserverList::const_iterator end = list->end();
CommandObserverList::iterator observer = list->begin();
while (observer != end)
(*observer++)->SetEnabled(enabled);
}
Command* CommandController::GetCommand(int id, bool create) {
Command* command = NULL;
bool supported = SupportsCommand(id);
if (supported) {
command = commands_[id];
} else if (create) {
command = new Command;
DCHECK(command) << "Controller::GetCommand - OOM!";
command->observers = new CommandObserverList;
DCHECK(command->observers) << "Controller::GetCommand - OOM!";
command->enabled = false;
commands_[id] = command;
}
return command;
}
void CommandController::AddCommandObserver(int id, CommandObserver* observer) {
Command* command = GetCommand(id, true);
CommandObserverList* list = command->observers;
CommandObserverList::const_iterator end = list->end();
CommandObserverList::iterator existing =
find(list->begin(), list->end(), observer);
if (existing != end)
return;
list->push_back(observer);
}
void CommandController::RemoveCommandObserver(int id,
CommandObserver* observer) {
Command* command = GetCommand(id, false);
if (!command)
return;
CommandObserverList* list = command->observers;
CommandObserverList::const_iterator end = list->end();
CommandObserverList::iterator existing =
find(list->begin(), list->end(), observer);
if (existing != end)
list->erase(existing);
}
void CommandController::AddManagedButton(ChromeViews::Button* b, int command) {
ButtonController* bc = new ButtonController(b, this, command);
managed_button_controllers_.push_back(bc);
}
|