OLD | NEW |
---|---|
(Empty) | |
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/extensions/api/commands/command_service.h" | |
8 #include "chrome/browser/extensions/api/commands/command_service_factory.h" | |
9 #include "chrome/browser/extensions/browser_event_router.h" | |
10 #include "chrome/browser/extensions/extension_service.h" | |
11 #include "chrome/browser/profiles/profile.h" | |
12 #include "chrome/common/extensions/extension.h" | |
13 #include "chrome/common/chrome_notification_types.h" | |
14 #include "chrome/common/extensions/extension_manifest_constants.h" | |
15 #include "content/public/browser/native_web_keyboard_event.h" | |
16 #include "content/public/browser/notification_service.h" | |
17 | |
18 namespace values = extension_manifest_values; | |
19 | |
20 // static | |
21 void extensions::ExtensionKeybindingRegistry::SetShortcutHandlingSuspended( | |
22 bool suspended) { | |
23 ExtensionKeybindingRegistryCocoa::set_shortcut_handling_suspended(suspended); | |
24 } | |
25 | |
26 bool ExtensionKeybindingRegistryCocoa::shortcut_handling_suspended_ = false; | |
27 | |
28 ExtensionKeybindingRegistryCocoa::ExtensionKeybindingRegistryCocoa( | |
29 Profile* profile, gfx::NativeWindow window) | |
30 : ExtensionKeybindingRegistry(profile), | |
31 profile_(profile), | |
32 window_(window) { | |
33 Init(); | |
34 } | |
35 | |
36 ExtensionKeybindingRegistryCocoa::~ExtensionKeybindingRegistryCocoa() { | |
37 } | |
38 | |
39 bool ExtensionKeybindingRegistryCocoa::ProcessKeyEvent( | |
40 const content::NativeWebKeyboardEvent& event) { | |
41 if (shortcut_handling_suspended_) | |
42 return false; | |
43 | |
44 ui::Accelerator accelerator( | |
45 static_cast<ui::KeyboardCode>(event.windowsKeyCode), | |
46 content::GetModifiersFromNativeWebKeyboardEvent(event)); | |
47 EventTargets::iterator it = event_targets_.find(accelerator); | |
48 if (it == event_targets_.end()) | |
49 return false; | |
50 | |
51 std::string extension_id = it->second.first; | |
Scott Hess - ex-Googler
2012/08/15 19:35:57
I think it would be reasonable to capture it->seco
| |
52 int type = 0; | |
53 if (it->second.second == values::kPageActionCommandEvent) { | |
54 type = chrome::NOTIFICATION_EXTENSION_COMMAND_PAGE_ACTION_MAC; | |
55 } else if (it->second.second == values::kBrowserActionCommandEvent) { | |
56 type = chrome::NOTIFICATION_EXTENSION_COMMAND_BROWSER_ACTION_MAC; | |
57 } else if (it->second.second == values::kScriptBadgeCommandEvent) { | |
58 type = chrome::NOTIFICATION_EXTENSION_COMMAND_SCRIPT_BADGE_MAC; | |
59 } else { | |
60 // Not handled by using notifications. Route it through the Browser Event | |
61 // Router. | |
62 ExtensionService* service = profile_->GetExtensionService(); | |
63 service->browser_event_router()->CommandExecuted( | |
64 profile_, it->second.first, it->second.second); | |
Scott Hess - ex-Googler
2012/08/15 19:35:57
extension_id instead of it->second.first, likewise
| |
65 return true; | |
66 } | |
67 | |
68 std::pair<const std::string, gfx::NativeWindow> details = | |
69 std::make_pair(extension_id, window_); | |
70 content::NotificationService::current()->Notify( | |
71 type, | |
72 content::Source<Profile>(profile_), | |
73 content::Details< | |
74 std::pair<const std::string, gfx::NativeWindow> >(&details)); | |
75 return true; | |
76 } | |
77 | |
78 void ExtensionKeybindingRegistryCocoa::AddExtensionKeybinding( | |
79 const extensions::Extension* extension, | |
80 const std::string& command_name) { | |
81 extensions::CommandService* command_service = | |
82 extensions::CommandServiceFactory::GetForProfile(profile_); | |
83 extensions::CommandMap commands; | |
84 command_service->GetNamedCommands( | |
85 extension->id(), | |
86 extensions::CommandService::ACTIVE_ONLY, | |
87 &commands); | |
88 | |
89 extensions::CommandMap::const_iterator iter = commands.begin(); | |
90 for (; iter != commands.end(); ++iter) { | |
91 if (!command_name.empty() && (iter->second.command_name() != command_name)) | |
92 continue; | |
93 | |
94 ui::Accelerator accelerator(iter->second.accelerator()); | |
95 event_targets_[accelerator] = | |
96 std::make_pair(extension->id(), iter->second.command_name()); | |
97 } | |
98 | |
99 // Mac implemenetation behaves like GTK with regards to what is kept in the | |
100 // event_targets_ map, because both GTK and Mac need to keep track of Browser | |
101 // and Page actions, as well as Script Badges. | |
102 extensions::Command browser_action; | |
103 if (command_service->GetBrowserActionCommand( | |
104 extension->id(), | |
105 extensions::CommandService::ACTIVE_ONLY, | |
106 &browser_action, | |
107 NULL)) { | |
108 ui::Accelerator accelerator(browser_action.accelerator()); | |
109 event_targets_[accelerator] = | |
110 std::make_pair(extension->id(), browser_action.command_name()); | |
111 } | |
112 | |
113 // Add the Page Action (if any). | |
114 extensions::Command page_action; | |
115 if (command_service->GetPageActionCommand( | |
116 extension->id(), | |
117 extensions::CommandService::ACTIVE_ONLY, | |
118 &page_action, | |
119 NULL)) { | |
120 ui::Accelerator accelerator(page_action.accelerator()); | |
121 event_targets_[accelerator] = | |
122 std::make_pair(extension->id(), page_action.command_name()); | |
123 } | |
124 | |
125 // Add the Script Badge (if any). | |
126 extensions::Command script_badge; | |
127 if (command_service->GetScriptBadgeCommand( | |
128 extension->id(), | |
129 extensions::CommandService::ACTIVE_ONLY, | |
130 &script_badge, | |
131 NULL)) { | |
132 ui::Accelerator accelerator(script_badge.accelerator()); | |
133 event_targets_[accelerator] = | |
134 std::make_pair(extension->id(), script_badge.command_name()); | |
135 } | |
136 } | |
137 | |
138 void ExtensionKeybindingRegistryCocoa::RemoveExtensionKeybinding( | |
139 const extensions::Extension* extension, | |
140 const std::string& command_name) { | |
141 EventTargets::iterator iter = event_targets_.begin(); | |
142 while (iter != event_targets_.end()) { | |
143 if (iter->second.first != extension->id() || | |
144 (!command_name.empty() && (iter->second.second != command_name))) { | |
145 ++iter; | |
146 continue; // Not the extension or command we asked for. | |
147 } | |
148 | |
149 EventTargets::iterator old = iter++; | |
150 event_targets_.erase(old); | |
Scott Hess - ex-Googler
2012/08/15 19:35:57
This seems convoluted. What if you just wrapped t
Finnur
2012/08/15 20:51:53
Just to be clear: You problem is more with the if
Scott Hess - ex-Googler
2012/08/15 21:07:03
Yeah, 149/150 look right, I'm more concerned about
| |
151 } | |
152 } | |
153 | |
OLD | NEW |