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"; | |
|
dcheng
2016/06/04 06:14:00
Either name this to reflect or comment that this i
| |
| 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 // Sends the data to mus server. | |
| 105 uint64_t sequence_number = 0; | |
| 106 mojo::Map<mojo::String, mojo::Array<uint8_t>> null_data(nullptr); | |
| 107 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 108 clipboard_->WriteClipboardData(GetType(type), std::move(null_data), | |
|
dcheng
2016/06/04 06:14:00
You can probably just use nullptr directly as the
| |
| 109 &sequence_number); | |
| 110 } | |
| 111 | |
| 112 void ClipboardMus::ReadAvailableTypes(ui::ClipboardType type, | |
| 113 std::vector<base::string16>* types, | |
| 114 bool* contains_filenames) const { | |
| 115 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 116 | |
| 117 uint64_t sequence_number = 0; | |
| 118 mojo::Array<mojo::String> available_types; | |
| 119 clipboard_->GetAvailableMimeTypes(GetType(type), &sequence_number, | |
| 120 &available_types); | |
| 121 | |
| 122 types->clear(); | |
| 123 if (HasMimeType(available_types, mus::mojom::MIME_TYPE_TEXT)) | |
| 124 types->push_back(base::UTF8ToUTF16(mus::mojom::MIME_TYPE_TEXT)); | |
| 125 if (HasMimeType(available_types, mus::mojom::MIME_TYPE_HTML)) | |
| 126 types->push_back(base::UTF8ToUTF16(mus::mojom::MIME_TYPE_HTML)); | |
| 127 if (HasMimeType(available_types, mus::mojom::MIME_TYPE_RTF)) | |
| 128 types->push_back(base::UTF8ToUTF16(mus::mojom::MIME_TYPE_RTF)); | |
| 129 if (HasMimeType(available_types, mus::mojom::MIME_TYPE_PNG)) | |
| 130 types->push_back(base::UTF8ToUTF16(mus::mojom::MIME_TYPE_PNG)); | |
| 131 | |
| 132 if (HasMimeType(available_types, kMimeTypeWebCustomData)) { | |
| 133 mojo::Array<uint8_t> custom_data; | |
| 134 uint64_t sequence_number = 0; | |
| 135 if (clipboard_->ReadClipboardData(GetType(type), kMimeTypeWebCustomData, | |
| 136 &sequence_number, &custom_data)) { | |
| 137 ui::ReadCustomDataTypes(&custom_data.front(), custom_data.size(), types); | |
| 138 } | |
| 139 } | |
| 140 | |
| 141 *contains_filenames = false; | |
| 142 } | |
| 143 | |
| 144 void ClipboardMus::ReadText(ui::ClipboardType type, | |
| 145 base::string16* result) const { | |
| 146 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 147 mojo::Array<uint8_t> text_data; | |
| 148 uint64_t sequence_number = 0; | |
| 149 if (clipboard_->ReadClipboardData(GetType(type), | |
| 150 mojo::String(mus::mojom::MIME_TYPE_TEXT), | |
| 151 &sequence_number, &text_data)) { | |
| 152 std::string text = text_data.To<std::string>(); | |
| 153 *result = base::UTF8ToUTF16(text); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 void ClipboardMus::ReadAsciiText(ui::ClipboardType type, | |
| 158 std::string* result) const { | |
| 159 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 160 mojo::Array<uint8_t> text_data; | |
| 161 uint64_t sequence_number = 0; | |
| 162 if (clipboard_->ReadClipboardData(GetType(type), | |
| 163 mojo::String(mus::mojom::MIME_TYPE_TEXT), | |
| 164 &sequence_number, &text_data)) { | |
| 165 *result = text_data.To<std::string>(); | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 void ClipboardMus::ReadHTML(ui::ClipboardType type, | |
| 170 base::string16* markup, | |
| 171 std::string* src_url, | |
| 172 uint32_t* fragment_start, | |
| 173 uint32_t* fragment_end) const { | |
| 174 markup->clear(); | |
| 175 if (src_url) | |
| 176 src_url->clear(); | |
| 177 *fragment_start = 0; | |
| 178 *fragment_end = 0; | |
| 179 | |
| 180 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 181 mojo::Array<uint8_t> html_data; | |
| 182 uint64_t sequence_number = 0; | |
| 183 if (clipboard_->ReadClipboardData(GetType(type), | |
| 184 mojo::String(mus::mojom::MIME_TYPE_HTML), | |
| 185 &sequence_number, &html_data)) { | |
| 186 *markup = base::UTF8ToUTF16(html_data.To<std::string>()); | |
| 187 *fragment_end = static_cast<uint32_t>(markup->length()); | |
| 188 | |
| 189 // We only bother fetching the source url if we were the ones who wrote | |
| 190 // this html data to the clipboard. | |
| 191 mojo::Array<uint8_t> url_data; | |
| 192 if (clipboard_->ReadClipboardData(GetType(type), kInternalURL, | |
| 193 &sequence_number, &url_data)) { | |
| 194 *src_url = url_data.To<std::string>(); | |
| 195 } | |
| 196 } | |
| 197 } | |
| 198 | |
| 199 void ClipboardMus::ReadRTF(ui::ClipboardType type, std::string* result) const { | |
| 200 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 201 mojo::Array<uint8_t> rtf_data; | |
| 202 uint64_t sequence_number = 0; | |
| 203 if (clipboard_->ReadClipboardData( | |
| 204 GetType(type), mojo::String(mus::mojom::MIME_TYPE_RTF), | |
| 205 &sequence_number, &rtf_data)) { | |
| 206 *result = rtf_data.To<std::string>(); | |
| 207 } | |
| 208 } | |
| 209 | |
| 210 SkBitmap ClipboardMus::ReadImage(ui::ClipboardType type) const { | |
| 211 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 212 mojo::Array<uint8_t> data; | |
| 213 uint64_t sequence_number = 0; | |
| 214 if (clipboard_->ReadClipboardData( | |
| 215 GetType(type), mojo::String(mus::mojom::MIME_TYPE_PNG), | |
| 216 &sequence_number, &data)) { | |
| 217 SkBitmap bitmap; | |
| 218 if (gfx::PNGCodec::Decode(&data.front(), data.size(), &bitmap)) | |
| 219 return SkBitmap(bitmap); | |
| 220 } | |
| 221 | |
| 222 return SkBitmap(); | |
| 223 } | |
| 224 | |
| 225 void ClipboardMus::ReadCustomData(ui::ClipboardType clipboard_type, | |
| 226 const base::string16& type, | |
| 227 base::string16* result) const { | |
| 228 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 229 mojo::Array<uint8_t> custom_data; | |
| 230 uint64_t sequence_number = 0; | |
| 231 if (clipboard_->ReadClipboardData(GetType(clipboard_type), | |
| 232 mojo::String(kMimeTypeWebCustomData), | |
| 233 &sequence_number, &custom_data)) { | |
| 234 ui::ReadCustomDataForType(&custom_data.front(), custom_data.size(), type, | |
| 235 result); | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 void ClipboardMus::ReadBookmark(base::string16* title, std::string* url) const { | |
| 240 // TODO(erg): This is NOTIMPLEMENTED() on all linux platforms? | |
| 241 NOTIMPLEMENTED(); | |
| 242 } | |
| 243 | |
| 244 void ClipboardMus::ReadData(const FormatType& format, | |
| 245 std::string* result) const { | |
| 246 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 247 mojo::Array<uint8_t> data; | |
| 248 uint64_t sequence_number = 0; | |
| 249 if (clipboard_->ReadClipboardData(mus::mojom::Clipboard::Type::COPY_PASTE, | |
| 250 GetMimeTypeFor(format), | |
| 251 &sequence_number, &data)) { | |
| 252 *result = data.To<std::string>(); | |
| 253 } | |
| 254 } | |
| 255 | |
| 256 void ClipboardMus::WriteObjects(ui::ClipboardType type, | |
| 257 const ObjectMap& objects) { | |
| 258 current_clipboard_.reset(new mojo::Map<mojo::String, mojo::Array<uint8_t>>); | |
| 259 for (ObjectMap::const_iterator iter = objects.begin(); iter != objects.end(); | |
|
dcheng
2016/06/04 06:14:00
for (const auto& object : objects)
DispatchObjec
| |
| 260 ++iter) { | |
| 261 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); | |
| 262 } | |
| 263 | |
| 264 // Sends the data to mus server. | |
| 265 uint64_t sequence_number = 0; | |
| 266 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 267 clipboard_->WriteClipboardData(GetType(type), std::move(*current_clipboard_), | |
| 268 &sequence_number); | |
| 269 current_clipboard_.reset(); | |
| 270 } | |
| 271 | |
| 272 void ClipboardMus::WriteText(const char* text_data, size_t text_len) { | |
| 273 DCHECK(current_clipboard_); | |
| 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 DCHECK(current_clipboard_); | |
| 284 current_clipboard_->insert( | |
| 285 mus::mojom::MIME_TYPE_HTML, | |
| 286 mojo::Array<uint8_t>::From(base::StringPiece(markup_data, markup_len))); | |
| 287 if (url_len > 0) { | |
| 288 current_clipboard_->insert( | |
| 289 kInternalURL, | |
| 290 mojo::Array<uint8_t>::From(base::StringPiece(url_data, url_len))); | |
| 291 } | |
| 292 } | |
| 293 | |
| 294 void ClipboardMus::WriteRTF(const char* rtf_data, size_t data_len) { | |
| 295 DCHECK(current_clipboard_); | |
| 296 current_clipboard_->insert( | |
| 297 mus::mojom::MIME_TYPE_RTF, | |
| 298 mojo::Array<uint8_t>::From(base::StringPiece(rtf_data, data_len))); | |
| 299 } | |
| 300 | |
| 301 void ClipboardMus::WriteBookmark(const char* title_data, | |
| 302 size_t title_len, | |
| 303 const char* url_data, | |
| 304 size_t url_len) { | |
| 305 // Writes a Mozilla url (UTF16: URL, newline, title) | |
| 306 base::string16 bookmark = | |
| 307 base::UTF8ToUTF16(base::StringPiece(url_data, url_len)) + | |
| 308 base::ASCIIToUTF16("\n") + | |
| 309 base::UTF8ToUTF16(base::StringPiece(title_data, title_len)); | |
| 310 | |
| 311 DCHECK(current_clipboard_); | |
| 312 current_clipboard_->insert(mus::mojom::MIME_TYPE_MOZ_URL, | |
| 313 mojo::Array<uint8_t>::From(bookmark)); | |
| 314 } | |
| 315 | |
| 316 void ClipboardMus::WriteWebSmartPaste() { | |
| 317 DCHECK(current_clipboard_); | |
| 318 current_clipboard_->insert(kMimeTypeWebkitSmartPaste, mojo::Array<uint8_t>()); | |
| 319 } | |
| 320 | |
| 321 void ClipboardMus::WriteBitmap(const SkBitmap& bitmap) { | |
| 322 DCHECK(current_clipboard_); | |
| 323 // Encode the bitmap as a PNG for transport. | |
| 324 std::vector<unsigned char> output; | |
| 325 if (gfx::PNGCodec::FastEncodeBGRASkBitmap(bitmap, false, &output)) { | |
| 326 current_clipboard_->insert(mus::mojom::MIME_TYPE_PNG, | |
| 327 mojo::Array<uint8_t>::From(output)); | |
| 328 } | |
| 329 } | |
| 330 | |
| 331 void ClipboardMus::WriteData(const FormatType& format, | |
| 332 const char* data_data, | |
| 333 size_t data_len) { | |
| 334 DCHECK(current_clipboard_); | |
| 335 current_clipboard_->insert( | |
| 336 GetMimeTypeFor(format), | |
| 337 mojo::Array<uint8_t>::From(base::StringPiece(data_data, data_len))); | |
| 338 } | |
| 339 | |
| 340 } // namespace views | |
| OLD | NEW |