| 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" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 return false; | 97 return false; |
| 98 | 98 |
| 99 return assigned; | 99 return assigned; |
| 100 } | 100 } |
| 101 | 101 |
| 102 // Merge |suggested_key_prefs| into the saved preferences for the extension. We | 102 // Merge |suggested_key_prefs| into the saved preferences for the extension. We |
| 103 // merge rather than overwrite to preserve existing was_assigned preferences. | 103 // merge rather than overwrite to preserve existing was_assigned preferences. |
| 104 void MergeSuggestedKeyPrefs( | 104 void MergeSuggestedKeyPrefs( |
| 105 const std::string& extension_id, | 105 const std::string& extension_id, |
| 106 ExtensionPrefs* extension_prefs, | 106 ExtensionPrefs* extension_prefs, |
| 107 scoped_ptr<base::DictionaryValue> suggested_key_prefs) { | 107 std::unique_ptr<base::DictionaryValue> suggested_key_prefs) { |
| 108 const base::DictionaryValue* current_prefs; | 108 const base::DictionaryValue* current_prefs; |
| 109 if (extension_prefs->ReadPrefAsDictionary(extension_id, | 109 if (extension_prefs->ReadPrefAsDictionary(extension_id, |
| 110 kCommands, | 110 kCommands, |
| 111 ¤t_prefs)) { | 111 ¤t_prefs)) { |
| 112 scoped_ptr<base::DictionaryValue> new_prefs(current_prefs->DeepCopy()); | 112 std::unique_ptr<base::DictionaryValue> new_prefs(current_prefs->DeepCopy()); |
| 113 new_prefs->MergeDictionary(suggested_key_prefs.get()); | 113 new_prefs->MergeDictionary(suggested_key_prefs.get()); |
| 114 suggested_key_prefs.reset(new_prefs.release()); | 114 suggested_key_prefs.reset(new_prefs.release()); |
| 115 } | 115 } |
| 116 | 116 |
| 117 extension_prefs->UpdateExtensionPref(extension_id, | 117 extension_prefs->UpdateExtensionPref(extension_id, |
| 118 kCommands, | 118 kCommands, |
| 119 suggested_key_prefs.release()); | 119 suggested_key_prefs.release()); |
| 120 } | 120 } |
| 121 | 121 |
| 122 } // namespace | 122 } // namespace |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 | 277 |
| 278 // Set the keybinding pref. | 278 // Set the keybinding pref. |
| 279 base::DictionaryValue* keybinding = new base::DictionaryValue(); | 279 base::DictionaryValue* keybinding = new base::DictionaryValue(); |
| 280 keybinding->SetString(kExtension, extension_id); | 280 keybinding->SetString(kExtension, extension_id); |
| 281 keybinding->SetString(kCommandName, command_name); | 281 keybinding->SetString(kCommandName, command_name); |
| 282 keybinding->SetBoolean(kGlobal, global); | 282 keybinding->SetBoolean(kGlobal, global); |
| 283 | 283 |
| 284 bindings->Set(key, keybinding); | 284 bindings->Set(key, keybinding); |
| 285 | 285 |
| 286 // Set the was_assigned pref for the suggested key. | 286 // Set the was_assigned pref for the suggested key. |
| 287 scoped_ptr<base::DictionaryValue> command_keys(new base::DictionaryValue); | 287 std::unique_ptr<base::DictionaryValue> command_keys( |
| 288 new base::DictionaryValue); |
| 288 command_keys->SetBoolean(kSuggestedKeyWasAssigned, true); | 289 command_keys->SetBoolean(kSuggestedKeyWasAssigned, true); |
| 289 scoped_ptr<base::DictionaryValue> suggested_key_prefs( | 290 std::unique_ptr<base::DictionaryValue> suggested_key_prefs( |
| 290 new base::DictionaryValue); | 291 new base::DictionaryValue); |
| 291 suggested_key_prefs->Set(command_name, command_keys.release()); | 292 suggested_key_prefs->Set(command_name, command_keys.release()); |
| 292 MergeSuggestedKeyPrefs(extension_id, ExtensionPrefs::Get(profile_), | 293 MergeSuggestedKeyPrefs(extension_id, ExtensionPrefs::Get(profile_), |
| 293 std::move(suggested_key_prefs)); | 294 std::move(suggested_key_prefs)); |
| 294 | 295 |
| 295 // Fetch the newly-updated command, and notify the observers. | 296 // Fetch the newly-updated command, and notify the observers. |
| 296 FOR_EACH_OBSERVER( | 297 FOR_EACH_OBSERVER( |
| 297 Observer, | 298 Observer, |
| 298 observers_, | 299 observers_, |
| 299 OnExtensionCommandAdded(extension_id, | 300 OnExtensionCommandAdded(extension_id, |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 625 DCHECK(chrome::IsChromeAccelerator(command.accelerator(), profile_)); | 626 DCHECK(chrome::IsChromeAccelerator(command.accelerator(), profile_)); |
| 626 return true; | 627 return true; |
| 627 } | 628 } |
| 628 | 629 |
| 629 return !chrome::IsChromeAccelerator(command.accelerator(), profile_); | 630 return !chrome::IsChromeAccelerator(command.accelerator(), profile_); |
| 630 } | 631 } |
| 631 } | 632 } |
| 632 | 633 |
| 633 void CommandService::UpdateExtensionSuggestedCommandPrefs( | 634 void CommandService::UpdateExtensionSuggestedCommandPrefs( |
| 634 const Extension* extension) { | 635 const Extension* extension) { |
| 635 scoped_ptr<base::DictionaryValue> suggested_key_prefs( | 636 std::unique_ptr<base::DictionaryValue> suggested_key_prefs( |
| 636 new base::DictionaryValue); | 637 new base::DictionaryValue); |
| 637 | 638 |
| 638 const CommandMap* commands = CommandsInfo::GetNamedCommands(extension); | 639 const CommandMap* commands = CommandsInfo::GetNamedCommands(extension); |
| 639 if (commands) { | 640 if (commands) { |
| 640 for (CommandMap::const_iterator iter = commands->begin(); | 641 for (CommandMap::const_iterator iter = commands->begin(); |
| 641 iter != commands->end(); ++iter) { | 642 iter != commands->end(); ++iter) { |
| 642 const Command command = iter->second; | 643 const Command command = iter->second; |
| 643 scoped_ptr<base::DictionaryValue> command_keys(new base::DictionaryValue); | 644 std::unique_ptr<base::DictionaryValue> command_keys( |
| 645 new base::DictionaryValue); |
| 644 command_keys->SetString( | 646 command_keys->SetString( |
| 645 kSuggestedKey, | 647 kSuggestedKey, |
| 646 Command::AcceleratorToString(command.accelerator())); | 648 Command::AcceleratorToString(command.accelerator())); |
| 647 suggested_key_prefs->Set(command.command_name(), command_keys.release()); | 649 suggested_key_prefs->Set(command.command_name(), command_keys.release()); |
| 648 } | 650 } |
| 649 } | 651 } |
| 650 | 652 |
| 651 const Command* browser_action_command = | 653 const Command* browser_action_command = |
| 652 CommandsInfo::GetBrowserActionCommand(extension); | 654 CommandsInfo::GetBrowserActionCommand(extension); |
| 653 // 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 |
| 654 // 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 |
| 655 // declared. See CommandsHandler::MaybeSetBrowserActionDefault. | 657 // declared. See CommandsHandler::MaybeSetBrowserActionDefault. |
| 656 if (browser_action_command && | 658 if (browser_action_command && |
| 657 browser_action_command->accelerator().key_code() != ui::VKEY_UNKNOWN) { | 659 browser_action_command->accelerator().key_code() != ui::VKEY_UNKNOWN) { |
| 658 scoped_ptr<base::DictionaryValue> command_keys(new base::DictionaryValue); | 660 std::unique_ptr<base::DictionaryValue> command_keys( |
| 661 new base::DictionaryValue); |
| 659 command_keys->SetString( | 662 command_keys->SetString( |
| 660 kSuggestedKey, | 663 kSuggestedKey, |
| 661 Command::AcceleratorToString(browser_action_command->accelerator())); | 664 Command::AcceleratorToString(browser_action_command->accelerator())); |
| 662 suggested_key_prefs->Set(browser_action_command->command_name(), | 665 suggested_key_prefs->Set(browser_action_command->command_name(), |
| 663 command_keys.release()); | 666 command_keys.release()); |
| 664 } | 667 } |
| 665 | 668 |
| 666 const Command* page_action_command = | 669 const Command* page_action_command = |
| 667 CommandsInfo::GetPageActionCommand(extension); | 670 CommandsInfo::GetPageActionCommand(extension); |
| 668 if (page_action_command) { | 671 if (page_action_command) { |
| 669 scoped_ptr<base::DictionaryValue> command_keys(new base::DictionaryValue); | 672 std::unique_ptr<base::DictionaryValue> command_keys( |
| 673 new base::DictionaryValue); |
| 670 command_keys->SetString( | 674 command_keys->SetString( |
| 671 kSuggestedKey, | 675 kSuggestedKey, |
| 672 Command::AcceleratorToString(page_action_command->accelerator())); | 676 Command::AcceleratorToString(page_action_command->accelerator())); |
| 673 suggested_key_prefs->Set(page_action_command->command_name(), | 677 suggested_key_prefs->Set(page_action_command->command_name(), |
| 674 command_keys.release()); | 678 command_keys.release()); |
| 675 } | 679 } |
| 676 | 680 |
| 677 // Merge into current prefs, if present. | 681 // Merge into current prefs, if present. |
| 678 MergeSuggestedKeyPrefs(extension->id(), ExtensionPrefs::Get(profile_), | 682 MergeSuggestedKeyPrefs(extension->id(), ExtensionPrefs::Get(profile_), |
| 679 std::move(suggested_key_prefs)); | 683 std::move(suggested_key_prefs)); |
| 680 } | 684 } |
| 681 | 685 |
| 682 void CommandService::RemoveDefunctExtensionSuggestedCommandPrefs( | 686 void CommandService::RemoveDefunctExtensionSuggestedCommandPrefs( |
| 683 const Extension* extension) { | 687 const Extension* extension) { |
| 684 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_); | 688 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_); |
| 685 const base::DictionaryValue* current_prefs = NULL; | 689 const base::DictionaryValue* current_prefs = NULL; |
| 686 extension_prefs->ReadPrefAsDictionary(extension->id(), | 690 extension_prefs->ReadPrefAsDictionary(extension->id(), |
| 687 kCommands, | 691 kCommands, |
| 688 ¤t_prefs); | 692 ¤t_prefs); |
| 689 | 693 |
| 690 if (current_prefs) { | 694 if (current_prefs) { |
| 691 scoped_ptr<base::DictionaryValue> suggested_key_prefs( | 695 std::unique_ptr<base::DictionaryValue> suggested_key_prefs( |
| 692 current_prefs->DeepCopy()); | 696 current_prefs->DeepCopy()); |
| 693 const CommandMap* named_commands = | 697 const CommandMap* named_commands = |
| 694 CommandsInfo::GetNamedCommands(extension); | 698 CommandsInfo::GetNamedCommands(extension); |
| 695 const Command* browser_action_command = | 699 const Command* browser_action_command = |
| 696 CommandsInfo::GetBrowserActionCommand(extension); | 700 CommandsInfo::GetBrowserActionCommand(extension); |
| 697 for (base::DictionaryValue::Iterator it(*current_prefs); | 701 for (base::DictionaryValue::Iterator it(*current_prefs); |
| 698 !it.IsAtEnd(); it.Advance()) { | 702 !it.IsAtEnd(); it.Advance()) { |
| 699 if (it.key() == manifest_values::kBrowserActionCommandEvent) { | 703 if (it.key() == manifest_values::kBrowserActionCommandEvent) { |
| 700 // The browser action command may be defaulted to an unassigned | 704 // The browser action command may be defaulted to an unassigned |
| 701 // accelerator if a browser action is specified by the extension but a | 705 // accelerator if a browser action is specified by the extension but a |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 907 return true; | 911 return true; |
| 908 } | 912 } |
| 909 | 913 |
| 910 template <> | 914 template <> |
| 911 void | 915 void |
| 912 BrowserContextKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() { | 916 BrowserContextKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() { |
| 913 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance()); | 917 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance()); |
| 914 } | 918 } |
| 915 | 919 |
| 916 } // namespace extensions | 920 } // namespace extensions |
| OLD | NEW |