Index: mojo/services/clipboard/clipboard_standalone_impl.cc |
diff --git a/mojo/services/clipboard/clipboard_standalone_impl.cc b/mojo/services/clipboard/clipboard_standalone_impl.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..257d60c036e0bfa4c999ccfa25f7150ef1b156da |
--- /dev/null |
+++ b/mojo/services/clipboard/clipboard_standalone_impl.cc |
@@ -0,0 +1,152 @@ |
+// Copyright (c) 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/clipboard/clipboard_standalone_impl.h" |
+ |
+namespace mojo { |
+ |
+// ClipboardData contains data copied to the Clipboard for a variety of formats. |
+// It mostly just provides APIs to cleanly access and manipulate this data. |
+class ClipboardStandaloneImpl::ClipboardData { |
+ public: |
+ ClipboardData() {} |
+ virtual ~ClipboardData() {} |
+ |
+ std::vector<std::string> GetMimeTypes() const { |
+ std::vector<std::string> types; |
+ for (std::map<std::string, std::string>::const_iterator it = |
+ data_types_.begin(); |
+ it != data_types_.end(); |
+ ++it) { |
+ types.push_back(it->first); |
+ } |
+ |
+ return types; |
+ } |
+ |
+ void SetData(const std::string& mime_type, const std::string& data) { |
+ data_types_[mime_type] = data; |
+ } |
+ |
+ bool GetData(const std::string& mime_type, std::string* data) const { |
+ std::map<std::string, std::string>::const_iterator it = |
+ data_types_.find(mime_type); |
+ if (it != data_types_.end()) { |
+ *data = it->second; |
+ return true; |
+ } |
+ |
+ return false; |
+ } |
+ |
+ // Extended data for mime type "text/html". |
+ const std::string& html_url() const { return html_url_; } |
+ void set_html_url(const std::string& url) { html_url_ = url; } |
+ |
+ // TODO(erg): We also need to handle images. |
+ |
+ private: |
+ // Storage for all normal, one byte-string datatypes. Some datatypes also |
+ // specify an additional piece of data, which live below. |
+ std::map<std::string, std::string> data_types_; |
+ |
+ // The URL parameter on an HTML in UTF8 format. |
+ std::string html_url_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ClipboardData); |
+}; |
+ |
+ClipboardStandaloneImpl::ClipboardStandaloneImpl() { |
+ for (int i = 0; i < kNumClipboards; ++i) { |
+ sequence_number_[i] = 0; |
+ clipboard_state_[i].reset(new ClipboardData); |
+ queued_data_[i].reset(new ClipboardData); |
+ } |
+} |
+ |
+ClipboardStandaloneImpl::~ClipboardStandaloneImpl() { |
+} |
+ |
+void ClipboardStandaloneImpl::GetSequenceNumber( |
+ Clipboard::Type clipboard_type, |
+ const mojo::Callback<void(uint64_t)>& callback) { |
+ callback.Run(sequence_number_[clipboard_type]); |
+} |
+ |
+void ClipboardStandaloneImpl::GetAvailableFormatMimeTypes( |
+ Clipboard::Type clipboard_type, |
+ const mojo::Callback<void(mojo::Array<mojo::String>)>& callback) { |
+ mojo::Array<mojo::String> types = mojo::Array<mojo::String>::From( |
+ clipboard_state_[clipboard_type]->GetMimeTypes()); |
+ callback.Run(types.Pass()); |
+} |
+ |
+void ClipboardStandaloneImpl::ReadPlainText( |
+ Clipboard::Type clipboard_type, |
+ const mojo::Callback<void(mojo::String)>& callback) { |
+ std::string text; |
+ clipboard_state_[clipboard_type]->GetData(mojo::Clipboard::MIME_TYPE_TEXT, |
+ &text); |
+ callback.Run(text); |
+} |
+ |
+void ClipboardStandaloneImpl::ReadHTML( |
+ Clipboard::Type clipboard_type, |
+ const mojo::Callback<void(mojo::String, mojo::String, uint32_t, uint32_t)>& |
+ callback) { |
+ std::string html_text; |
+ if (clipboard_state_[clipboard_type]->GetData(mojo::Clipboard::MIME_TYPE_HTML, |
+ &html_text)) { |
+ // Note: the fragment positioning is apparently relevant on Windows, because |
+ // the system passes additional context back to us. It's not relevant here, |
+ // but still needs to be part of the interface. |
+ uint32_t html_end = static_cast<uint32_t>(html_text.size()); |
+ callback.Run( |
+ html_text, clipboard_state_[clipboard_type]->html_url(), 0, html_end); |
+ return; |
+ } |
+ |
+ // Send back an empty. |
+ callback.Run(std::string(), std::string(), 0, 0); |
+} |
+ |
+void ClipboardStandaloneImpl::ReadMIMEType( |
+ Clipboard::Type clipboard_type, |
+ const mojo::String& mime_type, |
+ const mojo::Callback<void(mojo::String)>& callback) { |
+ std::string mime_data; |
+ clipboard_state_[clipboard_type]->GetData(mime_type.To<std::string>(), |
+ &mime_data); |
+ callback.Run(mime_data); |
+} |
+ |
+void ClipboardStandaloneImpl::QueueWritePlainText( |
+ Clipboard::Type clipboard_type, |
+ const mojo::String& text) { |
+ queued_data_[clipboard_type]->SetData(mojo::Clipboard::MIME_TYPE_TEXT, |
+ text.To<std::string>()); |
+} |
+ |
+void ClipboardStandaloneImpl::QueueWriteHTML(Clipboard::Type clipboard_type, |
+ const mojo::String& html_text, |
+ const mojo::String& url) { |
+ queued_data_[clipboard_type]->SetData(mojo::Clipboard::MIME_TYPE_HTML, |
+ html_text.To<std::string>()); |
+ queued_data_[clipboard_type]->set_html_url(url.To<std::string>()); |
+} |
+ |
+void ClipboardStandaloneImpl::QueueWriteMIMEType(Clipboard::Type clipboard_type, |
+ const mojo::String& mime_type, |
+ const mojo::String& data) { |
+ queued_data_[clipboard_type]->SetData(mime_type.To<std::string>(), |
+ data.To<std::string>()); |
+} |
+ |
+void ClipboardStandaloneImpl::WriteQueuedData(Clipboard::Type clipboard_type) { |
+ sequence_number_[clipboard_type]++; |
+ clipboard_state_[clipboard_type] = queued_data_[clipboard_type].Pass(); |
+ queued_data_[clipboard_type].reset(new ClipboardData); |
+} |
+ |
+} // namespace mojo |