OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "components/nacl/browser/nacl_validation_cache.h" | 5 #include "components/nacl/browser/nacl_validation_cache.h" |
6 | 6 |
7 #include "base/pickle.h" | 7 #include "base/pickle.h" |
8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
9 | 9 |
10 namespace nacl { | 10 namespace nacl { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 } | 45 } |
46 return false; | 46 return false; |
47 } | 47 } |
48 | 48 |
49 void NaClValidationCache::SetKnownToValidate(const std::string& signature) { | 49 void NaClValidationCache::SetKnownToValidate(const std::string& signature) { |
50 if (signature.length() == kValidationCacheEntrySize) { | 50 if (signature.length() == kValidationCacheEntrySize) { |
51 validation_cache_.Put(signature, true); | 51 validation_cache_.Put(signature, true); |
52 } | 52 } |
53 } | 53 } |
54 | 54 |
55 void NaClValidationCache::Serialize(Pickle* pickle) const { | 55 void NaClValidationCache::Serialize(base::Pickle* pickle) const { |
56 // Mark the beginning of the data stream. | 56 // Mark the beginning of the data stream. |
57 pickle->WriteString(kValidationCacheBeginMagic); | 57 pickle->WriteString(kValidationCacheBeginMagic); |
58 pickle->WriteString(validation_cache_key_); | 58 pickle->WriteString(validation_cache_key_); |
59 pickle->WriteInt(validation_cache_.size()); | 59 pickle->WriteInt(validation_cache_.size()); |
60 | 60 |
61 // Serialize the cache in reverse order so that deserializing it can easily | 61 // Serialize the cache in reverse order so that deserializing it can easily |
62 // preserve the MRU order. (Last item deserialized => most recently used.) | 62 // preserve the MRU order. (Last item deserialized => most recently used.) |
63 ValidationCacheType::const_reverse_iterator iter; | 63 ValidationCacheType::const_reverse_iterator iter; |
64 for (iter = validation_cache_.rbegin(); | 64 for (iter = validation_cache_.rbegin(); |
65 iter != validation_cache_.rend(); | 65 iter != validation_cache_.rend(); |
66 ++iter) { | 66 ++iter) { |
67 pickle->WriteString(iter->first); | 67 pickle->WriteString(iter->first); |
68 } | 68 } |
69 | 69 |
70 // Mark the end of the data stream. | 70 // Mark the end of the data stream. |
71 pickle->WriteString(kValidationCacheEndMagic); | 71 pickle->WriteString(kValidationCacheEndMagic); |
72 } | 72 } |
73 | 73 |
74 void NaClValidationCache::Reset() { | 74 void NaClValidationCache::Reset() { |
75 validation_cache_key_ = base::RandBytesAsString(kValidationCacheKeySize); | 75 validation_cache_key_ = base::RandBytesAsString(kValidationCacheKeySize); |
76 validation_cache_.Clear(); | 76 validation_cache_.Clear(); |
77 } | 77 } |
78 | 78 |
79 bool NaClValidationCache::Deserialize(const Pickle* pickle) { | 79 bool NaClValidationCache::Deserialize(const base::Pickle* pickle) { |
80 bool success = DeserializeImpl(pickle); | 80 bool success = DeserializeImpl(pickle); |
81 if (!success) { | 81 if (!success) { |
82 Reset(); | 82 Reset(); |
83 } | 83 } |
84 return success; | 84 return success; |
85 } | 85 } |
86 | 86 |
87 bool NaClValidationCache::DeserializeImpl(const Pickle* pickle) { | 87 bool NaClValidationCache::DeserializeImpl(const base::Pickle* pickle) { |
88 PickleIterator iter(*pickle); | 88 base::PickleIterator iter(*pickle); |
89 std::string buffer; | 89 std::string buffer; |
90 int count; | 90 int count; |
91 | 91 |
92 // Magic | 92 // Magic |
93 if (!iter.ReadString(&buffer)) | 93 if (!iter.ReadString(&buffer)) |
94 return false; | 94 return false; |
95 if (0 != buffer.compare(kValidationCacheBeginMagic)) | 95 if (0 != buffer.compare(kValidationCacheBeginMagic)) |
96 return false; | 96 return false; |
97 | 97 |
98 // Key | 98 // Key |
(...skipping 21 matching lines...) Expand all Loading... |
120 return false; | 120 return false; |
121 if (0 != buffer.compare(kValidationCacheEndMagic)) | 121 if (0 != buffer.compare(kValidationCacheEndMagic)) |
122 return false; | 122 return false; |
123 | 123 |
124 // Success! | 124 // Success! |
125 return true; | 125 return true; |
126 } | 126 } |
127 | 127 |
128 } // namespace nacl | 128 } // namespace nacl |
129 | 129 |
OLD | NEW |