Chromium Code Reviews| OLD | NEW | 
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ui/views/mus/clipboard_mus.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/stl_util.h" | |
| 9 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "base/threading/thread_restrictions.h" | |
| 11 #include "mojo/common/common_type_converters.h" | |
| 12 #include "services/shell/public/cpp/connector.h" | |
| 13 #include "third_party/skia/include/core/SkBitmap.h" | |
| 14 #include "ui/base/clipboard/custom_data_helper.h" | |
| 15 #include "ui/gfx/codec/png_codec.h" | |
| 16 | |
| 17 namespace views { | |
| 18 namespace { | |
| 19 | |
| 20 mus::mojom::Clipboard::Type GetType(ui::ClipboardType type) { | |
| 21 switch (type) { | |
| 22 case ui::CLIPBOARD_TYPE_COPY_PASTE: | |
| 23 return mus::mojom::Clipboard::Type::COPY_PASTE; | |
| 24 case ui::CLIPBOARD_TYPE_SELECTION: | |
| 25 return mus::mojom::Clipboard::Type::SELECTION; | |
| 26 case ui::CLIPBOARD_TYPE_DRAG: | |
| 27 return mus::mojom::Clipboard::Type::DRAG; | |
| 28 } | |
| 29 | |
| 30 NOTREACHED(); | |
| 31 return mus::mojom::Clipboard::Type::COPY_PASTE; | |
| 32 } | |
| 33 | |
| 34 const char kInternalURL[] = "chromium/internal-url"; | |
| 35 | |
| 36 } // namespace | |
| 37 | |
| 38 ClipboardMus::ClipboardMus() {} | |
| 39 | |
| 40 ClipboardMus::~ClipboardMus() {} | |
| 41 | |
| 42 void ClipboardMus::Init(shell::Connector* connector) { | |
| 43 connector->ConnectToInterface("mojo:mus", &clipboard_); | |
| 44 } | |
| 45 | |
| 46 // TODO(erg): This isn't optimal. It would be better to move the entire | |
| 47 // FormatType system to mime types throughout chrome, but that's a very large | |
| 48 // change. | |
| 49 mojo::String ClipboardMus::GetMimeTypeFor(const FormatType& format) const { | |
| 50 if (format.Equals(GetUrlFormatType()) || format.Equals(GetUrlWFormatType())) | |
| 51 return mus::mojom::MIME_TYPE_URI_LIST; | |
| 52 if (format.Equals(GetMozUrlFormatType())) | |
| 53 return mus::mojom::MIME_TYPE_MOZ_URL; | |
| 54 if (format.Equals(GetPlainTextFormatType()) || | |
| 55 format.Equals(GetPlainTextWFormatType())) { | |
| 56 return mus::mojom::MIME_TYPE_TEXT; | |
| 57 } | |
| 58 if (format.Equals(GetHtmlFormatType())) | |
| 59 return mus::mojom::MIME_TYPE_HTML; | |
| 60 if (format.Equals(GetRtfFormatType())) | |
| 61 return mus::mojom::MIME_TYPE_RTF; | |
| 62 if (format.Equals(GetBitmapFormatType())) | |
| 63 return mus::mojom::MIME_TYPE_PNG; | |
| 64 if (format.Equals(GetWebKitSmartPasteFormatType())) | |
| 65 return kMimeTypeWebkitSmartPaste; | |
| 66 if (format.Equals(GetWebCustomDataFormatType())) | |
| 67 return kMimeTypeWebCustomData; | |
| 68 if (format.Equals(GetPepperCustomDataFormatType())) | |
| 69 return kMimeTypePepperCustomData; | |
| 70 | |
| 71 // TODO(erg): This isn't optimal, but it's the best we can do. On windows, | |
| 72 // this will return strings that aren't MIME types, though they'll be | |
| 73 // unique and should be serializable on the other side of the mojo | |
| 74 // connection. | |
| 75 return format.Serialize(); | |
| 76 } | |
| 77 | |
| 78 bool ClipboardMus::HasMimeType(const mojo::Array<mojo::String>& available_types, | |
| 79 const std::string& type) const { | |
| 80 return ContainsValue(available_types, type); | |
| 81 } | |
| 82 | |
| 83 uint64_t ClipboardMus::GetSequenceNumber(ui::ClipboardType type) const { | |
| 84 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 85 uint64_t sequence_number = 0; | |
| 86 clipboard_->GetSequenceNumber(GetType(type), &sequence_number); | |
| 87 return sequence_number; | |
| 88 } | |
| 89 | |
| 90 bool ClipboardMus::IsFormatAvailable(const FormatType& format, | |
| 91 ui::ClipboardType type) const { | |
| 92 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 93 | |
| 94 uint64_t sequence_number = 0; | |
| 95 mojo::Array<mojo::String> available_types; | |
| 96 clipboard_->GetAvailableMimeTypes(GetType(type), &sequence_number, | |
| 97 &available_types); | |
| 98 | |
| 99 mojo::String format_in_mime = GetMimeTypeFor(format); | |
| 100 return ContainsValue(available_types, format_in_mime); | |
| 101 } | |
| 102 | |
| 103 void ClipboardMus::Clear(ui::ClipboardType type) { | |
| 104 current_clipboard_.SetToEmpty(); | |
| 105 | |
| 106 // Sends the data to mus server. | |
| 107 uint64_t sequence_number = 0; | |
| 108 mojo::Map<mojo::String, mojo::Array<uint8_t>> null_data(nullptr); | |
| 109 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 110 clipboard_->WriteClipboardData(GetType(type), std::move(null_data), | |
| 111 &sequence_number); | |
| 112 } | |
| 113 | |
| 114 void ClipboardMus::ReadAvailableTypes(ui::ClipboardType type, | |
| 115 std::vector<base::string16>* types, | |
| 116 bool* contains_filenames) const { | |
| 117 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 118 | |
| 119 uint64_t sequence_number = 0; | |
| 120 mojo::Array<mojo::String> available_types; | |
| 121 clipboard_->GetAvailableMimeTypes(GetType(type), &sequence_number, | |
| 122 &available_types); | |
| 123 | |
| 124 types->clear(); | |
| 125 if (HasMimeType(available_types, mus::mojom::MIME_TYPE_TEXT)) | |
| 126 types->push_back(base::UTF8ToUTF16(mus::mojom::MIME_TYPE_TEXT)); | |
| 127 if (HasMimeType(available_types, mus::mojom::MIME_TYPE_HTML)) | |
| 128 types->push_back(base::UTF8ToUTF16(mus::mojom::MIME_TYPE_HTML)); | |
| 129 if (HasMimeType(available_types, mus::mojom::MIME_TYPE_RTF)) | |
| 130 types->push_back(base::UTF8ToUTF16(mus::mojom::MIME_TYPE_RTF)); | |
| 131 if (HasMimeType(available_types, mus::mojom::MIME_TYPE_PNG)) | |
| 132 types->push_back(base::UTF8ToUTF16(mus::mojom::MIME_TYPE_PNG)); | |
| 133 | |
| 134 if (HasMimeType(available_types, kMimeTypeWebCustomData)) { | |
| 135 mojo::Array<uint8_t> custom_data; | |
| 136 uint64_t sequence_number = 0; | |
| 137 if (clipboard_->ReadClipboardData(GetType(type), kMimeTypeWebCustomData, | |
| 138 &sequence_number, &custom_data)) { | |
| 139 ui::ReadCustomDataTypes(&custom_data.front(), custom_data.size(), types); | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 *contains_filenames = false; | |
| 144 } | |
| 145 | |
| 146 void ClipboardMus::ReadText(ui::ClipboardType type, | |
| 147 base::string16* result) const { | |
| 148 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 149 mojo::Array<uint8_t> text_data; | |
| 150 uint64_t sequence_number = 0; | |
| 151 if (clipboard_->ReadClipboardData(GetType(type), | |
| 152 mojo::String(mus::mojom::MIME_TYPE_TEXT), | |
| 153 &sequence_number, &text_data)) { | |
| 154 std::string text = text_data.To<std::string>(); | |
| 
 
sky
2016/05/31 20:50:24
Do all of the converters succeed (not DCHECK/CHECK
 
Elliot Glaysher
2016/06/01 19:41:13
These type converters, defined in common_type_conv
 
 | |
| 155 *result = base::UTF8ToUTF16(text); | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 void ClipboardMus::ReadAsciiText(ui::ClipboardType type, | |
| 160 std::string* result) const { | |
| 161 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 162 mojo::Array<uint8_t> text_data; | |
| 163 uint64_t sequence_number = 0; | |
| 164 if (clipboard_->ReadClipboardData(GetType(type), | |
| 165 mojo::String(mus::mojom::MIME_TYPE_TEXT), | |
| 166 &sequence_number, &text_data)) { | |
| 167 *result = text_data.To<std::string>(); | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 void ClipboardMus::ReadHTML(ui::ClipboardType type, | |
| 172 base::string16* markup, | |
| 173 std::string* src_url, | |
| 174 uint32_t* fragment_start, | |
| 175 uint32_t* fragment_end) const { | |
| 176 markup->clear(); | |
| 177 if (src_url) | |
| 178 src_url->clear(); | |
| 179 *fragment_start = 0; | |
| 180 *fragment_end = 0; | |
| 181 | |
| 182 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 183 mojo::Array<uint8_t> html_data; | |
| 184 uint64_t sequence_number = 0; | |
| 185 if (clipboard_->ReadClipboardData(GetType(type), | |
| 186 mojo::String(mus::mojom::MIME_TYPE_HTML), | |
| 187 &sequence_number, &html_data)) { | |
| 188 *markup = base::UTF8ToUTF16(html_data.To<std::string>()); | |
| 189 *fragment_end = static_cast<uint32_t>(markup->length()); | |
| 190 | |
| 191 // We only bother fetching the source url if we were the ones who wrote | |
| 192 // this html data to the clipboard. | |
| 193 mojo::Array<uint8_t> url_data; | |
| 194 if (clipboard_->ReadClipboardData(GetType(type), kInternalURL, | |
| 195 &sequence_number, &url_data)) { | |
| 196 *src_url = url_data.To<std::string>(); | |
| 197 } | |
| 198 } | |
| 199 } | |
| 200 | |
| 201 void ClipboardMus::ReadRTF(ui::ClipboardType type, std::string* result) const { | |
| 202 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 203 mojo::Array<uint8_t> rtf_data; | |
| 204 uint64_t sequence_number = 0; | |
| 205 if (clipboard_->ReadClipboardData( | |
| 206 GetType(type), mojo::String(mus::mojom::MIME_TYPE_RTF), | |
| 207 &sequence_number, &rtf_data)) { | |
| 208 *result = rtf_data.To<std::string>(); | |
| 209 } | |
| 210 } | |
| 211 | |
| 212 SkBitmap ClipboardMus::ReadImage(ui::ClipboardType type) const { | |
| 213 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 214 mojo::Array<uint8_t> data; | |
| 215 uint64_t sequence_number = 0; | |
| 216 if (clipboard_->ReadClipboardData( | |
| 217 GetType(type), mojo::String(mus::mojom::MIME_TYPE_RTF), | |
| 
 
sky
2016/05/31 20:50:24
TYPE_PNG?
 
 | |
| 218 &sequence_number, &data)) { | |
| 219 SkBitmap bitmap; | |
| 220 if (gfx::PNGCodec::Decode(&data.front(), data.size(), &bitmap)) | |
| 221 return SkBitmap(bitmap); | |
| 222 } | |
| 223 | |
| 224 return SkBitmap(); | |
| 225 } | |
| 226 | |
| 227 void ClipboardMus::ReadCustomData(ui::ClipboardType clipboard_type, | |
| 228 const base::string16& type, | |
| 229 base::string16* result) const { | |
| 230 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 231 mojo::Array<uint8_t> custom_data; | |
| 232 uint64_t sequence_number = 0; | |
| 233 if (clipboard_->ReadClipboardData(GetType(clipboard_type), | |
| 234 mojo::String(kMimeTypeWebCustomData), | |
| 235 &sequence_number, &custom_data)) { | |
| 236 ui::ReadCustomDataForType(&custom_data.front(), custom_data.size(), type, | |
| 237 result); | |
| 238 } | |
| 239 } | |
| 240 | |
| 241 void ClipboardMus::ReadBookmark(base::string16* title, std::string* url) const { | |
| 242 // TODO(erg): This is NOTIMPLEMENTED() on all linux platforms? | |
| 243 NOTIMPLEMENTED(); | |
| 244 } | |
| 245 | |
| 246 void ClipboardMus::ReadData(const FormatType& format, | |
| 247 std::string* result) const { | |
| 248 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 249 mojo::Array<uint8_t> data; | |
| 250 uint64_t sequence_number = 0; | |
| 251 if (clipboard_->ReadClipboardData(mus::mojom::Clipboard::Type::COPY_PASTE, | |
| 252 GetMimeTypeFor(format), | |
| 253 &sequence_number, &data)) { | |
| 254 *result = data.To<std::string>(); | |
| 255 } | |
| 256 } | |
| 257 | |
| 258 void ClipboardMus::WriteObjects(ui::ClipboardType type, | |
| 259 const ObjectMap& objects) { | |
| 260 current_clipboard_.SetToEmpty(); | |
| 261 for (ObjectMap::const_iterator iter = objects.begin(); iter != objects.end(); | |
| 262 ++iter) { | |
| 263 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); | |
| 264 } | |
| 265 | |
| 266 // Sends the data to mus server. | |
| 267 uint64_t sequence_number = 0; | |
| 268 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 269 clipboard_->WriteClipboardData(GetType(type), std::move(current_clipboard_), | |
| 270 &sequence_number); | |
| 271 } | |
| 272 | |
| 273 void ClipboardMus::WriteText(const char* text_data, size_t text_len) { | |
| 274 current_clipboard_.insert( | |
| 275 mus::mojom::MIME_TYPE_TEXT, | |
| 276 mojo::Array<uint8_t>::From(base::StringPiece(text_data, text_len))); | |
| 277 } | |
| 278 | |
| 279 void ClipboardMus::WriteHTML(const char* markup_data, | |
| 280 size_t markup_len, | |
| 281 const char* url_data, | |
| 282 size_t url_len) { | |
| 283 current_clipboard_.insert( | |
| 284 mus::mojom::MIME_TYPE_HTML, | |
| 285 mojo::Array<uint8_t>::From(base::StringPiece(markup_data, markup_len))); | |
| 286 if (url_len > 0) { | |
| 287 current_clipboard_.insert( | |
| 288 kInternalURL, | |
| 289 mojo::Array<uint8_t>::From(base::StringPiece(url_data, url_len))); | |
| 290 } | |
| 291 } | |
| 292 | |
| 293 void ClipboardMus::WriteRTF(const char* rtf_data, size_t data_len) { | |
| 294 current_clipboard_.insert( | |
| 295 mus::mojom::MIME_TYPE_RTF, | |
| 296 mojo::Array<uint8_t>::From(base::StringPiece(rtf_data, data_len))); | |
| 297 } | |
| 298 | |
| 299 void ClipboardMus::WriteBookmark(const char* title_data, | |
| 300 size_t title_len, | |
| 301 const char* url_data, | |
| 302 size_t url_len) { | |
| 303 // Writes a Mozilla url (UTF16: URL, newline, title) | |
| 304 base::string16 bookmark = | |
| 305 base::UTF8ToUTF16(base::StringPiece(url_data, url_len)) + | |
| 306 base::ASCIIToUTF16("\n") + | |
| 307 base::UTF8ToUTF16(base::StringPiece(title_data, title_len)); | |
| 308 | |
| 309 current_clipboard_.insert(mus::mojom::MIME_TYPE_MOZ_URL, | |
| 310 mojo::Array<uint8_t>::From(bookmark)); | |
| 311 } | |
| 312 | |
| 313 void ClipboardMus::WriteWebSmartPaste() { | |
| 314 current_clipboard_.insert(kMimeTypeWebkitSmartPaste, mojo::Array<uint8_t>()); | |
| 315 } | |
| 316 | |
| 317 void ClipboardMus::WriteBitmap(const SkBitmap& bitmap) { | |
| 318 // Encode the bitmap as a PNG for transport. | |
| 319 std::vector<unsigned char> output; | |
| 320 if (gfx::PNGCodec::FastEncodeBGRASkBitmap(bitmap, false, &output)) { | |
| 321 current_clipboard_.insert(mus::mojom::MIME_TYPE_PNG, | |
| 322 mojo::Array<uint8_t>::From(output)); | |
| 323 } | |
| 324 } | |
| 325 | |
| 326 void ClipboardMus::WriteData(const FormatType& format, | |
| 327 const char* data_data, | |
| 328 size_t data_len) { | |
| 329 current_clipboard_.insert( | |
| 330 GetMimeTypeFor(format), | |
| 331 mojo::Array<uint8_t>::From(base::StringPiece(data_data, data_len))); | |
| 332 } | |
| 333 | |
| 334 } // namespace views | |
| OLD | NEW |