OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/command_updater.h" | 5 #include "chrome/browser/command_updater.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "chrome/browser/command_observer.h" | 12 #include "chrome/browser/command_observer.h" |
13 | 13 #include "chrome/browser/command_updater_delegate.h" |
14 CommandUpdater::CommandUpdaterDelegate::~CommandUpdaterDelegate() { | |
15 } | |
16 | 14 |
17 class CommandUpdater::Command { | 15 class CommandUpdater::Command { |
18 public: | 16 public: |
19 bool enabled; | 17 bool enabled; |
20 ObserverList<CommandObserver> observers; | 18 ObserverList<CommandObserver> observers; |
21 | 19 |
22 Command() : enabled(true) {} | 20 Command() : enabled(true) {} |
23 }; | 21 }; |
24 | 22 |
25 CommandUpdater::CommandUpdater(CommandUpdaterDelegate* handler) | 23 CommandUpdater::CommandUpdater(CommandUpdaterDelegate* delegate) |
26 : delegate_(handler) { | 24 : delegate_(delegate) { |
27 } | 25 } |
28 | 26 |
29 CommandUpdater::~CommandUpdater() { | 27 CommandUpdater::~CommandUpdater() { |
30 STLDeleteContainerPairSecondPointers(commands_.begin(), commands_.end()); | 28 STLDeleteContainerPairSecondPointers(commands_.begin(), commands_.end()); |
31 } | 29 } |
32 | 30 |
| 31 bool CommandUpdater::SupportsCommand(int id) const { |
| 32 return commands_.find(id) != commands_.end(); |
| 33 } |
| 34 |
33 bool CommandUpdater::IsCommandEnabled(int id) const { | 35 bool CommandUpdater::IsCommandEnabled(int id) const { |
34 const CommandMap::const_iterator command(commands_.find(id)); | 36 const CommandMap::const_iterator command(commands_.find(id)); |
35 if (command == commands_.end()) | 37 if (command == commands_.end()) |
36 return false; | 38 return false; |
37 return command->second->enabled; | 39 return command->second->enabled; |
38 } | 40 } |
39 | 41 |
40 bool CommandUpdater::SupportsCommand(int id) const { | |
41 return commands_.find(id) != commands_.end(); | |
42 } | |
43 | |
44 bool CommandUpdater::ExecuteCommand(int id) { | 42 bool CommandUpdater::ExecuteCommand(int id) { |
45 return ExecuteCommandWithDisposition(id, CURRENT_TAB); | 43 return ExecuteCommandWithDisposition(id, CURRENT_TAB); |
46 } | 44 } |
47 | 45 |
48 bool CommandUpdater::ExecuteCommandWithDisposition( | 46 bool CommandUpdater::ExecuteCommandWithDisposition( |
49 int id, | 47 int id, |
50 WindowOpenDisposition disposition) { | 48 WindowOpenDisposition disposition) { |
51 if (SupportsCommand(id) && IsCommandEnabled(id)) { | 49 if (SupportsCommand(id) && IsCommandEnabled(id)) { |
52 delegate_->ExecuteCommandWithDisposition(id, disposition); | 50 delegate_->ExecuteCommandWithDisposition(id, disposition); |
53 return true; | 51 return true; |
54 } | 52 } |
55 return false; | 53 return false; |
56 } | 54 } |
57 | 55 |
58 CommandObserver::~CommandObserver() { | 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 } |
59 } | 72 } |
60 | 73 |
61 void CommandUpdater::UpdateCommandEnabled(int id, bool enabled) { | 74 void CommandUpdater::UpdateCommandEnabled(int id, bool enabled) { |
62 Command* command = GetCommand(id, true); | 75 Command* command = GetCommand(id, true); |
63 if (command->enabled == enabled) | 76 if (command->enabled == enabled) |
64 return; // Nothing to do. | 77 return; // Nothing to do. |
65 command->enabled = enabled; | 78 command->enabled = enabled; |
66 FOR_EACH_OBSERVER(CommandObserver, command->observers, | 79 FOR_EACH_OBSERVER(CommandObserver, command->observers, |
67 EnabledStateChangedForCommand(id, enabled)); | 80 EnabledStateChangedForCommand(id, enabled)); |
68 } | 81 } |
69 | 82 |
70 CommandUpdater::Command* CommandUpdater::GetCommand(int id, bool create) { | 83 CommandUpdater::Command* CommandUpdater::GetCommand(int id, bool create) { |
71 bool supported = SupportsCommand(id); | 84 bool supported = SupportsCommand(id); |
72 if (supported) | 85 if (supported) |
73 return commands_[id]; | 86 return commands_[id]; |
74 DCHECK(create); | 87 DCHECK(create); |
75 Command* command = new Command; | 88 Command* command = new Command; |
76 commands_[id] = command; | 89 commands_[id] = command; |
77 return command; | 90 return command; |
78 } | 91 } |
79 | |
80 void CommandUpdater::AddCommandObserver(int id, CommandObserver* observer) { | |
81 GetCommand(id, true)->observers.AddObserver(observer); | |
82 } | |
83 | |
84 void CommandUpdater::RemoveCommandObserver(int id, CommandObserver* observer) { | |
85 GetCommand(id, false)->observers.RemoveObserver(observer); | |
86 } | |
87 | |
88 void CommandUpdater::RemoveCommandObserver(CommandObserver* observer) { | |
89 for (CommandMap::const_iterator it = commands_.begin(); | |
90 it != commands_.end(); | |
91 ++it) { | |
92 Command* command = it->second; | |
93 if (command) | |
94 command->observers.RemoveObserver(observer); | |
95 } | |
96 } | |
OLD | NEW |