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

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

Issue 14990002: Make sure keybindings removed don't come back during extension update. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Moving API calls out of ExtensionPrefs class Created 7 years, 7 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 | Annotate | Revision Log
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" 10 #include "chrome/browser/extensions/api/commands/commands.h"
11 #include "chrome/browser/extensions/extension_function_registry.h" 11 #include "chrome/browser/extensions/extension_function_registry.h"
12 #include "chrome/browser/extensions/extension_keybinding_registry.h" 12 #include "chrome/browser/extensions/extension_keybinding_registry.h"
13 #include "chrome/browser/extensions/extension_service.h" 13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/extensions/extension_system.h" 14 #include "chrome/browser/extensions/extension_system.h"
15 #include "chrome/browser/prefs/scoped_user_pref_update.h" 15 #include "chrome/browser/prefs/scoped_user_pref_update.h"
16 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/accelerator_utils.h" 17 #include "chrome/browser/ui/accelerator_utils.h"
18 #include "chrome/common/chrome_notification_types.h" 18 #include "chrome/common/chrome_notification_types.h"
19 #include "chrome/common/extensions/api/commands/commands_handler.h" 19 #include "chrome/common/extensions/api/commands/commands_handler.h"
20 #include "chrome/common/pref_names.h" 20 #include "chrome/common/pref_names.h"
21 #include "components/user_prefs/pref_registry_syncable.h" 21 #include "components/user_prefs/pref_registry_syncable.h"
22 #include "content/public/browser/notification_details.h" 22 #include "content/public/browser/notification_details.h"
23 #include "content/public/browser/notification_service.h" 23 #include "content/public/browser/notification_service.h"
24 24
25 using extensions::Extension; 25 using extensions::Extension;
26 using extensions::ExtensionPrefs;
26 27
27 namespace { 28 namespace {
28 29
29 const char kExtension[] = "extension"; 30 const char kExtension[] = "extension";
30 const char kCommandName[] = "command_name"; 31 const char kCommandName[] = "command_name";
31 32
33 // A preference that indicates that the initial keybindings for the given
34 // extension have been set.
35 const char kInitialBindingsHaveBeenAssigned[] = "initial_keybindings_set";
36
32 std::string GetPlatformKeybindingKeyForAccelerator( 37 std::string GetPlatformKeybindingKeyForAccelerator(
33 const ui::Accelerator& accelerator) { 38 const ui::Accelerator& accelerator) {
34 return extensions::Command::CommandPlatform() + ":" + 39 return extensions::Command::CommandPlatform() + ":" +
35 UTF16ToUTF8(accelerator.GetShortcutText()); 40 UTF16ToUTF8(accelerator.GetShortcutText());
36 } 41 }
37 42
43 void SetInitialBindingsHaveBeenAssigned(
44 ExtensionPrefs* prefs, const std::string& extension_id) {
45 prefs->UpdateExtensionPref(extension_id, kInitialBindingsHaveBeenAssigned,
46 Value::CreateBooleanValue(true));
47 }
48
49 bool InitialBindingsHaveBeenAssigned(
50 const ExtensionPrefs* prefs, const std::string& extension_id) {
51 bool assigned = false;
52 if (!prefs || !prefs->ReadPrefAsBoolean(extension_id,
53 kInitialBindingsHaveBeenAssigned,
54 &assigned))
55 return false;
56
57 return assigned;
58 }
59
38 } // namespace 60 } // namespace
39 61
40 namespace extensions { 62 namespace extensions {
41 63
42 // static 64 // static
43 void CommandService::RegisterUserPrefs(PrefRegistrySyncable* registry) { 65 void CommandService::RegisterUserPrefs(PrefRegistrySyncable* registry) {
44 registry->RegisterDictionaryPref(prefs::kExtensionCommands, 66 registry->RegisterDictionaryPref(prefs::kExtensionCommands,
45 PrefRegistrySyncable::SYNCABLE_PREF); 67 PrefRegistrySyncable::SYNCABLE_PREF);
46 } 68 }
47 69
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 246
225 return ui::Accelerator(); 247 return ui::Accelerator();
226 } 248 }
227 249
228 void CommandService::AssignInitialKeybindings(const Extension* extension) { 250 void CommandService::AssignInitialKeybindings(const Extension* extension) {
229 const extensions::CommandMap* commands = 251 const extensions::CommandMap* commands =
230 CommandsInfo::GetNamedCommands(extension); 252 CommandsInfo::GetNamedCommands(extension);
231 if (!commands) 253 if (!commands)
232 return; 254 return;
233 255
256 ExtensionService* extension_service =
257 ExtensionSystem::Get(profile_)->extension_service();
258 ExtensionPrefs* extension_prefs = extension_service->extension_prefs();
259 if (InitialBindingsHaveBeenAssigned(extension_prefs, extension->id()))
260 return;
261 SetInitialBindingsHaveBeenAssigned(extension_prefs, extension->id());
262
234 extensions::CommandMap::const_iterator iter = commands->begin(); 263 extensions::CommandMap::const_iterator iter = commands->begin();
235 for (; iter != commands->end(); ++iter) { 264 for (; iter != commands->end(); ++iter) {
236 if (!chrome::IsChromeAccelerator( 265 if (!chrome::IsChromeAccelerator(
237 iter->second.accelerator(), profile_)) { 266 iter->second.accelerator(), profile_)) {
238 AddKeybindingPref(iter->second.accelerator(), 267 AddKeybindingPref(iter->second.accelerator(),
239 extension->id(), 268 extension->id(),
240 iter->second.command_name(), 269 iter->second.command_name(),
241 false); // Overwriting not allowed. 270 false); // Overwriting not allowed.
242 } 271 }
243 } 272 }
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 return false; 395 return false;
367 396
368 *command = *requested_command; 397 *command = *requested_command;
369 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN) 398 if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN)
370 command->set_accelerator(shortcut_assigned); 399 command->set_accelerator(shortcut_assigned);
371 400
372 return true; 401 return true;
373 } 402 }
374 403
375 } // namespace extensions 404 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698