Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / base / clipboard / scoped_clipboard_writer.cc
blob380b8d2fb848bdb9f012ed0595df1371124e449b
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"
16 namespace ui {
18 ScopedClipboardWriter::ScopedClipboardWriter(Clipboard* clipboard,
19 ClipboardType type)
20 : clipboard_(clipboard),
21 type_(type) {
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;
42 parameters.push_back(
43 Clipboard::ObjectMapParam(utf8_markup.begin(),
44 utf8_markup.end()));
45 if (!source_url.empty()) {
46 parameters.push_back(Clipboard::ObjectMapParam(source_url.begin(),
47 source_url.end()));
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(),
56 rtf_data.end()));
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())
63 return;
65 std::string utf8_markup = base::UTF16ToUTF8(bookmark_title);
67 Clipboard::ObjectMapParams parameters;
68 parameters.push_back(Clipboard::ObjectMapParam(utf8_markup.begin(),
69 utf8_markup.end()));
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())
77 return;
79 // Construct the hyperlink.
80 std::string html("<a href=\"");
81 html.append(net::EscapeForHTML(url));
82 html.append("\">");
83 html.append(net::EscapeForHTML(base::UTF16ToUTF8(anchor_text)));
84 html.append("</a>");
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(),
96 format_string.end());
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() {
110 url_text_.clear();
111 objects_.clear();
114 void ScopedClipboardWriter::WriteTextOrURL(const base::string16& text,
115 bool is_url) {
116 std::string utf8_text = base::UTF16ToUTF8(text);
118 Clipboard::ObjectMapParams parameters;
119 parameters.push_back(Clipboard::ObjectMapParam(utf8_text.begin(),
120 utf8_text.end()));
121 objects_[Clipboard::CBF_TEXT] = parameters;
123 if (is_url) {
124 url_text_ = utf8_text;
125 } else {
126 url_text_.clear();
130 } // namespace ui