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/memory/ptr_util.h" | |
10 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
11 #include "base/stl_util.h" | |
12 #include "chrome/browser/command_observer.h" | 12 #include "chrome/browser/command_observer.h" |
13 #include "chrome/browser/command_updater_delegate.h" | 13 #include "chrome/browser/command_updater_delegate.h" |
14 | 14 |
15 class CommandUpdater::Command { | 15 class CommandUpdater::Command { |
16 public: | 16 public: |
17 bool enabled; | 17 bool enabled; |
18 base::ObserverList<CommandObserver> observers; | 18 base::ObserverList<CommandObserver> observers; |
19 | 19 |
20 Command() : enabled(true) {} | 20 Command() : enabled(true) {} |
21 }; | 21 }; |
22 | 22 |
23 CommandUpdater::CommandUpdater(CommandUpdaterDelegate* delegate) | 23 CommandUpdater::CommandUpdater(CommandUpdaterDelegate* delegate) |
24 : delegate_(delegate) { | 24 : delegate_(delegate) { |
25 } | 25 } |
26 | 26 |
27 CommandUpdater::~CommandUpdater() { | 27 CommandUpdater::~CommandUpdater() { |
28 base::STLDeleteContainerPairSecondPointers(commands_.begin(), | |
29 commands_.end()); | |
30 } | 28 } |
31 | 29 |
32 bool CommandUpdater::SupportsCommand(int id) const { | 30 bool CommandUpdater::SupportsCommand(int id) const { |
33 return commands_.find(id) != commands_.end(); | 31 return commands_.find(id) != commands_.end(); |
34 } | 32 } |
35 | 33 |
36 bool CommandUpdater::IsCommandEnabled(int id) const { | 34 bool CommandUpdater::IsCommandEnabled(int id) const { |
37 const CommandMap::const_iterator command(commands_.find(id)); | 35 auto command = commands_.find(id); |
38 if (command == commands_.end()) | 36 if (command == commands_.end()) |
39 return false; | 37 return false; |
40 return command->second->enabled; | 38 return command->second->enabled; |
41 } | 39 } |
42 | 40 |
43 bool CommandUpdater::ExecuteCommand(int id) { | 41 bool CommandUpdater::ExecuteCommand(int id) { |
44 return ExecuteCommandWithDisposition(id, WindowOpenDisposition::CURRENT_TAB); | 42 return ExecuteCommandWithDisposition(id, WindowOpenDisposition::CURRENT_TAB); |
45 } | 43 } |
46 | 44 |
47 bool CommandUpdater::ExecuteCommandWithDisposition( | 45 bool CommandUpdater::ExecuteCommandWithDisposition( |
48 int id, | 46 int id, |
49 WindowOpenDisposition disposition) { | 47 WindowOpenDisposition disposition) { |
50 if (SupportsCommand(id) && IsCommandEnabled(id)) { | 48 if (SupportsCommand(id) && IsCommandEnabled(id)) { |
51 delegate_->ExecuteCommandWithDisposition(id, disposition); | 49 delegate_->ExecuteCommandWithDisposition(id, disposition); |
52 return true; | 50 return true; |
53 } | 51 } |
54 return false; | 52 return false; |
55 } | 53 } |
56 | 54 |
57 void CommandUpdater::AddCommandObserver(int id, CommandObserver* observer) { | 55 void CommandUpdater::AddCommandObserver(int id, CommandObserver* observer) { |
58 GetCommand(id, true)->observers.AddObserver(observer); | 56 GetCommand(id, true)->observers.AddObserver(observer); |
59 } | 57 } |
60 | 58 |
61 void CommandUpdater::RemoveCommandObserver(int id, CommandObserver* observer) { | 59 void CommandUpdater::RemoveCommandObserver(int id, CommandObserver* observer) { |
62 GetCommand(id, false)->observers.RemoveObserver(observer); | 60 GetCommand(id, false)->observers.RemoveObserver(observer); |
63 } | 61 } |
64 | 62 |
65 void CommandUpdater::RemoveCommandObserver(CommandObserver* observer) { | 63 void CommandUpdater::RemoveCommandObserver(CommandObserver* observer) { |
66 for (CommandMap::const_iterator it = commands_.begin(); | 64 for (auto it = commands_.begin(); it != commands_.end(); ++it) { |
Nico
2016/09/22 15:56:11
can't this be a for ( ... : commands_) style loop?
Avi (use Gerrit)
2016/09/22 19:17:17
Done.
| |
67 it != commands_.end(); | 65 Command* command = it->second.get(); |
68 ++it) { | |
69 Command* command = it->second; | |
70 if (command) | 66 if (command) |
71 command->observers.RemoveObserver(observer); | 67 command->observers.RemoveObserver(observer); |
72 } | 68 } |
73 } | 69 } |
74 | 70 |
75 void CommandUpdater::UpdateCommandEnabled(int id, bool enabled) { | 71 void CommandUpdater::UpdateCommandEnabled(int id, bool enabled) { |
76 Command* command = GetCommand(id, true); | 72 Command* command = GetCommand(id, true); |
77 if (command->enabled == enabled) | 73 if (command->enabled == enabled) |
78 return; // Nothing to do. | 74 return; // Nothing to do. |
79 command->enabled = enabled; | 75 command->enabled = enabled; |
80 FOR_EACH_OBSERVER(CommandObserver, command->observers, | 76 FOR_EACH_OBSERVER(CommandObserver, command->observers, |
81 EnabledStateChangedForCommand(id, enabled)); | 77 EnabledStateChangedForCommand(id, enabled)); |
82 } | 78 } |
83 | 79 |
84 CommandUpdater::Command* CommandUpdater::GetCommand(int id, bool create) { | 80 CommandUpdater::Command* CommandUpdater::GetCommand(int id, bool create) { |
85 bool supported = SupportsCommand(id); | 81 bool supported = SupportsCommand(id); |
86 if (supported) | 82 if (supported) |
87 return commands_[id]; | 83 return commands_[id].get(); |
88 DCHECK(create); | 84 DCHECK(create); |
89 Command* command = new Command; | 85 commands_[id] = base::MakeUnique<Command>(); |
90 commands_[id] = command; | 86 return commands_[id].get(); |
Nico
2016/09/22 15:56:11
also walks twice
Avi (use Gerrit)
2016/09/22 19:17:16
Done.
| |
91 return command; | |
92 } | 87 } |
OLD | NEW |