Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1161)

Unified Diff: mojo/services/html_viewer/webclipboard_impl.cc

Issue 562483002: mojo: Create a basic clipboard. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase to ToT to fix patch apply. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..d0f11a01c20541f617341071a5081b2c274a1aa4
--- /dev/null
+++ b/mojo/services/html_viewer/webclipboard_impl.cc
@@ -0,0 +1,206 @@
+// 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) {
+ // blink does not differentiate between the requested data type not existing
+ // and the empty string.
+ *output = input.is_null() ? "" : input.To<std::string>();
+}
+
+void CopyVectorString(std::vector<std::string>* output,
+ const Array<String>& input) {
+ *output = input.To<std::vector<std::string> >();
+}
+
+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_->ReadMIMEType(
+ type, mojo::Clipboard::MIME_TYPE_TEXT, 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 html;
+ clipboard_->ReadMIMEType(
+ type, mojo::Clipboard::MIME_TYPE_HTML, base::Bind(&CopyString, &html));
+ clipboard_.WaitForIncomingMethodCall();
+
+ *fragmentStart = 0;
+ *fragmentEnd = html.size();
+
+ std::string url;
+ clipboard_->ReadMIMEType(
+ type, mojo::Clipboard::MIME_TYPE_URL, base::Bind(&CopyString, &url));
+ clipboard_.WaitForIncomingMethodCall();
+ *pageURL = GURL(url);
+
+ return blink::WebString::fromUTF8(html);
+}
+
+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) {
+ Array<MimeTypePairPtr> data;
+ MimeTypePairPtr text_data(MimeTypePair::New());
+ text_data->mime_type = mojo::Clipboard::MIME_TYPE_TEXT;
+ text_data->data = text.utf8();
+ data.push_back(text_data.Pass());
+
+ clipboard_->WriteClipboardData(mojo::Clipboard::TYPE_COPY_PASTE, data.Pass());
+}
+
+void WebClipboardImpl::writeHTML(const blink::WebString& htmlText,
+ const blink::WebURL& url,
+ const blink::WebString& plainText,
+ bool writeSmartPaste) {
+ Array<MimeTypePairPtr> data;
+ MimeTypePairPtr text_data(MimeTypePair::New());
+ text_data->mime_type = mojo::Clipboard::MIME_TYPE_TEXT;
+ text_data->data = plainText.utf8();
+ data.push_back(text_data.Pass());
+
+ MimeTypePairPtr html_data(MimeTypePair::New());
+ text_data->mime_type = mojo::Clipboard::MIME_TYPE_HTML;
+ text_data->data = htmlText.utf8();
+ data.push_back(html_data.Pass());
+
+ MimeTypePairPtr url_data(MimeTypePair::New());
+ url_data->mime_type = mojo::Clipboard::MIME_TYPE_URL;
+ url_data->data = url.string().utf8();
+ data.push_back(url_data.Pass());
+
+ if (writeSmartPaste) {
+ MimeTypePairPtr smart_paste(MimeTypePair::New());
+ url_data->mime_type = kMimeTypeWebkitSmartPaste;
+ url_data->data = "";
+ data.push_back(smart_paste.Pass());
+ }
+
+ clipboard_->WriteClipboardData(mojo::Clipboard::TYPE_COPY_PASTE, data.Pass());
+}
+
+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

Powered by Google App Engine
This is Rietveld 408576698