Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/cocoa/extensions/extension_keybinding_registry_cocoa .h" | 5 #include "chrome/browser/ui/cocoa/extensions/extension_keybinding_registry_cocoa .h" |
| 6 | 6 |
| 7 #include "chrome/browser/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
| 8 #include "chrome/browser/extensions/api/commands/command_service.h" | 8 #include "chrome/browser/extensions/api/commands/command_service.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 38 } | 38 } |
| 39 | 39 |
| 40 bool ExtensionKeybindingRegistryCocoa::ProcessKeyEvent( | 40 bool ExtensionKeybindingRegistryCocoa::ProcessKeyEvent( |
| 41 const content::NativeWebKeyboardEvent& event) { | 41 const content::NativeWebKeyboardEvent& event) { |
| 42 if (shortcut_handling_suspended_) | 42 if (shortcut_handling_suspended_) |
| 43 return false; | 43 return false; |
| 44 | 44 |
| 45 ui::Accelerator accelerator( | 45 ui::Accelerator accelerator( |
| 46 static_cast<ui::KeyboardCode>(event.windowsKeyCode), | 46 static_cast<ui::KeyboardCode>(event.windowsKeyCode), |
| 47 content::GetModifiersFromNativeWebKeyboardEvent(event)); | 47 content::GetModifiersFromNativeWebKeyboardEvent(event)); |
| 48 EventTargets::iterator it = event_targets_.find(accelerator); | 48 EventTargets::iterator targets = event_targets_.find(accelerator); |
| 49 if (it == event_targets_.end()) | 49 if (targets == event_targets_.end()) |
| 50 return false; | 50 return false; |
| 51 | 51 |
| 52 std::string extension_id = it->second.first; | 52 TargetList::const_iterator first_target = targets->second.begin(); |
| 53 std::string command_name = it->second.second; | 53 if (first_target == targets->second.end()) |
| 54 return false; | |
| 55 | |
| 56 std::string extension_id = first_target->first; | |
| 57 std::string command_name = first_target->second; | |
| 54 int type = 0; | 58 int type = 0; |
| 55 if (command_name == values::kPageActionCommandEvent) { | 59 if (command_name == values::kPageActionCommandEvent) { |
| 56 type = chrome::NOTIFICATION_EXTENSION_COMMAND_PAGE_ACTION_MAC; | 60 type = chrome::NOTIFICATION_EXTENSION_COMMAND_PAGE_ACTION_MAC; |
| 57 } else if (command_name == values::kBrowserActionCommandEvent) { | 61 } else if (command_name == values::kBrowserActionCommandEvent) { |
| 58 type = chrome::NOTIFICATION_EXTENSION_COMMAND_BROWSER_ACTION_MAC; | 62 type = chrome::NOTIFICATION_EXTENSION_COMMAND_BROWSER_ACTION_MAC; |
| 59 } else if (command_name == values::kScriptBadgeCommandEvent) { | 63 } else if (command_name == values::kScriptBadgeCommandEvent) { |
| 60 type = chrome::NOTIFICATION_EXTENSION_COMMAND_SCRIPT_BADGE_MAC; | 64 type = chrome::NOTIFICATION_EXTENSION_COMMAND_SCRIPT_BADGE_MAC; |
| 61 } else { | 65 } else { |
| 62 // Not handled by using notifications. Route it through the Browser Event | 66 // Not handled by using notifications. Route it through the Browser Event |
| 63 // Router. | 67 // Router using the base class (it will iterate through all targets). |
| 64 CommandExecuted(extension_id, command_name); | 68 return ExtensionKeybindingRegistry::NotifyEventTargetsByAccelerator( |
| 65 return true; | 69 accelerator); |
| 66 } | 70 } |
| 67 | 71 |
| 72 // type != named command, so we need to dispatch this event directly. | |
|
Finnur
2013/11/18 11:16:14
nit: Capitalize 'type'.
zhchbin
2013/11/18 13:10:13
Done.
| |
| 68 std::pair<const std::string, gfx::NativeWindow> details = | 73 std::pair<const std::string, gfx::NativeWindow> details = |
| 69 std::make_pair(extension_id, window_); | 74 std::make_pair(extension_id, window_); |
| 70 content::NotificationService::current()->Notify( | 75 content::NotificationService::current()->Notify( |
| 71 type, | 76 type, |
| 72 content::Source<Profile>(profile_), | 77 content::Source<Profile>(profile_), |
| 73 content::Details< | 78 content::Details< |
| 74 std::pair<const std::string, gfx::NativeWindow> >(&details)); | 79 std::pair<const std::string, gfx::NativeWindow> >(&details)); |
| 80 // We expect only one target for these types of events. | |
| 81 DCHECK(++first_target == targets->second.end()); | |
| 75 return true; | 82 return true; |
| 76 } | 83 } |
| 77 | 84 |
| 78 void ExtensionKeybindingRegistryCocoa::AddExtensionKeybinding( | 85 void ExtensionKeybindingRegistryCocoa::AddExtensionKeybinding( |
| 79 const extensions::Extension* extension, | 86 const extensions::Extension* extension, |
| 80 const std::string& command_name) { | 87 const std::string& command_name) { |
| 81 extensions::CommandService* command_service = | 88 extensions::CommandService* command_service = |
| 82 extensions::CommandService::Get(profile_); | 89 extensions::CommandService::Get(profile_); |
| 83 extensions::CommandMap commands; | 90 extensions::CommandMap commands; |
| 84 command_service->GetNamedCommands( | 91 command_service->GetNamedCommands( |
| 85 extension->id(), | 92 extension->id(), |
| 86 extensions::CommandService::ACTIVE_ONLY, | 93 extensions::CommandService::ACTIVE_ONLY, |
| 87 extensions::CommandService::REGULAR, | 94 extensions::CommandService::REGULAR, |
| 88 &commands); | 95 &commands); |
| 89 | 96 |
| 90 for (extensions::CommandMap::const_iterator iter = commands.begin(); | 97 for (extensions::CommandMap::const_iterator iter = commands.begin(); |
| 91 iter != commands.end(); ++iter) { | 98 iter != commands.end(); ++iter) { |
| 92 if (!command_name.empty() && (iter->second.command_name() != command_name)) | 99 if (!command_name.empty() && (iter->second.command_name() != command_name)) |
| 93 continue; | 100 continue; |
| 94 | 101 |
| 95 ui::Accelerator accelerator(iter->second.accelerator()); | 102 ui::Accelerator accelerator(iter->second.accelerator()); |
| 96 event_targets_[accelerator] = | 103 event_targets_[accelerator].push_back( |
| 97 std::make_pair(extension->id(), iter->second.command_name()); | 104 std::make_pair(extension->id(), iter->second.command_name())); |
| 105 // Shortcuts except media keys have only one target in the list. See | |
| 106 // comment about |event_targets_|. | |
| 107 if (!extensions::CommandService::IsMediaKey(iter->second.accelerator())) | |
| 108 DCHECK(event_targets_[iter->second.accelerator()].size() == 1); | |
| 98 } | 109 } |
| 99 | 110 |
| 100 // Mac implemenetation behaves like GTK with regards to what is kept in the | 111 // 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 | 112 // event_targets_ map, because both GTK and Mac need to keep track of Browser |
| 102 // and Page actions, as well as Script Badges. | 113 // and Page actions, as well as Script Badges. |
| 103 extensions::Command browser_action; | 114 extensions::Command browser_action; |
| 104 if (command_service->GetBrowserActionCommand( | 115 if (command_service->GetBrowserActionCommand( |
| 105 extension->id(), | 116 extension->id(), |
| 106 extensions::CommandService::ACTIVE_ONLY, | 117 extensions::CommandService::ACTIVE_ONLY, |
| 107 &browser_action, | 118 &browser_action, |
| 108 NULL)) { | 119 NULL)) { |
| 109 ui::Accelerator accelerator(browser_action.accelerator()); | 120 ui::Accelerator accelerator(browser_action.accelerator()); |
| 110 event_targets_[accelerator] = | 121 event_targets_[accelerator].push_back( |
| 111 std::make_pair(extension->id(), browser_action.command_name()); | 122 std::make_pair(extension->id(), browser_action.command_name())); |
| 123 // We should have only one target. See comment about |event_targets_|. | |
| 124 DCHECK(event_targets_[accelerator].size() == 1); | |
| 112 } | 125 } |
| 113 | 126 |
| 114 // Add the Page Action (if any). | 127 // Add the Page Action (if any). |
| 115 extensions::Command page_action; | 128 extensions::Command page_action; |
| 116 if (command_service->GetPageActionCommand( | 129 if (command_service->GetPageActionCommand( |
| 117 extension->id(), | 130 extension->id(), |
| 118 extensions::CommandService::ACTIVE_ONLY, | 131 extensions::CommandService::ACTIVE_ONLY, |
| 119 &page_action, | 132 &page_action, |
| 120 NULL)) { | 133 NULL)) { |
| 121 ui::Accelerator accelerator(page_action.accelerator()); | 134 ui::Accelerator accelerator(page_action.accelerator()); |
| 122 event_targets_[accelerator] = | 135 event_targets_[accelerator].push_back( |
| 123 std::make_pair(extension->id(), page_action.command_name()); | 136 std::make_pair(extension->id(), page_action.command_name())); |
| 137 DCHECK(event_targets_[accelerator].size() == 1); // Ditto. | |
| 124 } | 138 } |
| 125 | 139 |
| 126 // Add the Script Badge (if any). | 140 // Add the Script Badge (if any). |
| 127 extensions::Command script_badge; | 141 extensions::Command script_badge; |
| 128 if (command_service->GetScriptBadgeCommand( | 142 if (command_service->GetScriptBadgeCommand( |
| 129 extension->id(), | 143 extension->id(), |
| 130 extensions::CommandService::ACTIVE_ONLY, | 144 extensions::CommandService::ACTIVE_ONLY, |
| 131 &script_badge, | 145 &script_badge, |
| 132 NULL)) { | 146 NULL)) { |
| 133 ui::Accelerator accelerator(script_badge.accelerator()); | 147 ui::Accelerator accelerator(script_badge.accelerator()); |
| 134 event_targets_[accelerator] = | 148 event_targets_[accelerator].push_back( |
| 135 std::make_pair(extension->id(), script_badge.command_name()); | 149 std::make_pair(extension->id(), script_badge.command_name())); |
| 150 DCHECK(event_targets_[accelerator].size() == 1); // Ditto. | |
| 136 } | 151 } |
| 137 } | 152 } |
| 138 | 153 |
| 139 void ExtensionKeybindingRegistryCocoa::RemoveExtensionKeybindingImpl( | 154 void ExtensionKeybindingRegistryCocoa::RemoveExtensionKeybindingImpl( |
| 140 const ui::Accelerator& accelerator, | 155 const ui::Accelerator& accelerator, |
| 141 const std::string& command_name) { | 156 const std::string& command_name) { |
| 142 } | 157 } |
| OLD | NEW |