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