OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // TODO(dcheng): For efficiency reasons, consider passing custom data around | 5 // TODO(dcheng): For efficiency reasons, consider passing custom data around |
6 // as a vector instead. It allows us to append a | 6 // as a vector instead. It allows us to append a |
7 // std::pair<base::string16, base::string16> and swap the deserialized values. | 7 // std::pair<base::string16, base::string16> and swap the deserialized values. |
8 | 8 |
9 #include "ui/base/clipboard/custom_data_helper.h" | 9 #include "ui/base/clipboard/custom_data_helper.h" |
10 | 10 |
(...skipping 12 matching lines...) Expand all Loading... |
23 }; | 23 }; |
24 | 24 |
25 SkippablePickle::SkippablePickle(const void* data, size_t data_len) | 25 SkippablePickle::SkippablePickle(const void* data, size_t data_len) |
26 : Pickle(reinterpret_cast<const char*>(data), data_len) { | 26 : Pickle(reinterpret_cast<const char*>(data), data_len) { |
27 } | 27 } |
28 | 28 |
29 bool SkippablePickle::SkipString16(PickleIterator* iter) { | 29 bool SkippablePickle::SkipString16(PickleIterator* iter) { |
30 DCHECK(iter); | 30 DCHECK(iter); |
31 | 31 |
32 int len; | 32 int len; |
33 if (!ReadLength(iter, &len)) | 33 if (!iter->ReadLength(&len)) |
34 return false; | 34 return false; |
35 return iter->SkipBytes(len * sizeof(base::char16)); | 35 return iter->SkipBytes(len * sizeof(base::char16)); |
36 } | 36 } |
37 | 37 |
38 } // namespace | 38 } // namespace |
39 | 39 |
40 void ReadCustomDataTypes(const void* data, | 40 void ReadCustomDataTypes(const void* data, |
41 size_t data_length, | 41 size_t data_length, |
42 std::vector<base::string16>* types) { | 42 std::vector<base::string16>* types) { |
43 SkippablePickle pickle(data, data_length); | 43 SkippablePickle pickle(data, data_length); |
44 PickleIterator iter(pickle); | 44 PickleIterator iter(pickle); |
45 | 45 |
46 size_t size = 0; | 46 size_t size = 0; |
47 if (!pickle.ReadSizeT(&iter, &size)) | 47 if (!iter.ReadSizeT(&size)) |
48 return; | 48 return; |
49 | 49 |
50 // Keep track of the original elements in the types vector. On failure, we | 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 | 51 // truncate the vector to the original size since we want to ignore corrupt |
52 // custom data pickles. | 52 // custom data pickles. |
53 size_t original_size = types->size(); | 53 size_t original_size = types->size(); |
54 | 54 |
55 for (size_t i = 0; i < size; ++i) { | 55 for (size_t i = 0; i < size; ++i) { |
56 types->push_back(base::string16()); | 56 types->push_back(base::string16()); |
57 if (!pickle.ReadString16(&iter, &types->back()) || | 57 if (!iter.ReadString16(&types->back()) || !pickle.SkipString16(&iter)) { |
58 !pickle.SkipString16(&iter)) { | |
59 types->resize(original_size); | 58 types->resize(original_size); |
60 return; | 59 return; |
61 } | 60 } |
62 } | 61 } |
63 } | 62 } |
64 | 63 |
65 void ReadCustomDataForType(const void* data, | 64 void ReadCustomDataForType(const void* data, |
66 size_t data_length, | 65 size_t data_length, |
67 const base::string16& type, | 66 const base::string16& type, |
68 base::string16* result) { | 67 base::string16* result) { |
69 SkippablePickle pickle(data, data_length); | 68 SkippablePickle pickle(data, data_length); |
70 PickleIterator iter(pickle); | 69 PickleIterator iter(pickle); |
71 | 70 |
72 size_t size = 0; | 71 size_t size = 0; |
73 if (!pickle.ReadSizeT(&iter, &size)) | 72 if (!iter.ReadSizeT(&size)) |
74 return; | 73 return; |
75 | 74 |
76 for (size_t i = 0; i < size; ++i) { | 75 for (size_t i = 0; i < size; ++i) { |
77 base::string16 deserialized_type; | 76 base::string16 deserialized_type; |
78 if (!pickle.ReadString16(&iter, &deserialized_type)) | 77 if (!iter.ReadString16(&deserialized_type)) |
79 return; | 78 return; |
80 if (deserialized_type == type) { | 79 if (deserialized_type == type) { |
81 ignore_result(pickle.ReadString16(&iter, result)); | 80 ignore_result(iter.ReadString16(result)); |
82 return; | 81 return; |
83 } | 82 } |
84 if (!pickle.SkipString16(&iter)) | 83 if (!pickle.SkipString16(&iter)) |
85 return; | 84 return; |
86 } | 85 } |
87 } | 86 } |
88 | 87 |
89 void ReadCustomDataIntoMap(const void* data, | 88 void ReadCustomDataIntoMap(const void* data, |
90 size_t data_length, | 89 size_t data_length, |
91 std::map<base::string16, base::string16>* result) { | 90 std::map<base::string16, base::string16>* result) { |
92 Pickle pickle(reinterpret_cast<const char*>(data), data_length); | 91 Pickle pickle(reinterpret_cast<const char*>(data), data_length); |
93 PickleIterator iter(pickle); | 92 PickleIterator iter(pickle); |
94 | 93 |
95 size_t size = 0; | 94 size_t size = 0; |
96 if (!pickle.ReadSizeT(&iter, &size)) | 95 if (!iter.ReadSizeT(&size)) |
97 return; | 96 return; |
98 | 97 |
99 for (size_t i = 0; i < size; ++i) { | 98 for (size_t i = 0; i < size; ++i) { |
100 base::string16 type; | 99 base::string16 type; |
101 if (!pickle.ReadString16(&iter, &type)) { | 100 if (!iter.ReadString16(&type)) { |
102 // Data is corrupt, return an empty map. | 101 // Data is corrupt, return an empty map. |
103 result->clear(); | 102 result->clear(); |
104 return; | 103 return; |
105 } | 104 } |
106 std::pair<std::map<base::string16, base::string16>::iterator, bool> | 105 std::pair<std::map<base::string16, base::string16>::iterator, bool> |
107 insert_result = result->insert(std::make_pair(type, base::string16())); | 106 insert_result = result->insert(std::make_pair(type, base::string16())); |
108 if (!pickle.ReadString16(&iter, &insert_result.first->second)) { | 107 if (!iter.ReadString16(&insert_result.first->second)) { |
109 // Data is corrupt, return an empty map. | 108 // Data is corrupt, return an empty map. |
110 result->clear(); | 109 result->clear(); |
111 return; | 110 return; |
112 } | 111 } |
113 } | 112 } |
114 } | 113 } |
115 | 114 |
116 void WriteCustomDataToPickle( | 115 void WriteCustomDataToPickle( |
117 const std::map<base::string16, base::string16>& data, | 116 const std::map<base::string16, base::string16>& data, |
118 Pickle* pickle) { | 117 Pickle* pickle) { |
119 pickle->WriteSizeT(data.size()); | 118 pickle->WriteSizeT(data.size()); |
120 for (std::map<base::string16, base::string16>::const_iterator it = | 119 for (std::map<base::string16, base::string16>::const_iterator it = |
121 data.begin(); | 120 data.begin(); |
122 it != data.end(); | 121 it != data.end(); |
123 ++it) { | 122 ++it) { |
124 pickle->WriteString16(it->first); | 123 pickle->WriteString16(it->first); |
125 pickle->WriteString16(it->second); | 124 pickle->WriteString16(it->second); |
126 } | 125 } |
127 } | 126 } |
128 | 127 |
129 } // namespace ui | 128 } // namespace ui |
OLD | NEW |