Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: components/safe_browsing_db/v4_store.h

Issue 2066083002: SafeBrowising: Read and write V4Store from/to disk (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@01_UpdateDbAndStores
Patch Set: Nit: better test names. Tests for WriteToDisk Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ 5 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_
6 #define COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_
7 7
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/sequenced_task_runner.h" 10 #include "base/sequenced_task_runner.h"
11 #include "base/single_thread_task_runner.h" 11 #include "base/single_thread_task_runner.h"
12 #include "components/safe_browsing_db/v4_protocol_manager_util.h" 12 #include "components/safe_browsing_db/v4_protocol_manager_util.h"
13 13
14 namespace safe_browsing { 14 namespace safe_browsing {
15 15
16 class V4Store; 16 class V4Store;
17 17
18 typedef base::Callback<void(std::unique_ptr<V4Store>)> 18 typedef base::Callback<void(std::unique_ptr<V4Store>)>
19 UpdatedStoreReadyCallback; 19 UpdatedStoreReadyCallback;
20 20
21 // Enumerate different failure events while parsing the file read from disk for
22 // histogramming purposes. DO NOT CHANGE THE ORDERING OF THESE VALUES.
23 enum StoreReadFailureType {
24 // Reserved for errors in parsing this enum.
Nathan Parker 2016/06/16 00:07:50 (if this isn't stored in a proto, it probably won'
vakh (use Gerrit instead) 2016/06/16 08:25:18 Acknowledged.
25 UNEXPECTED_FAILURE = 0,
26
27 // No errors.
28 SUCCESS = 1,
29
30 // The contents of the file could not be read.
31 FILE_UNREADABLE_FAILURE = 2,
32
33 // The file was found to be empty.
34 FILE_EMPTY_FAILURE = 3,
35
36 // The contents of the file could not be interpreted as a valid
37 // V4StoreFileFormat proto.
38 PROTO_PARSING_FAILURE = 4,
39
40 // The magic number didn't match. We're most likely trying to read a file
41 // that doesn't contain hash-prefixes.
42 UNEXPECTED_MAGIC_NUMBER_FAILURE = 5,
43
44 // The version of the file is lower than expected and Chromium doesn't know
45 // how to interpret this file anymore.
46 FILE_VERSION_TOO_LOW_FAILURE = 6,
47
48 // The rest of the file could not be parsed as a ListUpdateResponse protobuf.
49 // The only case when this can happen is if the machine crashed before the
50 // file was fully written to disk.
Nathan Parker 2016/06/16 00:07:50 Or any general corruption on disk, ya?
vakh (use Gerrit instead) 2016/06/16 08:25:18 Done.
51 HASH_PREFIX_INFO_MISSING_FAILURE = 7,
52
53 // Memory space for histograms is determined by the max. ALWAYS
54 // ADD NEW VALUES BEFORE THIS ONE.
55 FORMAT_FAILURE_MAX
56 };
57
21 // Factory for creating V4Store. Tests implement this factory to create fake 58 // Factory for creating V4Store. Tests implement this factory to create fake
22 // stores for testing. 59 // stores for testing.
23 class V4StoreFactory { 60 class V4StoreFactory {
24 public: 61 public:
25 virtual ~V4StoreFactory() {} 62 virtual ~V4StoreFactory() {}
26 virtual V4Store* CreateV4Store( 63 virtual V4Store* CreateV4Store(
27 const scoped_refptr<base::SequencedTaskRunner>& task_runner, 64 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
28 const base::FilePath& store_path); 65 const base::FilePath& store_path);
29 }; 66 };
30 67
31 class V4Store { 68 class V4Store {
32 public: 69 public:
33 // The |task_runner| is used to ensure that the operations in this file are 70 // The |task_runner| is used to ensure that the operations in this file are
34 // performed on the correct thread. |store_path| specifies the location on 71 // performed on the correct thread. |store_path| specifies the location on
35 // disk for this file. 72 // disk for this file.
36 V4Store(const scoped_refptr<base::SequencedTaskRunner>& task_runner, 73 V4Store(const scoped_refptr<base::SequencedTaskRunner>& task_runner,
37 const base::FilePath& store_path); 74 const base::FilePath& store_path);
38 virtual ~V4Store(); 75 virtual ~V4Store();
39 76
40 const base::FilePath& store_path() const { return store_path_; } 77 const base::FilePath& store_path() const { return store_path_; }
41 78
42 const std::string& state() const { return state_; } 79 const std::string& state() const { return state_; }
43 80
81 void Initialize();
82
44 void ApplyUpdate(const ListUpdateResponse&, 83 void ApplyUpdate(const ListUpdateResponse&,
45 const scoped_refptr<base::SingleThreadTaskRunner>&, 84 const scoped_refptr<base::SingleThreadTaskRunner>&,
46 UpdatedStoreReadyCallback); 85 UpdatedStoreReadyCallback);
47 86
48 // Reset internal state and delete the backing file. 87 // Reset internal state and delete the backing file.
49 virtual bool Reset(); 88 virtual bool Reset();
50 89
51 std::string DebugString() const; 90 std::string DebugString() const;
52 91
53 protected: 92 protected:
54 const scoped_refptr<base::SequencedTaskRunner> task_runner_; 93 const scoped_refptr<base::SequencedTaskRunner> task_runner_;
55 const base::FilePath store_path_; 94 const base::FilePath store_path_;
56 std::string state_; 95 std::string state_;
57 96
58 private: 97 private:
98 // Creates a new store from an existing one when processing an update.
59 V4Store(const V4Store& other); 99 V4Store(const V4Store& other);
60 100
101 // Reads the state of the store from the file on disk and returns the reason
102 // for the failure or reports success.
103 StoreReadFailureType ReadFromDisk();
104
105 // Writes the FULL_UPDATE |response| to disk as a V4StoreFileFormat proto.
106 // Returns false if the write fails.
107 bool WriteToDisk(const ListUpdateResponse& response) const;
108
61 std::string GetHumanReadableState() const; 109 std::string GetHumanReadableState() const;
110
111 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromEmptyFile);
112 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromAbsentFile);
113 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromInvalidContentsFile);
114 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromUnexpectedMagicFile);
115 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromLowVersionFile);
116 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixInfoFile);
117 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixesFile);
118 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteNoResponseType);
119 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWritePartialResponseType);
120 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteFullResponseType);
62 }; 121 };
63 122
64 inline std::ostream& operator<<(std::ostream& os, const V4Store& store) { 123 inline std::ostream& operator<<(std::ostream& os, const V4Store& store) {
65 os << store.DebugString(); 124 os << store.DebugString();
66 return os; 125 return os;
67 } 126 }
68 127
69 } // namespace safe_browsing 128 } // namespace safe_browsing
70 129
71 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ 130 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698