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