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/extension_command_service.h" | 5 #include "chrome/browser/extensions/api/commands/extension_command_service.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "chrome/browser/extensions/extension_keybinding_registry.h" | 8 #include "chrome/browser/extensions/extension_keybinding_registry.h" |
9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
10 #include "chrome/browser/extensions/extension_system.h" | 10 #include "chrome/browser/extensions/extension_system.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 const char kCommandName[] = "command_name"; | 22 const char kCommandName[] = "command_name"; |
23 | 23 |
24 std::string GetPlatformKeybindingKeyForAccelerator( | 24 std::string GetPlatformKeybindingKeyForAccelerator( |
25 const ui::Accelerator& accelerator) { | 25 const ui::Accelerator& accelerator) { |
26 return extensions::Command::CommandPlatform() + ":" + | 26 return extensions::Command::CommandPlatform() + ":" + |
27 UTF16ToUTF8(accelerator.GetShortcutText()); | 27 UTF16ToUTF8(accelerator.GetShortcutText()); |
28 } | 28 } |
29 | 29 |
30 } // namespace | 30 } // namespace |
31 | 31 |
| 32 namespace extensions { |
| 33 |
32 // static | 34 // static |
33 void ExtensionCommandService::RegisterUserPrefs( | 35 void ExtensionCommandService::RegisterUserPrefs( |
34 PrefService* user_prefs) { | 36 PrefService* user_prefs) { |
35 user_prefs->RegisterDictionaryPref(prefs::kExtensionKeybindings, | 37 user_prefs->RegisterDictionaryPref(prefs::kExtensionKeybindings, |
36 PrefService::SYNCABLE_PREF); | 38 PrefService::SYNCABLE_PREF); |
37 } | 39 } |
38 | 40 |
39 ExtensionCommandService::ExtensionCommandService( | 41 ExtensionCommandService::ExtensionCommandService( |
40 Profile* profile) | 42 Profile* profile) |
41 : profile_(profile) { | 43 : profile_(profile) { |
42 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, | 44 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, |
43 content::Source<Profile>(profile)); | 45 content::Source<Profile>(profile)); |
44 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | 46 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
45 content::Source<Profile>(profile)); | 47 content::Source<Profile>(profile)); |
46 } | 48 } |
47 | 49 |
48 ExtensionCommandService::~ExtensionCommandService() { | 50 ExtensionCommandService::~ExtensionCommandService() { |
49 } | 51 } |
50 | 52 |
51 const extensions::Command* | 53 const extensions::Command* |
52 ExtensionCommandService::GetActiveBrowserActionCommand( | 54 ExtensionCommandService::GetBrowserActionCommand( |
53 const std::string& extension_id) { | 55 const std::string& extension_id, QueryType type) { |
54 const ExtensionSet* extensions = | 56 const ExtensionSet* extensions = |
55 ExtensionSystem::Get(profile_)->extension_service()->extensions(); | 57 ExtensionSystem::Get(profile_)->extension_service()->extensions(); |
56 const Extension* extension = extensions->GetByID(extension_id); | 58 const Extension* extension = extensions->GetByID(extension_id); |
57 CHECK(extension); | 59 CHECK(extension); |
58 | 60 |
59 const extensions::Command* command = extension->browser_action_command(); | 61 const extensions::Command* command = extension->browser_action_command(); |
60 if (!command) | 62 if (!command) |
61 return NULL; | 63 return NULL; |
62 if (!IsKeybindingActive(command->accelerator(), | 64 if (type == ACTIVE_ONLY && |
| 65 !IsKeybindingActive(command->accelerator(), |
63 extension_id, | 66 extension_id, |
64 command->command_name())) { | 67 command->command_name())) { |
65 return NULL; | 68 return NULL; |
66 } | 69 } |
67 | 70 |
68 return command; | 71 return command; |
69 } | 72 } |
70 | 73 |
71 const extensions::Command* ExtensionCommandService::GetActivePageActionCommand( | 74 const extensions::Command* ExtensionCommandService::GetPageActionCommand( |
72 const std::string& extension_id) { | 75 const std::string& extension_id, QueryType type) { |
73 const ExtensionSet* extensions = | 76 const ExtensionSet* extensions = |
74 ExtensionSystem::Get(profile_)->extension_service()->extensions(); | 77 ExtensionSystem::Get(profile_)->extension_service()->extensions(); |
75 const Extension* extension = extensions->GetByID(extension_id); | 78 const Extension* extension = extensions->GetByID(extension_id); |
76 CHECK(extension); | 79 CHECK(extension); |
77 | 80 |
78 const extensions::Command* command = extension->page_action_command(); | 81 const extensions::Command* command = extension->page_action_command(); |
79 if (!command) | 82 if (!command) |
80 return NULL; | 83 return NULL; |
81 if (!IsKeybindingActive(command->accelerator(), | 84 if (type == ACTIVE_ONLY && |
| 85 !IsKeybindingActive(command->accelerator(), |
82 extension_id, | 86 extension_id, |
83 command->command_name())) { | 87 command->command_name())) { |
84 return NULL; | 88 return NULL; |
85 } | 89 } |
86 | 90 |
87 return command; | 91 return command; |
88 } | 92 } |
89 | 93 |
90 extensions::CommandMap ExtensionCommandService::GetActiveNamedCommands( | 94 extensions::CommandMap ExtensionCommandService::GetNamedCommands( |
91 const std::string& extension_id) { | 95 const std::string& extension_id, QueryType type) { |
92 const ExtensionSet* extensions = | 96 const ExtensionSet* extensions = |
93 ExtensionSystem::Get(profile_)->extension_service()->extensions(); | 97 ExtensionSystem::Get(profile_)->extension_service()->extensions(); |
94 const Extension* extension = extensions->GetByID(extension_id); | 98 const Extension* extension = extensions->GetByID(extension_id); |
95 CHECK(extension); | 99 CHECK(extension); |
96 | 100 |
97 extensions::CommandMap result; | 101 extensions::CommandMap result; |
98 const extensions::CommandMap& commands = extension->named_commands(); | 102 const extensions::CommandMap& commands = extension->named_commands(); |
99 if (commands.empty()) | 103 if (commands.empty()) |
100 return result; | 104 return result; |
101 | 105 |
102 extensions::CommandMap::const_iterator iter = commands.begin(); | 106 extensions::CommandMap::const_iterator iter = commands.begin(); |
103 for (; iter != commands.end(); ++iter) { | 107 for (; iter != commands.end(); ++iter) { |
104 if (!IsKeybindingActive(iter->second.accelerator(), | 108 if (type == ACTIVE_ONLY && |
| 109 !IsKeybindingActive(iter->second.accelerator(), |
105 extension_id, | 110 extension_id, |
106 iter->second.command_name())) { | 111 iter->second.command_name())) { |
107 continue; | 112 continue; |
108 } | 113 } |
109 | 114 |
110 result[iter->second.command_name()] = iter->second; | 115 result[iter->second.command_name()] = iter->second; |
111 } | 116 } |
112 | 117 |
113 return result; | 118 return result; |
114 } | 119 } |
115 | 120 |
116 bool ExtensionCommandService::IsKeybindingActive( | 121 bool ExtensionCommandService::IsKeybindingActive( |
117 const ui::Accelerator& accelerator, | 122 const ui::Accelerator& accelerator, |
118 std::string extension_id, | 123 const std::string& extension_id, |
119 std::string command_name) { | 124 const std::string& command_name) const { |
120 CHECK(!extension_id.empty()); | 125 CHECK(!extension_id.empty()); |
121 CHECK(!command_name.empty()); | 126 CHECK(!command_name.empty()); |
122 | 127 |
123 std::string key = GetPlatformKeybindingKeyForAccelerator(accelerator); | 128 std::string key = GetPlatformKeybindingKeyForAccelerator(accelerator); |
124 const DictionaryValue* bindings = | 129 const DictionaryValue* bindings = |
125 profile_->GetPrefs()->GetDictionary(prefs::kExtensionKeybindings); | 130 profile_->GetPrefs()->GetDictionary(prefs::kExtensionKeybindings); |
126 if (!bindings->HasKey(key)) | 131 if (!bindings->HasKey(key)) |
127 return false; | 132 return false; |
128 | 133 |
129 DictionaryValue* value = NULL; | 134 DictionaryValue* value = NULL; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 item->GetString(kExtension, &extension); | 232 item->GetString(kExtension, &extension); |
228 if (extension == extension_id) | 233 if (extension == extension_id) |
229 keys_to_remove.push_back(key); | 234 keys_to_remove.push_back(key); |
230 } | 235 } |
231 | 236 |
232 for (KeysToRemove::const_iterator it = keys_to_remove.begin(); | 237 for (KeysToRemove::const_iterator it = keys_to_remove.begin(); |
233 it != keys_to_remove.end(); ++it) { | 238 it != keys_to_remove.end(); ++it) { |
234 bindings->Remove(*it, NULL); | 239 bindings->Remove(*it, NULL); |
235 } | 240 } |
236 } | 241 } |
| 242 |
| 243 } // namespace extensions |
OLD | NEW |