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