Home | History | Annotate | Download | only in browser
      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.
      4 
      5 #include "chrome/browser/command_updater.h"
      6 
      7 #include <algorithm>
      8 
      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"
     14 
     15 class CommandUpdater::Command {
     16  public:
     17   bool enabled;
     18   ObserverList<CommandObserver> observers;
     19 
     20   Command() : enabled(true) {}
     21 };
     22 
     23 CommandUpdater::CommandUpdater(CommandUpdaterDelegate* delegate)
     24     : delegate_(delegate) {
     25 }
     26 
     27 CommandUpdater::~CommandUpdater() {
     28   STLDeleteContainerPairSecondPointers(commands_.begin(), commands_.end());
     29 }
     30 
     31 bool CommandUpdater::SupportsCommand(int id) const {
     32   return commands_.find(id) != commands_.end();
     33 }
     34 
     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;
     40 }
     41 
     42 bool CommandUpdater::ExecuteCommand(int id) {
     43   return ExecuteCommandWithDisposition(id, CURRENT_TAB);
     44 }
     45 
     46 bool CommandUpdater::ExecuteCommandWithDisposition(
     47     int id,
     48     WindowOpenDisposition disposition) {
     49   if (SupportsCommand(id) && IsCommandEnabled(id)) {
     50     delegate_->ExecuteCommandWithDisposition(id, disposition);
     51     return true;
     52   }
     53   return false;
     54 }
     55 
     56 void CommandUpdater::AddCommandObserver(int id, CommandObserver* observer) {
     57   GetCommand(id, true)->observers.AddObserver(observer);
     58 }
     59 
     60 void CommandUpdater::RemoveCommandObserver(int id, CommandObserver* observer) {
     61   GetCommand(id, false)->observers.RemoveObserver(observer);
     62 }
     63 
     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);
     71   }
     72 }
     73 
     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));
     81 }
     82 
     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;
     91 }
     92