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/ui/cocoa/extensions/extension_keybinding_registry_cocoa.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/extensions/api/commands/command_service.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/extensions/accelerator_priority.h"
12 #include "content/public/browser/notification_service.h"
13 #include "extensions/common/extension.h"
14 #include "extensions/common/manifest_constants.h"
15 #include "ui/content_accelerators/accelerator_util.h"
17 namespace values = extensions::manifest_values;
19 ExtensionKeybindingRegistryCocoa::ExtensionKeybindingRegistryCocoa(
21 gfx::NativeWindow window,
22 ExtensionFilter extension_filter,
24 : ExtensionKeybindingRegistry(profile, extension_filter, delegate),
30 ExtensionKeybindingRegistryCocoa::~ExtensionKeybindingRegistryCocoa() {
33 bool ExtensionKeybindingRegistryCocoa::ProcessKeyEvent(
34 const content::NativeWebKeyboardEvent& event,
35 ui::AcceleratorManager::HandlerPriority priority) {
36 if (shortcut_handling_suspended())
39 ui::Accelerator accelerator =
40 ui::GetAcceleratorFromNativeWebKeyboardEvent(event);
42 std::string extension_id;
43 std::string command_name;
44 if (!GetFirstTarget(accelerator, &extension_id, &command_name))
47 const ui::AcceleratorManager::HandlerPriority accelerator_priority =
48 GetAcceleratorPriorityById(accelerator, extension_id, profile_);
49 // Only handle the event if it has the right priority.
50 if (priority != accelerator_priority)
54 if (command_name == values::kPageActionCommandEvent) {
55 type = extensions::NOTIFICATION_EXTENSION_COMMAND_PAGE_ACTION_MAC;
56 } else if (command_name == values::kBrowserActionCommandEvent) {
57 type = extensions::NOTIFICATION_EXTENSION_COMMAND_BROWSER_ACTION_MAC;
59 // Not handled by using notifications. Route it through the Browser Event
60 // Router using the base class (it will iterate through all targets).
61 return ExtensionKeybindingRegistry::NotifyEventTargets(accelerator);
64 // Type != named command, so we need to dispatch this event directly.
65 std::pair<const std::string, gfx::NativeWindow> details =
66 std::make_pair(extension_id, window_);
67 content::NotificationService::current()->Notify(
69 content::Source<Profile>(profile_),
71 std::pair<const std::string, gfx::NativeWindow> >(&details));
75 void ExtensionKeybindingRegistryCocoa::AddExtensionKeybindings(
76 const extensions::Extension* extension,
77 const std::string& command_name) {
78 extensions::CommandService* command_service =
79 extensions::CommandService::Get(profile_);
80 extensions::CommandMap commands;
81 command_service->GetNamedCommands(
83 extensions::CommandService::ACTIVE,
84 extensions::CommandService::REGULAR,
87 for (extensions::CommandMap::const_iterator iter = commands.begin();
88 iter != commands.end(); ++iter) {
89 if (!command_name.empty() && (iter->second.command_name() != command_name))
92 AddEventTarget(iter->second.accelerator(),
94 iter->second.command_name());
97 // The Mac implementation keeps track of browser and page actions in the
98 // event_targets_ map.
99 if (command_name.empty() ||
100 command_name == extensions::manifest_values::kBrowserActionCommandEvent) {
101 // Add the browser action (if any).
102 extensions::Command browser_action;
103 if (command_service->GetBrowserActionCommand(
105 extensions::CommandService::ACTIVE,
108 AddEventTarget(browser_action.accelerator(),
110 browser_action.command_name());
114 if (command_name.empty() ||
115 command_name == extensions::manifest_values::kPageActionCommandEvent) {
116 // Add the page action (if any).
117 extensions::Command page_action;
118 if (command_service->GetPageActionCommand(
120 extensions::CommandService::ACTIVE,
123 AddEventTarget(page_action.accelerator(),
125 page_action.command_name());
130 void ExtensionKeybindingRegistryCocoa::RemoveExtensionKeybindingImpl(
131 const ui::Accelerator& accelerator,
132 const std::string& command_name) {