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
|
// 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 "base/utf_string_conversions.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/prefs/scoped_user_pref_update.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/notification_service.h"
using extensions::Extension;
namespace {
const char kExtension[] = "extension";
const char kCommandName[] = "command_name";
std::string GetPlatformKeybindingKeyForAccelerator(
const ui::Accelerator& accelerator) {
return extensions::Command::CommandPlatform() + ":" +
UTF16ToUTF8(accelerator.GetShortcutText());
}
} // namespace
namespace extensions {
// static
void CommandService::RegisterUserPrefs(
PrefService* user_prefs) {
user_prefs->RegisterDictionaryPref(prefs::kExtensionKeybindings,
PrefService::SYNCABLE_PREF);
}
CommandService::CommandService(Profile* profile)
: profile_(profile) {
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
content::Source<Profile>(profile));
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
content::Source<Profile>(profile));
}
CommandService::~CommandService() {
}
const extensions::Command* CommandService::GetBrowserActionCommand(
const std::string& extension_id, QueryType type) {
const ExtensionSet* extensions =
ExtensionSystem::Get(profile_)->extension_service()->extensions();
const Extension* extension = extensions->GetByID(extension_id);
CHECK(extension);
const extensions::Command* command = extension->browser_action_command();
if (!command)
return NULL;
if (type == ACTIVE_ONLY &&
!IsKeybindingActive(command->accelerator(),
extension_id,
command->command_name())) {
return NULL;
}
return command;
}
const extensions::Command* CommandService::GetPageActionCommand(
const std::string& extension_id, QueryType type) {
const ExtensionSet* extensions =
ExtensionSystem::Get(profile_)->extension_service()->extensions();
const Extension* extension = extensions->GetByID(extension_id);
CHECK(extension);
const extensions::Command* command = extension->page_action_command();
if (!command)
return NULL;
if (type == ACTIVE_ONLY &&
!IsKeybindingActive(command->accelerator(),
extension_id,
command->command_name())) {
return NULL;
}
return command;
}
extensions::CommandMap CommandService::GetNamedCommands(
const std::string& extension_id, QueryType type) {
const ExtensionSet* extensions =
ExtensionSystem::Get(profile_)->extension_service()->extensions();
const Extension* extension = extensions->GetByID(extension_id);
CHECK(extension);
extensions::CommandMap result;
const extensions::CommandMap& commands = extension->named_commands();
if (commands.empty())
return result;
extensions::CommandMap::const_iterator iter = commands.begin();
for (; iter != commands.end(); ++iter) {
if (type == ACTIVE_ONLY &&
!IsKeybindingActive(iter->second.accelerator(),
extension_id,
iter->second.command_name())) {
continue;
}
result[iter->second.command_name()] = iter->second;
}
return result;
}
bool CommandService::IsKeybindingActive(
const ui::Accelerator& accelerator,
const std::string& extension_id,
const std::string& command_name) const {
CHECK(!extension_id.empty());
CHECK(!command_name.empty());
std::string key = GetPlatformKeybindingKeyForAccelerator(accelerator);
const DictionaryValue* bindings =
profile_->GetPrefs()->GetDictionary(prefs::kExtensionKeybindings);
if (!bindings->HasKey(key))
return false;
DictionaryValue* value = NULL;
if (!bindings->GetDictionary(key, &value))
return false;
std::string id;
if (!value->GetString(kExtension, &id) || id != extension_id)
return false; // Not active for this extension.
std::string command;
if (!value->GetString(kCommandName, &command) || command != command_name)
return false; // Not active for this command.
return true; // We found a match, this one is active.
}
bool CommandService::AddKeybindingPref(
const ui::Accelerator& accelerator,
std::string extension_id,
std::string command_name,
bool allow_overrides) {
DictionaryPrefUpdate updater(profile_->GetPrefs(),
prefs::kExtensionKeybindings);
DictionaryValue* bindings = updater.Get();
std::string key = GetPlatformKeybindingKeyForAccelerator(accelerator);
if (bindings->HasKey(key) && !allow_overrides)
return false; // Already taken.
DictionaryValue* keybinding = new DictionaryValue();
keybinding->SetString(kExtension, extension_id);
keybinding->SetString(kCommandName, command_name);
bindings->Set(key, keybinding);
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 Extension>(details).ptr());
break;
case chrome::NOTIFICATION_EXTENSION_UNINSTALLED:
RemoveKeybindingPrefs(*content::Details<std::string>(details).ptr());
break;
default:
NOTREACHED();
break;
}
}
void CommandService::AssignInitialKeybindings(const Extension* extension) {
const extensions::CommandMap& commands = extension->named_commands();
extensions::CommandMap::const_iterator iter = commands.begin();
for (; iter != commands.end(); ++iter) {
AddKeybindingPref(iter->second.accelerator(),
extension->id(),
iter->second.command_name(),
false); // Overwriting not allowed.
}
const extensions::Command* browser_action_command =
extension->browser_action_command();
if (browser_action_command) {
AddKeybindingPref(browser_action_command->accelerator(),
extension->id(),
browser_action_command->command_name(),
false); // Overwriting not allowed.
}
const extensions::Command* page_action_command =
extension->page_action_command();
if (page_action_command) {
AddKeybindingPref(page_action_command->accelerator(),
extension->id(),
page_action_command->command_name(),
false); // Overwriting not allowed.
}
}
void CommandService::RemoveKeybindingPrefs(std::string extension_id) {
DictionaryPrefUpdate updater(profile_->GetPrefs(),
prefs::kExtensionKeybindings);
DictionaryValue* bindings = updater.Get();
typedef std::vector<std::string> KeysToRemove;
KeysToRemove keys_to_remove;
for (DictionaryValue::key_iterator it = bindings->begin_keys();
it != bindings->end_keys(); ++it) {
std::string key = *it;
DictionaryValue* item = NULL;
bindings->GetDictionary(key, &item);
std::string extension;
item->GetString(kExtension, &extension);
if (extension == extension_id)
keys_to_remove.push_back(key);
}
for (KeysToRemove::const_iterator it = keys_to_remove.begin();
it != keys_to_remove.end(); ++it) {
bindings->Remove(*it, NULL);
}
}
} // namespace extensions
|