1 // Copyright (c) 2012 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 // TODO(dcheng): For efficiency reasons, consider passing custom data around
6 // as a vector instead. It allows us to append a
7 // std::pair<base::string16, base::string16> and swap the deserialized values.
9 #include "ui/base/clipboard/custom_data_helper.h"
13 #include "base/pickle.h"
19 class SkippablePickle
: public Pickle
{
21 SkippablePickle(const void* data
, size_t data_len
);
22 bool SkipString16(PickleIterator
* iter
);
25 SkippablePickle::SkippablePickle(const void* data
, size_t data_len
)
26 : Pickle(reinterpret_cast<const char*>(data
), data_len
) {
29 bool SkippablePickle::SkipString16(PickleIterator
* iter
) {
33 if (!iter
->ReadLength(&len
))
35 return iter
->SkipBytes(len
* sizeof(base::char16
));
40 void ReadCustomDataTypes(const void* data
,
42 std::vector
<base::string16
>* types
) {
43 SkippablePickle
pickle(data
, data_length
);
44 PickleIterator
iter(pickle
);
47 if (!iter
.ReadSizeT(&size
))
50 // Keep track of the original elements in the types vector. On failure, we
51 // truncate the vector to the original size since we want to ignore corrupt
52 // custom data pickles.
53 size_t original_size
= types
->size();
55 for (size_t i
= 0; i
< size
; ++i
) {
56 types
->push_back(base::string16());
57 if (!iter
.ReadString16(&types
->back()) || !pickle
.SkipString16(&iter
)) {
58 types
->resize(original_size
);
64 void ReadCustomDataForType(const void* data
,
66 const base::string16
& type
,
67 base::string16
* result
) {
68 SkippablePickle
pickle(data
, data_length
);
69 PickleIterator
iter(pickle
);
72 if (!iter
.ReadSizeT(&size
))
75 for (size_t i
= 0; i
< size
; ++i
) {
76 base::string16 deserialized_type
;
77 if (!iter
.ReadString16(&deserialized_type
))
79 if (deserialized_type
== type
) {
80 ignore_result(iter
.ReadString16(result
));
83 if (!pickle
.SkipString16(&iter
))
88 void ReadCustomDataIntoMap(const void* data
,
90 std::map
<base::string16
, base::string16
>* result
) {
91 Pickle
pickle(reinterpret_cast<const char*>(data
), data_length
);
92 PickleIterator
iter(pickle
);
95 if (!iter
.ReadSizeT(&size
))
98 for (size_t i
= 0; i
< size
; ++i
) {
100 if (!iter
.ReadString16(&type
)) {
101 // Data is corrupt, return an empty map.
105 std::pair
<std::map
<base::string16
, base::string16
>::iterator
, bool>
106 insert_result
= result
->insert(std::make_pair(type
, base::string16()));
107 if (!iter
.ReadString16(&insert_result
.first
->second
)) {
108 // Data is corrupt, return an empty map.
115 void WriteCustomDataToPickle(
116 const std::map
<base::string16
, base::string16
>& data
,
118 pickle
->WriteSizeT(data
.size());
119 for (std::map
<base::string16
, base::string16
>::const_iterator it
=
123 pickle
->WriteString16(it
->first
);
124 pickle
->WriteString16(it
->second
);