Update expectations according to build bot status.
[chromium-blink-merge.git] / gfx / gtk_native_view_id_manager.h
blob048cd2d43f226ffd6be1bca45e4a7f00fc8dcf63
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
3 // found in the LICENSE file.
5 #ifndef GFX_GTK_NATIVE_VIEW_ID_MANAGER_H_
6 #define GFX_GTK_NATIVE_VIEW_ID_MANAGER_H_
8 #include <map>
10 #include "base/singleton.h"
11 #include "gfx/native_widget_types.h"
13 typedef unsigned long XID;
15 // NativeViewIds are the opaque values which the renderer holds as a reference
16 // to a window. These ids are often used in sync calls from the renderer and
17 // one cannot terminate sync calls on the UI thread as that can lead to
18 // deadlocks.
20 // Because of this, we have the BACKGROUND_X11 thread for these calls and this
21 // thread has a separate X connection in order to answer them. But one cannot
22 // use GTK on multiple threads, so the BACKGROUND_X11 thread deals only in Xlib
23 // calls and, thus, XIDs.
25 // So we could make NativeViewIds be the X id of the window. However, at the
26 // time when we need to tell the renderer about its NativeViewId, an XID isn't
27 // availible and it goes very much against the grain of the code to make it so.
28 // Also, we worry that GTK might choose to change the underlying X window id
29 // when, say, the widget is hidden or repacked. Finally, if we used XIDs then a
30 // compromised renderer could start asking questions about any X windows on the
31 // system.
33 // Thus, we have this object. It produces random NativeViewIds from GtkWidget
34 // pointers and observes the various signals from the widget for when an X
35 // window is created, destroyed etc. Thus it provides a thread safe mapping
36 // from NativeViewIds to the current XID for that widget.
38 // You get a reference to the global instance with:
39 // Singleton<GtkNativeViewManager>()
40 class GtkNativeViewManager {
41 public:
42 // Must be called from the UI thread:
44 // Return a NativeViewId for the given widget and attach to the various
45 // signals emitted by that widget. The NativeViewId is pseudo-randomly
46 // allocated so that a compromised renderer trying to guess values will fail
47 // with high probability. The NativeViewId will not be reused for the
48 // lifetime of the GtkWidget.
49 gfx::NativeViewId GetIdForWidget(gfx::NativeView widget);
51 // May be called from any thread:
53 // xid: (output) the resulting X window ID, or 0
54 // id: a value previously returned from GetIdForWidget
55 // returns: true if |id| is a valid id, false otherwise.
57 // If the widget referenced by |id| does not current have an X window id,
58 // |*xid| is set to 0.
59 bool GetXIDForId(XID* xid, gfx::NativeViewId id);
61 // These are actually private functions, but need to be called from statics.
62 void OnRealize(gfx::NativeView widget);
63 void OnUnrealize(gfx::NativeView widget);
64 void OnDestroy(gfx::NativeView widget);
66 private:
67 // This object is a singleton:
68 GtkNativeViewManager();
69 friend struct DefaultSingletonTraits<GtkNativeViewManager>;
71 struct NativeViewInfo {
72 NativeViewInfo()
73 : x_window_id(0) {
76 XID x_window_id;
79 gfx::NativeViewId GetWidgetId(gfx::NativeView id);
81 // protects native_view_to_id_ and id_to_info_
82 Lock lock_;
83 // If asked for an id for the same widget twice, we want to return the same
84 // id. So this records the current mapping.
85 std::map<gfx::NativeView, gfx::NativeViewId> native_view_to_id_;
86 std::map<gfx::NativeViewId, NativeViewInfo> id_to_info_;
88 DISALLOW_COPY_AND_ASSIGN(GtkNativeViewManager);
91 #endif // GFX_GTK_NATIVE_VIEW_ID_MANAGER_H_