Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / extensions / extension_keybinding_registry_cocoa.mm
blob541b9655b138919d263ae28b2b5ee43167724053
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(
20     Profile* profile,
21     gfx::NativeWindow window,
22     ExtensionFilter extension_filter,
23     Delegate* delegate)
24     : ExtensionKeybindingRegistry(profile, extension_filter, delegate),
25       profile_(profile),
26       window_(window) {
27   Init();
30 ExtensionKeybindingRegistryCocoa::~ExtensionKeybindingRegistryCocoa() {
33 bool ExtensionKeybindingRegistryCocoa::ProcessKeyEvent(
34     const content::NativeWebKeyboardEvent& event,
35     ui::AcceleratorManager::HandlerPriority priority) {
36   if (shortcut_handling_suspended())
37     return false;
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))
45     return false;
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)
51     return false;
53   int type = 0;
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;
58   } else {
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);
62   }
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(
68       type,
69       content::Source<Profile>(profile_),
70       content::Details<
71           std::pair<const std::string, gfx::NativeWindow> >(&details));
72   return true;
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(
82           extension->id(),
83           extensions::CommandService::ACTIVE,
84           extensions::CommandService::REGULAR,
85           &commands);
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))
90       continue;
92     AddEventTarget(iter->second.accelerator(),
93                    extension->id(),
94                    iter->second.command_name());
95   }
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(
104             extension->id(),
105             extensions::CommandService::ACTIVE,
106             &browser_action,
107             NULL)) {
108       AddEventTarget(browser_action.accelerator(),
109                      extension->id(),
110                      browser_action.command_name());
111     }
112   }
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(
119             extension->id(),
120             extensions::CommandService::ACTIVE,
121             &page_action,
122             NULL)) {
123       AddEventTarget(page_action.accelerator(),
124                      extension->id(),
125                      page_action.command_name());
126     }
127   }
130 void ExtensionKeybindingRegistryCocoa::RemoveExtensionKeybindingImpl(
131     const ui::Accelerator& accelerator,
132     const std::string& command_name) {