Chromium Code Reviews| Index: components/safe_browsing_db/database_manager.cc |
| diff --git a/components/safe_browsing_db/database_manager.cc b/components/safe_browsing_db/database_manager.cc |
| index 218e8e038c8e3f0f139a4931d53ab2d51c490b97..5c38a0ec588c348ea05b807dff2f4aeeb5e1738a 100644 |
| --- a/components/safe_browsing_db/database_manager.cc |
| +++ b/components/safe_browsing_db/database_manager.cc |
| @@ -40,7 +40,39 @@ void SafeBrowsingDatabaseManager::StopOnIOThread(bool shutdown) { |
| delete v4_get_hash_protocol_manager_; |
| v4_get_hash_protocol_manager_ = NULL; |
| } |
| - // TODO(kcarattini): Call back clients with pending requests. |
| + |
| + // Delete pending checks, calling back any clients with empty metadata. |
| + for (auto check : api_checks_) { |
| + if (check->client()) { |
| + check->client()-> |
| + OnCheckApiBlacklistUrlResult(check->url(), ThreatMetadata()); |
| + } |
| + } |
| + STLDeleteElements(&api_checks_); |
| +} |
| + |
| +SafeBrowsingDatabaseManager::CurrentApiChecks::iterator |
| +SafeBrowsingDatabaseManager::FindClientCheck(Client* client) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + for (CurrentApiChecks::iterator it = api_checks_.begin(); |
|
Nathan Parker
2016/04/19 18:05:19
for (auto it : api_checks)..
And if you want to g
kcarattini
2016/04/20 01:55:53
I'm ok with leaving as-is.
|
| + it != api_checks_.end(); ++it) { |
| + if ((*it)->client() == client) { |
| + return it; |
| + } |
| + } |
| + return api_checks_.end(); |
| +} |
| + |
| +bool SafeBrowsingDatabaseManager::CancelApiCheck(Client* client) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + CurrentApiChecks::iterator it = FindClientCheck(client); |
| + if (it != api_checks_.end()) { |
| + delete *it; |
| + api_checks_.erase(it); |
| + return true; |
| + } |
| + NOTREACHED(); |
| + return false; |
| } |
| bool SafeBrowsingDatabaseManager::CheckApiBlacklistUrl(const GURL& url, |
| @@ -53,6 +85,9 @@ bool SafeBrowsingDatabaseManager::CheckApiBlacklistUrl(const GURL& url, |
| return true; |
| } |
| + // There can only be one in-progress check for the same client at a time. |
| + DCHECK(FindClientCheck(client) == api_checks_.end()); |
| + |
| // Compute a list of hashes for this url. |
| std::vector<SBFullHash> full_hashes; |
| UrlToFullHashes(url, false, &full_hashes); |
| @@ -69,9 +104,9 @@ bool SafeBrowsingDatabaseManager::CheckApiBlacklistUrl(const GURL& url, |
| prefixes.erase(std::unique(prefixes.begin(), prefixes.end()), prefixes.end()); |
| DCHECK(!prefixes.empty()); |
| - // TODO(kcarattini): Track checks in a map. |
| - std::shared_ptr<SafeBrowsingApiCheck> check( |
| - new SafeBrowsingApiCheck(url, full_hashes, client)); |
| + SafeBrowsingApiCheck* check = |
| + new SafeBrowsingApiCheck(url, full_hashes, client); |
| + api_checks_.insert(check); |
| // TODO(kcarattini): Implement cache compliance. |
| v4_get_hash_protocol_manager_->GetFullHashesWithApis(prefixes, |
| @@ -82,12 +117,18 @@ bool SafeBrowsingDatabaseManager::CheckApiBlacklistUrl(const GURL& url, |
| } |
| void SafeBrowsingDatabaseManager::HandleGetHashesWithApisResults( |
| - std::shared_ptr<SafeBrowsingApiCheck> check, |
| + SafeBrowsingApiCheck* check, |
| const std::vector<SBFullHashResult>& full_hash_results, |
| const base::TimeDelta& negative_cache_duration) { |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| DCHECK(check); |
| + // If the check is not in |api_checks_| then the request was cancelled by the |
| + // client. |
| + CurrentApiChecks::iterator it = api_checks_.find(check); |
| + if (it == api_checks_.end()) |
| + return; |
| + |
| ThreatMetadata md; |
| // Merge the metadata from all matching results. |
| // TODO(kcarattini): This is O(N^2). Look at improving performance by |
| @@ -104,6 +145,8 @@ void SafeBrowsingDatabaseManager::HandleGetHashesWithApisResults( |
| } |
| check->client()->OnCheckApiBlacklistUrlResult(check->url(), md); |
| + api_checks_.erase(it); |
| + delete check; |
| } |
| SafeBrowsingDatabaseManager::SafeBrowsingApiCheck::SafeBrowsingApiCheck( |