Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / extensions / extension_keybinding_registry_cocoa.mm
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.
4
5 #include "chrome/browser/ui/cocoa/extensions/extension_keybinding_registry_cocoa.h"
6
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"
16
17 namespace values = extensions::manifest_values;
18
19 // static
20 void extensions::ExtensionKeybindingRegistry::SetShortcutHandlingSuspended(
21     bool suspended) {
22   ExtensionKeybindingRegistryCocoa::set_shortcut_handling_suspended(suspended);
23 }
24
25 bool ExtensionKeybindingRegistryCocoa::shortcut_handling_suspended_ = false;
26
27 ExtensionKeybindingRegistryCocoa::ExtensionKeybindingRegistryCocoa(
28     Profile* profile,
29     gfx::NativeWindow window,
30     ExtensionFilter extension_filter,
31     Delegate* delegate)
32     : ExtensionKeybindingRegistry(profile, extension_filter, delegate),
33       profile_(profile),
34       window_(window) {
35   Init();
36 }
37
38 ExtensionKeybindingRegistryCocoa::~ExtensionKeybindingRegistryCocoa() {
39 }
40
41 bool ExtensionKeybindingRegistryCocoa::ProcessKeyEvent(
42     const content::NativeWebKeyboardEvent& event,
43     ui::AcceleratorManager::HandlerPriority priority) {
44   if (shortcut_handling_suspended_)
45     return false;
46
47   ui::Accelerator accelerator(
48       static_cast<ui::KeyboardCode>(event.windowsKeyCode),
49       content::GetModifiersFromNativeWebKeyboardEvent(event));
50
51   std::string extension_id;
52   std::string command_name;
53   if (!GetFirstTarget(accelerator, &extension_id, &command_name))
54     return false;
55
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)
60     return false;
61
62   int type = 0;
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;
67   } else {
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);
71   }
72
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(
77       type,
78       content::Source<Profile>(profile_),
79       content::Details<
80           std::pair<const std::string, gfx::NativeWindow> >(&details));
81   return true;
82 }
83
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(
91           extension->id(),
92           extensions::CommandService::ACTIVE_ONLY,
93           extensions::CommandService::REGULAR,
94           &commands);
95
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))
99       continue;
100
101     AddEventTarget(iter->second.accelerator(),
102                    extension->id(),
103                    iter->second.command_name());
104   }
105
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(
111           extension->id(),
112           extensions::CommandService::ACTIVE_ONLY,
113           &browser_action,
114           NULL)) {
115     AddEventTarget(browser_action.accelerator(),
116                    extension->id(),
117                    browser_action.command_name());
118   }
119
120   // Add the Page Action (if any).
121   extensions::Command page_action;
122   if (command_service->GetPageActionCommand(
123           extension->id(),
124           extensions::CommandService::ACTIVE_ONLY,
125           &page_action,
126           NULL)) {
127     AddEventTarget(page_action.accelerator(),
128                    extension->id(),
129                    page_action.command_name());
130   }
131 }
132
133 void ExtensionKeybindingRegistryCocoa::RemoveExtensionKeybindingImpl(
134     const ui::Accelerator& accelerator,
135     const std::string& command_name) {
136 }