1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/api/commands/command_service.h"
#include <vector>
#include "base/lazy_instance.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/api/commands/commands.h"
#include "chrome/browser/extensions/extension_commands_global_registry.h"
#include "chrome/browser/extensions/extension_function_registry.h"
#include "chrome/browser/extensions/extension_keybinding_registry.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/accelerator_utils.h"
#include "chrome/common/extensions/api/commands/commands_handler.h"
#include "chrome/common/pref_names.h"
#include "components/user_prefs/pref_registry_syncable.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "extensions/common/feature_switch.h"
#include "extensions/common/manifest_constants.h"
using extensions::Extension;
using extensions::ExtensionPrefs;
namespace {
const char kExtension[] = "extension";
const char kCommandName[] = "command_name";
const char kGlobal[] = "global";
// A preference that indicates that the initial keybindings for the given
// extension have been set.
const char kInitialBindingsHaveBeenAssigned[] = "initial_keybindings_set";
std::string GetPlatformKeybindingKeyForAccelerator(
const ui::Accelerator& accelerator, const std::string extension_id) {
std::string key = extensions::Command::CommandPlatform() + ":" +
extensions::Command::AcceleratorToString(accelerator);
// Media keys have a 1-to-many relationship with targets, unlike regular
// shortcut (1-to-1 relationship). That means two or more extensions can
// register for the same media key so the extension ID needs to be added to
// the key to make sure the key is unique.
if (extensions::CommandService::IsMediaKey(accelerator))
key += ":" + extension_id;
return key;
}
bool IsForCurrentPlatform(const std::string& key) {
return StartsWithASCII(
key, extensions::Command::CommandPlatform() + ":", true);
}
void SetInitialBindingsHaveBeenAssigned(
ExtensionPrefs* prefs, const std::string& extension_id) {
prefs->UpdateExtensionPref(extension_id, kInitialBindingsHaveBeenAssigned,
new base::FundamentalValue(true));
}
bool InitialBindingsHaveBeenAssigned(
const ExtensionPrefs* prefs, const std::string& extension_id) {
bool assigned = false;
if (!prefs || !prefs->ReadPrefAsBoolean(extension_id,
kInitialBindingsHaveBeenAssigned,
&assigned))
return false;
return assigned;
}
bool IsWhitelistedGlobalShortcut(const extensions::Command& command) {
// Non-global shortcuts are always allowed.
if (!command.global())
return true;
// Global shortcuts must be (Ctrl|Command)-Shift-[0-9].
#if defined OS_MACOSX
if (!command.accelerator().IsCmdDown())
return false;
#else
if (!command.accelerator().IsCtrlDown())
return false;
#endif
if (!command.accelerator().IsShiftDown())
return false;
return (command.accelerator().key_code() >= ui::VKEY_0 &&
command.accelerator().key_code() <= ui::VKEY_9);
}
} // namespace
namespace extensions {
// static
void CommandService::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterDictionaryPref(
prefs::kExtensionCommands,
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
}
CommandService::CommandService(Profile* profile)
: profile_(profile) {
ExtensionFunctionRegistry::GetInstance()->
RegisterFunction<GetAllCommandsFunction>();
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
content::Source<Profile>(profile));
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
content::Source<Profile>(profile));
}
CommandService::~CommandService() {
}
static base::LazyInstance<ProfileKeyedAPIFactory<CommandService> >
g_factory = LAZY_INSTANCE_INITIALIZER;
// static
ProfileKeyedAPIFactory<CommandService>* CommandService::GetFactoryInstance() {
return g_factory.Pointer();
}
// static
CommandService* CommandService::Get(Profile* profile) {
return ProfileKeyedAPIFactory<CommandService>::GetForProfile(profile);
}
// static
bool CommandService::IsMediaKey(const ui::Accelerator& accelerator) {
if (accelerator.modifiers() != 0)
return false;
return (accelerator.key_code() == ui::VKEY_MEDIA_NEXT_TRACK ||
accelerator.key_code() == ui::VKEY_MEDIA_PREV_TRACK ||
accelerator.key_code() == ui::VKEY_MEDIA_PLAY_PAUSE ||
accelerator.key_code() == ui::VKEY_MEDIA_STOP);
}
bool CommandService::GetBrowserActionCommand(
const std::string& extension_id,
QueryType type,
extensions::Command* command,
bool* active) {
return GetExtensionActionCommand(
extension_id, type, command, active, BROWSER_ACTION);
}
bool CommandService::GetPageActionCommand(
const std::string& extension_id,
QueryType type,
extensions::Command* command,
bool* active) {
return GetExtensionActionCommand(
extension_id, type, command, active, PAGE_ACTION);
}
bool CommandService::GetNamedCommands(const std::string& extension_id,
QueryType type,
CommandScope scope,
extensions::CommandMap* command_map) {
ExtensionService* extension_service =
ExtensionSystem::Get(profile_)->extension_service();
if (!extension_service)
return false; // Can occur during testing.
const ExtensionSet* extensions = extension_service->extensions();
const Extension* extension = extensions->GetByID(extension_id);
CHECK(extension);
command_map->clear();
const extensions::CommandMap* commands =
CommandsInfo::GetNamedCommands(extension);
if (!commands)
return false;
extensions::CommandMap::const_iterator iter = commands->begin();
for (; iter != commands->end(); ++iter) {
// Look up to see if the user has overridden how the command should work.
extensions::Command saved_command =
FindCommandByName(extension_id, iter->second.command_name());
ui::Accelerator shortcut_assigned = saved_command.accelerator();
if (type == ACTIVE_ONLY && shortcut_assigned.key_code() == ui::VKEY_UNKNOWN)
continue;
extensions::Command command = iter->second;
if (scope != ANY_SCOPE && ((scope == GLOBAL) != saved_command.global()))
continue;
if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN)
command.set_accelerator(shortcut_assigned);
command.set_global(saved_command.global());
(*command_map)[iter->second.command_name()] = command;
}
return true;
}
bool CommandService::AddKeybindingPref(
const ui::Accelerator& accelerator,
std::string extension_id,
std::string command_name,
bool allow_overrides,
bool global) {
if (accelerator.key_code() == ui::VKEY_UNKNOWN)
return false;
// Media Keys are allowed to be used by named command only.
DCHECK(!IsMediaKey(accelerator) ||
(command_name != manifest_values::kPageActionCommandEvent &&
command_name != manifest_values::kBrowserActionCommandEvent));
DictionaryPrefUpdate updater(profile_->GetPrefs(),
prefs::kExtensionCommands);
base::DictionaryValue* bindings = updater.Get();
std::string key = GetPlatformKeybindingKeyForAccelerator(accelerator,
extension_id);
if (bindings->HasKey(key)) {
if (!allow_overrides)
return false; // Already taken.
// If the shortcut has been assigned to another command, it should be
// removed before overriding, so that |ExtensionKeybindingRegistry| can get
// a chance to do clean-up.
const base::DictionaryValue* item = NULL;
bindings->GetDictionary(key, &item);
std::string old_extension_id;
std::string old_command_name;
item->GetString(kExtension, &old_extension_id);
item->GetString(kCommandName, &old_command_name);
RemoveKeybindingPrefs(old_extension_id, old_command_name);
}
base::DictionaryValue* keybinding = new base::DictionaryValue();
keybinding->SetString(kExtension, extension_id);
keybinding->SetString(kCommandName, command_name);
keybinding->SetBoolean(kGlobal, global);
bindings->Set(key, keybinding);
std::pair<const std::string, const std::string> details =
std::make_pair(extension_id, command_name);
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED,
content::Source<Profile>(profile_),
content::Details<
std::pair<const std::string, const std::string> >(&details));
return true;
}
void CommandService::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_EXTENSION_INSTALLED:
AssignInitialKeybindings(
content::Details<const InstalledExtensionInfo>(details)->extension);
break;
case chrome::NOTIFICATION_EXTENSION_UNINSTALLED:
RemoveKeybindingPrefs(
content::Details<const Extension>(details)->id(),
std::string());
break;
default:
NOTREACHED();
break;
}
}
void CommandService::UpdateKeybindingPrefs(const std::string& extension_id,
const std::string& command_name,
const std::string& keystroke) {
extensions::Command command = FindCommandByName(extension_id, command_name);
// The extension command might be assigned another shortcut. Remove that
// shortcut before proceeding.
RemoveKeybindingPrefs(extension_id, command_name);
ui::Accelerator accelerator =
Command::StringToAccelerator(keystroke, command_name);
AddKeybindingPref(accelerator, extension_id, command_name,
true, command.global());
}
bool CommandService::SetScope(const std::string& extension_id,
const std::string& command_name,
bool global) {
extensions::Command command = FindCommandByName(extension_id, command_name);
if (global == command.global())
return false;
// Pre-existing shortcuts must be removed before proceeding because the
// handlers for global and non-global extensions are not one and the same.
RemoveKeybindingPrefs(extension_id, command_name);
AddKeybindingPref(command.accelerator(), extension_id,
command_name, true, global);
return true;
}
Command CommandService::FindCommandByName(
const std::string& extension_id, const std::string& command) {
const base::DictionaryValue* bindings =
profile_->GetPrefs()->GetDictionary(prefs::kExtensionCommands);
for (base::DictionaryValue::Iterator it(*bindings); !it.IsAtEnd();
it.Advance()) {
const base::DictionaryValue* item = NULL;
it.value().GetAsDictionary(&item);
std::string extension;
item->GetString(kExtension, &extension);
if (extension != extension_id)
continue;
std::string command_name;
item->GetString(kCommandName, &command_name);
if (command != command_name)
continue;
// Format stored in Preferences is: "Platform:Shortcut[:ExtensionId]".
std::string shortcut = it.key();
if (!IsForCurrentPlatform(shortcut))
continue;
bool global = false;
if (FeatureSwitch::global_commands()->IsEnabled())
item->GetBoolean(kGlobal, &global);
std::vector<std::string> tokens;
base::SplitString(shortcut, ':', &tokens);
CHECK(tokens.size() >= 2);
shortcut = tokens[1];
return Command(command_name, base::string16(), shortcut, global);
}
return Command();
}
void CommandService::AssignInitialKeybindings(const Extension* extension) {
const extensions::CommandMap* commands =
CommandsInfo::GetNamedCommands(extension);
if (!commands)
return;
ExtensionService* extension_service =
ExtensionSystem::Get(profile_)->extension_service();
ExtensionPrefs* extension_prefs = extension_service->extension_prefs();
if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id()))
return;
SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id());
extensions::CommandMap::const_iterator iter = commands->begin();
for (; iter != commands->end(); ++iter) {
// Make sure registered Chrome shortcuts cannot be automatically assigned
// (overwritten) by extension developers. Media keys are an exception here.
if ((!chrome::IsChromeAccelerator(iter->second.accelerator(), profile_) &&
IsWhitelistedGlobalShortcut(iter->second)) ||
extensions::CommandService::IsMediaKey(iter->second.accelerator())) {
AddKeybindingPref(iter->second.accelerator(),
extension->id(),
iter->second.command_name(),
false, // Overwriting not allowed.
iter->second.global());
}
}
const extensions::Command* browser_action_command =
CommandsInfo::GetBrowserActionCommand(extension);
if (browser_action_command) {
if (!chrome::IsChromeAccelerator(
browser_action_command->accelerator(), profile_)) {
AddKeybindingPref(browser_action_command->accelerator(),
extension->id(),
browser_action_command->command_name(),
false, // Overwriting not allowed.
false); // Browser actions can't be global.
}
}
const extensions::Command* page_action_command =
CommandsInfo::GetPageActionCommand(extension);
if (page_action_command) {
if (!chrome::IsChromeAccelerator(
page_action_command->accelerator(), profile_)) {
AddKeybindingPref(page_action_command->accelerator(),
extension->id(),
page_action_command->command_name(),
false, // Overwriting not allowed.
false); // Page actions can't be global.
}
}
}
void CommandService::RemoveKeybindingPrefs(const std::string& extension_id,
const std::string& command_name) {
DictionaryPrefUpdate updater(profile_->GetPrefs(),
prefs::kExtensionCommands);
base::DictionaryValue* bindings = updater.Get();
typedef std::vector<std::string> KeysToRemove;
KeysToRemove keys_to_remove;
for (base::DictionaryValue::Iterator it(*bindings); !it.IsAtEnd();
it.Advance()) {
// Removal of keybinding preference should be limited to current platform.
if (!IsForCurrentPlatform(it.key()))
continue;
const base::DictionaryValue* item = NULL;
it.value().GetAsDictionary(&item);
std::string extension;
item->GetString(kExtension, &extension);
if (extension == extension_id) {
// If |command_name| is specified, delete only that command. Otherwise,
// delete all commands.
if (!command_name.empty()) {
std::string command;
item->GetString(kCommandName, &command);
if (command_name != command)
continue;
}
keys_to_remove.push_back(it.key());
}
}
for (KeysToRemove::const_iterator it = keys_to_remove.begin();
it != keys_to_remove.end(); ++it) {
std::string key = *it;
bindings->Remove(key, NULL);
std::pair<const std::string, const std::string> details =
std::make_pair(extension_id, command_name);
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED,
content::Source<Profile>(profile_),
content::Details<
std::pair<const std::string, const std::string> >(&details));
}
}
bool CommandService::GetExtensionActionCommand(
const std::string& extension_id,
QueryType query_type,
extensions::Command* command,
bool* active,
ExtensionActionType action_type) {
ExtensionService* service =
ExtensionSystem::Get(profile_)->extension_service();
if (!service)
return false; // Can happen in tests.
const ExtensionSet* extensions = service->extensions();
const Extension* extension = extensions->GetByID(extension_id);
CHECK(extension);
if (active)
*active = false;
const extensions::Command* requested_command = NULL;
switch (action_type) {
case BROWSER_ACTION:
requested_command = CommandsInfo::GetBrowserActionCommand(extension);
break;
case PAGE_ACTION:
requested_command = CommandsInfo::GetPageActionCommand(extension);
break;
}
if (!requested_command)
return false;
// Look up to see if the user has overridden how the command should work.
extensions::Command saved_command =
FindCommandByName(extension_id, requested_command->command_name());
ui::Accelerator shortcut_assigned = saved_command.accelerator();
if (active)
*active = (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN);
if (query_type == ACTIVE_ONLY &&
shortcut_assigned.key_code() == ui::VKEY_UNKNOWN)
return false;
*command = *requested_command;
if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN)
command->set_accelerator(shortcut_assigned);
return true;
}
template <>
void ProfileKeyedAPIFactory<CommandService>::DeclareFactoryDependencies() {
DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance());
}
} // namespace extensions
|