OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/extensions/extension_commands_global_registry.h" | |
6 | |
7 #include "base/lazy_instance.h" | |
8 #include "chrome/browser/chrome_notification_types.h" | |
9 #include "chrome/browser/extensions/api/commands/command_service.h" | |
10 #include "chrome/browser/extensions/extension_keybinding_registry.h" | |
11 #include "chrome/browser/extensions/extension_service.h" | |
12 #include "chrome/browser/extensions/global_shortcut_listener.h" | |
13 #include "chrome/browser/profiles/profile.h" | |
14 #include "chrome/common/extensions/extension.h" | |
15 | |
16 namespace extensions { | |
17 | |
18 ExtensionCommandsGlobalRegistry::ExtensionCommandsGlobalRegistry( | |
19 Profile* profile) | |
20 : ExtensionKeybindingRegistry( | |
21 profile, ExtensionKeybindingRegistry::ALL_EXTENSIONS, NULL), | |
22 profile_(profile) { | |
23 Init(); | |
24 } | |
25 | |
26 ExtensionCommandsGlobalRegistry::~ExtensionCommandsGlobalRegistry() { | |
27 EventTargets::const_iterator iter; | |
28 for (iter = event_targets_.begin(); iter != event_targets_.end(); ++iter) { | |
29 GlobalShortcutListener::GetInstance()->UnregisterAccelerator( | |
30 iter->first, this); | |
31 } | |
32 } | |
33 | |
34 static base::LazyInstance< | |
35 ProfileKeyedAPIFactory<ExtensionCommandsGlobalRegistry> > | |
36 g_factory = LAZY_INSTANCE_INITIALIZER; | |
37 | |
38 // static | |
39 ProfileKeyedAPIFactory<ExtensionCommandsGlobalRegistry>* | |
40 ExtensionCommandsGlobalRegistry::GetFactoryInstance() { | |
41 return &g_factory.Get(); | |
42 } | |
43 | |
44 template<> | |
45 void ProfileKeyedAPIFactory< | |
46 ExtensionCommandsGlobalRegistry>::DeclareFactoryDependencies() { | |
47 DependsOn(ExtensionSystemFactory::GetInstance()); | |
Yoyo Zhou
2013/10/03 22:08:50
This is already the default for DeclareFactoryDepe
Finnur
2013/10/04 17:57:02
Done.
| |
48 } | |
49 | |
50 // static | |
51 ExtensionCommandsGlobalRegistry* | |
52 ExtensionCommandsGlobalRegistry::Get(Profile* profile) { | |
53 return ProfileKeyedAPIFactory< | |
54 ExtensionCommandsGlobalRegistry>::GetForProfile(profile); | |
55 } | |
56 | |
57 | |
58 void ExtensionCommandsGlobalRegistry::AddExtensionKeybinding( | |
59 const extensions::Extension* extension, | |
60 const std::string& command_name) { | |
61 // This object only handles named commands, not browser/page actions. | |
62 if (ShouldIgnoreCommand(command_name)) | |
63 return; | |
64 | |
65 extensions::CommandService* command_service = | |
66 extensions::CommandService::Get(profile_); | |
67 // Add all the active keybindings (except page actions and browser actions, | |
Yoyo Zhou
2013/10/03 22:08:50
This comment doesn't seem to make sense, since pag
Finnur
2013/10/04 17:57:02
Done.
| |
68 // which are handled elsewhere). | |
69 extensions::CommandMap commands; | |
70 if (!command_service->GetNamedCommands( | |
71 extension->id(), | |
72 extensions::CommandService::ACTIVE_ONLY, | |
73 extensions::CommandService::GLOBAL, | |
74 &commands)) | |
75 return; | |
76 | |
77 extensions::CommandMap::const_iterator iter = commands.begin(); | |
78 for (; iter != commands.end(); ++iter) { | |
79 if (!command_name.empty() && (iter->second.command_name() != command_name)) | |
80 continue; | |
81 | |
82 LOG(ERROR) << "Adding global keybinding for " << extension->name().c_str() | |
Yoyo Zhou
2013/10/03 22:08:50
VLOG?
Finnur
2013/10/04 17:57:02
Done.
| |
83 << " " << command_name.c_str() | |
84 << " key: " << iter->second.accelerator().GetShortcutText(); | |
85 | |
86 event_targets_[iter->second.accelerator()] = | |
87 std::make_pair(extension->id(), iter->second.command_name()); | |
88 | |
89 GlobalShortcutListener::GetInstance()->RegisterAccelerator( | |
90 iter->second.accelerator(), this); | |
91 } | |
92 } | |
93 | |
94 void ExtensionCommandsGlobalRegistry::RemoveExtensionKeybinding( | |
95 const extensions::Extension* extension, | |
96 const std::string& command_name) { | |
97 // This object only handles named commands, not browser/page actions. | |
98 if (ShouldIgnoreCommand(command_name)) | |
99 return; | |
100 | |
101 EventTargets::iterator iter = event_targets_.begin(); | |
102 while (iter != event_targets_.end()) { | |
103 if (iter->second.first != extension->id() || | |
104 (!command_name.empty() && (iter->second.second != command_name))) { | |
105 ++iter; | |
106 continue; // Not the extension or command we asked for. | |
107 } | |
108 | |
109 LOG(ERROR) << "Removing keybinding for " << extension->name().c_str() | |
Yoyo Zhou
2013/10/03 22:08:50
ditto
Finnur
2013/10/04 17:57:02
Done.
| |
110 << " " << command_name.c_str(); | |
111 | |
112 GlobalShortcutListener::GetInstance()->UnregisterAccelerator( | |
113 iter->first, this); | |
114 | |
115 EventTargets::iterator old = iter++; | |
116 event_targets_.erase(old); | |
117 | |
118 // If a specific command_name was requested, it has now been deleted so | |
119 // no further work is required. | |
120 if (!command_name.empty()) | |
121 break; | |
122 } | |
123 } | |
124 | |
125 void ExtensionCommandsGlobalRegistry::OnKeyPressed( | |
126 const ui::Accelerator& accelerator) { | |
127 EventTargets::iterator it = event_targets_.find(accelerator); | |
128 if (it == event_targets_.end()) { | |
129 NOTREACHED(); // Shouldn't get this event for something not registered. | |
130 return; | |
131 } | |
132 | |
133 CommandExecuted(it->second.first, it->second.second); | |
134 } | |
135 | |
136 } // namespace extensions | |
OLD | NEW |