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_H_
6#define UI_BASE_DRAGDROP_OS_EXCHANGE_DATA_H_
7
8#include "build/build_config.h"
9
10#include <set>
11#include <string>
12
13#if defined(OS_WIN)
14#include <objidl.h>
15#elif defined(TOOLKIT_GTK)
16#include <gtk/gtk.h>
17#endif
18
19#include "base/basictypes.h"
20#include "base/files/file_path.h"
21#include "base/memory/scoped_ptr.h"
22#include "ui/base/clipboard/clipboard.h"
23#include "ui/base/dragdrop/download_file_interface.h"
24#include "ui/base/ui_export.h"
25
26class GURL;
27class Pickle;
28
29namespace gfx {
30class ImageSkia;
31class Vector2d;
32}
33
34namespace ui {
35
36///////////////////////////////////////////////////////////////////////////////
37//
38// OSExchangeData
39//  An object that holds interchange data to be sent out to OS services like
40//  clipboard, drag and drop, etc. This object exposes an API that clients can
41//  use to specify raw data and its high level type. This object takes care of
42//  translating that into something the OS can understand.
43//
44///////////////////////////////////////////////////////////////////////////////
45
46// NOTE: Support for html and file contents is required by TabContentViewWin.
47// TabContentsViewGtk uses a different class to handle drag support that does
48// not use OSExchangeData. As such, file contents and html support is only
49// compiled on windows.
50class UI_EXPORT OSExchangeData {
51 public:
52  // CustomFormats are used for non-standard data types. For example, bookmark
53  // nodes are written using a CustomFormat.
54  // TODO(dcheng): Remove this completely and just use Clipboard::FormatType.
55  typedef Clipboard::FormatType CustomFormat;
56
57  // Enumeration of the known formats.
58  enum Format {
59    STRING         = 1 << 0,
60    URL            = 1 << 1,
61    FILE_NAME      = 1 << 2,
62    PICKLED_DATA   = 1 << 3,
63#if defined(OS_WIN)
64    FILE_CONTENTS  = 1 << 4,
65#endif
66#if defined(OS_WIN) || defined(USE_AURA)
67    HTML           = 1 << 5,
68#endif
69  };
70
71  // Encapsulates the info about a file to be downloaded.
72  struct UI_EXPORT DownloadFileInfo {
73    DownloadFileInfo(const base::FilePath& filename,
74                     DownloadFileProvider* downloader);
75    ~DownloadFileInfo();
76
77    base::FilePath filename;
78    scoped_refptr<DownloadFileProvider> downloader;
79  };
80
81  // Encapsulates the info about a file.
82  struct UI_EXPORT FileInfo {
83    FileInfo(const base::FilePath& path, const base::FilePath& display_name);
84    ~FileInfo();
85
86    // The path of the file.
87    base::FilePath path;
88    // The display name of the file. This field is optional.
89    base::FilePath display_name;
90  };
91
92  // Provider defines the platform specific part of OSExchangeData that
93  // interacts with the native system.
94  class UI_EXPORT Provider {
95   public:
96    Provider() {}
97    virtual ~Provider() {}
98
99    virtual Provider* Clone() const = 0;
100
101    virtual void SetString(const base::string16& data) = 0;
102    virtual void SetURL(const GURL& url, const base::string16& title) = 0;
103    virtual void SetFilename(const base::FilePath& path) = 0;
104    virtual void SetFilenames(
105        const std::vector<FileInfo>& file_names) = 0;
106    virtual void SetPickledData(const CustomFormat& format,
107                                const Pickle& data) = 0;
108
109    virtual bool GetString(base::string16* data) const = 0;
110    virtual bool GetURLAndTitle(GURL* url, base::string16* title) const = 0;
111    virtual bool GetFilename(base::FilePath* path) const = 0;
112    virtual bool GetFilenames(
113        std::vector<FileInfo>* file_names) const = 0;
114    virtual bool GetPickledData(const CustomFormat& format,
115                                Pickle* data) const = 0;
116
117    virtual bool HasString() const = 0;
118    virtual bool HasURL() const = 0;
119    virtual bool HasFile() const = 0;
120    virtual bool HasCustomFormat(const CustomFormat& format) const = 0;
121
122#if defined(OS_WIN)
123    virtual void SetFileContents(const base::FilePath& filename,
124                                 const std::string& file_contents) = 0;
125    virtual bool GetFileContents(base::FilePath* filename,
126                                 std::string* file_contents) const = 0;
127    virtual bool HasFileContents() const = 0;
128    virtual void SetDownloadFileInfo(const DownloadFileInfo& download) = 0;
129    virtual void SetInDragLoop(bool in_drag_loop) = 0;
130#endif
131
132#if defined(OS_WIN) || defined(USE_AURA)
133    virtual void SetHtml(const base::string16& html, const GURL& base_url) = 0;
134    virtual bool GetHtml(base::string16* html, GURL* base_url) const = 0;
135    virtual bool HasHtml() const = 0;
136#endif
137
138#if defined(USE_AURA)
139    virtual void SetDragImage(const gfx::ImageSkia& image,
140                              const gfx::Vector2d& cursor_offset) = 0;
141    virtual const gfx::ImageSkia& GetDragImage() const = 0;
142    virtual const gfx::Vector2d& GetDragImageOffset() const = 0;
143#endif
144  };
145
146  // Creates the platform specific Provider.
147  static Provider* CreateProvider();
148
149  OSExchangeData();
150  // Creates an OSExchangeData with the specified provider. OSExchangeData
151  // takes ownership of the supplied provider.
152  explicit OSExchangeData(Provider* provider);
153
154  ~OSExchangeData();
155
156  // Returns the Provider, which actually stores and manages the data.
157  const Provider& provider() const { return *provider_; }
158  Provider& provider() { return *provider_; }
159
160  // These functions add data to the OSExchangeData object of various Chrome
161  // types. The OSExchangeData object takes care of translating the data into
162  // a format suitable for exchange with the OS.
163  // NOTE WELL: Typically, a data object like this will contain only one of the
164  //            following types of data. In cases where more data is held, the
165  //            order in which these functions are called is _important_!
166  //       ---> The order types are added to an OSExchangeData object controls
167  //            the order of enumeration in our IEnumFORMATETC implementation!
168  //            This comes into play when selecting the best (most preferable)
169  //            data type for insertion into a DropTarget.
170  void SetString(const base::string16& data);
171  // A URL can have an optional title in some exchange formats.
172  void SetURL(const GURL& url, const base::string16& title);
173  // A full path to a file.
174  void SetFilename(const base::FilePath& path);
175  // Full path to one or more files. See also SetFilenames() in Provider.
176  void SetFilenames(
177      const std::vector<FileInfo>& file_names);
178  // Adds pickled data of the specified format.
179  void SetPickledData(const CustomFormat& format, const Pickle& data);
180
181  // These functions retrieve data of the specified type. If data exists, the
182  // functions return and the result is in the out parameter. If the data does
183  // not exist, the out parameter is not touched. The out parameter cannot be
184  // NULL.
185  bool GetString(base::string16* data) const;
186  bool GetURLAndTitle(GURL* url, base::string16* title) const;
187  // Return the path of a file, if available.
188  bool GetFilename(base::FilePath* path) const;
189  bool GetFilenames(
190      std::vector<FileInfo>* file_names) const;
191  bool GetPickledData(const CustomFormat& format, Pickle* data) const;
192
193  // Test whether or not data of certain types is present, without actually
194  // returning anything.
195  bool HasString() const;
196  bool HasURL() const;
197  bool HasFile() const;
198  bool HasCustomFormat(const CustomFormat& format) const;
199
200  // Returns true if this OSExchangeData has data for ALL the formats in
201  // |formats| and ALL the custom formats in |custom_formats|.
202  bool HasAllFormats(int formats,
203                     const std::set<CustomFormat>& custom_formats) const;
204
205  // Returns true if this OSExchangeData has data in any of the formats in
206  // |formats| or any custom format in |custom_formats|.
207  bool HasAnyFormat(int formats,
208                     const std::set<CustomFormat>& custom_formats) const;
209
210#if defined(OS_WIN)
211  // Adds the bytes of a file (CFSTR_FILECONTENTS and CFSTR_FILEDESCRIPTOR).
212  void SetFileContents(const base::FilePath& filename,
213                       const std::string& file_contents);
214  bool GetFileContents(base::FilePath* filename,
215                       std::string* file_contents) const;
216
217  // Adds a download file with full path (CF_HDROP).
218  void SetDownloadFileInfo(const DownloadFileInfo& download);
219
220  void SetInDragLoop(bool in_drag_loop);
221#endif
222
223#if defined(OS_WIN) || defined(USE_AURA)
224  // Adds a snippet of HTML.  |html| is just raw html but this sets both
225  // text/html and CF_HTML.
226  void SetHtml(const base::string16& html, const GURL& base_url);
227  bool GetHtml(base::string16* html, GURL* base_url) const;
228#endif
229
230 private:
231  // Provides the actual data.
232  scoped_ptr<Provider> provider_;
233
234  DISALLOW_COPY_AND_ASSIGN(OSExchangeData);
235};
236
237}  // namespace ui
238
239#endif  // UI_BASE_DRAGDROP_OS_EXCHANGE_DATA_H_
240