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 (!iter->ReadLength(&len)) | 33 if (!ReadLength(iter, &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 (!iter.ReadSizeT(&size)) | 47 if (!pickle.ReadSizeT(&iter, &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 (!iter.ReadString16(&types->back()) || !pickle.SkipString16(&iter)) { | 57 if (!pickle.ReadString16(&iter, &types->back()) || |
| 58 !pickle.SkipString16(&iter)) { |
58 types->resize(original_size); | 59 types->resize(original_size); |
59 return; | 60 return; |
60 } | 61 } |
61 } | 62 } |
62 } | 63 } |
63 | 64 |
64 void ReadCustomDataForType(const void* data, | 65 void ReadCustomDataForType(const void* data, |
65 size_t data_length, | 66 size_t data_length, |
66 const base::string16& type, | 67 const base::string16& type, |
67 base::string16* result) { | 68 base::string16* result) { |
68 SkippablePickle pickle(data, data_length); | 69 SkippablePickle pickle(data, data_length); |
69 PickleIterator iter(pickle); | 70 PickleIterator iter(pickle); |
70 | 71 |
71 size_t size = 0; | 72 size_t size = 0; |
72 if (!iter.ReadSizeT(&size)) | 73 if (!pickle.ReadSizeT(&iter, &size)) |
73 return; | 74 return; |
74 | 75 |
75 for (size_t i = 0; i < size; ++i) { | 76 for (size_t i = 0; i < size; ++i) { |
76 base::string16 deserialized_type; | 77 base::string16 deserialized_type; |
77 if (!iter.ReadString16(&deserialized_type)) | 78 if (!pickle.ReadString16(&iter, &deserialized_type)) |
78 return; | 79 return; |
79 if (deserialized_type == type) { | 80 if (deserialized_type == type) { |
80 ignore_result(iter.ReadString16(result)); | 81 ignore_result(pickle.ReadString16(&iter, result)); |
81 return; | 82 return; |
82 } | 83 } |
83 if (!pickle.SkipString16(&iter)) | 84 if (!pickle.SkipString16(&iter)) |
84 return; | 85 return; |
85 } | 86 } |
86 } | 87 } |
87 | 88 |
88 void ReadCustomDataIntoMap(const void* data, | 89 void ReadCustomDataIntoMap(const void* data, |
89 size_t data_length, | 90 size_t data_length, |
90 std::map<base::string16, base::string16>* result) { | 91 std::map<base::string16, base::string16>* result) { |
91 Pickle pickle(reinterpret_cast<const char*>(data), data_length); | 92 Pickle pickle(reinterpret_cast<const char*>(data), data_length); |
92 PickleIterator iter(pickle); | 93 PickleIterator iter(pickle); |
93 | 94 |
94 size_t size = 0; | 95 size_t size = 0; |
95 if (!iter.ReadSizeT(&size)) | 96 if (!pickle.ReadSizeT(&iter, &size)) |
96 return; | 97 return; |
97 | 98 |
98 for (size_t i = 0; i < size; ++i) { | 99 for (size_t i = 0; i < size; ++i) { |
99 base::string16 type; | 100 base::string16 type; |
100 if (!iter.ReadString16(&type)) { | 101 if (!pickle.ReadString16(&iter, &type)) { |
101 // Data is corrupt, return an empty map. | 102 // Data is corrupt, return an empty map. |
102 result->clear(); | 103 result->clear(); |
103 return; | 104 return; |
104 } | 105 } |
105 std::pair<std::map<base::string16, base::string16>::iterator, bool> | 106 std::pair<std::map<base::string16, base::string16>::iterator, bool> |
106 insert_result = result->insert(std::make_pair(type, base::string16())); | 107 insert_result = result->insert(std::make_pair(type, base::string16())); |
107 if (!iter.ReadString16(&insert_result.first->second)) { | 108 if (!pickle.ReadString16(&iter, &insert_result.first->second)) { |
108 // Data is corrupt, return an empty map. | 109 // Data is corrupt, return an empty map. |
109 result->clear(); | 110 result->clear(); |
110 return; | 111 return; |
111 } | 112 } |
112 } | 113 } |
113 } | 114 } |
114 | 115 |
115 void WriteCustomDataToPickle( | 116 void WriteCustomDataToPickle( |
116 const std::map<base::string16, base::string16>& data, | 117 const std::map<base::string16, base::string16>& data, |
117 Pickle* pickle) { | 118 Pickle* pickle) { |
118 pickle->WriteSizeT(data.size()); | 119 pickle->WriteSizeT(data.size()); |
119 for (std::map<base::string16, base::string16>::const_iterator it = | 120 for (std::map<base::string16, base::string16>::const_iterator it = |
120 data.begin(); | 121 data.begin(); |
121 it != data.end(); | 122 it != data.end(); |
122 ++it) { | 123 ++it) { |
123 pickle->WriteString16(it->first); | 124 pickle->WriteString16(it->first); |
124 pickle->WriteString16(it->second); | 125 pickle->WriteString16(it->second); |
125 } | 126 } |
126 } | 127 } |
127 | 128 |
128 } // namespace ui | 129 } // namespace ui |
OLD | NEW |