Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / ui / base / clipboard / scoped_clipboard_writer.cc
blob5044f62d41f88cd4d05c4e4d4f58a79f4e3fc849
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/geometry/size.h"
16 namespace ui {
18 ScopedClipboardWriter::ScopedClipboardWriter(ClipboardType type) : type_(type) {
21 ScopedClipboardWriter::~ScopedClipboardWriter() {
22 if (!objects_.empty())
23 ui::Clipboard::GetForCurrentThread()->WriteObjects(type_, objects_);
26 void ScopedClipboardWriter::WriteText(const base::string16& text) {
27 WriteTextOrURL(text, false);
30 void ScopedClipboardWriter::WriteURL(const base::string16& text) {
31 WriteTextOrURL(text, true);
34 void ScopedClipboardWriter::WriteHTML(const base::string16& markup,
35 const std::string& source_url) {
36 std::string utf8_markup = base::UTF16ToUTF8(markup);
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()));
47 objects_[Clipboard::CBF_HTML] = parameters;
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;
57 void ScopedClipboardWriter::WriteBookmark(const base::string16& bookmark_title,
58 const std::string& url) {
59 if (bookmark_title.empty() || url.empty())
60 return;
62 std::string utf8_markup = base::UTF16ToUTF8(bookmark_title);
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;
71 void ScopedClipboardWriter::WriteHyperlink(const base::string16& anchor_text,
72 const std::string& url) {
73 if (anchor_text.empty() || url.empty())
74 return;
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());
85 void ScopedClipboardWriter::WriteWebSmartPaste() {
86 objects_[Clipboard::CBF_WEBKIT] = Clipboard::ObjectMapParams();
89 void ScopedClipboardWriter::WriteImage(const SkBitmap& bitmap) {
90 if (bitmap.drawsNothing()) {
91 return;
93 bitmap_ = bitmap;
94 // TODO(dcheng): This is slightly less horrible than what we used to do, but
95 // only very slightly less.
96 SkBitmap* bitmap_pointer = &bitmap_;
97 Clipboard::ObjectMapParam packed_pointer;
98 packed_pointer.resize(sizeof(bitmap_pointer));
99 *reinterpret_cast<SkBitmap**>(&*packed_pointer.begin()) = bitmap_pointer;
100 Clipboard::ObjectMapParams parameters;
101 parameters.push_back(packed_pointer);
102 objects_[Clipboard::CBF_SMBITMAP] = parameters;
105 void ScopedClipboardWriter::WritePickledData(
106 const Pickle& pickle, const Clipboard::FormatType& format) {
107 std::string format_string = format.Serialize();
108 Clipboard::ObjectMapParam format_parameter(format_string.begin(),
109 format_string.end());
110 Clipboard::ObjectMapParam data_parameter;
112 data_parameter.resize(pickle.size());
113 memcpy(const_cast<char*>(&data_parameter.front()),
114 pickle.data(), pickle.size());
116 Clipboard::ObjectMapParams parameters;
117 parameters.push_back(format_parameter);
118 parameters.push_back(data_parameter);
119 objects_[Clipboard::CBF_DATA] = parameters;
122 void ScopedClipboardWriter::Reset() {
123 url_text_.clear();
124 objects_.clear();
125 bitmap_.reset();
128 void ScopedClipboardWriter::WriteTextOrURL(const base::string16& text,
129 bool is_url) {
130 std::string utf8_text = base::UTF16ToUTF8(text);
132 Clipboard::ObjectMapParams parameters;
133 parameters.push_back(Clipboard::ObjectMapParam(utf8_text.begin(),
134 utf8_text.end()));
135 objects_[Clipboard::CBF_TEXT] = parameters;
137 if (is_url) {
138 url_text_ = utf8_text;
139 } else {
140 url_text_.clear();
144 } // namespace ui