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 // 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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 | 64 |
65 class PrefixSet { | 65 class PrefixSet { |
66 public: | 66 public: |
67 ~PrefixSet(); | 67 ~PrefixSet(); |
68 | 68 |
69 // |true| if |hash| is in the hashes passed to the set's builder, or if | 69 // |true| if |hash| is in the hashes passed to the set's builder, or if |
70 // |hash.prefix| is one of the prefixes passed to the set's builder. | 70 // |hash.prefix| is one of the prefixes passed to the set's builder. |
71 bool Exists(const SBFullHash& hash) const; | 71 bool Exists(const SBFullHash& hash) const; |
72 | 72 |
73 // Persist the set on disk. | 73 // Persist the set on disk. |
74 static scoped_ptr<PrefixSet> LoadFile(const base::FilePath& filter_name); | 74 static scoped_ptr<const PrefixSet> LoadFile( |
| 75 const base::FilePath& filter_name); |
75 bool WriteFile(const base::FilePath& filter_name) const; | 76 bool WriteFile(const base::FilePath& filter_name) const; |
76 | 77 |
77 private: | 78 private: |
78 friend class PrefixSetBuilder; | 79 friend class PrefixSetBuilder; |
79 | 80 |
80 friend class PrefixSetTest; | 81 friend class PrefixSetTest; |
81 FRIEND_TEST_ALL_PREFIXES(PrefixSetTest, AllBig); | 82 FRIEND_TEST_ALL_PREFIXES(PrefixSetTest, AllBig); |
82 FRIEND_TEST_ALL_PREFIXES(PrefixSetTest, EdgeCases); | 83 FRIEND_TEST_ALL_PREFIXES(PrefixSetTest, EdgeCases); |
83 FRIEND_TEST_ALL_PREFIXES(PrefixSetTest, Empty); | 84 FRIEND_TEST_ALL_PREFIXES(PrefixSetTest, Empty); |
84 FRIEND_TEST_ALL_PREFIXES(PrefixSetTest, FullHashBuild); | 85 FRIEND_TEST_ALL_PREFIXES(PrefixSetTest, FullHashBuild); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 // Helper for unit tests and format conversion. | 155 // Helper for unit tests and format conversion. |
155 explicit PrefixSetBuilder(const std::vector<SBPrefix>& prefixes); | 156 explicit PrefixSetBuilder(const std::vector<SBPrefix>& prefixes); |
156 | 157 |
157 // Add a prefix to the set. Prefixes must arrive in ascending order. | 158 // Add a prefix to the set. Prefixes must arrive in ascending order. |
158 // Duplicate prefixes are dropped. | 159 // Duplicate prefixes are dropped. |
159 void AddPrefix(SBPrefix prefix); | 160 void AddPrefix(SBPrefix prefix); |
160 | 161 |
161 // Flush any buffered prefixes, and return the final PrefixSet instance. | 162 // Flush any buffered prefixes, and return the final PrefixSet instance. |
162 // |hashes| are sorted and stored in |full_hashes_|. Any call other than the | 163 // |hashes| are sorted and stored in |full_hashes_|. Any call other than the |
163 // destructor is illegal after this call. | 164 // destructor is illegal after this call. |
164 scoped_ptr<PrefixSet> GetPrefixSet(const std::vector<SBFullHash>& hashes); | 165 scoped_ptr<const PrefixSet> GetPrefixSet( |
| 166 const std::vector<SBFullHash>& hashes); |
165 | 167 |
166 // Helper for clients which only track prefixes. Calls GetPrefixSet() with | 168 // Helper for clients which only track prefixes. Calls GetPrefixSet() with |
167 // empty hash vector. | 169 // empty hash vector. |
168 scoped_ptr<PrefixSet> GetPrefixSetNoHashes(); | 170 scoped_ptr<const PrefixSet> GetPrefixSetNoHashes(); |
169 | 171 |
170 private: | 172 private: |
171 // Encode a run of deltas for |AddRun()|. The run is broken by a too-large | 173 // Encode a run of deltas for |AddRun()|. The run is broken by a too-large |
172 // delta, or kMaxRun, whichever comes first. | 174 // delta, or kMaxRun, whichever comes first. |
173 void EmitRun(); | 175 void EmitRun(); |
174 | 176 |
175 // Buffers prefixes until enough are avaliable to emit a run. | 177 // Buffers prefixes until enough are avaliable to emit a run. |
176 std::vector<SBPrefix> buffer_; | 178 std::vector<SBPrefix> buffer_; |
177 | 179 |
178 // The PrefixSet being built. | 180 // The PrefixSet being built. |
179 scoped_ptr<PrefixSet> prefix_set_; | 181 scoped_ptr<PrefixSet> prefix_set_; |
180 }; | 182 }; |
181 | 183 |
182 } // namespace safe_browsing | 184 } // namespace safe_browsing |
183 | 185 |
184 #endif // CHROME_BROWSER_SAFE_BROWSING_PREFIX_SET_H_ | 186 #endif // CHROME_BROWSER_SAFE_BROWSING_PREFIX_SET_H_ |
OLD | NEW |