Chromium Code Reviews| 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_FILE_H_ | 5 #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_STORE_FILE_H_ |
| 6 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_STORE_FILE_H_ | 6 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_STORE_FILE_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "chrome/browser/safe_browsing/safe_browsing_store.h" | 11 #include "chrome/browser/safe_browsing/safe_browsing_store.h" |
| 12 | 12 |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/files/scoped_file.h" | 15 #include "base/files/scoped_file.h" |
| 16 #include "base/sequenced_task_runner.h" | |
| 16 #include "base/threading/non_thread_safe.h" | 17 #include "base/threading/non_thread_safe.h" |
|
gab
2015/02/19 14:38:23
rm this include
Alexei Svitkine (slow)
2015/02/20 15:42:44
Done.
| |
| 17 | 18 |
| 18 // Implement SafeBrowsingStore in terms of a flat file. The file | 19 // Implement SafeBrowsingStore in terms of a flat file. The file |
| 19 // format is pretty literal: | 20 // format is pretty literal: |
| 20 // | 21 // |
| 21 // int32 magic; // magic number "validating" file | 22 // int32 magic; // magic number "validating" file |
| 22 // int32 version; // format version | 23 // int32 version; // format version |
| 23 // | 24 // |
| 24 // // Counts for the various data which follows the header. | 25 // // Counts for the various data which follows the header. |
| 25 // uint32 add_chunk_count; // Chunks seen, including empties. | 26 // uint32 add_chunk_count; // Chunks seen, including empties. |
| 26 // uint32 sub_chunk_count; // Ditto. | 27 // uint32 sub_chunk_count; // Ditto. |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 // - When the transaction is finished: | 117 // - When the transaction is finished: |
| 117 // - Read the update data from the temp file into memory. | 118 // - Read the update data from the temp file into memory. |
| 118 // - Overwrite the temp file with new header data. | 119 // - Overwrite the temp file with new header data. |
| 119 // - Until done: | 120 // - Until done: |
| 120 // - Read shards of the original file's data into memory. | 121 // - Read shards of the original file's data into memory. |
| 121 // - Merge from the update data. | 122 // - Merge from the update data. |
| 122 // - Write shards to the temp file. | 123 // - Write shards to the temp file. |
| 123 // - Delete original file. | 124 // - Delete original file. |
| 124 // - Rename temp file to original filename. | 125 // - Rename temp file to original filename. |
| 125 | 126 |
| 126 class SafeBrowsingStoreFile : public SafeBrowsingStore, | 127 class SafeBrowsingStoreFile : public SafeBrowsingStore { |
| 127 public base::NonThreadSafe { | |
| 128 public: | 128 public: |
| 129 SafeBrowsingStoreFile(); | 129 explicit SafeBrowsingStoreFile( |
| 130 scoped_refptr<base::SequencedTaskRunner> task_runner); | |
| 130 ~SafeBrowsingStoreFile() override; | 131 ~SafeBrowsingStoreFile() override; |
| 131 | 132 |
| 132 void Init(const base::FilePath& filename, | 133 void Init(const base::FilePath& filename, |
| 133 const base::Closure& corruption_callback) override; | 134 const base::Closure& corruption_callback) override; |
| 134 | 135 |
| 135 // Delete any on-disk files, including the permanent storage. | 136 // Delete any on-disk files, including the permanent storage. |
| 136 bool Delete() override; | 137 bool Delete() override; |
| 137 | 138 |
| 138 // Get all add hash prefixes and full-length hashes, respectively, from | 139 // Get all add hash prefixes and full-length hashes, respectively, from |
| 139 // the store. | 140 // the store. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 // |filename|. Exported for unit tests. | 177 // |filename|. Exported for unit tests. |
| 177 static const base::FilePath TemporaryFileForFilename( | 178 static const base::FilePath TemporaryFileForFilename( |
| 178 const base::FilePath& filename) { | 179 const base::FilePath& filename) { |
| 179 return base::FilePath(filename.value() + FILE_PATH_LITERAL("_new")); | 180 return base::FilePath(filename.value() + FILE_PATH_LITERAL("_new")); |
| 180 } | 181 } |
| 181 | 182 |
| 182 // Delete any on-disk files, including the permanent storage. | 183 // Delete any on-disk files, including the permanent storage. |
| 183 static bool DeleteStore(const base::FilePath& basename); | 184 static bool DeleteStore(const base::FilePath& basename); |
| 184 | 185 |
| 185 private: | 186 private: |
| 187 // Checks whether the current thread is part of the sequenced task runner | |
| 188 // this object was initialized with. | |
| 189 bool CalledOnValidThread(); | |
| 190 | |
| 186 // Does the actual update for FinishUpdate(), so that FinishUpdate() can clean | 191 // Does the actual update for FinishUpdate(), so that FinishUpdate() can clean |
| 187 // up correctly in case of error. | 192 // up correctly in case of error. |
| 188 virtual bool DoUpdate(safe_browsing::PrefixSetBuilder* builder, | 193 virtual bool DoUpdate(safe_browsing::PrefixSetBuilder* builder, |
| 189 std::vector<SBAddFullHash>* add_full_hashes_result); | 194 std::vector<SBAddFullHash>* add_full_hashes_result); |
| 190 | 195 |
| 191 // Some very lucky users have an original-format file still in their | 196 // Some very lucky users have an original-format file still in their |
| 192 // profile. Check for it and delete, recording a histogram for the | 197 // profile. Check for it and delete, recording a histogram for the |
| 193 // result (no histogram for not-found). Logically this | 198 // result (no histogram for not-found). Logically this |
| 194 // would make more sense at the SafeBrowsingDatabase level, but | 199 // would make more sense at the SafeBrowsingDatabase level, but |
| 195 // practically speaking that code doesn't touch files directly. | 200 // practically speaking that code doesn't touch files directly. |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 222 // Clear all buffers used during update. | 227 // Clear all buffers used during update. |
| 223 void ClearUpdateBuffers() { | 228 void ClearUpdateBuffers() { |
| 224 ClearChunkBuffers(); | 229 ClearChunkBuffers(); |
| 225 chunks_written_ = 0; | 230 chunks_written_ = 0; |
| 226 std::set<int32>().swap(add_chunks_cache_); | 231 std::set<int32>().swap(add_chunks_cache_); |
| 227 std::set<int32>().swap(sub_chunks_cache_); | 232 std::set<int32>().swap(sub_chunks_cache_); |
| 228 base::hash_set<int32>().swap(add_del_cache_); | 233 base::hash_set<int32>().swap(add_del_cache_); |
| 229 base::hash_set<int32>().swap(sub_del_cache_); | 234 base::hash_set<int32>().swap(sub_del_cache_); |
| 230 } | 235 } |
| 231 | 236 |
| 237 // The sequenced task runner for this object, used to verify that its state | |
| 238 // is only ever accessed in sequence via the runner. | |
|
gab
2015/02/19 14:38:23
s/in sequence via the runner/from the runner
Alexei Svitkine (slow)
2015/02/20 15:42:44
Done.
| |
| 239 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
| 240 | |
| 232 // Buffers for collecting data between BeginChunk() and | 241 // Buffers for collecting data between BeginChunk() and |
| 233 // FinishChunk(). | 242 // FinishChunk(). |
| 234 SBAddPrefixes add_prefixes_; | 243 SBAddPrefixes add_prefixes_; |
| 235 SBSubPrefixes sub_prefixes_; | 244 SBSubPrefixes sub_prefixes_; |
| 236 std::vector<SBAddFullHash> add_hashes_; | 245 std::vector<SBAddFullHash> add_hashes_; |
| 237 std::vector<SBSubFullHash> sub_hashes_; | 246 std::vector<SBSubFullHash> sub_hashes_; |
| 238 | 247 |
| 239 // Count of chunks collected in |new_file_|. | 248 // Count of chunks collected in |new_file_|. |
| 240 int chunks_written_; | 249 int chunks_written_; |
| 241 | 250 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 265 | 274 |
| 266 // Tracks whether corruption has already been seen in the current | 275 // Tracks whether corruption has already been seen in the current |
| 267 // update, so that only one instance is recorded in the stats. | 276 // update, so that only one instance is recorded in the stats. |
| 268 // TODO(shess): Remove with format-migration support. | 277 // TODO(shess): Remove with format-migration support. |
| 269 bool corruption_seen_; | 278 bool corruption_seen_; |
| 270 | 279 |
| 271 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingStoreFile); | 280 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingStoreFile); |
| 272 }; | 281 }; |
| 273 | 282 |
| 274 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_STORE_FILE_H_ | 283 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_STORE_FILE_H_ |
| OLD | NEW |