Chromium Code Reviews| Index: ui/base/clipboard/clipboard_aura.cc |
| diff --git a/ui/base/clipboard/clipboard_aura.cc b/ui/base/clipboard/clipboard_aura.cc |
| index 21c120228c7bad24810243d3fbd6e8dd27bfd1a2..0e0b4d49dabc24375d45382f0298b4c9ee5e189a 100644 |
| --- a/ui/base/clipboard/clipboard_aura.cc |
| +++ b/ui/base/clipboard/clipboard_aura.cc |
| @@ -7,6 +7,8 @@ |
| #include "base/logging.h" |
| #include "base/utf_string_conversions.h" |
| #include "third_party/skia/include/core/SkBitmap.h" |
| +#include "ui/gfx/linux_util.h" |
| +#include "ui/gfx/size.h" |
| namespace ui { |
| @@ -14,11 +16,22 @@ namespace { |
| const char kMimeTypeBitmap[] = "image/bmp"; |
| const char kMimeTypeWebkitSmartPaste[] = "chromium/x-webkit-paste"; |
| -// A mimimum clipboard implementation for simple text cut&paste. |
| +// ClipboardData contains data copied to the Clipboard for a variety of formats. |
| +// It mostly just provides APIs to cleanly access and manipulate this data. |
| class ClipboardData { |
| public: |
| - ClipboardData() {} |
| - virtual ~ClipboardData() {} |
| + ClipboardData() : bitmap_data_(NULL), |
| + data_data_(NULL), |
| + data_len_(0), |
| + web_smart_paste_(false) { } |
| + |
| + virtual ~ClipboardData() { |
| + // Cleanup bitmap_data_ |
| + CleanupBitmapData(); |
| + |
| + // Cleanup custom format data |
| + CleanupDataData(); |
| + } |
| const std::string& text() const { return utf8_text_; } |
| @@ -26,9 +39,101 @@ class ClipboardData { |
| utf8_text_ = text; |
| } |
| + const std::string& markup_data() const { return markup_data_; } |
| + |
| + void set_markup_data(const std::string& markup_data) { |
| + markup_data_ = markup_data; |
| + } |
| + |
| + const std::string& url() const { return url_; } |
| + |
| + void set_url(const std::string& url) { |
| + url_ = url; |
| + } |
| + |
| + const std::string& bookmark_title() const { return bookmark_title_; } |
| + |
| + void set_bookmark_title(const std::string& bookmark_title) { |
| + bookmark_title_ = bookmark_title; |
| + } |
| + |
| + const std::string& bookmark_url() const { return bookmark_url_; } |
| + |
| + void set_bookmark_url(const std::string& bookmark_url) { |
| + bookmark_url_ = bookmark_url; |
| + } |
| + |
| + uint8_t* bitmap_data() const { return bitmap_data_; } |
| + |
| + void set_bitmap_data(uint8_t* bitmap_data) { |
| + CleanupBitmapData(); |
| + bitmap_data_ = bitmap_data; |
| + } |
| + |
| + const gfx::Size bitmap_size() const { return bitmap_size_; } |
| + |
| + void set_bitmap_size(const gfx::Size bitmap_size) { |
| + bitmap_size_ = bitmap_size; |
| + } |
| + |
| + const std::string& data_format() const { return data_format_; } |
| + char* data_data() const { return data_data_; } |
| + size_t data_len() const { return data_len_; } |
| + |
| + void SetData(const std::string& data_format, |
| + const char* data_data, |
| + size_t data_len) { |
| + CleanupDataData(); |
| + data_len_ = data_len; |
| + if (data_len_ == 0) |
| + return; |
| + data_data_ = new char[data_len_]; |
| + memcpy(data_data_, data_data, data_len_); |
| + data_format_ = data_format; |
| + } |
| + |
| + bool web_smart_paste() const { return web_smart_paste_; } |
| + |
| + void set_web_smart_paste(bool web_smart_paste) { |
| + web_smart_paste_ = web_smart_paste; |
| + } |
| + |
| private: |
| + void CleanupBitmapData() { |
| + if (bitmap_data_) |
| + free(bitmap_data_); // bitmap_data_ is allocated using malloc. |
| + bitmap_data_ = NULL; |
| + } |
| + |
| + void CleanupDataData() { |
| + if (data_data_) |
| + delete[] data_data_; |
| + data_data_ = NULL; |
| + } |
| + |
| + // Plain text |
| std::string utf8_text_; |
| + // HTML |
| + std::string markup_data_; |
| + std::string url_; |
| + |
| + // Bookmarks |
| + std::string bookmark_title_; |
| + std::string bookmark_url_; |
| + |
| + // Bitmap images |
| + uint8_t* bitmap_data_; |
| + gfx::Size bitmap_size_; |
| + |
| + // Data with custom format |
| + std::string data_format_; |
| + char* data_data_; |
| + 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.
|
| + |
| + // WebKit smart paste data |
| + bool web_smart_paste_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(ClipboardData); |
| }; |
| @@ -40,16 +145,40 @@ ClipboardData* GetClipboardData() { |
| return data; |
| } |
| +void DeleteClipboardData() { |
| + if (data) |
| + delete data; |
| + data = NULL; |
| +} |
| + |
| +void ClearClipboardData() { |
| + DCHECK(data); |
| + data->set_text(std::string()); |
| + data->set_markup_data(std::string()); |
| + data->set_url(std::string()); |
| +} |
| + |
| } // namespace |
| +// TODO(varunjain): Complete implementation: |
| +// 1. Handle different types of BUFFERs. |
| +// 2. Do we need to care about concurrency here? Can there be multiple instances |
| +// of ui::Clipboard? Ask oshima. |
| +// 3. Implement File types. |
| +// 4. Handle conversion between types. |
| + |
| Clipboard::Clipboard() { |
| - NOTIMPLEMENTED(); |
| + // Make sure clipboard is created. |
| + GetClipboardData(); |
| } |
| Clipboard::~Clipboard() { |
| } |
| void Clipboard::WriteObjects(const ObjectMap& objects) { |
| + // 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.
|
| + // everything and start fresh. |
| + DeleteClipboardData(); |
| for (ObjectMap::const_iterator iter = objects.begin(); |
| iter != objects.end(); ++iter) { |
| DispatchObject(static_cast<ObjectType>(iter->first), iter->second); |
| @@ -63,24 +192,47 @@ void Clipboard::WriteObjects(const ObjectMap& objects, |
| } |
| void Clipboard::DidWriteURL(const std::string& utf8_text) { |
| - NOTIMPLEMENTED(); |
| + // This function seems GTK specific. We probably dont care. |
| } |
| bool Clipboard::IsFormatAvailable(const FormatType& format, |
| Buffer buffer) const { |
| - NOTIMPLEMENTED(); |
| + ClipboardData* data = GetClipboardData(); |
| + if (GetPlainTextFormatType() == format) |
| + return !data->text().empty(); |
| + else if (GetHtmlFormatType() == format) |
| + return !data->markup_data().empty() || !data->url().empty(); |
| + else if (GetBitmapFormatType() == format) |
| + return data->bitmap_data(); |
| + else if (GetWebKitSmartPasteFormatType() == format) |
| + return data->web_smart_paste(); |
| + else if (data->data_format() == format) |
| + return true; |
| return false; |
| } |
| bool Clipboard::IsFormatAvailableByString(const std::string& format, |
| Buffer buffer) const { |
| - NOTIMPLEMENTED(); |
| - return false; |
| + return IsFormatAvailable(format, buffer); |
| } |
| void Clipboard::ReadAvailableTypes(Buffer buffer, std::vector<string16>* types, |
| bool* contains_filenames) const { |
| - NOTIMPLEMENTED(); |
| + if (!types || !contains_filenames) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + |
| + types->clear(); |
| + if (IsFormatAvailable(GetPlainTextFormatType(), buffer)) |
| + types->push_back(UTF8ToUTF16(GetPlainTextFormatType())); |
| + if (IsFormatAvailable(GetHtmlFormatType(), buffer)) |
| + types->push_back(UTF8ToUTF16(GetHtmlFormatType())); |
| + if (IsFormatAvailable(GetBitmapFormatType(), buffer)) |
| + types->push_back(UTF8ToUTF16(GetBitmapFormatType())); |
| + if (IsFormatAvailable(GetWebKitSmartPasteFormatType(), buffer)) |
| + types->push_back(UTF8ToUTF16(GetWebKitSmartPasteFormatType())); |
| + *contains_filenames = false; |
| } |
| void Clipboard::ReadText(Buffer buffer, string16* result) const { |
| @@ -93,16 +245,47 @@ void Clipboard::ReadAsciiText(Buffer buffer, std::string* result) const { |
| void Clipboard::ReadHTML(Buffer buffer, string16* markup, std::string* src_url, |
| uint32* fragment_start, uint32* fragment_end) const { |
| - NOTIMPLEMENTED(); |
| + markup->clear(); |
| + if (src_url) |
| + src_url->clear(); |
| + *fragment_start = 0; |
| + *fragment_end = 0; |
| + |
| + *markup = UTF8ToUTF16(GetClipboardData()->markup_data()); |
| + *src_url = GetClipboardData()->url(); |
| + |
| + *fragment_start = 0; |
| + DCHECK(markup->length() <= kuint32max); |
| + *fragment_end = static_cast<uint32>(markup->length()); |
| + |
| } |
| SkBitmap Clipboard::ReadImage(Buffer buffer) const { |
| - NOTIMPLEMENTED(); |
| - return SkBitmap(); |
| + const gfx::Size size = GetClipboardData()->bitmap_size(); |
| + uint8_t* bitmap = GetClipboardData()->bitmap_data(); |
| + SkBitmap image; |
| + image.setConfig(SkBitmap::kARGB_8888_Config, size.width(), size.height(), 0); |
| + image.allocPixels(); |
| + image.eraseARGB(0, 0, 0, 0); |
| + int byte_counter = 0; |
| + for (int i = 0; i < size.height(); ++i) { |
| + for (int j = 0; j < size.width(); ++j) { |
| + uint32* pixel = image.getAddr32(j, i); |
| + // We store the pixels in RGBA format. Create one ARGB pixel from four |
| + // RGBA values. |
| + *pixel = (bitmap[byte_counter] << 16) + /* R */ |
| + (bitmap[byte_counter + 1] << 8) + /* G */ |
| + (bitmap[byte_counter + 2] << 0) + /* B */ |
| + (bitmap[byte_counter + 3] << 24); /* A */ |
| + byte_counter += 4; |
| + } |
| + } |
| + return image; |
| } |
| void Clipboard::ReadBookmark(string16* title, std::string* url) const { |
| - NOTIMPLEMENTED(); |
| + *title = UTF8ToUTF16(GetClipboardData()->bookmark_title()); |
| + *url = GetClipboardData()->bookmark_url(); |
| } |
| void Clipboard::ReadFile(FilePath* file) const { |
| @@ -114,7 +297,10 @@ void Clipboard::ReadFiles(std::vector<FilePath>* files) const { |
| } |
| void Clipboard::ReadData(const std::string& format, std::string* result) { |
| - NOTIMPLEMENTED(); |
| + result->clear(); |
| + ClipboardData* data = GetClipboardData(); |
| + if (data->data_format() == format) |
| + *result = std::string(data->data_data(), data->data_len()); |
| } |
| uint64 Clipboard::GetSequenceNumber() { |
| @@ -130,27 +316,36 @@ void Clipboard::WriteHTML(const char* markup_data, |
| size_t markup_len, |
| const char* url_data, |
| size_t url_len) { |
| - NOTIMPLEMENTED(); |
| + GetClipboardData()->set_markup_data(std::string(markup_data, markup_len)); |
| + GetClipboardData()->set_url(std::string(url_data, url_len)); |
| } |
| void Clipboard::WriteBookmark(const char* title_data, |
| size_t title_len, |
| const char* url_data, |
| size_t url_len) { |
| - NOTIMPLEMENTED(); |
| + GetClipboardData()->set_bookmark_title(std::string(title_data, title_len)); |
| + GetClipboardData()->set_bookmark_url(std::string(url_data, url_len)); |
| } |
| void Clipboard::WriteWebSmartPaste() { |
| - NOTIMPLEMENTED(); |
| + GetClipboardData()->set_web_smart_paste(true); |
| } |
| void Clipboard::WriteBitmap(const char* pixel_data, const char* size_data) { |
| - NOTIMPLEMENTED(); |
| + const gfx::Size* size = reinterpret_cast<const gfx::Size*>(size_data); |
| + |
| + // Create an RGBA bitmap and store it in ClipboardData |
| + GetClipboardData()->set_bitmap_data( |
| + gfx::BGRAToRGBA(reinterpret_cast<const uint8_t*>(pixel_data), |
| + size->width(), size->height(), 0)); |
| + GetClipboardData()->set_bitmap_size(*size); |
| } |
| void Clipboard::WriteData(const char* format_name, size_t format_len, |
| const char* data_data, size_t data_len) { |
| - NOTIMPLEMENTED(); |
| + GetClipboardData()->SetData(std::string(format_name, format_len), |
| + data_data, data_len); |
| } |
| // static |