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 "webkit/plugins/npapi/gtk_plugin_container_manager.h" | |
6 | |
7 #include <gtk/gtk.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "ui/base/gtk/gtk_compat.h" | |
11 #include "ui/gfx/gtk_util.h" | |
12 #include "webkit/plugins/npapi/gtk_plugin_container.h" | |
13 #include "webkit/plugins/npapi/webplugin.h" | |
14 | |
15 namespace webkit { | |
16 namespace npapi { | |
17 | |
18 GtkPluginContainerManager::GtkPluginContainerManager() : host_widget_(NULL) {} | |
19 | |
20 GtkPluginContainerManager::~GtkPluginContainerManager() {} | |
21 | |
22 GtkWidget* GtkPluginContainerManager::CreatePluginContainer( | |
23 gfx::PluginWindowHandle id) { | |
24 DCHECK(host_widget_); | |
25 GtkWidget *widget = gtk_plugin_container_new(); | |
26 plugin_window_to_widget_map_.insert(std::make_pair(id, widget)); | |
27 | |
28 // The Realize callback is responsible for adding the plug into the socket. | |
29 // The reason is 2-fold: | |
30 // - the plug can't be added until the socket is realized, but this may not | |
31 // happen until the socket is attached to a top-level window, which isn't the | |
32 // case for background tabs. | |
33 // - when dragging tabs, the socket gets unrealized, which breaks the XEMBED | |
34 // connection. We need to make it again when the tab is reattached, and the | |
35 // socket gets realized again. | |
36 // | |
37 // Note, the RealizeCallback relies on the plugin_window_to_widget_map_ to | |
38 // have the mapping. | |
39 g_signal_connect(widget, "realize", | |
40 G_CALLBACK(RealizeCallback), this); | |
41 | |
42 // Don't destroy the widget when the plug is removed. | |
43 g_signal_connect(widget, "plug-removed", | |
44 G_CALLBACK(gtk_true), NULL); | |
45 | |
46 gtk_container_add(GTK_CONTAINER(host_widget_), widget); | |
47 gtk_widget_show(widget); | |
48 | |
49 return widget; | |
50 } | |
51 | |
52 void GtkPluginContainerManager::DestroyPluginContainer( | |
53 gfx::PluginWindowHandle id) { | |
54 DCHECK(host_widget_); | |
55 GtkWidget* widget = MapIDToWidget(id); | |
56 if (widget) | |
57 gtk_widget_destroy(widget); | |
58 | |
59 plugin_window_to_widget_map_.erase(id); | |
60 } | |
61 | |
62 void GtkPluginContainerManager::MovePluginContainer( | |
63 const WebPluginGeometry& move) { | |
64 DCHECK(host_widget_); | |
65 GtkWidget *widget = MapIDToWidget(move.window); | |
66 if (!widget) | |
67 return; | |
68 | |
69 DCHECK(gtk_widget_get_has_window(widget)); | |
70 | |
71 if (!move.visible) { | |
72 gtk_widget_hide(widget); | |
73 return; | |
74 } | |
75 | |
76 gtk_widget_show(widget); | |
77 | |
78 if (!move.rects_valid) | |
79 return; | |
80 | |
81 // TODO(piman): if the widget hasn't been realized (e.g. the tab has been | |
82 // torn off and the parent gtk widget has been detached from the hierarchy), | |
83 // we lose the cutout information. | |
84 if (gtk_widget_get_realized(widget)) { | |
85 GdkRectangle clip_rect = move.clip_rect.ToGdkRectangle(); | |
86 GdkRegion* clip_region = gdk_region_rectangle(&clip_rect); | |
87 gfx::SubtractRectanglesFromRegion(clip_region, move.cutout_rects); | |
88 gdk_window_shape_combine_region(gtk_widget_get_window(widget), | |
89 clip_region, 0, 0); | |
90 gdk_region_destroy(clip_region); | |
91 } | |
92 | |
93 // Update the window position. Resizing is handled by WebPluginDelegate. | |
94 // TODO(deanm): Verify that we only need to move and not resize. | |
95 // TODO(evanm): we should cache the last shape and position and skip all | |
96 // of this business in the common case where nothing has changed. | |
97 int current_x, current_y; | |
98 | |
99 // Until the above TODO is resolved, we can grab the last position | |
100 // off of the GtkFixed with a bit of hackery. | |
101 GValue value = {0}; | |
102 g_value_init(&value, G_TYPE_INT); | |
103 gtk_container_child_get_property(GTK_CONTAINER(host_widget_), widget, | |
104 "x", &value); | |
105 current_x = g_value_get_int(&value); | |
106 gtk_container_child_get_property(GTK_CONTAINER(host_widget_), widget, | |
107 "y", &value); | |
108 current_y = g_value_get_int(&value); | |
109 g_value_unset(&value); | |
110 | |
111 if (move.window_rect.x() != current_x || | |
112 move.window_rect.y() != current_y) { | |
113 // Calling gtk_fixed_move unnecessarily is a no-no, as it causes the | |
114 // parent window to repaint! | |
115 gtk_fixed_move(GTK_FIXED(host_widget_), | |
116 widget, | |
117 move.window_rect.x(), | |
118 move.window_rect.y()); | |
119 } | |
120 | |
121 gtk_plugin_container_set_size(widget, | |
122 move.window_rect.width(), | |
123 move.window_rect.height()); | |
124 } | |
125 | |
126 GtkWidget* GtkPluginContainerManager::MapIDToWidget( | |
127 gfx::PluginWindowHandle id) { | |
128 PluginWindowToWidgetMap::const_iterator i = | |
129 plugin_window_to_widget_map_.find(id); | |
130 if (i != plugin_window_to_widget_map_.end()) | |
131 return i->second; | |
132 | |
133 LOG(ERROR) << "Request for widget host for unknown window id " << id; | |
134 | |
135 return NULL; | |
136 } | |
137 | |
138 gfx::PluginWindowHandle GtkPluginContainerManager::MapWidgetToID( | |
139 GtkWidget* widget) { | |
140 for (PluginWindowToWidgetMap::const_iterator i = | |
141 plugin_window_to_widget_map_.begin(); | |
142 i != plugin_window_to_widget_map_.end(); ++i) { | |
143 if (i->second == widget) | |
144 return i->first; | |
145 } | |
146 | |
147 LOG(ERROR) << "Request for id for unknown widget"; | |
148 return 0; | |
149 } | |
150 | |
151 // static | |
152 void GtkPluginContainerManager::RealizeCallback(GtkWidget* widget, | |
153 void* user_data) { | |
154 GtkPluginContainerManager* plugin_container_manager = | |
155 static_cast<GtkPluginContainerManager*>(user_data); | |
156 | |
157 gfx::PluginWindowHandle id = plugin_container_manager->MapWidgetToID(widget); | |
158 if (id) | |
159 gtk_socket_add_id(GTK_SOCKET(widget), id); | |
160 } | |
161 | |
162 } // namespace npapi | |
163 } // namespace webkit | |
OLD | NEW |