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 // This file should not be build on Android but is currently getting built. | 5 // This file should not be build on Android but is currently getting built. |
| 6 // TODO(vakh): Fix that: http://crbug.com/621647 | 6 // TODO(vakh): Fix that: http://crbug.com/621647 |
| 7 | 7 |
| 8 #include "components/safe_browsing_db/v4_local_database_manager.h" | 8 #include "components/safe_browsing_db/v4_local_database_manager.h" |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" | |
| 14 #include "components/safe_browsing_db/v4_protocol_manager_util.h" | |
| 15 #include "components/safe_browsing_db/v4_update_protocol_manager.h" | |
| 13 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 14 | 17 |
| 15 using content::BrowserThread; | 18 using content::BrowserThread; |
| 16 | 19 |
| 17 namespace safe_browsing { | 20 namespace safe_browsing { |
| 18 | 21 |
| 19 namespace { | 22 namespace { |
| 20 | 23 |
| 21 // TODO(vakh): Implement this to populate the map appopriately. | 24 // TODO(vakh): Implement this to populate the map appopriately. |
| 22 // Filed as http://crbug.com/608075 | 25 // Filed as http://crbug.com/608075 |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 return true; | 143 return true; |
| 141 } | 144 } |
| 142 | 145 |
| 143 if (v4_database_) { | 146 if (v4_database_) { |
| 144 base::hash_set<FullHash> full_hashes; | 147 base::hash_set<FullHash> full_hashes; |
| 145 V4ProtocolManagerUtil::UrlToFullHashes(url, &full_hashes); | 148 V4ProtocolManagerUtil::UrlToFullHashes(url, &full_hashes); |
| 146 | 149 |
| 147 base::hash_set<UpdateListIdentifier> stores_to_look( | 150 base::hash_set<UpdateListIdentifier> stores_to_look( |
| 148 {GetUrlMalwareId(), GetUrlSocEngId()}); | 151 {GetUrlMalwareId(), GetUrlSocEngId()}); |
| 149 base::hash_set<HashPrefix> matched_hash_prefixes; | 152 base::hash_set<HashPrefix> matched_hash_prefixes; |
| 150 base::hash_set<UpdateListIdentifier> matched_stores; | 153 PlatformTypeSet matched_platforms; |
| 154 ThreatEntryTypeSet matched_threat_entry_types; | |
| 155 ThreatTypeSet matched_threat_types; | |
| 151 MatchedHashPrefixMap matched_hash_prefix_map; | 156 MatchedHashPrefixMap matched_hash_prefix_map; |
| 152 for (const auto& full_hash : full_hashes) { | 157 for (const auto& full_hash : full_hashes) { |
| 153 v4_database_->GetStoresMatchingFullHash(full_hash, stores_to_look, | 158 v4_database_->GetStoresMatchingFullHash(full_hash, stores_to_look, |
| 154 &matched_hash_prefix_map); | 159 &matched_hash_prefix_map); |
| 155 for (const auto& matched_pair : matched_hash_prefix_map) { | 160 for (const auto& matched_pair : matched_hash_prefix_map) { |
| 156 matched_stores.insert(matched_pair.first); | |
| 157 matched_hash_prefixes.insert(matched_pair.second); | 161 matched_hash_prefixes.insert(matched_pair.second); |
| 162 matched_platforms.insert(matched_pair.first.platform_type); | |
| 163 matched_threat_entry_types.insert(matched_pair.first.threat_entry_type); | |
| 164 matched_threat_types.insert(matched_pair.first.threat_type); | |
| 158 } | 165 } |
| 159 } | 166 } |
| 160 | 167 |
| 161 DCHECK_EQ(matched_stores.empty(), matched_hash_prefixes.empty()); | 168 DCHECK_EQ(matched_hash_prefixes.empty(), matched_platforms.empty()); |
| 162 | 169 DCHECK_EQ(matched_hash_prefixes.empty(), |
| 163 // TODO(vakh): Return false and fetch full hashes for the matching hash | 170 matched_threat_entry_types.empty()); |
| 164 // prefixes. | 171 DCHECK_EQ(matched_hash_prefixes.empty(), matched_threat_types.empty()); |
|
Scott Hess - ex-Googler
2016/08/23 20:25:46
Since they all have to match, I'd pick the shortes
| |
| 165 return matched_hash_prefixes.empty(); | 172 if (matched_hash_prefixes.empty()) { |
| 173 // No matching hash prefixes in the database, URL is good. | |
| 174 return true; | |
| 175 } else { | |
| 176 // Request full hashes. | |
| 177 // TODO(vakh): Record the information needed to respond to the client | |
| 178 // when the full hash check returns. | |
| 179 v4_get_hash_protocol_manager_->GetFullHashes( | |
| 180 matched_hash_prefixes, matched_platforms, matched_threat_entry_types, | |
| 181 matched_threat_types, | |
| 182 base::Bind(&V4LocalDatabaseManager::HandleGetHashes, | |
| 183 base::Unretained(this))); | |
| 184 return false; | |
| 185 } | |
| 166 } else { | 186 } else { |
| 167 // TODO(vakh): Queue the check and process it when the database becomes | 187 // TODO(vakh): Queue the check and process it when the database becomes |
| 168 // ready. | 188 // ready. |
| 169 return false; | 189 return false; |
| 170 } | 190 } |
| 171 } | 191 } |
| 172 | 192 |
| 193 void V4LocalDatabaseManager::HandleGetHashes( | |
| 194 const std::vector<SBFullHashResult>& full_hash_results, | |
| 195 const base::Time& negative_cache_expire) { | |
| 196 // TODO(vakh): Implement this skeleton. | |
| 197 // Ideally this should get a hash_set of FullHash objects. That will be done | |
| 198 // after http://crbug.com/616647 is fixed. | |
| 199 } | |
| 200 | |
| 173 void V4LocalDatabaseManager::CancelCheck(Client* client) { | 201 void V4LocalDatabaseManager::CancelCheck(Client* client) { |
| 174 // TODO(vakh): Implement this skeleton. | 202 // TODO(vakh): Implement this skeleton. |
| 175 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 203 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 176 DCHECK(enabled_); | 204 DCHECK(enabled_); |
| 177 } | 205 } |
| 178 | 206 |
| 179 void V4LocalDatabaseManager::StartOnIOThread( | 207 void V4LocalDatabaseManager::StartOnIOThread( |
| 180 net::URLRequestContextGetter* request_context_getter, | 208 net::URLRequestContextGetter* request_context_getter, |
| 181 const V4ProtocolConfig& config) { | 209 const V4ProtocolConfig& config) { |
| 182 SafeBrowsingDatabaseManager::StartOnIOThread(request_context_getter, config); | 210 SafeBrowsingDatabaseManager::StartOnIOThread(request_context_getter, config); |
| 183 | 211 |
| 184 db_updated_callback_ = base::Bind(&V4LocalDatabaseManager::DatabaseUpdated, | 212 db_updated_callback_ = base::Bind(&V4LocalDatabaseManager::DatabaseUpdated, |
| 185 base::Unretained(this)); | 213 base::Unretained(this)); |
| 186 | 214 |
| 187 SetupUpdateProtocolManager(request_context_getter, config); | 215 SetupUpdateProtocolManager(request_context_getter, config); |
| 188 | 216 |
| 217 SetupGetHashProtocolManager(request_context_getter, config); | |
| 218 | |
| 189 SetupDatabase(); | 219 SetupDatabase(); |
| 190 | 220 |
| 191 enabled_ = true; | 221 enabled_ = true; |
| 192 } | 222 } |
| 193 | 223 |
| 194 void V4LocalDatabaseManager::SetupUpdateProtocolManager( | 224 void V4LocalDatabaseManager::SetupUpdateProtocolManager( |
| 195 net::URLRequestContextGetter* request_context_getter, | 225 net::URLRequestContextGetter* request_context_getter, |
| 196 const V4ProtocolConfig& config) { | 226 const V4ProtocolConfig& config) { |
| 197 V4UpdateCallback callback = base::Bind( | 227 V4UpdateCallback callback = base::Bind( |
| 198 &V4LocalDatabaseManager::UpdateRequestCompleted, base::Unretained(this)); | 228 &V4LocalDatabaseManager::UpdateRequestCompleted, base::Unretained(this)); |
| 199 | 229 |
| 200 v4_update_protocol_manager_ = | 230 v4_update_protocol_manager_ = |
| 201 V4UpdateProtocolManager::Create(request_context_getter, config, callback); | 231 V4UpdateProtocolManager::Create(request_context_getter, config, callback); |
| 202 } | 232 } |
| 203 | 233 |
| 234 void V4LocalDatabaseManager::SetupGetHashProtocolManager( | |
| 235 net::URLRequestContextGetter* request_context_getter, | |
| 236 const V4ProtocolConfig& config) { | |
| 237 v4_get_hash_protocol_manager_.reset( | |
| 238 V4GetHashProtocolManager::Create(request_context_getter, config)); | |
| 239 } | |
| 240 | |
| 204 void V4LocalDatabaseManager::SetupDatabase() { | 241 void V4LocalDatabaseManager::SetupDatabase() { |
| 205 DCHECK(!base_path_.empty()); | 242 DCHECK(!base_path_.empty()); |
| 206 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 243 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 207 | 244 |
| 208 // Only get a new task runner if there isn't one already. If the service has | 245 // Only get a new task runner if there isn't one already. If the service has |
| 209 // previously been started and stopped, a task runner could already exist. | 246 // previously been started and stopped, a task runner could already exist. |
| 210 if (!task_runner_) { | 247 if (!task_runner_) { |
| 211 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); | 248 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 212 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( | 249 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| 213 pool->GetSequenceToken(), base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 250 pool->GetSequenceToken(), base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 245 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { | 282 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { |
| 246 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 283 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 247 | 284 |
| 248 enabled_ = false; | 285 enabled_ = false; |
| 249 | 286 |
| 250 // Delete the V4Database. Any pending writes to disk are completed. | 287 // Delete the V4Database. Any pending writes to disk are completed. |
| 251 // This operation happens on the task_runner on which v4_database_ operates | 288 // This operation happens on the task_runner on which v4_database_ operates |
| 252 // and doesn't block the IO thread. | 289 // and doesn't block the IO thread. |
| 253 V4Database::Destroy(std::move(v4_database_)); | 290 V4Database::Destroy(std::move(v4_database_)); |
| 254 | 291 |
| 292 // Delete the V4GetHashProtocolManager. | |
| 293 // This cancels any in-flight get-hash request. | |
| 294 v4_get_hash_protocol_manager_.reset(); | |
| 295 | |
| 255 // Delete the V4UpdateProtocolManager. | 296 // Delete the V4UpdateProtocolManager. |
| 256 // This cancels any in-flight update request. | 297 // This cancels any in-flight update request. |
| 257 v4_update_protocol_manager_.reset(); | 298 v4_update_protocol_manager_.reset(); |
| 258 | 299 |
| 259 db_updated_callback_.Reset(); | 300 db_updated_callback_.Reset(); |
| 260 | 301 |
| 261 SafeBrowsingDatabaseManager::StopOnIOThread(shutdown); | 302 SafeBrowsingDatabaseManager::StopOnIOThread(shutdown); |
| 262 } | 303 } |
| 263 | 304 |
| 264 void V4LocalDatabaseManager::UpdateRequestCompleted( | 305 void V4LocalDatabaseManager::UpdateRequestCompleted( |
| 265 std::unique_ptr<ParsedServerResponse> parsed_server_response) { | 306 std::unique_ptr<ParsedServerResponse> parsed_server_response) { |
| 266 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 307 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 267 v4_database_->ApplyUpdate(std::move(parsed_server_response), | 308 v4_database_->ApplyUpdate(std::move(parsed_server_response), |
| 268 db_updated_callback_); | 309 db_updated_callback_); |
| 269 } | 310 } |
| 270 | 311 |
| 271 void V4LocalDatabaseManager::DatabaseUpdated() { | 312 void V4LocalDatabaseManager::DatabaseUpdated() { |
| 272 if (enabled_) { | 313 if (enabled_) { |
| 273 v4_update_protocol_manager_->ScheduleNextUpdate( | 314 v4_update_protocol_manager_->ScheduleNextUpdate( |
| 274 v4_database_->GetStoreStateMap()); | 315 v4_database_->GetStoreStateMap()); |
| 275 } | 316 } |
| 276 } | 317 } |
| 277 | 318 |
| 278 } // namespace safe_browsing | 319 } // namespace safe_browsing |
| OLD | NEW |