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/extensions/api/commands/command_service.h" | 5 #include "chrome/browser/extensions/api/commands/command_service.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
11 #include "chrome/browser/extensions/api/commands/commands.h" | 11 #include "chrome/browser/extensions/api/commands/commands.h" |
| 12 #include "chrome/browser/extensions/extension_commands_global_registry.h" |
12 #include "chrome/browser/extensions/extension_function_registry.h" | 13 #include "chrome/browser/extensions/extension_function_registry.h" |
13 #include "chrome/browser/extensions/extension_keybinding_registry.h" | 14 #include "chrome/browser/extensions/extension_keybinding_registry.h" |
14 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
15 #include "chrome/browser/extensions/extension_system.h" | 16 #include "chrome/browser/extensions/extension_system.h" |
16 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 17 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/ui/accelerator_utils.h" | 19 #include "chrome/browser/ui/accelerator_utils.h" |
19 #include "chrome/common/extensions/api/commands/commands_handler.h" | 20 #include "chrome/common/extensions/api/commands/commands_handler.h" |
20 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
21 #include "components/user_prefs/pref_registry_syncable.h" | 22 #include "components/user_prefs/pref_registry_syncable.h" |
(...skipping 28 matching lines...) Expand all Loading... |
50 const ExtensionPrefs* prefs, const std::string& extension_id) { | 51 const ExtensionPrefs* prefs, const std::string& extension_id) { |
51 bool assigned = false; | 52 bool assigned = false; |
52 if (!prefs || !prefs->ReadPrefAsBoolean(extension_id, | 53 if (!prefs || !prefs->ReadPrefAsBoolean(extension_id, |
53 kInitialBindingsHaveBeenAssigned, | 54 kInitialBindingsHaveBeenAssigned, |
54 &assigned)) | 55 &assigned)) |
55 return false; | 56 return false; |
56 | 57 |
57 return assigned; | 58 return assigned; |
58 } | 59 } |
59 | 60 |
| 61 bool IsWhitelistedGlobalShortcut(const extensions::Command& command) { |
| 62 if (!command.global()) |
| 63 return true; |
| 64 if (!command.accelerator().IsCtrlDown()) |
| 65 return false; |
| 66 if (!command.accelerator().IsShiftDown()) |
| 67 return false; |
| 68 return (command.accelerator().key_code() >= ui::VKEY_0 && |
| 69 command.accelerator().key_code() <= ui::VKEY_9); |
| 70 } |
| 71 |
60 } // namespace | 72 } // namespace |
61 | 73 |
62 namespace extensions { | 74 namespace extensions { |
63 | 75 |
64 // static | 76 // static |
65 void CommandService::RegisterProfilePrefs( | 77 void CommandService::RegisterProfilePrefs( |
66 user_prefs::PrefRegistrySyncable* registry) { | 78 user_prefs::PrefRegistrySyncable* registry) { |
67 registry->RegisterDictionaryPref( | 79 registry->RegisterDictionaryPref( |
68 prefs::kExtensionCommands, | 80 prefs::kExtensionCommands, |
69 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 81 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 const std::string& extension_id, | 130 const std::string& extension_id, |
119 QueryType type, | 131 QueryType type, |
120 extensions::Command* command, | 132 extensions::Command* command, |
121 bool* active) { | 133 bool* active) { |
122 return GetExtensionActionCommand( | 134 return GetExtensionActionCommand( |
123 extension_id, type, command, active, SCRIPT_BADGE); | 135 extension_id, type, command, active, SCRIPT_BADGE); |
124 } | 136 } |
125 | 137 |
126 bool CommandService::GetNamedCommands(const std::string& extension_id, | 138 bool CommandService::GetNamedCommands(const std::string& extension_id, |
127 QueryType type, | 139 QueryType type, |
| 140 CommandScope scope, |
128 extensions::CommandMap* command_map) { | 141 extensions::CommandMap* command_map) { |
129 const ExtensionSet* extensions = | 142 ExtensionService* extension_service = |
130 ExtensionSystem::Get(profile_)->extension_service()->extensions(); | 143 ExtensionSystem::Get(profile_)->extension_service(); |
| 144 if (!extension_service) |
| 145 return false; // Can occur during testing. |
| 146 const ExtensionSet* extensions = extension_service->extensions(); |
131 const Extension* extension = extensions->GetByID(extension_id); | 147 const Extension* extension = extensions->GetByID(extension_id); |
132 CHECK(extension); | 148 CHECK(extension); |
133 | 149 |
134 command_map->clear(); | 150 command_map->clear(); |
135 const extensions::CommandMap* commands = | 151 const extensions::CommandMap* commands = |
136 CommandsInfo::GetNamedCommands(extension); | 152 CommandsInfo::GetNamedCommands(extension); |
137 if (!commands) | 153 if (!commands) |
138 return false; | 154 return false; |
139 | 155 |
140 extensions::CommandMap::const_iterator iter = commands->begin(); | 156 extensions::CommandMap::const_iterator iter = commands->begin(); |
141 for (; iter != commands->end(); ++iter) { | 157 for (; iter != commands->end(); ++iter) { |
142 ui::Accelerator shortcut_assigned = | 158 ui::Accelerator shortcut_assigned = |
143 FindShortcutForCommand(extension_id, iter->second.command_name()); | 159 FindShortcutForCommand(extension_id, iter->second.command_name()); |
144 | 160 |
145 if (type == ACTIVE_ONLY && shortcut_assigned.key_code() == ui::VKEY_UNKNOWN) | 161 if (type == ACTIVE_ONLY && shortcut_assigned.key_code() == ui::VKEY_UNKNOWN) |
146 continue; | 162 continue; |
147 | 163 |
148 extensions::Command command = iter->second; | 164 extensions::Command command = iter->second; |
| 165 if (scope != ANY_SCOPE && ((scope == GLOBAL) != command.global())) |
| 166 continue; |
| 167 |
149 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN) | 168 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN) |
150 command.set_accelerator(shortcut_assigned); | 169 command.set_accelerator(shortcut_assigned); |
151 | 170 |
152 (*command_map)[iter->second.command_name()] = command; | 171 (*command_map)[iter->second.command_name()] = command; |
153 } | 172 } |
154 | 173 |
155 return true; | 174 return true; |
156 } | 175 } |
157 | 176 |
158 bool CommandService::AddKeybindingPref( | 177 bool CommandService::AddKeybindingPref( |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 ExtensionService* extension_service = | 277 ExtensionService* extension_service = |
259 ExtensionSystem::Get(profile_)->extension_service(); | 278 ExtensionSystem::Get(profile_)->extension_service(); |
260 ExtensionPrefs* extension_prefs = extension_service->extension_prefs(); | 279 ExtensionPrefs* extension_prefs = extension_service->extension_prefs(); |
261 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id())) | 280 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id())) |
262 return; | 281 return; |
263 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id()); | 282 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id()); |
264 | 283 |
265 extensions::CommandMap::const_iterator iter = commands->begin(); | 284 extensions::CommandMap::const_iterator iter = commands->begin(); |
266 for (; iter != commands->end(); ++iter) { | 285 for (; iter != commands->end(); ++iter) { |
267 if (!chrome::IsChromeAccelerator( | 286 if (!chrome::IsChromeAccelerator( |
268 iter->second.accelerator(), profile_)) { | 287 iter->second.accelerator(), profile_) && |
| 288 IsWhitelistedGlobalShortcut(iter->second)) { |
269 AddKeybindingPref(iter->second.accelerator(), | 289 AddKeybindingPref(iter->second.accelerator(), |
270 extension->id(), | 290 extension->id(), |
271 iter->second.command_name(), | 291 iter->second.command_name(), |
272 false); // Overwriting not allowed. | 292 false); // Overwriting not allowed. |
273 } | 293 } |
274 } | 294 } |
275 | 295 |
276 const extensions::Command* browser_action_command = | 296 const extensions::Command* browser_action_command = |
277 CommandsInfo::GetBrowserActionCommand(extension); | 297 CommandsInfo::GetBrowserActionCommand(extension); |
278 if (browser_action_command) { | 298 if (browser_action_command) { |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 shortcut_assigned.key_code() == ui::VKEY_UNKNOWN) | 417 shortcut_assigned.key_code() == ui::VKEY_UNKNOWN) |
398 return false; | 418 return false; |
399 | 419 |
400 *command = *requested_command; | 420 *command = *requested_command; |
401 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN) | 421 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN) |
402 command->set_accelerator(shortcut_assigned); | 422 command->set_accelerator(shortcut_assigned); |
403 | 423 |
404 return true; | 424 return true; |
405 } | 425 } |
406 | 426 |
| 427 template <> |
| 428 void ProfileKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() { |
| 429 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance()); |
| 430 } |
| 431 |
407 } // namespace extensions | 432 } // namespace extensions |
OLD | NEW |