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 // This file implements the ScopedClipboardWriter class. Documentation on its
6 // purpose can be found in our header. Documentation on the format of the
7 // parameters for each clipboard target can be found in clipboard.h.
9 #include "ui/base/clipboard/scoped_clipboard_writer.h"
11 #include "base/pickle.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "net/base/escape.h"
14 #include "ui/gfx/size.h"
18 ScopedClipboardWriter::ScopedClipboardWriter(Clipboard
* clipboard
,
20 : clipboard_(clipboard
),
24 ScopedClipboardWriter::~ScopedClipboardWriter() {
25 if (!objects_
.empty() && clipboard_
)
26 clipboard_
->WriteObjects(type_
, objects_
);
29 void ScopedClipboardWriter::WriteText(const base::string16
& text
) {
30 WriteTextOrURL(text
, false);
33 void ScopedClipboardWriter::WriteURL(const base::string16
& text
) {
34 WriteTextOrURL(text
, true);
37 void ScopedClipboardWriter::WriteHTML(const base::string16
& markup
,
38 const std::string
& source_url
) {
39 std::string utf8_markup
= base::UTF16ToUTF8(markup
);
41 Clipboard::ObjectMapParams parameters
;
43 Clipboard::ObjectMapParam(utf8_markup
.begin(),
45 if (!source_url
.empty()) {
46 parameters
.push_back(Clipboard::ObjectMapParam(source_url
.begin(),
50 objects_
[Clipboard::CBF_HTML
] = parameters
;
53 void ScopedClipboardWriter::WriteRTF(const std::string
& rtf_data
) {
54 Clipboard::ObjectMapParams parameters
;
55 parameters
.push_back(Clipboard::ObjectMapParam(rtf_data
.begin(),
57 objects_
[Clipboard::CBF_RTF
] = parameters
;
60 void ScopedClipboardWriter::WriteBookmark(const base::string16
& bookmark_title
,
61 const std::string
& url
) {
62 if (bookmark_title
.empty() || url
.empty())
65 std::string utf8_markup
= base::UTF16ToUTF8(bookmark_title
);
67 Clipboard::ObjectMapParams parameters
;
68 parameters
.push_back(Clipboard::ObjectMapParam(utf8_markup
.begin(),
70 parameters
.push_back(Clipboard::ObjectMapParam(url
.begin(), url
.end()));
71 objects_
[Clipboard::CBF_BOOKMARK
] = parameters
;
74 void ScopedClipboardWriter::WriteHyperlink(const base::string16
& anchor_text
,
75 const std::string
& url
) {
76 if (anchor_text
.empty() || url
.empty())
79 // Construct the hyperlink.
80 std::string
html("<a href=\"");
81 html
.append(net::EscapeForHTML(url
));
83 html
.append(net::EscapeForHTML(base::UTF16ToUTF8(anchor_text
)));
85 WriteHTML(base::UTF8ToUTF16(html
), std::string());
88 void ScopedClipboardWriter::WriteWebSmartPaste() {
89 objects_
[Clipboard::CBF_WEBKIT
] = Clipboard::ObjectMapParams();
92 void ScopedClipboardWriter::WritePickledData(
93 const Pickle
& pickle
, const Clipboard::FormatType
& format
) {
94 std::string format_string
= format
.Serialize();
95 Clipboard::ObjectMapParam
format_parameter(format_string
.begin(),
97 Clipboard::ObjectMapParam data_parameter
;
99 data_parameter
.resize(pickle
.size());
100 memcpy(const_cast<char*>(&data_parameter
.front()),
101 pickle
.data(), pickle
.size());
103 Clipboard::ObjectMapParams parameters
;
104 parameters
.push_back(format_parameter
);
105 parameters
.push_back(data_parameter
);
106 objects_
[Clipboard::CBF_DATA
] = parameters
;
109 void ScopedClipboardWriter::Reset() {
114 void ScopedClipboardWriter::WriteTextOrURL(const base::string16
& text
,
116 std::string utf8_text
= base::UTF16ToUTF8(text
);
118 Clipboard::ObjectMapParams parameters
;
119 parameters
.push_back(Clipboard::ObjectMapParam(utf8_text
.begin(),
121 objects_
[Clipboard::CBF_TEXT
] = parameters
;
124 url_text_
= utf8_text
;