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.
4
5#ifndef UI_BASE_X_SELECTION_OWNER_H_
6#define UI_BASE_X_SELECTION_OWNER_H_
7
8#include <vector>
9
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"
19
20namespace ui {
21
22// Owns a specific X11 selection on an X window.
23//
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.
27class UI_BASE_EXPORT SelectionOwner {
28 public:
29  SelectionOwner(XDisplay* xdisplay,
30                 XID xwindow,
31                 XAtom selection_name);
32  ~SelectionOwner();
33
34  // Returns the current selection data. Useful for fast paths.
35  const SelectionFormatMap& selection_format_map() { return format_map_; }
36
37  // Appends a list of types we're offering to |targets|.
38  void RetrieveTargets(std::vector<XAtom>* targets);
39
40  // Attempts to take ownership of the selection. If we're successful, present
41  // |data| to other windows.
42  void TakeOwnershipOfSelection(const SelectionFormatMap& data);
43
44  // Clears our internal format map and clears the selection owner, whether we
45  // own the selection or not.
46  void ClearSelectionOwner();
47
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);
51
52  // Returns true if SelectionOwner can process the XPropertyEvent event,
53  // |event|.
54  bool CanDispatchPropertyEvent(const XEvent& event);
55
56  void OnPropertyEvent(const XEvent& event);
57
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();
69
70    // Parameters from the XSelectionRequest. The data is transferred over
71    // |property| on |window|.
72    XID window;
73    XAtom target;
74    XAtom property;
75
76    // The data to be transferred.
77    scoped_refptr<base::RefCountedMemory> data;
78
79    // The offset from the beginning of |data| of the first byte to be
80    // transferred in the next chunk.
81    size_t offset;
82
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;
86
87    // Used to unselect PropertyChangeMask on |window| when we are done with
88    // the data transfer.
89    int foreign_window_manager_id;
90  };
91
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);
96
97  // Sends the next chunk of data for given the incremental data transfer.
98  void ProcessIncrementalTransfer(IncrementalTransfer* transfer);
99
100  // Aborts any incremental data transfers which have timed out.
101  void AbortStaleIncrementalTransfers();
102
103  // Called when the transfer at |it| has completed to do cleanup.
104  void CompleteIncrementalTransfer(
105      std::vector<IncrementalTransfer>::iterator it);
106
107  // Returns the incremental data transfer, if any, which was waiting for
108  // |event|.
109  std::vector<IncrementalTransfer>::iterator FindIncrementalTransferForEvent(
110      const XEvent& event);
111
112  // Our X11 state.
113  XDisplay* x_display_;
114  XID x_window_;
115
116  // The X11 selection that this instance communicates on.
117  XAtom selection_name_;
118
119  // The maximum size of data we can put in XChangeProperty().
120  size_t max_request_size_;
121
122  // The data we are currently serving.
123  SelectionFormatMap format_map_;
124
125  std::vector<IncrementalTransfer> incremental_transfers_;
126
127  // Used to abort stale incremental data transfers.
128  base::RepeatingTimer<SelectionOwner> incremental_transfer_abort_timer_;
129
130  X11AtomCache atom_cache_;
131
132  DISALLOW_COPY_AND_ASSIGN(SelectionOwner);
133};
134
135}  // namespace ui
136
137#endif  // UI_BASE_X_SELECTION_OWNER_H_
138