1 // Copyright 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.
5 #include "components/clipboard/clipboard_standalone_impl.h"
9 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h"
10 #include "third_party/mojo/src/mojo/public/cpp/bindings/callback.h"
11 #include "third_party/mojo/src/mojo/public/cpp/bindings/string.h"
19 // ClipboardData contains data copied to the Clipboard for a variety of formats.
20 // It mostly just provides APIs to cleanly access and manipulate this data.
21 class ClipboardStandaloneImpl::ClipboardData
{
26 Array
<String
> GetMimeTypes() const {
27 Array
<String
> types(data_types_
.size());
29 for (auto it
= data_types_
.begin(); it
!= data_types_
.end(); ++it
, ++i
)
30 types
[i
] = it
.GetKey();
35 void SetData(Map
<String
, Array
<uint8_t>> data
) { data_types_
= data
.Pass(); }
37 void GetData(const String
& mime_type
, Array
<uint8_t>* data
) const {
38 auto it
= data_types_
.find(mime_type
);
39 if (it
!= data_types_
.end())
40 *data
= it
.GetValue().Clone();
44 Map
<String
, Array
<uint8_t>> data_types_
;
46 DISALLOW_COPY_AND_ASSIGN(ClipboardData
);
49 ClipboardStandaloneImpl::ClipboardStandaloneImpl(
50 mojo::InterfaceRequest
<mojo::Clipboard
> request
)
51 : binding_(this, request
.Pass()) {
52 for (int i
= 0; i
< kNumClipboards
; ++i
) {
53 sequence_number_
[i
] = 0;
54 clipboard_state_
[i
].reset(new ClipboardData
);
58 ClipboardStandaloneImpl::~ClipboardStandaloneImpl() {
61 void ClipboardStandaloneImpl::GetSequenceNumber(
62 Clipboard::Type clipboard_type
,
63 const mojo::Callback
<void(uint64_t)>& callback
) {
64 callback
.Run(sequence_number_
[clipboard_type
]);
67 void ClipboardStandaloneImpl::GetAvailableMimeTypes(
68 Clipboard::Type clipboard_type
,
69 const mojo::Callback
<void(Array
<String
>)>& callback
) {
70 callback
.Run(clipboard_state_
[clipboard_type
]->GetMimeTypes().Pass());
73 void ClipboardStandaloneImpl::ReadMimeType(
74 Clipboard::Type clipboard_type
,
75 const String
& mime_type
,
76 const mojo::Callback
<void(Array
<uint8_t>)>& callback
) {
77 Array
<uint8_t> mime_data
;
78 clipboard_state_
[clipboard_type
]->GetData(mime_type
, &mime_data
);
79 callback
.Run(mime_data
.Pass());
82 void ClipboardStandaloneImpl::WriteClipboardData(
83 Clipboard::Type clipboard_type
,
84 Map
<String
, Array
<uint8_t>> data
) {
85 sequence_number_
[clipboard_type
]++;
86 clipboard_state_
[clipboard_type
]->SetData(data
.Pass());
89 } // namespace clipboard