OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 14 matching lines...) Expand all Loading... |
25 #endif | 25 #endif |
26 | 26 |
27 namespace base { | 27 namespace base { |
28 class FilePath; | 28 class FilePath; |
29 | 29 |
30 namespace win { | 30 namespace win { |
31 class MessageWindow; | 31 class MessageWindow; |
32 } // namespace win | 32 } // namespace win |
33 } // namespace base | 33 } // namespace base |
34 | 34 |
| 35 // TODO(dcheng): Temporary until the IPC layer doesn't use WriteObjects(). |
| 36 namespace content { |
| 37 class ClipboardMessageFilter; |
| 38 } |
| 39 |
35 namespace gfx { | 40 namespace gfx { |
36 class Size; | 41 class Size; |
37 } | 42 } |
38 | 43 |
39 class SkBitmap; | 44 class SkBitmap; |
40 | 45 |
41 #ifdef __OBJC__ | 46 #ifdef __OBJC__ |
42 @class NSString; | 47 @class NSString; |
43 #else | 48 #else |
44 class NSString; | 49 class NSString; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 #elif defined(OS_MACOSX) | 111 #elif defined(OS_MACOSX) |
107 explicit FormatType(NSString* native_format); | 112 explicit FormatType(NSString* native_format); |
108 NSString* data_; | 113 NSString* data_; |
109 #else | 114 #else |
110 #error No FormatType definition. | 115 #error No FormatType definition. |
111 #endif | 116 #endif |
112 | 117 |
113 // Copyable and assignable, since this is essentially an opaque value type. | 118 // Copyable and assignable, since this is essentially an opaque value type. |
114 }; | 119 }; |
115 | 120 |
| 121 // TODO(dcheng): Make this private once the IPC layer no longer needs to |
| 122 // serialize this information. |
| 123 // ObjectType designates the type of data to be stored in the clipboard. This |
| 124 // designation is shared across all OSes. The system-specific designation |
| 125 // is defined by FormatType. A single ObjectType might be represented by |
| 126 // several system-specific FormatTypes. For example, on Linux the CBF_TEXT |
| 127 // ObjectType maps to "text/plain", "STRING", and several other formats. On |
| 128 // windows it maps to CF_UNICODETEXT. |
| 129 enum ObjectType { |
| 130 CBF_TEXT, |
| 131 CBF_HTML, |
| 132 CBF_RTF, |
| 133 CBF_BOOKMARK, |
| 134 CBF_WEBKIT, |
| 135 CBF_SMBITMAP, // Bitmap from shared memory. |
| 136 CBF_DATA, // Arbitrary block of bytes. |
| 137 }; |
| 138 |
| 139 // ObjectMap is a map from ObjectType to associated data. |
| 140 // The data is organized differently for each ObjectType. The following |
| 141 // table summarizes what kind of data is stored for each key. |
| 142 // * indicates an optional argument. |
| 143 // |
| 144 // Key Arguments Type |
| 145 // ------------------------------------- |
| 146 // CBF_TEXT text char array |
| 147 // CBF_HTML html char array |
| 148 // url* char array |
| 149 // CBF_RTF data byte array |
| 150 // CBF_BOOKMARK html char array |
| 151 // url char array |
| 152 // CBF_WEBKIT none empty vector |
| 153 // CBF_SMBITMAP shared_mem A pointer to an unmapped base::SharedMemory |
| 154 // object containing the bitmap data. The bitmap |
| 155 // data should be premultiplied. |
| 156 // size gfx::Size struct |
| 157 // CBF_DATA format char array |
| 158 // data byte array |
| 159 typedef std::vector<char> ObjectMapParam; |
| 160 typedef std::vector<ObjectMapParam> ObjectMapParams; |
| 161 typedef std::map<int /* ObjectType */, ObjectMapParams> ObjectMap; |
| 162 |
116 static bool IsSupportedClipboardType(int32 type) { | 163 static bool IsSupportedClipboardType(int32 type) { |
117 switch (type) { | 164 switch (type) { |
118 case CLIPBOARD_TYPE_COPY_PASTE: | 165 case CLIPBOARD_TYPE_COPY_PASTE: |
119 return true; | 166 return true; |
120 #if !defined(OS_WIN) && !defined(OS_MACOSX) && !defined(OS_CHROMEOS) | 167 #if !defined(OS_WIN) && !defined(OS_MACOSX) && !defined(OS_CHROMEOS) |
121 case CLIPBOARD_TYPE_SELECTION: | 168 case CLIPBOARD_TYPE_SELECTION: |
122 return true; | 169 return true; |
123 #endif | 170 #endif |
124 } | 171 } |
125 return false; | 172 return false; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 static const FormatType& GetWebKitSmartPasteFormatType(); | 260 static const FormatType& GetWebKitSmartPasteFormatType(); |
214 // Win: MS HTML Format, Other: Generic HTML format | 261 // Win: MS HTML Format, Other: Generic HTML format |
215 static const FormatType& GetHtmlFormatType(); | 262 static const FormatType& GetHtmlFormatType(); |
216 static const FormatType& GetRtfFormatType(); | 263 static const FormatType& GetRtfFormatType(); |
217 static const FormatType& GetBitmapFormatType(); | 264 static const FormatType& GetBitmapFormatType(); |
218 // TODO(raymes): Unify web custom data and pepper custom data: | 265 // TODO(raymes): Unify web custom data and pepper custom data: |
219 // crbug.com/158399. | 266 // crbug.com/158399. |
220 static const FormatType& GetWebCustomDataFormatType(); | 267 static const FormatType& GetWebCustomDataFormatType(); |
221 static const FormatType& GetPepperCustomDataFormatType(); | 268 static const FormatType& GetPepperCustomDataFormatType(); |
222 | 269 |
| 270 // Embeds a pointer to a SharedMemory object pointed to by |bitmap_handle| |
| 271 // belonging to |process| into a shared bitmap [CBF_SMBITMAP] slot in |
| 272 // |objects|. The pointer is deleted by DispatchObjects(). |
| 273 // |
| 274 // On non-Windows platforms, |process| is ignored. |
| 275 static bool ReplaceSharedMemHandle(ObjectMap* objects, |
| 276 base::SharedMemoryHandle bitmap_handle, |
| 277 base::ProcessHandle process) |
| 278 WARN_UNUSED_RESULT; |
223 #if defined(OS_WIN) | 279 #if defined(OS_WIN) |
224 // Firefox text/html | 280 // Firefox text/html |
225 static const FormatType& GetTextHtmlFormatType(); | 281 static const FormatType& GetTextHtmlFormatType(); |
226 static const FormatType& GetCFHDropFormatType(); | 282 static const FormatType& GetCFHDropFormatType(); |
227 static const FormatType& GetFileDescriptorFormatType(); | 283 static const FormatType& GetFileDescriptorFormatType(); |
228 static const FormatType& GetFileContentZeroFormatType(); | 284 static const FormatType& GetFileContentZeroFormatType(); |
229 static const FormatType& GetIDListFormatType(); | 285 static const FormatType& GetIDListFormatType(); |
230 #endif | 286 #endif |
231 | 287 |
232 protected: | 288 protected: |
233 static Clipboard* Create(); | 289 static Clipboard* Create(); |
234 | 290 |
235 Clipboard() {} | 291 Clipboard() {} |
236 virtual ~Clipboard() {} | 292 virtual ~Clipboard() {} |
237 | 293 |
238 // ObjectType designates the type of data to be stored in the clipboard. This | |
239 // designation is shared across all OSes. The system-specific designation | |
240 // is defined by FormatType. A single ObjectType might be represented by | |
241 // several system-specific FormatTypes. For example, on Linux the CBF_TEXT | |
242 // ObjectType maps to "text/plain", "STRING", and several other formats. On | |
243 // windows it maps to CF_UNICODETEXT. | |
244 enum ObjectType { | |
245 CBF_TEXT, | |
246 CBF_HTML, | |
247 CBF_RTF, | |
248 CBF_BOOKMARK, | |
249 CBF_WEBKIT, | |
250 CBF_SMBITMAP, // Bitmap from shared memory. | |
251 CBF_DATA, // Arbitrary block of bytes. | |
252 }; | |
253 | |
254 // ObjectMap is a map from ObjectType to associated data. | |
255 // The data is organized differently for each ObjectType. The following | |
256 // table summarizes what kind of data is stored for each key. | |
257 // * indicates an optional argument. | |
258 // | |
259 // Key Arguments Type | |
260 // ------------------------------------- | |
261 // CBF_TEXT text char array | |
262 // CBF_HTML html char array | |
263 // url* char array | |
264 // CBF_RTF data byte array | |
265 // CBF_BOOKMARK html char array | |
266 // url char array | |
267 // CBF_WEBKIT none empty vector | |
268 // CBF_SMBITMAP bitmap A pointer to a SkBitmap. The caller must ensure | |
269 // the SkBitmap remains live for the duration of | |
270 // the WriteObjects call. | |
271 // CBF_DATA format char array | |
272 // data byte array | |
273 typedef std::vector<char> ObjectMapParam; | |
274 typedef std::vector<ObjectMapParam> ObjectMapParams; | |
275 typedef std::map<int /* ObjectType */, ObjectMapParams> ObjectMap; | |
276 | |
277 // Write a bunch of objects to the system clipboard. Copies are made of the | 294 // Write a bunch of objects to the system clipboard. Copies are made of the |
278 // contents of |objects|. | 295 // contents of |objects|. |
279 virtual void WriteObjects(ClipboardType type, const ObjectMap& objects) = 0; | 296 virtual void WriteObjects(ClipboardType type, const ObjectMap& objects) = 0; |
280 | 297 |
281 void DispatchObject(ObjectType type, const ObjectMapParams& params); | 298 void DispatchObject(ObjectType type, const ObjectMapParams& params); |
282 | 299 |
283 virtual void WriteText(const char* text_data, size_t text_len) = 0; | 300 virtual void WriteText(const char* text_data, size_t text_len) = 0; |
284 | 301 |
285 virtual void WriteHTML(const char* markup_data, | 302 virtual void WriteHTML(const char* markup_data, |
286 size_t markup_len, | 303 size_t markup_len, |
287 const char* url_data, | 304 const char* url_data, |
288 size_t url_len) = 0; | 305 size_t url_len) = 0; |
289 | 306 |
290 virtual void WriteRTF(const char* rtf_data, size_t data_len) = 0; | 307 virtual void WriteRTF(const char* rtf_data, size_t data_len) = 0; |
291 | 308 |
292 virtual void WriteBookmark(const char* title_data, | 309 virtual void WriteBookmark(const char* title_data, |
293 size_t title_len, | 310 size_t title_len, |
294 const char* url_data, | 311 const char* url_data, |
295 size_t url_len) = 0; | 312 size_t url_len) = 0; |
296 | 313 |
297 virtual void WriteWebSmartPaste() = 0; | 314 virtual void WriteWebSmartPaste() = 0; |
298 | 315 |
299 virtual void WriteBitmap(const SkBitmap& bitmap) = 0; | 316 virtual void WriteBitmap(const SkBitmap& bitmap) = 0; |
300 | 317 |
301 virtual void WriteData(const FormatType& format, | 318 virtual void WriteData(const FormatType& format, |
302 const char* data_data, | 319 const char* data_data, |
303 size_t data_len) = 0; | 320 size_t data_len) = 0; |
304 | 321 |
305 private: | 322 private: |
| 323 template <typename T> |
| 324 friend class ClipboardTest; |
306 // For access to WriteObjects(). | 325 // For access to WriteObjects(). |
| 326 // TODO(dcheng): Remove the temporary exception for content. |
| 327 friend class content::ClipboardMessageFilter; |
307 friend class ScopedClipboardWriter; | 328 friend class ScopedClipboardWriter; |
308 friend class TestClipboard; | 329 friend class TestClipboard; |
309 | 330 |
310 // A list of allowed threads. By default, this is empty and no thread checking | 331 // A list of allowed threads. By default, this is empty and no thread checking |
311 // is done (in the unit test case), but a user (like content) can set which | 332 // is done (in the unit test case), but a user (like content) can set which |
312 // threads are allowed to call this method. | 333 // threads are allowed to call this method. |
313 typedef std::vector<base::PlatformThreadId> AllowedThreadsVector; | 334 typedef std::vector<base::PlatformThreadId> AllowedThreadsVector; |
314 static base::LazyInstance<AllowedThreadsVector> allowed_threads_; | 335 static base::LazyInstance<AllowedThreadsVector> allowed_threads_; |
315 | 336 |
316 // Mapping from threads to clipboard objects. | 337 // Mapping from threads to clipboard objects. |
317 typedef std::map<base::PlatformThreadId, Clipboard*> ClipboardMap; | 338 typedef std::map<base::PlatformThreadId, Clipboard*> ClipboardMap; |
318 static base::LazyInstance<ClipboardMap> clipboard_map_; | 339 static base::LazyInstance<ClipboardMap> clipboard_map_; |
319 | 340 |
320 // Mutex that controls access to |g_clipboard_map|. | 341 // Mutex that controls access to |g_clipboard_map|. |
321 static base::LazyInstance<base::Lock>::Leaky clipboard_map_lock_; | 342 static base::LazyInstance<base::Lock>::Leaky clipboard_map_lock_; |
322 | 343 |
323 DISALLOW_COPY_AND_ASSIGN(Clipboard); | 344 DISALLOW_COPY_AND_ASSIGN(Clipboard); |
324 }; | 345 }; |
325 | 346 |
326 } // namespace ui | 347 } // namespace ui |
327 | 348 |
328 #endif // UI_BASE_CLIPBOARD_CLIPBOARD_H_ | 349 #endif // UI_BASE_CLIPBOARD_CLIPBOARD_H_ |
OLD | NEW |