1 // Copyright (c) 2006-2008 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.
7 #include "chrome/browser/controller.h"
8 #include "base/logging.h"
10 CommandController::CommandController(CommandHandler
* handler
) :
14 CommandController::~CommandController() {
15 // The button controllers are command observers hence this list
16 // must be deleted before the commands below.
17 std::vector
<ButtonController
*>::iterator bc_iter
;
18 for (bc_iter
= managed_button_controllers_
.begin();
19 bc_iter
!= managed_button_controllers_
.end();
23 CommandMap::iterator command
;
24 for (command
= commands_
.begin(); command
!= commands_
.end(); ++command
) {
25 DCHECK(command
->second
);
26 delete command
->second
->observers
;
27 delete command
->second
;
31 bool CommandController::IsCommandEnabled(int id
) const {
32 const CommandMap::const_iterator
command(commands_
.find(id
));
33 if (command
== commands_
.end())
36 DCHECK(command
->second
);
37 return command
->second
->enabled
;
40 bool CommandController::SupportsCommand(int id
) const {
41 return commands_
.find(id
) != commands_
.end();
44 bool CommandController::GetContextualLabel(int id
, std::wstring
* out
) const {
45 return handler_
->GetContextualLabel(id
, out
);
48 void CommandController::ExecuteCommand(int id
) {
49 if (IsCommandEnabled(id
))
50 handler_
->ExecuteCommand(id
);
53 void CommandController::UpdateCommandEnabled(int id
, bool enabled
) {
54 Command
* command
= GetCommand(id
, true);
55 if (command
->enabled
== enabled
)
56 return; // Nothing to do.
57 command
->enabled
= enabled
;
58 CommandObserverList
* list
= command
->observers
;
60 // FIXME: If Update calls RemoveCommandObserver, the iterator will be
61 // invalidated. Darin says he's working on a new ObserverList
62 // class that will handle this properly. For right now, don't
64 CommandObserverList::const_iterator end
= list
->end();
65 CommandObserverList::iterator observer
= list
->begin();
66 while (observer
!= end
)
67 (*observer
++)->SetEnabled(enabled
);
70 Command
* CommandController::GetCommand(int id
, bool create
) {
71 Command
* command
= NULL
;
72 bool supported
= SupportsCommand(id
);
74 command
= commands_
[id
];
76 command
= new Command
;
77 DCHECK(command
) << "Controller::GetCommand - OOM!";
78 command
->observers
= new CommandObserverList
;
79 DCHECK(command
->observers
) << "Controller::GetCommand - OOM!";
80 command
->enabled
= false;
81 commands_
[id
] = command
;
86 void CommandController::AddCommandObserver(int id
, CommandObserver
* observer
) {
87 Command
* command
= GetCommand(id
, true);
88 CommandObserverList
* list
= command
->observers
;
90 CommandObserverList::const_iterator end
= list
->end();
91 CommandObserverList::iterator existing
=
92 find(list
->begin(), list
->end(), observer
);
96 list
->push_back(observer
);
99 void CommandController::RemoveCommandObserver(int id
,
100 CommandObserver
* observer
) {
101 Command
* command
= GetCommand(id
, false);
105 CommandObserverList
* list
= command
->observers
;
106 CommandObserverList::const_iterator end
= list
->end();
107 CommandObserverList::iterator existing
=
108 find(list
->begin(), list
->end(), observer
);
111 list
->erase(existing
);
114 void CommandController::AddManagedButton(ChromeViews::Button
* b
, int command
) {
115 ButtonController
* bc
= new ButtonController(b
, this, command
);
116 managed_button_controllers_
.push_back(bc
);