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
3 // found in the LICENSE file.
5 #ifndef UI_BASE_X_SELECTION_REQUESTOR_H_
6 #define UI_BASE_X_SELECTION_REQUESTOR_H_
10 // Get rid of a macro from Xlib.h that conflicts with Aura's RootWindow class.
16 #include "base/basictypes.h"
17 #include "base/callback.h"
18 #include "base/memory/ref_counted_memory.h"
19 #include "ui/base/ui_base_export.h"
20 #include "ui/gfx/x/x11_atom_cache.h"
23 class PlatformEventDispatcher
;
26 // Requests and later receives data from the X11 server through the selection
29 // X11 uses a system called "selections" to implement clipboards and drag and
30 // drop. This class interprets messages from the statefull selection request
31 // API. SelectionRequestor should only deal with the X11 details; it does not
32 // implement per-component fast-paths.
33 class UI_BASE_EXPORT SelectionRequestor
{
35 SelectionRequestor(Display
* xdisplay
,
37 ::Atom selection_name
,
38 PlatformEventDispatcher
* dispatcher
);
39 ~SelectionRequestor();
41 // Does the work of requesting |target| from the selection we handle,
42 // spinning up the nested message loop, and reading the resulting data
43 // back. The result is stored in |out_data|.
44 // |out_data_items| is the length of |out_data| in |out_type| items.
45 bool PerformBlockingConvertSelection(
47 scoped_refptr
<base::RefCountedMemory
>* out_data
,
48 size_t* out_data_items
,
51 // Requests |target| from the selection that we handle, passing |parameter|
52 // as a parameter to XConvertSelection().
53 void PerformBlockingConvertSelectionWithParameter(
55 const std::vector
< ::Atom
>& parameter
);
57 // Returns the first of |types| offered by the current selection holder, or
58 // returns NULL if none of those types are available.
59 SelectionData
RequestAndWaitForTypes(const std::vector
< ::Atom
>& types
);
61 // It is our owner's responsibility to plumb X11 SelectionNotify events on
63 void OnSelectionNotify(const XSelectionEvent
& event
);
66 // A request that has been issued and we are waiting for a response to.
67 struct PendingRequest
{
68 explicit PendingRequest(Atom target
);
71 // Data to the current XConvertSelection request. Used for error detection;
72 // we verify it on the return message.
75 // Called to terminate the nested message loop.
76 base::Closure quit_closure
;
78 // The property in the returning SelectNotify message is used to signal
79 // 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
81 // property on |x_window_| for the requested data.
82 ::Atom returned_property
;
84 // Set to true when return_property is populated.
88 // Blocks till SelectionNotify is received for the target specified in
90 void BlockTillSelectionNotifyForRequest(PendingRequest
* request
);
96 // The X11 selection that this instance communicates on.
97 ::Atom selection_name_
;
99 // Dispatcher which handles SelectionNotify and SelectionRequest for
100 // |selection_name_|. PerformBlockingConvertSelection() calls the
101 // dispatcher directly if PerformBlockingConvertSelection() is called after
102 // the PlatformEventSource is destroyed.
104 PlatformEventDispatcher
* dispatcher_
;
106 // A list of requests for which we are waiting for responses.
107 std::list
<PendingRequest
*> pending_requests_
;
109 X11AtomCache atom_cache_
;
111 DISALLOW_COPY_AND_ASSIGN(SelectionRequestor
);
116 #endif // UI_BASE_X_SELECTION_REQUESTOR_H_