OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 #include "ui/base/clipboard/clipboard.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/utf_string_conversions.h" | |
9 #include "third_party/skia/include/core/SkBitmap.h" | |
10 #include "ui/gfx/linux_util.h" | |
11 #include "ui/gfx/size.h" | |
12 | |
13 namespace ui { | |
14 | |
15 namespace { | |
16 const char kMimeTypeBitmap[] = "image/bmp"; | |
17 const char kMimeTypeWebkitSmartPaste[] = "chromium/x-webkit-paste"; | |
18 | |
19 // ClipboardData contains data copied to the Clipboard for a variety of formats. | |
20 // It mostly just provides APIs to cleanly access and manipulate this data. | |
21 class ClipboardData { | |
22 public: | |
23 ClipboardData() : bitmap_data_(), | |
24 custom_data_data_(), | |
25 custom_data_len_(0), | |
26 web_smart_paste_(false) {} | |
27 | |
28 virtual ~ClipboardData() {} | |
29 | |
30 const std::string& text() const { return utf8_text_; } | |
31 void set_text(const std::string& text) { utf8_text_ = text; } | |
32 | |
33 const std::string& markup_data() const { return markup_data_; } | |
34 void set_markup_data(const std::string& markup_data) { | |
35 markup_data_ = markup_data; | |
36 } | |
37 | |
38 const std::string& url() const { return url_; } | |
39 void set_url(const std::string& url) { url_ = url; } | |
40 | |
41 const std::string& bookmark_title() const { return bookmark_title_; } | |
42 void set_bookmark_title(const std::string& bookmark_title) { | |
43 bookmark_title_ = bookmark_title; | |
44 } | |
45 | |
46 const std::string& bookmark_url() const { return bookmark_url_; } | |
47 void set_bookmark_url(const std::string& bookmark_url) { | |
48 bookmark_url_ = bookmark_url; | |
49 } | |
50 | |
51 uint8_t* bitmap_data() const { return bitmap_data_.get(); } | |
52 void set_bitmap_data(uint8_t* bitmap_data) { | |
53 bitmap_data_.reset(bitmap_data); | |
54 } | |
55 | |
56 const gfx::Size bitmap_size() const { return bitmap_size_; } | |
57 void set_bitmap_size(const gfx::Size bitmap_size) { | |
58 bitmap_size_ = bitmap_size; | |
59 } | |
60 | |
61 const std::string& custom_data_format() const { return custom_data_format_; } | |
62 char* custom_data_data() const { return custom_data_data_.get(); } | |
63 size_t custom_data_len() const { return custom_data_len_; } | |
64 | |
65 void SetCustomData(const std::string& data_format, | |
66 const char* data_data, | |
67 size_t data_len) { | |
68 custom_data_len_ = data_len; | |
69 if (custom_data_len_ == 0) | |
70 return; | |
71 custom_data_data_.reset(new char[custom_data_len_]); | |
72 memcpy(custom_data_data_.get(), data_data, custom_data_len_); | |
73 custom_data_format_ = data_format; | |
74 } | |
75 | |
76 bool web_smart_paste() const { return web_smart_paste_; } | |
77 void set_web_smart_paste(bool web_smart_paste) { | |
78 web_smart_paste_ = web_smart_paste; | |
79 } | |
80 | |
81 private: | |
82 // Plain text | |
83 std::string utf8_text_; | |
84 | |
85 // HTML | |
86 std::string markup_data_; | |
87 std::string url_; | |
88 | |
89 // Bookmarks | |
90 std::string bookmark_title_; | |
91 std::string bookmark_url_; | |
92 | |
93 // Bitmap images | |
94 scoped_ptr_malloc<uint8_t> bitmap_data_; | |
95 gfx::Size bitmap_size_; | |
96 | |
97 // Data with custom format | |
98 std::string custom_data_format_; | |
99 scoped_array<char> custom_data_data_; | |
100 size_t custom_data_len_; | |
101 | |
102 // WebKit smart paste data | |
103 bool web_smart_paste_; | |
104 | |
105 DISALLOW_COPY_AND_ASSIGN(ClipboardData); | |
106 }; | |
107 | |
108 ClipboardData* clipboard_data = NULL; | |
109 | |
110 ClipboardData* GetClipboardData() { | |
111 if (!clipboard_data) | |
112 clipboard_data = new ClipboardData(); | |
113 return clipboard_data; | |
114 } | |
115 | |
116 void DeleteClipboardData() { | |
117 if (clipboard_data) | |
118 delete clipboard_data; | |
119 clipboard_data = NULL; | |
120 } | |
121 | |
122 } // namespace | |
123 | |
124 // TODO(varunjain): Complete implementation: | |
125 // 1. Handle different types of BUFFERs. | |
126 // 2. Do we need to care about concurrency here? Can there be multiple instances | |
127 // of ui::Clipboard? Ask oshima. | |
128 // 3. Implement File types. | |
129 // 4. Handle conversion between types. | |
130 | |
131 Clipboard::Clipboard() { | |
132 // Make sure clipboard is created. | |
133 GetClipboardData(); | |
134 } | |
135 | |
136 Clipboard::~Clipboard() { | |
137 } | |
138 | |
139 void Clipboard::WriteObjects(const ObjectMap& objects) { | |
140 // We need to overwrite previous data. Probably best to just delete | |
141 // everything and start fresh. | |
142 DeleteClipboardData(); | |
143 for (ObjectMap::const_iterator iter = objects.begin(); | |
144 iter != objects.end(); ++iter) { | |
145 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); | |
146 } | |
147 } | |
148 | |
149 void Clipboard::WriteObjects(const ObjectMap& objects, | |
150 base::ProcessHandle process) { | |
151 NOTIMPLEMENTED(); | |
152 } | |
153 | |
154 bool Clipboard::IsFormatAvailable(const FormatType& format, | |
155 Buffer buffer) const { | |
156 ClipboardData* data = GetClipboardData(); | |
157 if (GetPlainTextFormatType() == format) | |
158 return !data->text().empty(); | |
159 else if (GetHtmlFormatType() == format) | |
160 return !data->markup_data().empty() || !data->url().empty(); | |
161 else if (GetBitmapFormatType() == format) | |
162 return data->bitmap_data(); | |
oshima
2011/10/24 23:31:26
!! data->bitmap_data();
(this is common pattern in
varunjain
2011/10/25 21:04:40
Done.
| |
163 else if (GetWebKitSmartPasteFormatType() == format) | |
164 return data->web_smart_paste(); | |
oshima
2011/10/24 23:31:26
ditto
varunjain
2011/10/25 21:04:40
web smart paste is already a bool
| |
165 else if (data->custom_data_format() == format) | |
166 return true; | |
167 return false; | |
168 } | |
169 | |
170 bool Clipboard::IsFormatAvailableByString(const std::string& format, | |
171 Buffer buffer) const { | |
172 return IsFormatAvailable(format, buffer); | |
173 } | |
174 | |
175 void Clipboard::ReadAvailableTypes(Buffer buffer, std::vector<string16>* types, | |
176 bool* contains_filenames) const { | |
177 if (!types || !contains_filenames) { | |
178 NOTREACHED(); | |
179 return; | |
180 } | |
181 | |
182 types->clear(); | |
183 if (IsFormatAvailable(GetPlainTextFormatType(), buffer)) | |
184 types->push_back(UTF8ToUTF16(GetPlainTextFormatType())); | |
185 if (IsFormatAvailable(GetHtmlFormatType(), buffer)) | |
186 types->push_back(UTF8ToUTF16(GetHtmlFormatType())); | |
187 if (IsFormatAvailable(GetBitmapFormatType(), buffer)) | |
188 types->push_back(UTF8ToUTF16(GetBitmapFormatType())); | |
189 if (IsFormatAvailable(GetWebKitSmartPasteFormatType(), buffer)) | |
190 types->push_back(UTF8ToUTF16(GetWebKitSmartPasteFormatType())); | |
191 *contains_filenames = false; | |
192 } | |
193 | |
194 void Clipboard::ReadText(Buffer buffer, string16* result) const { | |
195 *result = UTF8ToUTF16(GetClipboardData()->text()); | |
196 } | |
197 | |
198 void Clipboard::ReadAsciiText(Buffer buffer, std::string* result) const { | |
199 *result = GetClipboardData()->text(); | |
200 } | |
201 | |
202 void Clipboard::ReadHTML(Buffer buffer, string16* markup, std::string* src_url, | |
203 uint32* fragment_start, uint32* fragment_end) const { | |
204 markup->clear(); | |
205 if (src_url) | |
206 src_url->clear(); | |
207 *fragment_start = 0; | |
208 *fragment_end = 0; | |
209 | |
210 *markup = UTF8ToUTF16(GetClipboardData()->markup_data()); | |
211 *src_url = GetClipboardData()->url(); | |
212 | |
213 *fragment_start = 0; | |
214 DCHECK(markup->length() <= kuint32max); | |
215 *fragment_end = static_cast<uint32>(markup->length()); | |
216 | |
217 } | |
218 | |
219 SkBitmap Clipboard::ReadImage(Buffer buffer) const { | |
220 const gfx::Size size = GetClipboardData()->bitmap_size(); | |
221 uint8_t* bitmap = GetClipboardData()->bitmap_data(); | |
222 SkBitmap image; | |
223 image.setConfig(SkBitmap::kARGB_8888_Config, size.width(), size.height(), 0); | |
224 image.allocPixels(); | |
225 image.eraseARGB(0, 0, 0, 0); | |
226 int byte_counter = 0; | |
227 for (int i = 0; i < size.height(); ++i) { | |
228 for (int j = 0; j < size.width(); ++j) { | |
229 uint32* pixel = image.getAddr32(j, i); | |
230 // We store the pixels in RGBA format. Create one ARGB pixel from four | |
231 // RGBA values. | |
232 *pixel = (bitmap[byte_counter] << 16) + /* R */ | |
233 (bitmap[byte_counter + 1] << 8) + /* G */ | |
234 (bitmap[byte_counter + 2] << 0) + /* B */ | |
235 (bitmap[byte_counter + 3] << 24); /* A */ | |
236 byte_counter += 4; | |
237 } | |
238 } | |
239 return image; | |
240 } | |
241 | |
242 void Clipboard::ReadBookmark(string16* title, std::string* url) const { | |
243 *title = UTF8ToUTF16(GetClipboardData()->bookmark_title()); | |
244 *url = GetClipboardData()->bookmark_url(); | |
245 } | |
246 | |
247 void Clipboard::ReadFile(FilePath* file) const { | |
248 NOTIMPLEMENTED(); | |
249 } | |
250 | |
251 void Clipboard::ReadFiles(std::vector<FilePath>* files) const { | |
252 NOTIMPLEMENTED(); | |
253 } | |
254 | |
255 void Clipboard::ReadData(const std::string& format, std::string* result) { | |
256 result->clear(); | |
257 ClipboardData* data = GetClipboardData(); | |
258 if (data->custom_data_format() == format) | |
259 *result = std::string(data->custom_data_data(), data->custom_data_len()); | |
260 } | |
261 | |
262 uint64 Clipboard::GetSequenceNumber() { | |
263 NOTIMPLEMENTED(); | |
264 return 0; | |
265 } | |
266 | |
267 void Clipboard::WriteText(const char* text_data, size_t text_len) { | |
268 GetClipboardData()->set_text(std::string(text_data, text_len)); | |
269 } | |
270 | |
271 void Clipboard::WriteHTML(const char* markup_data, | |
272 size_t markup_len, | |
273 const char* url_data, | |
274 size_t url_len) { | |
275 GetClipboardData()->set_markup_data(std::string(markup_data, markup_len)); | |
276 GetClipboardData()->set_url(std::string(url_data, url_len)); | |
277 } | |
278 | |
279 void Clipboard::WriteBookmark(const char* title_data, | |
280 size_t title_len, | |
281 const char* url_data, | |
282 size_t url_len) { | |
283 GetClipboardData()->set_bookmark_title(std::string(title_data, title_len)); | |
284 GetClipboardData()->set_bookmark_url(std::string(url_data, url_len)); | |
285 } | |
286 | |
287 void Clipboard::WriteWebSmartPaste() { | |
288 GetClipboardData()->set_web_smart_paste(true); | |
289 } | |
290 | |
291 void Clipboard::WriteBitmap(const char* pixel_data, const char* size_data) { | |
292 const gfx::Size* size = reinterpret_cast<const gfx::Size*>(size_data); | |
293 | |
294 // Create an RGBA bitmap and store it in ClipboardData | |
295 GetClipboardData()->set_bitmap_data( | |
296 gfx::BGRAToRGBA(reinterpret_cast<const uint8_t*>(pixel_data), | |
297 size->width(), size->height(), 0)); | |
298 GetClipboardData()->set_bitmap_size(*size); | |
299 } | |
300 | |
301 void Clipboard::WriteData(const char* format_name, size_t format_len, | |
302 const char* data_data, size_t data_len) { | |
303 GetClipboardData()->SetCustomData(std::string(format_name, format_len), | |
304 data_data, data_len); | |
305 } | |
306 | |
307 // static | |
308 Clipboard::FormatType Clipboard::GetPlainTextFormatType() { | |
309 return std::string(kMimeTypeText); | |
310 } | |
311 | |
312 // static | |
313 Clipboard::FormatType Clipboard::GetPlainTextWFormatType() { | |
314 return GetPlainTextFormatType(); | |
315 } | |
316 | |
317 // static | |
318 Clipboard::FormatType Clipboard::GetHtmlFormatType() { | |
319 return std::string(kMimeTypeHTML); | |
320 } | |
321 | |
322 // static | |
323 Clipboard::FormatType Clipboard::GetBitmapFormatType() { | |
324 return std::string(kMimeTypeBitmap); | |
325 } | |
326 | |
327 // static | |
328 Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() { | |
329 return std::string(kMimeTypeWebkitSmartPaste); | |
330 } | |
331 | |
332 } // namespace ui | |
OLD | NEW |