| 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 "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/threading/thread_restrictions.h" | |
| 11 #include "mojo/common/common_type_converters.h" | 10 #include "mojo/common/common_type_converters.h" |
| 11 #include "mojo/public/cpp/bindings/sync_call_restrictions.h" |
| 12 #include "services/shell/public/cpp/connector.h" | 12 #include "services/shell/public/cpp/connector.h" |
| 13 #include "third_party/skia/include/core/SkBitmap.h" | 13 #include "third_party/skia/include/core/SkBitmap.h" |
| 14 #include "ui/base/clipboard/custom_data_helper.h" | 14 #include "ui/base/clipboard/custom_data_helper.h" |
| 15 #include "ui/gfx/codec/png_codec.h" | 15 #include "ui/gfx/codec/png_codec.h" |
| 16 | 16 |
| 17 namespace views { | 17 namespace views { |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 mus::mojom::Clipboard::Type GetType(ui::ClipboardType type) { | 20 mus::mojom::Clipboard::Type GetType(ui::ClipboardType type) { |
| 21 switch (type) { | 21 switch (type) { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 // connection. | 75 // connection. |
| 76 return format.Serialize(); | 76 return format.Serialize(); |
| 77 } | 77 } |
| 78 | 78 |
| 79 bool ClipboardMus::HasMimeType(const mojo::Array<mojo::String>& available_types, | 79 bool ClipboardMus::HasMimeType(const mojo::Array<mojo::String>& available_types, |
| 80 const std::string& type) const { | 80 const std::string& type) const { |
| 81 return ContainsValue(available_types, type); | 81 return ContainsValue(available_types, type); |
| 82 } | 82 } |
| 83 | 83 |
| 84 uint64_t ClipboardMus::GetSequenceNumber(ui::ClipboardType type) const { | 84 uint64_t ClipboardMus::GetSequenceNumber(ui::ClipboardType type) const { |
| 85 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 85 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
| 86 uint64_t sequence_number = 0; | 86 uint64_t sequence_number = 0; |
| 87 clipboard_->GetSequenceNumber(GetType(type), &sequence_number); | 87 clipboard_->GetSequenceNumber(GetType(type), &sequence_number); |
| 88 return sequence_number; | 88 return sequence_number; |
| 89 } | 89 } |
| 90 | 90 |
| 91 bool ClipboardMus::IsFormatAvailable(const FormatType& format, | 91 bool ClipboardMus::IsFormatAvailable(const FormatType& format, |
| 92 ui::ClipboardType type) const { | 92 ui::ClipboardType type) const { |
| 93 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 93 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
| 94 | 94 |
| 95 uint64_t sequence_number = 0; | 95 uint64_t sequence_number = 0; |
| 96 mojo::Array<mojo::String> available_types; | 96 mojo::Array<mojo::String> available_types; |
| 97 clipboard_->GetAvailableMimeTypes(GetType(type), &sequence_number, | 97 clipboard_->GetAvailableMimeTypes(GetType(type), &sequence_number, |
| 98 &available_types); | 98 &available_types); |
| 99 | 99 |
| 100 mojo::String format_in_mime = GetMimeTypeFor(format); | 100 mojo::String format_in_mime = GetMimeTypeFor(format); |
| 101 return ContainsValue(available_types, format_in_mime); | 101 return ContainsValue(available_types, format_in_mime); |
| 102 } | 102 } |
| 103 | 103 |
| 104 void ClipboardMus::Clear(ui::ClipboardType type) { | 104 void ClipboardMus::Clear(ui::ClipboardType type) { |
| 105 // Sends the data to mus server. | 105 // Sends the data to mus server. |
| 106 uint64_t sequence_number = 0; | 106 uint64_t sequence_number = 0; |
| 107 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 107 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
| 108 clipboard_->WriteClipboardData(GetType(type), nullptr, | 108 clipboard_->WriteClipboardData(GetType(type), nullptr, |
| 109 &sequence_number); | 109 &sequence_number); |
| 110 } | 110 } |
| 111 | 111 |
| 112 void ClipboardMus::ReadAvailableTypes(ui::ClipboardType type, | 112 void ClipboardMus::ReadAvailableTypes(ui::ClipboardType type, |
| 113 std::vector<base::string16>* types, | 113 std::vector<base::string16>* types, |
| 114 bool* contains_filenames) const { | 114 bool* contains_filenames) const { |
| 115 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 115 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
| 116 | 116 |
| 117 uint64_t sequence_number = 0; | 117 uint64_t sequence_number = 0; |
| 118 mojo::Array<mojo::String> available_types; | 118 mojo::Array<mojo::String> available_types; |
| 119 clipboard_->GetAvailableMimeTypes(GetType(type), &sequence_number, | 119 clipboard_->GetAvailableMimeTypes(GetType(type), &sequence_number, |
| 120 &available_types); | 120 &available_types); |
| 121 | 121 |
| 122 types->clear(); | 122 types->clear(); |
| 123 if (HasMimeType(available_types, mus::mojom::kMimeTypeText)) | 123 if (HasMimeType(available_types, mus::mojom::kMimeTypeText)) |
| 124 types->push_back(base::UTF8ToUTF16(mus::mojom::kMimeTypeText)); | 124 types->push_back(base::UTF8ToUTF16(mus::mojom::kMimeTypeText)); |
| 125 if (HasMimeType(available_types, mus::mojom::kMimeTypeHTML)) | 125 if (HasMimeType(available_types, mus::mojom::kMimeTypeHTML)) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 136 &sequence_number, &custom_data)) { | 136 &sequence_number, &custom_data)) { |
| 137 ui::ReadCustomDataTypes(&custom_data.front(), custom_data.size(), types); | 137 ui::ReadCustomDataTypes(&custom_data.front(), custom_data.size(), types); |
| 138 } | 138 } |
| 139 } | 139 } |
| 140 | 140 |
| 141 *contains_filenames = false; | 141 *contains_filenames = false; |
| 142 } | 142 } |
| 143 | 143 |
| 144 void ClipboardMus::ReadText(ui::ClipboardType type, | 144 void ClipboardMus::ReadText(ui::ClipboardType type, |
| 145 base::string16* result) const { | 145 base::string16* result) const { |
| 146 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 146 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
| 147 mojo::Array<uint8_t> text_data; | 147 mojo::Array<uint8_t> text_data; |
| 148 uint64_t sequence_number = 0; | 148 uint64_t sequence_number = 0; |
| 149 if (clipboard_->ReadClipboardData(GetType(type), | 149 if (clipboard_->ReadClipboardData(GetType(type), |
| 150 mojo::String(mus::mojom::kMimeTypeText), | 150 mojo::String(mus::mojom::kMimeTypeText), |
| 151 &sequence_number, &text_data)) { | 151 &sequence_number, &text_data)) { |
| 152 std::string text = text_data.To<std::string>(); | 152 std::string text = text_data.To<std::string>(); |
| 153 *result = base::UTF8ToUTF16(text); | 153 *result = base::UTF8ToUTF16(text); |
| 154 } | 154 } |
| 155 } | 155 } |
| 156 | 156 |
| 157 void ClipboardMus::ReadAsciiText(ui::ClipboardType type, | 157 void ClipboardMus::ReadAsciiText(ui::ClipboardType type, |
| 158 std::string* result) const { | 158 std::string* result) const { |
| 159 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 159 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
| 160 mojo::Array<uint8_t> text_data; | 160 mojo::Array<uint8_t> text_data; |
| 161 uint64_t sequence_number = 0; | 161 uint64_t sequence_number = 0; |
| 162 if (clipboard_->ReadClipboardData(GetType(type), | 162 if (clipboard_->ReadClipboardData(GetType(type), |
| 163 mojo::String(mus::mojom::kMimeTypeText), | 163 mojo::String(mus::mojom::kMimeTypeText), |
| 164 &sequence_number, &text_data)) { | 164 &sequence_number, &text_data)) { |
| 165 *result = text_data.To<std::string>(); | 165 *result = text_data.To<std::string>(); |
| 166 } | 166 } |
| 167 } | 167 } |
| 168 | 168 |
| 169 void ClipboardMus::ReadHTML(ui::ClipboardType type, | 169 void ClipboardMus::ReadHTML(ui::ClipboardType type, |
| 170 base::string16* markup, | 170 base::string16* markup, |
| 171 std::string* src_url, | 171 std::string* src_url, |
| 172 uint32_t* fragment_start, | 172 uint32_t* fragment_start, |
| 173 uint32_t* fragment_end) const { | 173 uint32_t* fragment_end) const { |
| 174 markup->clear(); | 174 markup->clear(); |
| 175 if (src_url) | 175 if (src_url) |
| 176 src_url->clear(); | 176 src_url->clear(); |
| 177 *fragment_start = 0; | 177 *fragment_start = 0; |
| 178 *fragment_end = 0; | 178 *fragment_end = 0; |
| 179 | 179 |
| 180 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 180 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
| 181 mojo::Array<uint8_t> html_data; | 181 mojo::Array<uint8_t> html_data; |
| 182 uint64_t sequence_number = 0; | 182 uint64_t sequence_number = 0; |
| 183 if (clipboard_->ReadClipboardData(GetType(type), | 183 if (clipboard_->ReadClipboardData(GetType(type), |
| 184 mojo::String(mus::mojom::kMimeTypeHTML), | 184 mojo::String(mus::mojom::kMimeTypeHTML), |
| 185 &sequence_number, &html_data)) { | 185 &sequence_number, &html_data)) { |
| 186 *markup = base::UTF8ToUTF16(html_data.To<std::string>()); | 186 *markup = base::UTF8ToUTF16(html_data.To<std::string>()); |
| 187 *fragment_end = static_cast<uint32_t>(markup->length()); | 187 *fragment_end = static_cast<uint32_t>(markup->length()); |
| 188 | 188 |
| 189 // We only bother fetching the source url if we were the ones who wrote | 189 // We only bother fetching the source url if we were the ones who wrote |
| 190 // this html data to the clipboard. | 190 // this html data to the clipboard. |
| 191 mojo::Array<uint8_t> url_data; | 191 mojo::Array<uint8_t> url_data; |
| 192 if (clipboard_->ReadClipboardData(GetType(type), kInternalSourceURL, | 192 if (clipboard_->ReadClipboardData(GetType(type), kInternalSourceURL, |
| 193 &sequence_number, &url_data)) { | 193 &sequence_number, &url_data)) { |
| 194 *src_url = url_data.To<std::string>(); | 194 *src_url = url_data.To<std::string>(); |
| 195 } | 195 } |
| 196 } | 196 } |
| 197 } | 197 } |
| 198 | 198 |
| 199 void ClipboardMus::ReadRTF(ui::ClipboardType type, std::string* result) const { | 199 void ClipboardMus::ReadRTF(ui::ClipboardType type, std::string* result) const { |
| 200 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 200 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
| 201 mojo::Array<uint8_t> rtf_data; | 201 mojo::Array<uint8_t> rtf_data; |
| 202 uint64_t sequence_number = 0; | 202 uint64_t sequence_number = 0; |
| 203 if (clipboard_->ReadClipboardData( | 203 if (clipboard_->ReadClipboardData( |
| 204 GetType(type), mojo::String(mus::mojom::kMimeTypeRTF), | 204 GetType(type), mojo::String(mus::mojom::kMimeTypeRTF), |
| 205 &sequence_number, &rtf_data)) { | 205 &sequence_number, &rtf_data)) { |
| 206 *result = rtf_data.To<std::string>(); | 206 *result = rtf_data.To<std::string>(); |
| 207 } | 207 } |
| 208 } | 208 } |
| 209 | 209 |
| 210 SkBitmap ClipboardMus::ReadImage(ui::ClipboardType type) const { | 210 SkBitmap ClipboardMus::ReadImage(ui::ClipboardType type) const { |
| 211 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 211 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
| 212 mojo::Array<uint8_t> data; | 212 mojo::Array<uint8_t> data; |
| 213 uint64_t sequence_number = 0; | 213 uint64_t sequence_number = 0; |
| 214 if (clipboard_->ReadClipboardData( | 214 if (clipboard_->ReadClipboardData( |
| 215 GetType(type), mojo::String(mus::mojom::kMimeTypePNG), | 215 GetType(type), mojo::String(mus::mojom::kMimeTypePNG), |
| 216 &sequence_number, &data)) { | 216 &sequence_number, &data)) { |
| 217 SkBitmap bitmap; | 217 SkBitmap bitmap; |
| 218 if (gfx::PNGCodec::Decode(&data.front(), data.size(), &bitmap)) | 218 if (gfx::PNGCodec::Decode(&data.front(), data.size(), &bitmap)) |
| 219 return SkBitmap(bitmap); | 219 return SkBitmap(bitmap); |
| 220 } | 220 } |
| 221 | 221 |
| 222 return SkBitmap(); | 222 return SkBitmap(); |
| 223 } | 223 } |
| 224 | 224 |
| 225 void ClipboardMus::ReadCustomData(ui::ClipboardType clipboard_type, | 225 void ClipboardMus::ReadCustomData(ui::ClipboardType clipboard_type, |
| 226 const base::string16& type, | 226 const base::string16& type, |
| 227 base::string16* result) const { | 227 base::string16* result) const { |
| 228 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 228 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
| 229 mojo::Array<uint8_t> custom_data; | 229 mojo::Array<uint8_t> custom_data; |
| 230 uint64_t sequence_number = 0; | 230 uint64_t sequence_number = 0; |
| 231 if (clipboard_->ReadClipboardData(GetType(clipboard_type), | 231 if (clipboard_->ReadClipboardData(GetType(clipboard_type), |
| 232 mojo::String(kMimeTypeWebCustomData), | 232 mojo::String(kMimeTypeWebCustomData), |
| 233 &sequence_number, &custom_data)) { | 233 &sequence_number, &custom_data)) { |
| 234 ui::ReadCustomDataForType(&custom_data.front(), custom_data.size(), type, | 234 ui::ReadCustomDataForType(&custom_data.front(), custom_data.size(), type, |
| 235 result); | 235 result); |
| 236 } | 236 } |
| 237 } | 237 } |
| 238 | 238 |
| 239 void ClipboardMus::ReadBookmark(base::string16* title, std::string* url) const { | 239 void ClipboardMus::ReadBookmark(base::string16* title, std::string* url) const { |
| 240 // TODO(erg): This is NOTIMPLEMENTED() on all linux platforms? | 240 // TODO(erg): This is NOTIMPLEMENTED() on all linux platforms? |
| 241 NOTIMPLEMENTED(); | 241 NOTIMPLEMENTED(); |
| 242 } | 242 } |
| 243 | 243 |
| 244 void ClipboardMus::ReadData(const FormatType& format, | 244 void ClipboardMus::ReadData(const FormatType& format, |
| 245 std::string* result) const { | 245 std::string* result) const { |
| 246 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 246 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
| 247 mojo::Array<uint8_t> data; | 247 mojo::Array<uint8_t> data; |
| 248 uint64_t sequence_number = 0; | 248 uint64_t sequence_number = 0; |
| 249 if (clipboard_->ReadClipboardData(mus::mojom::Clipboard::Type::COPY_PASTE, | 249 if (clipboard_->ReadClipboardData(mus::mojom::Clipboard::Type::COPY_PASTE, |
| 250 GetMimeTypeFor(format), | 250 GetMimeTypeFor(format), |
| 251 &sequence_number, &data)) { | 251 &sequence_number, &data)) { |
| 252 *result = data.To<std::string>(); | 252 *result = data.To<std::string>(); |
| 253 } | 253 } |
| 254 } | 254 } |
| 255 | 255 |
| 256 void ClipboardMus::WriteObjects(ui::ClipboardType type, | 256 void ClipboardMus::WriteObjects(ui::ClipboardType type, |
| 257 const ObjectMap& objects) { | 257 const ObjectMap& objects) { |
| 258 current_clipboard_.reset(new mojo::Map<mojo::String, mojo::Array<uint8_t>>); | 258 current_clipboard_.reset(new mojo::Map<mojo::String, mojo::Array<uint8_t>>); |
| 259 for (const auto& p : objects) | 259 for (const auto& p : objects) |
| 260 DispatchObject(static_cast<ObjectType>(p.first), p.second); | 260 DispatchObject(static_cast<ObjectType>(p.first), p.second); |
| 261 | 261 |
| 262 // Sends the data to mus server. | 262 // Sends the data to mus server. |
| 263 uint64_t sequence_number = 0; | 263 uint64_t sequence_number = 0; |
| 264 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 264 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync_call; |
| 265 clipboard_->WriteClipboardData(GetType(type), std::move(*current_clipboard_), | 265 clipboard_->WriteClipboardData(GetType(type), std::move(*current_clipboard_), |
| 266 &sequence_number); | 266 &sequence_number); |
| 267 current_clipboard_.reset(); | 267 current_clipboard_.reset(); |
| 268 } | 268 } |
| 269 | 269 |
| 270 void ClipboardMus::WriteText(const char* text_data, size_t text_len) { | 270 void ClipboardMus::WriteText(const char* text_data, size_t text_len) { |
| 271 DCHECK(current_clipboard_); | 271 DCHECK(current_clipboard_); |
| 272 current_clipboard_->insert( | 272 current_clipboard_->insert( |
| 273 mus::mojom::kMimeTypeText, | 273 mus::mojom::kMimeTypeText, |
| 274 mojo::Array<uint8_t>::From(base::StringPiece(text_data, text_len))); | 274 mojo::Array<uint8_t>::From(base::StringPiece(text_data, text_len))); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 void ClipboardMus::WriteData(const FormatType& format, | 329 void ClipboardMus::WriteData(const FormatType& format, |
| 330 const char* data_data, | 330 const char* data_data, |
| 331 size_t data_len) { | 331 size_t data_len) { |
| 332 DCHECK(current_clipboard_); | 332 DCHECK(current_clipboard_); |
| 333 current_clipboard_->insert( | 333 current_clipboard_->insert( |
| 334 GetMimeTypeFor(format), | 334 GetMimeTypeFor(format), |
| 335 mojo::Array<uint8_t>::From(base::StringPiece(data_data, data_len))); | 335 mojo::Array<uint8_t>::From(base::StringPiece(data_data, data_len))); |
| 336 } | 336 } |
| 337 | 337 |
| 338 } // namespace views | 338 } // namespace views |
| OLD | NEW |