Chromium Code Reviews| Index: ui/views/mus/clipboard_mus.cc |
| diff --git a/ui/views/mus/clipboard_mus.cc b/ui/views/mus/clipboard_mus.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f14cc2485a7da7846ff129352e430be4671a4341 |
| --- /dev/null |
| +++ b/ui/views/mus/clipboard_mus.cc |
| @@ -0,0 +1,321 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/views/mus/clipboard_mus.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "base/threading/thread_restrictions.h" |
| +#include "mojo/common/common_type_converters.h" |
| +#include "services/shell/public/cpp/connector.h" |
| +#include "third_party/skia/include/core/SkBitmap.h" |
| +#include "ui/base/clipboard/custom_data_helper.h" |
| +#include "ui/gfx/codec/png_codec.h" |
| + |
| +namespace views { |
| +namespace { |
| + |
| +mus::mojom::Clipboard::Type GetType(ui::ClipboardType type) { |
| + switch (type) { |
| + case ui::CLIPBOARD_TYPE_COPY_PASTE: |
| + return mus::mojom::Clipboard::Type::COPY_PASTE; |
| + case ui::CLIPBOARD_TYPE_SELECTION: |
| + return mus::mojom::Clipboard::Type::SELECTION; |
| + case ui::CLIPBOARD_TYPE_DRAG: |
| + return mus::mojom::Clipboard::Type::DRAG; |
| + default: |
|
sky
2016/05/26 23:10:27
remove default and merge last with drag so you get
|
| + NOTREACHED(); |
| + return mus::mojom::Clipboard::Type::COPY_PASTE; |
| + } |
| +} |
| + |
| +const char kInternalURL[] = "chromium/internal-url"; |
| + |
| +} // namespace |
| + |
| +ClipboardMus::ClipboardMus() {} |
| + |
| +ClipboardMus::~ClipboardMus() {} |
| + |
| +void ClipboardMus::Init(shell::Connector* connector) { |
| + connector->ConnectToInterface("mojo:mus", &clipboard_); |
| +} |
| + |
| +// TODO(erg): This isn't optimal. It would be better to move the entire |
| +// FormatType system to mime types throughout chrome, but that's a very large |
| +// change. |
| +mojo::String ClipboardMus::GetMimeTypeFor(const FormatType& format) const { |
| + if (format.Equals(GetUrlFormatType())) |
| + return mus::mojom::MIME_TYPE_URI_LIST; |
| + else if (format.Equals(GetMozUrlFormatType())) |
|
sky
2016/05/26 23:10:27
nit: no else after return.
|
| + return mus::mojom::MIME_TYPE_MOZ_URL; |
| + else if (format.Equals(GetPlainTextFormatType())) |
| + return mus::mojom::MIME_TYPE_TEXT; |
| + else if (format.Equals(GetHtmlFormatType())) |
| + return mus::mojom::MIME_TYPE_HTML; |
| + else if (format.Equals(GetRtfFormatType())) |
| + return mus::mojom::MIME_TYPE_RTF; |
| + else if (format.Equals(GetBitmapFormatType())) |
| + return mus::mojom::MIME_TYPE_PNG; |
| + else if (format.Equals(GetWebCustomDataFormatType())) |
| + return kMimeTypeWebCustomData; |
| + |
| +#if defined(OS_WIN) |
| + else if (format.Equals(GetUrlWFormatType()) || |
| + format.Equals(GetPlainTextWFormatType())) |
| + NOTREACHED(); |
| +#endif |
| + |
| + NOTREACHED(); |
| + return mojo::String(); |
| +} |
| + |
| +bool ClipboardMus::HasMimeType(const mojo::Array<mojo::String>& available_types, |
| + const std::string& type) const { |
| + return std::find(available_types.begin(), available_types.end(), type) != |
|
sky
2016/05/26 23:10:27
ContainsValue(available_types, type)
in base/stl_u
|
| + available_types.end(); |
| +} |
| + |
| +uint64_t ClipboardMus::GetSequenceNumber(ui::ClipboardType type) const { |
| + base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| + uint64_t sequence_number = 0; |
| + clipboard_->GetSequenceNumber(GetType(type), &sequence_number); |
| + return sequence_number; |
| +} |
| + |
| +bool ClipboardMus::IsFormatAvailable(const FormatType& format, |
| + ui::ClipboardType type) const { |
| + base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| + |
| + uint64_t sequence_number = 0; |
|
sky
2016/05/26 23:10:27
If you cache the types and the current sequence nu
Elliot Glaysher
2016/05/31 17:01:40
If I cache the types and the sequence number, I'd
|
| + mojo::Array<mojo::String> available_types; |
| + clipboard_->GetAvailableMimeTypes(GetType(type), &sequence_number, |
| + &available_types); |
| + |
| + mojo::String format_in_mime = GetMimeTypeFor(format); |
| + return std::find(available_types.begin(), available_types.end(), |
|
sky
2016/05/26 23:10:27
ContainsValue.
|
| + format_in_mime) != available_types.end(); |
| +} |
| + |
| +void ClipboardMus::Clear(ui::ClipboardType type) { |
| + current_clipboard_.SetToEmpty(); |
| + |
| + // Sends the data to mus server. |
| + uint64_t sequence_number = 0; |
| + mojo::Map<mojo::String, mojo::Array<uint8_t>> null_data(nullptr); |
| + base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| + clipboard_->WriteClipboardData(GetType(type), std::move(null_data), |
| + &sequence_number); |
| +} |
| + |
| +void ClipboardMus::ReadAvailableTypes(ui::ClipboardType type, |
| + std::vector<base::string16>* types, |
| + bool* contains_filenames) const { |
| + base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| + |
| + uint64_t sequence_number = 0; |
| + mojo::Array<mojo::String> available_types; |
| + clipboard_->GetAvailableMimeTypes(GetType(type), &sequence_number, |
| + &available_types); |
| + |
| + types->clear(); |
| + if (HasMimeType(available_types, mus::mojom::MIME_TYPE_TEXT)) |
| + types->push_back(base::UTF8ToUTF16(mus::mojom::MIME_TYPE_TEXT)); |
| + if (HasMimeType(available_types, mus::mojom::MIME_TYPE_HTML)) |
| + types->push_back(base::UTF8ToUTF16(mus::mojom::MIME_TYPE_HTML)); |
| + if (HasMimeType(available_types, mus::mojom::MIME_TYPE_RTF)) |
| + types->push_back(base::UTF8ToUTF16(mus::mojom::MIME_TYPE_RTF)); |
| + if (HasMimeType(available_types, mus::mojom::MIME_TYPE_PNG)) |
| + types->push_back(base::UTF8ToUTF16(mus::mojom::MIME_TYPE_PNG)); |
| + |
| + if (HasMimeType(available_types, kMimeTypeWebCustomData)) { |
| + mojo::Array<uint8_t> custom_data; |
| + if (clipboard_->ReadMimeType(GetType(type), kMimeTypeWebCustomData, |
| + &custom_data)) { |
| + ui::ReadCustomDataTypes(&custom_data.front(), custom_data.size(), types); |
| + } |
| + } |
| + |
| + *contains_filenames = false; |
| +} |
| + |
| +void ClipboardMus::ReadText(ui::ClipboardType type, |
| + base::string16* result) const { |
| + base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| + mojo::Array<uint8_t> text_data; |
| + if (clipboard_->ReadMimeType(GetType(type), |
| + mojo::String(mus::mojom::MIME_TYPE_TEXT), |
| + &text_data)) { |
| + std::string text = text_data.To<std::string>(); |
| + *result = base::UTF8ToUTF16(text); |
| + } |
| +} |
| + |
| +void ClipboardMus::ReadAsciiText(ui::ClipboardType type, |
| + std::string* result) const { |
| + base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| + mojo::Array<uint8_t> text_data; |
| + if (clipboard_->ReadMimeType(GetType(type), |
| + mojo::String(mus::mojom::MIME_TYPE_TEXT), |
| + &text_data)) { |
| + *result = text_data.To<std::string>(); |
| + } |
| +} |
| + |
| +void ClipboardMus::ReadHTML(ui::ClipboardType type, |
| + base::string16* markup, |
| + std::string* src_url, |
| + uint32_t* fragment_start, |
| + uint32_t* fragment_end) const { |
| + markup->clear(); |
| + if (src_url) |
| + src_url->clear(); |
| + *fragment_start = 0; |
| + *fragment_end = 0; |
| + |
| + base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| + mojo::Array<uint8_t> html_data; |
| + if (clipboard_->ReadMimeType(GetType(type), |
| + mojo::String(mus::mojom::MIME_TYPE_HTML), |
| + &html_data)) { |
| + *markup = html_data.To<base::string16>(); |
| + *fragment_end = static_cast<uint32_t>(markup->length()); |
| + |
| + // We only bother fetching the source url if we were the ones who wrote |
| + // this html data to the clipboard. |
| + mojo::Array<uint8_t> url_data; |
| + if (clipboard_->ReadMimeType(GetType(type), kInternalURL, &url_data)) { |
| + *src_url = url_data.To<std::string>(); |
| + } |
| + } |
| +} |
| + |
| +void ClipboardMus::ReadRTF(ui::ClipboardType type, std::string* result) const { |
| + base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| + mojo::Array<uint8_t> rtf_data; |
| + if (clipboard_->ReadMimeType( |
| + GetType(type), mojo::String(mus::mojom::MIME_TYPE_RTF), &rtf_data)) { |
| + *result = rtf_data.To<std::string>(); |
| + } |
| +} |
| + |
| +SkBitmap ClipboardMus::ReadImage(ui::ClipboardType type) const { |
| + base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| + mojo::Array<uint8_t> data; |
| + if (clipboard_->ReadMimeType( |
| + GetType(type), mojo::String(mus::mojom::MIME_TYPE_RTF), &data)) { |
| + SkBitmap bitmap; |
| + if (gfx::PNGCodec::Decode(&data.front(), data.size(), &bitmap)) |
| + return SkBitmap(bitmap); |
| + } |
| + |
| + return SkBitmap(); |
| +} |
| + |
| +void ClipboardMus::ReadCustomData(ui::ClipboardType clipboard_type, |
| + const base::string16& type, |
| + base::string16* result) const { |
| + base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| + mojo::Array<uint8_t> custom_data; |
| + if (clipboard_->ReadMimeType(GetType(clipboard_type), |
| + mojo::String(kMimeTypeWebCustomData), |
| + &custom_data)) { |
| + ui::ReadCustomDataForType(&custom_data.front(), custom_data.size(), type, |
| + result); |
| + } |
| +} |
| + |
| +void ClipboardMus::ReadBookmark(base::string16* title, std::string* url) const { |
| + // TODO(erg): This is NOTIMPLEMENTED() on all linux platforms? |
| + NOTIMPLEMENTED(); |
| +} |
| + |
| +void ClipboardMus::ReadData(const FormatType& format, |
| + std::string* result) const { |
| + base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| + mojo::Array<uint8_t> data; |
| + if (clipboard_->ReadMimeType(mus::mojom::Clipboard::Type::COPY_PASTE, |
| + GetMimeTypeFor(format), &data)) { |
| + *result = data.To<std::string>(); |
| + } |
| +} |
| + |
| +void ClipboardMus::WriteObjects(ui::ClipboardType type, |
| + const ObjectMap& objects) { |
| + current_clipboard_.SetToEmpty(); |
| + for (ObjectMap::const_iterator iter = objects.begin(); iter != objects.end(); |
| + ++iter) { |
| + DispatchObject(static_cast<ObjectType>(iter->first), iter->second); |
| + } |
| + |
| + // Sends the data to mus server. |
| + uint64_t sequence_number = 0; |
| + base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| + clipboard_->WriteClipboardData(GetType(type), std::move(current_clipboard_), |
| + &sequence_number); |
| +} |
| + |
| +void ClipboardMus::WriteText(const char* text_data, size_t text_len) { |
| + current_clipboard_.insert( |
| + mus::mojom::MIME_TYPE_TEXT, |
| + mojo::Array<uint8_t>::From(base::StringPiece(text_data, text_len))); |
| +} |
| + |
| +void ClipboardMus::WriteHTML(const char* markup_data, |
| + size_t markup_len, |
| + const char* url_data, |
| + size_t url_len) { |
| + current_clipboard_.insert( |
| + mus::mojom::MIME_TYPE_HTML, |
| + mojo::Array<uint8_t>::From(base::StringPiece(markup_data, markup_len))); |
| + if (url_len > 0) { |
| + current_clipboard_.insert( |
| + kInternalURL, |
| + mojo::Array<uint8_t>::From(base::StringPiece(url_data, url_len))); |
| + } |
| +} |
| + |
| +void ClipboardMus::WriteRTF(const char* rtf_data, size_t data_len) { |
| + current_clipboard_.insert( |
| + mus::mojom::MIME_TYPE_RTF, |
| + mojo::Array<uint8_t>::From(base::StringPiece(rtf_data, data_len))); |
| +} |
| + |
| +void ClipboardMus::WriteBookmark(const char* title_data, |
| + size_t title_len, |
| + const char* url_data, |
| + size_t url_len) { |
| + // Writes a Mozilla url (UTF16: URL, newline, title) |
| + base::string16 bookmark = |
| + base::UTF8ToUTF16(base::StringPiece(url_data, url_len)) + |
| + base::ASCIIToUTF16("\n") + |
| + base::UTF8ToUTF16(base::StringPiece(title_data, title_len)); |
| + |
| + current_clipboard_.insert(mus::mojom::MIME_TYPE_MOZ_URL, |
| + mojo::Array<uint8_t>::From(bookmark)); |
| +} |
| + |
| +void ClipboardMus::WriteWebSmartPaste() { |
| + current_clipboard_.insert("chromium/x-webkit-paste", |
| + mojo::Array<uint8_t>(nullptr)); |
| +} |
| + |
| +void ClipboardMus::WriteBitmap(const SkBitmap& bitmap) { |
| + // Encode the bitmap as a PNG for transport. |
| + std::vector<unsigned char> output; |
| + if (gfx::PNGCodec::FastEncodeBGRASkBitmap(bitmap, false, &output)) { |
| + current_clipboard_.insert(mus::mojom::MIME_TYPE_PNG, |
| + mojo::Array<uint8_t>::From(output)); |
| + } |
| +} |
| + |
| +void ClipboardMus::WriteData(const FormatType& format, |
| + const char* data_data, |
| + size_t data_len) { |
| + current_clipboard_.insert( |
| + GetMimeTypeFor(format), |
| + mojo::Array<uint8_t>::From(base::StringPiece(data_data, data_len))); |
| +} |
| + |
| +} // namespace views |