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