Index: mojo/services/html_viewer/webclipboard_impl.cc |
diff --git a/mojo/services/html_viewer/webclipboard_impl.cc b/mojo/services/html_viewer/webclipboard_impl.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4cb2c2250cd106489dfd30effb74e32804ef9c60 |
--- /dev/null |
+++ b/mojo/services/html_viewer/webclipboard_impl.cc |
@@ -0,0 +1,200 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "mojo/services/html_viewer/webclipboard_impl.h" |
+ |
+#include "base/bind.h" |
+ |
+namespace mojo { |
+namespace { |
+ |
+void CopyUint64(uint64_t* output, uint64_t input) { |
+ *output = input; |
+} |
+ |
+void CopyString(std::string* output, const mojo::String& input) { |
+ *output = input.To<std::string>(); |
+} |
+ |
+void CopyVectorString(std::vector<std::string>* output, |
+ const Array<String>& input) { |
+ *output = input.To<std::vector<std::string> >(); |
+} |
+ |
+// Work around having an odd arrity in base/bind.h.pump. |
+struct HTMLOutput { |
+ std::string* text_output; |
+ blink::WebURL* url_output; |
+ unsigned* start_output; |
+ unsigned* end_output; |
+}; |
+void CopyHTMLReturnValues(HTMLOutput* output, |
+ const mojo::String& text_input, |
+ const mojo::String& url_input, |
+ uint32_t start_input, |
+ uint32_t end_input) { |
+ *output->text_output = text_input.To<std::string>(); |
+ *output->url_output = GURL(url_input.To<std::string>()); |
+ *output->start_output = start_input; |
+ *output->end_output = end_input; |
+} |
+ |
+template <typename T, typename U> |
+bool Contains(const std::vector<T>& v, const U& item) { |
+ return std::find(v.begin(), v.end(), item) != v.end(); |
+} |
+ |
+const char kMimeTypeWebkitSmartPaste[] = "chromium/x-webkit-paste"; |
+ |
+} // namespace |
+ |
+WebClipboardImpl::WebClipboardImpl(ClipboardPtr clipboard) |
+ : clipboard_(clipboard.Pass()) { |
+} |
+ |
+WebClipboardImpl::~WebClipboardImpl() { |
+} |
+ |
+uint64_t WebClipboardImpl::sequenceNumber(Buffer buffer) { |
+ mojo::Clipboard::Type clipboard_type = ConvertBufferType(buffer); |
+ |
+ uint64_t number = 0; |
+ clipboard_->GetSequenceNumber(clipboard_type, |
+ base::Bind(&CopyUint64, &number)); |
+ |
+ // Force this to be synchronous. |
+ clipboard_.WaitForIncomingMethodCall(); |
+ return number; |
+} |
+ |
+bool WebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) { |
+ mojo::Clipboard::Type clipboard_type = ConvertBufferType(buffer); |
+ |
+ std::vector<std::string> types; |
+ clipboard_->GetAvailableFormatMimeTypes( |
+ clipboard_type, base::Bind(&CopyVectorString, &types)); |
+ |
+ // Force this to be synchronous. |
+ clipboard_.WaitForIncomingMethodCall(); |
+ |
+ switch (format) { |
+ case FormatPlainText: |
+ return Contains(types, mojo::Clipboard::MIME_TYPE_TEXT); |
+ case FormatHTML: |
+ return Contains(types, mojo::Clipboard::MIME_TYPE_HTML); |
+ case FormatSmartPaste: |
+ return Contains(types, kMimeTypeWebkitSmartPaste); |
+ case FormatBookmark: |
+ // This might be difficult. |
+ return false; |
+ } |
+ |
+ return false; |
+} |
+ |
+blink::WebVector<blink::WebString> WebClipboardImpl::readAvailableTypes( |
+ Buffer buffer, |
+ bool* containsFilenames) { |
+ mojo::Clipboard::Type clipboard_type = ConvertBufferType(buffer); |
+ |
+ std::vector<std::string> types; |
+ clipboard_->GetAvailableFormatMimeTypes( |
+ clipboard_type, base::Bind(&CopyVectorString, &types)); |
+ |
+ // Force this to be synchronous. |
+ clipboard_.WaitForIncomingMethodCall(); |
+ |
+ // AFAICT, every instance of setting containsFilenames is false. |
+ *containsFilenames = false; |
+ |
+ blink::WebVector<blink::WebString> output(types.size()); |
+ for (size_t i = 0; i < types.size(); ++i) { |
+ output[i] = blink::WebString::fromUTF8(types[i]); |
+ } |
+ |
+ return output; |
+} |
+ |
+blink::WebString WebClipboardImpl::readPlainText(Buffer buffer) { |
+ mojo::Clipboard::Type type = ConvertBufferType(buffer); |
+ |
+ std::string text; |
+ clipboard_->ReadPlainText(type, base::Bind(&CopyString, &text)); |
+ |
+ // Force this to be synchronous. |
+ clipboard_.WaitForIncomingMethodCall(); |
+ |
+ return blink::WebString::fromUTF8(text); |
+} |
+ |
+blink::WebString WebClipboardImpl::readHTML(Buffer buffer, |
+ blink::WebURL* pageURL, |
+ unsigned* fragmentStart, |
+ unsigned* fragmentEnd) { |
+ mojo::Clipboard::Type type = ConvertBufferType(buffer); |
+ |
+ std::string text; |
+ struct HTMLOutput output = {&text, pageURL, fragmentStart, fragmentEnd}; |
+ clipboard_->ReadHTML(type, base::Bind(&CopyHTMLReturnValues, &output)); |
+ |
+ // Force this to be synchronous. |
+ clipboard_.WaitForIncomingMethodCall(); |
+ |
+ return blink::WebString::fromUTF8(text); |
+} |
+ |
+blink::WebString WebClipboardImpl::readCustomData( |
+ Buffer buffer, |
+ const blink::WebString& mime_type) { |
+ mojo::Clipboard::Type clipboard_type = ConvertBufferType(buffer); |
+ |
+ std::string data; |
+ clipboard_->ReadMIMEType( |
+ clipboard_type, mime_type.utf8(), base::Bind(&CopyString, &data)); |
+ |
+ // Force this to be synchronous. |
+ clipboard_.WaitForIncomingMethodCall(); |
+ |
+ return blink::WebString::fromUTF8(data); |
+} |
+ |
+void WebClipboardImpl::writePlainText(const blink::WebString& text) { |
+ // TODO(erg): What about the target buffer here!? |
+ clipboard_->QueueWritePlainText(mojo::Clipboard::TYPE_COPY_PASTE, |
+ text.utf8()); |
+ |
+ clipboard_->WriteQueuedData(mojo::Clipboard::TYPE_COPY_PASTE); |
+} |
+ |
+void WebClipboardImpl::writeHTML(const blink::WebString& htmlText, |
+ const blink::WebURL& url, |
+ const blink::WebString& plainText, |
+ bool writeSmartPaste) { |
+ clipboard_->QueueWriteHTML( |
+ mojo::Clipboard::TYPE_COPY_PASTE, htmlText.utf8(), url.string().utf8()); |
+ clipboard_->QueueWritePlainText(mojo::Clipboard::TYPE_COPY_PASTE, |
+ plainText.utf8()); |
+ |
+ if (writeSmartPaste) { |
+ clipboard_->QueueWriteMIMEType(mojo::Clipboard::TYPE_COPY_PASTE, |
+ kMimeTypeWebkitSmartPaste, |
+ std::string()); |
+ } |
+ |
+ clipboard_->WriteQueuedData(mojo::Clipboard::TYPE_COPY_PASTE); |
+} |
+ |
+mojo::Clipboard::Type WebClipboardImpl::ConvertBufferType(Buffer buffer) { |
+ switch (buffer) { |
+ case BufferStandard: |
+ return mojo::Clipboard::TYPE_COPY_PASTE; |
+ case BufferSelection: |
+ return mojo::Clipboard::TYPE_SELECTION; |
+ } |
+ |
+ NOTREACHED(); |
+ return mojo::Clipboard::TYPE_COPY_PASTE; |
+} |
+ |
+} // namespace mojo |