OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 "mojo/services/clipboard/clipboard_standalone_impl.h" |
| 6 |
| 7 namespace mojo { |
| 8 |
| 9 // ClipboardData contains data copied to the Clipboard for a variety of formats. |
| 10 // It mostly just provides APIs to cleanly access and manipulate this data. |
| 11 class ClipboardStandaloneImpl::ClipboardData { |
| 12 public: |
| 13 ClipboardData() {} |
| 14 virtual ~ClipboardData() {} |
| 15 |
| 16 std::vector<std::string> GetMimeTypes() const { |
| 17 std::vector<std::string> types; |
| 18 for (std::map<std::string, std::string>::const_iterator it = |
| 19 data_types_.begin(); |
| 20 it != data_types_.end(); |
| 21 ++it) { |
| 22 types.push_back(it->first); |
| 23 } |
| 24 |
| 25 return types; |
| 26 } |
| 27 |
| 28 void SetData(const std::string& mime_type, const std::string& data) { |
| 29 data_types_[mime_type] = data; |
| 30 } |
| 31 |
| 32 bool GetData(const std::string& mime_type, std::string* data) const { |
| 33 std::map<std::string, std::string>::const_iterator it = |
| 34 data_types_.find(mime_type); |
| 35 if (it != data_types_.end()) { |
| 36 *data = it->second; |
| 37 return true; |
| 38 } |
| 39 |
| 40 return false; |
| 41 } |
| 42 |
| 43 // Extended data for mime type "text/html". |
| 44 const std::string& html_url() const { return html_url_; } |
| 45 void set_html_url(const std::string& url) { html_url_ = url; } |
| 46 |
| 47 // TODO(erg): We also need to handle images. |
| 48 |
| 49 private: |
| 50 // Storage for all normal, one byte-string datatypes. Some datatypes also |
| 51 // specify an additional piece of data, which live below. |
| 52 std::map<std::string, std::string> data_types_; |
| 53 |
| 54 // The URL parameter on an HTML in UTF8 format. |
| 55 std::string html_url_; |
| 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(ClipboardData); |
| 58 }; |
| 59 |
| 60 ClipboardStandaloneImpl::ClipboardStandaloneImpl() { |
| 61 for (int i = 0; i < kNumClipboards; ++i) { |
| 62 sequence_number_[i] = 0; |
| 63 clipboard_state_[i].reset(new ClipboardData); |
| 64 queued_data_[i].reset(new ClipboardData); |
| 65 } |
| 66 } |
| 67 |
| 68 ClipboardStandaloneImpl::~ClipboardStandaloneImpl() { |
| 69 } |
| 70 |
| 71 void ClipboardStandaloneImpl::GetSequenceNumber( |
| 72 Clipboard::Type clipboard_type, |
| 73 const mojo::Callback<void(uint64_t)>& callback) { |
| 74 callback.Run(sequence_number_[clipboard_type]); |
| 75 } |
| 76 |
| 77 void ClipboardStandaloneImpl::GetAvailableFormatMimeTypes( |
| 78 Clipboard::Type clipboard_type, |
| 79 const mojo::Callback<void(mojo::Array<mojo::String>)>& callback) { |
| 80 mojo::Array<mojo::String> types = mojo::Array<mojo::String>::From( |
| 81 clipboard_state_[clipboard_type]->GetMimeTypes()); |
| 82 callback.Run(types.Pass()); |
| 83 } |
| 84 |
| 85 void ClipboardStandaloneImpl::ReadPlainText( |
| 86 Clipboard::Type clipboard_type, |
| 87 const mojo::Callback<void(mojo::String)>& callback) { |
| 88 std::string text; |
| 89 clipboard_state_[clipboard_type]->GetData(mojo::Clipboard::MIME_TYPE_TEXT, |
| 90 &text); |
| 91 callback.Run(text); |
| 92 } |
| 93 |
| 94 void ClipboardStandaloneImpl::ReadHTML( |
| 95 Clipboard::Type clipboard_type, |
| 96 const mojo::Callback<void(mojo::String, mojo::String, uint32_t, uint32_t)>& |
| 97 callback) { |
| 98 std::string html_text; |
| 99 if (clipboard_state_[clipboard_type]->GetData(mojo::Clipboard::MIME_TYPE_HTML, |
| 100 &html_text)) { |
| 101 // Note: the fragment positioning is apparently relevant on Windows, because |
| 102 // the system passes additional context back to us. It's not relevant here, |
| 103 // but still needs to be part of the interface. |
| 104 uint32_t html_end = static_cast<uint32_t>(html_text.size()); |
| 105 callback.Run( |
| 106 html_text, clipboard_state_[clipboard_type]->html_url(), 0, html_end); |
| 107 return; |
| 108 } |
| 109 |
| 110 // Send back an empty. |
| 111 callback.Run(std::string(), std::string(), 0, 0); |
| 112 } |
| 113 |
| 114 void ClipboardStandaloneImpl::ReadMIMEType( |
| 115 Clipboard::Type clipboard_type, |
| 116 const mojo::String& mime_type, |
| 117 const mojo::Callback<void(mojo::String)>& callback) { |
| 118 std::string mime_data; |
| 119 clipboard_state_[clipboard_type]->GetData(mime_type.To<std::string>(), |
| 120 &mime_data); |
| 121 callback.Run(mime_data); |
| 122 } |
| 123 |
| 124 void ClipboardStandaloneImpl::QueueWritePlainText( |
| 125 Clipboard::Type clipboard_type, |
| 126 const mojo::String& text) { |
| 127 queued_data_[clipboard_type]->SetData(mojo::Clipboard::MIME_TYPE_TEXT, |
| 128 text.To<std::string>()); |
| 129 } |
| 130 |
| 131 void ClipboardStandaloneImpl::QueueWriteHTML(Clipboard::Type clipboard_type, |
| 132 const mojo::String& html_text, |
| 133 const mojo::String& url) { |
| 134 queued_data_[clipboard_type]->SetData(mojo::Clipboard::MIME_TYPE_HTML, |
| 135 html_text.To<std::string>()); |
| 136 queued_data_[clipboard_type]->set_html_url(url.To<std::string>()); |
| 137 } |
| 138 |
| 139 void ClipboardStandaloneImpl::QueueWriteMIMEType(Clipboard::Type clipboard_type, |
| 140 const mojo::String& mime_type, |
| 141 const mojo::String& data) { |
| 142 queued_data_[clipboard_type]->SetData(mime_type.To<std::string>(), |
| 143 data.To<std::string>()); |
| 144 } |
| 145 |
| 146 void ClipboardStandaloneImpl::WriteQueuedData(Clipboard::Type clipboard_type) { |
| 147 sequence_number_[clipboard_type]++; |
| 148 clipboard_state_[clipboard_type] = queued_data_[clipboard_type].Pass(); |
| 149 queued_data_[clipboard_type].reset(new ClipboardData); |
| 150 } |
| 151 |
| 152 } // namespace mojo |
OLD | NEW |