Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: ui/base/clipboard/clipboard_aurax11.cc

Issue 8364037: Implement clipboard for aura and re-enable clipboard_unittests. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: modified according to comments Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/base/clipboard/clipboard_aura.cc ('k') | ui/base/clipboard/clipboard_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_(NULL),
24 custom_data_data_(NULL),
25 custom_data_len_(0),
26 web_smart_paste_(false) { }
oshima 2011/10/21 21:32:43 nit: just {}
varunjain 2011/10/24 05:51:53 Done.
27
28 virtual ~ClipboardData() {
29 // Cleanup bitmap_data_
30 CleanupBitmapData();
31
32 // Cleanup custom format data
33 CleanupDataData();
34 }
35
36 const std::string& text() const { return utf8_text_; }
37
oshima 2011/10/21 21:32:43 I prefer to remove new line to group them together
varunjain 2011/10/24 05:51:53 Done.
38 void set_text(const std::string& text) {
39 utf8_text_ = text;
40 }
41
42 const std::string& markup_data() const { return markup_data_; }
43
44 void set_markup_data(const std::string& markup_data) {
45 markup_data_ = markup_data;
46 }
47
48 const std::string& url() const { return url_; }
49
50 void set_url(const std::string& url) {
51 url_ = url;
52 }
53
54 const std::string& bookmark_title() const { return bookmark_title_; }
55
56 void set_bookmark_title(const std::string& bookmark_title) {
57 bookmark_title_ = bookmark_title;
58 }
59
60 const std::string& bookmark_url() const { return bookmark_url_; }
61
62 void set_bookmark_url(const std::string& bookmark_url) {
63 bookmark_url_ = bookmark_url;
64 }
65
66 uint8_t* bitmap_data() const { return bitmap_data_; }
67
68 void set_bitmap_data(uint8_t* bitmap_data) {
69 CleanupBitmapData();
70 bitmap_data_ = bitmap_data;
71 }
72
73 const gfx::Size bitmap_size() const { return bitmap_size_; }
74
75 void set_bitmap_size(const gfx::Size bitmap_size) {
76 bitmap_size_ = bitmap_size;
77 }
78
79 const std::string& custom_data_format() const { return custom_data_format_; }
80 char* custom_data_data() const { return custom_data_data_; }
81 size_t custom_data_len() const { return custom_data_len_; }
82
83 void SetData(const std::string& data_format,
84 const char* data_data,
85 size_t data_len) {
86 CleanupDataData();
87 custom_data_len_ = data_len;
88 if (custom_data_len_ == 0)
89 return;
90 custom_data_data_ = new char[custom_data_len_];
91 memcpy(custom_data_data_, data_data, custom_data_len_);
92 custom_data_format_ = data_format;
93 }
94
95 bool web_smart_paste() const { return web_smart_paste_; }
96
97 void set_web_smart_paste(bool web_smart_paste) {
98 web_smart_paste_ = web_smart_paste;
99 }
100
101 private:
102 void CleanupBitmapData() {
103 if (bitmap_data_)
104 free(bitmap_data_); // bitmap_data_ is allocated using malloc.
105 bitmap_data_ = NULL;
106 }
107
108 void CleanupDataData() {
109 if (custom_data_data_)
110 delete[] custom_data_data_;
111 custom_data_data_ = NULL;
112 }
113
114 // Plain text
115 std::string utf8_text_;
116
117 // HTML
118 std::string markup_data_;
119 std::string url_;
120
121 // Bookmarks
122 std::string bookmark_title_;
oshima 2011/10/21 21:32:43 utf8_ or just use text_ for text() and document s
varunjain 2011/10/24 05:51:53 I am not sure I understand. are you suggesting to
oshima 2011/10/24 23:31:26 If you use utf8_ for text, then use it for other u
varunjain 2011/10/25 21:04:40 Done.
123 std::string bookmark_url_;
124
125 // Bitmap images
126 uint8_t* bitmap_data_;
oshima 2011/10/21 21:32:43 you can use scoped_generic_obj and remove CleanupB
varunjain 2011/10/24 05:51:53 actually I can just do scoped_ptr_malloc
127 gfx::Size bitmap_size_;
128
129 // Data with custom format
130 std::string custom_data_format_;
131 char* custom_data_data_;
oshima 2011/10/21 21:32:43 scoped_ptr
varunjain 2011/10/24 05:51:53 Done. but with scoped_array
oshima 2011/10/24 23:31:26 yes, scoped_array
132 size_t custom_data_len_;
133
134 // WebKit smart paste data
135 bool web_smart_paste_;
136
137 DISALLOW_COPY_AND_ASSIGN(ClipboardData);
138 };
139
140 ClipboardData* clipboard_data = NULL;
141
142 ClipboardData* GetClipboardData() {
143 if (!clipboard_data)
144 clipboard_data = new ClipboardData();
145 return clipboard_data;
146 }
147
148 void DeleteClipboardData() {
149 if (clipboard_data)
150 delete clipboard_data;
151 clipboard_data = NULL;
152 }
153
154 } // namespace
155
156 // TODO(varunjain): Complete implementation:
157 // 1. Handle different types of BUFFERs.
158 // 2. Do we need to care about concurrency here? Can there be multiple instances
159 // of ui::Clipboard? Ask oshima.
160 // 3. Implement File types.
161 // 4. Handle conversion between types.
162
163 Clipboard::Clipboard() {
164 // Make sure clipboard is created.
165 GetClipboardData();
166 }
167
168 Clipboard::~Clipboard() {
169 }
170
171 void Clipboard::WriteObjects(const ObjectMap& objects) {
172 // We need to overwrite previous data. Probably best to just delete
173 // everything and start fresh.
174 DeleteClipboardData();
175 for (ObjectMap::const_iterator iter = objects.begin();
176 iter != objects.end(); ++iter) {
177 DispatchObject(static_cast<ObjectType>(iter->first), iter->second);
178 }
179 }
180
181
182 void Clipboard::WriteObjects(const ObjectMap& objects,
183 base::ProcessHandle process) {
184 NOTIMPLEMENTED();
185 }
186
187 void Clipboard::DidWriteURL(const std::string& utf8_text) {
188 // This function seems GTK specific. We probably dont care.
189 }
oshima 2011/10/21 21:32:43 can we exclude this for AURA?
varunjain 2011/10/24 05:51:53 Done.
190
191 bool Clipboard::IsFormatAvailable(const FormatType& format,
192 Buffer buffer) const {
193 ClipboardData* data = GetClipboardData();
194 if (GetPlainTextFormatType() == format)
195 return !data->text().empty();
196 else if (GetHtmlFormatType() == format)
197 return !data->markup_data().empty() || !data->url().empty();
198 else if (GetBitmapFormatType() == format)
199 return data->bitmap_data();
200 else if (GetWebKitSmartPasteFormatType() == format)
201 return data->web_smart_paste();
202 else if (data->custom_data_format() == format)
203 return true;
204 return false;
205 }
206
207 bool Clipboard::IsFormatAvailableByString(const std::string& format,
208 Buffer buffer) const {
209 return IsFormatAvailable(format, buffer);
210 }
211
212 void Clipboard::ReadAvailableTypes(Buffer buffer, std::vector<string16>* types,
213 bool* contains_filenames) const {
214 if (!types || !contains_filenames) {
215 NOTREACHED();
216 return;
217 }
218
219 types->clear();
220 if (IsFormatAvailable(GetPlainTextFormatType(), buffer))
221 types->push_back(UTF8ToUTF16(GetPlainTextFormatType()));
222 if (IsFormatAvailable(GetHtmlFormatType(), buffer))
223 types->push_back(UTF8ToUTF16(GetHtmlFormatType()));
224 if (IsFormatAvailable(GetBitmapFormatType(), buffer))
225 types->push_back(UTF8ToUTF16(GetBitmapFormatType()));
226 if (IsFormatAvailable(GetWebKitSmartPasteFormatType(), buffer))
227 types->push_back(UTF8ToUTF16(GetWebKitSmartPasteFormatType()));
228 *contains_filenames = false;
229 }
230
231 void Clipboard::ReadText(Buffer buffer, string16* result) const {
232 *result = UTF8ToUTF16(GetClipboardData()->text());
233 }
234
235 void Clipboard::ReadAsciiText(Buffer buffer, std::string* result) const {
236 *result = GetClipboardData()->text();
237 }
238
239 void Clipboard::ReadHTML(Buffer buffer, string16* markup, std::string* src_url,
240 uint32* fragment_start, uint32* fragment_end) const {
241 markup->clear();
242 if (src_url)
243 src_url->clear();
244 *fragment_start = 0;
245 *fragment_end = 0;
246
247 *markup = UTF8ToUTF16(GetClipboardData()->markup_data());
248 *src_url = GetClipboardData()->url();
249
250 *fragment_start = 0;
251 DCHECK(markup->length() <= kuint32max);
252 *fragment_end = static_cast<uint32>(markup->length());
253
254 }
255
256 SkBitmap Clipboard::ReadImage(Buffer buffer) const {
257 const gfx::Size size = GetClipboardData()->bitmap_size();
258 uint8_t* bitmap = GetClipboardData()->bitmap_data();
259 SkBitmap image;
260 image.setConfig(SkBitmap::kARGB_8888_Config, size.width(), size.height(), 0);
261 image.allocPixels();
262 image.eraseARGB(0, 0, 0, 0);
263 int byte_counter = 0;
264 for (int i = 0; i < size.height(); ++i) {
265 for (int j = 0; j < size.width(); ++j) {
266 uint32* pixel = image.getAddr32(j, i);
267 // We store the pixels in RGBA format. Create one ARGB pixel from four
268 // RGBA values.
oshima 2011/10/24 23:31:26 BGRA
varunjain 2011/10/25 21:04:40 Removed conversion.
269 *pixel = (bitmap[byte_counter] << 16) + /* R */
270 (bitmap[byte_counter + 1] << 8) + /* G */
271 (bitmap[byte_counter + 2] << 0) + /* B */
272 (bitmap[byte_counter + 3] << 24); /* A */
273 byte_counter += 4;
oshima 2011/10/21 21:32:43 can we use gfx::ConvertBetweenBGRAandRGBA?
varunjain 2011/10/24 05:51:53 I believe that function is not publicly visible. B
oshima 2011/10/24 23:31:26 Actually, why we're converting back and forth? Can
varunjain 2011/10/25 21:04:40 Removed conversion.
274 }
275 }
276 return image;
277 }
278
279 void Clipboard::ReadBookmark(string16* title, std::string* url) const {
280 *title = UTF8ToUTF16(GetClipboardData()->bookmark_title());
281 *url = GetClipboardData()->bookmark_url();
282 }
283
284 void Clipboard::ReadFile(FilePath* file) const {
285 NOTIMPLEMENTED();
286 }
287
288 void Clipboard::ReadFiles(std::vector<FilePath>* files) const {
289 NOTIMPLEMENTED();
290 }
291
292 void Clipboard::ReadData(const std::string& format, std::string* result) {
293 result->clear();
294 ClipboardData* data = GetClipboardData();
295 if (data->custom_data_format() == format)
296 *result = std::string(data->custom_data_data(), data->custom_data_len());
297 }
298
299 uint64 Clipboard::GetSequenceNumber() {
300 NOTIMPLEMENTED();
301 return 0;
302 }
303
304 void Clipboard::WriteText(const char* text_data, size_t text_len) {
305 GetClipboardData()->set_text(std::string(text_data, text_len));
306 }
307
308 void Clipboard::WriteHTML(const char* markup_data,
309 size_t markup_len,
310 const char* url_data,
311 size_t url_len) {
312 GetClipboardData()->set_markup_data(std::string(markup_data, markup_len));
313 GetClipboardData()->set_url(std::string(url_data, url_len));
314 }
315
316 void Clipboard::WriteBookmark(const char* title_data,
317 size_t title_len,
318 const char* url_data,
319 size_t url_len) {
320 GetClipboardData()->set_bookmark_title(std::string(title_data, title_len));
321 GetClipboardData()->set_bookmark_url(std::string(url_data, url_len));
322 }
323
324 void Clipboard::WriteWebSmartPaste() {
325 GetClipboardData()->set_web_smart_paste(true);
326 }
327
328 void Clipboard::WriteBitmap(const char* pixel_data, const char* size_data) {
329 const gfx::Size* size = reinterpret_cast<const gfx::Size*>(size_data);
330
331 // Create an RGBA bitmap and store it in ClipboardData
332 GetClipboardData()->set_bitmap_data(
333 gfx::BGRAToRGBA(reinterpret_cast<const uint8_t*>(pixel_data),
334 size->width(), size->height(), 0));
335 GetClipboardData()->set_bitmap_size(*size);
336 }
337
338 void Clipboard::WriteData(const char* format_name, size_t format_len,
339 const char* data_data, size_t data_len) {
340 GetClipboardData()->SetData(std::string(format_name, format_len),
341 data_data, data_len);
342 }
343
344 // static
345 Clipboard::FormatType Clipboard::GetPlainTextFormatType() {
346 return std::string(kMimeTypeText);
347 }
348
349 // static
350 Clipboard::FormatType Clipboard::GetPlainTextWFormatType() {
351 return GetPlainTextFormatType();
352 }
353
354 // static
355 Clipboard::FormatType Clipboard::GetHtmlFormatType() {
356 return std::string(kMimeTypeHTML);
357 }
358
359 // static
360 Clipboard::FormatType Clipboard::GetBitmapFormatType() {
361 return std::string(kMimeTypeBitmap);
362 }
363
364 // static
365 Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() {
366 return std::string(kMimeTypeWebkitSmartPaste);
367 }
368
369 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/clipboard/clipboard_aura.cc ('k') | ui/base/clipboard/clipboard_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698