1// Copyright (c) 2012 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_DRAGDROP_OS_EXCHANGE_DATA_PROVIDER_AURAX11_H_
6#define UI_BASE_DRAGDROP_OS_EXCHANGE_DATA_PROVIDER_AURAX11_H_
7
8#include <X11/Xlib.h>
9
10// Get rid of a macro from Xlib.h that conflicts with Aura's RootWindow class.
11#undef RootWindow
12
13#include <map>
14
15#include "base/files/file_path.h"
16#include "base/pickle.h"
17#include "ui/base/dragdrop/os_exchange_data.h"
18#include "ui/base/x/selection_owner.h"
19#include "ui/base/x/selection_requestor.h"
20#include "ui/base/x/selection_utils.h"
21#include "ui/events/platform/platform_event_dispatcher.h"
22#include "ui/gfx/image/image_skia.h"
23#include "ui/gfx/vector2d.h"
24#include "ui/gfx/x/x11_atom_cache.h"
25#include "url/gurl.h"
26
27namespace ui {
28
29class Clipboard;
30class OSExchangeDataProviderAuraX11Test;
31
32// OSExchangeData::Provider implementation for aura on linux.
33class UI_BASE_EXPORT OSExchangeDataProviderAuraX11
34    : public OSExchangeData::Provider,
35      public ui::PlatformEventDispatcher {
36 public:
37  // |x_window| is the window the cursor is over, and |selection| is the set of
38  // data being offered.
39  OSExchangeDataProviderAuraX11(::Window x_window,
40                                const SelectionFormatMap& selection);
41
42  // Creates a Provider for sending drag information. This creates its own,
43  // hidden X11 window to own send data.
44  OSExchangeDataProviderAuraX11();
45
46  virtual ~OSExchangeDataProviderAuraX11();
47
48  // After all the Set* methods have built up the data we're offering, call
49  // this to take ownership of the XdndSelection clipboard.
50  void TakeOwnershipOfSelection() const;
51
52  // Retrieves a list of types we're offering. Noop if we haven't taken the
53  // selection.
54  void RetrieveTargets(std::vector<Atom>* targets) const;
55
56  // Makes a copy of the format map currently being offered.
57  SelectionFormatMap GetFormatMap() const;
58
59  const base::FilePath& file_contents_name() const {
60    return file_contents_name_;
61  }
62
63  // Overridden from OSExchangeData::Provider:
64  virtual Provider* Clone() const OVERRIDE;
65  virtual void MarkOriginatedFromRenderer() OVERRIDE;
66  virtual bool DidOriginateFromRenderer() const OVERRIDE;
67  virtual void SetString(const base::string16& data) OVERRIDE;
68  virtual void SetURL(const GURL& url, const base::string16& title) OVERRIDE;
69  virtual void SetFilename(const base::FilePath& path) OVERRIDE;
70  virtual void SetFilenames(const std::vector<FileInfo>& filenames) OVERRIDE;
71  virtual void SetPickledData(const OSExchangeData::CustomFormat& format,
72                              const Pickle& pickle) OVERRIDE;
73  virtual bool GetString(base::string16* data) const OVERRIDE;
74  virtual bool GetURLAndTitle(OSExchangeData::FilenameToURLPolicy policy,
75                              GURL* url,
76                              base::string16* title) const OVERRIDE;
77  virtual bool GetFilename(base::FilePath* path) const OVERRIDE;
78  virtual bool GetFilenames(std::vector<FileInfo>* filenames) const OVERRIDE;
79  virtual bool GetPickledData(const OSExchangeData::CustomFormat& format,
80                              Pickle* pickle) const OVERRIDE;
81  virtual bool HasString() const OVERRIDE;
82  virtual bool HasURL(OSExchangeData::FilenameToURLPolicy policy) const
83      OVERRIDE;
84  virtual bool HasFile() const OVERRIDE;
85  virtual bool HasCustomFormat(const OSExchangeData::CustomFormat& format) const
86      OVERRIDE;
87
88  virtual void SetFileContents(const base::FilePath& filename,
89                               const std::string& file_contents) OVERRIDE;
90
91  virtual void SetHtml(const base::string16& html,
92                       const GURL& base_url) OVERRIDE;
93  virtual bool GetHtml(base::string16* html, GURL* base_url) const OVERRIDE;
94  virtual bool HasHtml() const OVERRIDE;
95  virtual void SetDragImage(const gfx::ImageSkia& image,
96                            const gfx::Vector2d& cursor_offset) OVERRIDE;
97  virtual const gfx::ImageSkia& GetDragImage() const OVERRIDE;
98  virtual const gfx::Vector2d& GetDragImageOffset() const OVERRIDE;
99
100  // ui::PlatformEventDispatcher:
101  virtual bool CanDispatchEvent(const PlatformEvent& event) OVERRIDE;
102  virtual uint32_t DispatchEvent(const PlatformEvent& event) OVERRIDE;
103
104 private:
105  friend class OSExchangeDataProviderAuraX11Test;
106  typedef std::map<OSExchangeData::CustomFormat, Pickle>  PickleData;
107
108  // Returns true if |formats_| contains a string format and the string can be
109  // parsed as a URL.
110  bool GetPlainTextURL(GURL* url) const;
111
112  // Returns the targets in |format_map_|.
113  std::vector< ::Atom> GetTargets() const;
114
115  // Drag image and offset data.
116  gfx::ImageSkia drag_image_;
117  gfx::Vector2d drag_image_offset_;
118
119  // Our X11 state.
120  Display* x_display_;
121  ::Window x_root_window_;
122
123  // In X11, because the IPC parts of drag operations are implemented by
124  // XSelection, we require an x11 window to receive drag messages on. The
125  // OSExchangeDataProvider system is modeled on the Windows implementation,
126  // which does not require a window. We only sometimes have a valid window
127  // available (in the case of drag receiving). Other times, we need to create
128  // our own xwindow just to receive events on it.
129  const bool own_window_;
130
131  ::Window x_window_;
132
133  X11AtomCache atom_cache_;
134
135  // A representation of data. This is either passed to us from the other
136  // process, or built up through a sequence of Set*() calls. It can be passed
137  // to |selection_owner_| when we take the selection.
138  SelectionFormatMap format_map_;
139
140  // Auxilary data for the X Direct Save protocol.
141  base::FilePath file_contents_name_;
142
143  // Takes a snapshot of |format_map_| and offers it to other windows.
144  mutable SelectionOwner selection_owner_;
145
146  DISALLOW_COPY_AND_ASSIGN(OSExchangeDataProviderAuraX11);
147};
148
149}  // namespace ui
150
151#endif  // UI_BASE_DRAGDROP_OS_EXCHANGE_DATA_PROVIDER_AURAX11_H_
152