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" | |
9 #include "chrome/browser/extensions/api/commands/command_service.h" | 8 #include "chrome/browser/extensions/api/commands/command_service.h" |
10 #include "chrome/browser/extensions/extension_keybinding_registry.h" | |
11 #include "chrome/browser/extensions/extension_service.h" | |
12 #include "chrome/browser/extensions/global_shortcut_listener.h" | 9 #include "chrome/browser/extensions/global_shortcut_listener.h" |
13 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
14 #include "extensions/common/extension.h" | 11 #include "extensions/common/extension.h" |
15 | 12 |
16 namespace extensions { | 13 namespace extensions { |
17 | 14 |
18 ExtensionCommandsGlobalRegistry::ExtensionCommandsGlobalRegistry( | 15 ExtensionCommandsGlobalRegistry::ExtensionCommandsGlobalRegistry( |
19 Profile* profile) | 16 Profile* profile) |
20 : ExtensionKeybindingRegistry( | 17 : ExtensionKeybindingRegistry( |
21 profile, ExtensionKeybindingRegistry::ALL_EXTENSIONS, NULL), | 18 profile, ExtensionKeybindingRegistry::ALL_EXTENSIONS, NULL), |
(...skipping 19 matching lines...) Expand all Loading... |
41 return &g_factory.Get(); | 38 return &g_factory.Get(); |
42 } | 39 } |
43 | 40 |
44 // static | 41 // static |
45 ExtensionCommandsGlobalRegistry* | 42 ExtensionCommandsGlobalRegistry* |
46 ExtensionCommandsGlobalRegistry::Get(Profile* profile) { | 43 ExtensionCommandsGlobalRegistry::Get(Profile* profile) { |
47 return ProfileKeyedAPIFactory< | 44 return ProfileKeyedAPIFactory< |
48 ExtensionCommandsGlobalRegistry>::GetForProfile(profile); | 45 ExtensionCommandsGlobalRegistry>::GetForProfile(profile); |
49 } | 46 } |
50 | 47 |
51 | |
52 void ExtensionCommandsGlobalRegistry::AddExtensionKeybinding( | 48 void ExtensionCommandsGlobalRegistry::AddExtensionKeybinding( |
53 const extensions::Extension* extension, | 49 const extensions::Extension* extension, |
54 const std::string& command_name) { | 50 const std::string& command_name) { |
55 // This object only handles named commands, not browser/page actions. | 51 // This object only handles named commands, not browser/page actions. |
56 if (ShouldIgnoreCommand(command_name)) | 52 if (ShouldIgnoreCommand(command_name)) |
57 return; | 53 return; |
58 | 54 |
59 extensions::CommandService* command_service = | 55 extensions::CommandService* command_service = |
60 extensions::CommandService::Get(profile_); | 56 extensions::CommandService::Get(profile_); |
61 // Add all the active global keybindings, if any. | 57 // Add all the active global keybindings, if any. |
62 extensions::CommandMap commands; | 58 extensions::CommandMap commands; |
63 if (!command_service->GetNamedCommands( | 59 if (!command_service->GetNamedCommands( |
64 extension->id(), | 60 extension->id(), |
65 extensions::CommandService::ACTIVE_ONLY, | 61 extensions::CommandService::ACTIVE_ONLY, |
66 extensions::CommandService::GLOBAL, | 62 extensions::CommandService::GLOBAL, |
67 &commands)) | 63 &commands)) |
68 return; | 64 return; |
69 | 65 |
70 extensions::CommandMap::const_iterator iter = commands.begin(); | 66 extensions::CommandMap::const_iterator iter = commands.begin(); |
71 for (; iter != commands.end(); ++iter) { | 67 for (; iter != commands.end(); ++iter) { |
72 if (!command_name.empty() && (iter->second.command_name() != command_name)) | 68 if (!command_name.empty() && (iter->second.command_name() != command_name)) |
73 continue; | 69 continue; |
| 70 const ui::Accelerator& accelerator = iter->second.accelerator(); |
74 | 71 |
75 VLOG(0) << "Adding global keybinding for " << extension->name().c_str() | 72 VLOG(0) << "Adding global keybinding for " << extension->name().c_str() |
76 << " " << command_name.c_str() | 73 << " " << command_name.c_str() |
77 << " key: " << iter->second.accelerator().GetShortcutText(); | 74 << " key: " << accelerator.GetShortcutText(); |
78 | 75 |
79 event_targets_[iter->second.accelerator()].push_back( | 76 if (event_targets_.find(accelerator) == event_targets_.end()) { |
| 77 if (!GlobalShortcutListener::GetInstance()->RegisterAccelerator( |
| 78 accelerator, this)) |
| 79 continue; |
| 80 } |
| 81 |
| 82 event_targets_[accelerator].push_back( |
80 std::make_pair(extension->id(), iter->second.command_name())); | 83 std::make_pair(extension->id(), iter->second.command_name())); |
81 // Shortcuts except media keys have only one target in the list. See comment | 84 // Shortcuts except media keys have only one target in the list. See comment |
82 // about |event_targets_|. | 85 // about |event_targets_|. |
83 if (!extensions::CommandService::IsMediaKey(iter->second.accelerator())) | 86 if (!extensions::CommandService::IsMediaKey(accelerator)) |
84 DCHECK(event_targets_[iter->second.accelerator()].size() == 1); | 87 DCHECK_EQ(1u, event_targets_[accelerator].size()); |
85 | |
86 GlobalShortcutListener::GetInstance()->RegisterAccelerator( | |
87 iter->second.accelerator(), this); | |
88 } | 88 } |
89 } | 89 } |
90 | 90 |
91 void ExtensionCommandsGlobalRegistry::RemoveExtensionKeybindingImpl( | 91 void ExtensionCommandsGlobalRegistry::RemoveExtensionKeybindingImpl( |
92 const ui::Accelerator& accelerator, | 92 const ui::Accelerator& accelerator, |
93 const std::string& command_name) { | 93 const std::string& command_name) { |
94 VLOG(0) << "Removing keybinding for " << command_name.c_str(); | 94 VLOG(0) << "Removing keybinding for " << command_name.c_str(); |
95 | 95 |
96 GlobalShortcutListener::GetInstance()->UnregisterAccelerator( | 96 GlobalShortcutListener::GetInstance()->UnregisterAccelerator( |
97 accelerator, this); | 97 accelerator, this); |
98 } | 98 } |
99 | 99 |
100 void ExtensionCommandsGlobalRegistry::OnKeyPressed( | 100 void ExtensionCommandsGlobalRegistry::OnKeyPressed( |
101 const ui::Accelerator& accelerator) { | 101 const ui::Accelerator& accelerator) { |
102 ExtensionKeybindingRegistry::NotifyEventTargets(accelerator); | 102 ExtensionKeybindingRegistry::NotifyEventTargets(accelerator); |
103 } | 103 } |
104 | 104 |
105 } // namespace extensions | 105 } // namespace extensions |
OLD | NEW |