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 // This only exists for the sake of IPC::Message serialization and | |
47 // deserialization. | |
48 FormatType(); | |
49 ~FormatType(); | |
50 | |
51 private: | |
52 friend class Clipboard; | |
53 | |
54 #if defined(OS_WIN) | |
55 explicit FormatType(UINT native_format); | |
56 UINT data() const { return data_; } | |
57 UINT data_; | |
58 #elif defined(USE_AURA) | |
59 explicit FormatType(const std::string& native_format); | |
60 const std::string& data() const { return data_; } | |
61 std::string data_; | |
62 #elif defined(TOOLKIT_USES_GTK) | |
63 explicit FormatType(const std::string& native_format); | |
64 explicit FormatType(const GdkAtom& native_format); | |
65 const GdkAtom& data() const { return data_; } | |
66 GdkAtom data_; | |
67 #elif defined(OS_MACOSX) | |
68 explicit FormatType(NSString* native_format); | |
69 NSString* data() const { return data_; } | |
70 // We don't use scoped_nsobject because this header gets included in a lot | |
71 // of places. | |
72 NSString* data_; | |
73 #else | |
74 #error No FormatType definition. | |
75 #endif | |
76 }; | |
35 | 77 |
36 // ObjectType designates the type of data to be stored in the clipboard. This | 78 // ObjectType designates the type of data to be stored in the clipboard. This |
37 // designation is shared across all OSes. The system-specific designation | 79 // designation is shared across all OSes. The system-specific designation |
38 // is defined by FormatType. A single ObjectType might be represented by | 80 // is defined by FormatType. A single ObjectType might be represented by |
39 // several system-specific FormatTypes. For example, on Linux the CBF_TEXT | 81 // several system-specific FormatTypes. For example, on Linux the CBF_TEXT |
40 // ObjectType maps to "text/plain", "STRING", and several other formats. On | 82 // ObjectType maps to "text/plain", "STRING", and several other formats. On |
41 // windows it maps to CF_UNICODETEXT. | 83 // windows it maps to CF_UNICODETEXT. |
42 enum ObjectType { | 84 enum ObjectType { |
43 CBF_TEXT, | 85 CBF_TEXT, |
44 CBF_HTML, | 86 CBF_HTML, |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
125 #endif | 167 #endif |
126 | 168 |
127 // Returns a sequence number which uniquely identifies clipboard state. | 169 // Returns a sequence number which uniquely identifies clipboard state. |
128 // This can be used to version the data on the clipboard and determine | 170 // This can be used to version the data on the clipboard and determine |
129 // whether it has changed. | 171 // whether it has changed. |
130 uint64 GetSequenceNumber(Buffer buffer); | 172 uint64 GetSequenceNumber(Buffer buffer); |
131 | 173 |
132 // Tests whether the clipboard contains a certain format | 174 // Tests whether the clipboard contains a certain format |
133 bool IsFormatAvailable(const FormatType& format, Buffer buffer) const; | 175 bool IsFormatAvailable(const FormatType& format, Buffer buffer) const; |
134 | 176 |
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, | 177 void ReadAvailableTypes(Buffer buffer, std::vector<string16>* types, |
141 bool* contains_filenames) const; | 178 bool* contains_filenames) const; |
142 | 179 |
143 // Reads UNICODE text from the clipboard, if available. | 180 // Reads UNICODE text from the clipboard, if available. |
144 void ReadText(Buffer buffer, string16* result) const; | 181 void ReadText(Buffer buffer, string16* result) const; |
145 | 182 |
146 // Reads ASCII text from the clipboard, if available. | 183 // Reads ASCII text from the clipboard, if available. |
147 void ReadAsciiText(Buffer buffer, std::string* result) const; | 184 void ReadAsciiText(Buffer buffer, std::string* result) const; |
148 | 185 |
149 // Reads HTML from the clipboard, if available. If the HTML fragment requires | 186 // 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. | 200 // Reads a bookmark from the clipboard, if available. |
164 void ReadBookmark(string16* title, std::string* url) const; | 201 void ReadBookmark(string16* title, std::string* url) const; |
165 | 202 |
166 // Reads a file or group of files from the clipboard, if available, into the | 203 // Reads a file or group of files from the clipboard, if available, into the |
167 // out parameter. | 204 // out parameter. |
168 void ReadFile(FilePath* file) const; | 205 void ReadFile(FilePath* file) const; |
169 void ReadFiles(std::vector<FilePath>* files) const; | 206 void ReadFiles(std::vector<FilePath>* files) const; |
170 | 207 |
171 // Reads raw data from the clipboard with the given format type. Stores result | 208 // Reads raw data from the clipboard with the given format type. Stores result |
172 // as a byte vector. | 209 // as a byte vector. |
173 // TODO(dcheng): Due to platform limitations on Windows, we should make sure | 210 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 | 211 |
177 // Get format Identifiers for various types. | 212 // Converts a format name into a FormatType, registering it with the system as |
178 static FormatType GetUrlFormatType(); | 213 // needed. Due to Windows/Linux limitiations, |format_string| must never be |
179 static FormatType GetUrlWFormatType(); | 214 // controlled by the user. |
180 static FormatType GetMozUrlFormatType(); | 215 static FormatType RegisterFormatType(const std::string& format_string); |
tony
2011/12/06 18:58:29
Nit: I would perhaps name this GetFormatType since
dcheng
2011/12/06 22:57:02
Done.
| |
181 static FormatType GetPlainTextFormatType(); | 216 |
182 static FormatType GetPlainTextWFormatType(); | 217 // Helpers to serialize/deserialize FormatType to strings. These probably do |
183 static FormatType GetFilenameFormatType(); | 218 // NOT do what you want. They are intended to help serialize FormatTypes for |
184 static FormatType GetFilenameWFormatType(); | 219 // IPC messages. |
185 static FormatType GetWebKitSmartPasteFormatType(); | 220 static std::string FormatTypeToString(const FormatType& type); |
221 static FormatType StringToFormatType(const std::string& format_string); | |
tony
2011/12/06 18:58:29
Why not just call these methods SerializeFormatTyp
dcheng
2011/12/06 22:57:02
Done.
| |
222 | |
223 // Get format identifiers for various types. | |
224 static const FormatType& GetUrlFormatType(); | |
225 static const FormatType& GetUrlWFormatType(); | |
226 static const FormatType& GetMozUrlFormatType(); | |
227 static const FormatType& GetPlainTextFormatType(); | |
228 static const FormatType& GetPlainTextWFormatType(); | |
229 static const FormatType& GetFilenameFormatType(); | |
230 static const FormatType& GetFilenameWFormatType(); | |
231 static const FormatType& GetWebKitSmartPasteFormatType(); | |
186 // Win: MS HTML Format, Other: Generic HTML format | 232 // Win: MS HTML Format, Other: Generic HTML format |
187 static FormatType GetHtmlFormatType(); | 233 static const FormatType& GetHtmlFormatType(); |
188 static FormatType GetBitmapFormatType(); | 234 static const FormatType& GetBitmapFormatType(); |
189 static FormatType GetWebCustomDataFormatType(); | 235 static const FormatType& GetWebCustomDataFormatType(); |
190 | 236 |
191 // Embeds a pointer to a SharedMemory object pointed to by |bitmap_handle| | 237 // Embeds a pointer to a SharedMemory object pointed to by |bitmap_handle| |
192 // belonging to |process| into a shared bitmap [CBF_SMBITMAP] slot in | 238 // belonging to |process| into a shared bitmap [CBF_SMBITMAP] slot in |
193 // |objects|. The pointer is deleted by DispatchObjects(). | 239 // |objects|. The pointer is deleted by DispatchObjects(). |
194 // | 240 // |
195 // On non-Windows platforms, |process| is ignored. | 241 // On non-Windows platforms, |process| is ignored. |
196 static void ReplaceSharedMemHandle(ObjectMap* objects, | 242 static void ReplaceSharedMemHandle(ObjectMap* objects, |
197 base::SharedMemoryHandle bitmap_handle, | 243 base::SharedMemoryHandle bitmap_handle, |
198 base::ProcessHandle process); | 244 base::ProcessHandle process); |
199 #if defined(OS_WIN) | 245 #if defined(OS_WIN) |
200 // Firefox text/html | 246 // Firefox text/html |
201 static FormatType GetTextHtmlFormatType(); | 247 static const FormatType& GetTextHtmlFormatType(); |
202 static FormatType GetCFHDropFormatType(); | 248 static const FormatType& GetCFHDropFormatType(); |
203 static FormatType GetFileDescriptorFormatType(); | 249 static const FormatType& GetFileDescriptorFormatType(); |
204 static FormatType GetFileContentFormatZeroType(); | 250 static const FormatType& GetFileContentFormatZeroType(); |
205 #endif | 251 #endif |
206 | 252 |
207 private: | 253 private: |
208 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, SharedBitmapTest); | 254 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, SharedBitmapTest); |
209 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, EmptyHTMLTest); | 255 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, EmptyHTMLTest); |
210 | 256 |
211 void DispatchObject(ObjectType type, const ObjectMapParams& params); | 257 void DispatchObject(ObjectType type, const ObjectMapParams& params); |
212 | 258 |
213 void WriteText(const char* text_data, size_t text_len); | 259 void WriteText(const char* text_data, size_t text_len); |
214 | 260 |
215 void WriteHTML(const char* markup_data, | 261 void WriteHTML(const char* markup_data, |
216 size_t markup_len, | 262 size_t markup_len, |
217 const char* url_data, | 263 const char* url_data, |
218 size_t url_len); | 264 size_t url_len); |
219 | 265 |
220 void WriteBookmark(const char* title_data, | 266 void WriteBookmark(const char* title_data, |
221 size_t title_len, | 267 size_t title_len, |
222 const char* url_data, | 268 const char* url_data, |
223 size_t url_len); | 269 size_t url_len); |
224 | 270 |
225 void WriteWebSmartPaste(); | 271 void WriteWebSmartPaste(); |
226 | 272 |
227 void WriteBitmap(const char* pixel_data, const char* size_data); | 273 void WriteBitmap(const char* pixel_data, const char* size_data); |
228 | 274 |
229 // |format_name| is an ASCII string and should be NULL-terminated. | 275 void WriteData(const FormatType& format, |
230 void WriteData(const char* format_name, size_t format_len, | 276 const char* data_data, |
231 const char* data_data, size_t data_len); | 277 size_t data_len); |
232 #if defined(OS_WIN) | 278 #if defined(OS_WIN) |
233 void WriteBitmapFromHandle(HBITMAP source_hbitmap, | 279 void WriteBitmapFromHandle(HBITMAP source_hbitmap, |
234 const gfx::Size& size); | 280 const gfx::Size& size); |
235 | 281 |
236 // Safely write to system clipboard. Free |handle| on failure. | 282 // Safely write to system clipboard. Free |handle| on failure. |
237 void WriteToClipboard(unsigned int format, HANDLE handle); | 283 void WriteToClipboard(unsigned int format, HANDLE handle); |
238 | 284 |
239 static void ParseBookmarkClipboardFormat(const string16& bookmark, | 285 static void ParseBookmarkClipboardFormat(const string16& bookmark, |
240 string16* title, | 286 string16* title, |
241 std::string* url); | 287 std::string* url); |
(...skipping 13 matching lines...) Expand all Loading... | |
255 #elif defined(TOOLKIT_USES_GTK) | 301 #elif defined(TOOLKIT_USES_GTK) |
256 // The public API is via WriteObjects() which dispatches to multiple | 302 // The public API is via WriteObjects() which dispatches to multiple |
257 // Write*() calls, but on GTK we must write all the clipboard types | 303 // 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 | 304 // 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 | 305 // of data we intend to put on the clipboard on clipboard_data_ as |
260 // WriteObjects is running, and then at the end call SetGtkClipboard | 306 // WriteObjects is running, and then at the end call SetGtkClipboard |
261 // which replaces whatever is on the system clipboard with the | 307 // which replaces whatever is on the system clipboard with the |
262 // contents of clipboard_data_. | 308 // contents of clipboard_data_. |
263 | 309 |
264 public: | 310 public: |
265 typedef std::map<FormatType, std::pair<char*, size_t> > TargetMap; | 311 typedef std::map<std::string, std::pair<char*, size_t> > TargetMap; |
266 | 312 |
267 private: | 313 private: |
268 // Write changes to gtk clipboard. | 314 // Write changes to gtk clipboard. |
269 void SetGtkClipboard(); | 315 void SetGtkClipboard(); |
270 // Insert a mapping into clipboard_data_. | 316 // Insert a mapping into clipboard_data_. |
271 void InsertMapping(const char* key, char* data, size_t data_len); | 317 void InsertMapping(const char* key, char* data, size_t data_len); |
272 | 318 |
273 // Find the gtk clipboard for the passed buffer enum. | 319 // Find the gtk clipboard for the passed buffer enum. |
274 GtkClipboard* LookupBackingClipboard(Buffer clipboard) const; | 320 GtkClipboard* LookupBackingClipboard(Buffer clipboard) const; |
275 | 321 |
276 TargetMap* clipboard_data_; | 322 TargetMap* clipboard_data_; |
277 GtkClipboard* clipboard_; | 323 GtkClipboard* clipboard_; |
278 GtkClipboard* primary_selection_; | 324 GtkClipboard* primary_selection_; |
279 #endif | 325 #endif |
280 | 326 |
281 // MIME type constants. | 327 // MIME type constants. |
282 static const char kMimeTypeText[]; | 328 static const char kMimeTypeText[]; |
283 static const char kMimeTypeHTML[]; | 329 static const char kMimeTypeHTML[]; |
284 static const char kMimeTypePNG[]; | 330 static const char kMimeTypePNG[]; |
285 | 331 |
286 DISALLOW_COPY_AND_ASSIGN(Clipboard); | 332 DISALLOW_COPY_AND_ASSIGN(Clipboard); |
287 }; | 333 }; |
288 | 334 |
289 } // namespace ui | 335 } // namespace ui |
290 | 336 |
291 #endif // UI_BASE_CLIPBOARD_CLIPBOARD_H_ | 337 #endif // UI_BASE_CLIPBOARD_CLIPBOARD_H_ |
OLD | NEW |