summaryrefslogtreecommitdiffstats
path: root/chrome/browser/command_updater.cc
diff options
context:
space:
mode:
authorben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-20 23:03:14 +0000
committerben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-20 23:03:14 +0000
commit1fc025209fbaeb2eb91cfeac8dbced8bc493f3b2 (patch)
tree13faf73a68138e0cd30e7b628a677ebb0422aa8c /chrome/browser/command_updater.cc
parent91f57782b0dd7a847b8d31020003f00099366545 (diff)
downloadchromium_src-1fc025209fbaeb2eb91cfeac8dbced8bc493f3b2.zip
chromium_src-1fc025209fbaeb2eb91cfeac8dbced8bc493f3b2.tar.gz
chromium_src-1fc025209fbaeb2eb91cfeac8dbced8bc493f3b2.tar.bz2
Rework the command updater to not be dependent on views::Button (needed for porting).
TEST=make sure back/forward buttons still enable/disable correctly depending on the length of the back/forward navigation list. Review URL: http://codereview.chromium.org/18343 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8332 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/command_updater.cc')
-rw-r--r--chrome/browser/command_updater.cc61
1 files changed, 61 insertions, 0 deletions
diff --git a/chrome/browser/command_updater.cc b/chrome/browser/command_updater.cc
new file mode 100644
index 0000000..8839965
--- /dev/null
+++ b/chrome/browser/command_updater.cc
@@ -0,0 +1,61 @@
+// 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/command_updater.h"
+
+#include "base/logging.h"
+#include "chrome/common/stl_util-inl.h"
+
+CommandUpdater::CommandUpdater(CommandUpdaterDelegate* handler)
+ : delegate_(handler) {
+}
+
+CommandUpdater::~CommandUpdater() {
+ STLDeleteContainerPairSecondPointers(commands_.begin(), commands_.end());
+}
+
+bool CommandUpdater::IsCommandEnabled(int id) const {
+ const CommandMap::const_iterator command(commands_.find(id));
+ if (command == commands_.end())
+ return false;
+ return command->second->enabled;
+}
+
+bool CommandUpdater::SupportsCommand(int id) const {
+ return commands_.find(id) != commands_.end();
+}
+
+void CommandUpdater::ExecuteCommand(int id) {
+ if (IsCommandEnabled(id))
+ delegate_->ExecuteCommand(id);
+}
+
+void CommandUpdater::UpdateCommandEnabled(int id, bool enabled) {
+ Command* command = GetCommand(id, true);
+ if (command->enabled == enabled)
+ return; // Nothing to do.
+ command->enabled = enabled;
+ FOR_EACH_OBSERVER(CommandObserver, command->observers,
+ EnabledStateChangedForCommand(id, enabled));
+}
+
+CommandUpdater::Command* CommandUpdater::GetCommand(int id, bool create) {
+ bool supported = SupportsCommand(id);
+ if (supported)
+ return commands_[id];
+ DCHECK(create);
+ Command* command = new Command;
+ commands_[id] = command;
+ return command;
+}
+
+void CommandUpdater::AddCommandObserver(int id, CommandObserver* observer) {
+ GetCommand(id, true)->observers.AddObserver(observer);
+}
+
+void CommandUpdater::RemoveCommandObserver(int id, CommandObserver* observer) {
+ GetCommand(id, false)->observers.RemoveObserver(observer);
+}