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 <vector> | |
8 | |
7 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
8 #include "base/prefs/scoped_user_pref_update.h" | 10 #include "base/prefs/scoped_user_pref_update.h" |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "base/strings/string_split.h" | |
9 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
12 #include "chrome/browser/extensions/api/commands/commands.h" | 16 #include "chrome/browser/extensions/api/commands/commands.h" |
13 #include "chrome/browser/extensions/extension_commands_global_registry.h" | 17 #include "chrome/browser/extensions/extension_commands_global_registry.h" |
14 #include "chrome/browser/extensions/extension_function_registry.h" | 18 #include "chrome/browser/extensions/extension_function_registry.h" |
15 #include "chrome/browser/extensions/extension_keybinding_registry.h" | 19 #include "chrome/browser/extensions/extension_keybinding_registry.h" |
16 #include "chrome/browser/extensions/extension_service.h" | 20 #include "chrome/browser/extensions/extension_service.h" |
17 #include "chrome/browser/extensions/extension_system.h" | 21 #include "chrome/browser/extensions/extension_system.h" |
18 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 | 81 |
78 // static | 82 // static |
79 void CommandService::RegisterProfilePrefs( | 83 void CommandService::RegisterProfilePrefs( |
80 user_prefs::PrefRegistrySyncable* registry) { | 84 user_prefs::PrefRegistrySyncable* registry) { |
81 registry->RegisterDictionaryPref( | 85 registry->RegisterDictionaryPref( |
82 prefs::kExtensionCommands, | 86 prefs::kExtensionCommands, |
83 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 87 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
84 } | 88 } |
85 | 89 |
86 CommandService::CommandService(Profile* profile) | 90 CommandService::CommandService(Profile* profile) |
87 : profile_(profile) { | 91 : media_keys_command_counter_(0), |
92 profile_(profile) { | |
88 ExtensionFunctionRegistry::GetInstance()-> | 93 ExtensionFunctionRegistry::GetInstance()-> |
89 RegisterFunction<GetAllCommandsFunction>(); | 94 RegisterFunction<GetAllCommandsFunction>(); |
90 | 95 |
91 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, | 96 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, |
92 content::Source<Profile>(profile)); | 97 content::Source<Profile>(profile)); |
93 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | 98 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
94 content::Source<Profile>(profile)); | 99 content::Source<Profile>(profile)); |
95 } | 100 } |
96 | 101 |
97 CommandService::~CommandService() { | 102 CommandService::~CommandService() { |
98 } | 103 } |
99 | 104 |
100 static base::LazyInstance<ProfileKeyedAPIFactory<CommandService> > | 105 static base::LazyInstance<ProfileKeyedAPIFactory<CommandService> > |
101 g_factory = LAZY_INSTANCE_INITIALIZER; | 106 g_factory = LAZY_INSTANCE_INITIALIZER; |
102 | 107 |
103 // static | 108 // static |
104 ProfileKeyedAPIFactory<CommandService>* CommandService::GetFactoryInstance() { | 109 ProfileKeyedAPIFactory<CommandService>* CommandService::GetFactoryInstance() { |
105 return &g_factory.Get(); | 110 return &g_factory.Get(); |
106 } | 111 } |
107 | 112 |
108 // static | 113 // static |
109 CommandService* CommandService::Get(Profile* profile) { | 114 CommandService* CommandService::Get(Profile* profile) { |
110 return ProfileKeyedAPIFactory<CommandService>::GetForProfile(profile); | 115 return ProfileKeyedAPIFactory<CommandService>::GetForProfile(profile); |
111 } | 116 } |
112 | 117 |
118 // staitc | |
Finnur
2013/11/12 16:27:54
s/staitc/static/
zhchbin
2013/11/13 05:01:57
Done.
| |
119 bool CommandService::IsMediaKey(const ui::Accelerator& accelerator) { | |
120 if (accelerator.modifiers() != 0) | |
121 return false; | |
122 | |
123 return (accelerator.key_code() == ui::VKEY_MEDIA_NEXT_TRACK || | |
124 accelerator.key_code() == ui::VKEY_MEDIA_PREV_TRACK || | |
125 accelerator.key_code() == ui::VKEY_MEDIA_PLAY_PAUSE || | |
126 accelerator.key_code() == ui::VKEY_MEDIA_STOP); | |
127 } | |
128 | |
113 bool CommandService::GetBrowserActionCommand( | 129 bool CommandService::GetBrowserActionCommand( |
114 const std::string& extension_id, | 130 const std::string& extension_id, |
115 QueryType type, | 131 QueryType type, |
116 extensions::Command* command, | 132 extensions::Command* command, |
117 bool* active) { | 133 bool* active) { |
118 return GetExtensionActionCommand( | 134 return GetExtensionActionCommand( |
119 extension_id, type, command, active, BROWSER_ACTION); | 135 extension_id, type, command, active, BROWSER_ACTION); |
120 } | 136 } |
121 | 137 |
122 bool CommandService::GetPageActionCommand( | 138 bool CommandService::GetPageActionCommand( |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
186 bool allow_overrides, | 202 bool allow_overrides, |
187 bool global) { | 203 bool global) { |
188 if (accelerator.key_code() == ui::VKEY_UNKNOWN) | 204 if (accelerator.key_code() == ui::VKEY_UNKNOWN) |
189 return false; | 205 return false; |
190 | 206 |
191 DictionaryPrefUpdate updater(profile_->GetPrefs(), | 207 DictionaryPrefUpdate updater(profile_->GetPrefs(), |
192 prefs::kExtensionCommands); | 208 prefs::kExtensionCommands); |
193 base::DictionaryValue* bindings = updater.Get(); | 209 base::DictionaryValue* bindings = updater.Get(); |
194 | 210 |
195 std::string key = GetPlatformKeybindingKeyForAccelerator(accelerator); | 211 std::string key = GetPlatformKeybindingKeyForAccelerator(accelerator); |
212 if (IsMediaKey(accelerator)) | |
Finnur
2013/11/12 16:27:54
What happens if I assign a Media key to a browser
zhchbin
2013/11/13 05:01:57
1. How can it happen? We only parse Media Keys for
Finnur
2013/11/13 10:22:24
That's fine. Perhaps just add a DCHECK inside the
zhchbin
2013/11/13 16:05:10
Done.
| |
213 key += (":" + base::IntToString(media_keys_command_counter_++)); | |
Finnur
2013/11/12 16:27:54
nit: Do you need the parens around the whole expre
zhchbin
2013/11/13 05:01:57
Done.
| |
196 | 214 |
197 if (!allow_overrides && bindings->HasKey(key)) | 215 if (!allow_overrides && bindings->HasKey(key)) |
198 return false; // Already taken. | 216 return false; // Already taken. |
199 | 217 |
200 base::DictionaryValue* keybinding = new base::DictionaryValue(); | 218 base::DictionaryValue* keybinding = new base::DictionaryValue(); |
201 keybinding->SetString(kExtension, extension_id); | 219 keybinding->SetString(kExtension, extension_id); |
202 keybinding->SetString(kCommandName, command_name); | 220 keybinding->SetString(kCommandName, command_name); |
203 keybinding->SetBoolean(kGlobal, global); | 221 keybinding->SetBoolean(kGlobal, global); |
204 | 222 |
205 bindings->Set(key, keybinding); | 223 bindings->Set(key, keybinding); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
280 continue; | 298 continue; |
281 std::string command_name; | 299 std::string command_name; |
282 item->GetString(kCommandName, &command_name); | 300 item->GetString(kCommandName, &command_name); |
283 if (command != command_name) | 301 if (command != command_name) |
284 continue; | 302 continue; |
285 bool global = false; | 303 bool global = false; |
286 if (FeatureSwitch::global_commands()->IsEnabled()) | 304 if (FeatureSwitch::global_commands()->IsEnabled()) |
287 item->GetBoolean(kGlobal, &global); | 305 item->GetBoolean(kGlobal, &global); |
288 | 306 |
289 std::string shortcut = it.key(); | 307 std::string shortcut = it.key(); |
290 if (StartsWithASCII(shortcut, Command::CommandPlatform() + ":", true)) | 308 if (StartsWithASCII(shortcut, Command::CommandPlatform() + ":", true)) { |
291 shortcut = shortcut.substr(Command::CommandPlatform().length() + 1); | 309 std::vector<std::string> tokens; |
310 base::SplitString(shortcut, ':', &tokens); | |
311 CHECK(tokens.size() >= 2); | |
312 shortcut = tokens[1]; // The second token is the shortcut that assigned. | |
Finnur
2013/11/12 16:27:54
nit: Drop the word 'that'
zhchbin
2013/11/13 05:01:57
Done.
| |
313 } | |
292 | 314 |
293 return Command(command_name, string16(), shortcut, global); | 315 return Command(command_name, string16(), shortcut, global); |
294 } | 316 } |
295 | 317 |
296 return Command(); | 318 return Command(); |
297 } | 319 } |
298 | 320 |
299 void CommandService::AssignInitialKeybindings(const Extension* extension) { | 321 void CommandService::AssignInitialKeybindings(const Extension* extension) { |
300 const extensions::CommandMap* commands = | 322 const extensions::CommandMap* commands = |
301 CommandsInfo::GetNamedCommands(extension); | 323 CommandsInfo::GetNamedCommands(extension); |
302 if (!commands) | 324 if (!commands) |
303 return; | 325 return; |
304 | 326 |
305 ExtensionService* extension_service = | 327 ExtensionService* extension_service = |
306 ExtensionSystem::Get(profile_)->extension_service(); | 328 ExtensionSystem::Get(profile_)->extension_service(); |
307 ExtensionPrefs* extension_prefs = extension_service->extension_prefs(); | 329 ExtensionPrefs* extension_prefs = extension_service->extension_prefs(); |
308 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id())) | 330 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id())) |
309 return; | 331 return; |
310 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id()); | 332 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id()); |
311 | 333 |
312 extensions::CommandMap::const_iterator iter = commands->begin(); | 334 extensions::CommandMap::const_iterator iter = commands->begin(); |
313 for (; iter != commands->end(); ++iter) { | 335 for (; iter != commands->end(); ++iter) { |
314 if (!chrome::IsChromeAccelerator( | 336 if ((!chrome::IsChromeAccelerator(iter->second.accelerator(), profile_) && |
315 iter->second.accelerator(), profile_) && | 337 IsWhitelistedGlobalShortcut(iter->second)) || |
316 IsWhitelistedGlobalShortcut(iter->second)) { | 338 extensions::CommandService::IsMediaKey(iter->second.accelerator())) { |
Finnur
2013/11/12 16:27:54
I think we always want the ChromeAccelerator check
zhchbin
2013/11/13 05:01:57
Not. !chrome::IsChromeAccelerator will be false fo
Finnur
2013/11/13 10:22:24
So, I would actually expect IsChromeAccelerator to
zhchbin
2013/11/13 16:05:10
Do you mean remove media keys in the reserved Chro
Finnur
2013/11/18 11:16:14
Hmm... Are they already reserved? OK, then that wo
zhchbin
2013/11/18 13:10:13
Yes, you can find the reserved shortcuts on aura h
| |
317 AddKeybindingPref(iter->second.accelerator(), | 339 AddKeybindingPref(iter->second.accelerator(), |
318 extension->id(), | 340 extension->id(), |
319 iter->second.command_name(), | 341 iter->second.command_name(), |
320 false, // Overwriting not allowed. | 342 false, // Overwriting not allowed. |
321 iter->second.global()); | 343 iter->second.global()); |
322 } | 344 } |
323 } | 345 } |
324 | 346 |
325 const extensions::Command* browser_action_command = | 347 const extensions::Command* browser_action_command = |
326 CommandsInfo::GetBrowserActionCommand(extension); | 348 CommandsInfo::GetBrowserActionCommand(extension); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
457 | 479 |
458 return true; | 480 return true; |
459 } | 481 } |
460 | 482 |
461 template <> | 483 template <> |
462 void ProfileKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() { | 484 void ProfileKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() { |
463 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance()); | 485 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance()); |
464 } | 486 } |
465 | 487 |
466 } // namespace extensions | 488 } // namespace extensions |
OLD | NEW |