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

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

Issue 2349603003: V4LDBM: Get response from GetHashManager, detect severest result (Closed)
Patch Set: Add test for GetSeverestThreatTypeAndMetadata Created 4 years, 3 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_LOCAL_DATABASE_MANAGER_H_ 5 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_LOCAL_DATABASE_MANAGER_H_
6 #define COMPONENTS_SAFE_BROWSING_DB_V4_LOCAL_DATABASE_MANAGER_H_ 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_LOCAL_DATABASE_MANAGER_H_
7 7
8 // A class that provides the interface between the SafeBrowsing protocol manager 8 // A class that provides the interface between the SafeBrowsing protocol manager
9 // and database that holds the downloaded updates. 9 // and database that holds the downloaded updates.
10 10
11 #include <memory> 11 #include <memory>
12 12
13 #include "components/safe_browsing_db/database_manager.h" 13 #include "components/safe_browsing_db/database_manager.h"
14 #include "components/safe_browsing_db/hit_report.h" 14 #include "components/safe_browsing_db/hit_report.h"
15 #include "components/safe_browsing_db/v4_database.h" 15 #include "components/safe_browsing_db/v4_database.h"
16 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" 16 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h"
17 #include "components/safe_browsing_db/v4_protocol_manager_util.h" 17 #include "components/safe_browsing_db/v4_protocol_manager_util.h"
18 #include "components/safe_browsing_db/v4_update_protocol_manager.h" 18 #include "components/safe_browsing_db/v4_update_protocol_manager.h"
19 #include "url/gurl.h" 19 #include "url/gurl.h"
20 20
21 using content::ResourceType; 21 using content::ResourceType;
22 22
23 namespace safe_browsing { 23 namespace safe_browsing {
24 24
25 typedef unsigned ThreatSeverity;
26
25 // Manages the local, on-disk database of updates downloaded from the 27 // Manages the local, on-disk database of updates downloaded from the
26 // SafeBrowsing service and interfaces with the protocol manager. 28 // SafeBrowsing service and interfaces with the protocol manager.
27 class V4LocalDatabaseManager : public SafeBrowsingDatabaseManager { 29 class V4LocalDatabaseManager : public SafeBrowsingDatabaseManager {
28 public: 30 public:
31 enum class CheckType {
Nathan Parker 2016/09/20 17:51:09 I think this name is a bit too general. Does this
vakh (use Gerrit instead) 2016/09/20 18:21:17 Yes, it is supposed to reflect the function to cal
Scott Hess - ex-Googler 2016/09/20 21:55:55 OMG +++!!!1!111!!oneoneone
32 // This represents the case when we're trying to determine if a URL is
33 // unsafe from the following perspectives: Malware, Phishing, UwS.
34 CHECK_BROWSE_URL = 0,
35
36 // This should always be the last value.
37 CHECK_MAX
38 };
39
40 // The information we need to return the response to the SafeBrowsing client
41 // that asked for the safety reputation of a URL (or a full hash) if we can't
42 // determine that synchronously.
43 struct PendingCheck {
Nathan Parker 2016/09/20 17:51:09 Will this be used just for single URL checks, or o
vakh (use Gerrit instead) 2016/09/20 18:21:17 URLs for now. Other types later.
44 PendingCheck(CheckType check_type, Client* client, const GURL& url);
45
46 ~PendingCheck();
47
48 // Determines which funtion from the |client| needs to be called once we
49 // know whether each of the the URL(s) in |urls| is safe or unsafe.
50 CheckType check_type;
51
52 // The SafeBrowsing client that's waiting for the safe/unsafe verdict.
53 Client* client;
54
55 // The URL that are being checked for being unsafe.
56 GURL url;
Nathan Parker 2016/09/20 17:51:09 Should this be a vector, like the comments above i
vakh (use Gerrit instead) 2016/09/20 18:21:17 Not now, later.
57
58 // The metadata associated with the full hash of the severest match found
59 // for that URL.
60 ThreatMetadata url_metadata;
61
62 // The threat verdict for the URL being checked.
63 SBThreatType result_threat_type;
64 };
65
29 // Construct V4LocalDatabaseManager. 66 // Construct V4LocalDatabaseManager.
30 // Must be initialized by calling StartOnIOThread() before using. 67 // Must be initialized by calling StartOnIOThread() before using.
31 V4LocalDatabaseManager(const base::FilePath& base_path); 68 V4LocalDatabaseManager(const base::FilePath& base_path);
32 69
33 // 70 //
34 // SafeBrowsingDatabaseManager implementation 71 // SafeBrowsingDatabaseManager implementation
35 // 72 //
36 73
37 bool IsSupported() const override; 74 bool IsSupported() const override;
38 safe_browsing::ThreatSource GetThreatSource() const override; 75 safe_browsing::ThreatSource GetThreatSource() const override;
(...skipping 22 matching lines...) Expand all
61 protected: 98 protected:
62 std::unordered_set<UpdateListIdentifier> GetStoresForFullHashRequests() 99 std::unordered_set<UpdateListIdentifier> GetStoresForFullHashRequests()
63 override; 100 override;
64 101
65 private: 102 private:
66 friend class V4LocalDatabaseManagerTest; 103 friend class V4LocalDatabaseManagerTest;
67 void SetTaskRunnerForTest( 104 void SetTaskRunnerForTest(
68 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { 105 const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
69 task_runner_ = task_runner; 106 task_runner_ = task_runner;
70 } 107 }
108 FRIEND_TEST_ALL_PREFIXES(V4LocalDatabaseManagerTest,
109 TestGetSeverestThreatTypeAndMetadata);
110
111 // The set of clients awaiting a full hash response.
112 typedef std::unordered_set<Client*> PendingClients;
71 113
72 ~V4LocalDatabaseManager() override; 114 ~V4LocalDatabaseManager() override;
73 115
74 // The callback called each time the protocol manager downloads updates 116 // The callback called each time the protocol manager downloads updates
75 // successfully. 117 // successfully.
76 void UpdateRequestCompleted( 118 void UpdateRequestCompleted(
77 std::unique_ptr<ParsedServerResponse> parsed_server_response); 119 std::unique_ptr<ParsedServerResponse> parsed_server_response);
78 120
79 void SetupUpdateProtocolManager( 121 void SetupUpdateProtocolManager(
80 net::URLRequestContextGetter* request_context_getter, 122 net::URLRequestContextGetter* request_context_getter,
81 const V4ProtocolConfig& config); 123 const V4ProtocolConfig& config);
82 124
83 void SetupDatabase(); 125 void SetupDatabase();
84 126
85 void OnFullHashResponse(const std::vector<FullHashInfo>& full_hash_infos); 127 // Called when the |v4_get_hash_protocol_manager_| has the full hash response
128 // avaialble for the URL that we requested. It determines the severest
129 // threat type and responds to the |client| with that information.
130 void OnFullHashResponse(std::unique_ptr<PendingCheck> pending_check,
131 const std::vector<FullHashInfo>& full_hash_infos);
86 132
133 // Called when all the stores managed by the database have been read from
134 // disk after startup and the database is ready for use.
87 void DatabaseReady(std::unique_ptr<V4Database> v4_database); 135 void DatabaseReady(std::unique_ptr<V4Database> v4_database);
88 136
89 // Called when the database has been updated and schedules the next update. 137 // Called when the database has been updated and schedules the next update.
90 void DatabaseUpdated(); 138 void DatabaseUpdated();
91 139
140 // Calls the appopriate method on the |client| object, based on the contents
141 // of |pending_check|.
142 void RespondToClient(std::unique_ptr<PendingCheck> pending_check);
143
144 // Finds the most severe |SBThreatType| and the corresponding |metadata| from
145 // |full_hash_infos|.
146 static void GetSeverestThreatTypeAndMetadata(
147 SBThreatType* result_threat_type,
148 ThreatMetadata* metadata,
149 const std::vector<FullHashInfo>& full_hash_infos);
150
92 // The base directory under which to create the files that contain hashes. 151 // The base directory under which to create the files that contain hashes.
93 const base::FilePath base_path_; 152 const base::FilePath base_path_;
94 153
95 // Whether the service is running. 154 // Whether the service is running.
96 bool enabled_; 155 bool enabled_;
97 156
98 // Stores the current status of the lists to download from the SafeBrowsing 157 // The set of clients that are waiting for a gull hash response from the
Nathan Parker 2016/09/20 17:51:09 nit: s/gull/full
vakh (use Gerrit instead) 2016/09/20 18:21:17 Done.
99 // servers. 158 // SafeBrowsing service.
100 // TODO(vakh): current_list_states_ doesn't really belong here. 159 PendingClients pending_clients_;
101 // It should come through the database, from the various V4Stores.
102 base::hash_map<UpdateListIdentifier, std::string> current_list_states_;
103 160
104 // The list of stores to manage (for hash prefixes and full hashes), along 161 // The list of stores to manage (for hash prefixes and full hashes), along
105 // with the corresponding filename on disk for each of them. 162 // with the corresponding filename on disk for each of them.
106 StoreIdAndFileNames store_id_file_names_; 163 StoreIdAndFileNames store_id_file_names_;
107 164
108 // The protocol manager that downloads the hash prefix updates. 165 // The protocol manager that downloads the hash prefix updates.
109 std::unique_ptr<V4UpdateProtocolManager> v4_update_protocol_manager_; 166 std::unique_ptr<V4UpdateProtocolManager> v4_update_protocol_manager_;
110 167
111 // The database that manages the stores containing the hash prefix updates. 168 // The database that manages the stores containing the hash prefix updates.
112 // All writes to this variable must happen on the IO thread only. 169 // All writes to this variable must happen on the IO thread only.
113 std::unique_ptr<V4Database> v4_database_; 170 std::unique_ptr<V4Database> v4_database_;
114 171
115 // Called when the V4Database has finished applying the latest update and is 172 // Called when the V4Database has finished applying the latest update and is
116 // ready to process next update. 173 // ready to process next update.
117 DatabaseUpdatedCallback db_updated_callback_; 174 DatabaseUpdatedCallback db_updated_callback_;
118 175
119 // The sequenced task runner for running safe browsing database operations. 176 // The sequenced task runner for running safe browsing database operations.
120 scoped_refptr<base::SequencedTaskRunner> task_runner_; 177 scoped_refptr<base::SequencedTaskRunner> task_runner_;
121 178
122 friend class base::RefCountedThreadSafe<V4LocalDatabaseManager>; 179 friend class base::RefCountedThreadSafe<V4LocalDatabaseManager>;
123 DISALLOW_COPY_AND_ASSIGN(V4LocalDatabaseManager); 180 DISALLOW_COPY_AND_ASSIGN(V4LocalDatabaseManager);
124 }; // class V4LocalDatabaseManager 181 }; // class V4LocalDatabaseManager
125 182
126 } // namespace safe_browsing 183 } // namespace safe_browsing
127 184
128 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_LOCAL_DATABASE_MANAGER_H_ 185 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_LOCAL_DATABASE_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698