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.
4
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.
8
9 #include "ui/base/clipboard/scoped_clipboard_writer.h"
10
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"
15
16 namespace ui {
17
ScopedClipboardWriter(ClipboardType type)18 ScopedClipboardWriter::ScopedClipboardWriter(ClipboardType type) : type_(type) {
19 }
20
~ScopedClipboardWriter()21 ScopedClipboardWriter::~ScopedClipboardWriter() {
22 if (!objects_.empty())
23 ui::Clipboard::GetForCurrentThread()->WriteObjects(type_, objects_);
24 }
25
WriteText(const base::string16 & text)26 void ScopedClipboardWriter::WriteText(const base::string16& text) {
27 WriteTextOrURL(text, false);
28 }
29
WriteURL(const base::string16 & text)30 void ScopedClipboardWriter::WriteURL(const base::string16& text) {
31 WriteTextOrURL(text, true);
32 }
33
WriteHTML(const base::string16 & markup,const std::string & source_url)34 void ScopedClipboardWriter::WriteHTML(const base::string16& markup,
35 const std::string& source_url) {
36 std::string utf8_markup = base::UTF16ToUTF8(markup);
37
38 Clipboard::ObjectMapParams parameters;
39 parameters.push_back(
40 Clipboard::ObjectMapParam(utf8_markup.begin(),
41 utf8_markup.end()));
42 if (!source_url.empty()) {
43 parameters.push_back(Clipboard::ObjectMapParam(source_url.begin(),
44 source_url.end()));
45 }
46
47 objects_[Clipboard::CBF_HTML] = parameters;
48 }
49
WriteRTF(const std::string & rtf_data)50 void ScopedClipboardWriter::WriteRTF(const std::string& rtf_data) {
51 Clipboard::ObjectMapParams parameters;
52 parameters.push_back(Clipboard::ObjectMapParam(rtf_data.begin(),
53 rtf_data.end()));
54 objects_[Clipboard::CBF_RTF] = parameters;
55 }
56
WriteBookmark(const base::string16 & bookmark_title,const std::string & url)57 void ScopedClipboardWriter::WriteBookmark(const base::string16& bookmark_title,
58 const std::string& url) {
59 if (bookmark_title.empty() || url.empty())
60 return;
61
62 std::string utf8_markup = base::UTF16ToUTF8(bookmark_title);
63
64 Clipboard::ObjectMapParams parameters;
65 parameters.push_back(Clipboard::ObjectMapParam(utf8_markup.begin(),
66 utf8_markup.end()));
67 parameters.push_back(Clipboard::ObjectMapParam(url.begin(), url.end()));
68 objects_[Clipboard::CBF_BOOKMARK] = parameters;
69 }
70
WriteHyperlink(const base::string16 & anchor_text,const std::string & url)71 void ScopedClipboardWriter::WriteHyperlink(const base::string16& anchor_text,
72 const std::string& url) {
73 if (anchor_text.empty() || url.empty())
74 return;
75
76 // Construct the hyperlink.
77 std::string html("<a href=\"");
78 html.append(net::EscapeForHTML(url));
79 html.append("\">");
80 html.append(net::EscapeForHTML(base::UTF16ToUTF8(anchor_text)));
81 html.append("</a>");
82 WriteHTML(base::UTF8ToUTF16(html), std::string());
83 }
84
WriteWebSmartPaste()85 void ScopedClipboardWriter::WriteWebSmartPaste() {
86 objects_[Clipboard::CBF_WEBKIT] = Clipboard::ObjectMapParams();
87 }
88
WritePickledData(const Pickle & pickle,const Clipboard::FormatType & format)89 void ScopedClipboardWriter::WritePickledData(
90 const Pickle& pickle, const Clipboard::FormatType& format) {
91 std::string format_string = format.Serialize();
92 Clipboard::ObjectMapParam format_parameter(format_string.begin(),
93 format_string.end());
94 Clipboard::ObjectMapParam data_parameter;
95
96 data_parameter.resize(pickle.size());
97 memcpy(const_cast<char*>(&data_parameter.front()),
98 pickle.data(), pickle.size());
99
100 Clipboard::ObjectMapParams parameters;
101 parameters.push_back(format_parameter);
102 parameters.push_back(data_parameter);
103 objects_[Clipboard::CBF_DATA] = parameters;
104 }
105
Reset()106 void ScopedClipboardWriter::Reset() {
107 url_text_.clear();
108 objects_.clear();
109 }
110
WriteTextOrURL(const base::string16 & text,bool is_url)111 void ScopedClipboardWriter::WriteTextOrURL(const base::string16& text,
112 bool is_url) {
113 std::string utf8_text = base::UTF16ToUTF8(text);
114
115 Clipboard::ObjectMapParams parameters;
116 parameters.push_back(Clipboard::ObjectMapParam(utf8_text.begin(),
117 utf8_text.end()));
118 objects_[Clipboard::CBF_TEXT] = parameters;
119
120 if (is_url) {
121 url_text_ = utf8_text;
122 } else {
123 url_text_.clear();
124 }
125 }
126
127 } // namespace ui
128