1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/gtk/extensions/extension_keybinding_registry_gtk.h"
#include "chrome/browser/extensions/extension_browser_event_router.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/extension.h"
ExtensionKeybindingRegistryGtk::ExtensionKeybindingRegistryGtk(
Profile* profile, gfx::NativeWindow window)
: ExtensionKeybindingRegistry(profile),
profile_(profile),
window_(window),
accel_group_(NULL) {
Init();
}
ExtensionKeybindingRegistryGtk::~ExtensionKeybindingRegistryGtk() {
if (accel_group_) {
gtk_accel_group_disconnect(accel_group_,
NULL); // Remove all closures.
event_targets_.clear();
gtk_window_remove_accel_group(window_, accel_group_);
g_object_unref(accel_group_);
accel_group_ = NULL;
}
}
gboolean ExtensionKeybindingRegistryGtk::HasPriorityHandler(
const GdkEventKey* event) const {
ui::AcceleratorGtk accelerator(ui::WindowsKeyCodeForGdkKeyCode(event->keyval),
event->state & GDK_SHIFT_MASK,
event->state & GDK_CONTROL_MASK,
event->state & GDK_MOD1_MASK);
return event_targets_.find(accelerator) != event_targets_.end();
}
void ExtensionKeybindingRegistryGtk::AddExtensionKeybinding(
const Extension* extension) {
// Add all the keybindings (except pageAction and browserAction, which are
// handled elsewhere).
const Extension::CommandMap& commands = extension->named_commands();
Extension::CommandMap::const_iterator iter = commands.begin();
for (; iter != commands.end(); ++iter) {
ui::AcceleratorGtk accelerator(iter->second.accelerator().key_code(),
iter->second.accelerator().IsShiftDown(),
iter->second.accelerator().IsCtrlDown(),
iter->second.accelerator().IsAltDown());
event_targets_[accelerator] =
std::make_pair(extension->id(), iter->second.command_name());
if (ShouldIgnoreCommand(iter->second.command_name()))
continue;
if (!accel_group_) {
accel_group_ = gtk_accel_group_new();
gtk_window_add_accel_group(window_, accel_group_);
}
gtk_accel_group_connect(
accel_group_,
accelerator.GetGdkKeyCode(),
accelerator.gdk_modifier_type(),
GtkAccelFlags(0),
g_cclosure_new(G_CALLBACK(OnGtkAcceleratorThunk), this, NULL));
}
}
void ExtensionKeybindingRegistryGtk::RemoveExtensionKeybinding(
const Extension* extension) {
EventTargets::iterator iter = event_targets_.begin();
while (iter != event_targets_.end()) {
if (iter->second.first != extension->id()) {
++iter;
continue; // Not the extension we asked for.
}
// On GTK, unlike Windows, the Event Targets contain all events.
if (ShouldIgnoreCommand(iter->second.second)) {
++iter;
continue;
}
gtk_accel_group_disconnect_key(accel_group_,
iter->first.GetGdkKeyCode(),
iter->first.gdk_modifier_type());
EventTargets::iterator old = iter++;
event_targets_.erase(old);
}
}
gboolean ExtensionKeybindingRegistryGtk::OnGtkAccelerator(
GtkAccelGroup* group,
GObject* acceleratable,
guint keyval,
GdkModifierType modifier) {
ui::AcceleratorGtk accelerator(keyval, modifier);
ExtensionService* service = profile_->GetExtensionService();
EventTargets::iterator it = event_targets_.find(accelerator);
if (it == event_targets_.end()) {
NOTREACHED(); // Shouldn't get this event for something not registered.
return FALSE;
}
service->browser_event_router()->CommandExecuted(
profile_, it->second.first, it->second.second);
return TRUE;
}
|