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 | |
40 namespace gfx { | 35 namespace gfx { |
41 class Size; | 36 class Size; |
42 } | 37 } |
43 | 38 |
44 class SkBitmap; | 39 class SkBitmap; |
45 | 40 |
46 #ifdef __OBJC__ | 41 #ifdef __OBJC__ |
47 @class NSString; | 42 @class NSString; |
48 #else | 43 #else |
49 class NSString; | 44 class NSString; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 #elif defined(OS_MACOSX) | 106 #elif defined(OS_MACOSX) |
112 explicit FormatType(NSString* native_format); | 107 explicit FormatType(NSString* native_format); |
113 NSString* data_; | 108 NSString* data_; |
114 #else | 109 #else |
115 #error No FormatType definition. | 110 #error No FormatType definition. |
116 #endif | 111 #endif |
117 | 112 |
118 // Copyable and assignable, since this is essentially an opaque value type. | 113 // Copyable and assignable, since this is essentially an opaque value type. |
119 }; | 114 }; |
120 | 115 |
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 | |
163 static bool IsSupportedClipboardType(int32 type) { | 116 static bool IsSupportedClipboardType(int32 type) { |
164 switch (type) { | 117 switch (type) { |
165 case CLIPBOARD_TYPE_COPY_PASTE: | 118 case CLIPBOARD_TYPE_COPY_PASTE: |
166 return true; | 119 return true; |
167 #if !defined(OS_WIN) && !defined(OS_MACOSX) && !defined(OS_CHROMEOS) | 120 #if !defined(OS_WIN) && !defined(OS_MACOSX) && !defined(OS_CHROMEOS) |
168 case CLIPBOARD_TYPE_SELECTION: | 121 case CLIPBOARD_TYPE_SELECTION: |
169 return true; | 122 return true; |
170 #endif | 123 #endif |
171 } | 124 } |
172 return false; | 125 return false; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 static const FormatType& GetWebKitSmartPasteFormatType(); | 213 static const FormatType& GetWebKitSmartPasteFormatType(); |
261 // Win: MS HTML Format, Other: Generic HTML format | 214 // Win: MS HTML Format, Other: Generic HTML format |
262 static const FormatType& GetHtmlFormatType(); | 215 static const FormatType& GetHtmlFormatType(); |
263 static const FormatType& GetRtfFormatType(); | 216 static const FormatType& GetRtfFormatType(); |
264 static const FormatType& GetBitmapFormatType(); | 217 static const FormatType& GetBitmapFormatType(); |
265 // TODO(raymes): Unify web custom data and pepper custom data: | 218 // TODO(raymes): Unify web custom data and pepper custom data: |
266 // crbug.com/158399. | 219 // crbug.com/158399. |
267 static const FormatType& GetWebCustomDataFormatType(); | 220 static const FormatType& GetWebCustomDataFormatType(); |
268 static const FormatType& GetPepperCustomDataFormatType(); | 221 static const FormatType& GetPepperCustomDataFormatType(); |
269 | 222 |
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; | |
279 #if defined(OS_WIN) | 223 #if defined(OS_WIN) |
280 // Firefox text/html | 224 // Firefox text/html |
281 static const FormatType& GetTextHtmlFormatType(); | 225 static const FormatType& GetTextHtmlFormatType(); |
282 static const FormatType& GetCFHDropFormatType(); | 226 static const FormatType& GetCFHDropFormatType(); |
283 static const FormatType& GetFileDescriptorFormatType(); | 227 static const FormatType& GetFileDescriptorFormatType(); |
284 static const FormatType& GetFileContentZeroFormatType(); | 228 static const FormatType& GetFileContentZeroFormatType(); |
285 static const FormatType& GetIDListFormatType(); | 229 static const FormatType& GetIDListFormatType(); |
286 #endif | 230 #endif |
287 | 231 |
288 protected: | 232 protected: |
289 static Clipboard* Create(); | 233 static Clipboard* Create(); |
290 | 234 |
291 Clipboard() {} | 235 Clipboard() {} |
292 virtual ~Clipboard() {} | 236 virtual ~Clipboard() {} |
293 | 237 |
| 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 |
294 // Write a bunch of objects to the system clipboard. Copies are made of the | 277 // Write a bunch of objects to the system clipboard. Copies are made of the |
295 // contents of |objects|. | 278 // contents of |objects|. |
296 virtual void WriteObjects(ClipboardType type, const ObjectMap& objects) = 0; | 279 virtual void WriteObjects(ClipboardType type, const ObjectMap& objects) = 0; |
297 | 280 |
298 void DispatchObject(ObjectType type, const ObjectMapParams& params); | 281 void DispatchObject(ObjectType type, const ObjectMapParams& params); |
299 | 282 |
300 virtual void WriteText(const char* text_data, size_t text_len) = 0; | 283 virtual void WriteText(const char* text_data, size_t text_len) = 0; |
301 | 284 |
302 virtual void WriteHTML(const char* markup_data, | 285 virtual void WriteHTML(const char* markup_data, |
303 size_t markup_len, | 286 size_t markup_len, |
304 const char* url_data, | 287 const char* url_data, |
305 size_t url_len) = 0; | 288 size_t url_len) = 0; |
306 | 289 |
307 virtual void WriteRTF(const char* rtf_data, size_t data_len) = 0; | 290 virtual void WriteRTF(const char* rtf_data, size_t data_len) = 0; |
308 | 291 |
309 virtual void WriteBookmark(const char* title_data, | 292 virtual void WriteBookmark(const char* title_data, |
310 size_t title_len, | 293 size_t title_len, |
311 const char* url_data, | 294 const char* url_data, |
312 size_t url_len) = 0; | 295 size_t url_len) = 0; |
313 | 296 |
314 virtual void WriteWebSmartPaste() = 0; | 297 virtual void WriteWebSmartPaste() = 0; |
315 | 298 |
316 virtual void WriteBitmap(const SkBitmap& bitmap) = 0; | 299 virtual void WriteBitmap(const SkBitmap& bitmap) = 0; |
317 | 300 |
318 virtual void WriteData(const FormatType& format, | 301 virtual void WriteData(const FormatType& format, |
319 const char* data_data, | 302 const char* data_data, |
320 size_t data_len) = 0; | 303 size_t data_len) = 0; |
321 | 304 |
322 private: | 305 private: |
323 template <typename T> | |
324 friend class ClipboardTest; | |
325 // For access to WriteObjects(). | 306 // For access to WriteObjects(). |
326 // TODO(dcheng): Remove the temporary exception for content. | |
327 friend class content::ClipboardMessageFilter; | |
328 friend class ScopedClipboardWriter; | 307 friend class ScopedClipboardWriter; |
329 friend class TestClipboard; | 308 friend class TestClipboard; |
330 | 309 |
331 // A list of allowed threads. By default, this is empty and no thread checking | 310 // A list of allowed threads. By default, this is empty and no thread checking |
332 // is done (in the unit test case), but a user (like content) can set which | 311 // is done (in the unit test case), but a user (like content) can set which |
333 // threads are allowed to call this method. | 312 // threads are allowed to call this method. |
334 typedef std::vector<base::PlatformThreadId> AllowedThreadsVector; | 313 typedef std::vector<base::PlatformThreadId> AllowedThreadsVector; |
335 static base::LazyInstance<AllowedThreadsVector> allowed_threads_; | 314 static base::LazyInstance<AllowedThreadsVector> allowed_threads_; |
336 | 315 |
337 // Mapping from threads to clipboard objects. | 316 // Mapping from threads to clipboard objects. |
338 typedef std::map<base::PlatformThreadId, Clipboard*> ClipboardMap; | 317 typedef std::map<base::PlatformThreadId, Clipboard*> ClipboardMap; |
339 static base::LazyInstance<ClipboardMap> clipboard_map_; | 318 static base::LazyInstance<ClipboardMap> clipboard_map_; |
340 | 319 |
341 // Mutex that controls access to |g_clipboard_map|. | 320 // Mutex that controls access to |g_clipboard_map|. |
342 static base::LazyInstance<base::Lock>::Leaky clipboard_map_lock_; | 321 static base::LazyInstance<base::Lock>::Leaky clipboard_map_lock_; |
343 | 322 |
344 DISALLOW_COPY_AND_ASSIGN(Clipboard); | 323 DISALLOW_COPY_AND_ASSIGN(Clipboard); |
345 }; | 324 }; |
346 | 325 |
347 } // namespace ui | 326 } // namespace ui |
348 | 327 |
349 #endif // UI_BASE_CLIPBOARD_CLIPBOARD_H_ | 328 #endif // UI_BASE_CLIPBOARD_CLIPBOARD_H_ |
OLD | NEW |