| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 // A read-only set implementation for |SBPrefix| items. Prefixes are | 5 // A read-only set implementation for |SBPrefix| items. Prefixes are |
| 6 // sorted and stored as 16-bit deltas from the previous prefix. An | 6 // sorted and stored as 16-bit deltas from the previous prefix. An |
| 7 // index structure provides quick random access, and also handles | 7 // index structure provides quick random access, and also handles |
| 8 // cases where 16 bits cannot encode a delta. | 8 // cases where 16 bits cannot encode a delta. |
| 9 // | 9 // |
| 10 // For example, the sequence {20, 25, 41, 65432, 150000, 160000} would | 10 // For example, the sequence {20, 25, 41, 65432, 150000, 160000} would |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 bool Exists(SBPrefix prefix) const; | 67 bool Exists(SBPrefix prefix) const; |
| 68 | 68 |
| 69 // Persist the set on disk. | 69 // Persist the set on disk. |
| 70 static PrefixSet* LoadFile(const FilePath& filter_name); | 70 static PrefixSet* LoadFile(const FilePath& filter_name); |
| 71 bool WriteFile(const FilePath& filter_name) const; | 71 bool WriteFile(const FilePath& filter_name) const; |
| 72 | 72 |
| 73 // Regenerate the vector of prefixes passed to the constructor into | 73 // Regenerate the vector of prefixes passed to the constructor into |
| 74 // |prefixes|. Prefixes will be added in sorted order. | 74 // |prefixes|. Prefixes will be added in sorted order. |
| 75 void GetPrefixes(std::vector<SBPrefix>* prefixes) const; | 75 void GetPrefixes(std::vector<SBPrefix>* prefixes) const; |
| 76 | 76 |
| 77 // TODO(shess): The following are debugging accessors. Delete once |
| 78 // the encoding problem is figured out. |
| 79 |
| 80 size_t IndexBinFor(size_t target_index) const; |
| 81 |
| 82 // The number of prefixes represented. |
| 83 size_t GetSize() const; |
| 84 |
| 85 // Returns |true| if the element at |target_index| is between items in the |
| 86 // |index_| array. |
| 87 bool IsDeltaAt(size_t target_index) const; |
| 88 |
| 89 // Returns the delta used to calculate the element at |
| 90 // |target_index|. Only call if |IsDeltaAt()| returned |true|. |
| 91 uint16 DeltaAt(size_t target_index) const; |
| 92 |
| 77 private: | 93 private: |
| 78 // Maximum number of consecutive deltas to encode before generating | 94 // Maximum number of consecutive deltas to encode before generating |
| 79 // a new index entry. This helps keep the worst-case performance | 95 // a new index entry. This helps keep the worst-case performance |
| 80 // for |Exists()| under control. | 96 // for |Exists()| under control. |
| 81 static const size_t kMaxRun = 100; | 97 static const size_t kMaxRun = 100; |
| 82 | 98 |
| 83 // Helper for |LoadFile()|. Steals the contents of |index| and | 99 // Helper for |LoadFile()|. Steals the contents of |index| and |
| 84 // |deltas| using |swap()|. | 100 // |deltas| using |swap()|. |
| 85 PrefixSet(std::vector<std::pair<SBPrefix,size_t> > *index, | 101 PrefixSet(std::vector<std::pair<SBPrefix,size_t> > *index, |
| 86 std::vector<uint16> *deltas); | 102 std::vector<uint16> *deltas); |
| 87 | 103 |
| 88 // Top-level index of prefix to offset in |deltas_|. Each pair | 104 // Top-level index of prefix to offset in |deltas_|. Each pair |
| 89 // indicates a base prefix and where the deltas from that prefix | 105 // indicates a base prefix and where the deltas from that prefix |
| 90 // begin in |deltas_|. The deltas for a pair end at the next pair's | 106 // begin in |deltas_|. The deltas for a pair end at the next pair's |
| 91 // index into |deltas_|. | 107 // index into |deltas_|. |
| 92 std::vector<std::pair<SBPrefix,size_t> > index_; | 108 std::vector<std::pair<SBPrefix,size_t> > index_; |
| 93 | 109 |
| 94 // Deltas which are added to the prefix in |index_| to generate | 110 // Deltas which are added to the prefix in |index_| to generate |
| 95 // prefixes. Deltas are only valid between consecutive items from | 111 // prefixes. Deltas are only valid between consecutive items from |
| 96 // |index_|, or the end of |deltas_| for the last |index_| pair. | 112 // |index_|, or the end of |deltas_| for the last |index_| pair. |
| 97 std::vector<uint16> deltas_; | 113 std::vector<uint16> deltas_; |
| 98 | 114 |
| 99 DISALLOW_COPY_AND_ASSIGN(PrefixSet); | 115 DISALLOW_COPY_AND_ASSIGN(PrefixSet); |
| 100 }; | 116 }; |
| 101 | 117 |
| 102 } // namespace safe_browsing | 118 } // namespace safe_browsing |
| 103 | 119 |
| 104 #endif // CHROME_BROWSER_SAFE_BROWSING_PREFIX_SET_H_ | 120 #endif // CHROME_BROWSER_SAFE_BROWSING_PREFIX_SET_H_ |
| OLD | NEW |