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

Side by Side Diff: chrome/browser/extensions/extension_commands_global_registry.cc

Issue 109413003: Refactor GlobalShortcutListener. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Clearer logic. Created 7 years 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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/extension_commands_global_registry.h" 5 #include "chrome/browser/extensions/extension_commands_global_registry.h"
6 6
7 #include "base/lazy_instance.h" 7 #include "base/lazy_instance.h"
8 #include "chrome/browser/chrome_notification_types.h" 8 #include "chrome/browser/chrome_notification_types.h"
Mark Mentovai 2013/12/19 21:03:17 This isn’t used in here.
zhchbin 2013/12/20 09:13:12 Done.
9 #include "chrome/browser/extensions/api/commands/command_service.h" 9 #include "chrome/browser/extensions/api/commands/command_service.h"
10 #include "chrome/browser/extensions/extension_keybinding_registry.h" 10 #include "chrome/browser/extensions/extension_keybinding_registry.h"
Mark Mentovai 2013/12/19 21:03:17 This isn’t necessary because the associated header
zhchbin 2013/12/20 09:13:12 Done.
11 #include "chrome/browser/extensions/extension_service.h" 11 #include "chrome/browser/extensions/extension_service.h"
Mark Mentovai 2013/12/19 21:03:17 I don’t believe this one is used in this file.
zhchbin 2013/12/20 09:13:12 Done.
12 #include "chrome/browser/extensions/global_shortcut_listener.h" 12 #include "chrome/browser/extensions/global_shortcut_listener.h"
13 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
14 #include "extensions/common/extension.h" 14 #include "extensions/common/extension.h"
15 15
16 namespace extensions { 16 namespace extensions {
17 17
18 ExtensionCommandsGlobalRegistry::ExtensionCommandsGlobalRegistry( 18 ExtensionCommandsGlobalRegistry::ExtensionCommandsGlobalRegistry(
19 Profile* profile) 19 Profile* profile)
20 : ExtensionKeybindingRegistry( 20 : ExtensionKeybindingRegistry(
21 profile, ExtensionKeybindingRegistry::ALL_EXTENSIONS, NULL), 21 profile, ExtensionKeybindingRegistry::ALL_EXTENSIONS, NULL),
22 profile_(profile) { 22 profile_(profile) {
23 Init(); 23 Init();
24 } 24 }
25 25
26 ExtensionCommandsGlobalRegistry::~ExtensionCommandsGlobalRegistry() { 26 ExtensionCommandsGlobalRegistry::~ExtensionCommandsGlobalRegistry() {
27 for (EventTargets::const_iterator iter = event_targets_.begin(); 27 for (EventTargets::const_iterator iter = event_targets_.begin();
28 iter != event_targets_.end(); ++iter) { 28 iter != event_targets_.end(); ++iter) {
29 GlobalShortcutListener::GetInstance()->UnregisterAccelerator( 29 GlobalShortcutListener::GetInstance()->UnregisterAccelerator(
30 iter->first, this); 30 iter->first, this);
31 } 31 }
32 } 32 }
33 33
34 static base::LazyInstance< 34 static base::LazyInstance<
35 ProfileKeyedAPIFactory<ExtensionCommandsGlobalRegistry> > 35 ProfileKeyedAPIFactory<ExtensionCommandsGlobalRegistry> >
36 g_factory = LAZY_INSTANCE_INITIALIZER; 36 g_factory = LAZY_INSTANCE_INITIALIZER;
37 37
38 // static 38 // static
39 ProfileKeyedAPIFactory<ExtensionCommandsGlobalRegistry>* 39 ProfileKeyedAPIFactory<ExtensionCommandsGlobalRegistry>*
40 ExtensionCommandsGlobalRegistry::GetFactoryInstance() { 40 ExtensionCommandsGlobalRegistry::GetFactoryInstance() {
41 return &g_factory.Get(); 41 return &g_factory.Get();
Mark Mentovai 2013/12/19 21:03:17 This class is also single-threaded, right? (I’m as
zhchbin 2013/12/20 09:13:12 I have no idea currently, but it follows the instr
Mark Mentovai 2013/12/20 17:54:47 zhchbin wrote:
zhchbin 2013/12/21 09:10:25 After searching with "GetFactoryInstance" in codes
Mark Mentovai 2014/01/02 17:42:37 zhchbin wrote:
42 } 42 }
43 43
44 // static 44 // static
45 ExtensionCommandsGlobalRegistry* 45 ExtensionCommandsGlobalRegistry*
46 ExtensionCommandsGlobalRegistry::Get(Profile* profile) { 46 ExtensionCommandsGlobalRegistry::Get(Profile* profile) {
47 return ProfileKeyedAPIFactory< 47 return ProfileKeyedAPIFactory<
48 ExtensionCommandsGlobalRegistry>::GetForProfile(profile); 48 ExtensionCommandsGlobalRegistry>::GetForProfile(profile);
49 } 49 }
50 50
Mark Mentovai 2013/12/19 21:03:17 (nit) Extra blank line.
zhchbin 2013/12/20 09:13:12 Done.
51 51
52 void ExtensionCommandsGlobalRegistry::AddExtensionKeybinding( 52 void ExtensionCommandsGlobalRegistry::AddExtensionKeybinding(
53 const extensions::Extension* extension, 53 const extensions::Extension* extension,
54 const std::string& command_name) { 54 const std::string& command_name) {
55 // This object only handles named commands, not browser/page actions. 55 // This object only handles named commands, not browser/page actions.
56 if (ShouldIgnoreCommand(command_name)) 56 if (ShouldIgnoreCommand(command_name))
57 return; 57 return;
58 58
59 extensions::CommandService* command_service = 59 extensions::CommandService* command_service =
60 extensions::CommandService::Get(profile_); 60 extensions::CommandService::Get(profile_);
61 // Add all the active global keybindings, if any. 61 // Add all the active global keybindings, if any.
62 extensions::CommandMap commands; 62 extensions::CommandMap commands;
63 if (!command_service->GetNamedCommands( 63 if (!command_service->GetNamedCommands(
64 extension->id(), 64 extension->id(),
65 extensions::CommandService::ACTIVE_ONLY, 65 extensions::CommandService::ACTIVE_ONLY,
66 extensions::CommandService::GLOBAL, 66 extensions::CommandService::GLOBAL,
67 &commands)) 67 &commands))
68 return; 68 return;
69 69
70 extensions::CommandMap::const_iterator iter = commands.begin(); 70 extensions::CommandMap::const_iterator iter = commands.begin();
71 for (; iter != commands.end(); ++iter) { 71 for (; iter != commands.end(); ++iter) {
72 if (!command_name.empty() && (iter->second.command_name() != command_name)) 72 if (!command_name.empty() && (iter->second.command_name() != command_name))
73 continue; 73 continue;
74 const ui::Accelerator& accelerator = iter->second.accelerator();
74 75
75 VLOG(0) << "Adding global keybinding for " << extension->name().c_str() 76 VLOG(0) << "Adding global keybinding for " << extension->name().c_str()
76 << " " << command_name.c_str() 77 << " " << command_name.c_str()
77 << " key: " << iter->second.accelerator().GetShortcutText(); 78 << " key: " << accelerator.GetShortcutText();
78 79
79 event_targets_[iter->second.accelerator()].push_back( 80 if (event_targets_.find(accelerator) == event_targets_.end()) {
81 if (!GlobalShortcutListener::GetInstance()->RegisterAccelerator(
82 accelerator, this))
83 continue;
84 }
85
86 event_targets_[accelerator].push_back(
Mark Mentovai 2013/12/19 18:00:29 Protected data members that are maintained by a cl
Finnur 2013/12/19 19:59:13 Glad you are reviewing this... :) I guess the sup
Mark Mentovai 2013/12/19 20:15:43 Finnur wrote:
80 std::make_pair(extension->id(), iter->second.command_name())); 87 std::make_pair(extension->id(), iter->second.command_name()));
81 // Shortcuts except media keys have only one target in the list. See comment 88 // Shortcuts except media keys have only one target in the list. See comment
82 // about |event_targets_|. 89 // about |event_targets_|.
83 if (!extensions::CommandService::IsMediaKey(iter->second.accelerator())) 90 if (!extensions::CommandService::IsMediaKey(accelerator))
84 DCHECK(event_targets_[iter->second.accelerator()].size() == 1); 91 DCHECK_EQ(1u, event_targets_[accelerator].size());
Mark Mentovai 2013/12/19 15:24:13 Is this the only check for multiple registration o
Finnur 2013/12/19 15:40:40 When you set a shortcut the current code removes t
Mark Mentovai 2013/12/19 18:00:29 Finnur wrote:
85
86 GlobalShortcutListener::GetInstance()->RegisterAccelerator(
87 iter->second.accelerator(), this);
88 } 92 }
89 } 93 }
90 94
91 void ExtensionCommandsGlobalRegistry::RemoveExtensionKeybindingImpl( 95 void ExtensionCommandsGlobalRegistry::RemoveExtensionKeybindingImpl(
92 const ui::Accelerator& accelerator, 96 const ui::Accelerator& accelerator,
93 const std::string& command_name) { 97 const std::string& command_name) {
94 VLOG(0) << "Removing keybinding for " << command_name.c_str(); 98 VLOG(0) << "Removing keybinding for " << command_name.c_str();
95 99
96 GlobalShortcutListener::GetInstance()->UnregisterAccelerator( 100 GlobalShortcutListener::GetInstance()->UnregisterAccelerator(
Mark Mentovai 2013/12/19 18:00:29 A similar question. What happens if: 1. extension
Finnur 2013/12/19 19:59:13 Correct. Step 2 can only happen if a user assigns
97 accelerator, this); 101 accelerator, this);
98 } 102 }
99 103
100 void ExtensionCommandsGlobalRegistry::OnKeyPressed( 104 void ExtensionCommandsGlobalRegistry::OnKeyPressed(
101 const ui::Accelerator& accelerator) { 105 const ui::Accelerator& accelerator) {
102 ExtensionKeybindingRegistry::NotifyEventTargets(accelerator); 106 ExtensionKeybindingRegistry::NotifyEventTargets(accelerator);
103 } 107 }
104 108
105 } // namespace extensions 109 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698