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