Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: chrome/browser/extensions/api/commands/command_service.cc

Issue 11662013: Move Commands from Extension to CommandsHandler (Closed) Base URL: http://git.chromium.org/chromium/src.git@dc_unref_script_badge
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "chrome/browser/extensions/extension_system.h" 11 #include "chrome/browser/extensions/extension_system.h"
12 #include "chrome/browser/prefs/scoped_user_pref_update.h" 12 #include "chrome/browser/prefs/scoped_user_pref_update.h"
13 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/chrome_notification_types.h" 14 #include "chrome/common/chrome_notification_types.h"
15 #include "chrome/common/extensions/api/commands/commands_handler.h"
15 #include "chrome/common/extensions/extension_manifest_constants.h" 16 #include "chrome/common/extensions/extension_manifest_constants.h"
16 #include "chrome/common/pref_names.h" 17 #include "chrome/common/pref_names.h"
17 #include "content/public/browser/notification_details.h" 18 #include "content/public/browser/notification_details.h"
18 #include "content/public/browser/notification_service.h" 19 #include "content/public/browser/notification_service.h"
19 20
20 using extensions::Extension; 21 using extensions::Extension;
21 22
22 namespace { 23 namespace {
23 24
24 const char kExtension[] = "extension"; 25 const char kExtension[] = "extension";
(...skipping 11 matching lines...) Expand all
36 37
37 // static 38 // static
38 void CommandService::RegisterUserPrefs( 39 void CommandService::RegisterUserPrefs(
39 PrefService* user_prefs) { 40 PrefService* user_prefs) {
40 user_prefs->RegisterDictionaryPref(prefs::kExtensionCommands, 41 user_prefs->RegisterDictionaryPref(prefs::kExtensionCommands,
41 PrefService::SYNCABLE_PREF); 42 PrefService::SYNCABLE_PREF);
42 } 43 }
43 44
44 CommandService::CommandService(Profile* profile) 45 CommandService::CommandService(Profile* profile)
45 : profile_(profile) { 46 : profile_(profile) {
47 ManifestHandler::Register(extension_manifest_keys::kCommands,
Devlin 2012/12/21 20:36:21 I don't think we want to create an additional Comm
Yoyo Zhou 2012/12/22 00:38:33 No, it doesn't need to be renamed.
48 new CommandsHandler);
46 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, 49 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
47 content::Source<Profile>(profile)); 50 content::Source<Profile>(profile));
48 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, 51 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
49 content::Source<Profile>(profile)); 52 content::Source<Profile>(profile));
50 } 53 }
51 54
52 CommandService::~CommandService() { 55 CommandService::~CommandService() {
53 } 56 }
54 57
55 bool CommandService::GetBrowserActionCommand( 58 bool CommandService::GetBrowserActionCommand(
(...skipping 25 matching lines...) Expand all
81 84
82 bool CommandService::GetNamedCommands(const std::string& extension_id, 85 bool CommandService::GetNamedCommands(const std::string& extension_id,
83 QueryType type, 86 QueryType type,
84 extensions::CommandMap* command_map) { 87 extensions::CommandMap* command_map) {
85 const ExtensionSet* extensions = 88 const ExtensionSet* extensions =
86 ExtensionSystem::Get(profile_)->extension_service()->extensions(); 89 ExtensionSystem::Get(profile_)->extension_service()->extensions();
87 const Extension* extension = extensions->GetByID(extension_id); 90 const Extension* extension = extensions->GetByID(extension_id);
88 CHECK(extension); 91 CHECK(extension);
89 92
90 command_map->clear(); 93 command_map->clear();
91 const extensions::CommandMap& commands = extension->named_commands(); 94 const extensions::CommandMap* commands =
92 if (commands.empty()) 95 CommandsInfo::GetNamedCommands(extension);
96 if (!commands)
93 return false; 97 return false;
94 98
95 extensions::CommandMap::const_iterator iter = commands.begin(); 99 extensions::CommandMap::const_iterator iter = commands->begin();
96 for (; iter != commands.end(); ++iter) { 100 for (; iter != commands->end(); ++iter) {
97 ui::Accelerator shortcut_assigned = 101 ui::Accelerator shortcut_assigned =
98 FindShortcutForCommand(extension_id, iter->second.command_name()); 102 FindShortcutForCommand(extension_id, iter->second.command_name());
99 103
100 if (type == ACTIVE_ONLY && shortcut_assigned.key_code() == ui::VKEY_UNKNOWN) 104 if (type == ACTIVE_ONLY && shortcut_assigned.key_code() == ui::VKEY_UNKNOWN)
101 continue; 105 continue;
102 106
103 extensions::Command command = iter->second; 107 extensions::Command command = iter->second;
104 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN) 108 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN)
105 command.set_accelerator(shortcut_assigned); 109 command.set_accelerator(shortcut_assigned);
106 110
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 if (StartsWithASCII(shortcut, Command::CommandPlatform() + ":", true)) 201 if (StartsWithASCII(shortcut, Command::CommandPlatform() + ":", true))
198 shortcut = shortcut.substr(Command::CommandPlatform().length() + 1); 202 shortcut = shortcut.substr(Command::CommandPlatform().length() + 1);
199 203
200 return Command::StringToAccelerator(shortcut); 204 return Command::StringToAccelerator(shortcut);
201 } 205 }
202 206
203 return ui::Accelerator(); 207 return ui::Accelerator();
204 } 208 }
205 209
206 void CommandService::AssignInitialKeybindings(const Extension* extension) { 210 void CommandService::AssignInitialKeybindings(const Extension* extension) {
207 const extensions::CommandMap& commands = extension->named_commands(); 211 const extensions::CommandMap* commands =
208 extensions::CommandMap::const_iterator iter = commands.begin(); 212 CommandsInfo::GetNamedCommands(extension);
209 for (; iter != commands.end(); ++iter) { 213 if (!commands)
214 return;
215
216 extensions::CommandMap::const_iterator iter = commands->begin();
217 for (; iter != commands->end(); ++iter) {
210 AddKeybindingPref(iter->second.accelerator(), 218 AddKeybindingPref(iter->second.accelerator(),
211 extension->id(), 219 extension->id(),
212 iter->second.command_name(), 220 iter->second.command_name(),
213 false); // Overwriting not allowed. 221 false); // Overwriting not allowed.
214 } 222 }
215 223
216 const extensions::Command* browser_action_command = 224 const extensions::Command* browser_action_command =
217 extension->browser_action_command(); 225 CommandsInfo::GetBrowserActionCommand(extension);
218 if (browser_action_command) { 226 if (browser_action_command) {
219 AddKeybindingPref(browser_action_command->accelerator(), 227 AddKeybindingPref(browser_action_command->accelerator(),
220 extension->id(), 228 extension->id(),
221 browser_action_command->command_name(), 229 browser_action_command->command_name(),
222 false); // Overwriting not allowed. 230 false); // Overwriting not allowed.
223 } 231 }
224 232
225 const extensions::Command* page_action_command = 233 const extensions::Command* page_action_command =
226 extension->page_action_command(); 234 CommandsInfo::GetPageActionCommand(extension);
227 if (page_action_command) { 235 if (page_action_command) {
228 AddKeybindingPref(page_action_command->accelerator(), 236 AddKeybindingPref(page_action_command->accelerator(),
229 extension->id(), 237 extension->id(),
230 page_action_command->command_name(), 238 page_action_command->command_name(),
231 false); // Overwriting not allowed. 239 false); // Overwriting not allowed.
232 } 240 }
233 241
234 const extensions::Command* script_badge_command = 242 const extensions::Command* script_badge_command =
235 extension->script_badge_command(); 243 CommandsInfo::GetScriptBadgeCommand(extension);
236 if (script_badge_command) { 244 if (script_badge_command) {
237 AddKeybindingPref(script_badge_command->accelerator(), 245 AddKeybindingPref(script_badge_command->accelerator(),
238 extension->id(), 246 extension->id(),
239 script_badge_command->command_name(), 247 script_badge_command->command_name(),
240 false); // Overwriting not allowed. 248 false); // Overwriting not allowed.
241 } 249 }
242 } 250 }
243 251
244 void CommandService::RemoveKeybindingPrefs(const std::string& extension_id, 252 void CommandService::RemoveKeybindingPrefs(const std::string& extension_id,
245 const std::string& command_name) { 253 const std::string& command_name) {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 const ExtensionSet* extensions = service->extensions(); 308 const ExtensionSet* extensions = service->extensions();
301 const Extension* extension = extensions->GetByID(extension_id); 309 const Extension* extension = extensions->GetByID(extension_id);
302 CHECK(extension); 310 CHECK(extension);
303 311
304 if (active) 312 if (active)
305 *active = false; 313 *active = false;
306 314
307 const extensions::Command* requested_command = NULL; 315 const extensions::Command* requested_command = NULL;
308 switch (action_type) { 316 switch (action_type) {
309 case BROWSER_ACTION: 317 case BROWSER_ACTION:
310 requested_command = extension->browser_action_command(); 318 requested_command = CommandsInfo::GetBrowserActionCommand(extension);
311 break; 319 break;
312 case PAGE_ACTION: 320 case PAGE_ACTION:
313 requested_command = extension->page_action_command(); 321 requested_command = CommandsInfo::GetPageActionCommand(extension);
314 break; 322 break;
315 case SCRIPT_BADGE: 323 case SCRIPT_BADGE:
316 requested_command = extension->script_badge_command(); 324 requested_command = CommandsInfo::GetScriptBadgeCommand(extension);
317 break; 325 break;
318 } 326 }
319 if (!requested_command) 327 if (!requested_command)
320 return false; 328 return false;
321 329
322 ui::Accelerator shortcut_assigned = 330 ui::Accelerator shortcut_assigned =
323 FindShortcutForCommand(extension_id, requested_command->command_name()); 331 FindShortcutForCommand(extension_id, requested_command->command_name());
324 332
325 if (active) 333 if (active)
326 *active = (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN); 334 *active = (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN);
327 335
328 if (query_type == ACTIVE_ONLY && 336 if (query_type == ACTIVE_ONLY &&
329 shortcut_assigned.key_code() == ui::VKEY_UNKNOWN) 337 shortcut_assigned.key_code() == ui::VKEY_UNKNOWN)
330 return false; 338 return false;
331 339
332 *command = *requested_command; 340 *command = *requested_command;
333 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN) 341 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN)
334 command->set_accelerator(shortcut_assigned); 342 command->set_accelerator(shortcut_assigned);
335 343
336 return true; 344 return true;
337 } 345 }
338 346
339 } // namespace extensions 347 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698