| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 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/views/mus/os_exchange_data_provider_mus.h" | 5 #include "ui/aura/mus/os_exchange_data_provider_mus.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "mojo/common/common_type_converters.h" | 16 #include "mojo/common/common_type_converters.h" |
| 17 #include "net/base/filename_util.h" | 17 #include "net/base/filename_util.h" |
| 18 #include "services/ui/public/interfaces/clipboard.mojom.h" | 18 #include "services/ui/public/interfaces/clipboard.mojom.h" |
| 19 #include "ui/base/dragdrop/file_info.h" | 19 #include "ui/base/dragdrop/file_info.h" |
| 20 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 21 | 21 |
| 22 namespace views { | 22 namespace aura { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 std::vector<uint8_t> FromString(const std::string& str) { | 26 std::vector<uint8_t> FromString(const std::string& str) { |
| 27 return std::vector<uint8_t>(str.begin(), str.end()); | 27 return std::vector<uint8_t>(str.begin(), str.end()); |
| 28 } | 28 } |
| 29 | 29 |
| 30 std::string ToString(const std::vector<uint8_t>& v) { | 30 std::string ToString(const std::vector<uint8_t>& v) { |
| 31 return std::string(v.begin(), v.end()); | 31 return std::string(v.begin(), v.end()); |
| 32 } | 32 } |
| 33 | 33 |
| 34 base::string16 ToString16(const std::vector<uint8_t>& v) { | 34 base::string16 ToString16(const std::vector<uint8_t>& v) { |
| 35 DCHECK_EQ(0u, v.size() % 2); | 35 DCHECK_EQ(0u, v.size() % 2); |
| 36 return base::string16( | 36 return base::string16(reinterpret_cast<const base::char16*>(v.data()), |
| 37 reinterpret_cast<const base::char16*>(v.data()), | 37 v.size() / 2); |
| 38 v.size() / 2); | |
| 39 } | 38 } |
| 40 | 39 |
| 41 std::vector<base::StringPiece> ParseURIList(const std::vector<uint8_t>& data) { | 40 std::vector<base::StringPiece> ParseURIList(const std::vector<uint8_t>& data) { |
| 42 return base::SplitStringPiece( | 41 return base::SplitStringPiece( |
| 43 base::StringPiece( | 42 base::StringPiece(reinterpret_cast<const char*>(&data.front()), |
| 44 reinterpret_cast<const char*>(&data.front()), data.size()), | 43 data.size()), |
| 45 "\n", | 44 "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 46 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
| 47 } | 45 } |
| 48 | 46 |
| 49 void AddString16ToVector(const base::string16& str, | 47 void AddString16ToVector(const base::string16& str, |
| 50 std::vector<uint8_t>* bytes) { | 48 std::vector<uint8_t>* bytes) { |
| 51 const unsigned char* front = | 49 const unsigned char* front = reinterpret_cast<const uint8_t*>(str.data()); |
| 52 reinterpret_cast<const uint8_t*>(str.data()); | |
| 53 bytes->insert(bytes->end(), front, front + (str.size() * 2)); | 50 bytes->insert(bytes->end(), front, front + (str.size() * 2)); |
| 54 } | 51 } |
| 55 | 52 |
| 56 } // namespace | 53 } // namespace |
| 57 | 54 |
| 58 OSExchangeDataProviderMus::OSExchangeDataProviderMus() {} | 55 OSExchangeDataProviderMus::OSExchangeDataProviderMus() {} |
| 59 | 56 |
| 60 OSExchangeDataProviderMus::OSExchangeDataProviderMus(Data data) | 57 OSExchangeDataProviderMus::OSExchangeDataProviderMus(Data data) |
| 61 : mime_data_(std::move(data)) {} | 58 : mime_data_(std::move(data)) {} |
| 62 | 59 |
| 63 OSExchangeDataProviderMus::~OSExchangeDataProviderMus() {} | 60 OSExchangeDataProviderMus::~OSExchangeDataProviderMus() {} |
| 64 | 61 |
| 65 OSExchangeDataProviderMus::Data OSExchangeDataProviderMus::GetData() const { | 62 OSExchangeDataProviderMus::Data OSExchangeDataProviderMus::GetData() const { |
| 66 return mime_data_; | 63 return mime_data_; |
| 67 } | 64 } |
| 68 | 65 |
| 69 std::unique_ptr<ui::OSExchangeData::Provider> | 66 std::unique_ptr<ui::OSExchangeData::Provider> OSExchangeDataProviderMus::Clone() |
| 70 OSExchangeDataProviderMus::Clone() const { | 67 const { |
| 71 std::unique_ptr<OSExchangeDataProviderMus> r = | 68 std::unique_ptr<OSExchangeDataProviderMus> r = |
| 72 base::MakeUnique<OSExchangeDataProviderMus>(); | 69 base::MakeUnique<OSExchangeDataProviderMus>(); |
| 73 r->drag_image_ = drag_image_; | 70 r->drag_image_ = drag_image_; |
| 74 r->drag_image_offset_ = drag_image_offset_; | 71 r->drag_image_offset_ = drag_image_offset_; |
| 75 r->mime_data_ = mime_data_; | 72 r->mime_data_ = mime_data_; |
| 76 return base::WrapUnique<ui::OSExchangeData::Provider>(r.release()); | 73 return base::WrapUnique<ui::OSExchangeData::Provider>(r.release()); |
| 77 } | 74 } |
| 78 | 75 |
| 79 void OSExchangeDataProviderMus::MarkOriginatedFromRenderer() { | 76 void OSExchangeDataProviderMus::MarkOriginatedFromRenderer() { |
| 80 // Currently unimplemented because ChromeOS doesn't need this. | 77 // Currently unimplemented because ChromeOS doesn't need this. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 109 void OSExchangeDataProviderMus::SetFilename(const base::FilePath& path) { | 106 void OSExchangeDataProviderMus::SetFilename(const base::FilePath& path) { |
| 110 std::vector<ui::FileInfo> data; | 107 std::vector<ui::FileInfo> data; |
| 111 data.push_back(ui::FileInfo(path, base::FilePath())); | 108 data.push_back(ui::FileInfo(path, base::FilePath())); |
| 112 SetFilenames(data); | 109 SetFilenames(data); |
| 113 } | 110 } |
| 114 | 111 |
| 115 void OSExchangeDataProviderMus::SetFilenames( | 112 void OSExchangeDataProviderMus::SetFilenames( |
| 116 const std::vector<ui::FileInfo>& file_names) { | 113 const std::vector<ui::FileInfo>& file_names) { |
| 117 std::vector<std::string> paths; | 114 std::vector<std::string> paths; |
| 118 for (std::vector<ui::FileInfo>::const_iterator it = file_names.begin(); | 115 for (std::vector<ui::FileInfo>::const_iterator it = file_names.begin(); |
| 119 it != file_names.end(); | 116 it != file_names.end(); ++it) { |
| 120 ++it) { | |
| 121 std::string url_spec = net::FilePathToFileURL(it->path).spec(); | 117 std::string url_spec = net::FilePathToFileURL(it->path).spec(); |
| 122 if (!url_spec.empty()) | 118 if (!url_spec.empty()) |
| 123 paths.push_back(url_spec); | 119 paths.push_back(url_spec); |
| 124 } | 120 } |
| 125 | 121 |
| 126 std::string joined_data = base::JoinString(paths, "\n"); | 122 std::string joined_data = base::JoinString(paths, "\n"); |
| 127 mime_data_[ui::mojom::kMimeTypeURIList] = FromString(joined_data); | 123 mime_data_[ui::mojom::kMimeTypeURIList] = FromString(joined_data); |
| 128 } | 124 } |
| 129 | 125 |
| 130 void OSExchangeDataProviderMus::SetPickledData( | 126 void OSExchangeDataProviderMus::SetPickledData( |
| 131 const ui::Clipboard::FormatType& format, | 127 const ui::Clipboard::FormatType& format, |
| 132 const base::Pickle& pickle) { | 128 const base::Pickle& pickle) { |
| 133 const unsigned char* bytes = | 129 const unsigned char* bytes = |
| 134 reinterpret_cast<const unsigned char*>(pickle.data()); | 130 reinterpret_cast<const unsigned char*>(pickle.data()); |
| 135 | 131 |
| 136 mime_data_[format.Serialize()] = mojo::Array<uint8_t>( | 132 mime_data_[format.Serialize()] = |
| 137 std::vector<uint8_t>(bytes, bytes + pickle.size())); | 133 mojo::Array<uint8_t>(std::vector<uint8_t>(bytes, bytes + pickle.size())); |
| 138 } | 134 } |
| 139 | 135 |
| 140 bool OSExchangeDataProviderMus::GetString(base::string16* data) const { | 136 bool OSExchangeDataProviderMus::GetString(base::string16* data) const { |
| 141 auto it = mime_data_.find(ui::mojom::kMimeTypeText); | 137 auto it = mime_data_.find(ui::mojom::kMimeTypeText); |
| 142 if (it != mime_data_.end()) | 138 if (it != mime_data_.end()) |
| 143 *data = base::UTF8ToUTF16(ToString(it->second)); | 139 *data = base::UTF8ToUTF16(ToString(it->second)); |
| 144 return it != mime_data_.end(); | 140 return it != mime_data_.end(); |
| 145 } | 141 } |
| 146 | 142 |
| 147 bool OSExchangeDataProviderMus::GetURLAndTitle( | 143 bool OSExchangeDataProviderMus::GetURLAndTitle( |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 bool OSExchangeDataProviderMus::HasCustomFormat( | 247 bool OSExchangeDataProviderMus::HasCustomFormat( |
| 252 const ui::Clipboard::FormatType& format) const { | 248 const ui::Clipboard::FormatType& format) const { |
| 253 return base::ContainsKey(mime_data_, format.Serialize()); | 249 return base::ContainsKey(mime_data_, format.Serialize()); |
| 254 } | 250 } |
| 255 | 251 |
| 256 // These methods were added in an ad-hoc way to different operating | 252 // These methods were added in an ad-hoc way to different operating |
| 257 // systems. We need to support them until they get cleaned up. | 253 // systems. We need to support them until they get cleaned up. |
| 258 #if (!defined(OS_CHROMEOS) && defined(USE_X11)) || defined(OS_WIN) | 254 #if (!defined(OS_CHROMEOS) && defined(USE_X11)) || defined(OS_WIN) |
| 259 void OSExchangeDataProviderMus::SetFileContents( | 255 void OSExchangeDataProviderMus::SetFileContents( |
| 260 const base::FilePath& filename, | 256 const base::FilePath& filename, |
| 261 const std::string& file_contents) { | 257 const std::string& file_contents) {} |
| 262 } | |
| 263 #endif | 258 #endif |
| 264 | 259 |
| 265 #if defined(OS_WIN) | 260 #if defined(OS_WIN) |
| 266 bool OSExchangeDataProviderMus::GetFileContents( | 261 bool OSExchangeDataProviderMus::GetFileContents( |
| 267 base::FilePath* filename, | 262 base::FilePath* filename, |
| 268 std::string* file_contents) const { | 263 std::string* file_contents) const { |
| 269 return false; | 264 return false; |
| 270 } | 265 } |
| 271 | 266 |
| 272 bool OSExchangeDataProviderMus::HasFileContents() const { | 267 bool OSExchangeDataProviderMus::HasFileContents() const { |
| 273 return false; | 268 return false; |
| 274 } | 269 } |
| 275 | 270 |
| 276 void OSExchangeDataProviderMus::SetDownloadFileInfo( | 271 void OSExchangeDataProviderMus::SetDownloadFileInfo( |
| 277 const ui::OSExchangeData::DownloadFileInfo& download) { | 272 const ui::OSExchangeData::DownloadFileInfo& download) {} |
| 278 } | |
| 279 #endif | 273 #endif |
| 280 | 274 |
| 281 #if defined(USE_AURA) | 275 #if defined(USE_AURA) |
| 282 void OSExchangeDataProviderMus::SetHtml(const base::string16& html, | 276 void OSExchangeDataProviderMus::SetHtml(const base::string16& html, |
| 283 const GURL& base_url) { | 277 const GURL& base_url) { |
| 284 std::vector<unsigned char> bytes; | 278 std::vector<unsigned char> bytes; |
| 285 // Manually jam a UTF16 BOM into bytes because otherwise, other programs will | 279 // Manually jam a UTF16 BOM into bytes because otherwise, other programs will |
| 286 // assume UTF-8. | 280 // assume UTF-8. |
| 287 bytes.push_back(0xFF); | 281 bytes.push_back(0xFF); |
| 288 bytes.push_back(0xFE); | 282 bytes.push_back(0xFE); |
| 289 AddString16ToVector(html, &bytes); | 283 AddString16ToVector(html, &bytes); |
| 290 mime_data_[ui::mojom::kMimeTypeHTML] = bytes; | 284 mime_data_[ui::mojom::kMimeTypeHTML] = bytes; |
| 291 } | 285 } |
| 292 | 286 |
| 293 bool OSExchangeDataProviderMus::GetHtml(base::string16* html, | 287 bool OSExchangeDataProviderMus::GetHtml(base::string16* html, |
| 294 GURL* base_url) const { | 288 GURL* base_url) const { |
| 295 auto it = mime_data_.find(ui::mojom::kMimeTypeHTML); | 289 auto it = mime_data_.find(ui::mojom::kMimeTypeHTML); |
| 296 if (it == mime_data_.end()) | 290 if (it == mime_data_.end()) |
| 297 return false; | 291 return false; |
| 298 | 292 |
| 299 const unsigned char* data = it->second.data(); | 293 const unsigned char* data = it->second.data(); |
| 300 size_t size = it->second.size(); | 294 size_t size = it->second.size(); |
| 301 base::string16 markup; | 295 base::string16 markup; |
| 302 | 296 |
| 303 // If the data starts with 0xFEFF, i.e., Byte Order Mark, assume it is | 297 // If the data starts with 0xFEFF, i.e., Byte Order Mark, assume it is |
| 304 // UTF-16, otherwise assume UTF-8. | 298 // UTF-16, otherwise assume UTF-8. |
| 305 if (size >= 2 && | 299 if (size >= 2 && reinterpret_cast<const uint16_t*>(data)[0] == 0xFEFF) { |
| 306 reinterpret_cast<const uint16_t*>(data)[0] == 0xFEFF) { | |
| 307 markup.assign(reinterpret_cast<const base::char16*>(data) + 1, | 300 markup.assign(reinterpret_cast<const base::char16*>(data) + 1, |
| 308 (size / 2) - 1); | 301 (size / 2) - 1); |
| 309 } else { | 302 } else { |
| 310 base::UTF8ToUTF16(reinterpret_cast<const char*>(data), size, &markup); | 303 base::UTF8ToUTF16(reinterpret_cast<const char*>(data), size, &markup); |
| 311 } | 304 } |
| 312 | 305 |
| 313 // If there is a terminating NULL, drop it. | 306 // If there is a terminating NULL, drop it. |
| 314 if (!markup.empty() && markup.at(markup.length() - 1) == '\0') | 307 if (!markup.empty() && markup.at(markup.length() - 1) == '\0') |
| 315 markup.resize(markup.length() - 1); | 308 markup.resize(markup.length() - 1); |
| 316 | 309 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 | 355 |
| 363 GURL test_url(str); | 356 GURL test_url(str); |
| 364 if (!test_url.is_valid()) | 357 if (!test_url.is_valid()) |
| 365 return false; | 358 return false; |
| 366 | 359 |
| 367 if (url) | 360 if (url) |
| 368 *url = test_url; | 361 *url = test_url; |
| 369 return true; | 362 return true; |
| 370 } | 363 } |
| 371 | 364 |
| 372 } // namespace views | 365 } // namespace aura |
| OLD | NEW |