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/string_util.h" | 7 #include "base/string_util.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/extensions/extension_keybinding_registry.h" | 9 #include "chrome/browser/extensions/extension_keybinding_registry.h" |
10 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 } | 50 } |
51 | 51 |
52 CommandService::~CommandService() { | 52 CommandService::~CommandService() { |
53 } | 53 } |
54 | 54 |
55 bool CommandService::GetBrowserActionCommand( | 55 bool CommandService::GetBrowserActionCommand( |
56 const std::string& extension_id, | 56 const std::string& extension_id, |
57 QueryType type, | 57 QueryType type, |
58 extensions::Command* command, | 58 extensions::Command* command, |
59 bool* active) { | 59 bool* active) { |
60 return GetExtensionActionCommand(extension_id, type, command, active, true); | 60 return GetExtensionActionCommand( |
| 61 extension_id, type, command, active, BROWSER_ACTION); |
61 } | 62 } |
62 | 63 |
63 bool CommandService::GetPageActionCommand( | 64 bool CommandService::GetPageActionCommand( |
64 const std::string& extension_id, | 65 const std::string& extension_id, |
65 QueryType type, | 66 QueryType type, |
66 extensions::Command* command, | 67 extensions::Command* command, |
67 bool* active) { | 68 bool* active) { |
68 return GetExtensionActionCommand(extension_id, type, command, active, false); | 69 return GetExtensionActionCommand( |
| 70 extension_id, type, command, active, PAGE_ACTION); |
| 71 } |
| 72 |
| 73 bool CommandService::GetScriptBadgeCommand( |
| 74 const std::string& extension_id, |
| 75 QueryType type, |
| 76 extensions::Command* command, |
| 77 bool* active) { |
| 78 return GetExtensionActionCommand( |
| 79 extension_id, type, command, active, SCRIPT_BADGE); |
69 } | 80 } |
70 | 81 |
71 bool CommandService::GetNamedCommands(const std::string& extension_id, | 82 bool CommandService::GetNamedCommands(const std::string& extension_id, |
72 QueryType type, | 83 QueryType type, |
73 extensions::CommandMap* command_map) { | 84 extensions::CommandMap* command_map) { |
74 const ExtensionSet* extensions = | 85 const ExtensionSet* extensions = |
75 ExtensionSystem::Get(profile_)->extension_service()->extensions(); | 86 ExtensionSystem::Get(profile_)->extension_service()->extensions(); |
76 const Extension* extension = extensions->GetByID(extension_id); | 87 const Extension* extension = extensions->GetByID(extension_id); |
77 CHECK(extension); | 88 CHECK(extension); |
78 | 89 |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 } | 223 } |
213 | 224 |
214 const extensions::Command* page_action_command = | 225 const extensions::Command* page_action_command = |
215 extension->page_action_command(); | 226 extension->page_action_command(); |
216 if (page_action_command) { | 227 if (page_action_command) { |
217 AddKeybindingPref(page_action_command->accelerator(), | 228 AddKeybindingPref(page_action_command->accelerator(), |
218 extension->id(), | 229 extension->id(), |
219 page_action_command->command_name(), | 230 page_action_command->command_name(), |
220 false); // Overwriting not allowed. | 231 false); // Overwriting not allowed. |
221 } | 232 } |
| 233 |
| 234 const extensions::Command* script_badge_command = |
| 235 extension->script_badge_command(); |
| 236 if (script_badge_command) { |
| 237 AddKeybindingPref(script_badge_command->accelerator(), |
| 238 extension->id(), |
| 239 script_badge_command->command_name(), |
| 240 false); // Overwriting not allowed. |
| 241 } |
222 } | 242 } |
223 | 243 |
224 void CommandService::RemoveKeybindingPrefs(const std::string& extension_id, | 244 void CommandService::RemoveKeybindingPrefs(const std::string& extension_id, |
225 const std::string& command_name) { | 245 const std::string& command_name) { |
226 DictionaryPrefUpdate updater(profile_->GetPrefs(), | 246 DictionaryPrefUpdate updater(profile_->GetPrefs(), |
227 prefs::kExtensionCommands); | 247 prefs::kExtensionCommands); |
228 DictionaryValue* bindings = updater.Get(); | 248 DictionaryValue* bindings = updater.Get(); |
229 | 249 |
230 typedef std::vector<std::string> KeysToRemove; | 250 typedef std::vector<std::string> KeysToRemove; |
231 KeysToRemove keys_to_remove; | 251 KeysToRemove keys_to_remove; |
(...skipping 28 matching lines...) Expand all Loading... |
260 std::pair<const std::string, const std::string> details = | 280 std::pair<const std::string, const std::string> details = |
261 std::make_pair(extension_id, command_name); | 281 std::make_pair(extension_id, command_name); |
262 content::NotificationService::current()->Notify( | 282 content::NotificationService::current()->Notify( |
263 chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, | 283 chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, |
264 content::Source<Profile>(profile_), | 284 content::Source<Profile>(profile_), |
265 content::Details< | 285 content::Details< |
266 std::pair<const std::string, const std::string> >(&details)); | 286 std::pair<const std::string, const std::string> >(&details)); |
267 } | 287 } |
268 } | 288 } |
269 | 289 |
270 bool CommandService::GetExtensionActionCommand(const std::string& extension_id, | 290 bool CommandService::GetExtensionActionCommand( |
271 QueryType type, | 291 const std::string& extension_id, |
272 extensions::Command* command, | 292 QueryType query_type, |
273 bool* active, | 293 extensions::Command* command, |
274 bool browser_action) { | 294 bool* active, |
| 295 ExtensionActionType action_type) { |
275 const ExtensionSet* extensions = | 296 const ExtensionSet* extensions = |
276 ExtensionSystem::Get(profile_)->extension_service()->extensions(); | 297 ExtensionSystem::Get(profile_)->extension_service()->extensions(); |
277 const Extension* extension = extensions->GetByID(extension_id); | 298 const Extension* extension = extensions->GetByID(extension_id); |
278 CHECK(extension); | 299 CHECK(extension); |
279 | 300 |
280 if (active) | 301 if (active) |
281 *active = false; | 302 *active = false; |
282 | 303 |
283 const extensions::Command* requested_command = | 304 const extensions::Command* requested_command = NULL; |
284 browser_action ? extension->browser_action_command() : | 305 switch (action_type) { |
285 extension->page_action_command(); | 306 case BROWSER_ACTION: |
| 307 requested_command = extension->browser_action_command(); |
| 308 break; |
| 309 case PAGE_ACTION: |
| 310 requested_command = extension->page_action_command(); |
| 311 break; |
| 312 case SCRIPT_BADGE: |
| 313 requested_command = extension->script_badge_command(); |
| 314 break; |
| 315 } |
286 if (!requested_command) | 316 if (!requested_command) |
287 return false; | 317 return false; |
288 | 318 |
289 ui::Accelerator shortcut_assigned = | 319 ui::Accelerator shortcut_assigned = |
290 FindShortcutForCommand(extension_id, requested_command->command_name()); | 320 FindShortcutForCommand(extension_id, requested_command->command_name()); |
291 | 321 |
292 if (active) | 322 if (active) |
293 *active = (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN); | 323 *active = (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN); |
294 | 324 |
295 if (type == ACTIVE_ONLY && shortcut_assigned.key_code() == ui::VKEY_UNKNOWN) | 325 if (query_type == ACTIVE_ONLY && |
| 326 shortcut_assigned.key_code() == ui::VKEY_UNKNOWN) |
296 return false; | 327 return false; |
297 | 328 |
298 *command = *requested_command; | 329 *command = *requested_command; |
299 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN) | 330 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN) |
300 command->set_accelerator(shortcut_assigned); | 331 command->set_accelerator(shortcut_assigned); |
301 | 332 |
302 return true; | 333 return true; |
303 } | 334 } |
304 | 335 |
305 } // namespace extensions | 336 } // namespace extensions |
OLD | NEW |