OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 UI_BASE_X_SELECTION_REQUESTOR_H_ | 5 #ifndef UI_BASE_X_SELECTION_REQUESTOR_H_ |
6 #define UI_BASE_X_SELECTION_REQUESTOR_H_ | 6 #define UI_BASE_X_SELECTION_REQUESTOR_H_ |
7 | 7 |
8 #include <X11/Xlib.h> | 8 #include <X11/Xlib.h> |
9 | 9 |
10 // Get rid of a macro from Xlib.h that conflicts with Aura's RootWindow class. | |
11 #undef RootWindow | |
12 | |
13 #include <list> | 10 #include <list> |
14 #include <vector> | |
15 | 11 |
16 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
17 #include "base/callback.h" | 13 #include "base/callback.h" |
18 #include "base/memory/ref_counted_memory.h" | 14 #include "base/memory/ref_counted_memory.h" |
19 #include "ui/base/ui_base_export.h" | 15 #include "ui/base/ui_base_export.h" |
20 #include "ui/gfx/x/x11_atom_cache.h" | 16 #include "ui/gfx/x/x11_atom_cache.h" |
21 | 17 |
22 namespace ui { | 18 namespace ui { |
23 class PlatformEventDispatcher; | 19 class PlatformEventDispatcher; |
24 class SelectionData; | 20 class SelectionData; |
25 | 21 |
26 // Requests and later receives data from the X11 server through the selection | 22 // Requests and later receives data from the X11 server through the selection |
27 // system. | 23 // system. |
28 // | 24 // |
29 // X11 uses a system called "selections" to implement clipboards and drag and | 25 // X11 uses a system called "selections" to implement clipboards and drag and |
30 // drop. This class interprets messages from the statefull selection request | 26 // drop. This class interprets messages from the statefull selection request |
31 // API. SelectionRequestor should only deal with the X11 details; it does not | 27 // API. SelectionRequestor should only deal with the X11 details; it does not |
32 // implement per-component fast-paths. | 28 // implement per-component fast-paths. |
33 class UI_BASE_EXPORT SelectionRequestor { | 29 class UI_BASE_EXPORT SelectionRequestor { |
34 public: | 30 public: |
35 SelectionRequestor(Display* xdisplay, | 31 SelectionRequestor(Display* xdisplay, |
36 ::Window xwindow, | 32 ::Window xwindow, |
37 ::Atom selection_name, | |
38 PlatformEventDispatcher* dispatcher); | 33 PlatformEventDispatcher* dispatcher); |
39 ~SelectionRequestor(); | 34 ~SelectionRequestor(); |
40 | 35 |
41 // Does the work of requesting |target| from the selection we handle, | 36 // Does the work of requesting |target| from |selection|, spinning up the |
42 // spinning up the nested message loop, and reading the resulting data | 37 // nested message loop, and reading the resulting data back. The result is |
43 // back. The result is stored in |out_data|. | 38 // stored in |out_data|. |
44 // |out_data_items| is the length of |out_data| in |out_type| items. | 39 // |out_data_items| is the length of |out_data| in |out_type| items. |
45 bool PerformBlockingConvertSelection( | 40 bool PerformBlockingConvertSelection( |
| 41 ::Atom selection, |
46 ::Atom target, | 42 ::Atom target, |
47 scoped_refptr<base::RefCountedMemory>* out_data, | 43 scoped_refptr<base::RefCountedMemory>* out_data, |
48 size_t* out_data_items, | 44 size_t* out_data_items, |
49 ::Atom* out_type); | 45 ::Atom* out_type); |
50 | 46 |
51 // Requests |target| from the selection that we handle, passing |parameter| | 47 // Requests |target| from |selection|, passing |parameter| as a parameter to |
52 // as a parameter to XConvertSelection(). | 48 // XConvertSelection(). |
53 void PerformBlockingConvertSelectionWithParameter( | 49 void PerformBlockingConvertSelectionWithParameter( |
| 50 ::Atom selection, |
54 ::Atom target, | 51 ::Atom target, |
55 const std::vector< ::Atom>& parameter); | 52 const std::vector< ::Atom>& parameter); |
56 | 53 |
57 // Returns the first of |types| offered by the current selection holder, or | 54 // Returns the first of |types| offered by the current owner of |selection|, |
58 // returns NULL if none of those types are available. | 55 // or returns NULL if none of those types are available. |
59 SelectionData RequestAndWaitForTypes(const std::vector< ::Atom>& types); | 56 SelectionData RequestAndWaitForTypes(::Atom selection, |
| 57 const std::vector< ::Atom>& types); |
60 | 58 |
61 // It is our owner's responsibility to plumb X11 SelectionNotify events on | 59 // It is our owner's responsibility to plumb X11 SelectionNotify events on |
62 // |xwindow_| to us. | 60 // |xwindow_| to us. |
63 void OnSelectionNotify(const XSelectionEvent& event); | 61 void OnSelectionNotify(const XSelectionEvent& event); |
64 | 62 |
65 private: | 63 private: |
66 // A request that has been issued and we are waiting for a response to. | 64 // A request that has been issued and we are waiting for a response to. |
67 struct PendingRequest { | 65 struct PendingRequest { |
68 explicit PendingRequest(Atom target); | 66 PendingRequest(::Atom requested_selection, ::Atom requested_target); |
69 ~PendingRequest(); | 67 ~PendingRequest(); |
70 | 68 |
71 // Data to the current XConvertSelection request. Used for error detection; | 69 // The target and selection requested in the XConvertSelection() request. |
72 // we verify it on the return message. | 70 // Used for error detection. |
| 71 ::Atom selection; |
73 ::Atom target; | 72 ::Atom target; |
74 | 73 |
75 // Called to terminate the nested message loop. | 74 // Called to terminate the nested message loop. |
76 base::Closure quit_closure; | 75 base::Closure quit_closure; |
77 | 76 |
78 // The property in the returning SelectNotify message is used to signal | 77 // The property in the returning SelectNotify message is used to signal |
79 // success. If None, our request failed somehow. If equal to the property | 78 // success. If None, our request failed somehow. If equal to the property |
80 // atom that we sent in the XConvertSelection call, we can read that | 79 // atom that we sent in the XConvertSelection call, we can read that |
81 // property on |x_window_| for the requested data. | 80 // property on |x_window_| for the requested data. |
82 ::Atom returned_property; | 81 ::Atom returned_property; |
83 | 82 |
84 // Set to true when return_property is populated. | 83 // Set to true when return_property is populated. |
85 bool returned; | 84 bool returned; |
86 }; | 85 }; |
87 | 86 |
| 87 // Helper method for PerformBlockingConvertSelection() and |
| 88 // PerformBlockingConvertSelectionWithParameter(). |
| 89 bool PerformBlockingConvertSelectionImpl( |
| 90 ::Atom selection, |
| 91 ::Atom target, |
| 92 const char* property_name, |
| 93 scoped_refptr<base::RefCountedMemory>* out_data, |
| 94 size_t* out_data_items, |
| 95 ::Atom* out_type); |
| 96 |
88 // Blocks till SelectionNotify is received for the target specified in | 97 // Blocks till SelectionNotify is received for the target specified in |
89 // |request|. | 98 // |request|. |
90 void BlockTillSelectionNotifyForRequest(PendingRequest* request); | 99 void BlockTillSelectionNotifyForRequest(PendingRequest* request); |
91 | 100 |
92 // Our X11 state. | 101 // Our X11 state. |
93 Display* x_display_; | 102 Display* x_display_; |
94 ::Window x_window_; | 103 ::Window x_window_; |
95 | 104 |
96 // The X11 selection that this instance communicates on. | |
97 ::Atom selection_name_; | |
98 | |
99 // Dispatcher which handles SelectionNotify and SelectionRequest for | 105 // Dispatcher which handles SelectionNotify and SelectionRequest for |
100 // |selection_name_|. PerformBlockingConvertSelection() calls the | 106 // |selection_name_|. PerformBlockingConvertSelection() calls the |
101 // dispatcher directly if PerformBlockingConvertSelection() is called after | 107 // dispatcher directly if PerformBlockingConvertSelection() is called after |
102 // the PlatformEventSource is destroyed. | 108 // the PlatformEventSource is destroyed. |
103 // Not owned. | 109 // Not owned. |
104 PlatformEventDispatcher* dispatcher_; | 110 PlatformEventDispatcher* dispatcher_; |
105 | 111 |
| 112 // A pool of properties which can be used in XConvertSelection() requests. |
| 113 std::list<const char*> property_pool_; |
| 114 |
106 // A list of requests for which we are waiting for responses. | 115 // A list of requests for which we are waiting for responses. |
107 std::list<PendingRequest*> pending_requests_; | 116 std::list<PendingRequest*> pending_requests_; |
108 | 117 |
109 X11AtomCache atom_cache_; | 118 X11AtomCache atom_cache_; |
110 | 119 |
111 DISALLOW_COPY_AND_ASSIGN(SelectionRequestor); | 120 DISALLOW_COPY_AND_ASSIGN(SelectionRequestor); |
112 }; | 121 }; |
113 | 122 |
114 } // namespace ui | 123 } // namespace ui |
115 | 124 |
116 #endif // UI_BASE_X_SELECTION_REQUESTOR_H_ | 125 #endif // UI_BASE_X_SELECTION_REQUESTOR_H_ |
OLD | NEW |