| 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/gtk/extensions/extension_keybinding_registry_gtk.h" | 5 #include "chrome/browser/ui/gtk/extensions/extension_keybinding_registry_gtk.h" |
| 6 | 6 |
| 7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
| 8 | 8 |
| 9 #include "chrome/browser/extensions/api/commands/command_service.h" | 9 #include "chrome/browser/extensions/api/commands/command_service.h" |
| 10 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 extensions::CommandService::ACTIVE_ONLY, | 66 extensions::CommandService::ACTIVE_ONLY, |
| 67 extensions::CommandService::REGULAR, | 67 extensions::CommandService::REGULAR, |
| 68 &commands); | 68 &commands); |
| 69 | 69 |
| 70 for (extensions::CommandMap::const_iterator iter = commands.begin(); | 70 for (extensions::CommandMap::const_iterator iter = commands.begin(); |
| 71 iter != commands.end(); ++iter) { | 71 iter != commands.end(); ++iter) { |
| 72 if (!command_name.empty() && (iter->second.command_name() != command_name)) | 72 if (!command_name.empty() && (iter->second.command_name() != command_name)) |
| 73 continue; | 73 continue; |
| 74 | 74 |
| 75 ui::Accelerator accelerator(iter->second.accelerator()); | 75 ui::Accelerator accelerator(iter->second.accelerator()); |
| 76 event_targets_[accelerator] = | 76 event_targets_[accelerator].push_back( |
| 77 std::make_pair(extension->id(), iter->second.command_name()); | 77 std::make_pair(extension->id(), iter->second.command_name())); |
| 78 |
| 79 // Shortcuts except media keys have only one target in the list. See comment |
| 80 // about |event_targets_|. |
| 81 if (!extensions::CommandService::IsMediaKey(iter->second.accelerator())) |
| 82 DCHECK(event_targets_[accelerator].size() == 1); |
| 78 | 83 |
| 79 if (!accel_group_) { | 84 if (!accel_group_) { |
| 80 accel_group_ = gtk_accel_group_new(); | 85 accel_group_ = gtk_accel_group_new(); |
| 81 gtk_window_add_accel_group(window_, accel_group_); | 86 gtk_window_add_accel_group(window_, accel_group_); |
| 82 } | 87 } |
| 83 | 88 |
| 84 gtk_accel_group_connect( | 89 gtk_accel_group_connect( |
| 85 accel_group_, | 90 accel_group_, |
| 86 ui::GetGdkKeyCodeForAccelerator(accelerator), | 91 ui::GetGdkKeyCodeForAccelerator(accelerator), |
| 87 ui::GetGdkModifierForAccelerator(accelerator), | 92 ui::GetGdkModifierForAccelerator(accelerator), |
| 88 GtkAccelFlags(0), | 93 GtkAccelFlags(0), |
| 89 g_cclosure_new(G_CALLBACK(OnGtkAcceleratorThunk), this, NULL)); | 94 g_cclosure_new(G_CALLBACK(OnGtkAcceleratorThunk), this, NULL)); |
| 90 } | 95 } |
| 91 | 96 |
| 92 // Unlike on Windows, we need to explicitly add the browser action and page | 97 // Unlike on Windows, we need to explicitly add the browser action and page |
| 93 // action to the event_targets_, even though we don't register them as | 98 // action to the event_targets_, even though we don't register them as |
| 94 // handlers. See http://crbug.com/124873. | 99 // handlers. See http://crbug.com/124873. |
| 95 extensions::Command browser_action; | 100 extensions::Command browser_action; |
| 96 if (command_service->GetBrowserActionCommand( | 101 if (command_service->GetBrowserActionCommand( |
| 97 extension->id(), | 102 extension->id(), |
| 98 extensions::CommandService::ACTIVE_ONLY, | 103 extensions::CommandService::ACTIVE_ONLY, |
| 99 &browser_action, | 104 &browser_action, |
| 100 NULL)) { | 105 NULL)) { |
| 101 ui::Accelerator accelerator(browser_action.accelerator()); | 106 ui::Accelerator accelerator(browser_action.accelerator()); |
| 102 event_targets_[accelerator] = | 107 event_targets_[accelerator].push_back( |
| 103 std::make_pair(extension->id(), browser_action.command_name()); | 108 std::make_pair(extension->id(), browser_action.command_name())); |
| 109 // We should have only one target. See comment about |event_targets_|. |
| 110 DCHECK(event_targets_[accelerator].size() == 1); |
| 104 } | 111 } |
| 105 | 112 |
| 106 // Add the Page Action (if any). | 113 // Add the Page Action (if any). |
| 107 extensions::Command page_action; | 114 extensions::Command page_action; |
| 108 if (command_service->GetPageActionCommand( | 115 if (command_service->GetPageActionCommand( |
| 109 extension->id(), | 116 extension->id(), |
| 110 extensions::CommandService::ACTIVE_ONLY, | 117 extensions::CommandService::ACTIVE_ONLY, |
| 111 &page_action, | 118 &page_action, |
| 112 NULL)) { | 119 NULL)) { |
| 113 ui::Accelerator accelerator(page_action.accelerator()); | 120 ui::Accelerator accelerator(page_action.accelerator()); |
| 114 event_targets_[accelerator] = | 121 event_targets_[accelerator].push_back( |
| 115 std::make_pair(extension->id(), page_action.command_name()); | 122 std::make_pair(extension->id(), page_action.command_name())); |
| 123 DCHECK(event_targets_[accelerator].size() == 1); // Ditto. |
| 116 } | 124 } |
| 117 | 125 |
| 118 // Add the Script Badge (if any). | 126 // Add the Script Badge (if any). |
| 119 extensions::Command script_badge; | 127 extensions::Command script_badge; |
| 120 if (command_service->GetScriptBadgeCommand( | 128 if (command_service->GetScriptBadgeCommand( |
| 121 extension->id(), | 129 extension->id(), |
| 122 extensions::CommandService::ACTIVE_ONLY, | 130 extensions::CommandService::ACTIVE_ONLY, |
| 123 &script_badge, | 131 &script_badge, |
| 124 NULL)) { | 132 NULL)) { |
| 125 ui::Accelerator accelerator(script_badge.accelerator()); | 133 ui::Accelerator accelerator(script_badge.accelerator()); |
| 126 event_targets_[accelerator] = | 134 event_targets_[accelerator].push_back( |
| 127 std::make_pair(extension->id(), script_badge.command_name()); | 135 std::make_pair(extension->id(), script_badge.command_name())); |
| 136 DCHECK(event_targets_[accelerator].size() == 1); // Ditto. |
| 128 } | 137 } |
| 129 } | 138 } |
| 130 | 139 |
| 131 void ExtensionKeybindingRegistryGtk::RemoveExtensionKeybindingImpl( | 140 void ExtensionKeybindingRegistryGtk::RemoveExtensionKeybindingImpl( |
| 132 const ui::Accelerator& accelerator, | 141 const ui::Accelerator& accelerator, |
| 133 const std::string& command_name) { | 142 const std::string& command_name) { |
| 134 // On GTK, unlike Windows, the Event Targets contain all events but we must | 143 // On GTK, unlike Windows, the Event Targets contain all events but we must |
| 135 // only unregister the ones we registered targets for. | 144 // only unregister the ones we registered targets for. |
| 136 if (!ShouldIgnoreCommand(command_name)) { | 145 if (!ShouldIgnoreCommand(command_name)) { |
| 137 gtk_accel_group_disconnect_key( | 146 gtk_accel_group_disconnect_key( |
| 138 accel_group_, | 147 accel_group_, |
| 139 ui::GetGdkKeyCodeForAccelerator(accelerator), | 148 ui::GetGdkKeyCodeForAccelerator(accelerator), |
| 140 ui::GetGdkModifierForAccelerator(accelerator)); | 149 ui::GetGdkModifierForAccelerator(accelerator)); |
| 141 } | 150 } |
| 142 } | 151 } |
| 143 | 152 |
| 144 gboolean ExtensionKeybindingRegistryGtk::OnGtkAccelerator( | 153 gboolean ExtensionKeybindingRegistryGtk::OnGtkAccelerator( |
| 145 GtkAccelGroup* group, | 154 GtkAccelGroup* group, |
| 146 GObject* acceleratable, | 155 GObject* acceleratable, |
| 147 guint keyval, | 156 guint keyval, |
| 148 GdkModifierType modifier) { | 157 GdkModifierType modifier) { |
| 149 ui::Accelerator accelerator = ui::AcceleratorForGdkKeyCodeAndModifier( | 158 ui::Accelerator accelerator = ui::AcceleratorForGdkKeyCodeAndModifier( |
| 150 keyval, modifier); | 159 keyval, modifier); |
| 151 | 160 |
| 152 EventTargets::iterator it = event_targets_.find(accelerator); | 161 return ExtensionKeybindingRegistry::NotifyEventTargets(accelerator); |
| 153 if (it == event_targets_.end()) { | |
| 154 NOTREACHED(); // Shouldn't get this event for something not registered. | |
| 155 return FALSE; | |
| 156 } | |
| 157 | |
| 158 CommandExecuted(it->second.first, it->second.second); | |
| 159 return TRUE; | |
| 160 } | 162 } |
| OLD | NEW |