Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / base / x / selection_owner.h
blob6e3ac7f5a9d1439f7354ae77d1e73b2e811a7c2c
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_
8 #include <vector>
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"
20 namespace ui {
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
26 // processes.
27 class UI_BASE_EXPORT SelectionOwner {
28 public:
29 SelectionOwner(XDisplay* xdisplay,
30 XID xwindow,
31 XAtom selection_name);
32 ~SelectionOwner();
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,
53 // |event|.
54 bool CanDispatchPropertyEvent(const XEvent& event);
56 void OnPropertyEvent(const XEvent& event);
58 private:
59 // Holds state related to an incremental data transfer.
60 struct IncrementalTransfer {
61 IncrementalTransfer(XID window,
62 XAtom target,
63 XAtom property,
64 const scoped_refptr<base::RefCountedMemory>& data,
65 int offset,
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|.
72 XID window;
73 XAtom target;
74 XAtom property;
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.
81 size_t offset;
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
88 // the data transfer.
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|
94 // of |requestor|.
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
108 // |event|.
109 std::vector<IncrementalTransfer>::iterator FindIncrementalTransferForEvent(
110 const XEvent& event);
112 // Our X11 state.
113 XDisplay* x_display_;
114 XID x_window_;
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);
135 } // namespace ui
137 #endif // UI_BASE_X_SELECTION_OWNER_H_