OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "gfx/gtk_native_view_id_manager.h" | 5 #include "gfx/gtk_native_view_id_manager.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 #include <gdk/gdkx.h> | 8 #include <gdk/gdkx.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 | 56 |
57 gfx::NativeViewId new_id = | 57 gfx::NativeViewId new_id = |
58 static_cast<gfx::NativeViewId>(base::RandUint64()); | 58 static_cast<gfx::NativeViewId>(base::RandUint64()); |
59 while (id_to_info_.find(new_id) != id_to_info_.end()) | 59 while (id_to_info_.find(new_id) != id_to_info_.end()) |
60 new_id = static_cast<gfx::NativeViewId>(base::RandUint64()); | 60 new_id = static_cast<gfx::NativeViewId>(base::RandUint64()); |
61 | 61 |
62 NativeViewInfo info; | 62 NativeViewInfo info; |
63 info.widget = widget; | 63 info.widget = widget; |
64 if (GTK_WIDGET_REALIZED(widget)) { | 64 if (GTK_WIDGET_REALIZED(widget)) { |
65 GdkWindow *gdk_window = widget->window; | 65 GdkWindow *gdk_window = widget->window; |
66 CHECK(gdk_window); | 66 DCHECK(gdk_window); |
67 info.x_window_id = GDK_WINDOW_XID(gdk_window); | 67 info.x_window_id = GDK_WINDOW_XID(gdk_window); |
68 } | 68 } |
69 | 69 |
70 native_view_to_id_[widget] = new_id; | 70 native_view_to_id_[widget] = new_id; |
71 id_to_info_[new_id] = info; | 71 id_to_info_[new_id] = info; |
72 | 72 |
73 g_signal_connect(widget, "realize", G_CALLBACK(::OnRealize), this); | 73 g_signal_connect(widget, "realize", G_CALLBACK(::OnRealize), this); |
74 g_signal_connect(widget, "unrealize", G_CALLBACK(::OnUnrealize), this); | 74 g_signal_connect(widget, "unrealize", G_CALLBACK(::OnUnrealize), this); |
75 g_signal_connect(widget, "destroy", G_CALLBACK(::OnDestroy), this); | 75 g_signal_connect(widget, "destroy", G_CALLBACK(::OnDestroy), this); |
76 | 76 |
(...skipping 18 matching lines...) Expand all Loading... |
95 AutoLock locked(lock_); | 95 AutoLock locked(lock_); |
96 | 96 |
97 std::map<gfx::NativeViewId, NativeViewInfo>::iterator i = | 97 std::map<gfx::NativeViewId, NativeViewInfo>::iterator i = |
98 id_to_info_.find(id); | 98 id_to_info_.find(id); |
99 | 99 |
100 if (i == id_to_info_.end()) | 100 if (i == id_to_info_.end()) |
101 return false; | 101 return false; |
102 | 102 |
103 // We only return permanent XIDs for widgets that allow us to guarantee that | 103 // We only return permanent XIDs for widgets that allow us to guarantee that |
104 // the XID will not change. | 104 // the XID will not change. |
105 if (!GTK_IS_PRESERVE_WINDOW(i->second.widget)) | 105 DCHECK(GTK_IS_PRESERVE_WINDOW(i->second.widget)); |
106 return false; | |
107 | |
108 GtkPreserveWindow* widget = | 106 GtkPreserveWindow* widget = |
109 reinterpret_cast<GtkPreserveWindow*>(i->second.widget); | 107 reinterpret_cast<GtkPreserveWindow*>(i->second.widget); |
110 gtk_preserve_window_set_preserve(widget, TRUE); | 108 gtk_preserve_window_set_preserve(widget, TRUE); |
111 | 109 |
112 *output = GDK_WINDOW_XID(i->second.widget->window); | 110 *output = GDK_WINDOW_XID(i->second.widget->window); |
| 111 |
| 112 // Update the reference count on the permanent XID. |
| 113 PermanentXIDInfo info; |
| 114 info.widget = widget; |
| 115 info.ref_count = 1; |
| 116 std::pair<std::map<XID, PermanentXIDInfo>::iterator, bool> ret = |
| 117 perm_xid_to_info_.insert(std::make_pair(*output, info)); |
| 118 |
| 119 if (!ret.second) { |
| 120 DCHECK(ret.first->second.widget == widget); |
| 121 ret.first->second.ref_count++; |
| 122 } |
| 123 |
113 return true; | 124 return true; |
114 } | 125 } |
115 | 126 |
| 127 void GtkNativeViewManager::ReleasePermanentXID(XID xid) { |
| 128 AutoLock locked(lock_); |
| 129 |
| 130 std::map<XID, PermanentXIDInfo>::iterator i = |
| 131 perm_xid_to_info_.find(xid); |
| 132 |
| 133 if (i == perm_xid_to_info_.end()) |
| 134 return; |
| 135 |
| 136 if (i->second.ref_count > 1) { |
| 137 i->second.ref_count--; |
| 138 } else { |
| 139 if (i->second.widget) { |
| 140 gtk_preserve_window_set_preserve(i->second.widget, FALSE); |
| 141 } else { |
| 142 GdkWindow* window = reinterpret_cast<GdkWindow*>( |
| 143 gdk_xid_table_lookup(xid)); |
| 144 DCHECK(window); |
| 145 gdk_window_destroy(window); |
| 146 } |
| 147 perm_xid_to_info_.erase(i); |
| 148 } |
| 149 } |
| 150 |
116 // ----------------------------------------------------------------------------- | 151 // ----------------------------------------------------------------------------- |
117 | 152 |
118 | 153 |
119 // ----------------------------------------------------------------------------- | 154 // ----------------------------------------------------------------------------- |
120 // Private functions... | 155 // Private functions... |
121 | 156 |
122 gfx::NativeViewId GtkNativeViewManager::GetWidgetId(gfx::NativeView widget) { | 157 gfx::NativeViewId GtkNativeViewManager::GetWidgetId(gfx::NativeView widget) { |
123 lock_.AssertAcquired(); | 158 lock_.AssertAcquired(); |
124 | 159 |
125 std::map<gfx::NativeView, gfx::NativeViewId>::const_iterator i = | 160 std::map<gfx::NativeView, gfx::NativeViewId>::const_iterator i = |
(...skipping 18 matching lines...) Expand all Loading... |
144 | 179 |
145 void GtkNativeViewManager::OnUnrealize(gfx::NativeView widget) { | 180 void GtkNativeViewManager::OnUnrealize(gfx::NativeView widget) { |
146 AutoLock unrealize_locked(unrealize_lock_); | 181 AutoLock unrealize_locked(unrealize_lock_); |
147 AutoLock locked(lock_); | 182 AutoLock locked(lock_); |
148 | 183 |
149 const gfx::NativeViewId id = GetWidgetId(widget); | 184 const gfx::NativeViewId id = GetWidgetId(widget); |
150 std::map<gfx::NativeViewId, NativeViewInfo>::iterator i = | 185 std::map<gfx::NativeViewId, NativeViewInfo>::iterator i = |
151 id_to_info_.find(id); | 186 id_to_info_.find(id); |
152 | 187 |
153 CHECK(i != id_to_info_.end()); | 188 CHECK(i != id_to_info_.end()); |
154 | |
155 i->second.x_window_id = 0; | |
156 } | 189 } |
157 | 190 |
158 void GtkNativeViewManager::OnDestroy(gfx::NativeView widget) { | 191 void GtkNativeViewManager::OnDestroy(gfx::NativeView widget) { |
159 AutoLock locked(lock_); | 192 AutoLock locked(lock_); |
160 | 193 |
161 std::map<gfx::NativeView, gfx::NativeViewId>::iterator i = | 194 std::map<gfx::NativeView, gfx::NativeViewId>::iterator i = |
162 native_view_to_id_.find(widget); | 195 native_view_to_id_.find(widget); |
163 CHECK(i != native_view_to_id_.end()); | 196 CHECK(i != native_view_to_id_.end()); |
164 | 197 |
165 std::map<gfx::NativeViewId, NativeViewInfo>::iterator j = | 198 std::map<gfx::NativeViewId, NativeViewInfo>::iterator j = |
166 id_to_info_.find(i->second); | 199 id_to_info_.find(i->second); |
167 CHECK(j != id_to_info_.end()); | 200 CHECK(j != id_to_info_.end()); |
168 | 201 |
| 202 // If the XID is supposed to outlive the widget, mark it |
| 203 // in the lookup table. |
| 204 if (GTK_IS_PRESERVE_WINDOW(widget) && |
| 205 gtk_preserve_window_get_preserve( |
| 206 reinterpret_cast<GtkPreserveWindow*>(widget))) { |
| 207 std::map<XID, PermanentXIDInfo>::iterator k = |
| 208 perm_xid_to_info_.find(GDK_WINDOW_XID(widget->window)); |
| 209 |
| 210 if (k != perm_xid_to_info_.end()) |
| 211 k->second.widget = NULL; |
| 212 } |
| 213 |
169 native_view_to_id_.erase(i); | 214 native_view_to_id_.erase(i); |
170 id_to_info_.erase(j); | 215 id_to_info_.erase(j); |
171 } | 216 } |
172 | 217 |
173 // ----------------------------------------------------------------------------- | 218 // ----------------------------------------------------------------------------- |
OLD | NEW |