Upstream version 6.35.121.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 "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"
15
16 namespace values = extensions::manifest_values;
17
18 // static
19 void extensions::ExtensionKeybindingRegistry::SetShortcutHandlingSuspended(
20     bool suspended) {
21   ExtensionKeybindingRegistryCocoa::set_shortcut_handling_suspended(suspended);
22 }
23
24 bool ExtensionKeybindingRegistryCocoa::shortcut_handling_suspended_ = false;
25
26 ExtensionKeybindingRegistryCocoa::ExtensionKeybindingRegistryCocoa(
27     Profile* profile,
28     gfx::NativeWindow window,
29     ExtensionFilter extension_filter,
30     Delegate* delegate)
31     : ExtensionKeybindingRegistry(profile, extension_filter, delegate),
32       profile_(profile),
33       window_(window) {
34   Init();
35 }
36
37 ExtensionKeybindingRegistryCocoa::~ExtensionKeybindingRegistryCocoa() {
38 }
39
40 bool ExtensionKeybindingRegistryCocoa::ProcessKeyEvent(
41     const content::NativeWebKeyboardEvent& event) {
42   if (shortcut_handling_suspended_)
43     return false;
44
45   ui::Accelerator accelerator(
46       static_cast<ui::KeyboardCode>(event.windowsKeyCode),
47       content::GetModifiersFromNativeWebKeyboardEvent(event));
48
49   std::string extension_id;
50   std::string command_name;
51   if (!GetFirstTarget(accelerator, &extension_id, &command_name))
52     return false;
53
54   int type = 0;
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;
59   } else {
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);
63   }
64
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(
69       type,
70       content::Source<Profile>(profile_),
71       content::Details<
72           std::pair<const std::string, gfx::NativeWindow> >(&details));
73   return true;
74 }
75
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(
83           extension->id(),
84           extensions::CommandService::ACTIVE_ONLY,
85           extensions::CommandService::REGULAR,
86           &commands);
87
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))
91       continue;
92
93     AddEventTarget(iter->second.accelerator(),
94                    extension->id(),
95                    iter->second.command_name());
96   }
97
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(
103           extension->id(),
104           extensions::CommandService::ACTIVE_ONLY,
105           &browser_action,
106           NULL)) {
107     AddEventTarget(browser_action.accelerator(),
108                    extension->id(),
109                    browser_action.command_name());
110   }
111
112   // Add the Page Action (if any).
113   extensions::Command page_action;
114   if (command_service->GetPageActionCommand(
115           extension->id(),
116           extensions::CommandService::ACTIVE_ONLY,
117           &page_action,
118           NULL)) {
119     AddEventTarget(page_action.accelerator(),
120                    extension->id(),
121                    page_action.command_name());
122   }
123 }
124
125 void ExtensionKeybindingRegistryCocoa::RemoveExtensionKeybindingImpl(
126     const ui::Accelerator& accelerator,
127     const std::string& command_name) {
128 }