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

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: Incorporate nparker@'s feedback 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 ClientCallbackType {
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 if we can't determine that
42 // synchronously.
Nathan Parker 2016/09/20 18:43:38 Can you add a todo or note that this will be exten
vakh (use Gerrit instead) 2016/09/20 20:51:28 Done.
43 struct PendingCheck {
44 PendingCheck(Client* client,
45 ClientCallbackType client_callback_type,
46 const GURL& url);
47
48 ~PendingCheck();
49
50 // The SafeBrowsing client that's waiting for the safe/unsafe verdict.
51 Client* client;
52
53 // Determines which funtion from the |client| needs to be called once we
54 // know whether the URL in |url| is safe or unsafe.
55 ClientCallbackType client_callback_type;
56
57 // The URL that are being checked for being unsafe.
Nathan Parker 2016/09/20 18:43:37 nit: s/are/is
vakh (use Gerrit instead) 2016/09/20 20:51:28 Done.
58 GURL url;
59
60 // The metadata associated with the full hash of the severest match found
61 // for that URL.
62 ThreatMetadata url_metadata;
63
64 // The threat verdict for the URL being checked.
65 SBThreatType result_threat_type;
66 };
67
29 // Construct V4LocalDatabaseManager. 68 // Construct V4LocalDatabaseManager.
30 // Must be initialized by calling StartOnIOThread() before using. 69 // Must be initialized by calling StartOnIOThread() before using.
31 V4LocalDatabaseManager(const base::FilePath& base_path); 70 V4LocalDatabaseManager(const base::FilePath& base_path);
32 71
33 // 72 //
34 // SafeBrowsingDatabaseManager implementation 73 // SafeBrowsingDatabaseManager implementation
35 // 74 //
36 75
37 bool IsSupported() const override; 76 bool IsSupported() const override;
38 safe_browsing::ThreatSource GetThreatSource() const override; 77 safe_browsing::ThreatSource GetThreatSource() const override;
(...skipping 22 matching lines...) Expand all
61 protected: 100 protected:
62 std::unordered_set<UpdateListIdentifier> GetStoresForFullHashRequests() 101 std::unordered_set<UpdateListIdentifier> GetStoresForFullHashRequests()
63 override; 102 override;
64 103
65 private: 104 private:
66 friend class V4LocalDatabaseManagerTest; 105 friend class V4LocalDatabaseManagerTest;
67 void SetTaskRunnerForTest( 106 void SetTaskRunnerForTest(
68 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { 107 const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
69 task_runner_ = task_runner; 108 task_runner_ = task_runner;
70 } 109 }
110 FRIEND_TEST_ALL_PREFIXES(V4LocalDatabaseManagerTest,
111 TestGetSeverestThreatTypeAndMetadata);
112
113 // The set of clients awaiting a full hash response.
114 typedef std::unordered_set<Client*> PendingClients;
71 115
72 ~V4LocalDatabaseManager() override; 116 ~V4LocalDatabaseManager() override;
73 117
74 // The callback called each time the protocol manager downloads updates 118 // The callback called each time the protocol manager downloads updates
75 // successfully. 119 // successfully.
76 void UpdateRequestCompleted( 120 void UpdateRequestCompleted(
77 std::unique_ptr<ParsedServerResponse> parsed_server_response); 121 std::unique_ptr<ParsedServerResponse> parsed_server_response);
78 122
79 void SetupUpdateProtocolManager( 123 void SetupUpdateProtocolManager(
80 net::URLRequestContextGetter* request_context_getter, 124 net::URLRequestContextGetter* request_context_getter,
81 const V4ProtocolConfig& config); 125 const V4ProtocolConfig& config);
82 126
83 void SetupDatabase(); 127 void SetupDatabase();
84 128
85 void OnFullHashResponse(const std::vector<FullHashInfo>& full_hash_infos); 129 // Called when the |v4_get_hash_protocol_manager_| has the full hash response
130 // avaialble for the URL that we requested. It determines the severest
131 // threat type and responds to the |client| with that information.
132 void OnFullHashResponse(std::unique_ptr<PendingCheck> pending_check,
133 const std::vector<FullHashInfo>& full_hash_infos);
86 134
135 // Called when all the stores managed by the database have been read from
136 // disk after startup and the database is ready for use.
87 void DatabaseReady(std::unique_ptr<V4Database> v4_database); 137 void DatabaseReady(std::unique_ptr<V4Database> v4_database);
88 138
89 // Called when the database has been updated and schedules the next update. 139 // Called when the database has been updated and schedules the next update.
90 void DatabaseUpdated(); 140 void DatabaseUpdated();
91 141
142 // Calls the appopriate method on the |client| object, based on the contents
143 // of |pending_check|.
144 void RespondToClient(std::unique_ptr<PendingCheck> pending_check);
145
146 // Finds the most severe |SBThreatType| and the corresponding |metadata| from
147 // |full_hash_infos|.
148 static void GetSeverestThreatTypeAndMetadata(
149 SBThreatType* result_threat_type,
150 ThreatMetadata* metadata,
151 const std::vector<FullHashInfo>& full_hash_infos);
152
92 // The base directory under which to create the files that contain hashes. 153 // The base directory under which to create the files that contain hashes.
93 const base::FilePath base_path_; 154 const base::FilePath base_path_;
94 155
95 // Whether the service is running. 156 // Whether the service is running.
96 bool enabled_; 157 bool enabled_;
97 158
98 // Stores the current status of the lists to download from the SafeBrowsing 159 // The set of clients that are waiting for a full hash response from the
99 // servers. 160 // SafeBrowsing service.
Nathan Parker 2016/09/20 18:43:38 Add what it's used for: For tracking when a client
vakh (use Gerrit instead) 2016/09/20 20:51:28 Done.
100 // TODO(vakh): current_list_states_ doesn't really belong here. 161 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 162
104 // The list of stores to manage (for hash prefixes and full hashes), along 163 // The list of stores to manage (for hash prefixes and full hashes), along
105 // with the corresponding filename on disk for each of them. 164 // with the corresponding filename on disk for each of them.
106 StoreIdAndFileNames store_id_file_names_; 165 StoreIdAndFileNames store_id_file_names_;
107 166
108 // The protocol manager that downloads the hash prefix updates. 167 // The protocol manager that downloads the hash prefix updates.
109 std::unique_ptr<V4UpdateProtocolManager> v4_update_protocol_manager_; 168 std::unique_ptr<V4UpdateProtocolManager> v4_update_protocol_manager_;
110 169
111 // The database that manages the stores containing the hash prefix updates. 170 // The database that manages the stores containing the hash prefix updates.
112 // All writes to this variable must happen on the IO thread only. 171 // All writes to this variable must happen on the IO thread only.
113 std::unique_ptr<V4Database> v4_database_; 172 std::unique_ptr<V4Database> v4_database_;
114 173
115 // Called when the V4Database has finished applying the latest update and is 174 // Called when the V4Database has finished applying the latest update and is
116 // ready to process next update. 175 // ready to process next update.
117 DatabaseUpdatedCallback db_updated_callback_; 176 DatabaseUpdatedCallback db_updated_callback_;
118 177
119 // The sequenced task runner for running safe browsing database operations. 178 // The sequenced task runner for running safe browsing database operations.
120 scoped_refptr<base::SequencedTaskRunner> task_runner_; 179 scoped_refptr<base::SequencedTaskRunner> task_runner_;
121 180
122 friend class base::RefCountedThreadSafe<V4LocalDatabaseManager>; 181 friend class base::RefCountedThreadSafe<V4LocalDatabaseManager>;
123 DISALLOW_COPY_AND_ASSIGN(V4LocalDatabaseManager); 182 DISALLOW_COPY_AND_ASSIGN(V4LocalDatabaseManager);
124 }; // class V4LocalDatabaseManager 183 }; // class V4LocalDatabaseManager
125 184
126 } // namespace safe_browsing 185 } // namespace safe_browsing
127 186
128 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_LOCAL_DATABASE_MANAGER_H_ 187 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_LOCAL_DATABASE_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698