| 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/clipboard_mus.h" | 5 #include "ui/views/mus/clipboard_mus.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "mojo/common/common_type_converters.h" | |
| 15 #include "mojo/public/cpp/bindings/sync_call_restrictions.h" | 14 #include "mojo/public/cpp/bindings/sync_call_restrictions.h" |
| 16 #include "services/service_manager/public/cpp/connector.h" | 15 #include "services/service_manager/public/cpp/connector.h" |
| 17 #include "services/ui/public/interfaces/constants.mojom.h" | 16 #include "services/ui/public/interfaces/constants.mojom.h" |
| 18 #include "third_party/skia/include/core/SkBitmap.h" | 17 #include "third_party/skia/include/core/SkBitmap.h" |
| 19 #include "ui/base/clipboard/custom_data_helper.h" | 18 #include "ui/base/clipboard/custom_data_helper.h" |
| 20 #include "ui/gfx/codec/png_codec.h" | 19 #include "ui/gfx/codec/png_codec.h" |
| 21 | 20 |
| 22 namespace views { | 21 namespace views { |
| 23 namespace { | 22 namespace { |
| 24 | 23 |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 | 147 |
| 149 *contains_filenames = false; | 148 *contains_filenames = false; |
| 150 } | 149 } |
| 151 | 150 |
| 152 void ClipboardMus::ReadText(ui::ClipboardType type, | 151 void ClipboardMus::ReadText(ui::ClipboardType type, |
| 153 base::string16* result) const { | 152 base::string16* result) const { |
| 154 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; | 153 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
| 155 base::Optional<std::vector<uint8_t>> text_data; | 154 base::Optional<std::vector<uint8_t>> text_data; |
| 156 uint64_t sequence_number = 0; | 155 uint64_t sequence_number = 0; |
| 157 if (clipboard_->ReadClipboardData(GetType(type), ui::mojom::kMimeTypeText, | 156 if (clipboard_->ReadClipboardData(GetType(type), ui::mojom::kMimeTypeText, |
| 158 &sequence_number, &text_data)) { | 157 &sequence_number, &text_data) && |
| 159 std::string text = | 158 text_data) { |
| 160 mojo::Array<uint8_t>(std::move(text_data)).To<std::string>(); | 159 *result = base::UTF8ToUTF16(base::StringPiece( |
| 161 *result = base::UTF8ToUTF16(text); | 160 reinterpret_cast<char*>(text_data->data()), text_data->size())); |
| 162 } | 161 } |
| 163 } | 162 } |
| 164 | 163 |
| 165 void ClipboardMus::ReadAsciiText(ui::ClipboardType type, | 164 void ClipboardMus::ReadAsciiText(ui::ClipboardType type, |
| 166 std::string* result) const { | 165 std::string* result) const { |
| 167 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; | 166 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
| 168 base::Optional<std::vector<uint8_t>> text_data; | 167 base::Optional<std::vector<uint8_t>> text_data; |
| 169 uint64_t sequence_number = 0; | 168 uint64_t sequence_number = 0; |
| 170 if (clipboard_->ReadClipboardData(GetType(type), ui::mojom::kMimeTypeText, | 169 if (clipboard_->ReadClipboardData(GetType(type), ui::mojom::kMimeTypeText, |
| 171 &sequence_number, &text_data)) { | 170 &sequence_number, &text_data) && |
| 172 *result = mojo::Array<uint8_t>(std::move(text_data)).To<std::string>(); | 171 text_data) { |
| 172 result->assign(text_data->begin(), text_data->end()); |
| 173 } | 173 } |
| 174 } | 174 } |
| 175 | 175 |
| 176 void ClipboardMus::ReadHTML(ui::ClipboardType type, | 176 void ClipboardMus::ReadHTML(ui::ClipboardType type, |
| 177 base::string16* markup, | 177 base::string16* markup, |
| 178 std::string* src_url, | 178 std::string* src_url, |
| 179 uint32_t* fragment_start, | 179 uint32_t* fragment_start, |
| 180 uint32_t* fragment_end) const { | 180 uint32_t* fragment_end) const { |
| 181 markup->clear(); | 181 markup->clear(); |
| 182 if (src_url) | 182 if (src_url) |
| 183 src_url->clear(); | 183 src_url->clear(); |
| 184 *fragment_start = 0; | 184 *fragment_start = 0; |
| 185 *fragment_end = 0; | 185 *fragment_end = 0; |
| 186 | 186 |
| 187 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; | 187 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
| 188 base::Optional<std::vector<uint8_t>> html_data; | 188 base::Optional<std::vector<uint8_t>> html_data; |
| 189 uint64_t sequence_number = 0; | 189 uint64_t sequence_number = 0; |
| 190 if (clipboard_->ReadClipboardData(GetType(type), ui::mojom::kMimeTypeHTML, | 190 if (clipboard_->ReadClipboardData(GetType(type), ui::mojom::kMimeTypeHTML, |
| 191 &sequence_number, &html_data)) { | 191 &sequence_number, &html_data) && |
| 192 *markup = base::UTF8ToUTF16( | 192 html_data) { |
| 193 mojo::Array<uint8_t>(std::move(html_data)).To<std::string>()); | 193 *markup = base::UTF8ToUTF16(base::StringPiece( |
| 194 reinterpret_cast<char*>(html_data->data()), html_data->size())); |
| 194 *fragment_end = static_cast<uint32_t>(markup->length()); | 195 *fragment_end = static_cast<uint32_t>(markup->length()); |
| 195 | 196 |
| 196 // We only bother fetching the source url if we were the ones who wrote | 197 // We only bother fetching the source url if we were the ones who wrote |
| 197 // this html data to the clipboard. | 198 // this html data to the clipboard. |
| 198 base::Optional<std::vector<uint8_t>> url_data; | 199 base::Optional<std::vector<uint8_t>> url_data; |
| 199 if (clipboard_->ReadClipboardData(GetType(type), kInternalSourceURL, | 200 if (clipboard_->ReadClipboardData(GetType(type), kInternalSourceURL, |
| 200 &sequence_number, &url_data)) { | 201 &sequence_number, &url_data) && |
| 201 *src_url = mojo::Array<uint8_t>(std::move(url_data)).To<std::string>(); | 202 url_data) { |
| 203 src_url->assign(url_data->begin(), url_data->end()); |
| 202 } | 204 } |
| 203 } | 205 } |
| 204 } | 206 } |
| 205 | 207 |
| 206 void ClipboardMus::ReadRTF(ui::ClipboardType type, std::string* result) const { | 208 void ClipboardMus::ReadRTF(ui::ClipboardType type, std::string* result) const { |
| 207 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; | 209 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
| 208 base::Optional<std::vector<uint8_t>> rtf_data; | 210 base::Optional<std::vector<uint8_t>> rtf_data; |
| 209 uint64_t sequence_number = 0; | 211 uint64_t sequence_number = 0; |
| 210 if (clipboard_->ReadClipboardData(GetType(type), ui::mojom::kMimeTypeRTF, | 212 if (clipboard_->ReadClipboardData(GetType(type), ui::mojom::kMimeTypeRTF, |
| 211 &sequence_number, &rtf_data)) { | 213 &sequence_number, &rtf_data) && |
| 212 *result = mojo::Array<uint8_t>(std::move(rtf_data)).To<std::string>(); | 214 rtf_data) { |
| 215 result->assign(rtf_data->begin(), rtf_data->end()); |
| 213 } | 216 } |
| 214 } | 217 } |
| 215 | 218 |
| 216 SkBitmap ClipboardMus::ReadImage(ui::ClipboardType type) const { | 219 SkBitmap ClipboardMus::ReadImage(ui::ClipboardType type) const { |
| 217 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; | 220 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
| 218 base::Optional<std::vector<uint8_t>> data; | 221 base::Optional<std::vector<uint8_t>> data; |
| 219 uint64_t sequence_number = 0; | 222 uint64_t sequence_number = 0; |
| 220 if (clipboard_->ReadClipboardData(GetType(type), ui::mojom::kMimeTypePNG, | 223 if (clipboard_->ReadClipboardData(GetType(type), ui::mojom::kMimeTypePNG, |
| 221 &sequence_number, &data) && | 224 &sequence_number, &data) && |
| 222 data.has_value()) { | 225 data.has_value()) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 248 NOTIMPLEMENTED(); | 251 NOTIMPLEMENTED(); |
| 249 } | 252 } |
| 250 | 253 |
| 251 void ClipboardMus::ReadData(const FormatType& format, | 254 void ClipboardMus::ReadData(const FormatType& format, |
| 252 std::string* result) const { | 255 std::string* result) const { |
| 253 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; | 256 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
| 254 base::Optional<std::vector<uint8_t>> data; | 257 base::Optional<std::vector<uint8_t>> data; |
| 255 uint64_t sequence_number = 0; | 258 uint64_t sequence_number = 0; |
| 256 if (clipboard_->ReadClipboardData(ui::mojom::Clipboard::Type::COPY_PASTE, | 259 if (clipboard_->ReadClipboardData(ui::mojom::Clipboard::Type::COPY_PASTE, |
| 257 GetMimeTypeFor(format), &sequence_number, | 260 GetMimeTypeFor(format), &sequence_number, |
| 258 &data)) { | 261 &data) && |
| 259 *result = mojo::Array<uint8_t>(std::move(data)).To<std::string>(); | 262 data) { |
| 263 result->assign(data->begin(), data->end()); |
| 260 } | 264 } |
| 261 } | 265 } |
| 262 | 266 |
| 263 void ClipboardMus::WriteObjects(ui::ClipboardType type, | 267 void ClipboardMus::WriteObjects(ui::ClipboardType type, |
| 264 const ObjectMap& objects) { | 268 const ObjectMap& objects) { |
| 265 current_clipboard_.emplace(); | 269 current_clipboard_.emplace(); |
| 266 for (const auto& p : objects) | 270 for (const auto& p : objects) |
| 267 DispatchObject(static_cast<ObjectType>(p.first), p.second); | 271 DispatchObject(static_cast<ObjectType>(p.first), p.second); |
| 268 | 272 |
| 269 // Sends the data to mus server. | 273 // Sends the data to mus server. |
| 270 uint64_t sequence_number = 0; | 274 uint64_t sequence_number = 0; |
| 271 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; | 275 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
| 272 clipboard_->WriteClipboardData(GetType(type), std::move(current_clipboard_), | 276 clipboard_->WriteClipboardData(GetType(type), std::move(current_clipboard_), |
| 273 &sequence_number); | 277 &sequence_number); |
| 274 } | 278 } |
| 275 | 279 |
| 276 void ClipboardMus::WriteText(const char* text_data, size_t text_len) { | 280 void ClipboardMus::WriteText(const char* text_data, size_t text_len) { |
| 277 DCHECK(current_clipboard_); | 281 DCHECK(current_clipboard_); |
| 278 current_clipboard_.value()[ui::mojom::kMimeTypeText] = | 282 current_clipboard_.value()[ui::mojom::kMimeTypeText] = |
| 279 mojo::Array<uint8_t>::From(base::StringPiece(text_data, text_len)) | 283 std::vector<uint8_t>(text_data, text_data + text_len); |
| 280 .PassStorage(); | |
| 281 } | 284 } |
| 282 | 285 |
| 283 void ClipboardMus::WriteHTML(const char* markup_data, | 286 void ClipboardMus::WriteHTML(const char* markup_data, |
| 284 size_t markup_len, | 287 size_t markup_len, |
| 285 const char* url_data, | 288 const char* url_data, |
| 286 size_t url_len) { | 289 size_t url_len) { |
| 287 DCHECK(current_clipboard_); | 290 DCHECK(current_clipboard_); |
| 288 current_clipboard_.value()[ui::mojom::kMimeTypeHTML] = | 291 current_clipboard_.value()[ui::mojom::kMimeTypeHTML] = |
| 289 mojo::Array<uint8_t>::From(base::StringPiece(markup_data, markup_len)) | 292 std::vector<uint8_t>(markup_data, markup_data + markup_len); |
| 290 .PassStorage(); | |
| 291 if (url_len > 0) { | 293 if (url_len > 0) { |
| 292 current_clipboard_.value()[kInternalSourceURL] = | 294 current_clipboard_.value()[kInternalSourceURL] = |
| 293 mojo::Array<uint8_t>::From(base::StringPiece(url_data, url_len)) | 295 std::vector<uint8_t>(url_data, url_data + url_len); |
| 294 .PassStorage(); | |
| 295 } | 296 } |
| 296 } | 297 } |
| 297 | 298 |
| 298 void ClipboardMus::WriteRTF(const char* rtf_data, size_t data_len) { | 299 void ClipboardMus::WriteRTF(const char* rtf_data, size_t data_len) { |
| 299 DCHECK(current_clipboard_); | 300 DCHECK(current_clipboard_); |
| 300 current_clipboard_.value()[ui::mojom::kMimeTypeRTF] = | 301 current_clipboard_.value()[ui::mojom::kMimeTypeRTF] = |
| 301 mojo::Array<uint8_t>::From(base::StringPiece(rtf_data, data_len)) | 302 std::vector<uint8_t>(rtf_data, rtf_data + data_len); |
| 302 .PassStorage(); | |
| 303 } | 303 } |
| 304 | 304 |
| 305 void ClipboardMus::WriteBookmark(const char* title_data, | 305 void ClipboardMus::WriteBookmark(const char* title_data, |
| 306 size_t title_len, | 306 size_t title_len, |
| 307 const char* url_data, | 307 const char* url_data, |
| 308 size_t url_len) { | 308 size_t url_len) { |
| 309 // Writes a Mozilla url (UTF16: URL, newline, title) | 309 // Writes a Mozilla url (UTF16: URL, newline, title) |
| 310 base::string16 bookmark = | 310 base::string16 bookmark = |
| 311 base::UTF8ToUTF16(base::StringPiece(url_data, url_len)) + | 311 base::UTF8ToUTF16(base::StringPiece(url_data, url_len)) + |
| 312 base::ASCIIToUTF16("\n") + | 312 base::ASCIIToUTF16("\n") + |
| 313 base::UTF8ToUTF16(base::StringPiece(title_data, title_len)); | 313 base::UTF8ToUTF16(base::StringPiece(title_data, title_len)); |
| 314 | 314 |
| 315 DCHECK(current_clipboard_); | 315 DCHECK(current_clipboard_); |
| 316 current_clipboard_.value()[ui::mojom::kMimeTypeMozillaURL] = | 316 current_clipboard_.value()[ui::mojom::kMimeTypeMozillaURL] = |
| 317 mojo::Array<uint8_t>::From(bookmark).PassStorage(); | 317 std::vector<uint8_t>( |
| 318 reinterpret_cast<const uint8_t*>(bookmark.data()), |
| 319 reinterpret_cast<const uint8_t*>(bookmark.data() + bookmark.size())); |
| 318 } | 320 } |
| 319 | 321 |
| 320 void ClipboardMus::WriteWebSmartPaste() { | 322 void ClipboardMus::WriteWebSmartPaste() { |
| 321 DCHECK(current_clipboard_); | 323 DCHECK(current_clipboard_); |
| 322 current_clipboard_.value()[kMimeTypeWebkitSmartPaste] = | 324 current_clipboard_.value()[kMimeTypeWebkitSmartPaste] = |
| 323 std::vector<uint8_t>(); | 325 std::vector<uint8_t>(); |
| 324 } | 326 } |
| 325 | 327 |
| 326 void ClipboardMus::WriteBitmap(const SkBitmap& bitmap) { | 328 void ClipboardMus::WriteBitmap(const SkBitmap& bitmap) { |
| 327 DCHECK(current_clipboard_); | 329 DCHECK(current_clipboard_); |
| 328 // Encode the bitmap as a PNG for transport. | 330 // Encode the bitmap as a PNG for transport. |
| 329 std::vector<unsigned char> output; | 331 std::vector<unsigned char> output; |
| 330 if (gfx::PNGCodec::FastEncodeBGRASkBitmap(bitmap, false, &output)) { | 332 if (gfx::PNGCodec::FastEncodeBGRASkBitmap(bitmap, false, &output)) { |
| 331 current_clipboard_.value()[ui::mojom::kMimeTypePNG] = | 333 current_clipboard_.value()[ui::mojom::kMimeTypePNG] = std::move(output); |
| 332 mojo::Array<uint8_t>::From(output).PassStorage(); | |
| 333 } | 334 } |
| 334 } | 335 } |
| 335 | 336 |
| 336 void ClipboardMus::WriteData(const FormatType& format, | 337 void ClipboardMus::WriteData(const FormatType& format, |
| 337 const char* data_data, | 338 const char* data_data, |
| 338 size_t data_len) { | 339 size_t data_len) { |
| 339 DCHECK(current_clipboard_); | 340 DCHECK(current_clipboard_); |
| 340 current_clipboard_.value()[GetMimeTypeFor(format)] = | 341 current_clipboard_.value()[GetMimeTypeFor(format)] = |
| 341 mojo::Array<uint8_t>::From(base::StringPiece(data_data, data_len)) | 342 std::vector<uint8_t>(data_data, data_data + data_len); |
| 342 .PassStorage(); | |
| 343 } | 343 } |
| 344 | 344 |
| 345 } // namespace views | 345 } // namespace views |
| OLD | NEW |