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"
39 ///////////////////////////////////////////////////////////////////////////////
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.
47 ///////////////////////////////////////////////////////////////////////////////
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.
53 class UI_BASE_EXPORT OSExchangeData
{
55 // CustomFormats are used for non-standard data types. For example, bookmark
56 // nodes are written using a CustomFormat.
57 // TODO(dcheng): Remove this completely and just use Clipboard::FormatType.
58 typedef Clipboard::FormatType CustomFormat
;
60 // Enumeration of the known formats.
65 PICKLED_DATA
= 1 << 3,
67 FILE_CONTENTS
= 1 << 4,
74 // Controls whether or not filenames should be converted to file: URLs when
76 enum FilenameToURLPolicy
{ CONVERT_FILENAMES
, DO_NOT_CONVERT_FILENAMES
, };
78 // Encapsulates the info about a file to be downloaded.
79 struct UI_BASE_EXPORT DownloadFileInfo
{
80 DownloadFileInfo(const base::FilePath
& filename
,
81 DownloadFileProvider
* downloader
);
84 base::FilePath filename
;
85 scoped_refptr
<DownloadFileProvider
> downloader
;
88 // Provider defines the platform specific part of OSExchangeData that
89 // interacts with the native system.
90 class UI_BASE_EXPORT Provider
{
93 virtual ~Provider() {}
95 virtual Provider
* Clone() const = 0;
97 virtual void MarkOriginatedFromRenderer() = 0;
98 virtual bool DidOriginateFromRenderer() const = 0;
100 virtual void SetString(const base::string16
& data
) = 0;
101 virtual void SetURL(const GURL
& url
, const base::string16
& title
) = 0;
102 virtual void SetFilename(const base::FilePath
& path
) = 0;
103 virtual void SetFilenames(
104 const std::vector
<FileInfo
>& file_names
) = 0;
105 virtual void SetPickledData(const CustomFormat
& format
,
106 const base::Pickle
& data
) = 0;
108 virtual bool GetString(base::string16
* data
) const = 0;
109 virtual bool GetURLAndTitle(FilenameToURLPolicy policy
,
111 base::string16
* title
) const = 0;
112 virtual bool GetFilename(base::FilePath
* path
) const = 0;
113 virtual bool GetFilenames(
114 std::vector
<FileInfo
>* file_names
) const = 0;
115 virtual bool GetPickledData(const CustomFormat
& format
,
116 base::Pickle
* data
) const = 0;
118 virtual bool HasString() const = 0;
119 virtual bool HasURL(FilenameToURLPolicy policy
) const = 0;
120 virtual bool HasFile() const = 0;
121 virtual bool HasCustomFormat(const CustomFormat
& format
) const = 0;
123 #if (!defined(OS_CHROMEOS) && defined(USE_X11)) || defined(OS_WIN)
124 virtual void SetFileContents(const base::FilePath
& filename
,
125 const std::string
& file_contents
) = 0;
128 virtual bool GetFileContents(base::FilePath
* filename
,
129 std::string
* file_contents
) const = 0;
130 virtual bool HasFileContents() const = 0;
131 virtual void SetDownloadFileInfo(const DownloadFileInfo
& download
) = 0;
134 #if defined(USE_AURA)
135 virtual void SetHtml(const base::string16
& html
, const GURL
& base_url
) = 0;
136 virtual bool GetHtml(base::string16
* html
, GURL
* base_url
) const = 0;
137 virtual bool HasHtml() const = 0;
140 #if defined(USE_AURA)
141 virtual void SetDragImage(const gfx::ImageSkia
& image
,
142 const gfx::Vector2d
& cursor_offset
) = 0;
143 virtual const gfx::ImageSkia
& GetDragImage() const = 0;
144 virtual const gfx::Vector2d
& GetDragImageOffset() const = 0;
148 // Creates the platform specific Provider.
149 static Provider
* CreateProvider();
152 // Creates an OSExchangeData with the specified provider. OSExchangeData
153 // takes ownership of the supplied provider.
154 explicit OSExchangeData(Provider
* provider
);
158 // Returns the Provider, which actually stores and manages the data.
159 const Provider
& provider() const { return *provider_
; }
160 Provider
& provider() { return *provider_
; }
162 // Marks drag data as tainted if it originates from the renderer. This is used
163 // to avoid granting privileges to a renderer when dragging in tainted data,
164 // since it could allow potential escalation of privileges.
165 void MarkOriginatedFromRenderer();
166 bool DidOriginateFromRenderer() const;
168 // These functions add data to the OSExchangeData object of various Chrome
169 // types. The OSExchangeData object takes care of translating the data into
170 // a format suitable for exchange with the OS.
171 // NOTE WELL: Typically, a data object like this will contain only one of the
172 // following types of data. In cases where more data is held, the
173 // order in which these functions are called is _important_!
174 // ---> The order types are added to an OSExchangeData object controls
175 // the order of enumeration in our IEnumFORMATETC implementation!
176 // This comes into play when selecting the best (most preferable)
177 // data type for insertion into a DropTarget.
178 void SetString(const base::string16
& data
);
179 // A URL can have an optional title in some exchange formats.
180 void SetURL(const GURL
& url
, const base::string16
& title
);
181 // A full path to a file.
182 void SetFilename(const base::FilePath
& path
);
183 // Full path to one or more files. See also SetFilenames() in Provider.
185 const std::vector
<FileInfo
>& file_names
);
186 // Adds pickled data of the specified format.
187 void SetPickledData(const CustomFormat
& format
, const base::Pickle
& data
);
189 // These functions retrieve data of the specified type. If data exists, the
190 // functions return and the result is in the out parameter. If the data does
191 // not exist, the out parameter is not touched. The out parameter cannot be
193 bool GetString(base::string16
* data
) const;
194 bool GetURLAndTitle(FilenameToURLPolicy policy
,
196 base::string16
* title
) const;
197 // Return the path of a file, if available.
198 bool GetFilename(base::FilePath
* path
) const;
200 std::vector
<FileInfo
>* file_names
) const;
201 bool GetPickledData(const CustomFormat
& format
, base::Pickle
* data
) const;
203 // Test whether or not data of certain types is present, without actually
204 // returning anything.
205 bool HasString() const;
206 bool HasURL(FilenameToURLPolicy policy
) const;
207 bool HasFile() const;
208 bool HasCustomFormat(const CustomFormat
& format
) const;
210 // Returns true if this OSExchangeData has data in any of the formats in
211 // |formats| or any custom format in |custom_formats|.
212 bool HasAnyFormat(int formats
,
213 const std::set
<CustomFormat
>& custom_formats
) const;
216 // Adds the bytes of a file (CFSTR_FILECONTENTS and CFSTR_FILEDESCRIPTOR on
218 void SetFileContents(const base::FilePath
& filename
,
219 const std::string
& file_contents
);
220 bool GetFileContents(base::FilePath
* filename
,
221 std::string
* file_contents
) const;
223 // Adds a download file with full path (CF_HDROP).
224 void SetDownloadFileInfo(const DownloadFileInfo
& download
);
227 #if defined(USE_AURA)
228 // Adds a snippet of HTML. |html| is just raw html but this sets both
229 // text/html and CF_HTML.
230 void SetHtml(const base::string16
& html
, const GURL
& base_url
);
231 bool GetHtml(base::string16
* html
, GURL
* base_url
) const;
235 // Provides the actual data.
236 scoped_ptr
<Provider
> provider_
;
238 DISALLOW_COPY_AND_ASSIGN(OSExchangeData
);
243 #endif // UI_BASE_DRAGDROP_OS_EXCHANGE_DATA_H_