OLD | NEW |
1 // Copyright (c) 2011 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<string16, string16> and swap the deserialized values in. | 7 // std::pair<string16, string16> and swap the deserialized values in. |
8 | 8 |
9 #include "ui/base/clipboard/custom_data_helper.h" | 9 #include "ui/base/clipboard/custom_data_helper.h" |
10 | 10 |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/pickle.h" | 13 #include "base/pickle.h" |
14 | 14 |
15 namespace ui { | 15 namespace ui { |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 class SkippablePickle : public Pickle { | 19 class SkippablePickle : public Pickle { |
20 public: | 20 public: |
21 SkippablePickle(const void* data, size_t data_len); | 21 SkippablePickle(const void* data, size_t data_len); |
22 bool SkipString16(void** iter); | 22 bool SkipString16(PickleIterator* iter); |
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(void** 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 (!ReadLength(iter, &len)) |
34 return false; | 34 return false; |
35 if (!IteratorHasRoomFor(*iter, len * sizeof(char16))) | 35 return iter->SkipBytes(len * sizeof(char16)); |
36 return false; | |
37 | |
38 UpdateIter(iter, len * sizeof(char16)); | |
39 return true; | |
40 } | 36 } |
41 | 37 |
42 } // namespace | 38 } // namespace |
43 | 39 |
44 void ReadCustomDataTypes(const void* data, | 40 void ReadCustomDataTypes(const void* data, |
45 size_t data_length, | 41 size_t data_length, |
46 std::vector<string16>* types) { | 42 std::vector<string16>* types) { |
47 SkippablePickle pickle(data, data_length); | 43 SkippablePickle pickle(data, data_length); |
48 void* iter = NULL; | 44 PickleIterator iter(pickle); |
49 | 45 |
50 size_t size = 0; | 46 size_t size = 0; |
51 if (!pickle.ReadSize(&iter, &size)) | 47 if (!pickle.ReadSize(&iter, &size)) |
52 return; | 48 return; |
53 | 49 |
54 // 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 |
55 // 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 |
56 // custom data pickles. | 52 // custom data pickles. |
57 size_t original_size = types->size(); | 53 size_t original_size = types->size(); |
58 | 54 |
59 for (size_t i = 0; i < size; ++i) { | 55 for (size_t i = 0; i < size; ++i) { |
60 types->push_back(string16()); | 56 types->push_back(string16()); |
61 if (!pickle.ReadString16(&iter, &types->back()) || | 57 if (!pickle.ReadString16(&iter, &types->back()) || |
62 !pickle.SkipString16(&iter)) { | 58 !pickle.SkipString16(&iter)) { |
63 types->resize(original_size); | 59 types->resize(original_size); |
64 return; | 60 return; |
65 } | 61 } |
66 } | 62 } |
67 } | 63 } |
68 | 64 |
69 void ReadCustomDataForType(const void* data, | 65 void ReadCustomDataForType(const void* data, |
70 size_t data_length, | 66 size_t data_length, |
71 const string16& type, | 67 const string16& type, |
72 string16* result) { | 68 string16* result) { |
73 SkippablePickle pickle(data, data_length); | 69 SkippablePickle pickle(data, data_length); |
74 void* iter = NULL; | 70 PickleIterator iter(pickle); |
75 | 71 |
76 size_t size = 0; | 72 size_t size = 0; |
77 if (!pickle.ReadSize(&iter, &size)) | 73 if (!pickle.ReadSize(&iter, &size)) |
78 return; | 74 return; |
79 | 75 |
80 for (size_t i = 0; i < size; ++i) { | 76 for (size_t i = 0; i < size; ++i) { |
81 string16 deserialized_type; | 77 string16 deserialized_type; |
82 if (!pickle.ReadString16(&iter, &deserialized_type)) | 78 if (!pickle.ReadString16(&iter, &deserialized_type)) |
83 return; | 79 return; |
84 if (deserialized_type == type) { | 80 if (deserialized_type == type) { |
85 pickle.ReadString16(&iter, result); | 81 pickle.ReadString16(&iter, result); |
86 return; | 82 return; |
87 } | 83 } |
88 if (!pickle.SkipString16(&iter)) | 84 if (!pickle.SkipString16(&iter)) |
89 return; | 85 return; |
90 } | 86 } |
91 } | 87 } |
92 | 88 |
93 void ReadCustomDataIntoMap(const void* data, | 89 void ReadCustomDataIntoMap(const void* data, |
94 size_t data_length, | 90 size_t data_length, |
95 std::map<string16, string16>* result) { | 91 std::map<string16, string16>* result) { |
96 Pickle pickle(reinterpret_cast<const char*>(data), data_length); | 92 Pickle pickle(reinterpret_cast<const char*>(data), data_length); |
97 void* iter = NULL; | 93 PickleIterator iter(pickle); |
98 | 94 |
99 size_t size = 0; | 95 size_t size = 0; |
100 if (!pickle.ReadSize(&iter, &size)) | 96 if (!pickle.ReadSize(&iter, &size)) |
101 return; | 97 return; |
102 | 98 |
103 for (size_t i = 0; i < size; ++i) { | 99 for (size_t i = 0; i < size; ++i) { |
104 string16 type; | 100 string16 type; |
105 if (!pickle.ReadString16(&iter, &type)) { | 101 if (!pickle.ReadString16(&iter, &type)) { |
106 // Data is corrupt, return an empty map. | 102 // Data is corrupt, return an empty map. |
107 result->clear(); | 103 result->clear(); |
(...skipping 14 matching lines...) Expand all Loading... |
122 pickle->WriteSize(data.size()); | 118 pickle->WriteSize(data.size()); |
123 for (std::map<string16, string16>::const_iterator it = data.begin(); | 119 for (std::map<string16, string16>::const_iterator it = data.begin(); |
124 it != data.end(); | 120 it != data.end(); |
125 ++it) { | 121 ++it) { |
126 pickle->WriteString16(it->first); | 122 pickle->WriteString16(it->first); |
127 pickle->WriteString16(it->second); | 123 pickle->WriteString16(it->second); |
128 } | 124 } |
129 } | 125 } |
130 | 126 |
131 } // namespace ui | 127 } // namespace ui |
OLD | NEW |