| 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 // The source URL of copied HTML. |
| 35 const char kInternalSourceURL[] = "chromium/internal-url"; |
| 36 |
| 37 } // namespace |
| 38 |
| 39 ClipboardMus::ClipboardMus() {} |
| 40 |
| 41 ClipboardMus::~ClipboardMus() {} |
| 42 |
| 43 void ClipboardMus::Init(shell::Connector* connector) { |
| 44 connector->ConnectToInterface("mojo:mus", &clipboard_); |
| 45 } |
| 46 |
| 47 // TODO(erg): This isn't optimal. It would be better to move the entire |
| 48 // FormatType system to mime types throughout chrome, but that's a very large |
| 49 // change. |
| 50 mojo::String ClipboardMus::GetMimeTypeFor(const FormatType& format) { |
| 51 if (format.Equals(GetUrlFormatType()) || format.Equals(GetUrlWFormatType())) |
| 52 return mus::mojom::kMimeTypeURIList; |
| 53 if (format.Equals(GetMozUrlFormatType())) |
| 54 return mus::mojom::kMimeTypeMozillaURL; |
| 55 if (format.Equals(GetPlainTextFormatType()) || |
| 56 format.Equals(GetPlainTextWFormatType())) { |
| 57 return mus::mojom::kMimeTypeText; |
| 58 } |
| 59 if (format.Equals(GetHtmlFormatType())) |
| 60 return mus::mojom::kMimeTypeHTML; |
| 61 if (format.Equals(GetRtfFormatType())) |
| 62 return mus::mojom::kMimeTypeRTF; |
| 63 if (format.Equals(GetBitmapFormatType())) |
| 64 return mus::mojom::kMimeTypePNG; |
| 65 if (format.Equals(GetWebKitSmartPasteFormatType())) |
| 66 return kMimeTypeWebkitSmartPaste; |
| 67 if (format.Equals(GetWebCustomDataFormatType())) |
| 68 return kMimeTypeWebCustomData; |
| 69 if (format.Equals(GetPepperCustomDataFormatType())) |
| 70 return kMimeTypePepperCustomData; |
| 71 |
| 72 // TODO(erg): This isn't optimal, but it's the best we can do. On windows, |
| 73 // this will return strings that aren't MIME types, though they'll be |
| 74 // unique and should be serializable on the other side of the mojo |
| 75 // connection. |
| 76 return format.Serialize(); |
| 77 } |
| 78 |
| 79 bool ClipboardMus::HasMimeType(const mojo::Array<mojo::String>& available_types, |
| 80 const std::string& type) const { |
| 81 return ContainsValue(available_types, type); |
| 82 } |
| 83 |
| 84 uint64_t ClipboardMus::GetSequenceNumber(ui::ClipboardType type) const { |
| 85 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 86 uint64_t sequence_number = 0; |
| 87 clipboard_->GetSequenceNumber(GetType(type), &sequence_number); |
| 88 return sequence_number; |
| 89 } |
| 90 |
| 91 bool ClipboardMus::IsFormatAvailable(const FormatType& format, |
| 92 ui::ClipboardType type) const { |
| 93 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 94 |
| 95 uint64_t sequence_number = 0; |
| 96 mojo::Array<mojo::String> available_types; |
| 97 clipboard_->GetAvailableMimeTypes(GetType(type), &sequence_number, |
| 98 &available_types); |
| 99 |
| 100 mojo::String format_in_mime = GetMimeTypeFor(format); |
| 101 return ContainsValue(available_types, format_in_mime); |
| 102 } |
| 103 |
| 104 void ClipboardMus::Clear(ui::ClipboardType type) { |
| 105 // Sends the data to mus server. |
| 106 uint64_t sequence_number = 0; |
| 107 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 108 clipboard_->WriteClipboardData(GetType(type), nullptr, |
| 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::kMimeTypeText)) |
| 124 types->push_back(base::UTF8ToUTF16(mus::mojom::kMimeTypeText)); |
| 125 if (HasMimeType(available_types, mus::mojom::kMimeTypeHTML)) |
| 126 types->push_back(base::UTF8ToUTF16(mus::mojom::kMimeTypeHTML)); |
| 127 if (HasMimeType(available_types, mus::mojom::kMimeTypeRTF)) |
| 128 types->push_back(base::UTF8ToUTF16(mus::mojom::kMimeTypeRTF)); |
| 129 if (HasMimeType(available_types, mus::mojom::kMimeTypePNG)) |
| 130 types->push_back(base::UTF8ToUTF16(mus::mojom::kMimeTypePNG)); |
| 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::kMimeTypeText), |
| 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::kMimeTypeText), |
| 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::kMimeTypeHTML), |
| 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), kInternalSourceURL, |
| 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::kMimeTypeRTF), |
| 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::kMimeTypePNG), |
| 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 (const auto& p : objects) |
| 260 DispatchObject(static_cast<ObjectType>(p.first), p.second); |
| 261 |
| 262 // Sends the data to mus server. |
| 263 uint64_t sequence_number = 0; |
| 264 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 265 clipboard_->WriteClipboardData(GetType(type), std::move(*current_clipboard_), |
| 266 &sequence_number); |
| 267 current_clipboard_.reset(); |
| 268 } |
| 269 |
| 270 void ClipboardMus::WriteText(const char* text_data, size_t text_len) { |
| 271 DCHECK(current_clipboard_); |
| 272 current_clipboard_->insert( |
| 273 mus::mojom::kMimeTypeText, |
| 274 mojo::Array<uint8_t>::From(base::StringPiece(text_data, text_len))); |
| 275 } |
| 276 |
| 277 void ClipboardMus::WriteHTML(const char* markup_data, |
| 278 size_t markup_len, |
| 279 const char* url_data, |
| 280 size_t url_len) { |
| 281 DCHECK(current_clipboard_); |
| 282 current_clipboard_->insert( |
| 283 mus::mojom::kMimeTypeHTML, |
| 284 mojo::Array<uint8_t>::From(base::StringPiece(markup_data, markup_len))); |
| 285 if (url_len > 0) { |
| 286 current_clipboard_->insert( |
| 287 kInternalSourceURL, |
| 288 mojo::Array<uint8_t>::From(base::StringPiece(url_data, url_len))); |
| 289 } |
| 290 } |
| 291 |
| 292 void ClipboardMus::WriteRTF(const char* rtf_data, size_t data_len) { |
| 293 DCHECK(current_clipboard_); |
| 294 current_clipboard_->insert( |
| 295 mus::mojom::kMimeTypeRTF, |
| 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 DCHECK(current_clipboard_); |
| 310 current_clipboard_->insert(mus::mojom::kMimeTypeMozillaURL, |
| 311 mojo::Array<uint8_t>::From(bookmark)); |
| 312 } |
| 313 |
| 314 void ClipboardMus::WriteWebSmartPaste() { |
| 315 DCHECK(current_clipboard_); |
| 316 current_clipboard_->insert(kMimeTypeWebkitSmartPaste, mojo::Array<uint8_t>()); |
| 317 } |
| 318 |
| 319 void ClipboardMus::WriteBitmap(const SkBitmap& bitmap) { |
| 320 DCHECK(current_clipboard_); |
| 321 // Encode the bitmap as a PNG for transport. |
| 322 std::vector<unsigned char> output; |
| 323 if (gfx::PNGCodec::FastEncodeBGRASkBitmap(bitmap, false, &output)) { |
| 324 current_clipboard_->insert(mus::mojom::kMimeTypePNG, |
| 325 mojo::Array<uint8_t>::From(output)); |
| 326 } |
| 327 } |
| 328 |
| 329 void ClipboardMus::WriteData(const FormatType& format, |
| 330 const char* data_data, |
| 331 size_t data_len) { |
| 332 DCHECK(current_clipboard_); |
| 333 current_clipboard_->insert( |
| 334 GetMimeTypeFor(format), |
| 335 mojo::Array<uint8_t>::From(base::StringPiece(data_data, data_len))); |
| 336 } |
| 337 |
| 338 } // namespace views |
| OLD | NEW |