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" |
jdoerrie
2017/04/06 14:25:50
#include "base/values.h"
vabr (Chromium)
2017/04/07 20:40:40
Done.
| |
15 #include "chrome/app/chrome_command_ids.h" | 15 #include "chrome/app/chrome_command_ids.h" |
16 #include "chrome/browser/extensions/api/commands/commands.h" | 16 #include "chrome/browser/extensions/api/commands/commands.h" |
17 #include "chrome/browser/extensions/extension_commands_global_registry.h" | 17 #include "chrome/browser/extensions/extension_commands_global_registry.h" |
18 #include "chrome/browser/extensions/extension_keybinding_registry.h" | 18 #include "chrome/browser/extensions/extension_keybinding_registry.h" |
19 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
20 #include "chrome/browser/ui/accelerator_utils.h" | 20 #include "chrome/browser/ui/accelerator_utils.h" |
21 #include "chrome/common/extensions/api/commands/commands_handler.h" | 21 #include "chrome/common/extensions/api/commands/commands_handler.h" |
22 #include "chrome/common/extensions/manifest_handlers/ui_overrides_handler.h" | 22 #include "chrome/common/extensions/manifest_handlers/ui_overrides_handler.h" |
23 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
24 #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... | |
270 item->GetString(kCommandName, &old_command_name); | 270 item->GetString(kCommandName, &old_command_name); |
271 RemoveKeybindingPrefs(old_extension_id, old_command_name); | 271 RemoveKeybindingPrefs(old_extension_id, old_command_name); |
272 } | 272 } |
273 | 273 |
274 // 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 |
275 // it before assigning the new one. | 275 // it before assigning the new one. |
276 if (existing_command.accelerator().key_code() != ui::VKEY_UNKNOWN) | 276 if (existing_command.accelerator().key_code() != ui::VKEY_UNKNOWN) |
277 RemoveKeybindingPrefs(extension_id, command_name); | 277 RemoveKeybindingPrefs(extension_id, command_name); |
278 | 278 |
279 // Set the keybinding pref. | 279 // Set the keybinding pref. |
280 base::DictionaryValue* keybinding = new base::DictionaryValue(); | 280 auto keybinding = base::MakeUnique<base::DictionaryValue>(); |
281 keybinding->SetString(kExtension, extension_id); | 281 keybinding->SetString(kExtension, extension_id); |
282 keybinding->SetString(kCommandName, command_name); | 282 keybinding->SetString(kCommandName, command_name); |
283 keybinding->SetBoolean(kGlobal, global); | 283 keybinding->SetBoolean(kGlobal, global); |
284 | 284 |
285 bindings->Set(key, keybinding); | 285 bindings->Set(key, std::move(keybinding)); |
286 | 286 |
287 // Set the was_assigned pref for the suggested key. | 287 // Set the was_assigned pref for the suggested key. |
288 std::unique_ptr<base::DictionaryValue> command_keys( | 288 std::unique_ptr<base::DictionaryValue> command_keys( |
289 new base::DictionaryValue); | 289 new base::DictionaryValue); |
290 command_keys->SetBoolean(kSuggestedKeyWasAssigned, true); | 290 command_keys->SetBoolean(kSuggestedKeyWasAssigned, true); |
291 std::unique_ptr<base::DictionaryValue> suggested_key_prefs( | 291 std::unique_ptr<base::DictionaryValue> suggested_key_prefs( |
292 new base::DictionaryValue); | 292 new base::DictionaryValue); |
293 suggested_key_prefs->Set(command_name, command_keys.release()); | 293 suggested_key_prefs->Set(command_name, std::move(command_keys)); |
294 MergeSuggestedKeyPrefs(extension_id, ExtensionPrefs::Get(profile_), | 294 MergeSuggestedKeyPrefs(extension_id, ExtensionPrefs::Get(profile_), |
295 std::move(suggested_key_prefs)); | 295 std::move(suggested_key_prefs)); |
296 | 296 |
297 // Fetch the newly-updated command, and notify the observers. | 297 // Fetch the newly-updated command, and notify the observers. |
298 for (auto& observer : observers_) { | 298 for (auto& observer : observers_) { |
299 observer.OnExtensionCommandAdded( | 299 observer.OnExtensionCommandAdded( |
300 extension_id, FindCommandByName(extension_id, command_name)); | 300 extension_id, FindCommandByName(extension_id, command_name)); |
301 } | 301 } |
302 | 302 |
303 // 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... | |
639 const CommandMap* commands = CommandsInfo::GetNamedCommands(extension); | 639 const CommandMap* commands = CommandsInfo::GetNamedCommands(extension); |
640 if (commands) { | 640 if (commands) { |
641 for (CommandMap::const_iterator iter = commands->begin(); | 641 for (CommandMap::const_iterator iter = commands->begin(); |
642 iter != commands->end(); ++iter) { | 642 iter != commands->end(); ++iter) { |
643 const Command command = iter->second; | 643 const Command command = iter->second; |
644 std::unique_ptr<base::DictionaryValue> command_keys( | 644 std::unique_ptr<base::DictionaryValue> command_keys( |
645 new base::DictionaryValue); | 645 new base::DictionaryValue); |
646 command_keys->SetString( | 646 command_keys->SetString( |
647 kSuggestedKey, | 647 kSuggestedKey, |
648 Command::AcceleratorToString(command.accelerator())); | 648 Command::AcceleratorToString(command.accelerator())); |
649 suggested_key_prefs->Set(command.command_name(), command_keys.release()); | 649 suggested_key_prefs->Set(command.command_name(), std::move(command_keys)); |
650 } | 650 } |
651 } | 651 } |
652 | 652 |
653 const Command* browser_action_command = | 653 const Command* browser_action_command = |
654 CommandsInfo::GetBrowserActionCommand(extension); | 654 CommandsInfo::GetBrowserActionCommand(extension); |
655 // 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 |
656 // 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 |
657 // declared. See CommandsHandler::MaybeSetBrowserActionDefault. | 657 // declared. See CommandsHandler::MaybeSetBrowserActionDefault. |
658 if (browser_action_command && | 658 if (browser_action_command && |
659 browser_action_command->accelerator().key_code() != ui::VKEY_UNKNOWN) { | 659 browser_action_command->accelerator().key_code() != ui::VKEY_UNKNOWN) { |
660 std::unique_ptr<base::DictionaryValue> command_keys( | 660 std::unique_ptr<base::DictionaryValue> command_keys( |
661 new base::DictionaryValue); | 661 new base::DictionaryValue); |
662 command_keys->SetString( | 662 command_keys->SetString( |
663 kSuggestedKey, | 663 kSuggestedKey, |
664 Command::AcceleratorToString(browser_action_command->accelerator())); | 664 Command::AcceleratorToString(browser_action_command->accelerator())); |
665 suggested_key_prefs->Set(browser_action_command->command_name(), | 665 suggested_key_prefs->Set(browser_action_command->command_name(), |
666 command_keys.release()); | 666 std::move(command_keys)); |
667 } | 667 } |
668 | 668 |
669 const Command* page_action_command = | 669 const Command* page_action_command = |
670 CommandsInfo::GetPageActionCommand(extension); | 670 CommandsInfo::GetPageActionCommand(extension); |
671 if (page_action_command) { | 671 if (page_action_command) { |
672 std::unique_ptr<base::DictionaryValue> command_keys( | 672 std::unique_ptr<base::DictionaryValue> command_keys( |
673 new base::DictionaryValue); | 673 new base::DictionaryValue); |
674 command_keys->SetString( | 674 command_keys->SetString( |
675 kSuggestedKey, | 675 kSuggestedKey, |
676 Command::AcceleratorToString(page_action_command->accelerator())); | 676 Command::AcceleratorToString(page_action_command->accelerator())); |
677 suggested_key_prefs->Set(page_action_command->command_name(), | 677 suggested_key_prefs->Set(page_action_command->command_name(), |
678 command_keys.release()); | 678 std::move(command_keys)); |
679 } | 679 } |
680 | 680 |
681 // Merge into current prefs, if present. | 681 // Merge into current prefs, if present. |
682 MergeSuggestedKeyPrefs(extension->id(), ExtensionPrefs::Get(profile_), | 682 MergeSuggestedKeyPrefs(extension->id(), ExtensionPrefs::Get(profile_), |
683 std::move(suggested_key_prefs)); | 683 std::move(suggested_key_prefs)); |
684 } | 684 } |
685 | 685 |
686 void CommandService::RemoveDefunctExtensionSuggestedCommandPrefs( | 686 void CommandService::RemoveDefunctExtensionSuggestedCommandPrefs( |
687 const Extension* extension) { | 687 const Extension* extension) { |
688 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... | |
908 return true; | 908 return true; |
909 } | 909 } |
910 | 910 |
911 template <> | 911 template <> |
912 void | 912 void |
913 BrowserContextKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() { | 913 BrowserContextKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() { |
914 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance()); | 914 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance()); |
915 } | 915 } |
916 | 916 |
917 } // namespace extensions | 917 } // namespace extensions |
OLD | NEW |