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 "chrome/common/extensions/extension.h" | 14 #include "chrome/common/extensions/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 |
| 33 for (MediaKeysEventTargets::const_iterator iter = |
| 34 media_keys_event_targets_.begin(); |
| 35 iter != media_keys_event_targets_.end(); ++iter) { |
| 36 GlobalShortcutListener::GetInstance()->UnregisterAccelerator( |
| 37 iter->first, this); |
| 38 } |
32 } | 39 } |
33 | 40 |
34 static base::LazyInstance< | 41 static base::LazyInstance< |
35 ProfileKeyedAPIFactory<ExtensionCommandsGlobalRegistry> > | 42 ProfileKeyedAPIFactory<ExtensionCommandsGlobalRegistry> > |
36 g_factory = LAZY_INSTANCE_INITIALIZER; | 43 g_factory = LAZY_INSTANCE_INITIALIZER; |
37 | 44 |
38 // static | 45 // static |
39 ProfileKeyedAPIFactory<ExtensionCommandsGlobalRegistry>* | 46 ProfileKeyedAPIFactory<ExtensionCommandsGlobalRegistry>* |
40 ExtensionCommandsGlobalRegistry::GetFactoryInstance() { | 47 ExtensionCommandsGlobalRegistry::GetFactoryInstance() { |
41 return &g_factory.Get(); | 48 return &g_factory.Get(); |
(...skipping 27 matching lines...) Expand all Loading... |
69 | 76 |
70 extensions::CommandMap::const_iterator iter = commands.begin(); | 77 extensions::CommandMap::const_iterator iter = commands.begin(); |
71 for (; iter != commands.end(); ++iter) { | 78 for (; iter != commands.end(); ++iter) { |
72 if (!command_name.empty() && (iter->second.command_name() != command_name)) | 79 if (!command_name.empty() && (iter->second.command_name() != command_name)) |
73 continue; | 80 continue; |
74 | 81 |
75 VLOG(0) << "Adding global keybinding for " << extension->name().c_str() | 82 VLOG(0) << "Adding global keybinding for " << extension->name().c_str() |
76 << " " << command_name.c_str() | 83 << " " << command_name.c_str() |
77 << " key: " << iter->second.accelerator().GetShortcutText(); | 84 << " key: " << iter->second.accelerator().GetShortcutText(); |
78 | 85 |
79 event_targets_[iter->second.accelerator()] = | 86 if (extensions::CommandService::IsMediaKey(iter->second.accelerator())) { |
80 std::make_pair(extension->id(), iter->second.command_name()); | 87 std::pair<std::string, std::string> target = |
| 88 std::make_pair(extension->id(), iter->second.command_name()); |
| 89 media_keys_event_targets_[iter->second.accelerator()].push_back(target); |
| 90 } else { |
| 91 event_targets_[iter->second.accelerator()] = |
| 92 std::make_pair(extension->id(), iter->second.command_name()); |
| 93 } |
81 | 94 |
82 GlobalShortcutListener::GetInstance()->RegisterAccelerator( | 95 GlobalShortcutListener::GetInstance()->RegisterAccelerator( |
83 iter->second.accelerator(), this); | 96 iter->second.accelerator(), this); |
84 } | 97 } |
85 } | 98 } |
86 | 99 |
87 void ExtensionCommandsGlobalRegistry::RemoveExtensionKeybindingImpl( | 100 void ExtensionCommandsGlobalRegistry::RemoveExtensionKeybindingImpl( |
88 const ui::Accelerator& accelerator, | 101 const ui::Accelerator& accelerator, |
89 const std::string& command_name) { | 102 const std::string& command_name) { |
90 VLOG(0) << "Removing keybinding for " << command_name.c_str(); | 103 VLOG(0) << "Removing keybinding for " << command_name.c_str(); |
91 | 104 |
92 GlobalShortcutListener::GetInstance()->UnregisterAccelerator( | 105 GlobalShortcutListener::GetInstance()->UnregisterAccelerator( |
93 accelerator, this); | 106 accelerator, this); |
94 } | 107 } |
95 | 108 |
96 void ExtensionCommandsGlobalRegistry::OnKeyPressed( | 109 void ExtensionCommandsGlobalRegistry::OnKeyPressed( |
97 const ui::Accelerator& accelerator) { | 110 const ui::Accelerator& accelerator) { |
98 EventTargets::iterator it = event_targets_.find(accelerator); | 111 ExtensionKeybindingRegistry::NotifyEventTargetsByAccelerator(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 } | 112 } |
106 | 113 |
107 } // namespace extensions | 114 } // namespace extensions |
OLD | NEW |