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

Side by Side Diff: components/safe_browsing_db/database_manager.cc

Issue 1890753002: SafeBrowsing: Track and cancel API checks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@osb-impl-2
Patch Set: Check callback Created 4 years, 8 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 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"
(...skipping 22 matching lines...) Expand all
33 // |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
34 // multiple times during the life of the DatabaseManager. 34 // multiple times during the life of the DatabaseManager.
35 // Must be called on IO thread. 35 // Must be called on IO thread.
36 void SafeBrowsingDatabaseManager::StopOnIOThread(bool shutdown) { 36 void SafeBrowsingDatabaseManager::StopOnIOThread(bool shutdown) {
37 DCHECK_CURRENTLY_ON(BrowserThread::IO); 37 DCHECK_CURRENTLY_ON(BrowserThread::IO);
38 // This cancels all in-flight GetHash requests. 38 // This cancels all in-flight GetHash requests.
39 if (v4_get_hash_protocol_manager_) { 39 if (v4_get_hash_protocol_manager_) {
40 delete v4_get_hash_protocol_manager_; 40 delete v4_get_hash_protocol_manager_;
41 v4_get_hash_protocol_manager_ = NULL; 41 v4_get_hash_protocol_manager_ = NULL;
42 } 42 }
43 // TODO(kcarattini): Call back clients with pending requests. 43
44 // Delete pending checks, calling back any clients with empty metadata.
45 for (auto check : api_checks_) {
46 if (check->client()) {
47 check->client()->
48 OnCheckApiBlacklistUrlResult(check->url(), ThreatMetadata());
49 }
50 }
51 STLDeleteElements(&api_checks_);
52 }
53
54 SafeBrowsingDatabaseManager::CurrentApiChecks::iterator
55 SafeBrowsingDatabaseManager::FindClientApiCheck(Client* client) {
56 DCHECK_CURRENTLY_ON(BrowserThread::IO);
57 for (CurrentApiChecks::iterator it = api_checks_.begin();
58 it != api_checks_.end(); ++it) {
59 if ((*it)->client() == client) {
60 return it;
61 }
62 }
63 return api_checks_.end();
64 }
65
66 bool SafeBrowsingDatabaseManager::CancelApiCheck(Client* client) {
67 DCHECK_CURRENTLY_ON(BrowserThread::IO);
68 CurrentApiChecks::iterator it = FindClientApiCheck(client);
69 if (it != api_checks_.end()) {
70 delete *it;
71 api_checks_.erase(it);
72 return true;
73 }
74 NOTREACHED();
75 return false;
44 } 76 }
45 77
46 bool SafeBrowsingDatabaseManager::CheckApiBlacklistUrl(const GURL& url, 78 bool SafeBrowsingDatabaseManager::CheckApiBlacklistUrl(const GURL& url,
47 Client* client) { 79 Client* client) {
48 DCHECK_CURRENTLY_ON(BrowserThread::IO); 80 DCHECK_CURRENTLY_ON(BrowserThread::IO);
49 DCHECK(v4_get_hash_protocol_manager_); 81 DCHECK(v4_get_hash_protocol_manager_);
50 82
51 // Make sure we can check this url. 83 // Make sure we can check this url.
52 if (!(url.SchemeIs(url::kHttpScheme) || url.SchemeIs(url::kHttpsScheme))) { 84 if (!(url.SchemeIs(url::kHttpScheme) || url.SchemeIs(url::kHttpsScheme))) {
53 return true; 85 return true;
54 } 86 }
55 87
88 // There can only be one in-progress check for the same client at a time.
89 DCHECK(FindClientApiCheck(client) == api_checks_.end());
90
56 // Compute a list of hashes for this url. 91 // Compute a list of hashes for this url.
57 std::vector<SBFullHash> full_hashes; 92 std::vector<SBFullHash> full_hashes;
58 UrlToFullHashes(url, false, &full_hashes); 93 UrlToFullHashes(url, false, &full_hashes);
59 if (full_hashes.empty()) 94 if (full_hashes.empty())
60 return true; 95 return true;
61 96
62 // Copy to prefixes. 97 // Copy to prefixes.
63 std::vector<SBPrefix> prefixes; 98 std::vector<SBPrefix> prefixes;
64 for (const SBFullHash& full_hash : full_hashes) { 99 for (const SBFullHash& full_hash : full_hashes) {
65 prefixes.push_back(full_hash.prefix); 100 prefixes.push_back(full_hash.prefix);
66 } 101 }
67 // Multiple full hashes could share a prefix, remove duplicates. 102 // Multiple full hashes could share a prefix, remove duplicates.
68 std::sort(prefixes.begin(), prefixes.end()); 103 std::sort(prefixes.begin(), prefixes.end());
69 prefixes.erase(std::unique(prefixes.begin(), prefixes.end()), prefixes.end()); 104 prefixes.erase(std::unique(prefixes.begin(), prefixes.end()), prefixes.end());
70 DCHECK(!prefixes.empty()); 105 DCHECK(!prefixes.empty());
71 106
72 // TODO(kcarattini): Track checks in a map. 107 SafeBrowsingApiCheck* check =
73 std::shared_ptr<SafeBrowsingApiCheck> check( 108 new SafeBrowsingApiCheck(url, full_hashes, client);
74 new SafeBrowsingApiCheck(url, full_hashes, client)); 109 api_checks_.insert(check);
75 110
76 // TODO(kcarattini): Implement cache compliance. 111 // TODO(kcarattini): Implement cache compliance.
77 v4_get_hash_protocol_manager_->GetFullHashesWithApis(prefixes, 112 v4_get_hash_protocol_manager_->GetFullHashesWithApis(prefixes,
78 base::Bind(&SafeBrowsingDatabaseManager::HandleGetHashesWithApisResults, 113 base::Bind(&SafeBrowsingDatabaseManager::HandleGetHashesWithApisResults,
79 base::Unretained(this), check)); 114 base::Unretained(this), check));
80 115
81 return false; 116 return false;
82 } 117 }
83 118
84 void SafeBrowsingDatabaseManager::HandleGetHashesWithApisResults( 119 void SafeBrowsingDatabaseManager::HandleGetHashesWithApisResults(
85 std::shared_ptr<SafeBrowsingApiCheck> check, 120 SafeBrowsingApiCheck* check,
86 const std::vector<SBFullHashResult>& full_hash_results, 121 const std::vector<SBFullHashResult>& full_hash_results,
87 const base::TimeDelta& negative_cache_duration) { 122 const base::TimeDelta& negative_cache_duration) {
88 DCHECK_CURRENTLY_ON(BrowserThread::IO); 123 DCHECK_CURRENTLY_ON(BrowserThread::IO);
89 DCHECK(check); 124 DCHECK(check);
90 125
126 // If the check is not in |api_checks_| then the request was cancelled by the
127 // client.
128 CurrentApiChecks::iterator it = api_checks_.find(check);
129 if (it == api_checks_.end())
130 return;
131
91 ThreatMetadata md; 132 ThreatMetadata md;
92 // Merge the metadata from all matching results. 133 // Merge the metadata from all matching results.
93 // TODO(kcarattini): This is O(N^2). Look at improving performance by 134 // TODO(kcarattini): This is O(N^2). Look at improving performance by
94 // using a map, sorting or doing binary search etc.. 135 // using a map, sorting or doing binary search etc..
95 for (const SBFullHashResult& result : full_hash_results) { 136 for (const SBFullHashResult& result : full_hash_results) {
96 for (const SBFullHash& full_hash : check->full_hashes()) { 137 for (const SBFullHash& full_hash : check->full_hashes()) {
97 if (SBFullHashEqual(full_hash, result.hash)) { 138 if (SBFullHashEqual(full_hash, result.hash)) {
98 md.api_permissions.insert(md.api_permissions.end(), 139 md.api_permissions.insert(md.api_permissions.end(),
99 result.metadata.api_permissions.begin(), 140 result.metadata.api_permissions.begin(),
100 result.metadata.api_permissions.end()); 141 result.metadata.api_permissions.end());
101 break; 142 break;
102 } 143 }
103 } 144 }
104 } 145 }
105 146
106 check->client()->OnCheckApiBlacklistUrlResult(check->url(), md); 147 check->client()->OnCheckApiBlacklistUrlResult(check->url(), md);
148 api_checks_.erase(it);
149 delete check;
107 } 150 }
108 151
109 SafeBrowsingDatabaseManager::SafeBrowsingApiCheck::SafeBrowsingApiCheck( 152 SafeBrowsingDatabaseManager::SafeBrowsingApiCheck::SafeBrowsingApiCheck(
110 const GURL& url, const std::vector<SBFullHash>& full_hashes, Client* client) 153 const GURL& url, const std::vector<SBFullHash>& full_hashes, Client* client)
111 : url_(url), full_hashes_(full_hashes), client_(client) { 154 : url_(url), full_hashes_(full_hashes), client_(client) {
112 } 155 }
113 156
114 SafeBrowsingDatabaseManager::SafeBrowsingApiCheck::~SafeBrowsingApiCheck() { 157 SafeBrowsingDatabaseManager::SafeBrowsingApiCheck::~SafeBrowsingApiCheck() {
115 } 158 }
116 159
117 } // namespace safe_browsing 160 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « components/safe_browsing_db/database_manager.h ('k') | components/safe_browsing_db/database_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698