| 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 #include "ui/base/clipboard/clipboard.h" | 5 #include "ui/base/clipboard/clipboard.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 // Clipboard data format used by AuraClipboard. | 28 // Clipboard data format used by AuraClipboard. |
| 29 enum AuraClipboardFormat { | 29 enum AuraClipboardFormat { |
| 30 TEXT = 1 << 0, | 30 TEXT = 1 << 0, |
| 31 HTML = 1 << 1, | 31 HTML = 1 << 1, |
| 32 RTF = 1 << 2, | 32 RTF = 1 << 2, |
| 33 BOOKMARK = 1 << 3, | 33 BOOKMARK = 1 << 3, |
| 34 BITMAP = 1 << 4, | 34 BITMAP = 1 << 4, |
| 35 CUSTOM = 1 << 5, | 35 CUSTOM = 1 << 5, |
| 36 WEB = 1 << 6, | 36 WEB = 1 << 6, |
| 37 SOURCETAG = 1 << 7, | |
| 38 }; | 37 }; |
| 39 | 38 |
| 40 // ClipboardData contains data copied to the Clipboard for a variety of formats. | 39 // ClipboardData contains data copied to the Clipboard for a variety of formats. |
| 41 // It mostly just provides APIs to cleanly access and manipulate this data. | 40 // It mostly just provides APIs to cleanly access and manipulate this data. |
| 42 class ClipboardData { | 41 class ClipboardData { |
| 43 public: | 42 public: |
| 44 ClipboardData() | 43 ClipboardData() |
| 45 : bitmap_data_(), | 44 : bitmap_data_(), |
| 46 source_tag_(), | |
| 47 web_smart_paste_(false), | 45 web_smart_paste_(false), |
| 48 format_(0) {} | 46 format_(0) {} |
| 49 | 47 |
| 50 virtual ~ClipboardData() {} | 48 virtual ~ClipboardData() {} |
| 51 | 49 |
| 52 // Bitmask of AuraClipboardFormat types. | 50 // Bitmask of AuraClipboardFormat types. |
| 53 const int format() const { return format_; } | 51 const int format() const { return format_; } |
| 54 | 52 |
| 55 const std::string& text() const { return text_; } | 53 const std::string& text() const { return text_; } |
| 56 void set_text(const std::string& text) { | 54 void set_text(const std::string& text) { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 custom_data_format_ = data_format; | 111 custom_data_format_ = data_format; |
| 114 format_ |= CUSTOM; | 112 format_ |= CUSTOM; |
| 115 } | 113 } |
| 116 | 114 |
| 117 bool web_smart_paste() const { return web_smart_paste_; } | 115 bool web_smart_paste() const { return web_smart_paste_; } |
| 118 void set_web_smart_paste(bool web_smart_paste) { | 116 void set_web_smart_paste(bool web_smart_paste) { |
| 119 web_smart_paste_ = web_smart_paste; | 117 web_smart_paste_ = web_smart_paste; |
| 120 format_ |= WEB; | 118 format_ |= WEB; |
| 121 } | 119 } |
| 122 | 120 |
| 123 Clipboard::SourceTag source_tag() const { return source_tag_; } | |
| 124 void set_source_tag(Clipboard::SourceTag tag) { | |
| 125 source_tag_ = tag; | |
| 126 format_ |= SOURCETAG; | |
| 127 } | |
| 128 | |
| 129 private: | 121 private: |
| 130 // Plain text in UTF8 format. | 122 // Plain text in UTF8 format. |
| 131 std::string text_; | 123 std::string text_; |
| 132 | 124 |
| 133 // HTML markup data in UTF8 format. | 125 // HTML markup data in UTF8 format. |
| 134 std::string markup_data_; | 126 std::string markup_data_; |
| 135 std::string url_; | 127 std::string url_; |
| 136 | 128 |
| 137 // RTF data. | 129 // RTF data. |
| 138 std::string rtf_data_; | 130 std::string rtf_data_; |
| 139 | 131 |
| 140 // Bookmark title in UTF8 format. | 132 // Bookmark title in UTF8 format. |
| 141 std::string bookmark_title_; | 133 std::string bookmark_title_; |
| 142 std::string bookmark_url_; | 134 std::string bookmark_url_; |
| 143 | 135 |
| 144 // Filenames. | 136 // Filenames. |
| 145 std::vector<std::string> files_; | 137 std::vector<std::string> files_; |
| 146 | 138 |
| 147 // Bitmap images. | 139 // Bitmap images. |
| 148 scoped_ptr<uint8_t[]> bitmap_data_; | 140 scoped_ptr<uint8_t[]> bitmap_data_; |
| 149 gfx::Size bitmap_size_; | 141 gfx::Size bitmap_size_; |
| 150 | 142 |
| 151 // Data with custom format. | 143 // Data with custom format. |
| 152 std::string custom_data_format_; | 144 std::string custom_data_format_; |
| 153 std::string custom_data_data_; | 145 std::string custom_data_data_; |
| 154 | 146 |
| 155 // SourceTag. | |
| 156 Clipboard::SourceTag source_tag_; | |
| 157 | |
| 158 // WebKit smart paste data. | 147 // WebKit smart paste data. |
| 159 bool web_smart_paste_; | 148 bool web_smart_paste_; |
| 160 | 149 |
| 161 int format_; | 150 int format_; |
| 162 | 151 |
| 163 DISALLOW_COPY_AND_ASSIGN(ClipboardData); | 152 DISALLOW_COPY_AND_ASSIGN(ClipboardData); |
| 164 }; | 153 }; |
| 165 | 154 |
| 166 // Platform clipboard implementation for Aura. This handles things like format | 155 // Platform clipboard implementation for Aura. This handles things like format |
| 167 // conversion, versioning of clipboard items etc. The goal is to roughly provide | 156 // conversion, versioning of clipboard items etc. The goal is to roughly provide |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 | 289 |
| 301 void ReadData(const std::string& type, std::string* result) const { | 290 void ReadData(const std::string& type, std::string* result) const { |
| 302 result->clear(); | 291 result->clear(); |
| 303 const ClipboardData* data = GetData(); | 292 const ClipboardData* data = GetData(); |
| 304 if (!HasFormat(CUSTOM) || type != data->custom_data_format()) | 293 if (!HasFormat(CUSTOM) || type != data->custom_data_format()) |
| 305 return; | 294 return; |
| 306 | 295 |
| 307 *result = data->custom_data_data(); | 296 *result = data->custom_data_data(); |
| 308 } | 297 } |
| 309 | 298 |
| 310 Clipboard::SourceTag ReadSourceTag() const { | |
| 311 if (!HasFormat(SOURCETAG)) | |
| 312 return Clipboard::SourceTag(); | |
| 313 const ClipboardData* data = GetData(); | |
| 314 return data->source_tag(); | |
| 315 } | |
| 316 | |
| 317 // Writes |data| to the top of the clipboard stack. | 299 // Writes |data| to the top of the clipboard stack. |
| 318 void WriteData(ClipboardData* data) { | 300 void WriteData(ClipboardData* data) { |
| 319 DCHECK(data); | 301 DCHECK(data); |
| 320 AddToListEnsuringSize(data); | 302 AddToListEnsuringSize(data); |
| 321 } | 303 } |
| 322 | 304 |
| 323 private: | 305 private: |
| 324 // True if the data on top of the clipboard stack has format |format|. | 306 // True if the data on top of the clipboard stack has format |format|. |
| 325 bool HasFormat(AuraClipboardFormat format) const { | 307 bool HasFormat(AuraClipboardFormat format) const { |
| 326 const ClipboardData* data = GetData(); | 308 const ClipboardData* data = GetData(); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 data->SetBitmapData(pixel_data, size_data); | 392 data->SetBitmapData(pixel_data, size_data); |
| 411 } | 393 } |
| 412 | 394 |
| 413 static void WriteData(const std::string& format, | 395 static void WriteData(const std::string& format, |
| 414 const char* data_data, | 396 const char* data_data, |
| 415 size_t data_len) { | 397 size_t data_len) { |
| 416 ClipboardData* data = GetCurrentData(); | 398 ClipboardData* data = GetCurrentData(); |
| 417 data->SetCustomData(format, std::string(data_data, data_len)); | 399 data->SetCustomData(format, std::string(data_data, data_len)); |
| 418 } | 400 } |
| 419 | 401 |
| 420 static void WriteSourceTag(Clipboard::SourceTag tag) { | |
| 421 ClipboardData* data = GetCurrentData(); | |
| 422 data->set_source_tag(tag); | |
| 423 } | |
| 424 | |
| 425 private: | 402 private: |
| 426 static ClipboardData* GetCurrentData() { | 403 static ClipboardData* GetCurrentData() { |
| 427 if (!current_data_) | 404 if (!current_data_) |
| 428 current_data_ = new ClipboardData; | 405 current_data_ = new ClipboardData; |
| 429 return current_data_; | 406 return current_data_; |
| 430 } | 407 } |
| 431 | 408 |
| 432 static ClipboardData* current_data_; | 409 static ClipboardData* current_data_; |
| 433 }; | 410 }; |
| 434 | 411 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 464 DCHECK(CalledOnValidThread()); | 441 DCHECK(CalledOnValidThread()); |
| 465 // Make sure clipboard is created. | 442 // Make sure clipboard is created. |
| 466 GetClipboard(); | 443 GetClipboard(); |
| 467 } | 444 } |
| 468 | 445 |
| 469 Clipboard::~Clipboard() { | 446 Clipboard::~Clipboard() { |
| 470 DCHECK(CalledOnValidThread()); | 447 DCHECK(CalledOnValidThread()); |
| 471 DeleteClipboard(); | 448 DeleteClipboard(); |
| 472 } | 449 } |
| 473 | 450 |
| 474 void Clipboard::WriteObjectsImpl(Buffer buffer, | 451 void Clipboard::WriteObjects(Buffer buffer, const ObjectMap& objects) { |
| 475 const ObjectMap& objects, | |
| 476 SourceTag tag) { | |
| 477 DCHECK(CalledOnValidThread()); | 452 DCHECK(CalledOnValidThread()); |
| 478 DCHECK(IsValidBuffer(buffer)); | 453 DCHECK(IsValidBuffer(buffer)); |
| 479 for (ObjectMap::const_iterator iter = objects.begin(); | 454 for (ObjectMap::const_iterator iter = objects.begin(); |
| 480 iter != objects.end(); ++iter) { | 455 iter != objects.end(); ++iter) { |
| 481 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); | 456 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); |
| 482 } | 457 } |
| 483 WriteSourceTag(tag); | |
| 484 ClipboardDataBuilder::CommitToClipboard(); | 458 ClipboardDataBuilder::CommitToClipboard(); |
| 485 } | 459 } |
| 486 | 460 |
| 487 bool Clipboard::IsFormatAvailable(const FormatType& format, | 461 bool Clipboard::IsFormatAvailable(const FormatType& format, |
| 488 Buffer buffer) const { | 462 Buffer buffer) const { |
| 489 DCHECK(CalledOnValidThread()); | 463 DCHECK(CalledOnValidThread()); |
| 490 DCHECK(IsValidBuffer(buffer)); | 464 DCHECK(IsValidBuffer(buffer)); |
| 491 AuraClipboard* clipboard = GetClipboard(); | 465 AuraClipboard* clipboard = GetClipboard(); |
| 492 if (GetPlainTextFormatType().Equals(format) || | 466 if (GetPlainTextFormatType().Equals(format) || |
| 493 GetUrlFormatType().Equals(format)) | 467 GetUrlFormatType().Equals(format)) |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 void Clipboard::ReadBookmark(string16* title, std::string* url) const { | 554 void Clipboard::ReadBookmark(string16* title, std::string* url) const { |
| 581 DCHECK(CalledOnValidThread()); | 555 DCHECK(CalledOnValidThread()); |
| 582 GetClipboard()->ReadBookmark(title, url); | 556 GetClipboard()->ReadBookmark(title, url); |
| 583 } | 557 } |
| 584 | 558 |
| 585 void Clipboard::ReadData(const FormatType& format, std::string* result) const { | 559 void Clipboard::ReadData(const FormatType& format, std::string* result) const { |
| 586 DCHECK(CalledOnValidThread()); | 560 DCHECK(CalledOnValidThread()); |
| 587 GetClipboard()->ReadData(format.ToString(), result); | 561 GetClipboard()->ReadData(format.ToString(), result); |
| 588 } | 562 } |
| 589 | 563 |
| 590 Clipboard::SourceTag Clipboard::ReadSourceTag(Buffer buffer) const { | |
| 591 DCHECK(CalledOnValidThread()); | |
| 592 DCHECK_EQ(BUFFER_STANDARD, buffer); | |
| 593 return GetClipboard()->ReadSourceTag(); | |
| 594 } | |
| 595 | |
| 596 uint64 Clipboard::GetSequenceNumber(Buffer buffer) { | 564 uint64 Clipboard::GetSequenceNumber(Buffer buffer) { |
| 597 DCHECK(CalledOnValidThread()); | 565 DCHECK(CalledOnValidThread()); |
| 598 return GetClipboard()->GetNumClipboardEntries(); | 566 return GetClipboard()->GetNumClipboardEntries(); |
| 599 } | 567 } |
| 600 | 568 |
| 601 void Clipboard::WriteText(const char* text_data, size_t text_len) { | 569 void Clipboard::WriteText(const char* text_data, size_t text_len) { |
| 602 ClipboardDataBuilder::WriteText(text_data, text_len); | 570 ClipboardDataBuilder::WriteText(text_data, text_len); |
| 603 } | 571 } |
| 604 | 572 |
| 605 void Clipboard::WriteHTML(const char* markup_data, | 573 void Clipboard::WriteHTML(const char* markup_data, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 627 void Clipboard::WriteBitmap(const char* pixel_data, const char* size_data) { | 595 void Clipboard::WriteBitmap(const char* pixel_data, const char* size_data) { |
| 628 ClipboardDataBuilder::WriteBitmap(pixel_data, size_data); | 596 ClipboardDataBuilder::WriteBitmap(pixel_data, size_data); |
| 629 } | 597 } |
| 630 | 598 |
| 631 void Clipboard::WriteData(const FormatType& format, | 599 void Clipboard::WriteData(const FormatType& format, |
| 632 const char* data_data, | 600 const char* data_data, |
| 633 size_t data_len) { | 601 size_t data_len) { |
| 634 ClipboardDataBuilder::WriteData(format.ToString(), data_data, data_len); | 602 ClipboardDataBuilder::WriteData(format.ToString(), data_data, data_len); |
| 635 } | 603 } |
| 636 | 604 |
| 637 void Clipboard::WriteSourceTag(SourceTag tag) { | |
| 638 if (tag != SourceTag()) | |
| 639 ClipboardDataBuilder::WriteSourceTag(tag); | |
| 640 } | |
| 641 | |
| 642 // static | 605 // static |
| 643 Clipboard::FormatType Clipboard::GetFormatType( | 606 Clipboard::FormatType Clipboard::GetFormatType( |
| 644 const std::string& format_string) { | 607 const std::string& format_string) { |
| 645 return FormatType::Deserialize(format_string); | 608 return FormatType::Deserialize(format_string); |
| 646 } | 609 } |
| 647 | 610 |
| 648 // static | 611 // static |
| 649 const Clipboard::FormatType& Clipboard::GetUrlFormatType() { | 612 const Clipboard::FormatType& Clipboard::GetUrlFormatType() { |
| 650 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kMimeTypeURIList)); | 613 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kMimeTypeURIList)); |
| 651 return type; | 614 return type; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 708 return type; | 671 return type; |
| 709 } | 672 } |
| 710 | 673 |
| 711 // static | 674 // static |
| 712 const Clipboard::FormatType& Clipboard::GetPepperCustomDataFormatType() { | 675 const Clipboard::FormatType& Clipboard::GetPepperCustomDataFormatType() { |
| 713 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kMimeTypePepperCustomData)); | 676 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kMimeTypePepperCustomData)); |
| 714 return type; | 677 return type; |
| 715 } | 678 } |
| 716 | 679 |
| 717 } // namespace ui | 680 } // namespace ui |
| OLD | NEW |