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

Side by Side Diff: ui/base/clipboard/clipboard_aura.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: some changes and added more tests 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 | « no previous file | 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
sadrul 2011/10/21 19:58:07 A general note: I think this clipboard_aura is use
varunjain 2011/10/21 21:03:46 Done.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/base/clipboard/clipboard.h" 5 #include "ui/base/clipboard/clipboard.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/utf_string_conversions.h" 8 #include "base/utf_string_conversions.h"
9 #include "third_party/skia/include/core/SkBitmap.h" 9 #include "third_party/skia/include/core/SkBitmap.h"
10 #include "ui/gfx/linux_util.h"
11 #include "ui/gfx/size.h"
10 12
11 namespace ui { 13 namespace ui {
12 14
13 namespace { 15 namespace {
14 const char kMimeTypeBitmap[] = "image/bmp"; 16 const char kMimeTypeBitmap[] = "image/bmp";
15 const char kMimeTypeWebkitSmartPaste[] = "chromium/x-webkit-paste"; 17 const char kMimeTypeWebkitSmartPaste[] = "chromium/x-webkit-paste";
16 18
17 // A mimimum clipboard implementation for simple text cut&paste. 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.
18 class ClipboardData { 21 class ClipboardData {
19 public: 22 public:
20 ClipboardData() {} 23 ClipboardData() : bitmap_data_(NULL),
21 virtual ~ClipboardData() {} 24 data_data_(NULL),
25 data_len_(0),
26 web_smart_paste_(false) { }
27
28 virtual ~ClipboardData() {
29 // Cleanup bitmap_data_
30 CleanupBitmapData();
31
32 // Cleanup custom format data
33 CleanupDataData();
34 }
22 35
23 const std::string& text() const { return utf8_text_; } 36 const std::string& text() const { return utf8_text_; }
24 37
25 void set_text(const std::string& text) { 38 void set_text(const std::string& text) {
26 utf8_text_ = text; 39 utf8_text_ = text;
27 } 40 }
28 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& data_format() const { return data_format_; }
80 char* data_data() const { return data_data_; }
81 size_t data_len() const { return data_len_; }
82
83 void SetData(const std::string& data_format,
84 const char* data_data,
85 size_t data_len) {
86 CleanupDataData();
87 data_len_ = data_len;
88 if (data_len_ == 0)
89 return;
90 data_data_ = new char[data_len_];
91 memcpy(data_data_, data_data, data_len_);
92 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
29 private: 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 (data_data_)
110 delete[] data_data_;
111 data_data_ = NULL;
112 }
113
114 // Plain text
30 std::string utf8_text_; 115 std::string utf8_text_;
31 116
117 // HTML
118 std::string markup_data_;
119 std::string url_;
120
121 // Bookmarks
122 std::string bookmark_title_;
123 std::string bookmark_url_;
124
125 // Bitmap images
126 uint8_t* bitmap_data_;
127 gfx::Size bitmap_size_;
128
129 // Data with custom format
130 std::string data_format_;
131 char* data_data_;
132 size_t data_len_;
sadrul 2011/10/21 19:58:07 Call these custom_.._? (data_data_ sounds weird)
varunjain 2011/10/21 21:03:46 Done.
133
134 // WebKit smart paste data
135 bool web_smart_paste_;
136
32 DISALLOW_COPY_AND_ASSIGN(ClipboardData); 137 DISALLOW_COPY_AND_ASSIGN(ClipboardData);
33 }; 138 };
34 139
35 ClipboardData* data = NULL; 140 ClipboardData* data = NULL;
sadrul 2011/10/21 19:58:07 Perhaps it's a good time to rename this. 'data' ca
varunjain 2011/10/21 21:03:46 Done.
36 141
37 ClipboardData* GetClipboardData() { 142 ClipboardData* GetClipboardData() {
38 if (!data) 143 if (!data)
39 data = new ClipboardData(); 144 data = new ClipboardData();
40 return data; 145 return data;
41 } 146 }
42 147
148 void DeleteClipboardData() {
149 if (data)
150 delete data;
151 data = NULL;
152 }
153
154 void ClearClipboardData() {
155 DCHECK(data);
156 data->set_text(std::string());
157 data->set_markup_data(std::string());
158 data->set_url(std::string());
159 }
160
43 } // namespace 161 } // namespace
44 162
163 // TODO(varunjain): Complete implementation:
164 // 1. Handle different types of BUFFERs.
165 // 2. Do we need to care about concurrency here? Can there be multiple instances
166 // of ui::Clipboard? Ask oshima.
167 // 3. Implement File types.
168 // 4. Handle conversion between types.
169
45 Clipboard::Clipboard() { 170 Clipboard::Clipboard() {
46 NOTIMPLEMENTED(); 171 // Make sure clipboard is created.
172 GetClipboardData();
47 } 173 }
48 174
49 Clipboard::~Clipboard() { 175 Clipboard::~Clipboard() {
50 } 176 }
51 177
52 void Clipboard::WriteObjects(const ObjectMap& objects) { 178 void Clipboard::WriteObjects(const ObjectMap& objects) {
179 // We need to over write previous data. Probably best to just delete
sadrul 2011/10/21 19:58:07 overwrite
varunjain 2011/10/21 21:03:46 Done.
180 // everything and start fresh.
181 DeleteClipboardData();
53 for (ObjectMap::const_iterator iter = objects.begin(); 182 for (ObjectMap::const_iterator iter = objects.begin();
54 iter != objects.end(); ++iter) { 183 iter != objects.end(); ++iter) {
55 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); 184 DispatchObject(static_cast<ObjectType>(iter->first), iter->second);
56 } 185 }
57 } 186 }
58 187
59 188
60 void Clipboard::WriteObjects(const ObjectMap& objects, 189 void Clipboard::WriteObjects(const ObjectMap& objects,
61 base::ProcessHandle process) { 190 base::ProcessHandle process) {
62 NOTIMPLEMENTED(); 191 NOTIMPLEMENTED();
63 } 192 }
64 193
65 void Clipboard::DidWriteURL(const std::string& utf8_text) { 194 void Clipboard::DidWriteURL(const std::string& utf8_text) {
66 NOTIMPLEMENTED(); 195 // This function seems GTK specific. We probably dont care.
67 } 196 }
68 197
69 bool Clipboard::IsFormatAvailable(const FormatType& format, 198 bool Clipboard::IsFormatAvailable(const FormatType& format,
70 Buffer buffer) const { 199 Buffer buffer) const {
71 NOTIMPLEMENTED(); 200 ClipboardData* data = GetClipboardData();
201 if (GetPlainTextFormatType() == format)
202 return !data->text().empty();
203 else if (GetHtmlFormatType() == format)
204 return !data->markup_data().empty() || !data->url().empty();
205 else if (GetBitmapFormatType() == format)
206 return data->bitmap_data();
207 else if (GetWebKitSmartPasteFormatType() == format)
208 return data->web_smart_paste();
209 else if (data->data_format() == format)
210 return true;
72 return false; 211 return false;
73 } 212 }
74 213
75 bool Clipboard::IsFormatAvailableByString(const std::string& format, 214 bool Clipboard::IsFormatAvailableByString(const std::string& format,
76 Buffer buffer) const { 215 Buffer buffer) const {
77 NOTIMPLEMENTED(); 216 return IsFormatAvailable(format, buffer);
78 return false;
79 } 217 }
80 218
81 void Clipboard::ReadAvailableTypes(Buffer buffer, std::vector<string16>* types, 219 void Clipboard::ReadAvailableTypes(Buffer buffer, std::vector<string16>* types,
82 bool* contains_filenames) const { 220 bool* contains_filenames) const {
83 NOTIMPLEMENTED(); 221 if (!types || !contains_filenames) {
222 NOTREACHED();
223 return;
224 }
225
226 types->clear();
227 if (IsFormatAvailable(GetPlainTextFormatType(), buffer))
228 types->push_back(UTF8ToUTF16(GetPlainTextFormatType()));
229 if (IsFormatAvailable(GetHtmlFormatType(), buffer))
230 types->push_back(UTF8ToUTF16(GetHtmlFormatType()));
231 if (IsFormatAvailable(GetBitmapFormatType(), buffer))
232 types->push_back(UTF8ToUTF16(GetBitmapFormatType()));
233 if (IsFormatAvailable(GetWebKitSmartPasteFormatType(), buffer))
234 types->push_back(UTF8ToUTF16(GetWebKitSmartPasteFormatType()));
235 *contains_filenames = false;
84 } 236 }
85 237
86 void Clipboard::ReadText(Buffer buffer, string16* result) const { 238 void Clipboard::ReadText(Buffer buffer, string16* result) const {
87 *result = UTF8ToUTF16(GetClipboardData()->text()); 239 *result = UTF8ToUTF16(GetClipboardData()->text());
88 } 240 }
89 241
90 void Clipboard::ReadAsciiText(Buffer buffer, std::string* result) const { 242 void Clipboard::ReadAsciiText(Buffer buffer, std::string* result) const {
91 *result = GetClipboardData()->text(); 243 *result = GetClipboardData()->text();
92 } 244 }
93 245
94 void Clipboard::ReadHTML(Buffer buffer, string16* markup, std::string* src_url, 246 void Clipboard::ReadHTML(Buffer buffer, string16* markup, std::string* src_url,
95 uint32* fragment_start, uint32* fragment_end) const { 247 uint32* fragment_start, uint32* fragment_end) const {
96 NOTIMPLEMENTED(); 248 markup->clear();
249 if (src_url)
250 src_url->clear();
251 *fragment_start = 0;
252 *fragment_end = 0;
253
254 *markup = UTF8ToUTF16(GetClipboardData()->markup_data());
255 *src_url = GetClipboardData()->url();
256
257 *fragment_start = 0;
258 DCHECK(markup->length() <= kuint32max);
259 *fragment_end = static_cast<uint32>(markup->length());
260
97 } 261 }
98 262
99 SkBitmap Clipboard::ReadImage(Buffer buffer) const { 263 SkBitmap Clipboard::ReadImage(Buffer buffer) const {
100 NOTIMPLEMENTED(); 264 const gfx::Size size = GetClipboardData()->bitmap_size();
101 return SkBitmap(); 265 uint8_t* bitmap = GetClipboardData()->bitmap_data();
266 SkBitmap image;
267 image.setConfig(SkBitmap::kARGB_8888_Config, size.width(), size.height(), 0);
268 image.allocPixels();
269 image.eraseARGB(0, 0, 0, 0);
270 int byte_counter = 0;
271 for (int i = 0; i < size.height(); ++i) {
272 for (int j = 0; j < size.width(); ++j) {
273 uint32* pixel = image.getAddr32(j, i);
274 // We store the pixels in RGBA format. Create one ARGB pixel from four
275 // RGBA values.
276 *pixel = (bitmap[byte_counter] << 16) + /* R */
277 (bitmap[byte_counter + 1] << 8) + /* G */
278 (bitmap[byte_counter + 2] << 0) + /* B */
279 (bitmap[byte_counter + 3] << 24); /* A */
280 byte_counter += 4;
281 }
282 }
283 return image;
102 } 284 }
103 285
104 void Clipboard::ReadBookmark(string16* title, std::string* url) const { 286 void Clipboard::ReadBookmark(string16* title, std::string* url) const {
105 NOTIMPLEMENTED(); 287 *title = UTF8ToUTF16(GetClipboardData()->bookmark_title());
288 *url = GetClipboardData()->bookmark_url();
106 } 289 }
107 290
108 void Clipboard::ReadFile(FilePath* file) const { 291 void Clipboard::ReadFile(FilePath* file) const {
109 NOTIMPLEMENTED(); 292 NOTIMPLEMENTED();
110 } 293 }
111 294
112 void Clipboard::ReadFiles(std::vector<FilePath>* files) const { 295 void Clipboard::ReadFiles(std::vector<FilePath>* files) const {
113 NOTIMPLEMENTED(); 296 NOTIMPLEMENTED();
114 } 297 }
115 298
116 void Clipboard::ReadData(const std::string& format, std::string* result) { 299 void Clipboard::ReadData(const std::string& format, std::string* result) {
117 NOTIMPLEMENTED(); 300 result->clear();
301 ClipboardData* data = GetClipboardData();
302 if (data->data_format() == format)
303 *result = std::string(data->data_data(), data->data_len());
118 } 304 }
119 305
120 uint64 Clipboard::GetSequenceNumber() { 306 uint64 Clipboard::GetSequenceNumber() {
121 NOTIMPLEMENTED(); 307 NOTIMPLEMENTED();
122 return 0; 308 return 0;
123 } 309 }
124 310
125 void Clipboard::WriteText(const char* text_data, size_t text_len) { 311 void Clipboard::WriteText(const char* text_data, size_t text_len) {
126 GetClipboardData()->set_text(std::string(text_data, text_len)); 312 GetClipboardData()->set_text(std::string(text_data, text_len));
127 } 313 }
128 314
129 void Clipboard::WriteHTML(const char* markup_data, 315 void Clipboard::WriteHTML(const char* markup_data,
130 size_t markup_len, 316 size_t markup_len,
131 const char* url_data, 317 const char* url_data,
132 size_t url_len) { 318 size_t url_len) {
133 NOTIMPLEMENTED(); 319 GetClipboardData()->set_markup_data(std::string(markup_data, markup_len));
320 GetClipboardData()->set_url(std::string(url_data, url_len));
134 } 321 }
135 322
136 void Clipboard::WriteBookmark(const char* title_data, 323 void Clipboard::WriteBookmark(const char* title_data,
137 size_t title_len, 324 size_t title_len,
138 const char* url_data, 325 const char* url_data,
139 size_t url_len) { 326 size_t url_len) {
140 NOTIMPLEMENTED(); 327 GetClipboardData()->set_bookmark_title(std::string(title_data, title_len));
328 GetClipboardData()->set_bookmark_url(std::string(url_data, url_len));
141 } 329 }
142 330
143 void Clipboard::WriteWebSmartPaste() { 331 void Clipboard::WriteWebSmartPaste() {
144 NOTIMPLEMENTED(); 332 GetClipboardData()->set_web_smart_paste(true);
145 } 333 }
146 334
147 void Clipboard::WriteBitmap(const char* pixel_data, const char* size_data) { 335 void Clipboard::WriteBitmap(const char* pixel_data, const char* size_data) {
148 NOTIMPLEMENTED(); 336 const gfx::Size* size = reinterpret_cast<const gfx::Size*>(size_data);
337
338 // Create an RGBA bitmap and store it in ClipboardData
339 GetClipboardData()->set_bitmap_data(
340 gfx::BGRAToRGBA(reinterpret_cast<const uint8_t*>(pixel_data),
341 size->width(), size->height(), 0));
342 GetClipboardData()->set_bitmap_size(*size);
149 } 343 }
150 344
151 void Clipboard::WriteData(const char* format_name, size_t format_len, 345 void Clipboard::WriteData(const char* format_name, size_t format_len,
152 const char* data_data, size_t data_len) { 346 const char* data_data, size_t data_len) {
153 NOTIMPLEMENTED(); 347 GetClipboardData()->SetData(std::string(format_name, format_len),
348 data_data, data_len);
154 } 349 }
155 350
156 // static 351 // static
157 Clipboard::FormatType Clipboard::GetPlainTextFormatType() { 352 Clipboard::FormatType Clipboard::GetPlainTextFormatType() {
158 return std::string(kMimeTypeText); 353 return std::string(kMimeTypeText);
159 } 354 }
160 355
161 // static 356 // static
162 Clipboard::FormatType Clipboard::GetPlainTextWFormatType() { 357 Clipboard::FormatType Clipboard::GetPlainTextWFormatType() {
163 return GetPlainTextFormatType(); 358 return GetPlainTextFormatType();
164 } 359 }
165 360
166 // static 361 // static
167 Clipboard::FormatType Clipboard::GetHtmlFormatType() { 362 Clipboard::FormatType Clipboard::GetHtmlFormatType() {
168 return std::string(kMimeTypeHTML); 363 return std::string(kMimeTypeHTML);
169 } 364 }
170 365
171 // static 366 // static
172 Clipboard::FormatType Clipboard::GetBitmapFormatType() { 367 Clipboard::FormatType Clipboard::GetBitmapFormatType() {
173 return std::string(kMimeTypeBitmap); 368 return std::string(kMimeTypeBitmap);
174 } 369 }
175 370
176 // static 371 // static
177 Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() { 372 Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() {
178 return std::string(kMimeTypeWebkitSmartPaste); 373 return std::string(kMimeTypeWebkitSmartPaste);
179 } 374 }
180 375
181 } // namespace ui 376 } // namespace ui
OLDNEW
« no previous file with comments | « no previous file | ui/base/clipboard/clipboard_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698