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
3 // found in the LICENSE file.
5 #include "ui/base/clipboard/clipboard.h"
9 #include "base/basictypes.h"
10 #include "base/files/file_path.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/stl_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "third_party/skia/include/core/SkBitmap.h"
16 #include "ui/base/clipboard/custom_data_helper.h"
17 #include "ui/gfx/size.h"
22 const char kMimeTypeFilename
[] = "chromium/filename";
23 const char kMimeTypeBitmap
[] = "image/bmp";
24 const char kMimeTypePepperCustomData
[] = "chromium/x-pepper-custom-data";
25 const char kMimeTypeWebkitSmartPaste
[] = "chromium/x-webkit-paste";
26 const size_t kMaxClipboardSize
= 1;
28 // Clipboard data format used by AuraClipboard.
29 enum AuraClipboardFormat
{
39 // ClipboardData contains data copied to the Clipboard for a variety of formats.
40 // It mostly just provides APIs to cleanly access and manipulate this data.
44 : web_smart_paste_(false),
47 virtual ~ClipboardData() {}
49 // Bitmask of AuraClipboardFormat types.
50 int format() const { return format_
; }
52 const std::string
& text() const { return text_
; }
53 void set_text(const std::string
& text
) {
58 const std::string
& markup_data() const { return markup_data_
; }
59 void set_markup_data(const std::string
& markup_data
) {
60 markup_data_
= markup_data
;
64 const std::string
& rtf_data() const { return rtf_data_
; }
65 void SetRTFData(const std::string
& rtf_data
) {
70 const std::string
& url() const { return url_
; }
71 void set_url(const std::string
& url
) {
76 const std::string
& bookmark_title() const { return bookmark_title_
; }
77 void set_bookmark_title(const std::string
& bookmark_title
) {
78 bookmark_title_
= bookmark_title
;
82 const std::string
& bookmark_url() const { return bookmark_url_
; }
83 void set_bookmark_url(const std::string
& bookmark_url
) {
84 bookmark_url_
= bookmark_url
;
88 const SkBitmap
& bitmap() const { return bitmap_
; }
89 void SetBitmapData(const SkBitmap
& bitmap
) {
90 bitmap
.copyTo(&bitmap_
);
94 const std::string
& custom_data_format() const { return custom_data_format_
; }
95 const std::string
& custom_data_data() const { return custom_data_data_
; }
96 void SetCustomData(const std::string
& data_format
,
97 const std::string
& data_data
) {
98 if (data_data
.size() == 0) {
99 custom_data_data_
.clear();
100 custom_data_format_
.clear();
103 custom_data_data_
= data_data
;
104 custom_data_format_
= data_format
;
108 bool web_smart_paste() const { return web_smart_paste_
; }
109 void set_web_smart_paste(bool web_smart_paste
) {
110 web_smart_paste_
= web_smart_paste
;
115 // Plain text in UTF8 format.
118 // HTML markup data in UTF8 format.
119 std::string markup_data_
;
123 std::string rtf_data_
;
125 // Bookmark title in UTF8 format.
126 std::string bookmark_title_
;
127 std::string bookmark_url_
;
130 std::vector
<std::string
> files_
;
135 // Data with custom format.
136 std::string custom_data_format_
;
137 std::string custom_data_data_
;
139 // WebKit smart paste data.
140 bool web_smart_paste_
;
144 DISALLOW_COPY_AND_ASSIGN(ClipboardData
);
147 // Platform clipboard implementation for Aura. This handles things like format
148 // conversion, versioning of clipboard items etc. The goal is to roughly provide
149 // a substitute to platform clipboards on other platforms such as GtkClipboard
150 // on gtk or winapi clipboard on win.
151 class AuraClipboard
{
153 AuraClipboard() : sequence_number_(0) {
162 STLDeleteContainerPointers(data_list_
.begin(), data_list_
.end());
166 uint64_t sequence_number() const {
167 return sequence_number_
;
170 // Returns the data currently on the top of the clipboard stack, NULL if the
171 // clipboard stack is empty.
172 const ClipboardData
* GetData() const {
173 if (data_list_
.empty())
175 return data_list_
.front();
178 // Returns true if the data on top of the clipboard stack has format |format|
179 // or another format that can be converted to |format|.
180 bool IsFormatAvailable(AuraClipboardFormat format
) const {
183 return HasFormat(TEXT
) || HasFormat(BOOKMARK
);
185 return HasFormat(format
);
189 // Reads text from the data at the top of clipboard stack.
190 void ReadText(base::string16
* result
) const {
191 std::string utf8_result
;
192 ReadAsciiText(&utf8_result
);
193 *result
= base::UTF8ToUTF16(utf8_result
);
196 // Reads ascii text from the data at the top of clipboard stack.
197 void ReadAsciiText(std::string
* result
) const {
199 const ClipboardData
* data
= GetData();
203 *result
= data
->text();
204 else if (HasFormat(HTML
))
205 *result
= data
->markup_data();
206 else if (HasFormat(BOOKMARK
))
207 *result
= data
->bookmark_url();
210 // Reads HTML from the data at the top of clipboard stack.
211 void ReadHTML(base::string16
* markup
,
212 std::string
* src_url
,
213 uint32
* fragment_start
,
214 uint32
* fragment_end
) const {
221 if (!HasFormat(HTML
))
224 const ClipboardData
* data
= GetData();
225 *markup
= base::UTF8ToUTF16(data
->markup_data());
226 *src_url
= data
->url();
229 DCHECK_LE(markup
->length(), kuint32max
);
230 *fragment_end
= static_cast<uint32
>(markup
->length());
233 // Reads RTF from the data at the top of clipboard stack.
234 void ReadRTF(std::string
* result
) const {
236 const ClipboardData
* data
= GetData();
240 *result
= data
->rtf_data();
243 // Reads image from the data at the top of clipboard stack.
244 SkBitmap
ReadImage() const {
246 if (!HasFormat(BITMAP
))
249 // A shallow copy should be fine here, but just to be safe...
250 const SkBitmap
& clipboard_bitmap
= GetData()->bitmap();
251 clipboard_bitmap
.copyTo(&img
);
255 // Reads data of type |type| from the data at the top of clipboard stack.
256 void ReadCustomData(const base::string16
& type
,
257 base::string16
* result
) const {
259 const ClipboardData
* data
= GetData();
260 if (!HasFormat(CUSTOM
))
263 ui::ReadCustomDataForType(data
->custom_data_data().c_str(),
264 data
->custom_data_data().size(),
268 // Reads bookmark from the data at the top of clipboard stack.
269 void ReadBookmark(base::string16
* title
, std::string
* url
) const {
272 if (!HasFormat(BOOKMARK
))
275 const ClipboardData
* data
= GetData();
276 *title
= base::UTF8ToUTF16(data
->bookmark_title());
277 *url
= data
->bookmark_url();
280 void ReadData(const std::string
& type
, std::string
* result
) const {
282 const ClipboardData
* data
= GetData();
283 if (!HasFormat(CUSTOM
) || type
!= data
->custom_data_format())
286 *result
= data
->custom_data_data();
289 // Writes |data| to the top of the clipboard stack.
290 void WriteData(ClipboardData
* data
) {
292 AddToListEnsuringSize(data
);
296 // True if the data on top of the clipboard stack has format |format|.
297 bool HasFormat(AuraClipboardFormat format
) const {
298 const ClipboardData
* data
= GetData();
302 return data
->format() & format
;
305 void AddToListEnsuringSize(ClipboardData
* data
) {
308 data_list_
.push_front(data
);
310 // If the size of list becomes more than the maximum allowed, we delete the
312 if (data_list_
.size() > kMaxClipboardSize
) {
313 ClipboardData
* last
= data_list_
.back();
314 data_list_
.pop_back();
319 // Stack containing various versions of ClipboardData.
320 std::list
<ClipboardData
*> data_list_
;
322 // Sequence number uniquely identifying clipboard state.
323 uint64_t sequence_number_
;
325 DISALLOW_COPY_AND_ASSIGN(AuraClipboard
);
328 AuraClipboard
* aura_clipboard
= NULL
;
330 AuraClipboard
* GetClipboard() {
332 aura_clipboard
= new AuraClipboard();
333 return aura_clipboard
;
336 void DeleteClipboard() {
338 delete aura_clipboard
;
339 aura_clipboard
= NULL
;
342 // Helper class to build a ClipboardData object and write it to clipboard.
343 class ClipboardDataBuilder
{
345 static void CommitToClipboard() {
346 GetClipboard()->WriteData(GetCurrentData());
347 current_data_
= NULL
;
350 static void WriteText(const char* text_data
, size_t text_len
) {
351 ClipboardData
* data
= GetCurrentData();
352 data
->set_text(std::string(text_data
, text_len
));
355 static void WriteHTML(const char* markup_data
,
357 const char* url_data
,
359 ClipboardData
* data
= GetCurrentData();
360 data
->set_markup_data(std::string(markup_data
, markup_len
));
361 data
->set_url(std::string(url_data
, url_len
));
364 static void WriteRTF(const char* rtf_data
, size_t rtf_len
) {
365 ClipboardData
* data
= GetCurrentData();
366 data
->SetRTFData(std::string(rtf_data
, rtf_len
));
369 static void WriteBookmark(const char* title_data
,
371 const char* url_data
,
373 ClipboardData
* data
= GetCurrentData();
374 data
->set_bookmark_title(std::string(title_data
, title_len
));
375 data
->set_bookmark_url(std::string(url_data
, url_len
));
378 static void WriteWebSmartPaste() {
379 ClipboardData
* data
= GetCurrentData();
380 data
->set_web_smart_paste(true);
383 static void WriteBitmap(const SkBitmap
& bitmap
) {
384 ClipboardData
* data
= GetCurrentData();
385 data
->SetBitmapData(bitmap
);
388 static void WriteData(const std::string
& format
,
389 const char* data_data
,
391 ClipboardData
* data
= GetCurrentData();
392 data
->SetCustomData(format
, std::string(data_data
, data_len
));
396 static ClipboardData
* GetCurrentData() {
398 current_data_
= new ClipboardData
;
399 return current_data_
;
402 static ClipboardData
* current_data_
;
405 ClipboardData
* ClipboardDataBuilder::current_data_
= NULL
;
409 Clipboard::FormatType::FormatType() {
412 Clipboard::FormatType::FormatType(const std::string
& native_format
)
413 : data_(native_format
) {
416 Clipboard::FormatType::~FormatType() {
419 std::string
Clipboard::FormatType::Serialize() const {
424 Clipboard::FormatType
Clipboard::FormatType::Deserialize(
425 const std::string
& serialization
) {
426 return FormatType(serialization
);
429 bool Clipboard::FormatType::operator<(const FormatType
& other
) const {
430 return data_
< other
.data_
;
433 bool Clipboard::FormatType::Equals(const FormatType
& other
) const {
434 return data_
== other
.data_
;
437 Clipboard::Clipboard() {
438 DCHECK(CalledOnValidThread());
439 // Make sure clipboard is created.
443 Clipboard::~Clipboard() {
444 DCHECK(CalledOnValidThread());
448 void Clipboard::WriteObjects(ClipboardType type
, const ObjectMap
& objects
) {
449 DCHECK(CalledOnValidThread());
450 DCHECK(IsSupportedClipboardType(type
));
451 for (ObjectMap::const_iterator iter
= objects
.begin();
452 iter
!= objects
.end(); ++iter
) {
453 DispatchObject(static_cast<ObjectType
>(iter
->first
), iter
->second
);
455 ClipboardDataBuilder::CommitToClipboard();
458 bool Clipboard::IsFormatAvailable(const FormatType
& format
,
459 ClipboardType type
) const {
460 DCHECK(CalledOnValidThread());
461 DCHECK(IsSupportedClipboardType(type
));
462 AuraClipboard
* clipboard
= GetClipboard();
463 if (GetPlainTextFormatType().Equals(format
) ||
464 GetUrlFormatType().Equals(format
))
465 return clipboard
->IsFormatAvailable(TEXT
);
466 else if (GetHtmlFormatType().Equals(format
))
467 return clipboard
->IsFormatAvailable(HTML
);
468 else if (GetRtfFormatType().Equals(format
))
469 return clipboard
->IsFormatAvailable(RTF
);
470 else if (GetBitmapFormatType().Equals(format
))
471 return clipboard
->IsFormatAvailable(BITMAP
);
472 else if (GetWebKitSmartPasteFormatType().Equals(format
))
473 return clipboard
->IsFormatAvailable(WEB
);
475 const ClipboardData
* data
= clipboard
->GetData();
476 if (data
&& data
->custom_data_format() == format
.ToString())
482 void Clipboard::Clear(ClipboardType type
) {
483 DCHECK(CalledOnValidThread());
484 DCHECK(IsSupportedClipboardType(type
));
485 AuraClipboard
* clipboard
= GetClipboard();
489 void Clipboard::ReadAvailableTypes(ClipboardType type
,
490 std::vector
<base::string16
>* types
,
491 bool* contains_filenames
) const {
492 DCHECK(CalledOnValidThread());
493 if (!types
|| !contains_filenames
) {
499 *contains_filenames
= false;
500 if (IsFormatAvailable(GetPlainTextFormatType(), type
))
501 types
->push_back(base::UTF8ToUTF16(GetPlainTextFormatType().ToString()));
502 if (IsFormatAvailable(GetHtmlFormatType(), type
))
503 types
->push_back(base::UTF8ToUTF16(GetHtmlFormatType().ToString()));
504 if (IsFormatAvailable(GetRtfFormatType(), type
))
505 types
->push_back(base::UTF8ToUTF16(GetRtfFormatType().ToString()));
506 if (IsFormatAvailable(GetBitmapFormatType(), type
))
507 types
->push_back(base::UTF8ToUTF16(kMimeTypePNG
));
509 AuraClipboard
* clipboard
= GetClipboard();
510 if (clipboard
->IsFormatAvailable(CUSTOM
) && clipboard
->GetData()) {
511 ui::ReadCustomDataTypes(clipboard
->GetData()->custom_data_data().c_str(),
512 clipboard
->GetData()->custom_data_data().size(), types
);
516 void Clipboard::ReadText(ClipboardType type
, base::string16
* result
) const {
517 DCHECK(CalledOnValidThread());
518 GetClipboard()->ReadText(result
);
521 void Clipboard::ReadAsciiText(ClipboardType type
, std::string
* result
) const {
522 DCHECK(CalledOnValidThread());
523 GetClipboard()->ReadAsciiText(result
);
526 void Clipboard::ReadHTML(ClipboardType type
,
527 base::string16
* markup
,
528 std::string
* src_url
,
529 uint32
* fragment_start
,
530 uint32
* fragment_end
) const {
531 DCHECK(CalledOnValidThread());
532 GetClipboard()->ReadHTML(markup
, src_url
, fragment_start
, fragment_end
);
535 void Clipboard::ReadRTF(ClipboardType type
, std::string
* result
) const {
536 DCHECK(CalledOnValidThread());
537 GetClipboard()->ReadRTF(result
);
540 SkBitmap
Clipboard::ReadImage(ClipboardType type
) const {
541 DCHECK(CalledOnValidThread());
542 return GetClipboard()->ReadImage();
545 void Clipboard::ReadCustomData(ClipboardType clipboard_type
,
546 const base::string16
& type
,
547 base::string16
* result
) const {
548 DCHECK(CalledOnValidThread());
549 GetClipboard()->ReadCustomData(type
, result
);
552 void Clipboard::ReadBookmark(base::string16
* title
, std::string
* url
) const {
553 DCHECK(CalledOnValidThread());
554 GetClipboard()->ReadBookmark(title
, url
);
557 void Clipboard::ReadData(const FormatType
& format
, std::string
* result
) const {
558 DCHECK(CalledOnValidThread());
559 GetClipboard()->ReadData(format
.ToString(), result
);
562 uint64
Clipboard::GetSequenceNumber(ClipboardType type
) {
563 DCHECK(CalledOnValidThread());
564 return GetClipboard()->sequence_number();
567 void Clipboard::WriteText(const char* text_data
, size_t text_len
) {
568 ClipboardDataBuilder::WriteText(text_data
, text_len
);
571 void Clipboard::WriteHTML(const char* markup_data
,
573 const char* url_data
,
575 ClipboardDataBuilder::WriteHTML(markup_data
, markup_len
, url_data
, url_len
);
578 void Clipboard::WriteRTF(const char* rtf_data
, size_t data_len
) {
579 ClipboardDataBuilder::WriteRTF(rtf_data
, data_len
);
582 void Clipboard::WriteBookmark(const char* title_data
,
584 const char* url_data
,
586 ClipboardDataBuilder::WriteBookmark(title_data
, title_len
, url_data
, url_len
);
589 void Clipboard::WriteWebSmartPaste() {
590 ClipboardDataBuilder::WriteWebSmartPaste();
593 void Clipboard::WriteBitmap(const SkBitmap
& bitmap
) {
594 ClipboardDataBuilder::WriteBitmap(bitmap
);
597 void Clipboard::WriteData(const FormatType
& format
,
598 const char* data_data
,
600 ClipboardDataBuilder::WriteData(format
.ToString(), data_data
, data_len
);
604 Clipboard::FormatType
Clipboard::GetFormatType(
605 const std::string
& format_string
) {
606 return FormatType::Deserialize(format_string
);
610 const Clipboard::FormatType
& Clipboard::GetUrlFormatType() {
611 CR_DEFINE_STATIC_LOCAL(FormatType
, type
, (kMimeTypeURIList
));
616 const Clipboard::FormatType
& Clipboard::GetUrlWFormatType() {
617 return GetUrlFormatType();
621 const Clipboard::FormatType
& Clipboard::GetPlainTextFormatType() {
622 CR_DEFINE_STATIC_LOCAL(FormatType
, type
, (kMimeTypeText
));
627 const Clipboard::FormatType
& Clipboard::GetPlainTextWFormatType() {
628 return GetPlainTextFormatType();
632 const Clipboard::FormatType
& Clipboard::GetFilenameFormatType() {
633 CR_DEFINE_STATIC_LOCAL(FormatType
, type
, (kMimeTypeFilename
));
638 const Clipboard::FormatType
& Clipboard::GetFilenameWFormatType() {
639 return Clipboard::GetFilenameFormatType();
643 const Clipboard::FormatType
& Clipboard::GetHtmlFormatType() {
644 CR_DEFINE_STATIC_LOCAL(FormatType
, type
, (kMimeTypeHTML
));
649 const Clipboard::FormatType
& Clipboard::GetRtfFormatType() {
650 CR_DEFINE_STATIC_LOCAL(FormatType
, type
, (kMimeTypeRTF
));
655 const Clipboard::FormatType
& Clipboard::GetBitmapFormatType() {
656 CR_DEFINE_STATIC_LOCAL(FormatType
, type
, (kMimeTypeBitmap
));
661 const Clipboard::FormatType
& Clipboard::GetWebKitSmartPasteFormatType() {
662 CR_DEFINE_STATIC_LOCAL(FormatType
, type
, (kMimeTypeWebkitSmartPaste
));
667 const Clipboard::FormatType
& Clipboard::GetWebCustomDataFormatType() {
668 CR_DEFINE_STATIC_LOCAL(FormatType
, type
, (kMimeTypeWebCustomData
));
673 const Clipboard::FormatType
& Clipboard::GetPepperCustomDataFormatType() {
674 CR_DEFINE_STATIC_LOCAL(FormatType
, type
, (kMimeTypePepperCustomData
));