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.
5 #include "content/browser/renderer_host/gtk_plugin_container_manager.h"
9 #include "base/logging.h"
10 #include "content/browser/renderer_host/gtk_plugin_container.h"
11 #include "content/common/webplugin_geometry.h"
12 #include "ui/gfx/gtk_compat.h"
13 #include "ui/gfx/gtk_util.h"
17 GtkPluginContainerManager::GtkPluginContainerManager() : host_widget_(NULL
) {}
19 GtkPluginContainerManager::~GtkPluginContainerManager() {}
21 GtkWidget
* GtkPluginContainerManager::CreatePluginContainer(
22 gfx::PluginWindowHandle id
) {
24 GtkWidget
*widget
= gtk_plugin_container_new();
25 plugin_window_to_widget_map_
.insert(std::make_pair(id
, widget
));
27 // The Realize callback is responsible for adding the plug into the socket.
28 // The reason is 2-fold:
29 // - the plug can't be added until the socket is realized, but this may not
30 // happen until the socket is attached to a top-level window, which isn't the
31 // case for background tabs.
32 // - when dragging tabs, the socket gets unrealized, which breaks the XEMBED
33 // connection. We need to make it again when the tab is reattached, and the
34 // socket gets realized again.
36 // Note, the RealizeCallback relies on the plugin_window_to_widget_map_ to
38 g_signal_connect(widget
, "realize",
39 G_CALLBACK(RealizeCallback
), this);
41 // Don't destroy the widget when the plug is removed.
42 g_signal_connect(widget
, "plug-removed",
43 G_CALLBACK(gtk_true
), NULL
);
45 gtk_container_add(GTK_CONTAINER(host_widget_
), widget
);
46 gtk_widget_show(widget
);
51 void GtkPluginContainerManager::DestroyPluginContainer(
52 gfx::PluginWindowHandle id
) {
54 GtkWidget
* widget
= MapIDToWidget(id
);
56 gtk_widget_destroy(widget
);
58 plugin_window_to_widget_map_
.erase(id
);
61 void GtkPluginContainerManager::MovePluginContainer(
62 const WebPluginGeometry
& move
) {
64 GtkWidget
*widget
= MapIDToWidget(move
.window
);
68 DCHECK(gtk_widget_get_has_window(widget
));
71 gtk_widget_hide(widget
);
75 gtk_widget_show(widget
);
77 if (!move
.rects_valid
)
80 // TODO(piman): if the widget hasn't been realized (e.g. the tab has been
81 // torn off and the parent gtk widget has been detached from the hierarchy),
82 // we lose the cutout information.
83 if (gtk_widget_get_realized(widget
)) {
84 GdkRectangle clip_rect
= move
.clip_rect
.ToGdkRectangle();
85 GdkRegion
* clip_region
= gdk_region_rectangle(&clip_rect
);
86 gfx::SubtractRectanglesFromRegion(clip_region
, move
.cutout_rects
);
87 gdk_window_shape_combine_region(gtk_widget_get_window(widget
),
89 gdk_region_destroy(clip_region
);
92 // Update the window position. Resizing is handled by WebPluginDelegate.
93 // TODO(deanm): Verify that we only need to move and not resize.
94 // TODO(evanm): we should cache the last shape and position and skip all
95 // of this business in the common case where nothing has changed.
96 int current_x
, current_y
;
98 // Until the above TODO is resolved, we can grab the last position
99 // off of the GtkFixed with a bit of hackery.
101 g_value_init(&value
, G_TYPE_INT
);
102 gtk_container_child_get_property(GTK_CONTAINER(host_widget_
), widget
,
104 current_x
= g_value_get_int(&value
);
105 gtk_container_child_get_property(GTK_CONTAINER(host_widget_
), widget
,
107 current_y
= g_value_get_int(&value
);
108 g_value_unset(&value
);
110 if (move
.window_rect
.x() != current_x
||
111 move
.window_rect
.y() != current_y
) {
112 // Calling gtk_fixed_move unnecessarily is a no-no, as it causes the
113 // parent window to repaint!
114 gtk_fixed_move(GTK_FIXED(host_widget_
),
116 move
.window_rect
.x(),
117 move
.window_rect
.y());
120 gtk_plugin_container_set_size(widget
,
121 move
.window_rect
.width(),
122 move
.window_rect
.height());
125 GtkWidget
* GtkPluginContainerManager::MapIDToWidget(
126 gfx::PluginWindowHandle id
) {
127 PluginWindowToWidgetMap::const_iterator i
=
128 plugin_window_to_widget_map_
.find(id
);
129 if (i
!= plugin_window_to_widget_map_
.end())
132 LOG(ERROR
) << "Request for widget host for unknown window id " << id
;
137 gfx::PluginWindowHandle
GtkPluginContainerManager::MapWidgetToID(
139 for (PluginWindowToWidgetMap::const_iterator i
=
140 plugin_window_to_widget_map_
.begin();
141 i
!= plugin_window_to_widget_map_
.end(); ++i
) {
142 if (i
->second
== widget
)
146 LOG(ERROR
) << "Request for id for unknown widget";
151 void GtkPluginContainerManager::RealizeCallback(GtkWidget
* widget
,
153 GtkPluginContainerManager
* plugin_container_manager
=
154 static_cast<GtkPluginContainerManager
*>(user_data
);
156 gfx::PluginWindowHandle id
= plugin_container_manager
->MapWidgetToID(widget
);
158 gtk_socket_add_id(GTK_SOCKET(widget
), id
);
161 } // namespace content