OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/extensions/extension_commands_global_registry.h" | 5 #include "chrome/browser/extensions/extension_commands_global_registry.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
9 #include "chrome/browser/extensions/api/commands/command_service.h" | 9 #include "chrome/browser/extensions/api/commands/command_service.h" |
10 #include "chrome/browser/extensions/extension_keybinding_registry.h" | 10 #include "chrome/browser/extensions/extension_keybinding_registry.h" |
11 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
12 #include "chrome/browser/extensions/global_shortcut_listener.h" | 12 #include "chrome/browser/extensions/global_shortcut_listener.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "extensions/common/extension.h" | 14 #include "extensions/common/extension.h" |
15 | 15 |
16 namespace extensions { | 16 namespace extensions { |
17 | 17 |
18 ExtensionCommandsGlobalRegistry::ExtensionCommandsGlobalRegistry( | 18 ExtensionCommandsGlobalRegistry::ExtensionCommandsGlobalRegistry( |
19 Profile* profile) | 19 Profile* profile) |
20 : ExtensionKeybindingRegistry( | 20 : ExtensionKeybindingRegistry( |
21 profile, ExtensionKeybindingRegistry::ALL_EXTENSIONS, NULL), | 21 profile, ExtensionKeybindingRegistry::ALL_EXTENSIONS, NULL), |
22 profile_(profile) { | 22 profile_(profile) { |
23 Init(); | 23 Init(); |
24 } | 24 } |
25 | 25 |
26 ExtensionCommandsGlobalRegistry::~ExtensionCommandsGlobalRegistry() { | 26 ExtensionCommandsGlobalRegistry::~ExtensionCommandsGlobalRegistry() { |
27 EventTargets::const_iterator iter; | 27 for (EventTargets::const_iterator iter = event_targets_.begin(); |
28 for (iter = event_targets_.begin(); iter != event_targets_.end(); ++iter) { | 28 iter != event_targets_.end(); ++iter) { |
29 GlobalShortcutListener::GetInstance()->UnregisterAccelerator( | 29 GlobalShortcutListener::GetInstance()->UnregisterAccelerator( |
30 iter->first, this); | 30 iter->first, this); |
31 } | 31 } |
32 } | 32 } |
33 | 33 |
34 static base::LazyInstance< | 34 static base::LazyInstance< |
35 ProfileKeyedAPIFactory<ExtensionCommandsGlobalRegistry> > | 35 ProfileKeyedAPIFactory<ExtensionCommandsGlobalRegistry> > |
36 g_factory = LAZY_INSTANCE_INITIALIZER; | 36 g_factory = LAZY_INSTANCE_INITIALIZER; |
37 | 37 |
38 // static | 38 // static |
(...skipping 30 matching lines...) Expand all Loading... |
69 | 69 |
70 extensions::CommandMap::const_iterator iter = commands.begin(); | 70 extensions::CommandMap::const_iterator iter = commands.begin(); |
71 for (; iter != commands.end(); ++iter) { | 71 for (; iter != commands.end(); ++iter) { |
72 if (!command_name.empty() && (iter->second.command_name() != command_name)) | 72 if (!command_name.empty() && (iter->second.command_name() != command_name)) |
73 continue; | 73 continue; |
74 | 74 |
75 VLOG(0) << "Adding global keybinding for " << extension->name().c_str() | 75 VLOG(0) << "Adding global keybinding for " << extension->name().c_str() |
76 << " " << command_name.c_str() | 76 << " " << command_name.c_str() |
77 << " key: " << iter->second.accelerator().GetShortcutText(); | 77 << " key: " << iter->second.accelerator().GetShortcutText(); |
78 | 78 |
79 event_targets_[iter->second.accelerator()] = | 79 event_targets_[iter->second.accelerator()].push_back( |
80 std::make_pair(extension->id(), iter->second.command_name()); | 80 std::make_pair(extension->id(), iter->second.command_name())); |
| 81 // Shortcuts except media keys have only one target in the list. See comment |
| 82 // about |event_targets_|. |
| 83 if (!extensions::CommandService::IsMediaKey(iter->second.accelerator())) |
| 84 DCHECK(event_targets_[iter->second.accelerator()].size() == 1); |
81 | 85 |
82 GlobalShortcutListener::GetInstance()->RegisterAccelerator( | 86 GlobalShortcutListener::GetInstance()->RegisterAccelerator( |
83 iter->second.accelerator(), this); | 87 iter->second.accelerator(), this); |
84 } | 88 } |
85 } | 89 } |
86 | 90 |
87 void ExtensionCommandsGlobalRegistry::RemoveExtensionKeybindingImpl( | 91 void ExtensionCommandsGlobalRegistry::RemoveExtensionKeybindingImpl( |
88 const ui::Accelerator& accelerator, | 92 const ui::Accelerator& accelerator, |
89 const std::string& command_name) { | 93 const std::string& command_name) { |
90 VLOG(0) << "Removing keybinding for " << command_name.c_str(); | 94 VLOG(0) << "Removing keybinding for " << command_name.c_str(); |
91 | 95 |
92 GlobalShortcutListener::GetInstance()->UnregisterAccelerator( | 96 GlobalShortcutListener::GetInstance()->UnregisterAccelerator( |
93 accelerator, this); | 97 accelerator, this); |
94 } | 98 } |
95 | 99 |
96 void ExtensionCommandsGlobalRegistry::OnKeyPressed( | 100 void ExtensionCommandsGlobalRegistry::OnKeyPressed( |
97 const ui::Accelerator& accelerator) { | 101 const ui::Accelerator& accelerator) { |
98 EventTargets::iterator it = event_targets_.find(accelerator); | 102 ExtensionKeybindingRegistry::NotifyEventTargets(accelerator); |
99 if (it == event_targets_.end()) { | |
100 NOTREACHED(); // Shouldn't get this event for something not registered. | |
101 return; | |
102 } | |
103 | |
104 CommandExecuted(it->second.first, it->second.second); | |
105 } | 103 } |
106 | 104 |
107 } // namespace extensions | 105 } // namespace extensions |
OLD | NEW |