- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / gtk_plugin_container_manager.cc
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 "content/browser/renderer_host/gtk_plugin_container_manager.h"
6
7 #include <gtk/gtk.h>
8
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"
14
15 namespace content {
16
17 GtkPluginContainerManager::GtkPluginContainerManager() : host_widget_(NULL) {}
18
19 GtkPluginContainerManager::~GtkPluginContainerManager() {}
20
21 GtkWidget* GtkPluginContainerManager::CreatePluginContainer(
22     gfx::PluginWindowHandle id) {
23   DCHECK(host_widget_);
24   GtkWidget *widget = gtk_plugin_container_new();
25   plugin_window_to_widget_map_.insert(std::make_pair(id, widget));
26
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.
35   //
36   // Note, the RealizeCallback relies on the plugin_window_to_widget_map_ to
37   // have the mapping.
38   g_signal_connect(widget, "realize",
39                    G_CALLBACK(RealizeCallback), this);
40
41   // Don't destroy the widget when the plug is removed.
42   g_signal_connect(widget, "plug-removed",
43                    G_CALLBACK(gtk_true), NULL);
44
45   gtk_container_add(GTK_CONTAINER(host_widget_), widget);
46   gtk_widget_show(widget);
47
48   return widget;
49 }
50
51 void GtkPluginContainerManager::DestroyPluginContainer(
52     gfx::PluginWindowHandle id) {
53   DCHECK(host_widget_);
54   GtkWidget* widget = MapIDToWidget(id);
55   if (widget)
56     gtk_widget_destroy(widget);
57
58   plugin_window_to_widget_map_.erase(id);
59 }
60
61 void GtkPluginContainerManager::MovePluginContainer(
62     const WebPluginGeometry& move) {
63   DCHECK(host_widget_);
64   GtkWidget *widget = MapIDToWidget(move.window);
65   if (!widget)
66     return;
67
68   DCHECK(gtk_widget_get_has_window(widget));
69
70   if (!move.visible) {
71     gtk_widget_hide(widget);
72     return;
73   }
74
75   gtk_widget_show(widget);
76
77   if (!move.rects_valid)
78     return;
79
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),
88                                     clip_region, 0, 0);
89     gdk_region_destroy(clip_region);
90   }
91
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;
97
98   // Until the above TODO is resolved, we can grab the last position
99   // off of the GtkFixed with a bit of hackery.
100   GValue value = {0};
101   g_value_init(&value, G_TYPE_INT);
102   gtk_container_child_get_property(GTK_CONTAINER(host_widget_), widget,
103                                    "x", &value);
104   current_x = g_value_get_int(&value);
105   gtk_container_child_get_property(GTK_CONTAINER(host_widget_), widget,
106                                    "y", &value);
107   current_y = g_value_get_int(&value);
108   g_value_unset(&value);
109
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_),
115                    widget,
116                    move.window_rect.x(),
117                    move.window_rect.y());
118   }
119
120   gtk_plugin_container_set_size(widget,
121                                 move.window_rect.width(),
122                                 move.window_rect.height());
123 }
124
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())
130     return i->second;
131
132   LOG(ERROR) << "Request for widget host for unknown window id " << id;
133
134   return NULL;
135 }
136
137 gfx::PluginWindowHandle GtkPluginContainerManager::MapWidgetToID(
138      GtkWidget* widget) {
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)
143       return i->first;
144   }
145
146   LOG(ERROR) << "Request for id for unknown widget";
147   return 0;
148 }
149
150 // static
151 void GtkPluginContainerManager::RealizeCallback(GtkWidget* widget,
152                                                 void* user_data) {
153   GtkPluginContainerManager* plugin_container_manager =
154       static_cast<GtkPluginContainerManager*>(user_data);
155
156   gfx::PluginWindowHandle id = plugin_container_manager->MapWidgetToID(widget);
157   if (id)
158     gtk_socket_add_id(GTK_SOCKET(widget), id);
159 }
160
161 }  // namespace content