OLD | NEW |
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 StoreReadResult { |
| 24 // No errors. |
| 25 READ_SUCCESS = 0, |
| 26 |
| 27 // Reserved for errors in parsing this enum. |
| 28 UNEXPECTED_READ_FAILURE = 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 different from expected and Chromium doesn't |
| 45 // know how to interpret this version of the file. |
| 46 FILE_VERSION_INCOMPATIBLE_FAILURE = 6, |
| 47 |
| 48 // The rest of the file could not be parsed as a ListUpdateResponse protobuf. |
| 49 // This can happen if the machine crashed before the file was fully written to |
| 50 // disk or if there was disk corruption. |
| 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 STORE_READ_RESULT_MAX |
| 56 }; |
| 57 |
| 58 // Enumerate different failure events while writing the file to disk after |
| 59 // applying updates for histogramming purposes. |
| 60 // DO NOT CHANGE THE ORDERING OF THESE VALUES. |
| 61 enum StoreWriteResult { |
| 62 // No errors. |
| 63 WRITE_SUCCESS = 0, |
| 64 |
| 65 // Reserved for errors in parsing this enum. |
| 66 UNEXPECTED_WRITE_FAILURE = 1, |
| 67 |
| 68 // The proto being written to disk wasn't a FULL_UPDATE proto. |
| 69 INVALID_RESPONSE_TYPE_FAILURE = 2, |
| 70 |
| 71 // Number of bytes written to disk was different from the size of the proto. |
| 72 UNEXPECTED_BYTES_WRITTEN_FAILURE = 3, |
| 73 |
| 74 // Renaming the temporary file to store file failed. |
| 75 UNABLE_TO_RENAME_FAILURE = 4, |
| 76 |
| 77 // Memory space for histograms is determined by the max. ALWAYS |
| 78 // ADD NEW VALUES BEFORE THIS ONE. |
| 79 STORE_WRITE_RESULT_MAX |
| 80 }; |
| 81 |
21 // Factory for creating V4Store. Tests implement this factory to create fake | 82 // Factory for creating V4Store. Tests implement this factory to create fake |
22 // stores for testing. | 83 // stores for testing. |
23 class V4StoreFactory { | 84 class V4StoreFactory { |
24 public: | 85 public: |
25 virtual ~V4StoreFactory() {} | 86 virtual ~V4StoreFactory() {} |
26 virtual V4Store* CreateV4Store( | 87 virtual V4Store* CreateV4Store( |
27 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | 88 const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
28 const base::FilePath& store_path); | 89 const base::FilePath& store_path); |
29 }; | 90 }; |
30 | 91 |
31 class V4Store { | 92 class V4Store { |
32 public: | 93 public: |
33 // The |task_runner| is used to ensure that the operations in this file are | 94 // 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 | 95 // performed on the correct thread. |store_path| specifies the location on |
35 // disk for this file. | 96 // disk for this file. The constructor doesn't read the store file from disk. |
36 V4Store(const scoped_refptr<base::SequencedTaskRunner>& task_runner, | 97 V4Store(const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
37 const base::FilePath& store_path); | 98 const base::FilePath& store_path); |
38 virtual ~V4Store(); | 99 virtual ~V4Store(); |
39 | 100 |
40 const std::string& state() const { return state_; } | 101 const std::string& state() const { return state_; } |
41 | 102 |
42 const base::FilePath& store_path() const { return store_path_; } | 103 const base::FilePath& store_path() const { return store_path_; } |
43 | 104 |
44 void ApplyUpdate(const ListUpdateResponse&, | 105 void ApplyUpdate(const ListUpdateResponse&, |
45 const scoped_refptr<base::SingleThreadTaskRunner>&, | 106 const scoped_refptr<base::SingleThreadTaskRunner>&, |
46 UpdatedStoreReadyCallback); | 107 UpdatedStoreReadyCallback); |
47 | 108 |
48 std::string DebugString() const; | 109 std::string DebugString() const; |
49 | 110 |
| 111 // Reads the store file from disk and populates the in-memory representation |
| 112 // of the hash prefixes. |
| 113 void Initialize(); |
| 114 |
50 // Reset internal state and delete the backing file. | 115 // Reset internal state and delete the backing file. |
51 virtual bool Reset(); | 116 virtual bool Reset(); |
52 | 117 |
53 private: | 118 private: |
| 119 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromEmptyFile); |
| 120 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromAbsentFile); |
| 121 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromInvalidContentsFile); |
| 122 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromUnexpectedMagicFile); |
| 123 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromLowVersionFile); |
| 124 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixInfoFile); |
| 125 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixesFile); |
| 126 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteNoResponseType); |
| 127 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWritePartialResponseType); |
| 128 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteFullResponseType); |
| 129 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromFileWithUnknownProto); |
| 130 |
| 131 // Reads the state of the store from the file on disk and returns the reason |
| 132 // for the failure or reports success. |
| 133 StoreReadResult ReadFromDisk(); |
| 134 |
| 135 // Writes the FULL_UPDATE |response| to disk as a V4StoreFileFormat proto. |
| 136 StoreWriteResult WriteToDisk(const ListUpdateResponse& response) const; |
| 137 |
54 // The state of the store as returned by the PVer4 server in the last applied | 138 // The state of the store as returned by the PVer4 server in the last applied |
55 // update response. | 139 // update response. |
56 std::string state_; | 140 std::string state_; |
57 const base::FilePath store_path_; | 141 const base::FilePath store_path_; |
58 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | 142 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
59 }; | 143 }; |
60 | 144 |
61 std::ostream& operator<<(std::ostream& os, const V4Store& store); | 145 std::ostream& operator<<(std::ostream& os, const V4Store& store); |
62 | 146 |
63 } // namespace safe_browsing | 147 } // namespace safe_browsing |
64 | 148 |
65 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ | 149 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ |
OLD | NEW |