- add sources.
[platform/framework/web/crosswalk.git] / src / ui / base / dragdrop / os_exchange_data_provider_aurax11.h
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/message_loop/message_pump_dispatcher.h"
17 #include "base/pickle.h"
18 #include "ui/base/dragdrop/os_exchange_data.h"
19 #include "ui/base/x/selection_owner.h"
20 #include "ui/base/x/selection_requestor.h"
21 #include "ui/base/x/selection_utils.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
27 namespace ui {
28
29 class Clipboard;
30
31 // OSExchangeData::Provider implementation for aura on linux.
32 class UI_EXPORT OSExchangeDataProviderAuraX11
33     : public OSExchangeData::Provider,
34       public base::MessagePumpDispatcher {
35  public:
36   // |x_window| is the window the cursor is over, and |selection| is the set of
37   // data being offered.
38   OSExchangeDataProviderAuraX11(::Window x_window,
39                                 const SelectionFormatMap& selection);
40
41   // Creates a Provider for sending drag information. This creates its own,
42   // hidden X11 window to own send data.
43   OSExchangeDataProviderAuraX11();
44
45   virtual ~OSExchangeDataProviderAuraX11();
46
47   // After all the Set* methods have built up the data we're offering, call
48   // this to take ownership of the XdndSelection clipboard.
49   void TakeOwnershipOfSelection() const;
50
51   // Retrieves a list of types we're offering. Noop if we haven't taken the
52   // selection.
53   void RetrieveTargets(std::vector<Atom>* targets) const;
54
55   // Makes a copy of the format map currently being offered.
56   SelectionFormatMap GetFormatMap() const;
57
58   // Overridden from OSExchangeData::Provider:
59   virtual Provider* Clone() const OVERRIDE;
60   virtual void SetString(const base::string16& data) OVERRIDE;
61   virtual void SetURL(const GURL& url, const base::string16& title) OVERRIDE;
62   virtual void SetFilename(const base::FilePath& path) OVERRIDE;
63   virtual void SetFilenames(
64       const std::vector<OSExchangeData::FileInfo>& filenames) OVERRIDE;
65   virtual void SetPickledData(const OSExchangeData::CustomFormat& format,
66                               const Pickle& pickle) OVERRIDE;
67   virtual bool GetString(base::string16* data) const OVERRIDE;
68   virtual bool GetURLAndTitle(GURL* url, base::string16* title) const OVERRIDE;
69   virtual bool GetFilename(base::FilePath* path) const OVERRIDE;
70   virtual bool GetFilenames(
71       std::vector<OSExchangeData::FileInfo>* filenames) const OVERRIDE;
72   virtual bool GetPickledData(const OSExchangeData::CustomFormat& format,
73                               Pickle* pickle) const OVERRIDE;
74   virtual bool HasString() const OVERRIDE;
75   virtual bool HasURL() const OVERRIDE;
76   virtual bool HasFile() const OVERRIDE;
77   virtual bool HasCustomFormat(const OSExchangeData::CustomFormat& format) const
78       OVERRIDE;
79
80   virtual void SetHtml(const base::string16& html,
81                        const GURL& base_url) OVERRIDE;
82   virtual bool GetHtml(base::string16* html, GURL* base_url) const OVERRIDE;
83   virtual bool HasHtml() const OVERRIDE;
84   virtual void SetDragImage(const gfx::ImageSkia& image,
85                             const gfx::Vector2d& cursor_offset) OVERRIDE;
86   virtual const gfx::ImageSkia& GetDragImage() const OVERRIDE;
87   virtual const gfx::Vector2d& GetDragImageOffset() const OVERRIDE;
88
89   // Overridden from base::MessagePumpDispatcher:
90   virtual bool Dispatch(const base::NativeEvent& event) OVERRIDE;
91
92  private:
93   typedef std::map<OSExchangeData::CustomFormat, Pickle>  PickleData;
94
95   // Returns true if |formats_| contains a string format and the string can be
96   // parsed as a URL.
97   bool GetPlainTextURL(GURL* url) const;
98
99   // Returns the targets in |format_map_|.
100   std::vector< ::Atom> GetTargets() const;
101
102   // Drag image and offset data.
103   gfx::ImageSkia drag_image_;
104   gfx::Vector2d drag_image_offset_;
105
106   // Our X11 state.
107   Display* x_display_;
108   ::Window x_root_window_;
109
110   // In X11, because the IPC parts of drag operations are implemented by
111   // XSelection, we require an x11 window to receive drag messages on. The
112   // OSExchangeDataProvider system is modeled on the Windows implementation,
113   // which does not require a window. We only sometimes have a valid window
114   // available (in the case of drag receiving). Other times, we need to create
115   // our own xwindow just to receive events on it.
116   const bool own_window_;
117
118   ::Window x_window_;
119
120   X11AtomCache atom_cache_;
121
122   // A representation of data. This is either passed to us from the other
123   // process, or built up through a sequence of Set*() calls. It can be passed
124   // to |selection_owner_| when we take the selection.
125   SelectionFormatMap format_map_;
126
127   // Takes a snapshot of |format_map_| and offers it to other windows.
128   mutable SelectionOwner selection_owner_;
129
130   DISALLOW_COPY_AND_ASSIGN(OSExchangeDataProviderAuraX11);
131 };
132
133 }  // namespace ui
134
135 #endif  // UI_BASE_DRAGDROP_OS_EXCHANGE_DATA_PROVIDER_AURAX11_H_