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