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:
ClipboardData()15 ClipboardData() {}
~ClipboardData()16 ~ClipboardData() {}
17
GetMimeTypes() const18 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
SetData(std::map<std::string,ByteVector> * data)30 void SetData(std::map<std::string, ByteVector>* data) {
31 std::swap(data_types_, *data);
32 }
33
GetData(const std::string & mime_type,ByteVector * data) const34 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 std::map<std::string, ByteVector> data_types_;
47
48 DISALLOW_COPY_AND_ASSIGN(ClipboardData);
49 };
50
ClipboardStandaloneImpl()51 ClipboardStandaloneImpl::ClipboardStandaloneImpl() {
52 for (int i = 0; i < kNumClipboards; ++i) {
53 sequence_number_[i] = 0;
54 clipboard_state_[i].reset(new ClipboardData);
55 }
56 }
57
~ClipboardStandaloneImpl()58 ClipboardStandaloneImpl::~ClipboardStandaloneImpl() {
59 }
60
GetSequenceNumber(Clipboard::Type clipboard_type,const mojo::Callback<void (uint64_t)> & callback)61 void ClipboardStandaloneImpl::GetSequenceNumber(
62 Clipboard::Type clipboard_type,
63 const mojo::Callback<void(uint64_t)>& callback) {
64 callback.Run(sequence_number_[clipboard_type]);
65 }
66
GetAvailableMimeTypes(Clipboard::Type clipboard_type,const mojo::Callback<void (mojo::Array<mojo::String>)> & callback)67 void ClipboardStandaloneImpl::GetAvailableMimeTypes(
68 Clipboard::Type clipboard_type,
69 const mojo::Callback<void(mojo::Array<mojo::String>)>& callback) {
70 mojo::Array<mojo::String> types = mojo::Array<mojo::String>::From(
71 clipboard_state_[clipboard_type]->GetMimeTypes());
72 callback.Run(types.Pass());
73 }
74
ReadMimeType(Clipboard::Type clipboard_type,const mojo::String & mime_type,const mojo::Callback<void (mojo::Array<uint8_t>)> & callback)75 void ClipboardStandaloneImpl::ReadMimeType(
76 Clipboard::Type clipboard_type,
77 const mojo::String& mime_type,
78 const mojo::Callback<void(mojo::Array<uint8_t>)>& callback) {
79 ByteVector mime_data;
80 if (clipboard_state_[clipboard_type]->GetData(
81 mime_type.To<std::string>(), &mime_data)) {
82 callback.Run(mojo::Array<uint8_t>::From(mime_data).Pass());
83 return;
84 }
85
86 callback.Run(mojo::Array<uint8_t>().Pass());
87 }
88
WriteClipboardData(Clipboard::Type clipboard_type,mojo::Array<MimeTypePairPtr> data)89 void ClipboardStandaloneImpl::WriteClipboardData(
90 Clipboard::Type clipboard_type,
91 mojo::Array<MimeTypePairPtr> data) {
92 std::map<std::string, ByteVector> mime_data;
93 for (size_t i = 0; i < data.size(); ++i)
94 mime_data[data[i]->mime_type] = data[i]->data;
95
96 sequence_number_[clipboard_type]++;
97 clipboard_state_[clipboard_type]->SetData(&mime_data);
98 }
99
100 } // namespace mojo
101