Chromium Code Reviews| 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 typedef std::vector<uint8_t> ByteVector; | |
| 10 | |
| 11 // ClipboardData contains data copied to the Clipboard for a variety of formats. | |
| 12 // It mostly just provides APIs to cleanly access and manipulate this data. | |
| 13 class ClipboardStandaloneImpl::ClipboardData { | |
| 14 public: | |
| 15 ClipboardData() {} | |
| 16 virtual ~ClipboardData() {} | |
|
sky
2014/09/15 23:16:52
nit: no virtual.
| |
| 17 | |
| 18 std::vector<std::string> GetMimeTypes() const { | |
| 19 std::vector<std::string> types; | |
| 20 for (std::map<std::string, ByteVector>::const_iterator it = | |
| 21 data_types_.begin(); | |
| 22 it != data_types_.end(); | |
| 23 ++it) { | |
| 24 types.push_back(it->first); | |
| 25 } | |
| 26 | |
| 27 return types; | |
| 28 } | |
| 29 | |
| 30 void SetData(std::map<std::string, ByteVector>* data) { | |
| 31 std::swap(data_types_, *data); | |
| 32 } | |
| 33 | |
| 34 bool GetData(const std::string& mime_type, ByteVector* data) const { | |
| 35 std::map<std::string, ByteVector>::const_iterator it = | |
| 36 data_types_.find(mime_type); | |
| 37 if (it != data_types_.end()) { | |
| 38 *data = it->second; | |
| 39 return true; | |
| 40 } | |
| 41 | |
| 42 return false; | |
| 43 } | |
| 44 | |
| 45 private: | |
| 46 // Storage for all normal, one byte-string datatypes. Some datatypes also | |
|
sky
2014/09/15 23:16:52
This comment seems out of date.
| |
| 47 // specify an additional piece of data, which live below. | |
| 48 std::map<std::string, ByteVector> data_types_; | |
| 49 | |
| 50 DISALLOW_COPY_AND_ASSIGN(ClipboardData); | |
| 51 }; | |
| 52 | |
| 53 ClipboardStandaloneImpl::ClipboardStandaloneImpl() { | |
| 54 for (int i = 0; i < kNumClipboards; ++i) { | |
| 55 sequence_number_[i] = 0; | |
| 56 clipboard_state_[i].reset(new ClipboardData); | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 ClipboardStandaloneImpl::~ClipboardStandaloneImpl() { | |
| 61 } | |
| 62 | |
| 63 void ClipboardStandaloneImpl::GetSequenceNumber( | |
| 64 Clipboard::Type clipboard_type, | |
| 65 const mojo::Callback<void(uint64_t)>& callback) { | |
| 66 callback.Run(sequence_number_[clipboard_type]); | |
| 67 } | |
| 68 | |
| 69 void ClipboardStandaloneImpl::GetAvailableMimeTypes( | |
| 70 Clipboard::Type clipboard_type, | |
| 71 const mojo::Callback<void(mojo::Array<mojo::String>)>& callback) { | |
| 72 mojo::Array<mojo::String> types = mojo::Array<mojo::String>::From( | |
| 73 clipboard_state_[clipboard_type]->GetMimeTypes()); | |
|
sky
2014/09/15 23:16:52
Do we guarantee enum values start at 0?
Elliot Glaysher
2014/09/15 23:30:14
I believe we do, but just to make it explicit, I'v
| |
| 74 callback.Run(types.Pass()); | |
| 75 } | |
| 76 | |
| 77 void ClipboardStandaloneImpl::ReadMimeType( | |
| 78 Clipboard::Type clipboard_type, | |
| 79 const mojo::String& mime_type, | |
| 80 const mojo::Callback<void(mojo::Array<uint8_t>)>& callback) { | |
| 81 ByteVector mime_data; | |
| 82 if (clipboard_state_[clipboard_type]->GetData( | |
| 83 mime_type.To<std::string>(), &mime_data)) { | |
| 84 callback.Run(mojo::Array<uint8_t>::From(mime_data).Pass()); | |
|
sky
2014/09/15 23:16:52
Should this supply NULL so that the caller knows t
Elliot Glaysher
2014/09/15 23:30:14
I thought we do supply NULL later on ("callback.Ru
| |
| 85 return; | |
| 86 } | |
| 87 | |
| 88 callback.Run(mojo::Array<uint8_t>().Pass()); | |
| 89 } | |
| 90 | |
| 91 void ClipboardStandaloneImpl::WriteClipboardData( | |
| 92 Clipboard::Type clipboard_type, | |
| 93 mojo::Array<MimeTypePairPtr> data) { | |
| 94 std::map<std::string, ByteVector> mime_data; | |
| 95 for (size_t i = 0; i < data.size(); ++i) | |
| 96 mime_data[data[i]->mime_type] = data[i]->data; | |
| 97 | |
| 98 sequence_number_[clipboard_type]++; | |
| 99 clipboard_state_[clipboard_type]->SetData(&mime_data); | |
| 100 } | |
| 101 | |
| 102 } // namespace mojo | |
| OLD | NEW |