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

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 7 years, 11 months 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
« no previous file with comments | « no previous file | chrome/browser/extensions/extension_function_registry.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/lazy_instance.h" 7 #include "base/lazy_instance.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/extensions/api/commands/commands.h"
11 #include "chrome/browser/extensions/extension_function_registry.h"
10 #include "chrome/browser/extensions/extension_keybinding_registry.h" 12 #include "chrome/browser/extensions/extension_keybinding_registry.h"
11 #include "chrome/browser/extensions/extension_service.h" 13 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/extension_system.h" 14 #include "chrome/browser/extensions/extension_system.h"
13 #include "chrome/browser/prefs/scoped_user_pref_update.h" 15 #include "chrome/browser/prefs/scoped_user_pref_update.h"
14 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/chrome_notification_types.h" 17 #include "chrome/common/chrome_notification_types.h"
18 #include "chrome/common/extensions/api/commands/commands_handler.h"
16 #include "chrome/common/extensions/extension_manifest_constants.h" 19 #include "chrome/common/extensions/extension_manifest_constants.h"
17 #include "chrome/common/pref_names.h" 20 #include "chrome/common/pref_names.h"
18 #include "content/public/browser/notification_details.h" 21 #include "content/public/browser/notification_details.h"
19 #include "content/public/browser/notification_service.h" 22 #include "content/public/browser/notification_service.h"
20 23
21 using extensions::Extension; 24 using extensions::Extension;
22 25
23 namespace { 26 namespace {
24 27
25 const char kExtension[] = "extension"; 28 const char kExtension[] = "extension";
(...skipping 10 matching lines...) Expand all
36 namespace extensions { 39 namespace extensions {
37 40
38 // static 41 // static
39 void CommandService::RegisterUserPrefs(PrefServiceSyncable* user_prefs) { 42 void CommandService::RegisterUserPrefs(PrefServiceSyncable* user_prefs) {
40 user_prefs->RegisterDictionaryPref(prefs::kExtensionCommands, 43 user_prefs->RegisterDictionaryPref(prefs::kExtensionCommands,
41 PrefServiceSyncable::SYNCABLE_PREF); 44 PrefServiceSyncable::SYNCABLE_PREF);
42 } 45 }
43 46
44 CommandService::CommandService(Profile* profile) 47 CommandService::CommandService(Profile* profile)
45 : profile_(profile) { 48 : profile_(profile) {
49 ManifestHandler::Register(extension_manifest_keys::kCommands,
50 new CommandsHandler);
51
52 ExtensionFunctionRegistry::GetInstance()->
53 RegisterFunction<GetAllCommandsFunction>();
54
46 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, 55 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
47 content::Source<Profile>(profile)); 56 content::Source<Profile>(profile));
48 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, 57 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
49 content::Source<Profile>(profile)); 58 content::Source<Profile>(profile));
50 } 59 }
51 60
52 CommandService::~CommandService() { 61 CommandService::~CommandService() {
53 } 62 }
54 63
55 static base::LazyInstance<ProfileKeyedAPIFactory<CommandService> > 64 static base::LazyInstance<ProfileKeyedAPIFactory<CommandService> >
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 103
95 bool CommandService::GetNamedCommands(const std::string& extension_id, 104 bool CommandService::GetNamedCommands(const std::string& extension_id,
96 QueryType type, 105 QueryType type,
97 extensions::CommandMap* command_map) { 106 extensions::CommandMap* command_map) {
98 const ExtensionSet* extensions = 107 const ExtensionSet* extensions =
99 ExtensionSystem::Get(profile_)->extension_service()->extensions(); 108 ExtensionSystem::Get(profile_)->extension_service()->extensions();
100 const Extension* extension = extensions->GetByID(extension_id); 109 const Extension* extension = extensions->GetByID(extension_id);
101 CHECK(extension); 110 CHECK(extension);
102 111
103 command_map->clear(); 112 command_map->clear();
104 const extensions::CommandMap& commands = extension->named_commands(); 113 const extensions::CommandMap* commands =
105 if (commands.empty()) 114 CommandsInfo::GetNamedCommands(extension);
115 if (!commands)
106 return false; 116 return false;
107 117
108 extensions::CommandMap::const_iterator iter = commands.begin(); 118 extensions::CommandMap::const_iterator iter = commands->begin();
109 for (; iter != commands.end(); ++iter) { 119 for (; iter != commands->end(); ++iter) {
110 ui::Accelerator shortcut_assigned = 120 ui::Accelerator shortcut_assigned =
111 FindShortcutForCommand(extension_id, iter->second.command_name()); 121 FindShortcutForCommand(extension_id, iter->second.command_name());
112 122
113 if (type == ACTIVE_ONLY && shortcut_assigned.key_code() == ui::VKEY_UNKNOWN) 123 if (type == ACTIVE_ONLY && shortcut_assigned.key_code() == ui::VKEY_UNKNOWN)
114 continue; 124 continue;
115 125
116 extensions::Command command = iter->second; 126 extensions::Command command = iter->second;
117 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN) 127 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN)
118 command.set_accelerator(shortcut_assigned); 128 command.set_accelerator(shortcut_assigned);
119 129
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 if (StartsWithASCII(shortcut, Command::CommandPlatform() + ":", true)) 220 if (StartsWithASCII(shortcut, Command::CommandPlatform() + ":", true))
211 shortcut = shortcut.substr(Command::CommandPlatform().length() + 1); 221 shortcut = shortcut.substr(Command::CommandPlatform().length() + 1);
212 222
213 return Command::StringToAccelerator(shortcut); 223 return Command::StringToAccelerator(shortcut);
214 } 224 }
215 225
216 return ui::Accelerator(); 226 return ui::Accelerator();
217 } 227 }
218 228
219 void CommandService::AssignInitialKeybindings(const Extension* extension) { 229 void CommandService::AssignInitialKeybindings(const Extension* extension) {
220 const extensions::CommandMap& commands = extension->named_commands(); 230 const extensions::CommandMap* commands =
221 extensions::CommandMap::const_iterator iter = commands.begin(); 231 CommandsInfo::GetNamedCommands(extension);
222 for (; iter != commands.end(); ++iter) { 232 if (!commands)
233 return;
234
235 extensions::CommandMap::const_iterator iter = commands->begin();
236 for (; iter != commands->end(); ++iter) {
223 AddKeybindingPref(iter->second.accelerator(), 237 AddKeybindingPref(iter->second.accelerator(),
224 extension->id(), 238 extension->id(),
225 iter->second.command_name(), 239 iter->second.command_name(),
226 false); // Overwriting not allowed. 240 false); // Overwriting not allowed.
227 } 241 }
228 242
229 const extensions::Command* browser_action_command = 243 const extensions::Command* browser_action_command =
230 extension->browser_action_command(); 244 CommandsInfo::GetBrowserActionCommand(extension);
231 if (browser_action_command) { 245 if (browser_action_command) {
232 AddKeybindingPref(browser_action_command->accelerator(), 246 AddKeybindingPref(browser_action_command->accelerator(),
233 extension->id(), 247 extension->id(),
234 browser_action_command->command_name(), 248 browser_action_command->command_name(),
235 false); // Overwriting not allowed. 249 false); // Overwriting not allowed.
236 } 250 }
237 251
238 const extensions::Command* page_action_command = 252 const extensions::Command* page_action_command =
239 extension->page_action_command(); 253 CommandsInfo::GetPageActionCommand(extension);
240 if (page_action_command) { 254 if (page_action_command) {
241 AddKeybindingPref(page_action_command->accelerator(), 255 AddKeybindingPref(page_action_command->accelerator(),
242 extension->id(), 256 extension->id(),
243 page_action_command->command_name(), 257 page_action_command->command_name(),
244 false); // Overwriting not allowed. 258 false); // Overwriting not allowed.
245 } 259 }
246 260
247 const extensions::Command* script_badge_command = 261 const extensions::Command* script_badge_command =
248 extension->script_badge_command(); 262 CommandsInfo::GetScriptBadgeCommand(extension);
249 if (script_badge_command) { 263 if (script_badge_command) {
250 AddKeybindingPref(script_badge_command->accelerator(), 264 AddKeybindingPref(script_badge_command->accelerator(),
251 extension->id(), 265 extension->id(),
252 script_badge_command->command_name(), 266 script_badge_command->command_name(),
253 false); // Overwriting not allowed. 267 false); // Overwriting not allowed.
254 } 268 }
255 } 269 }
256 270
257 void CommandService::RemoveKeybindingPrefs(const std::string& extension_id, 271 void CommandService::RemoveKeybindingPrefs(const std::string& extension_id,
258 const std::string& command_name) { 272 const std::string& command_name) {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 const ExtensionSet* extensions = service->extensions(); 327 const ExtensionSet* extensions = service->extensions();
314 const Extension* extension = extensions->GetByID(extension_id); 328 const Extension* extension = extensions->GetByID(extension_id);
315 CHECK(extension); 329 CHECK(extension);
316 330
317 if (active) 331 if (active)
318 *active = false; 332 *active = false;
319 333
320 const extensions::Command* requested_command = NULL; 334 const extensions::Command* requested_command = NULL;
321 switch (action_type) { 335 switch (action_type) {
322 case BROWSER_ACTION: 336 case BROWSER_ACTION:
323 requested_command = extension->browser_action_command(); 337 requested_command = CommandsInfo::GetBrowserActionCommand(extension);
324 break; 338 break;
325 case PAGE_ACTION: 339 case PAGE_ACTION:
326 requested_command = extension->page_action_command(); 340 requested_command = CommandsInfo::GetPageActionCommand(extension);
327 break; 341 break;
328 case SCRIPT_BADGE: 342 case SCRIPT_BADGE:
329 requested_command = extension->script_badge_command(); 343 requested_command = CommandsInfo::GetScriptBadgeCommand(extension);
330 break; 344 break;
331 } 345 }
332 if (!requested_command) 346 if (!requested_command)
333 return false; 347 return false;
334 348
335 ui::Accelerator shortcut_assigned = 349 ui::Accelerator shortcut_assigned =
336 FindShortcutForCommand(extension_id, requested_command->command_name()); 350 FindShortcutForCommand(extension_id, requested_command->command_name());
337 351
338 if (active) 352 if (active)
339 *active = (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN); 353 *active = (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN);
340 354
341 if (query_type == ACTIVE_ONLY && 355 if (query_type == ACTIVE_ONLY &&
342 shortcut_assigned.key_code() == ui::VKEY_UNKNOWN) 356 shortcut_assigned.key_code() == ui::VKEY_UNKNOWN)
343 return false; 357 return false;
344 358
345 *command = *requested_command; 359 *command = *requested_command;
346 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN) 360 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN)
347 command->set_accelerator(shortcut_assigned); 361 command->set_accelerator(shortcut_assigned);
348 362
349 return true; 363 return true;
350 } 364 }
351 365
352 } // namespace extensions 366 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/extensions/extension_function_registry.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698