Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include "components/safe_browsing_db/database_manager.h" | 5 #include "components/safe_browsing_db/database_manager.h" |
| 6 | 6 |
| 7 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" | 7 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" |
| 8 #include "content/public/browser/browser_thread.h" | 8 #include "content/public/browser/browser_thread.h" |
| 9 #include "net/url_request/url_request_context_getter.h" | 9 #include "net/url_request/url_request_context_getter.h" |
| 10 #include "url/gurl.h" | 10 #include "url/gurl.h" |
| 11 | 11 |
| 12 using content::BrowserThread; | 12 using content::BrowserThread; |
| 13 | 13 |
| 14 namespace safe_browsing { | 14 namespace safe_browsing { |
| 15 | 15 |
| 16 SafeBrowsingDatabaseManager::SafeBrowsingDatabaseManager() | 16 SafeBrowsingDatabaseManager::SafeBrowsingDatabaseManager() |
| 17 : v4_get_hash_protocol_manager_(NULL) { | 17 : v4_get_hash_protocol_manager_(NULL) { |
| 18 } | 18 } |
| 19 | 19 |
| 20 SafeBrowsingDatabaseManager::~SafeBrowsingDatabaseManager() { | 20 SafeBrowsingDatabaseManager::~SafeBrowsingDatabaseManager() { |
| 21 DCHECK(v4_get_hash_protocol_manager_ == NULL); | 21 DCHECK(v4_get_hash_protocol_manager_ == NULL); |
| 22 } | 22 } |
| 23 | 23 |
| 24 void SafeBrowsingDatabaseManager::StartOnIOThread( | 24 void SafeBrowsingDatabaseManager::StartOnIOThread( |
| 25 net::URLRequestContextGetter* request_context_getter, | 25 net::URLRequestContextGetter* request_context_getter, |
| 26 const V4ProtocolConfig& config) { | 26 const V4ProtocolConfig& config) { |
| 27 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 27 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 28 if (request_context_getter) { | 28 |
| 29 // Instantiate a V4GetHashProtocolManager. | 29 v4_get_hash_protocol_manager_ = V4GetHashProtocolManager::Create( |
| 30 v4_get_hash_protocol_manager_ = V4GetHashProtocolManager::Create( | 30 request_context_getter, config); |
| 31 request_context_getter, config); | |
| 32 } | |
| 33 } | 31 } |
| 34 | 32 |
| 35 // |shutdown| not used. Destroys the v4 protocol managers. This may be called | 33 // |shutdown| not used. Destroys the v4 protocol managers. This may be called |
| 36 // multiple times during the life of the DatabaseManager. | 34 // multiple times during the life of the DatabaseManager. |
| 37 // Must be called on IO thread. | 35 // Must be called on IO thread. |
| 38 void SafeBrowsingDatabaseManager::StopOnIOThread(bool shutdown) { | 36 void SafeBrowsingDatabaseManager::StopOnIOThread(bool shutdown) { |
| 39 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 37 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 40 // This cancels all in-flight GetHash requests. | 38 // This cancels all in-flight GetHash requests. |
| 41 if (v4_get_hash_protocol_manager_) { | 39 if (v4_get_hash_protocol_manager_) { |
| 42 delete v4_get_hash_protocol_manager_; | 40 delete v4_get_hash_protocol_manager_; |
| 43 v4_get_hash_protocol_manager_ = NULL; | 41 v4_get_hash_protocol_manager_ = NULL; |
| 44 } | 42 } |
| 43 // TODO(kcarattini): Call back clients with pending requests. | |
| 45 } | 44 } |
| 46 | 45 |
| 47 void SafeBrowsingDatabaseManager::CheckApiBlacklistUrl(const GURL& url, | 46 bool SafeBrowsingDatabaseManager::CheckApiBlacklistUrl(const GURL& url, |
| 48 Client* client) { | 47 Client* client) { |
| 49 // TODO(kcarattini): Implement this. | 48 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 49 DCHECK(v4_get_hash_protocol_manager_); | |
| 50 | |
| 51 // Make sure we can check this url. | |
| 52 if (!(url.SchemeIs(url::kHttpScheme) || url.SchemeIs(url::kHttpsScheme))) { | |
| 53 return true; | |
| 54 } | |
| 55 | |
| 56 // Compute a list of hashes for this url. | |
| 57 std::vector<SBFullHash> full_hashes; | |
| 58 UrlToFullHashes(url, false, &full_hashes); | |
| 59 if (full_hashes.empty()) | |
| 60 return true; | |
| 61 | |
| 62 // Copy to prefixes. | |
| 63 std::vector<SBPrefix> prefixes; | |
| 64 for (const SBFullHash& full_hash : full_hashes) { | |
| 65 prefixes.push_back(full_hash.prefix); | |
| 66 } | |
| 67 // Multiple full hashes could share a prefix, remove duplicates. | |
| 68 std::sort(prefixes.begin(), prefixes.end()); | |
| 69 prefixes.erase(std::unique(prefixes.begin(), prefixes.end()), prefixes.end()); | |
| 70 DCHECK(!prefixes.empty()); | |
| 71 | |
| 72 // TODO(kcarattini): Track checks in a map. | |
| 73 std::shared_ptr<SafeBrowsingApiCheck> check( | |
| 74 new SafeBrowsingApiCheck(url, full_hashes, client)); | |
| 75 | |
| 76 // TODO(kcarattini): Implement cache compliance. | |
| 77 v4_get_hash_protocol_manager_->GetFullHashesWithApis(prefixes, | |
| 78 base::Bind(&SafeBrowsingDatabaseManager::HandleGetHashesWithApisResults, | |
| 79 base::Unretained(this), check)); | |
|
Nathan Parker
2016/04/15 23:14:29
The tracking of requests is required so that you c
kcarattini
2016/04/18 06:56:56
Sorry if I'm missing something -- I don't see how
Nathan Parker
2016/04/18 17:26:57
Yea so this class is then protected from those. SG
| |
| 80 | |
| 81 return false; | |
| 82 } | |
| 83 | |
| 84 void SafeBrowsingDatabaseManager::HandleGetHashesWithApisResults( | |
| 85 std::shared_ptr<SafeBrowsingApiCheck> check, | |
| 86 const std::vector<SBFullHashResult>& full_hash_results, | |
| 87 const base::TimeDelta& negative_cache_duration) { | |
| 88 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 89 // TODO(kcarattini): Implement response handler. | |
| 90 } | |
| 91 | |
| 92 SafeBrowsingDatabaseManager::SafeBrowsingApiCheck::SafeBrowsingApiCheck( | |
| 93 const GURL& url, const std::vector<SBFullHash>& full_hashes, Client* client) | |
| 94 : url_(url), full_hashes_(full_hashes), client_(client) { | |
| 95 } | |
| 96 | |
| 97 SafeBrowsingDatabaseManager::SafeBrowsingApiCheck::~SafeBrowsingApiCheck() { | |
| 50 } | 98 } |
| 51 | 99 |
| 52 } // namespace safe_browsing | 100 } // namespace safe_browsing |
| OLD | NEW |