| 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 #ifndef GFX_GTK_NATIVE_VIEW_ID_MANAGER_H_ | 5 #ifndef GFX_GTK_NATIVE_VIEW_ID_MANAGER_H_ |
| 6 #define GFX_GTK_NATIVE_VIEW_ID_MANAGER_H_ | 6 #define GFX_GTK_NATIVE_VIEW_ID_MANAGER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include "ui/gfx/gtk_native_view_id_manager.h" |
| 10 | 10 // TODO(sail): remove this file once all includes have been updated. |
| 11 #include "base/singleton.h" | |
| 12 #include "base/synchronization/lock.h" | |
| 13 #include "gfx/native_widget_types.h" | |
| 14 | |
| 15 typedef unsigned long XID; | |
| 16 struct _GtkPreserveWindow; | |
| 17 | |
| 18 // NativeViewIds are the opaque values which the renderer holds as a reference | |
| 19 // to a window. These ids are often used in sync calls from the renderer and | |
| 20 // one cannot terminate sync calls on the UI thread as that can lead to | |
| 21 // deadlocks. | |
| 22 // | |
| 23 // Because of this, we have the BACKGROUND_X11 thread for these calls and this | |
| 24 // thread has a separate X connection in order to answer them. But one cannot | |
| 25 // use GTK on multiple threads, so the BACKGROUND_X11 thread deals only in Xlib | |
| 26 // calls and, thus, XIDs. | |
| 27 // | |
| 28 // So we could make NativeViewIds be the X id of the window. However, at the | |
| 29 // time when we need to tell the renderer about its NativeViewId, an XID isn't | |
| 30 // availible and it goes very much against the grain of the code to make it so. | |
| 31 // Also, we worry that GTK might choose to change the underlying X window id | |
| 32 // when, say, the widget is hidden or repacked. Finally, if we used XIDs then a | |
| 33 // compromised renderer could start asking questions about any X windows on the | |
| 34 // system. | |
| 35 // | |
| 36 // Thus, we have this object. It produces random NativeViewIds from GtkWidget | |
| 37 // pointers and observes the various signals from the widget for when an X | |
| 38 // window is created, destroyed etc. Thus it provides a thread safe mapping | |
| 39 // from NativeViewIds to the current XID for that widget. | |
| 40 class GtkNativeViewManager { | |
| 41 public: | |
| 42 // Returns the singleton instance. | |
| 43 static GtkNativeViewManager* GetInstance(); | |
| 44 | |
| 45 // Must be called from the UI thread: | |
| 46 // | |
| 47 // Return a NativeViewId for the given widget and attach to the various | |
| 48 // signals emitted by that widget. The NativeViewId is pseudo-randomly | |
| 49 // allocated so that a compromised renderer trying to guess values will fail | |
| 50 // with high probability. The NativeViewId will not be reused for the | |
| 51 // lifetime of the GtkWidget. | |
| 52 gfx::NativeViewId GetIdForWidget(gfx::NativeView widget); | |
| 53 | |
| 54 // May be called from any thread: | |
| 55 // | |
| 56 // xid: (output) the resulting X window ID, or 0 | |
| 57 // id: a value previously returned from GetIdForWidget | |
| 58 // returns: true if |id| is a valid id, false otherwise. | |
| 59 // | |
| 60 // If the widget referenced by |id| does not current have an X window id, | |
| 61 // |*xid| is set to 0. | |
| 62 bool GetXIDForId(XID* xid, gfx::NativeViewId id); | |
| 63 | |
| 64 // Must be called from the UI thread because we may need the associated | |
| 65 // widget to create a window. | |
| 66 // | |
| 67 // Keeping the XID permanent requires a bit of overhead, so it must | |
| 68 // be explicitly requested. | |
| 69 // | |
| 70 // xid: (output) the resulting X window | |
| 71 // id: a value previously returned from GetIdForWidget | |
| 72 // returns: true if |id| is a valid id, false otherwise. | |
| 73 bool GetPermanentXIDForId(XID* xid, gfx::NativeViewId id); | |
| 74 | |
| 75 // Must be called from the UI thread because we may need to access a | |
| 76 // GtkWidget or destroy a GdkWindow. | |
| 77 // | |
| 78 // If the widget associated with the XID is still alive, allow the widget | |
| 79 // to destroy the associated XID when it wants. Otherwise, destroy the | |
| 80 // GdkWindow associated with the XID. | |
| 81 void ReleasePermanentXID(XID xid); | |
| 82 | |
| 83 // These are actually private functions, but need to be called from statics. | |
| 84 void OnRealize(gfx::NativeView widget); | |
| 85 void OnUnrealize(gfx::NativeView widget); | |
| 86 void OnDestroy(gfx::NativeView widget); | |
| 87 | |
| 88 base::Lock& unrealize_lock() { return unrealize_lock_; } | |
| 89 | |
| 90 private: | |
| 91 // This object is a singleton: | |
| 92 GtkNativeViewManager(); | |
| 93 ~GtkNativeViewManager(); | |
| 94 friend struct DefaultSingletonTraits<GtkNativeViewManager>; | |
| 95 | |
| 96 struct NativeViewInfo { | |
| 97 NativeViewInfo() : widget(NULL), x_window_id(0) { | |
| 98 } | |
| 99 gfx::NativeView widget; | |
| 100 XID x_window_id; | |
| 101 }; | |
| 102 | |
| 103 gfx::NativeViewId GetWidgetId(gfx::NativeView id); | |
| 104 | |
| 105 // This lock can be used to block GTK from unrealizing windows. This is needed | |
| 106 // when the BACKGROUND_X11 thread is using a window obtained via GetXIDForId, | |
| 107 // and can't allow the X11 resource to be deleted. | |
| 108 base::Lock unrealize_lock_; | |
| 109 | |
| 110 // protects native_view_to_id_ and id_to_info_ | |
| 111 base::Lock lock_; | |
| 112 | |
| 113 // If asked for an id for the same widget twice, we want to return the same | |
| 114 // id. So this records the current mapping. | |
| 115 std::map<gfx::NativeView, gfx::NativeViewId> native_view_to_id_; | |
| 116 std::map<gfx::NativeViewId, NativeViewInfo> id_to_info_; | |
| 117 | |
| 118 struct PermanentXIDInfo { | |
| 119 PermanentXIDInfo() : widget(NULL), ref_count(0) { | |
| 120 } | |
| 121 _GtkPreserveWindow* widget; | |
| 122 int ref_count; | |
| 123 }; | |
| 124 | |
| 125 // Used to maintain the reference count for permanent XIDs | |
| 126 // (referenced by GetPermanentXIDForId and dereferenced by | |
| 127 // ReleasePermanentXID). Only those XIDs with a positive reference count | |
| 128 // will be in the table. | |
| 129 // | |
| 130 // In general, several GTK widgets may share the same X window. We assume | |
| 131 // that is not true of the widgets stored in this registry. | |
| 132 // | |
| 133 // An XID will map to NULL, if there is an outstanding reference but the | |
| 134 // widget was destroyed. In this case, the destruction of the X window | |
| 135 // is deferred to the dropping of all references. | |
| 136 std::map<XID, PermanentXIDInfo> perm_xid_to_info_; | |
| 137 | |
| 138 DISALLOW_COPY_AND_ASSIGN(GtkNativeViewManager); | |
| 139 }; | |
| 140 | 11 |
| 141 #endif // GFX_GTK_NATIVE_VIEW_ID_MANAGER_H_ | 12 #endif // GFX_GTK_NATIVE_VIEW_ID_MANAGER_H_ |
| OLD | NEW |