| 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> |
| 11 | 11 |
| 12 #include "base/callback.h" | |
| 13 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 14 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 15 #include "base/process.h" | 14 #include "base/process.h" |
| 16 #include "base/shared_memory.h" | 15 #include "base/shared_memory.h" |
| 17 #include "base/string16.h" | 16 #include "base/string16.h" |
| 18 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
| 19 #include "base/threading/platform_thread.h" | 18 #include "base/threading/platform_thread.h" |
| 20 #include "ui/base/ui_export.h" | 19 #include "ui/base/ui_export.h" |
| 21 | 20 |
| 22 #if defined(TOOLKIT_GTK) | 21 #if defined(TOOLKIT_GTK) |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 // size gfx::Size struct | 162 // size gfx::Size struct |
| 164 // CBF_SMBITMAP shared_mem A pointer to an unmapped base::SharedMemory | 163 // CBF_SMBITMAP shared_mem A pointer to an unmapped base::SharedMemory |
| 165 // object containing the bitmap data. | 164 // object containing the bitmap data. |
| 166 // size gfx::Size struct | 165 // size gfx::Size struct |
| 167 // CBF_DATA format char array | 166 // CBF_DATA format char array |
| 168 // data byte array | 167 // data byte array |
| 169 typedef std::vector<char> ObjectMapParam; | 168 typedef std::vector<char> ObjectMapParam; |
| 170 typedef std::vector<ObjectMapParam> ObjectMapParams; | 169 typedef std::vector<ObjectMapParam> ObjectMapParams; |
| 171 typedef std::map<int /* ObjectType */, ObjectMapParams> ObjectMap; | 170 typedef std::map<int /* ObjectType */, ObjectMapParams> ObjectMap; |
| 172 | 171 |
| 173 // WriteObject() caller can use the SourceTag that will be stored in the | |
| 174 // clipboard. NULL value means "no tag". | |
| 175 typedef void* SourceTag; | |
| 176 static ObjectMapParam SourceTag2Binary(SourceTag tag); | |
| 177 static SourceTag Binary2SourceTag(const std::string& serialization); | |
| 178 | |
| 179 // Buffer designates which clipboard the action should be applied to. | 172 // Buffer designates which clipboard the action should be applied to. |
| 180 // Only platforms that use the X Window System support the selection | 173 // Only platforms that use the X Window System support the selection |
| 181 // buffer. | 174 // buffer. |
| 182 enum Buffer { | 175 enum Buffer { |
| 183 BUFFER_STANDARD, | 176 BUFFER_STANDARD, |
| 184 BUFFER_SELECTION, | 177 BUFFER_SELECTION, |
| 185 }; | 178 }; |
| 186 | 179 |
| 187 // The callback is called after Clipboard::WriteObjects(). | |
| 188 // Don't use it for notification about changed OS clipboard. | |
| 189 void set_write_objects_callback_for_testing( | |
| 190 const base::Callback<void(Buffer)>& cb) { | |
| 191 write_objects_callback_ = cb; | |
| 192 } | |
| 193 | |
| 194 static bool IsValidBuffer(int32 buffer) { | 180 static bool IsValidBuffer(int32 buffer) { |
| 195 switch (buffer) { | 181 switch (buffer) { |
| 196 case BUFFER_STANDARD: | 182 case BUFFER_STANDARD: |
| 197 return true; | 183 return true; |
| 198 #if defined(USE_X11) && !defined(OS_CHROMEOS) | 184 #if defined(USE_X11) && !defined(OS_CHROMEOS) |
| 199 case BUFFER_SELECTION: | 185 case BUFFER_SELECTION: |
| 200 return true; | 186 return true; |
| 201 #endif | 187 #endif |
| 202 } | 188 } |
| 203 return false; | 189 return false; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 221 | 207 |
| 222 // Destroys the clipboard for the current thread. Usually, this will clean up | 208 // Destroys the clipboard for the current thread. Usually, this will clean up |
| 223 // all clipboards, except on Windows. (Previous code leaks the IO thread | 209 // all clipboards, except on Windows. (Previous code leaks the IO thread |
| 224 // clipboard, so it shouldn't be a problem.) | 210 // clipboard, so it shouldn't be a problem.) |
| 225 static void DestroyClipboardForCurrentThread(); | 211 static void DestroyClipboardForCurrentThread(); |
| 226 | 212 |
| 227 // Write a bunch of objects to the system clipboard. Copies are made of the | 213 // Write a bunch of objects to the system clipboard. Copies are made of the |
| 228 // contents of |objects|. On Windows they are copied to the system clipboard. | 214 // contents of |objects|. On Windows they are copied to the system clipboard. |
| 229 // On linux they are copied into a structure owned by the Clipboard object and | 215 // On linux they are copied into a structure owned by the Clipboard object and |
| 230 // kept until the system clipboard is set again. | 216 // kept until the system clipboard is set again. |
| 231 // SourceTag is optional value to be stored in the clipboard, NULL won't be | 217 void WriteObjects(Buffer buffer, const ObjectMap& objects); |
| 232 // stored. | |
| 233 void WriteObjects(Buffer buffer, const ObjectMap& objects, SourceTag tag); | |
| 234 | 218 |
| 235 // Returns a sequence number which uniquely identifies clipboard state. | 219 // Returns a sequence number which uniquely identifies clipboard state. |
| 236 // This can be used to version the data on the clipboard and determine | 220 // This can be used to version the data on the clipboard and determine |
| 237 // whether it has changed. | 221 // whether it has changed. |
| 238 uint64 GetSequenceNumber(Buffer buffer); | 222 uint64 GetSequenceNumber(Buffer buffer); |
| 239 | 223 |
| 240 // Tests whether the clipboard contains a certain format | 224 // Tests whether the clipboard contains a certain format |
| 241 bool IsFormatAvailable(const FormatType& format, Buffer buffer) const; | 225 bool IsFormatAvailable(const FormatType& format, Buffer buffer) const; |
| 242 | 226 |
| 243 // Clear the clipboard data. | 227 // Clear the clipboard data. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 270 const string16& type, | 254 const string16& type, |
| 271 string16* result) const; | 255 string16* result) const; |
| 272 | 256 |
| 273 // Reads a bookmark from the clipboard, if available. | 257 // Reads a bookmark from the clipboard, if available. |
| 274 void ReadBookmark(string16* title, std::string* url) const; | 258 void ReadBookmark(string16* title, std::string* url) const; |
| 275 | 259 |
| 276 // Reads raw data from the clipboard with the given format type. Stores result | 260 // Reads raw data from the clipboard with the given format type. Stores result |
| 277 // as a byte vector. | 261 // as a byte vector. |
| 278 void ReadData(const FormatType& format, std::string* result) const; | 262 void ReadData(const FormatType& format, std::string* result) const; |
| 279 | 263 |
| 280 // Reads Source tag from the clipboard, if available. | |
| 281 SourceTag ReadSourceTag(Buffer buffer) const; | |
| 282 | |
| 283 // Gets the FormatType corresponding to an arbitrary format string, | 264 // Gets the FormatType corresponding to an arbitrary format string, |
| 284 // registering it with the system if needed. Due to Windows/Linux | 265 // registering it with the system if needed. Due to Windows/Linux |
| 285 // limitiations, |format_string| must never be controlled by the user. | 266 // limitiations, |format_string| must never be controlled by the user. |
| 286 static FormatType GetFormatType(const std::string& format_string); | 267 static FormatType GetFormatType(const std::string& format_string); |
| 287 | 268 |
| 288 // Get format identifiers for various types. | 269 // Get format identifiers for various types. |
| 289 static const FormatType& GetUrlFormatType(); | 270 static const FormatType& GetUrlFormatType(); |
| 290 static const FormatType& GetUrlWFormatType(); | 271 static const FormatType& GetUrlWFormatType(); |
| 291 static const FormatType& GetMozUrlFormatType(); | 272 static const FormatType& GetMozUrlFormatType(); |
| 292 static const FormatType& GetPlainTextFormatType(); | 273 static const FormatType& GetPlainTextFormatType(); |
| 293 static const FormatType& GetPlainTextWFormatType(); | 274 static const FormatType& GetPlainTextWFormatType(); |
| 294 static const FormatType& GetFilenameFormatType(); | 275 static const FormatType& GetFilenameFormatType(); |
| 295 static const FormatType& GetFilenameWFormatType(); | 276 static const FormatType& GetFilenameWFormatType(); |
| 296 static const FormatType& GetWebKitSmartPasteFormatType(); | 277 static const FormatType& GetWebKitSmartPasteFormatType(); |
| 297 // Win: MS HTML Format, Other: Generic HTML format | 278 // Win: MS HTML Format, Other: Generic HTML format |
| 298 static const FormatType& GetHtmlFormatType(); | 279 static const FormatType& GetHtmlFormatType(); |
| 299 static const FormatType& GetRtfFormatType(); | 280 static const FormatType& GetRtfFormatType(); |
| 300 static const FormatType& GetBitmapFormatType(); | 281 static const FormatType& GetBitmapFormatType(); |
| 301 // TODO(raymes): Unify web custom data and pepper custom data: | 282 // TODO(raymes): Unify web custom data and pepper custom data: |
| 302 // crbug.com/158399. | 283 // crbug.com/158399. |
| 303 static const FormatType& GetWebCustomDataFormatType(); | 284 static const FormatType& GetWebCustomDataFormatType(); |
| 304 static const FormatType& GetPepperCustomDataFormatType(); | 285 static const FormatType& GetPepperCustomDataFormatType(); |
| 305 static const FormatType& GetSourceTagFormatType(); | |
| 306 | 286 |
| 307 // Embeds a pointer to a SharedMemory object pointed to by |bitmap_handle| | 287 // Embeds a pointer to a SharedMemory object pointed to by |bitmap_handle| |
| 308 // belonging to |process| into a shared bitmap [CBF_SMBITMAP] slot in | 288 // belonging to |process| into a shared bitmap [CBF_SMBITMAP] slot in |
| 309 // |objects|. The pointer is deleted by DispatchObjects(). | 289 // |objects|. The pointer is deleted by DispatchObjects(). |
| 310 // | 290 // |
| 311 // On non-Windows platforms, |process| is ignored. | 291 // On non-Windows platforms, |process| is ignored. |
| 312 static void ReplaceSharedMemHandle(ObjectMap* objects, | 292 static void ReplaceSharedMemHandle(ObjectMap* objects, |
| 313 base::SharedMemoryHandle bitmap_handle, | 293 base::SharedMemoryHandle bitmap_handle, |
| 314 base::ProcessHandle process); | 294 base::ProcessHandle process); |
| 315 #if defined(OS_WIN) | 295 #if defined(OS_WIN) |
| 316 // Firefox text/html | 296 // Firefox text/html |
| 317 static const FormatType& GetTextHtmlFormatType(); | 297 static const FormatType& GetTextHtmlFormatType(); |
| 318 static const FormatType& GetCFHDropFormatType(); | 298 static const FormatType& GetCFHDropFormatType(); |
| 319 static const FormatType& GetFileDescriptorFormatType(); | 299 static const FormatType& GetFileDescriptorFormatType(); |
| 320 static const FormatType& GetFileContentFormatZeroType(); | 300 static const FormatType& GetFileContentFormatZeroType(); |
| 321 #endif | 301 #endif |
| 322 | 302 |
| 323 private: | 303 private: |
| 324 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, SharedBitmapTest); | 304 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, SharedBitmapTest); |
| 325 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, EmptyHTMLTest); | 305 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, EmptyHTMLTest); |
| 326 friend class ClipboardTest; | 306 friend class ClipboardTest; |
| 327 | 307 |
| 328 Clipboard(); | 308 Clipboard(); |
| 329 ~Clipboard(); | 309 ~Clipboard(); |
| 330 | 310 |
| 331 void DispatchObject(ObjectType type, const ObjectMapParams& params); | 311 void DispatchObject(ObjectType type, const ObjectMapParams& params); |
| 332 | 312 |
| 333 void WriteObjectsImpl(Buffer buffer, const ObjectMap& objects, SourceTag tag); | |
| 334 | |
| 335 void WriteText(const char* text_data, size_t text_len); | 313 void WriteText(const char* text_data, size_t text_len); |
| 336 | 314 |
| 337 void WriteHTML(const char* markup_data, | 315 void WriteHTML(const char* markup_data, |
| 338 size_t markup_len, | 316 size_t markup_len, |
| 339 const char* url_data, | 317 const char* url_data, |
| 340 size_t url_len); | 318 size_t url_len); |
| 341 | 319 |
| 342 void WriteRTF(const char* rtf_data, size_t data_len); | 320 void WriteRTF(const char* rtf_data, size_t data_len); |
| 343 | 321 |
| 344 void WriteBookmark(const char* title_data, | 322 void WriteBookmark(const char* title_data, |
| 345 size_t title_len, | 323 size_t title_len, |
| 346 const char* url_data, | 324 const char* url_data, |
| 347 size_t url_len); | 325 size_t url_len); |
| 348 | 326 |
| 349 void WriteWebSmartPaste(); | 327 void WriteWebSmartPaste(); |
| 350 | 328 |
| 351 void WriteBitmap(const char* pixel_data, const char* size_data); | 329 void WriteBitmap(const char* pixel_data, const char* size_data); |
| 352 | 330 |
| 353 void WriteData(const FormatType& format, | 331 void WriteData(const FormatType& format, |
| 354 const char* data_data, | 332 const char* data_data, |
| 355 size_t data_len); | 333 size_t data_len); |
| 356 | |
| 357 void WriteSourceTag(SourceTag tag); | |
| 358 #if defined(OS_WIN) | 334 #if defined(OS_WIN) |
| 359 void WriteBitmapFromHandle(HBITMAP source_hbitmap, | 335 void WriteBitmapFromHandle(HBITMAP source_hbitmap, |
| 360 const gfx::Size& size); | 336 const gfx::Size& size); |
| 361 | 337 |
| 362 // Safely write to system clipboard. Free |handle| on failure. | 338 // Safely write to system clipboard. Free |handle| on failure. |
| 363 void WriteToClipboard(unsigned int format, HANDLE handle); | 339 void WriteToClipboard(unsigned int format, HANDLE handle); |
| 364 | 340 |
| 365 static void ParseBookmarkClipboardFormat(const string16& bookmark, | 341 static void ParseBookmarkClipboardFormat(const string16& bookmark, |
| 366 string16* title, | 342 string16* title, |
| 367 std::string* url); | 343 std::string* url); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 391 typedef std::map<std::string, std::pair<char*, size_t> > TargetMap; | 367 typedef std::map<std::string, std::pair<char*, size_t> > TargetMap; |
| 392 | 368 |
| 393 private: | 369 private: |
| 394 // Write changes to gtk clipboard. | 370 // Write changes to gtk clipboard. |
| 395 void SetGtkClipboard(Buffer buffer); | 371 void SetGtkClipboard(Buffer buffer); |
| 396 // Insert a mapping into clipboard_data_. | 372 // Insert a mapping into clipboard_data_. |
| 397 void InsertMapping(const char* key, char* data, size_t data_len); | 373 void InsertMapping(const char* key, char* data, size_t data_len); |
| 398 | 374 |
| 399 // Find the gtk clipboard for the passed buffer enum. | 375 // Find the gtk clipboard for the passed buffer enum. |
| 400 GtkClipboard* LookupBackingClipboard(Buffer clipboard) const; | 376 GtkClipboard* LookupBackingClipboard(Buffer clipboard) const; |
| 401 // Reads raw data from the specified clipboard with the given format type. | |
| 402 void ReadDataImpl(Buffer buffer, | |
| 403 const FormatType& format, | |
| 404 std::string* result) const; | |
| 405 | 377 |
| 406 TargetMap* clipboard_data_; | 378 TargetMap* clipboard_data_; |
| 407 GtkClipboard* clipboard_; | 379 GtkClipboard* clipboard_; |
| 408 GtkClipboard* primary_selection_; | 380 GtkClipboard* primary_selection_; |
| 409 #elif defined(USE_AURA) && defined(USE_X11) && !defined(OS_CHROMEOS) | 381 #elif defined(USE_AURA) && defined(USE_X11) && !defined(OS_CHROMEOS) |
| 410 private: | 382 private: |
| 411 // Reads raw data from the specified clipboard with the given format type. | |
| 412 void ReadDataImpl(Buffer buffer, | |
| 413 const FormatType& format, | |
| 414 std::string* result) const; | |
| 415 // We keep our implementation details private because otherwise we bring in | 383 // We keep our implementation details private because otherwise we bring in |
| 416 // the X11 headers and break chrome compile. | 384 // the X11 headers and break chrome compile. |
| 417 class AuraX11Details; | 385 class AuraX11Details; |
| 418 scoped_ptr<AuraX11Details> aurax11_details_; | 386 scoped_ptr<AuraX11Details> aurax11_details_; |
| 419 #endif | 387 #endif |
| 420 base::Callback<void(Buffer)> write_objects_callback_; | |
| 421 | 388 |
| 422 DISALLOW_COPY_AND_ASSIGN(Clipboard); | 389 DISALLOW_COPY_AND_ASSIGN(Clipboard); |
| 423 }; | 390 }; |
| 424 | 391 |
| 425 } // namespace ui | 392 } // namespace ui |
| 426 | 393 |
| 427 #endif // UI_BASE_CLIPBOARD_CLIPBOARD_H_ | 394 #endif // UI_BASE_CLIPBOARD_CLIPBOARD_H_ |
| OLD | NEW |