Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / base / clipboard / custom_data_helper.cc
bloba50a0a072c712ce347e30466e3bddcdee5432525
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.
4 //
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"
11 #include <utility>
13 #include "base/pickle.h"
15 namespace ui {
17 namespace {
19 class SkippablePickle : public base::Pickle {
20 public:
21 SkippablePickle(const void* data, size_t data_len);
22 bool SkipString16(base::PickleIterator* iter);
25 SkippablePickle::SkippablePickle(const void* data, size_t data_len)
26 : base::Pickle(reinterpret_cast<const char*>(data), data_len) {
29 bool SkippablePickle::SkipString16(base::PickleIterator* iter) {
30 DCHECK(iter);
32 int len;
33 if (!iter->ReadLength(&len))
34 return false;
35 return iter->SkipBytes(len * sizeof(base::char16));
38 } // namespace
40 void ReadCustomDataTypes(const void* data,
41 size_t data_length,
42 std::vector<base::string16>* types) {
43 SkippablePickle pickle(data, data_length);
44 base::PickleIterator iter(pickle);
46 size_t size = 0;
47 if (!iter.ReadSizeT(&size))
48 return;
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);
59 return;
64 void ReadCustomDataForType(const void* data,
65 size_t data_length,
66 const base::string16& type,
67 base::string16* result) {
68 SkippablePickle pickle(data, data_length);
69 base::PickleIterator iter(pickle);
71 size_t size = 0;
72 if (!iter.ReadSizeT(&size))
73 return;
75 for (size_t i = 0; i < size; ++i) {
76 base::string16 deserialized_type;
77 if (!iter.ReadString16(&deserialized_type))
78 return;
79 if (deserialized_type == type) {
80 ignore_result(iter.ReadString16(result));
81 return;
83 if (!pickle.SkipString16(&iter))
84 return;
88 void ReadCustomDataIntoMap(const void* data,
89 size_t data_length,
90 std::map<base::string16, base::string16>* result) {
91 base::Pickle pickle(reinterpret_cast<const char*>(data), data_length);
92 base::PickleIterator iter(pickle);
94 size_t size = 0;
95 if (!iter.ReadSizeT(&size))
96 return;
98 for (size_t i = 0; i < size; ++i) {
99 base::string16 type;
100 if (!iter.ReadString16(&type)) {
101 // Data is corrupt, return an empty map.
102 result->clear();
103 return;
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.
109 result->clear();
110 return;
115 void WriteCustomDataToPickle(
116 const std::map<base::string16, base::string16>& data,
117 base::Pickle* pickle) {
118 pickle->WriteSizeT(data.size());
119 for (std::map<base::string16, base::string16>::const_iterator it =
120 data.begin();
121 it != data.end();
122 ++it) {
123 pickle->WriteString16(it->first);
124 pickle->WriteString16(it->second);
128 } // namespace ui