| Index: ui/views/mus/os_exchange_data_provider_mus.cc
|
| diff --git a/ui/views/mus/os_exchange_data_provider_mus.cc b/ui/views/mus/os_exchange_data_provider_mus.cc
|
| index 84a35d5ff84f34db22943a08c2d0f3a6bdf29c2c..0df6ff411d5fd83f5b90afce337ceea95a44c068 100644
|
| --- a/ui/views/mus/os_exchange_data_provider_mus.cc
|
| +++ b/ui/views/mus/os_exchange_data_provider_mus.cc
|
| @@ -4,18 +4,73 @@
|
|
|
| #include "ui/views/mus/os_exchange_data_provider_mus.h"
|
|
|
| +#include "base/stl_util.h"
|
| +#include "base/strings/string_split.h"
|
| +#include "base/strings/utf_string_conversions.h"
|
| +#include "mojo/common/common_type_converters.h"
|
| +#include "net/base/filename_util.h"
|
| +#include "services/ui/public/interfaces/clipboard.mojom.h"
|
| +#include "ui/base/dragdrop/file_info.h"
|
| +#include "url/gurl.h"
|
| +
|
| namespace views {
|
|
|
| +namespace {
|
| +
|
| +std::vector<uint8_t> FromString(const std::string& str) {
|
| + return std::vector<uint8_t>(str.begin(), str.end());
|
| +}
|
| +
|
| +std::string ToString(const std::vector<uint8_t>& v) {
|
| + return std::string(v.begin(), v.end());
|
| +}
|
| +
|
| +base::string16 ToString16(const std::vector<uint8_t>& v) {
|
| + DCHECK_EQ(0u, v.size() % 2);
|
| + return base::string16(
|
| + reinterpret_cast<const base::char16*>(v.data()),
|
| + v.size() / 2);
|
| +}
|
| +
|
| +std::vector<base::StringPiece> ParseURIList(const std::vector<uint8_t>& data) {
|
| + return base::SplitStringPiece(
|
| + base::StringPiece(
|
| + reinterpret_cast<const char*>(&data.front()), data.size()),
|
| + "\n",
|
| + base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
|
| +}
|
| +
|
| +void AddString16ToVector(const base::string16& str,
|
| + std::vector<uint8_t>* bytes) {
|
| + const unsigned char* front =
|
| + reinterpret_cast<const uint8_t*>(str.data());
|
| + bytes->insert(bytes->end(), front, front + (str.size() * 2));
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| OSExchangeDataProviderMus::OSExchangeDataProviderMus() {}
|
|
|
| OSExchangeDataProviderMus::~OSExchangeDataProviderMus() {}
|
|
|
| +OSExchangeDataProviderMus::Data OSExchangeDataProviderMus::GetData() const {
|
| + return mime_data_;
|
| +}
|
| +
|
| std::unique_ptr<ui::OSExchangeData::Provider>
|
| OSExchangeDataProviderMus::Clone() const {
|
| - return std::unique_ptr<ui::OSExchangeData::Provider>();
|
| + std::unique_ptr<OSExchangeDataProviderMus> r =
|
| + base::MakeUnique<OSExchangeDataProviderMus>();
|
| + r->drag_image_ = drag_image_;
|
| + r->drag_image_offset_ = drag_image_offset_;
|
| + r->mime_data_ = mime_data_;
|
| + return base::WrapUnique<ui::OSExchangeData::Provider>(r.release());
|
| }
|
|
|
| void OSExchangeDataProviderMus::MarkOriginatedFromRenderer() {
|
| + // Currently unimplemented because ChromeOS doesn't need this.
|
| + //
|
| + // TODO(erg): Implement this when we start porting mus to other platforms.
|
| }
|
|
|
| bool OSExchangeDataProviderMus::DidOriginateFromRenderer() const {
|
| @@ -23,66 +78,170 @@ bool OSExchangeDataProviderMus::DidOriginateFromRenderer() const {
|
| }
|
|
|
| void OSExchangeDataProviderMus::SetString(const base::string16& data) {
|
| + if (HasString())
|
| + return;
|
| +
|
| + mime_data_[ui::mojom::kMimeTypeText] = FromString(base::UTF16ToUTF8(data));
|
| }
|
|
|
| void OSExchangeDataProviderMus::SetURL(const GURL& url,
|
| const base::string16& title) {
|
| + base::string16 spec = base::UTF8ToUTF16(url.spec());
|
| + std::vector<unsigned char> data;
|
| + AddString16ToVector(spec, &data);
|
| + AddString16ToVector(base::ASCIIToUTF16("\n"), &data);
|
| + AddString16ToVector(title, &data);
|
| + mime_data_[ui::mojom::kMimeTypeMozillaURL] = std::move(data);
|
| +
|
| + if (!base::ContainsKey(mime_data_, ui::mojom::kMimeTypeText))
|
| + mime_data_[ui::mojom::kMimeTypeText] = FromString(url.spec());
|
| }
|
|
|
| void OSExchangeDataProviderMus::SetFilename(const base::FilePath& path) {
|
| + std::vector<ui::FileInfo> data;
|
| + data.push_back(ui::FileInfo(path, base::FilePath()));
|
| + SetFilenames(data);
|
| }
|
|
|
| void OSExchangeDataProviderMus::SetFilenames(
|
| const std::vector<ui::FileInfo>& file_names) {
|
| + std::vector<std::string> paths;
|
| + for (std::vector<ui::FileInfo>::const_iterator it = file_names.begin();
|
| + it != file_names.end();
|
| + ++it) {
|
| + std::string url_spec = net::FilePathToFileURL(it->path).spec();
|
| + if (!url_spec.empty())
|
| + paths.push_back(url_spec);
|
| + }
|
| +
|
| + std::string joined_data = base::JoinString(paths, "\n");
|
| + mime_data_[ui::mojom::kMimeTypeURIList] = FromString(joined_data);
|
| }
|
|
|
| void OSExchangeDataProviderMus::SetPickledData(
|
| const ui::Clipboard::FormatType& format,
|
| - const base::Pickle& data) {
|
| + const base::Pickle& pickle) {
|
| + const unsigned char* bytes =
|
| + reinterpret_cast<const unsigned char*>(pickle.data());
|
| +
|
| + mime_data_[format.Serialize()] = mojo::Array<uint8_t>(
|
| + std::vector<uint8_t>(bytes, bytes + pickle.size()));
|
| }
|
|
|
| bool OSExchangeDataProviderMus::GetString(base::string16* data) const {
|
| - return false;
|
| + auto it = mime_data_.find(ui::mojom::kMimeTypeText);
|
| + if (it != mime_data_.end())
|
| + *data = base::UTF8ToUTF16(ToString(it->second));
|
| + return it != mime_data_.end();
|
| }
|
|
|
| bool OSExchangeDataProviderMus::GetURLAndTitle(
|
| ui::OSExchangeData::FilenameToURLPolicy policy,
|
| GURL* url,
|
| base::string16* title) const {
|
| - return false;
|
| + auto it = mime_data_.find(ui::mojom::kMimeTypeMozillaURL);
|
| + if (it == mime_data_.end()) {
|
| + title->clear();
|
| + return GetPlainTextURL(url) ||
|
| + (policy == ui::OSExchangeData::CONVERT_FILENAMES && GetFileURL(url));
|
| + }
|
| +
|
| + base::string16 data = ToString16(it->second);
|
| + base::string16::size_type newline = data.find('\n');
|
| + if (newline == std::string::npos)
|
| + return false;
|
| +
|
| + GURL unparsed_url(data.substr(0, newline));
|
| + if (!unparsed_url.is_valid())
|
| + return false;
|
| +
|
| + *url = unparsed_url;
|
| + *title = data.substr(newline + 1);
|
| + return true;
|
| }
|
|
|
| bool OSExchangeDataProviderMus::GetFilename(base::FilePath* path) const {
|
| + std::vector<ui::FileInfo> filenames;
|
| + if (GetFilenames(&filenames)) {
|
| + *path = filenames.front().path;
|
| + return true;
|
| + }
|
| +
|
| return false;
|
| }
|
|
|
| bool OSExchangeDataProviderMus::GetFilenames(
|
| std::vector<ui::FileInfo>* file_names) const {
|
| - return false;
|
| + auto it = mime_data_.find(ui::mojom::kMimeTypeURIList);
|
| + if (it == mime_data_.end())
|
| + return false;
|
| +
|
| + file_names->clear();
|
| + for (const base::StringPiece& piece : ParseURIList(it->second)) {
|
| + GURL url(piece);
|
| + base::FilePath file_path;
|
| + if (url.SchemeIsFile() && net::FileURLToFilePath(url, &file_path))
|
| + file_names->push_back(ui::FileInfo(file_path, base::FilePath()));
|
| + }
|
| +
|
| + return true;
|
| }
|
|
|
| bool OSExchangeDataProviderMus::GetPickledData(
|
| const ui::Clipboard::FormatType& format,
|
| base::Pickle* data) const {
|
| - return false;
|
| + auto it = mime_data_.find(format.Serialize());
|
| + if (it == mime_data_.end())
|
| + return false;
|
| +
|
| + // Note that the pickle object on the right hand side of the assignment
|
| + // only refers to the bytes in |data|. The assignment copies the data.
|
| + *data = base::Pickle(reinterpret_cast<const char*>(it->second.data()),
|
| + static_cast<int>(it->second.size()));
|
| + return true;
|
| }
|
|
|
| bool OSExchangeDataProviderMus::HasString() const {
|
| - return false;
|
| + return base::ContainsKey(mime_data_, ui::mojom::kMimeTypeText);
|
| }
|
|
|
| bool OSExchangeDataProviderMus::HasURL(
|
| ui::OSExchangeData::FilenameToURLPolicy policy) const {
|
| + if (base::ContainsKey(mime_data_, ui::mojom::kMimeTypeMozillaURL))
|
| + return true;
|
| +
|
| + auto it = mime_data_.find(ui::mojom::kMimeTypeURIList);
|
| + if (it == mime_data_.end())
|
| + return false;
|
| +
|
| + for (const base::StringPiece& piece : ParseURIList(it->second)) {
|
| + if (!GURL(piece).SchemeIsFile() ||
|
| + policy == ui::OSExchangeData::CONVERT_FILENAMES) {
|
| + return true;
|
| + }
|
| + }
|
| +
|
| return false;
|
| }
|
|
|
| bool OSExchangeDataProviderMus::HasFile() const {
|
| + auto it = mime_data_.find(ui::mojom::kMimeTypeURIList);
|
| + if (it == mime_data_.end())
|
| + return false;
|
| +
|
| + for (const base::StringPiece& piece : ParseURIList(it->second)) {
|
| + GURL url(piece);
|
| + base::FilePath file_path;
|
| + if (url.SchemeIsFile() && net::FileURLToFilePath(url, &file_path))
|
| + return true;
|
| + }
|
| +
|
| return false;
|
| }
|
|
|
| bool OSExchangeDataProviderMus::HasCustomFormat(
|
| const ui::Clipboard::FormatType& format) const {
|
| - return false;
|
| + return base::ContainsKey(mime_data_, format.Serialize());
|
| }
|
|
|
| // These methods were added in an ad-hoc way to different operating
|
| @@ -114,16 +273,46 @@ void OSExchangeDataProviderMus::SetDownloadFileInfo(
|
| #if defined(USE_AURA)
|
| void OSExchangeDataProviderMus::SetHtml(const base::string16& html,
|
| const GURL& base_url) {
|
| -
|
| + std::vector<unsigned char> bytes;
|
| + // Manually jam a UTF16 BOM into bytes because otherwise, other programs will
|
| + // assume UTF-8.
|
| + bytes.push_back(0xFF);
|
| + bytes.push_back(0xFE);
|
| + AddString16ToVector(html, &bytes);
|
| + mime_data_[ui::mojom::kMimeTypeHTML] = bytes;
|
| }
|
|
|
| bool OSExchangeDataProviderMus::GetHtml(base::string16* html,
|
| GURL* base_url) const {
|
| - return false;
|
| + auto it = mime_data_.find(ui::mojom::kMimeTypeHTML);
|
| + if (it == mime_data_.end())
|
| + return false;
|
| +
|
| + const unsigned char* data = it->second.data();
|
| + size_t size = it->second.size();
|
| + base::string16 markup;
|
| +
|
| + // If the data starts with 0xFEFF, i.e., Byte Order Mark, assume it is
|
| + // UTF-16, otherwise assume UTF-8.
|
| + if (size >= 2 &&
|
| + reinterpret_cast<const uint16_t*>(data)[0] == 0xFEFF) {
|
| + markup.assign(reinterpret_cast<const base::char16*>(data) + 1,
|
| + (size / 2) - 1);
|
| + } else {
|
| + base::UTF8ToUTF16(reinterpret_cast<const char*>(data), size, &markup);
|
| + }
|
| +
|
| + // If there is a terminating NULL, drop it.
|
| + if (!markup.empty() && markup.at(markup.length() - 1) == '\0')
|
| + markup.resize(markup.length() - 1);
|
| +
|
| + *html = markup;
|
| + *base_url = GURL();
|
| + return true;
|
| }
|
|
|
| bool OSExchangeDataProviderMus::HasHtml() const {
|
| - return false;
|
| + return base::ContainsKey(mime_data_, ui::mojom::kMimeTypeHTML);
|
| }
|
| #endif
|
|
|
| @@ -144,4 +333,32 @@ const gfx::Vector2d& OSExchangeDataProviderMus::GetDragImageOffset() const {
|
| }
|
| #endif
|
|
|
| +bool OSExchangeDataProviderMus::GetFileURL(GURL* url) const {
|
| + base::FilePath file_path;
|
| + if (!GetFilename(&file_path))
|
| + return false;
|
| +
|
| + GURL test_url = net::FilePathToFileURL(file_path);
|
| + if (!test_url.is_valid())
|
| + return false;
|
| +
|
| + if (url)
|
| + *url = test_url;
|
| + return true;
|
| +}
|
| +
|
| +bool OSExchangeDataProviderMus::GetPlainTextURL(GURL* url) const {
|
| + base::string16 str;
|
| + if (!GetString(&str))
|
| + return false;
|
| +
|
| + GURL test_url(str);
|
| + if (!test_url.is_valid())
|
| + return false;
|
| +
|
| + if (url)
|
| + *url = test_url;
|
| + return true;
|
| +}
|
| +
|
| } // namespace views
|
|
|