OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_BASE_CLIPBOARD_CLIPBOARD_H_ | 5 #ifndef UI_BASE_CLIPBOARD_CLIPBOARD_H_ |
6 #define UI_BASE_CLIPBOARD_CLIPBOARD_H_ | 6 #define UI_BASE_CLIPBOARD_CLIPBOARD_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
14 #include "base/process.h" | 14 #include "base/process.h" |
15 #include "base/shared_memory.h" | 15 #include "base/shared_memory.h" |
16 #include "base/string16.h" | 16 #include "base/string16.h" |
17 #include "ui/base/ui_export.h" | 17 #include "ui/base/ui_export.h" |
18 | 18 |
| 19 #if defined(TOOLKIT_USES_GTK) |
| 20 #include <gdk/gdk.h> |
| 21 #endif |
| 22 |
19 namespace gfx { | 23 namespace gfx { |
20 class Size; | 24 class Size; |
21 } | 25 } |
22 | 26 |
23 class FilePath; | 27 class FilePath; |
24 class SkBitmap; | 28 class SkBitmap; |
25 | 29 |
26 #if defined(TOOLKIT_USES_GTK) | 30 #if defined(TOOLKIT_USES_GTK) |
27 typedef struct _GtkClipboard GtkClipboard; | 31 typedef struct _GtkClipboard GtkClipboard; |
28 #endif | 32 #endif |
29 | 33 |
| 34 #ifdef __OBJC__ |
| 35 @class NSString; |
| 36 #else |
| 37 class NSString; |
| 38 #endif |
| 39 |
30 namespace ui { | 40 namespace ui { |
31 | 41 |
32 class UI_EXPORT Clipboard { | 42 class UI_EXPORT Clipboard { |
33 public: | 43 public: |
34 typedef std::string FormatType; | 44 // Platform neutral holder for native data representation of a clipboard type. |
| 45 struct UI_EXPORT FormatType { |
| 46 FormatType(); |
| 47 ~FormatType(); |
| 48 |
| 49 std::string Serialize() const; |
| 50 static FormatType Deserialize(const std::string& serialization); |
| 51 |
| 52 private: |
| 53 friend class Clipboard; |
| 54 |
| 55 bool Equals(const FormatType& other) const; |
| 56 |
| 57 #if defined(OS_WIN) |
| 58 explicit FormatType(UINT native_format); |
| 59 UINT ToUINT() const { return data_; } |
| 60 UINT data_; |
| 61 #elif defined(USE_AURA) |
| 62 explicit FormatType(const std::string& native_format); |
| 63 const std::string& ToString() const { return data_; } |
| 64 std::string data_; |
| 65 #elif defined(TOOLKIT_USES_GTK) |
| 66 explicit FormatType(const std::string& native_format); |
| 67 explicit FormatType(const GdkAtom& native_format); |
| 68 const GdkAtom& ToGdkAtom() const { return data_; } |
| 69 GdkAtom data_; |
| 70 #elif defined(OS_MACOSX) |
| 71 public: |
| 72 FormatType(const FormatType& other); |
| 73 FormatType& operator=(const FormatType& other); |
| 74 private: |
| 75 explicit FormatType(NSString* native_format); |
| 76 NSString* ToNSString() const { return data_; } |
| 77 NSString* data_; |
| 78 #else |
| 79 #error No FormatType definition. |
| 80 #endif |
| 81 }; |
35 | 82 |
36 // ObjectType designates the type of data to be stored in the clipboard. This | 83 // ObjectType designates the type of data to be stored in the clipboard. This |
37 // designation is shared across all OSes. The system-specific designation | 84 // designation is shared across all OSes. The system-specific designation |
38 // is defined by FormatType. A single ObjectType might be represented by | 85 // is defined by FormatType. A single ObjectType might be represented by |
39 // several system-specific FormatTypes. For example, on Linux the CBF_TEXT | 86 // several system-specific FormatTypes. For example, on Linux the CBF_TEXT |
40 // ObjectType maps to "text/plain", "STRING", and several other formats. On | 87 // ObjectType maps to "text/plain", "STRING", and several other formats. On |
41 // windows it maps to CF_UNICODETEXT. | 88 // windows it maps to CF_UNICODETEXT. |
42 enum ObjectType { | 89 enum ObjectType { |
43 CBF_TEXT, | 90 CBF_TEXT, |
44 CBF_HTML, | 91 CBF_HTML, |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 #endif | 172 #endif |
126 | 173 |
127 // Returns a sequence number which uniquely identifies clipboard state. | 174 // Returns a sequence number which uniquely identifies clipboard state. |
128 // This can be used to version the data on the clipboard and determine | 175 // This can be used to version the data on the clipboard and determine |
129 // whether it has changed. | 176 // whether it has changed. |
130 uint64 GetSequenceNumber(Buffer buffer); | 177 uint64 GetSequenceNumber(Buffer buffer); |
131 | 178 |
132 // Tests whether the clipboard contains a certain format | 179 // Tests whether the clipboard contains a certain format |
133 bool IsFormatAvailable(const FormatType& format, Buffer buffer) const; | 180 bool IsFormatAvailable(const FormatType& format, Buffer buffer) const; |
134 | 181 |
135 // As above, but instead of interpreting |format| by some platform-specific | |
136 // definition, interpret it as a literal MIME type. | |
137 bool IsFormatAvailableByString(const std::string& format, | |
138 Buffer buffer) const; | |
139 | |
140 void ReadAvailableTypes(Buffer buffer, std::vector<string16>* types, | 182 void ReadAvailableTypes(Buffer buffer, std::vector<string16>* types, |
141 bool* contains_filenames) const; | 183 bool* contains_filenames) const; |
142 | 184 |
143 // Reads UNICODE text from the clipboard, if available. | 185 // Reads UNICODE text from the clipboard, if available. |
144 void ReadText(Buffer buffer, string16* result) const; | 186 void ReadText(Buffer buffer, string16* result) const; |
145 | 187 |
146 // Reads ASCII text from the clipboard, if available. | 188 // Reads ASCII text from the clipboard, if available. |
147 void ReadAsciiText(Buffer buffer, std::string* result) const; | 189 void ReadAsciiText(Buffer buffer, std::string* result) const; |
148 | 190 |
149 // Reads HTML from the clipboard, if available. If the HTML fragment requires | 191 // Reads HTML from the clipboard, if available. If the HTML fragment requires |
(...skipping 13 matching lines...) Expand all Loading... |
163 // Reads a bookmark from the clipboard, if available. | 205 // Reads a bookmark from the clipboard, if available. |
164 void ReadBookmark(string16* title, std::string* url) const; | 206 void ReadBookmark(string16* title, std::string* url) const; |
165 | 207 |
166 // Reads a file or group of files from the clipboard, if available, into the | 208 // Reads a file or group of files from the clipboard, if available, into the |
167 // out parameter. | 209 // out parameter. |
168 void ReadFile(FilePath* file) const; | 210 void ReadFile(FilePath* file) const; |
169 void ReadFiles(std::vector<FilePath>* files) const; | 211 void ReadFiles(std::vector<FilePath>* files) const; |
170 | 212 |
171 // Reads raw data from the clipboard with the given format type. Stores result | 213 // Reads raw data from the clipboard with the given format type. Stores result |
172 // as a byte vector. | 214 // as a byte vector. |
173 // TODO(dcheng): Due to platform limitations on Windows, we should make sure | 215 void ReadData(const FormatType& format, std::string* result) const; |
174 // format is never controlled by the user. | |
175 void ReadData(const std::string& format, std::string* result) const; | |
176 | 216 |
177 // Get format Identifiers for various types. | 217 // Gets the FormatType corresponding to an arbitrary format string, |
178 static FormatType GetUrlFormatType(); | 218 // registering it with the system if needed. Due to Windows/Linux |
179 static FormatType GetUrlWFormatType(); | 219 // limitiations, |format_string| must never be controlled by the user. |
180 static FormatType GetMozUrlFormatType(); | 220 static FormatType GetFormatType(const std::string& format_string); |
181 static FormatType GetPlainTextFormatType(); | 221 |
182 static FormatType GetPlainTextWFormatType(); | 222 // Get format identifiers for various types. |
183 static FormatType GetFilenameFormatType(); | 223 static const FormatType& GetUrlFormatType(); |
184 static FormatType GetFilenameWFormatType(); | 224 static const FormatType& GetUrlWFormatType(); |
185 static FormatType GetWebKitSmartPasteFormatType(); | 225 static const FormatType& GetMozUrlFormatType(); |
| 226 static const FormatType& GetPlainTextFormatType(); |
| 227 static const FormatType& GetPlainTextWFormatType(); |
| 228 static const FormatType& GetFilenameFormatType(); |
| 229 static const FormatType& GetFilenameWFormatType(); |
| 230 static const FormatType& GetWebKitSmartPasteFormatType(); |
186 // Win: MS HTML Format, Other: Generic HTML format | 231 // Win: MS HTML Format, Other: Generic HTML format |
187 static FormatType GetHtmlFormatType(); | 232 static const FormatType& GetHtmlFormatType(); |
188 static FormatType GetBitmapFormatType(); | 233 static const FormatType& GetBitmapFormatType(); |
189 static FormatType GetWebCustomDataFormatType(); | 234 static const FormatType& GetWebCustomDataFormatType(); |
190 | 235 |
191 // Embeds a pointer to a SharedMemory object pointed to by |bitmap_handle| | 236 // Embeds a pointer to a SharedMemory object pointed to by |bitmap_handle| |
192 // belonging to |process| into a shared bitmap [CBF_SMBITMAP] slot in | 237 // belonging to |process| into a shared bitmap [CBF_SMBITMAP] slot in |
193 // |objects|. The pointer is deleted by DispatchObjects(). | 238 // |objects|. The pointer is deleted by DispatchObjects(). |
194 // | 239 // |
195 // On non-Windows platforms, |process| is ignored. | 240 // On non-Windows platforms, |process| is ignored. |
196 static void ReplaceSharedMemHandle(ObjectMap* objects, | 241 static void ReplaceSharedMemHandle(ObjectMap* objects, |
197 base::SharedMemoryHandle bitmap_handle, | 242 base::SharedMemoryHandle bitmap_handle, |
198 base::ProcessHandle process); | 243 base::ProcessHandle process); |
199 #if defined(OS_WIN) | 244 #if defined(OS_WIN) |
200 // Firefox text/html | 245 // Firefox text/html |
201 static FormatType GetTextHtmlFormatType(); | 246 static const FormatType& GetTextHtmlFormatType(); |
202 static FormatType GetCFHDropFormatType(); | 247 static const FormatType& GetCFHDropFormatType(); |
203 static FormatType GetFileDescriptorFormatType(); | 248 static const FormatType& GetFileDescriptorFormatType(); |
204 static FormatType GetFileContentFormatZeroType(); | 249 static const FormatType& GetFileContentFormatZeroType(); |
205 #endif | 250 #endif |
206 | 251 |
207 private: | 252 private: |
208 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, SharedBitmapTest); | 253 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, SharedBitmapTest); |
209 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, EmptyHTMLTest); | 254 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, EmptyHTMLTest); |
210 | 255 |
211 void DispatchObject(ObjectType type, const ObjectMapParams& params); | 256 void DispatchObject(ObjectType type, const ObjectMapParams& params); |
212 | 257 |
213 void WriteText(const char* text_data, size_t text_len); | 258 void WriteText(const char* text_data, size_t text_len); |
214 | 259 |
215 void WriteHTML(const char* markup_data, | 260 void WriteHTML(const char* markup_data, |
216 size_t markup_len, | 261 size_t markup_len, |
217 const char* url_data, | 262 const char* url_data, |
218 size_t url_len); | 263 size_t url_len); |
219 | 264 |
220 void WriteBookmark(const char* title_data, | 265 void WriteBookmark(const char* title_data, |
221 size_t title_len, | 266 size_t title_len, |
222 const char* url_data, | 267 const char* url_data, |
223 size_t url_len); | 268 size_t url_len); |
224 | 269 |
225 void WriteWebSmartPaste(); | 270 void WriteWebSmartPaste(); |
226 | 271 |
227 void WriteBitmap(const char* pixel_data, const char* size_data); | 272 void WriteBitmap(const char* pixel_data, const char* size_data); |
228 | 273 |
229 // |format_name| is an ASCII string and should be NULL-terminated. | 274 void WriteData(const FormatType& format, |
230 void WriteData(const char* format_name, size_t format_len, | 275 const char* data_data, |
231 const char* data_data, size_t data_len); | 276 size_t data_len); |
232 #if defined(OS_WIN) | 277 #if defined(OS_WIN) |
233 void WriteBitmapFromHandle(HBITMAP source_hbitmap, | 278 void WriteBitmapFromHandle(HBITMAP source_hbitmap, |
234 const gfx::Size& size); | 279 const gfx::Size& size); |
235 | 280 |
236 // Safely write to system clipboard. Free |handle| on failure. | 281 // Safely write to system clipboard. Free |handle| on failure. |
237 void WriteToClipboard(unsigned int format, HANDLE handle); | 282 void WriteToClipboard(unsigned int format, HANDLE handle); |
238 | 283 |
239 static void ParseBookmarkClipboardFormat(const string16& bookmark, | 284 static void ParseBookmarkClipboardFormat(const string16& bookmark, |
240 string16* title, | 285 string16* title, |
241 std::string* url); | 286 std::string* url); |
(...skipping 13 matching lines...) Expand all Loading... |
255 #elif defined(TOOLKIT_USES_GTK) | 300 #elif defined(TOOLKIT_USES_GTK) |
256 // The public API is via WriteObjects() which dispatches to multiple | 301 // The public API is via WriteObjects() which dispatches to multiple |
257 // Write*() calls, but on GTK we must write all the clipboard types | 302 // Write*() calls, but on GTK we must write all the clipboard types |
258 // in a single GTK call. To support this we store the current set | 303 // in a single GTK call. To support this we store the current set |
259 // of data we intend to put on the clipboard on clipboard_data_ as | 304 // of data we intend to put on the clipboard on clipboard_data_ as |
260 // WriteObjects is running, and then at the end call SetGtkClipboard | 305 // WriteObjects is running, and then at the end call SetGtkClipboard |
261 // which replaces whatever is on the system clipboard with the | 306 // which replaces whatever is on the system clipboard with the |
262 // contents of clipboard_data_. | 307 // contents of clipboard_data_. |
263 | 308 |
264 public: | 309 public: |
265 typedef std::map<FormatType, std::pair<char*, size_t> > TargetMap; | 310 typedef std::map<std::string, std::pair<char*, size_t> > TargetMap; |
266 | 311 |
267 private: | 312 private: |
268 // Write changes to gtk clipboard. | 313 // Write changes to gtk clipboard. |
269 void SetGtkClipboard(); | 314 void SetGtkClipboard(); |
270 // Insert a mapping into clipboard_data_. | 315 // Insert a mapping into clipboard_data_. |
271 void InsertMapping(const char* key, char* data, size_t data_len); | 316 void InsertMapping(const char* key, char* data, size_t data_len); |
272 | 317 |
273 // Find the gtk clipboard for the passed buffer enum. | 318 // Find the gtk clipboard for the passed buffer enum. |
274 GtkClipboard* LookupBackingClipboard(Buffer clipboard) const; | 319 GtkClipboard* LookupBackingClipboard(Buffer clipboard) const; |
275 | 320 |
276 TargetMap* clipboard_data_; | 321 TargetMap* clipboard_data_; |
277 GtkClipboard* clipboard_; | 322 GtkClipboard* clipboard_; |
278 GtkClipboard* primary_selection_; | 323 GtkClipboard* primary_selection_; |
279 #endif | 324 #endif |
280 | 325 |
281 // MIME type constants. | 326 // MIME type constants. |
282 static const char kMimeTypeText[]; | 327 static const char kMimeTypeText[]; |
283 static const char kMimeTypeHTML[]; | 328 static const char kMimeTypeHTML[]; |
284 static const char kMimeTypePNG[]; | 329 static const char kMimeTypePNG[]; |
285 | 330 |
286 DISALLOW_COPY_AND_ASSIGN(Clipboard); | 331 DISALLOW_COPY_AND_ASSIGN(Clipboard); |
287 }; | 332 }; |
288 | 333 |
289 } // namespace ui | 334 } // namespace ui |
290 | 335 |
291 #endif // UI_BASE_CLIPBOARD_CLIPBOARD_H_ | 336 #endif // UI_BASE_CLIPBOARD_CLIPBOARD_H_ |
OLD | NEW |