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

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

Issue 64273008: [Windows] Finish global and non-global media keys support on Windows. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address some comments. Created 7 years, 1 month 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 <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"
Finnur 2013/11/13 10:22:24 Can either of this include be deleted now?
zhchbin 2013/11/13 16:05:11 Done.
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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 // static 107 // static
104 ProfileKeyedAPIFactory<CommandService>* CommandService::GetFactoryInstance() { 108 ProfileKeyedAPIFactory<CommandService>* CommandService::GetFactoryInstance() {
105 return &g_factory.Get(); 109 return &g_factory.Get();
106 } 110 }
107 111
108 // static 112 // static
109 CommandService* CommandService::Get(Profile* profile) { 113 CommandService* CommandService::Get(Profile* profile) {
110 return ProfileKeyedAPIFactory<CommandService>::GetForProfile(profile); 114 return ProfileKeyedAPIFactory<CommandService>::GetForProfile(profile);
111 } 115 }
112 116
117 // static
118 bool CommandService::IsMediaKey(const ui::Accelerator& accelerator) {
119 if (accelerator.modifiers() != 0)
120 return false;
121
122 return (accelerator.key_code() == ui::VKEY_MEDIA_NEXT_TRACK ||
123 accelerator.key_code() == ui::VKEY_MEDIA_PREV_TRACK ||
124 accelerator.key_code() == ui::VKEY_MEDIA_PLAY_PAUSE ||
125 accelerator.key_code() == ui::VKEY_MEDIA_STOP);
126 }
127
113 bool CommandService::GetBrowserActionCommand( 128 bool CommandService::GetBrowserActionCommand(
114 const std::string& extension_id, 129 const std::string& extension_id,
115 QueryType type, 130 QueryType type,
116 extensions::Command* command, 131 extensions::Command* command,
117 bool* active) { 132 bool* active) {
118 return GetExtensionActionCommand( 133 return GetExtensionActionCommand(
119 extension_id, type, command, active, BROWSER_ACTION); 134 extension_id, type, command, active, BROWSER_ACTION);
120 } 135 }
121 136
122 bool CommandService::GetPageActionCommand( 137 bool CommandService::GetPageActionCommand(
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 bool allow_overrides, 201 bool allow_overrides,
187 bool global) { 202 bool global) {
188 if (accelerator.key_code() == ui::VKEY_UNKNOWN) 203 if (accelerator.key_code() == ui::VKEY_UNKNOWN)
189 return false; 204 return false;
190 205
191 DictionaryPrefUpdate updater(profile_->GetPrefs(), 206 DictionaryPrefUpdate updater(profile_->GetPrefs(),
192 prefs::kExtensionCommands); 207 prefs::kExtensionCommands);
193 base::DictionaryValue* bindings = updater.Get(); 208 base::DictionaryValue* bindings = updater.Get();
194 209
195 std::string key = GetPlatformKeybindingKeyForAccelerator(accelerator); 210 std::string key = GetPlatformKeybindingKeyForAccelerator(accelerator);
211 if (IsMediaKey(accelerator))
Finnur 2013/11/13 10:22:24 Please document why we do this. Perhaps something
zhchbin 2013/11/13 16:05:11 Done.
212 key += ":" + extension_id;
196 213
197 if (!allow_overrides && bindings->HasKey(key)) 214 if (!allow_overrides && bindings->HasKey(key))
198 return false; // Already taken. 215 return false; // Already taken.
199 216
200 base::DictionaryValue* keybinding = new base::DictionaryValue(); 217 base::DictionaryValue* keybinding = new base::DictionaryValue();
201 keybinding->SetString(kExtension, extension_id); 218 keybinding->SetString(kExtension, extension_id);
202 keybinding->SetString(kCommandName, command_name); 219 keybinding->SetString(kCommandName, command_name);
203 keybinding->SetBoolean(kGlobal, global); 220 keybinding->SetBoolean(kGlobal, global);
204 221
205 bindings->Set(key, keybinding); 222 bindings->Set(key, keybinding);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 continue; 297 continue;
281 std::string command_name; 298 std::string command_name;
282 item->GetString(kCommandName, &command_name); 299 item->GetString(kCommandName, &command_name);
283 if (command != command_name) 300 if (command != command_name)
284 continue; 301 continue;
285 bool global = false; 302 bool global = false;
286 if (FeatureSwitch::global_commands()->IsEnabled()) 303 if (FeatureSwitch::global_commands()->IsEnabled())
287 item->GetBoolean(kGlobal, &global); 304 item->GetBoolean(kGlobal, &global);
288 305
289 std::string shortcut = it.key(); 306 std::string shortcut = it.key();
290 if (StartsWithASCII(shortcut, Command::CommandPlatform() + ":", true)) 307 if (StartsWithASCII(shortcut, Command::CommandPlatform() + ":", true)) {
291 shortcut = shortcut.substr(Command::CommandPlatform().length() + 1); 308 std::vector<std::string> tokens;
309 base::SplitString(shortcut, ':', &tokens);
310 CHECK(tokens.size() >= 2);
311 shortcut = tokens[1]; // The second token is the shortcut assigned.
312 }
292 313
293 return Command(command_name, string16(), shortcut, global); 314 return Command(command_name, string16(), shortcut, global);
294 } 315 }
295 316
296 return Command(); 317 return Command();
297 } 318 }
298 319
299 void CommandService::AssignInitialKeybindings(const Extension* extension) { 320 void CommandService::AssignInitialKeybindings(const Extension* extension) {
300 const extensions::CommandMap* commands = 321 const extensions::CommandMap* commands =
301 CommandsInfo::GetNamedCommands(extension); 322 CommandsInfo::GetNamedCommands(extension);
302 if (!commands) 323 if (!commands)
303 return; 324 return;
304 325
305 ExtensionService* extension_service = 326 ExtensionService* extension_service =
306 ExtensionSystem::Get(profile_)->extension_service(); 327 ExtensionSystem::Get(profile_)->extension_service();
307 ExtensionPrefs* extension_prefs = extension_service->extension_prefs(); 328 ExtensionPrefs* extension_prefs = extension_service->extension_prefs();
308 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id())) 329 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id()))
309 return; 330 return;
310 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id()); 331 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id());
311 332
312 extensions::CommandMap::const_iterator iter = commands->begin(); 333 extensions::CommandMap::const_iterator iter = commands->begin();
313 for (; iter != commands->end(); ++iter) { 334 for (; iter != commands->end(); ++iter) {
314 if (!chrome::IsChromeAccelerator( 335 if ((!chrome::IsChromeAccelerator(iter->second.accelerator(), profile_) &&
315 iter->second.accelerator(), profile_) && 336 IsWhitelistedGlobalShortcut(iter->second)) ||
316 IsWhitelistedGlobalShortcut(iter->second)) { 337 extensions::CommandService::IsMediaKey(iter->second.accelerator())) {
317 AddKeybindingPref(iter->second.accelerator(), 338 AddKeybindingPref(iter->second.accelerator(),
318 extension->id(), 339 extension->id(),
319 iter->second.command_name(), 340 iter->second.command_name(),
320 false, // Overwriting not allowed. 341 false, // Overwriting not allowed.
321 iter->second.global()); 342 iter->second.global());
322 } 343 }
323 } 344 }
324 345
325 const extensions::Command* browser_action_command = 346 const extensions::Command* browser_action_command =
326 CommandsInfo::GetBrowserActionCommand(extension); 347 CommandsInfo::GetBrowserActionCommand(extension);
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
457 478
458 return true; 479 return true;
459 } 480 }
460 481
461 template <> 482 template <>
462 void ProfileKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() { 483 void ProfileKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() {
463 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance()); 484 DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance());
464 } 485 }
465 486
466 } // namespace extensions 487 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698