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_OWNER_H_
6 #define UI_BASE_X_SELECTION_OWNER_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/memory/ref_counted_memory.h"
13 #include "base/time/time.h"
14 #include "base/timer/timer.h"
15 #include "ui/base/ui_base_export.h"
16 #include "ui/base/x/selection_utils.h"
17 #include "ui/gfx/x/x11_atom_cache.h"
18 #include "ui/gfx/x/x11_types.h"
22 // Owns a specific X11 selection on an X window.
24 // The selection owner object keeps track of which xwindow is the current
25 // owner, and when its |xwindow_|, offers different data types to other
27 class UI_BASE_EXPORT SelectionOwner
{
29 SelectionOwner(XDisplay
* xdisplay
,
31 XAtom selection_name
);
34 // Returns the current selection data. Useful for fast paths.
35 const SelectionFormatMap
& selection_format_map() { return format_map_
; }
37 // Appends a list of types we're offering to |targets|.
38 void RetrieveTargets(std::vector
<XAtom
>* targets
);
40 // Attempts to take ownership of the selection. If we're successful, present
41 // |data| to other windows.
42 void TakeOwnershipOfSelection(const SelectionFormatMap
& data
);
44 // Clears our internal format map and clears the selection owner, whether we
45 // own the selection or not.
46 void ClearSelectionOwner();
48 // It is our owner's responsibility to plumb X11 events on |xwindow_| to us.
49 void OnSelectionRequest(const XEvent
& event
);
50 void OnSelectionClear(const XEvent
& event
);
52 // Returns true if SelectionOwner can process the XPropertyEvent event,
54 bool CanDispatchPropertyEvent(const XEvent
& event
);
56 void OnPropertyEvent(const XEvent
& event
);
59 // Holds state related to an incremental data transfer.
60 struct IncrementalTransfer
{
61 IncrementalTransfer(XID window
,
64 const scoped_refptr
<base::RefCountedMemory
>& data
,
66 base::TimeTicks timeout
,
67 int foreign_window_manager_id
);
68 ~IncrementalTransfer();
70 // Parameters from the XSelectionRequest. The data is transferred over
71 // |property| on |window|.
76 // The data to be transferred.
77 scoped_refptr
<base::RefCountedMemory
> data
;
79 // The offset from the beginning of |data| of the first byte to be
80 // transferred in the next chunk.
83 // Time when the transfer should be aborted because the selection requestor
84 // is taking too long to notify us that we can send the next chunk.
85 base::TimeTicks timeout
;
87 // Used to unselect PropertyChangeMask on |window| when we are done with
89 int foreign_window_manager_id
;
92 // Attempts to convert the selection to |target|. If the conversion is
93 // successful, true is returned and the result is stored in the |property|
95 bool ProcessTarget(XAtom target
, XID requestor
, XAtom property
);
97 // Sends the next chunk of data for given the incremental data transfer.
98 void ProcessIncrementalTransfer(IncrementalTransfer
* transfer
);
100 // Aborts any incremental data transfers which have timed out.
101 void AbortStaleIncrementalTransfers();
103 // Called when the transfer at |it| has completed to do cleanup.
104 void CompleteIncrementalTransfer(
105 std::vector
<IncrementalTransfer
>::iterator it
);
107 // Returns the incremental data transfer, if any, which was waiting for
109 std::vector
<IncrementalTransfer
>::iterator
FindIncrementalTransferForEvent(
110 const XEvent
& event
);
113 XDisplay
* x_display_
;
116 // The X11 selection that this instance communicates on.
117 XAtom selection_name_
;
119 // The maximum size of data we can put in XChangeProperty().
120 size_t max_request_size_
;
122 // The data we are currently serving.
123 SelectionFormatMap format_map_
;
125 std::vector
<IncrementalTransfer
> incremental_transfers_
;
127 // Used to abort stale incremental data transfers.
128 base::RepeatingTimer
<SelectionOwner
> incremental_transfer_abort_timer_
;
130 X11AtomCache atom_cache_
;
132 DISALLOW_COPY_AND_ASSIGN(SelectionOwner
);
137 #endif // UI_BASE_X_SELECTION_OWNER_H_