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.
5 #ifndef UI_BASE_DRAGDROP_OS_EXCHANGE_DATA_H_
6 #define UI_BASE_DRAGDROP_OS_EXCHANGE_DATA_H_
8 #include "build/build_config.h"
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"
36 ///////////////////////////////////////////////////////////////////////////////
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.
44 ///////////////////////////////////////////////////////////////////////////////
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.
50 class UI_BASE_EXPORT OSExchangeData
{
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
;
57 // Enumeration of the known formats.
62 PICKLED_DATA
= 1 << 3,
64 FILE_CONTENTS
= 1 << 4,
71 // Controls whether or not filenames should be converted to file: URLs when
73 enum FilenameToURLPolicy
{ CONVERT_FILENAMES
, DO_NOT_CONVERT_FILENAMES
, };
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
);
81 base::FilePath filename
;
82 scoped_refptr
<DownloadFileProvider
> downloader
;
85 // Provider defines the platform specific part of OSExchangeData that
86 // interacts with the native system.
87 class UI_BASE_EXPORT Provider
{
90 virtual ~Provider() {}
92 virtual Provider
* Clone() const = 0;
94 virtual void MarkOriginatedFromRenderer() = 0;
95 virtual bool DidOriginateFromRenderer() const = 0;
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;
105 virtual bool GetString(base::string16
* data
) const = 0;
106 virtual bool GetURLAndTitle(FilenameToURLPolicy policy
,
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;
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;
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;
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;
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;
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;
145 // Creates the platform specific Provider.
146 static Provider
* CreateProvider();
149 // Creates an OSExchangeData with the specified provider. OSExchangeData
150 // takes ownership of the supplied provider.
151 explicit OSExchangeData(Provider
* provider
);
155 // Returns the Provider, which actually stores and manages the data.
156 const Provider
& provider() const { return *provider_
; }
157 Provider
& provider() { return *provider_
; }
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;
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.
182 const std::vector
<FileInfo
>& file_names
);
183 // Adds pickled data of the specified format.
184 void SetPickledData(const CustomFormat
& format
, const Pickle
& data
);
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
190 bool GetString(base::string16
* data
) const;
191 bool GetURLAndTitle(FilenameToURLPolicy policy
,
193 base::string16
* title
) const;
194 // Return the path of a file, if available.
195 bool GetFilename(base::FilePath
* path
) const;
197 std::vector
<FileInfo
>* file_names
) const;
198 bool GetPickledData(const CustomFormat
& format
, Pickle
* data
) const;
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;
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;
213 // Adds the bytes of a file (CFSTR_FILECONTENTS and CFSTR_FILEDESCRIPTOR on
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;
220 // Adds a download file with full path (CF_HDROP).
221 void SetDownloadFileInfo(const DownloadFileInfo
& download
);
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;
232 // Provides the actual data.
233 scoped_ptr
<Provider
> provider_
;
235 DISALLOW_COPY_AND_ASSIGN(OSExchangeData
);
240 #endif // UI_BASE_DRAGDROP_OS_EXCHANGE_DATA_H_