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