1 // Copyright (c) 2010 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 APP_OS_EXCHANGE_DATA_H_
6 #define APP_OS_EXCHANGE_DATA_H_
9 #include "build/build_config.h"
16 #elif !defined(OS_MACOSX)
20 #include "app/download_file_interface.h"
21 #include "base/basictypes.h"
22 #include "base/file_path.h"
23 #include "base/scoped_ptr.h"
28 ///////////////////////////////////////////////////////////////////////////////
31 // An object that holds interchange data to be sent out to OS services like
32 // clipboard, drag and drop, etc. This object exposes an API that clients can
33 // use to specify raw data and its high level type. This object takes care of
34 // translating that into something the OS can understand.
36 ///////////////////////////////////////////////////////////////////////////////
38 // NOTE: Support for html and file contents is required by TabContentViewWin.
39 // TabContentsViewGtk uses a different class to handle drag support that does
40 // not use OSExchangeData. As such, file contents and html support is only
41 // compiled on windows.
42 class OSExchangeData
{
44 // CustomFormats are used for non-standard data types. For example, bookmark
45 // nodes are written using a CustomFormat.
47 typedef CLIPFORMAT CustomFormat
;
48 #elif !defined(OS_MACOSX)
49 typedef GdkAtom CustomFormat
;
52 // Enumeration of the known formats.
57 PICKLED_DATA
= 1 << 3,
59 FILE_CONTENTS
= 1 << 4,
64 // Encapsulates the info about a file to be downloaded.
65 struct DownloadFileInfo
{
66 DownloadFileInfo(const FilePath
& filename
,
67 DownloadFileProvider
* downloader
);
71 scoped_refptr
<DownloadFileProvider
> downloader
;
74 // Provider defines the platform specific part of OSExchangeData that
75 // interacts with the native system.
79 virtual ~Provider() {}
81 virtual void SetString(const std::wstring
& data
) = 0;
82 virtual void SetURL(const GURL
& url
, const std::wstring
& title
) = 0;
83 virtual void SetFilename(const std::wstring
& full_path
) = 0;
84 virtual void SetPickledData(CustomFormat format
, const Pickle
& data
) = 0;
86 virtual bool GetString(std::wstring
* data
) const = 0;
87 virtual bool GetURLAndTitle(GURL
* url
, std::wstring
* title
) const = 0;
88 virtual bool GetFilename(std::wstring
* full_path
) const = 0;
89 virtual bool GetPickledData(CustomFormat format
, Pickle
* data
) const = 0;
91 virtual bool HasString() const = 0;
92 virtual bool HasURL() const = 0;
93 virtual bool HasFile() const = 0;
94 virtual bool HasCustomFormat(
95 OSExchangeData::CustomFormat format
) const = 0;
98 virtual void SetFileContents(const std::wstring
& filename
,
99 const std::string
& file_contents
) = 0;
100 virtual void SetHtml(const std::wstring
& html
, const GURL
& base_url
) = 0;
101 virtual bool GetFileContents(std::wstring
* filename
,
102 std::string
* file_contents
) const = 0;
103 virtual bool GetHtml(std::wstring
* html
, GURL
* base_url
) const = 0;
104 virtual bool HasFileContents() const = 0;
105 virtual bool HasHtml() const = 0;
106 virtual void SetDownloadFileInfo(const DownloadFileInfo
& download
) = 0;
111 // Creates an OSExchangeData with the specified provider. OSExchangeData
112 // takes ownership of the supplied provider.
113 explicit OSExchangeData(Provider
* provider
);
117 // Registers the specific string as a possible format for data.
118 static CustomFormat
RegisterCustomFormat(const std::string
& type
);
120 // Returns the Provider, which actually stores and manages the data.
121 const Provider
& provider() const { return *provider_
; }
122 Provider
& provider() { return *provider_
; }
124 // These functions add data to the OSExchangeData object of various Chrome
125 // types. The OSExchangeData object takes care of translating the data into
126 // a format suitable for exchange with the OS.
127 // NOTE WELL: Typically, a data object like this will contain only one of the
128 // following types of data. In cases where more data is held, the
129 // order in which these functions are called is _important_!
130 // ---> The order types are added to an OSExchangeData object controls
131 // the order of enumeration in our IEnumFORMATETC implementation!
132 // This comes into play when selecting the best (most preferable)
133 // data type for insertion into a DropTarget.
134 void SetString(const std::wstring
& data
);
135 // A URL can have an optional title in some exchange formats.
136 void SetURL(const GURL
& url
, const std::wstring
& title
);
137 // A full path to a file.
138 // TODO: convert to Filepath.
139 void SetFilename(const std::wstring
& full_path
);
140 // Adds pickled data of the specified format.
141 void SetPickledData(CustomFormat format
, const Pickle
& data
);
143 // These functions retrieve data of the specified type. If data exists, the
144 // functions return and the result is in the out parameter. If the data does
145 // not exist, the out parameter is not touched. The out parameter cannot be
147 bool GetString(std::wstring
* data
) const;
148 bool GetURLAndTitle(GURL
* url
, std::wstring
* title
) const;
149 // Return the path of a file, if available.
150 bool GetFilename(std::wstring
* full_path
) const;
151 bool GetPickledData(CustomFormat format
, Pickle
* data
) const;
153 // Test whether or not data of certain types is present, without actually
154 // returning anything.
155 bool HasString() const;
157 bool HasFile() const;
158 bool HasCustomFormat(CustomFormat format
) const;
160 // Returns true if this OSExchangeData has data for ALL the formats in
161 // |formats| and ALL the custom formats in |custom_formats|.
162 bool HasAllFormats(int formats
,
163 const std::set
<CustomFormat
>& custom_formats
) const;
165 // Returns true if this OSExchangeData has data in any of the formats in
166 // |formats| or any custom format in |custom_formats|.
167 bool HasAnyFormat(int formats
,
168 const std::set
<CustomFormat
>& custom_formats
) const;
171 // Adds the bytes of a file (CFSTR_FILECONTENTS and CFSTR_FILEDESCRIPTOR).
172 void SetFileContents(const std::wstring
& filename
,
173 const std::string
& file_contents
);
174 // Adds a snippet of HTML. |html| is just raw html but this sets both
175 // text/html and CF_HTML.
176 void SetHtml(const std::wstring
& html
, const GURL
& base_url
);
177 bool GetFileContents(std::wstring
* filename
,
178 std::string
* file_contents
) const;
179 bool GetHtml(std::wstring
* html
, GURL
* base_url
) const;
181 // Adds a download file with full path (CF_HDROP).
182 void SetDownloadFileInfo(const DownloadFileInfo
& download
);
186 // Creates the platform specific Provider.
187 static Provider
* CreateProvider();
189 // Provides the actual data.
190 scoped_ptr
<Provider
> provider_
;
192 DISALLOW_COPY_AND_ASSIGN(OSExchangeData
);
195 #endif // APP_OS_EXCHANGE_DATA_H_