| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 "components/html_viewer/web_clipboard_impl.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "components/html_viewer/blink_basic_type_converters.h" | |
| 12 | |
| 13 using mojo::Array; | |
| 14 using mojo::Clipboard; | |
| 15 using mojo::Map; | |
| 16 using mojo::String; | |
| 17 | |
| 18 namespace html_viewer { | |
| 19 namespace { | |
| 20 | |
| 21 void CopyUint64(uint64_t* output, uint64_t input) { | |
| 22 *output = input; | |
| 23 } | |
| 24 | |
| 25 void CopyWebString(blink::WebString* output, const Array<uint8_t>& input) { | |
| 26 // blink does not differentiate between the requested data type not existing | |
| 27 // and the empty string. | |
| 28 if (input.is_null()) { | |
| 29 output->reset(); | |
| 30 } else { | |
| 31 *output = blink::WebString::fromUTF8( | |
| 32 reinterpret_cast<const char*>(&input.front()), | |
| 33 input.size()); | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 void CopyURL(blink::WebURL* pageURL, const Array<uint8_t>& input) { | |
| 38 if (input.is_null()) { | |
| 39 *pageURL = blink::WebURL(); | |
| 40 } else { | |
| 41 *pageURL = GURL(std::string(reinterpret_cast<const char*>(&input.front()), | |
| 42 input.size())); | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 void CopyVectorString(std::vector<std::string>* output, | |
| 47 const Array<String>& input) { | |
| 48 *output = input.To<std::vector<std::string> >(); | |
| 49 } | |
| 50 | |
| 51 template <typename T, typename U> | |
| 52 bool Contains(const std::vector<T>& v, const U& item) { | |
| 53 return std::find(v.begin(), v.end(), item) != v.end(); | |
| 54 } | |
| 55 | |
| 56 const char kMimeTypeWebkitSmartPaste[] = "chromium/x-webkit-paste"; | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 WebClipboardImpl::WebClipboardImpl(mojo::ClipboardPtr clipboard) | |
| 61 : clipboard_(std::move(clipboard)) {} | |
| 62 | |
| 63 WebClipboardImpl::~WebClipboardImpl() { | |
| 64 } | |
| 65 | |
| 66 uint64_t WebClipboardImpl::sequenceNumber(Buffer buffer) { | |
| 67 mojo::Clipboard::Type clipboard_type = ConvertBufferType(buffer); | |
| 68 | |
| 69 uint64_t number = 0; | |
| 70 clipboard_->GetSequenceNumber(clipboard_type, | |
| 71 base::Bind(&CopyUint64, &number)); | |
| 72 | |
| 73 // Force this to be synchronous. | |
| 74 clipboard_.WaitForIncomingResponse(); | |
| 75 return number; | |
| 76 } | |
| 77 | |
| 78 bool WebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) { | |
| 79 Clipboard::Type clipboard_type = ConvertBufferType(buffer); | |
| 80 | |
| 81 std::vector<std::string> types; | |
| 82 clipboard_->GetAvailableMimeTypes( | |
| 83 clipboard_type, base::Bind(&CopyVectorString, &types)); | |
| 84 | |
| 85 // Force this to be synchronous. | |
| 86 clipboard_.WaitForIncomingResponse(); | |
| 87 | |
| 88 switch (format) { | |
| 89 case FormatPlainText: | |
| 90 return Contains(types, Clipboard::MIME_TYPE_TEXT); | |
| 91 case FormatHTML: | |
| 92 return Contains(types, Clipboard::MIME_TYPE_HTML); | |
| 93 case FormatSmartPaste: | |
| 94 return Contains(types, kMimeTypeWebkitSmartPaste); | |
| 95 case FormatBookmark: | |
| 96 // This might be difficult. | |
| 97 return false; | |
| 98 } | |
| 99 | |
| 100 return false; | |
| 101 } | |
| 102 | |
| 103 blink::WebVector<blink::WebString> WebClipboardImpl::readAvailableTypes( | |
| 104 Buffer buffer, | |
| 105 bool* contains_filenames) { | |
| 106 Clipboard::Type clipboard_type = ConvertBufferType(buffer); | |
| 107 | |
| 108 std::vector<std::string> types; | |
| 109 clipboard_->GetAvailableMimeTypes( | |
| 110 clipboard_type, base::Bind(&CopyVectorString, &types)); | |
| 111 | |
| 112 // Force this to be synchronous. | |
| 113 clipboard_.WaitForIncomingResponse(); | |
| 114 | |
| 115 // AFAICT, every instance of setting contains_filenames is false. | |
| 116 *contains_filenames = false; | |
| 117 | |
| 118 blink::WebVector<blink::WebString> output(types.size()); | |
| 119 for (size_t i = 0; i < types.size(); ++i) { | |
| 120 output[i] = blink::WebString::fromUTF8(types[i]); | |
| 121 } | |
| 122 | |
| 123 return output; | |
| 124 } | |
| 125 | |
| 126 blink::WebString WebClipboardImpl::readPlainText(Buffer buffer) { | |
| 127 Clipboard::Type type = ConvertBufferType(buffer); | |
| 128 | |
| 129 blink::WebString text; | |
| 130 clipboard_->ReadMimeType(type, Clipboard::MIME_TYPE_TEXT, | |
| 131 base::Bind(&CopyWebString, &text)); | |
| 132 | |
| 133 // Force this to be synchronous. | |
| 134 clipboard_.WaitForIncomingResponse(); | |
| 135 | |
| 136 return text; | |
| 137 } | |
| 138 | |
| 139 blink::WebString WebClipboardImpl::readHTML(Buffer buffer, | |
| 140 blink::WebURL* page_url, | |
| 141 unsigned* fragment_start, | |
| 142 unsigned* fragment_end) { | |
| 143 Clipboard::Type type = ConvertBufferType(buffer); | |
| 144 | |
| 145 blink::WebString html; | |
| 146 clipboard_->ReadMimeType(type, Clipboard::MIME_TYPE_HTML, | |
| 147 base::Bind(&CopyWebString, &html)); | |
| 148 clipboard_.WaitForIncomingResponse(); | |
| 149 | |
| 150 *fragment_start = 0; | |
| 151 *fragment_end = static_cast<unsigned>(html.length()); | |
| 152 | |
| 153 clipboard_->ReadMimeType(type, Clipboard::MIME_TYPE_URL, | |
| 154 base::Bind(&CopyURL, page_url)); | |
| 155 clipboard_.WaitForIncomingResponse(); | |
| 156 | |
| 157 return html; | |
| 158 } | |
| 159 | |
| 160 blink::WebString WebClipboardImpl::readCustomData( | |
| 161 Buffer buffer, | |
| 162 const blink::WebString& mime_type) { | |
| 163 Clipboard::Type clipboard_type = ConvertBufferType(buffer); | |
| 164 | |
| 165 blink::WebString data; | |
| 166 clipboard_->ReadMimeType( | |
| 167 clipboard_type, mime_type.utf8(), base::Bind(&CopyWebString, &data)); | |
| 168 | |
| 169 // Force this to be synchronous. | |
| 170 clipboard_.WaitForIncomingResponse(); | |
| 171 | |
| 172 return data; | |
| 173 } | |
| 174 | |
| 175 void WebClipboardImpl::writePlainText(const blink::WebString& plain_text) { | |
| 176 Map<String, Array<uint8_t>> data; | |
| 177 data[Clipboard::MIME_TYPE_TEXT] = Array<uint8_t>::From(plain_text); | |
| 178 | |
| 179 clipboard_->WriteClipboardData(Clipboard::Type::COPY_PASTE, std::move(data)); | |
| 180 } | |
| 181 | |
| 182 void WebClipboardImpl::writeHTML(const blink::WebString& html_text, | |
| 183 const blink::WebURL& source_url, | |
| 184 const blink::WebString& plain_text, | |
| 185 bool writeSmartPaste) { | |
| 186 Map<String, Array<uint8_t>> data; | |
| 187 data[Clipboard::MIME_TYPE_TEXT] = Array<uint8_t>::From(plain_text); | |
| 188 data[Clipboard::MIME_TYPE_HTML] = Array<uint8_t>::From(html_text); | |
| 189 data[Clipboard::MIME_TYPE_URL] = Array<uint8_t>::From(source_url.string()); | |
| 190 | |
| 191 if (writeSmartPaste) | |
| 192 data[kMimeTypeWebkitSmartPaste] = Array<uint8_t>::From(blink::WebString()); | |
| 193 | |
| 194 clipboard_->WriteClipboardData(Clipboard::Type::COPY_PASTE, std::move(data)); | |
| 195 } | |
| 196 | |
| 197 Clipboard::Type WebClipboardImpl::ConvertBufferType(Buffer buffer) { | |
| 198 switch (buffer) { | |
| 199 case BufferStandard: | |
| 200 return Clipboard::Type::COPY_PASTE; | |
| 201 case BufferSelection: | |
| 202 return Clipboard::Type::SELECTION; | |
| 203 } | |
| 204 | |
| 205 NOTREACHED(); | |
| 206 return Clipboard::Type::COPY_PASTE; | |
| 207 } | |
| 208 | |
| 209 } // namespace html_viewer | |
| OLD | NEW |