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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/extension_keybinding_registry.h"
7 #include "base/values.h"
8 #include "chrome/browser/extensions/active_tab_permission_granter.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/common/extensions/command.h"
11 #include "content/public/browser/browser_context.h"
12 #include "extensions/browser/event_router.h"
13 #include "extensions/browser/extension_registry.h"
14 #include "extensions/browser/notification_types.h"
15 #include "extensions/common/extension_set.h"
16 #include "extensions/common/manifest_constants.h"
19 const char kOnCommandEventName
[] = "commands.onCommand";
22 namespace extensions
{
24 ExtensionKeybindingRegistry::ExtensionKeybindingRegistry(
25 content::BrowserContext
* context
,
26 ExtensionFilter extension_filter
,
28 : browser_context_(context
),
29 extension_filter_(extension_filter
),
31 extension_registry_observer_(this),
32 shortcut_handling_suspended_(false) {
33 extension_registry_observer_
.Add(ExtensionRegistry::Get(browser_context_
));
35 Profile
* profile
= Profile::FromBrowserContext(browser_context_
);
37 extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED
,
38 content::Source
<Profile
>(profile
->GetOriginalProfile()));
40 extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED
,
41 content::Source
<Profile
>(profile
->GetOriginalProfile()));
44 ExtensionKeybindingRegistry::~ExtensionKeybindingRegistry() {
47 void ExtensionKeybindingRegistry::SetShortcutHandlingSuspended(bool suspended
) {
48 shortcut_handling_suspended_
= suspended
;
49 OnShortcutHandlingSuspended(suspended
);
52 void ExtensionKeybindingRegistry::RemoveExtensionKeybinding(
53 const Extension
* extension
,
54 const std::string
& command_name
) {
55 EventTargets::iterator it
= event_targets_
.begin();
56 while (it
!= event_targets_
.end()) {
57 TargetList
& target_list
= it
->second
;
58 TargetList::iterator target
= target_list
.begin();
59 while (target
!= target_list
.end()) {
60 if (target
->first
== extension
->id() &&
61 (command_name
.empty() || command_name
== target
->second
))
62 target
= target_list
.erase(target
);
67 EventTargets::iterator old
= it
++;
68 if (target_list
.empty()) {
69 // Let each platform-specific implementation get a chance to clean up.
70 RemoveExtensionKeybindingImpl(old
->first
, command_name
);
71 event_targets_
.erase(old
);
73 // If a specific command_name was requested, it has now been deleted so no
74 // further work is required.
75 if (!command_name
.empty())
81 void ExtensionKeybindingRegistry::Init() {
82 ExtensionRegistry
* registry
= ExtensionRegistry::Get(browser_context_
);
84 return; // ExtensionRegistry can be null during testing.
86 for (const scoped_refptr
<const extensions::Extension
>& extension
:
87 registry
->enabled_extensions())
88 if (ExtensionMatchesFilter(extension
.get()))
89 AddExtensionKeybindings(extension
.get(), std::string());
92 bool ExtensionKeybindingRegistry::ShouldIgnoreCommand(
93 const std::string
& command
) const {
94 return command
== manifest_values::kPageActionCommandEvent
||
95 command
== manifest_values::kBrowserActionCommandEvent
;
98 bool ExtensionKeybindingRegistry::NotifyEventTargets(
99 const ui::Accelerator
& accelerator
) {
100 return ExecuteCommands(accelerator
, std::string());
103 void ExtensionKeybindingRegistry::CommandExecuted(
104 const std::string
& extension_id
, const std::string
& command
) {
105 const Extension
* extension
= ExtensionRegistry::Get(browser_context_
)
106 ->enabled_extensions()
107 .GetByID(extension_id
);
111 // Grant before sending the event so that the permission is granted before
112 // the extension acts on the command. NOTE: The Global Commands handler does
113 // not set the delegate as it deals only with named commands (not page/browser
114 // actions that are associated with the current page directly).
115 ActiveTabPermissionGranter
* granter
=
116 delegate_
? delegate_
->GetActiveTabPermissionGranter() : NULL
;
118 granter
->GrantIfRequested(extension
);
120 scoped_ptr
<base::ListValue
> args(new base::ListValue());
121 args
->Append(new base::StringValue(command
));
123 scoped_ptr
<Event
> event(new Event(kOnCommandEventName
, args
.Pass()));
124 event
->restrict_to_browser_context
= browser_context_
;
125 event
->user_gesture
= EventRouter::USER_GESTURE_ENABLED
;
126 EventRouter::Get(browser_context_
)
127 ->DispatchEventToExtension(extension_id
, event
.Pass());
130 bool ExtensionKeybindingRegistry::IsAcceleratorRegistered(
131 const ui::Accelerator
& accelerator
) const {
132 return event_targets_
.find(accelerator
) != event_targets_
.end();
135 void ExtensionKeybindingRegistry::AddEventTarget(
136 const ui::Accelerator
& accelerator
,
137 const std::string
& extension_id
,
138 const std::string
& command_name
) {
139 event_targets_
[accelerator
].push_back(
140 std::make_pair(extension_id
, command_name
));
141 // Shortcuts except media keys have only one target in the list. See comment
142 // about |event_targets_|.
143 if (!extensions::Command::IsMediaKey(accelerator
))
144 DCHECK_EQ(1u, event_targets_
[accelerator
].size());
147 bool ExtensionKeybindingRegistry::GetFirstTarget(
148 const ui::Accelerator
& accelerator
,
149 std::string
* extension_id
,
150 std::string
* command_name
) const {
151 EventTargets::const_iterator targets
= event_targets_
.find(accelerator
);
152 if (targets
== event_targets_
.end())
155 DCHECK(!targets
->second
.empty());
156 TargetList::const_iterator first_target
= targets
->second
.begin();
157 *extension_id
= first_target
->first
;
158 *command_name
= first_target
->second
;
162 bool ExtensionKeybindingRegistry::IsEventTargetsEmpty() const {
163 return event_targets_
.empty();
166 void ExtensionKeybindingRegistry::ExecuteCommand(
167 const std::string
& extension_id
,
168 const ui::Accelerator
& accelerator
) {
169 ExecuteCommands(accelerator
, extension_id
);
172 void ExtensionKeybindingRegistry::OnExtensionLoaded(
173 content::BrowserContext
* browser_context
,
174 const Extension
* extension
) {
175 if (ExtensionMatchesFilter(extension
))
176 AddExtensionKeybindings(extension
, std::string());
179 void ExtensionKeybindingRegistry::OnExtensionUnloaded(
180 content::BrowserContext
* browser_context
,
181 const Extension
* extension
,
182 UnloadedExtensionInfo::Reason reason
) {
183 if (ExtensionMatchesFilter(extension
))
184 RemoveExtensionKeybinding(extension
, std::string());
187 void ExtensionKeybindingRegistry::Observe(
189 const content::NotificationSource
& source
,
190 const content::NotificationDetails
& details
) {
192 case extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED
:
193 case extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED
: {
194 ExtensionCommandRemovedDetails
* payload
=
195 content::Details
<ExtensionCommandRemovedDetails
>(details
).ptr();
197 const Extension
* extension
= ExtensionRegistry::Get(browser_context_
)
198 ->enabled_extensions()
199 .GetByID(payload
->extension_id
);
200 // During install and uninstall the extension won't be found. We'll catch
201 // those events above, with the LOADED/UNLOADED, so we ignore this event.
205 if (ExtensionMatchesFilter(extension
)) {
206 if (type
== extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED
) {
207 // Component extensions triggers OnExtensionLoaded for extension
208 // installs as well as loads. This can cause adding of multiple key
210 if (extension
->location() == Manifest::COMPONENT
)
213 AddExtensionKeybindings(extension
, payload
->command_name
);
215 RemoveExtensionKeybinding(extension
, payload
->command_name
);
226 bool ExtensionKeybindingRegistry::ExtensionMatchesFilter(
227 const extensions::Extension
* extension
)
229 switch (extension_filter_
) {
232 case PLATFORM_APPS_ONLY
:
233 return extension
->is_platform_app();
240 bool ExtensionKeybindingRegistry::ExecuteCommands(
241 const ui::Accelerator
& accelerator
,
242 const std::string
& extension_id
) {
243 EventTargets::iterator targets
= event_targets_
.find(accelerator
);
244 if (targets
== event_targets_
.end() || targets
->second
.empty())
247 bool executed
= false;
248 for (TargetList::const_iterator it
= targets
->second
.begin();
249 it
!= targets
->second
.end(); it
++) {
250 if (!extensions::EventRouter::Get(browser_context_
)
251 ->ExtensionHasEventListener(it
->first
, kOnCommandEventName
))
254 if (extension_id
.empty() || it
->first
== extension_id
) {
255 CommandExecuted(it
->first
, it
->second
);
263 } // namespace extensions