OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 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 base/scoped_clipboard_writer.h. Documentation on the | |
7 // format of the parameters for each clipboard target can be found in | |
8 // base/clipboard.h. | |
9 | |
10 #include "base/scoped_clipboard_writer.h" | |
11 | |
12 #include "base/gfx/size.h" | |
13 #include "base/pickle.h" | |
14 #include "base/string_util.h" | |
15 | |
16 ScopedClipboardWriter::ScopedClipboardWriter(Clipboard* clipboard) | |
17 : clipboard_(clipboard) { | |
18 } | |
19 | |
20 ScopedClipboardWriter::~ScopedClipboardWriter() { | |
21 if (!objects_.empty() && clipboard_) { | |
22 clipboard_->WriteObjects(objects_); | |
23 if (url_text_.length()) | |
24 clipboard_->DidWriteURL(url_text_); | |
25 } | |
26 } | |
27 | |
28 void ScopedClipboardWriter::WriteText(const string16& text) { | |
29 WriteTextOrURL(text, false); | |
30 } | |
31 | |
32 void ScopedClipboardWriter::WriteURL(const string16& text) { | |
33 WriteTextOrURL(text, true); | |
34 } | |
35 | |
36 void ScopedClipboardWriter::WriteHTML(const string16& markup, | |
37 const std::string& source_url) { | |
38 if (markup.empty()) | |
39 return; | |
40 | |
41 std::string utf8_markup = UTF16ToUTF8(markup); | |
42 | |
43 Clipboard::ObjectMapParams parameters; | |
44 parameters.push_back( | |
45 Clipboard::ObjectMapParam(utf8_markup.begin(), | |
46 utf8_markup.end())); | |
47 if (!source_url.empty()) { | |
48 parameters.push_back(Clipboard::ObjectMapParam(source_url.begin(), | |
49 source_url.end())); | |
50 } | |
51 | |
52 objects_[Clipboard::CBF_HTML] = parameters; | |
53 } | |
54 | |
55 void ScopedClipboardWriter::WriteBookmark(const string16& bookmark_title, | |
56 const std::string& url) { | |
57 if (bookmark_title.empty() || url.empty()) | |
58 return; | |
59 | |
60 std::string utf8_markup = UTF16ToUTF8(bookmark_title); | |
61 | |
62 Clipboard::ObjectMapParams parameters; | |
63 parameters.push_back(Clipboard::ObjectMapParam(utf8_markup.begin(), | |
64 utf8_markup.end())); | |
65 parameters.push_back(Clipboard::ObjectMapParam(url.begin(), url.end())); | |
66 objects_[Clipboard::CBF_BOOKMARK] = parameters; | |
67 } | |
68 | |
69 void ScopedClipboardWriter::WriteHyperlink(const std::string& anchor_text, | |
70 const std::string& url) { | |
71 if (anchor_text.empty() || url.empty()) | |
72 return; | |
73 | |
74 // Construct the hyperlink. | |
75 std::string html("<a href=\""); | |
76 html.append(url); | |
77 html.append("\">"); | |
78 html.append(anchor_text); | |
79 html.append("</a>"); | |
80 WriteHTML(UTF8ToUTF16(html), std::string()); | |
81 } | |
82 | |
83 void ScopedClipboardWriter::WriteFile(const FilePath& file) { | |
84 WriteFiles(std::vector<FilePath>(1, file)); | |
85 } | |
86 | |
87 // Save the filenames as a string separated by nulls and terminated with an | |
88 // extra null. | |
89 void ScopedClipboardWriter::WriteFiles(const std::vector<FilePath>& files) { | |
90 if (files.empty()) | |
91 return; | |
92 | |
93 Clipboard::ObjectMapParam parameter; | |
94 | |
95 for (std::vector<FilePath>::const_iterator iter = files.begin(); | |
96 iter != files.end(); ++iter) { | |
97 FilePath filepath = *iter; | |
98 FilePath::StringType filename = filepath.value(); | |
99 | |
100 size_t data_length = filename.length() * sizeof(FilePath::CharType); | |
101 const char* data = reinterpret_cast<const char*>(filename.data()); | |
102 const char* data_end = data + data_length; | |
103 | |
104 for (const char* ch = data; ch < data_end; ++ch) | |
105 parameter.push_back(*ch); | |
106 | |
107 // NUL-terminate the string. | |
108 for (size_t i = 0; i < sizeof(FilePath::CharType); ++i) | |
109 parameter.push_back('\0'); | |
110 } | |
111 | |
112 // NUL-terminate the string list. | |
113 for (size_t i = 0; i < sizeof(FilePath::CharType); ++i) | |
114 parameter.push_back('\0'); | |
115 | |
116 Clipboard::ObjectMapParams parameters; | |
117 parameters.push_back(parameter); | |
118 objects_[Clipboard::CBF_FILES] = parameters; | |
119 } | |
120 | |
121 void ScopedClipboardWriter::WriteWebSmartPaste() { | |
122 objects_[Clipboard::CBF_WEBKIT] = Clipboard::ObjectMapParams(); | |
123 } | |
124 | |
125 void ScopedClipboardWriter::WriteBitmapFromPixels(const void* pixels, | |
126 const gfx::Size& size) { | |
127 Clipboard::ObjectMapParam pixels_parameter, size_parameter; | |
128 const char* pixels_data = reinterpret_cast<const char*>(pixels); | |
129 size_t pixels_length = 4 * size.width() * size.height(); | |
130 for (size_t i = 0; i < pixels_length; i++) | |
131 pixels_parameter.push_back(pixels_data[i]); | |
132 | |
133 const char* size_data = reinterpret_cast<const char*>(&size); | |
134 size_t size_length = sizeof(gfx::Size); | |
135 for (size_t i = 0; i < size_length; i++) | |
136 size_parameter.push_back(size_data[i]); | |
137 | |
138 Clipboard::ObjectMapParams parameters; | |
139 parameters.push_back(pixels_parameter); | |
140 parameters.push_back(size_parameter); | |
141 objects_[Clipboard::CBF_BITMAP] = parameters; | |
142 } | |
143 | |
144 void ScopedClipboardWriter::WritePickledData(const Pickle& pickle, | |
145 Clipboard::FormatType format) { | |
146 Clipboard::ObjectMapParam format_parameter(format.begin(), format.end()); | |
147 Clipboard::ObjectMapParam data_parameter; | |
148 | |
149 data_parameter.resize(pickle.size()); | |
150 memcpy(const_cast<char*>(&data_parameter.front()), | |
151 pickle.data(), pickle.size()); | |
152 | |
153 Clipboard::ObjectMapParams parameters; | |
154 parameters.push_back(format_parameter); | |
155 parameters.push_back(data_parameter); | |
156 objects_[Clipboard::CBF_DATA] = parameters; | |
157 } | |
158 | |
159 void ScopedClipboardWriter::WriteTextOrURL(const string16& text, bool is_url) { | |
160 if (text.empty()) | |
161 return; | |
162 | |
163 std::string utf8_text = UTF16ToUTF8(text); | |
164 | |
165 Clipboard::ObjectMapParams parameters; | |
166 parameters.push_back(Clipboard::ObjectMapParam(utf8_text.begin(), | |
167 utf8_text.end())); | |
168 objects_[Clipboard::CBF_TEXT] = parameters; | |
169 | |
170 if (is_url) { | |
171 url_text_ = utf8_text; | |
172 } else { | |
173 url_text_.clear(); | |
174 } | |
175 } | |
176 | |
OLD | NEW |