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#endif
16
17#include "base/basictypes.h"
18#include "base/files/file_path.h"
19#include "base/memory/scoped_ptr.h"
20#include "ui/base/clipboard/clipboard.h"
21#include "ui/base/dragdrop/download_file_interface.h"
22#include "ui/base/ui_base_export.h"
23
24class GURL;
25class Pickle;
26
27namespace gfx {
28class ImageSkia;
29class Vector2d;
30}
31
32namespace ui {
33
34struct FileInfo;
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_BASE_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(USE_AURA)
67    HTML           = 1 << 5,
68#endif
69  };
70
71  // Controls whether or not filenames should be converted to file: URLs when
72  // getting a URL.
73  enum FilenameToURLPolicy { CONVERT_FILENAMES, DO_NOT_CONVERT_FILENAMES, };
74
75  // Encapsulates the info about a file to be downloaded.
76  struct UI_BASE_EXPORT DownloadFileInfo {
77    DownloadFileInfo(const base::FilePath& filename,
78                     DownloadFileProvider* downloader);
79    ~DownloadFileInfo();
80
81    base::FilePath filename;
82    scoped_refptr<DownloadFileProvider> downloader;
83  };
84
85  // Provider defines the platform specific part of OSExchangeData that
86  // interacts with the native system.
87  class UI_BASE_EXPORT Provider {
88   public:
89    Provider() {}
90    virtual ~Provider() {}
91
92    virtual Provider* Clone() const = 0;
93
94    virtual void MarkOriginatedFromRenderer() = 0;
95    virtual bool DidOriginateFromRenderer() const = 0;
96
97    virtual void SetString(const base::string16& data) = 0;
98    virtual void SetURL(const GURL& url, const base::string16& title) = 0;
99    virtual void SetFilename(const base::FilePath& path) = 0;
100    virtual void SetFilenames(
101        const std::vector<FileInfo>& file_names) = 0;
102    virtual void SetPickledData(const CustomFormat& format,
103                                const Pickle& data) = 0;
104
105    virtual bool GetString(base::string16* data) const = 0;
106    virtual bool GetURLAndTitle(FilenameToURLPolicy policy,
107                                GURL* url,
108                                base::string16* title) const = 0;
109    virtual bool GetFilename(base::FilePath* path) const = 0;
110    virtual bool GetFilenames(
111        std::vector<FileInfo>* file_names) const = 0;
112    virtual bool GetPickledData(const CustomFormat& format,
113                                Pickle* data) const = 0;
114
115    virtual bool HasString() const = 0;
116    virtual bool HasURL(FilenameToURLPolicy policy) const = 0;
117    virtual bool HasFile() const = 0;
118    virtual bool HasCustomFormat(const CustomFormat& format) const = 0;
119
120#if (!defined(OS_CHROMEOS) && defined(USE_X11)) || defined(OS_WIN)
121    virtual void SetFileContents(const base::FilePath& filename,
122                                 const std::string& file_contents) = 0;
123#endif
124#if defined(OS_WIN)
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#endif
130
131#if defined(USE_AURA)
132    virtual void SetHtml(const base::string16& html, const GURL& base_url) = 0;
133    virtual bool GetHtml(base::string16* html, GURL* base_url) const = 0;
134    virtual bool HasHtml() const = 0;
135#endif
136
137#if defined(USE_AURA)
138    virtual void SetDragImage(const gfx::ImageSkia& image,
139                              const gfx::Vector2d& cursor_offset) = 0;
140    virtual const gfx::ImageSkia& GetDragImage() const = 0;
141    virtual const gfx::Vector2d& GetDragImageOffset() const = 0;
142#endif
143  };
144
145  // Creates the platform specific Provider.
146  static Provider* CreateProvider();
147
148  OSExchangeData();
149  // Creates an OSExchangeData with the specified provider. OSExchangeData
150  // takes ownership of the supplied provider.
151  explicit OSExchangeData(Provider* provider);
152
153  ~OSExchangeData();
154
155  // Returns the Provider, which actually stores and manages the data.
156  const Provider& provider() const { return *provider_; }
157  Provider& provider() { return *provider_; }
158
159  // Marks drag data as tainted if it originates from the renderer. This is used
160  // to avoid granting privileges to a renderer when dragging in tainted data,
161  // since it could allow potential escalation of privileges.
162  void MarkOriginatedFromRenderer();
163  bool DidOriginateFromRenderer() const;
164
165  // These functions add data to the OSExchangeData object of various Chrome
166  // types. The OSExchangeData object takes care of translating the data into
167  // a format suitable for exchange with the OS.
168  // NOTE WELL: Typically, a data object like this will contain only one of the
169  //            following types of data. In cases where more data is held, the
170  //            order in which these functions are called is _important_!
171  //       ---> The order types are added to an OSExchangeData object controls
172  //            the order of enumeration in our IEnumFORMATETC implementation!
173  //            This comes into play when selecting the best (most preferable)
174  //            data type for insertion into a DropTarget.
175  void SetString(const base::string16& data);
176  // A URL can have an optional title in some exchange formats.
177  void SetURL(const GURL& url, const base::string16& title);
178  // A full path to a file.
179  void SetFilename(const base::FilePath& path);
180  // Full path to one or more files. See also SetFilenames() in Provider.
181  void SetFilenames(
182      const std::vector<FileInfo>& file_names);
183  // Adds pickled data of the specified format.
184  void SetPickledData(const CustomFormat& format, const Pickle& data);
185
186  // These functions retrieve data of the specified type. If data exists, the
187  // functions return and the result is in the out parameter. If the data does
188  // not exist, the out parameter is not touched. The out parameter cannot be
189  // NULL.
190  bool GetString(base::string16* data) const;
191  bool GetURLAndTitle(FilenameToURLPolicy policy,
192                      GURL* url,
193                      base::string16* title) const;
194  // Return the path of a file, if available.
195  bool GetFilename(base::FilePath* path) const;
196  bool GetFilenames(
197      std::vector<FileInfo>* file_names) const;
198  bool GetPickledData(const CustomFormat& format, Pickle* data) const;
199
200  // Test whether or not data of certain types is present, without actually
201  // returning anything.
202  bool HasString() const;
203  bool HasURL(FilenameToURLPolicy policy) const;
204  bool HasFile() const;
205  bool HasCustomFormat(const CustomFormat& format) const;
206
207  // Returns true if this OSExchangeData has data in any of the formats in
208  // |formats| or any custom format in |custom_formats|.
209  bool HasAnyFormat(int formats,
210                     const std::set<CustomFormat>& custom_formats) const;
211
212#if defined(OS_WIN)
213  // Adds the bytes of a file (CFSTR_FILECONTENTS and CFSTR_FILEDESCRIPTOR on
214  // Windows).
215  void SetFileContents(const base::FilePath& filename,
216                       const std::string& file_contents);
217  bool GetFileContents(base::FilePath* filename,
218                       std::string* file_contents) const;
219
220  // Adds a download file with full path (CF_HDROP).
221  void SetDownloadFileInfo(const DownloadFileInfo& download);
222#endif
223
224#if defined(USE_AURA)
225  // Adds a snippet of HTML.  |html| is just raw html but this sets both
226  // text/html and CF_HTML.
227  void SetHtml(const base::string16& html, const GURL& base_url);
228  bool GetHtml(base::string16* html, GURL* base_url) const;
229#endif
230
231 private:
232  // Provides the actual data.
233  scoped_ptr<Provider> provider_;
234
235  DISALLOW_COPY_AND_ASSIGN(OSExchangeData);
236};
237
238}  // namespace ui
239
240#endif  // UI_BASE_DRAGDROP_OS_EXCHANGE_DATA_H_
241