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 "content/public/browser/native_web_keyboard_event.h"
12 #include "content/public/browser/notification_service.h"
13 #include "extensions/common/extension.h"
14 #include "extensions/common/manifest_constants.h"
16 namespace values = extensions::manifest_values;
19 void extensions::ExtensionKeybindingRegistry::SetShortcutHandlingSuspended(
21 ExtensionKeybindingRegistryCocoa::set_shortcut_handling_suspended(suspended);
24 bool ExtensionKeybindingRegistryCocoa::shortcut_handling_suspended_ = false;
26 ExtensionKeybindingRegistryCocoa::ExtensionKeybindingRegistryCocoa(
28 gfx::NativeWindow window,
29 ExtensionFilter extension_filter,
31 : ExtensionKeybindingRegistry(profile, extension_filter, delegate),
37 ExtensionKeybindingRegistryCocoa::~ExtensionKeybindingRegistryCocoa() {
40 bool ExtensionKeybindingRegistryCocoa::ProcessKeyEvent(
41 const content::NativeWebKeyboardEvent& event) {
42 if (shortcut_handling_suspended_)
45 ui::Accelerator accelerator(
46 static_cast<ui::KeyboardCode>(event.windowsKeyCode),
47 content::GetModifiersFromNativeWebKeyboardEvent(event));
49 std::string extension_id;
50 std::string command_name;
51 if (!GetFirstTarget(accelerator, &extension_id, &command_name))
55 if (command_name == values::kPageActionCommandEvent) {
56 type = chrome::NOTIFICATION_EXTENSION_COMMAND_PAGE_ACTION_MAC;
57 } else if (command_name == values::kBrowserActionCommandEvent) {
58 type = chrome::NOTIFICATION_EXTENSION_COMMAND_BROWSER_ACTION_MAC;
60 // Not handled by using notifications. Route it through the Browser Event
61 // Router using the base class (it will iterate through all targets).
62 return ExtensionKeybindingRegistry::NotifyEventTargets(accelerator);
65 // Type != named command, so we need to dispatch this event directly.
66 std::pair<const std::string, gfx::NativeWindow> details =
67 std::make_pair(extension_id, window_);
68 content::NotificationService::current()->Notify(
70 content::Source<Profile>(profile_),
72 std::pair<const std::string, gfx::NativeWindow> >(&details));
76 void ExtensionKeybindingRegistryCocoa::AddExtensionKeybinding(
77 const extensions::Extension* extension,
78 const std::string& command_name) {
79 extensions::CommandService* command_service =
80 extensions::CommandService::Get(profile_);
81 extensions::CommandMap commands;
82 command_service->GetNamedCommands(
84 extensions::CommandService::ACTIVE_ONLY,
85 extensions::CommandService::REGULAR,
88 for (extensions::CommandMap::const_iterator iter = commands.begin();
89 iter != commands.end(); ++iter) {
90 if (!command_name.empty() && (iter->second.command_name() != command_name))
93 AddEventTarget(iter->second.accelerator(),
95 iter->second.command_name());
98 // Mac implemenetation behaves like GTK with regards to what is kept in the
99 // event_targets_ map, because both GTK and Mac need to keep track of Browser
100 // and Page actions, as well as Script Badges.
101 extensions::Command browser_action;
102 if (command_service->GetBrowserActionCommand(
104 extensions::CommandService::ACTIVE_ONLY,
107 AddEventTarget(browser_action.accelerator(),
109 browser_action.command_name());
112 // Add the Page Action (if any).
113 extensions::Command page_action;
114 if (command_service->GetPageActionCommand(
116 extensions::CommandService::ACTIVE_ONLY,
119 AddEventTarget(page_action.accelerator(),
121 page_action.command_name());
125 void ExtensionKeybindingRegistryCocoa::RemoveExtensionKeybindingImpl(
126 const ui::Accelerator& accelerator,
127 const std::string& command_name) {