| 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 #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_STORE_H_ | 5 #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_STORE_H_ |
| 6 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_STORE_H_ | 6 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_STORE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> |
| 9 |
| 8 #include <deque> | 10 #include <deque> |
| 9 #include <set> | 11 #include <set> |
| 10 #include <vector> | 12 #include <vector> |
| 11 | 13 |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
| 14 #include "base/containers/hash_tables.h" | 15 #include "base/containers/hash_tables.h" |
| 16 #include "base/macros.h" |
| 15 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 16 #include "chrome/browser/safe_browsing/safe_browsing_util.h" | 18 #include "chrome/browser/safe_browsing/safe_browsing_util.h" |
| 17 | 19 |
| 18 namespace base { | 20 namespace base { |
| 19 class FilePath; | 21 class FilePath; |
| 20 } | 22 } |
| 21 | 23 |
| 22 namespace safe_browsing { | 24 namespace safe_browsing { |
| 23 | 25 |
| 24 class PrefixSetBuilder; | 26 class PrefixSetBuilder; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 40 // | 42 // |
| 41 // FinishUpdate() also handles dropping items who's chunk has been | 43 // FinishUpdate() also handles dropping items who's chunk has been |
| 42 // deleted, and netting out the add/sub lists (when a sub matches an | 44 // deleted, and netting out the add/sub lists (when a sub matches an |
| 43 // add, both are dropped). | 45 // add, both are dropped). |
| 44 | 46 |
| 45 // GetAddChunkId(), GetAddPrefix() and GetFullHash() are exposed so | 47 // GetAddChunkId(), GetAddPrefix() and GetFullHash() are exposed so |
| 46 // that these items can be generically compared with each other by | 48 // that these items can be generically compared with each other by |
| 47 // SBAddPrefixLess() and SBAddPrefixHashLess(). | 49 // SBAddPrefixLess() and SBAddPrefixHashLess(). |
| 48 | 50 |
| 49 struct SBAddPrefix { | 51 struct SBAddPrefix { |
| 50 int32 chunk_id; | 52 int32_t chunk_id; |
| 51 SBPrefix prefix; | 53 SBPrefix prefix; |
| 52 | 54 |
| 53 SBAddPrefix(int32 id, SBPrefix p) : chunk_id(id), prefix(p) {} | 55 SBAddPrefix(int32_t id, SBPrefix p) : chunk_id(id), prefix(p) {} |
| 54 SBAddPrefix() : chunk_id(), prefix() {} | 56 SBAddPrefix() : chunk_id(), prefix() {} |
| 55 | 57 |
| 56 int32 GetAddChunkId() const { return chunk_id; } | 58 int32_t GetAddChunkId() const { return chunk_id; } |
| 57 SBPrefix GetAddPrefix() const { return prefix; } | 59 SBPrefix GetAddPrefix() const { return prefix; } |
| 58 }; | 60 }; |
| 59 | 61 |
| 60 // TODO(shess): Measure the performance impact of switching this back to | 62 // TODO(shess): Measure the performance impact of switching this back to |
| 61 // std::vector<> once the v8 file format dominates. Also SBSubPrefixes. | 63 // std::vector<> once the v8 file format dominates. Also SBSubPrefixes. |
| 62 typedef std::deque<SBAddPrefix> SBAddPrefixes; | 64 typedef std::deque<SBAddPrefix> SBAddPrefixes; |
| 63 | 65 |
| 64 struct SBSubPrefix { | 66 struct SBSubPrefix { |
| 65 int32 chunk_id; | 67 int32_t chunk_id; |
| 66 int32 add_chunk_id; | 68 int32_t add_chunk_id; |
| 67 SBPrefix add_prefix; | 69 SBPrefix add_prefix; |
| 68 | 70 |
| 69 SBSubPrefix(int32 id, int32 add_id, SBPrefix prefix) | 71 SBSubPrefix(int32_t id, int32_t add_id, SBPrefix prefix) |
| 70 : chunk_id(id), add_chunk_id(add_id), add_prefix(prefix) {} | 72 : chunk_id(id), add_chunk_id(add_id), add_prefix(prefix) {} |
| 71 SBSubPrefix() : chunk_id(), add_chunk_id(), add_prefix() {} | 73 SBSubPrefix() : chunk_id(), add_chunk_id(), add_prefix() {} |
| 72 | 74 |
| 73 int32 GetAddChunkId() const { return add_chunk_id; } | 75 int32_t GetAddChunkId() const { return add_chunk_id; } |
| 74 SBPrefix GetAddPrefix() const { return add_prefix; } | 76 SBPrefix GetAddPrefix() const { return add_prefix; } |
| 75 }; | 77 }; |
| 76 | 78 |
| 77 typedef std::deque<SBSubPrefix> SBSubPrefixes; | 79 typedef std::deque<SBSubPrefix> SBSubPrefixes; |
| 78 | 80 |
| 79 struct SBAddFullHash { | 81 struct SBAddFullHash { |
| 80 int32 chunk_id; | 82 int32_t chunk_id; |
| 81 // Received field is not used anymore, but is kept for DB compatability. | 83 // Received field is not used anymore, but is kept for DB compatability. |
| 82 // TODO(shess): Deprecate and remove. | 84 // TODO(shess): Deprecate and remove. |
| 83 int32 deprecated_received; | 85 int32_t deprecated_received; |
| 84 SBFullHash full_hash; | 86 SBFullHash full_hash; |
| 85 | 87 |
| 86 SBAddFullHash(int32 id, const SBFullHash& h) | 88 SBAddFullHash(int32_t id, const SBFullHash& h) |
| 87 : chunk_id(id), deprecated_received(), full_hash(h) {} | 89 : chunk_id(id), deprecated_received(), full_hash(h) {} |
| 88 | 90 |
| 89 SBAddFullHash() : chunk_id(), deprecated_received(), full_hash() {} | 91 SBAddFullHash() : chunk_id(), deprecated_received(), full_hash() {} |
| 90 | 92 |
| 91 int32 GetAddChunkId() const { return chunk_id; } | 93 int32_t GetAddChunkId() const { return chunk_id; } |
| 92 SBPrefix GetAddPrefix() const { return full_hash.prefix; } | 94 SBPrefix GetAddPrefix() const { return full_hash.prefix; } |
| 93 }; | 95 }; |
| 94 | 96 |
| 95 struct SBSubFullHash { | 97 struct SBSubFullHash { |
| 96 int32 chunk_id; | 98 int32_t chunk_id; |
| 97 int32 add_chunk_id; | 99 int32_t add_chunk_id; |
| 98 SBFullHash full_hash; | 100 SBFullHash full_hash; |
| 99 | 101 |
| 100 SBSubFullHash(int32 id, int32 add_id, const SBFullHash& h) | 102 SBSubFullHash(int32_t id, int32_t add_id, const SBFullHash& h) |
| 101 : chunk_id(id), add_chunk_id(add_id), full_hash(h) {} | 103 : chunk_id(id), add_chunk_id(add_id), full_hash(h) {} |
| 102 SBSubFullHash() : chunk_id(), add_chunk_id(), full_hash() {} | 104 SBSubFullHash() : chunk_id(), add_chunk_id(), full_hash() {} |
| 103 | 105 |
| 104 int32 GetAddChunkId() const { return add_chunk_id; } | 106 int32_t GetAddChunkId() const { return add_chunk_id; } |
| 105 SBPrefix GetAddPrefix() const { return full_hash.prefix; } | 107 SBPrefix GetAddPrefix() const { return full_hash.prefix; } |
| 106 }; | 108 }; |
| 107 | 109 |
| 108 // Determine less-than based on prefix and add chunk. | 110 // Determine less-than based on prefix and add chunk. |
| 109 template <class T, class U> | 111 template <class T, class U> |
| 110 bool SBAddPrefixLess(const T& a, const U& b) { | 112 bool SBAddPrefixLess(const T& a, const U& b) { |
| 111 if (a.GetAddPrefix() != b.GetAddPrefix()) | 113 if (a.GetAddPrefix() != b.GetAddPrefix()) |
| 112 return a.GetAddPrefix() < b.GetAddPrefix(); | 114 return a.GetAddPrefix() < b.GetAddPrefix(); |
| 113 | 115 |
| 114 return a.GetAddChunkId() < b.GetAddChunkId(); | 116 return a.GetAddChunkId() < b.GetAddChunkId(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 132 // Process the lists for subs which knock out adds. For any item in | 134 // Process the lists for subs which knock out adds. For any item in |
| 133 // |sub_prefixes| which has a match in |add_prefixes|, knock out the | 135 // |sub_prefixes| which has a match in |add_prefixes|, knock out the |
| 134 // matched items from all vectors. Additionally remove items from | 136 // matched items from all vectors. Additionally remove items from |
| 135 // deleted chunks. | 137 // deleted chunks. |
| 136 // | 138 // |
| 137 // The inputs must be sorted by SBAddPrefixLess or SBAddPrefixHashLess. | 139 // The inputs must be sorted by SBAddPrefixLess or SBAddPrefixHashLess. |
| 138 void SBProcessSubs(SBAddPrefixes* add_prefixes, | 140 void SBProcessSubs(SBAddPrefixes* add_prefixes, |
| 139 SBSubPrefixes* sub_prefixes, | 141 SBSubPrefixes* sub_prefixes, |
| 140 std::vector<SBAddFullHash>* add_full_hashes, | 142 std::vector<SBAddFullHash>* add_full_hashes, |
| 141 std::vector<SBSubFullHash>* sub_full_hashes, | 143 std::vector<SBSubFullHash>* sub_full_hashes, |
| 142 const base::hash_set<int32>& add_chunks_deleted, | 144 const base::hash_set<int32_t>& add_chunks_deleted, |
| 143 const base::hash_set<int32>& sub_chunks_deleted); | 145 const base::hash_set<int32_t>& sub_chunks_deleted); |
| 144 | 146 |
| 145 // Abstract interface for storing data. | 147 // Abstract interface for storing data. |
| 146 class SafeBrowsingStore { | 148 class SafeBrowsingStore { |
| 147 public: | 149 public: |
| 148 SafeBrowsingStore() {} | 150 SafeBrowsingStore() {} |
| 149 virtual ~SafeBrowsingStore() {} | 151 virtual ~SafeBrowsingStore() {} |
| 150 | 152 |
| 151 // Sets up the information for later use, but does not necessarily | 153 // Sets up the information for later use, but does not necessarily |
| 152 // check whether the underlying file exists, or is valid. If | 154 // check whether the underlying file exists, or is valid. If |
| 153 // |curruption_callback| is non-NULL it will be called if corruption | 155 // |curruption_callback| is non-NULL it will be called if corruption |
| (...skipping 19 matching lines...) Expand all Loading... |
| 173 // should be terminated by FinishUpdate() or CancelUpdate(). | 175 // should be terminated by FinishUpdate() or CancelUpdate(). |
| 174 virtual bool BeginUpdate() = 0; | 176 virtual bool BeginUpdate() = 0; |
| 175 | 177 |
| 176 // Start a chunk of data. None of the methods through FinishChunk() | 178 // Start a chunk of data. None of the methods through FinishChunk() |
| 177 // should be called unless this returns true. | 179 // should be called unless this returns true. |
| 178 // TODO(shess): Would it make sense for this to accept |chunk_id|? | 180 // TODO(shess): Would it make sense for this to accept |chunk_id|? |
| 179 // Possibly not, because of possible confusion between sub_chunk_id | 181 // Possibly not, because of possible confusion between sub_chunk_id |
| 180 // and add_chunk_id. | 182 // and add_chunk_id. |
| 181 virtual bool BeginChunk() = 0; | 183 virtual bool BeginChunk() = 0; |
| 182 | 184 |
| 183 virtual bool WriteAddPrefix(int32 chunk_id, SBPrefix prefix) = 0; | 185 virtual bool WriteAddPrefix(int32_t chunk_id, SBPrefix prefix) = 0; |
| 184 virtual bool WriteAddHash(int32 chunk_id, | 186 virtual bool WriteAddHash(int32_t chunk_id, const SBFullHash& full_hash) = 0; |
| 185 const SBFullHash& full_hash) = 0; | 187 virtual bool WriteSubPrefix(int32_t chunk_id, |
| 186 virtual bool WriteSubPrefix(int32 chunk_id, | 188 int32_t add_chunk_id, |
| 187 int32 add_chunk_id, SBPrefix prefix) = 0; | 189 SBPrefix prefix) = 0; |
| 188 virtual bool WriteSubHash(int32 chunk_id, int32 add_chunk_id, | 190 virtual bool WriteSubHash(int32_t chunk_id, |
| 191 int32_t add_chunk_id, |
| 189 const SBFullHash& full_hash) = 0; | 192 const SBFullHash& full_hash) = 0; |
| 190 | 193 |
| 191 // Collect the chunk data and preferrably store it on disk to | 194 // Collect the chunk data and preferrably store it on disk to |
| 192 // release memory. Shoul not modify the data in-place. | 195 // release memory. Shoul not modify the data in-place. |
| 193 virtual bool FinishChunk() = 0; | 196 virtual bool FinishChunk() = 0; |
| 194 | 197 |
| 195 // Track the chunks which have been seen. | 198 // Track the chunks which have been seen. |
| 196 virtual void SetAddChunk(int32 chunk_id) = 0; | 199 virtual void SetAddChunk(int32_t chunk_id) = 0; |
| 197 virtual bool CheckAddChunk(int32 chunk_id) = 0; | 200 virtual bool CheckAddChunk(int32_t chunk_id) = 0; |
| 198 virtual void GetAddChunks(std::vector<int32>* out) = 0; | 201 virtual void GetAddChunks(std::vector<int32_t>* out) = 0; |
| 199 virtual void SetSubChunk(int32 chunk_id) = 0; | 202 virtual void SetSubChunk(int32_t chunk_id) = 0; |
| 200 virtual bool CheckSubChunk(int32 chunk_id) = 0; | 203 virtual bool CheckSubChunk(int32_t chunk_id) = 0; |
| 201 virtual void GetSubChunks(std::vector<int32>* out) = 0; | 204 virtual void GetSubChunks(std::vector<int32_t>* out) = 0; |
| 202 | 205 |
| 203 // Delete the indicated chunk_id. The chunk will continue to be | 206 // Delete the indicated chunk_id. The chunk will continue to be |
| 204 // visible until the end of the transaction. | 207 // visible until the end of the transaction. |
| 205 virtual void DeleteAddChunk(int32 chunk_id) = 0; | 208 virtual void DeleteAddChunk(int32_t chunk_id) = 0; |
| 206 virtual void DeleteSubChunk(int32 chunk_id) = 0; | 209 virtual void DeleteSubChunk(int32_t chunk_id) = 0; |
| 207 | 210 |
| 208 // May be called during update to verify that the storage is valid. | 211 // May be called during update to verify that the storage is valid. |
| 209 // Return true if the store seems valid. If corruption is detected, | 212 // Return true if the store seems valid. If corruption is detected, |
| 210 // calls the corruption callback and return false. | 213 // calls the corruption callback and return false. |
| 211 // NOTE(shess): When storage was SQLite, there was no guarantee that | 214 // NOTE(shess): When storage was SQLite, there was no guarantee that |
| 212 // a structurally sound database actually contained valid data, | 215 // a structurally sound database actually contained valid data, |
| 213 // whereas SafeBrowsingStoreFile checksums the data. For now, this | 216 // whereas SafeBrowsingStoreFile checksums the data. For now, this |
| 214 // distinction doesn't matter. | 217 // distinction doesn't matter. |
| 215 virtual bool CheckValidity() = 0; | 218 virtual bool CheckValidity() = 0; |
| 216 | 219 |
| 217 // Pass the collected chunks through SBPRocessSubs() and commit to | 220 // Pass the collected chunks through SBPRocessSubs() and commit to |
| 218 // permanent storage. The resulting add prefixes and hashes will be | 221 // permanent storage. The resulting add prefixes and hashes will be |
| 219 // stored in |add_prefixes_result| and |add_full_hashes_result|. | 222 // stored in |add_prefixes_result| and |add_full_hashes_result|. |
| 220 virtual bool FinishUpdate( | 223 virtual bool FinishUpdate( |
| 221 PrefixSetBuilder* builder, | 224 PrefixSetBuilder* builder, |
| 222 std::vector<SBAddFullHash>* add_full_hashes_result) = 0; | 225 std::vector<SBAddFullHash>* add_full_hashes_result) = 0; |
| 223 | 226 |
| 224 // Cancel the update in process and remove any temporary disk | 227 // Cancel the update in process and remove any temporary disk |
| 225 // storage, leaving the original data unmodified. | 228 // storage, leaving the original data unmodified. |
| 226 virtual bool CancelUpdate() = 0; | 229 virtual bool CancelUpdate() = 0; |
| 227 | 230 |
| 228 private: | 231 private: |
| 229 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingStore); | 232 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingStore); |
| 230 }; | 233 }; |
| 231 | 234 |
| 232 } // namespace safe_browsing | 235 } // namespace safe_browsing |
| 233 | 236 |
| 234 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_STORE_H_ | 237 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_STORE_H_ |
| OLD | NEW |