Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(244)

Side by Side Diff: chrome/browser/ui/cocoa/extensions/extension_keybinding_registry_cocoa.mm

Issue 10824307: Port Extension Commands to Mac. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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;
52 std::string command_name = it->second.second;
53 int type = 0;
54 if (it->second.second == values::kPageActionCommandEvent) {
Scott Hess - ex-Googler 2012/08/16 20:36:59 Actually, I was hoping for command_name in these t
Finnur 2012/08/16 23:07:11 Ah, of course. :) Will fix tomorrow morning. On 2
55 type = chrome::NOTIFICATION_EXTENSION_COMMAND_PAGE_ACTION_MAC;
56 } else if (it->second.second == values::kBrowserActionCommandEvent) {
57 type = chrome::NOTIFICATION_EXTENSION_COMMAND_BROWSER_ACTION_MAC;
58 } else if (it->second.second == values::kScriptBadgeCommandEvent) {
59 type = chrome::NOTIFICATION_EXTENSION_COMMAND_SCRIPT_BADGE_MAC;
60 } else {
61 // Not handled by using notifications. Route it through the Browser Event
62 // Router.
63 ExtensionService* service = profile_->GetExtensionService();
64 service->browser_event_router()->CommandExecuted(
65 profile_, extension_id, command_name);
66 return true;
67 }
68
69 std::pair<const std::string, gfx::NativeWindow> details =
70 std::make_pair(extension_id, window_);
71 content::NotificationService::current()->Notify(
72 type,
73 content::Source<Profile>(profile_),
74 content::Details<
75 std::pair<const std::string, gfx::NativeWindow> >(&details));
76 return true;
77 }
78
79 void ExtensionKeybindingRegistryCocoa::AddExtensionKeybinding(
80 const extensions::Extension* extension,
81 const std::string& command_name) {
82 extensions::CommandService* command_service =
83 extensions::CommandServiceFactory::GetForProfile(profile_);
84 extensions::CommandMap commands;
85 command_service->GetNamedCommands(
86 extension->id(),
87 extensions::CommandService::ACTIVE_ONLY,
88 &commands);
89
90 extensions::CommandMap::const_iterator iter = commands.begin();
Scott Hess - ex-Googler 2012/08/16 20:36:59 This usage seems to promise that iter will be used
Finnur 2012/08/16 23:07:11 Yes. On 2012/08/16 20:36:59, shess wrote:
91 for (; iter != commands.end(); ++iter) {
92 if (!command_name.empty() && (iter->second.command_name() != command_name))
93 continue;
94
95 ui::Accelerator accelerator(iter->second.accelerator());
96 event_targets_[accelerator] =
97 std::make_pair(extension->id(), iter->second.command_name());
98 }
99
100 // Mac implemenetation behaves like GTK with regards to what is kept in the
101 // event_targets_ map, because both GTK and Mac need to keep track of Browser
102 // and Page actions, as well as Script Badges.
103 extensions::Command browser_action;
104 if (command_service->GetBrowserActionCommand(
105 extension->id(),
106 extensions::CommandService::ACTIVE_ONLY,
107 &browser_action,
108 NULL)) {
109 ui::Accelerator accelerator(browser_action.accelerator());
110 event_targets_[accelerator] =
111 std::make_pair(extension->id(), browser_action.command_name());
112 }
113
114 // Add the Page Action (if any).
115 extensions::Command page_action;
116 if (command_service->GetPageActionCommand(
117 extension->id(),
118 extensions::CommandService::ACTIVE_ONLY,
119 &page_action,
120 NULL)) {
121 ui::Accelerator accelerator(page_action.accelerator());
122 event_targets_[accelerator] =
123 std::make_pair(extension->id(), page_action.command_name());
124 }
125
126 // Add the Script Badge (if any).
127 extensions::Command script_badge;
128 if (command_service->GetScriptBadgeCommand(
129 extension->id(),
130 extensions::CommandService::ACTIVE_ONLY,
131 &script_badge,
132 NULL)) {
133 ui::Accelerator accelerator(script_badge.accelerator());
134 event_targets_[accelerator] =
135 std::make_pair(extension->id(), script_badge.command_name());
136 }
137 }
138
139 void ExtensionKeybindingRegistryCocoa::RemoveExtensionKeybinding(
140 const extensions::Extension* extension,
141 const std::string& command_name) {
142 EventTargets::iterator iter = event_targets_.begin();
143 while (iter != event_targets_.end()) {
144 EventTargets::iterator old = iter++;
145 if (iter->second.first == extension->id() &&
146 (command_name.empty() || (iter->second.second == command_name)))
Scott Hess - ex-Googler 2012/08/16 20:36:59 This you need the {} because of the multi-line if(
Finnur 2012/08/16 23:07:11 Woops. Thanks! On 2012/08/16 20:36:59, shess wrot
147 event_targets_.erase(old);
148 }
149 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698