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 <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "base/values.h" | |
16 #include "chrome/app/chrome_command_ids.h" | 15 #include "chrome/app/chrome_command_ids.h" |
17 #include "chrome/browser/extensions/api/commands/commands.h" | 16 #include "chrome/browser/extensions/api/commands/commands.h" |
18 #include "chrome/browser/extensions/extension_commands_global_registry.h" | 17 #include "chrome/browser/extensions/extension_commands_global_registry.h" |
19 #include "chrome/browser/extensions/extension_keybinding_registry.h" | 18 #include "chrome/browser/extensions/extension_keybinding_registry.h" |
20 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/ui/accelerator_utils.h" | 20 #include "chrome/browser/ui/accelerator_utils.h" |
22 #include "chrome/common/extensions/api/commands/commands_handler.h" | 21 #include "chrome/common/extensions/api/commands/commands_handler.h" |
23 #include "chrome/common/extensions/manifest_handlers/ui_overrides_handler.h" | 22 #include "chrome/common/extensions/manifest_handlers/ui_overrides_handler.h" |
24 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
25 #include "components/pref_registry/pref_registry_syncable.h" | 24 #include "components/pref_registry/pref_registry_syncable.h" |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 item->GetString(kCommandName, &old_command_name); | 270 item->GetString(kCommandName, &old_command_name); |
272 RemoveKeybindingPrefs(old_extension_id, old_command_name); | 271 RemoveKeybindingPrefs(old_extension_id, old_command_name); |
273 } | 272 } |
274 | 273 |
275 // If the command that is taking a new shortcut already has a shortcut, remove | 274 // If the command that is taking a new shortcut already has a shortcut, remove |
276 // it before assigning the new one. | 275 // it before assigning the new one. |
277 if (existing_command.accelerator().key_code() != ui::VKEY_UNKNOWN) | 276 if (existing_command.accelerator().key_code() != ui::VKEY_UNKNOWN) |
278 RemoveKeybindingPrefs(extension_id, command_name); | 277 RemoveKeybindingPrefs(extension_id, command_name); |
279 | 278 |
280 // Set the keybinding pref. | 279 // Set the keybinding pref. |
281 auto keybinding = base::MakeUnique<base::DictionaryValue>(); | 280 base::DictionaryValue* keybinding = new base::DictionaryValue(); |
282 keybinding->SetString(kExtension, extension_id); | 281 keybinding->SetString(kExtension, extension_id); |
283 keybinding->SetString(kCommandName, command_name); | 282 keybinding->SetString(kCommandName, command_name); |
284 keybinding->SetBoolean(kGlobal, global); | 283 keybinding->SetBoolean(kGlobal, global); |
285 | 284 |
286 bindings->Set(key, std::move(keybinding)); | 285 bindings->Set(key, keybinding); |
287 | 286 |
288 // Set the was_assigned pref for the suggested key. | 287 // Set the was_assigned pref for the suggested key. |
289 std::unique_ptr<base::DictionaryValue> command_keys( | 288 std::unique_ptr<base::DictionaryValue> command_keys( |
290 new base::DictionaryValue); | 289 new base::DictionaryValue); |
291 command_keys->SetBoolean(kSuggestedKeyWasAssigned, true); | 290 command_keys->SetBoolean(kSuggestedKeyWasAssigned, true); |
292 std::unique_ptr<base::DictionaryValue> suggested_key_prefs( | 291 std::unique_ptr<base::DictionaryValue> suggested_key_prefs( |
293 new base::DictionaryValue); | 292 new base::DictionaryValue); |
294 suggested_key_prefs->Set(command_name, std::move(command_keys)); | 293 suggested_key_prefs->Set(command_name, command_keys.release()); |
295 MergeSuggestedKeyPrefs(extension_id, ExtensionPrefs::Get(profile_), | 294 MergeSuggestedKeyPrefs(extension_id, ExtensionPrefs::Get(profile_), |
296 std::move(suggested_key_prefs)); | 295 std::move(suggested_key_prefs)); |
297 | 296 |
298 // Fetch the newly-updated command, and notify the observers. | 297 // Fetch the newly-updated command, and notify the observers. |
299 for (auto& observer : observers_) { | 298 for (auto& observer : observers_) { |
300 observer.OnExtensionCommandAdded( | 299 observer.OnExtensionCommandAdded( |
301 extension_id, FindCommandByName(extension_id, command_name)); | 300 extension_id, FindCommandByName(extension_id, command_name)); |
302 } | 301 } |
303 | 302 |
304 // TODO(devlin): Deprecate this notification in favor of the observers. | 303 // TODO(devlin): Deprecate this notification in favor of the observers. |
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
640 const CommandMap* commands = CommandsInfo::GetNamedCommands(extension); | 639 const CommandMap* commands = CommandsInfo::GetNamedCommands(extension); |
641 if (commands) { | 640 if (commands) { |
642 for (CommandMap::const_iterator iter = commands->begin(); | 641 for (CommandMap::const_iterator iter = commands->begin(); |
643 iter != commands->end(); ++iter) { | 642 iter != commands->end(); ++iter) { |
644 const Command command = iter->second; | 643 const Command command = iter->second; |
645 std::unique_ptr<base::DictionaryValue> command_keys( | 644 std::unique_ptr<base::DictionaryValue> command_keys( |
646 new base::DictionaryValue); | 645 new base::DictionaryValue); |
647 command_keys->SetString( | 646 command_keys->SetString( |
648 kSuggestedKey, | 647 kSuggestedKey, |
649 Command::AcceleratorToString(command.accelerator())); | 648 Command::AcceleratorToString(command.accelerator())); |
650 suggested_key_prefs->Set(command.command_name(), std::move(command_keys)); | 649 suggested_key_prefs->Set(command.command_name(), command_keys.release()); |
651 } | 650 } |
652 } | 651 } |
653 | 652 |
654 const Command* browser_action_command = | 653 const Command* browser_action_command = |
655 CommandsInfo::GetBrowserActionCommand(extension); | 654 CommandsInfo::GetBrowserActionCommand(extension); |
656 // The browser action command may be defaulted to an unassigned accelerator if | 655 // The browser action command may be defaulted to an unassigned accelerator if |
657 // a browser action is specified by the extension but a keybinding is not | 656 // a browser action is specified by the extension but a keybinding is not |
658 // declared. See CommandsHandler::MaybeSetBrowserActionDefault. | 657 // declared. See CommandsHandler::MaybeSetBrowserActionDefault. |
659 if (browser_action_command && | 658 if (browser_action_command && |
660 browser_action_command->accelerator().key_code() != ui::VKEY_UNKNOWN) { | 659 browser_action_command->accelerator().key_code() != ui::VKEY_UNKNOWN) { |
661 std::unique_ptr<base::DictionaryValue> command_keys( | 660 std::unique_ptr<base::DictionaryValue> command_keys( |
662 new base::DictionaryValue); | 661 new base::DictionaryValue); |
663 command_keys->SetString( | 662 command_keys->SetString( |
664 kSuggestedKey, | 663 kSuggestedKey, |
665 Command::AcceleratorToString(browser_action_command->accelerator())); | 664 Command::AcceleratorToString(browser_action_command->accelerator())); |
666 suggested_key_prefs->Set(browser_action_command->command_name(), | 665 suggested_key_prefs->Set(browser_action_command->command_name(), |
667 std::move(command_keys)); | 666 command_keys.release()); |
668 } | 667 } |
669 | 668 |
670 const Command* page_action_command = | 669 const Command* page_action_command = |
671 CommandsInfo::GetPageActionCommand(extension); | 670 CommandsInfo::GetPageActionCommand(extension); |
672 if (page_action_command) { | 671 if (page_action_command) { |
673 std::unique_ptr<base::DictionaryValue> command_keys( | 672 std::unique_ptr<base::DictionaryValue> command_keys( |
674 new base::DictionaryValue); | 673 new base::DictionaryValue); |
675 command_keys->SetString( | 674 command_keys->SetString( |
676 kSuggestedKey, | 675 kSuggestedKey, |
677 Command::AcceleratorToString(page_action_command->accelerator())); | 676 Command::AcceleratorToString(page_action_command->accelerator())); |
678 suggested_key_prefs->Set(page_action_command->command_name(), | 677 suggested_key_prefs->Set(page_action_command->command_name(), |
679 std::move(command_keys)); | 678 command_keys.release()); |
680 } | 679 } |
681 | 680 |
682 // Merge into current prefs, if present. | 681 // Merge into current prefs, if present. |
683 MergeSuggestedKeyPrefs(extension->id(), ExtensionPrefs::Get(profile_), | 682 MergeSuggestedKeyPrefs(extension->id(), ExtensionPrefs::Get(profile_), |
684 std::move(suggested_key_prefs)); | 683 std::move(suggested_key_prefs)); |
685 } | 684 } |
686 | 685 |
687 void CommandService::RemoveDefunctExtensionSuggestedCommandPrefs( | 686 void CommandService::RemoveDefunctExtensionSuggestedCommandPrefs( |
688 const Extension* extension) { | 687 const Extension* extension) { |
689 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_); | 688 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_); |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
909 return true; | 908 return true; |
910 } | 909 } |
911 | 910 |
912 template <> | 911 template <> |
913 void | 912 void |
914 BrowserContextKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() { | 913 BrowserContextKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() { |
915 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance()); | 914 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance()); |
916 } | 915 } |
917 | 916 |
918 } // namespace extensions | 917 } // namespace extensions |
OLD | NEW |