OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef BASE_GFX_GTK_NATIVE_VIEW_ID_MANAGER_H_ | |
6 #define BASE_GFX_GTK_NATIVE_VIEW_ID_MANAGER_H_ | |
7 | |
8 #include <map> | |
9 | |
10 #include "base/singleton.h" | |
11 #include "base/gfx/native_widget_types.h" | |
12 | |
13 typedef unsigned long XID; | |
14 | |
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. | |
19 // | |
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. | |
24 // | |
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. | |
32 // | |
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. | |
37 // | |
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: | |
43 // | |
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); | |
50 | |
51 // May be called from any thread: | |
52 // | |
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. | |
56 // | |
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); | |
60 | |
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); | |
65 | |
66 private: | |
67 // This object is a singleton: | |
68 GtkNativeViewManager(); | |
69 friend struct DefaultSingletonTraits<GtkNativeViewManager>; | |
70 | |
71 struct NativeViewInfo { | |
72 NativeViewInfo() | |
73 : x_window_id(0) { | |
74 } | |
75 | |
76 XID x_window_id; | |
77 }; | |
78 | |
79 gfx::NativeViewId GetWidgetId(gfx::NativeView id); | |
80 | |
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_; | |
87 | |
88 DISALLOW_COPY_AND_ASSIGN(GtkNativeViewManager); | |
89 }; | |
90 | |
91 #endif // BASE_GFX_GTK_NATIVE_VIEW_ID_MANAGER_H_ | |
OLD | NEW |