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/native_web_keyboard_event.h"
13 #include "content/public/browser/notification_service.h"
14 #include "extensions/common/extension.h"
15 #include "extensions/common/manifest_constants.h"
17 namespace values = extensions::manifest_values;
20 void extensions::ExtensionKeybindingRegistry::SetShortcutHandlingSuspended(
22 ExtensionKeybindingRegistryCocoa::set_shortcut_handling_suspended(suspended);
25 bool ExtensionKeybindingRegistryCocoa::shortcut_handling_suspended_ = false;
27 ExtensionKeybindingRegistryCocoa::ExtensionKeybindingRegistryCocoa(
29 gfx::NativeWindow window,
30 ExtensionFilter extension_filter,
32 : ExtensionKeybindingRegistry(profile, extension_filter, delegate),
38 ExtensionKeybindingRegistryCocoa::~ExtensionKeybindingRegistryCocoa() {
41 bool ExtensionKeybindingRegistryCocoa::ProcessKeyEvent(
42 const content::NativeWebKeyboardEvent& event,
43 ui::AcceleratorManager::HandlerPriority priority) {
44 if (shortcut_handling_suspended_)
47 ui::Accelerator accelerator(
48 static_cast<ui::KeyboardCode>(event.windowsKeyCode),
49 content::GetModifiersFromNativeWebKeyboardEvent(event));
51 std::string extension_id;
52 std::string command_name;
53 if (!GetFirstTarget(accelerator, &extension_id, &command_name))
56 const ui::AcceleratorManager::HandlerPriority accelerator_priority =
57 GetAcceleratorPriorityById(accelerator, extension_id, profile_);
58 // Only handle the event if it has the right priority.
59 if (priority != accelerator_priority)
63 if (command_name == values::kPageActionCommandEvent) {
64 type = extensions::NOTIFICATION_EXTENSION_COMMAND_PAGE_ACTION_MAC;
65 } else if (command_name == values::kBrowserActionCommandEvent) {
66 type = extensions::NOTIFICATION_EXTENSION_COMMAND_BROWSER_ACTION_MAC;
68 // Not handled by using notifications. Route it through the Browser Event
69 // Router using the base class (it will iterate through all targets).
70 return ExtensionKeybindingRegistry::NotifyEventTargets(accelerator);
73 // Type != named command, so we need to dispatch this event directly.
74 std::pair<const std::string, gfx::NativeWindow> details =
75 std::make_pair(extension_id, window_);
76 content::NotificationService::current()->Notify(
78 content::Source<Profile>(profile_),
80 std::pair<const std::string, gfx::NativeWindow> >(&details));
84 void ExtensionKeybindingRegistryCocoa::AddExtensionKeybinding(
85 const extensions::Extension* extension,
86 const std::string& command_name) {
87 extensions::CommandService* command_service =
88 extensions::CommandService::Get(profile_);
89 extensions::CommandMap commands;
90 command_service->GetNamedCommands(
92 extensions::CommandService::ACTIVE_ONLY,
93 extensions::CommandService::REGULAR,
96 for (extensions::CommandMap::const_iterator iter = commands.begin();
97 iter != commands.end(); ++iter) {
98 if (!command_name.empty() && (iter->second.command_name() != command_name))
101 AddEventTarget(iter->second.accelerator(),
103 iter->second.command_name());
106 // Mac implemenetation behaves like GTK with regards to what is kept in the
107 // event_targets_ map, because both GTK and Mac need to keep track of Browser
108 // and Page actions, as well as Script Badges.
109 extensions::Command browser_action;
110 if (command_service->GetBrowserActionCommand(
112 extensions::CommandService::ACTIVE_ONLY,
115 AddEventTarget(browser_action.accelerator(),
117 browser_action.command_name());
120 // Add the Page Action (if any).
121 extensions::Command page_action;
122 if (command_service->GetPageActionCommand(
124 extensions::CommandService::ACTIVE_ONLY,
127 AddEventTarget(page_action.accelerator(),
129 page_action.command_name());
133 void ExtensionKeybindingRegistryCocoa::RemoveExtensionKeybindingImpl(
134 const ui::Accelerator& accelerator,
135 const std::string& command_name) {