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

Side by Side Diff: chrome/browser/safe_browsing/protocol_manager.cc

Issue 220493003: Safebrowsing: change gethash caching to match api 2.3 rules, fix some corner cases. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: changes for review #2 Created 6 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/safe_browsing/protocol_manager.h" 5 #include "chrome/browser/safe_browsing/protocol_manager.h"
6 6
7 #ifndef NDEBUG 7 #ifndef NDEBUG
8 #include "base/base64.h" 8 #include "base/base64.h"
9 #endif 9 #endif
10 #include "base/environment.h" 10 #include "base/environment.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 UPDATE_RESULT_BACKUP_START = UPDATE_RESULT_BACKUP_CONNECT_FAIL, 47 UPDATE_RESULT_BACKUP_START = UPDATE_RESULT_BACKUP_CONNECT_FAIL,
48 }; 48 };
49 49
50 void RecordUpdateResult(UpdateResult result) { 50 void RecordUpdateResult(UpdateResult result) {
51 DCHECK(result >= 0 && result < UPDATE_RESULT_MAX); 51 DCHECK(result >= 0 && result < UPDATE_RESULT_MAX);
52 UMA_HISTOGRAM_ENUMERATION("SB2.UpdateResult", result, UPDATE_RESULT_MAX); 52 UMA_HISTOGRAM_ENUMERATION("SB2.UpdateResult", result, UPDATE_RESULT_MAX);
53 } 53 }
54 54
55 } // namespace 55 } // namespace
56 56
57 // The maximum staleness for a cached entry.
58 // TODO(mattm): remove this when switching to Safebrowsing API 2.3.
59 static const int kMaxStalenessMinutes = 45;
60
57 // Minimum time, in seconds, from start up before we must issue an update query. 61 // Minimum time, in seconds, from start up before we must issue an update query.
58 static const int kSbTimerStartIntervalSecMin = 60; 62 static const int kSbTimerStartIntervalSecMin = 60;
59 63
60 // Maximum time, in seconds, from start up before we must issue an update query. 64 // Maximum time, in seconds, from start up before we must issue an update query.
61 static const int kSbTimerStartIntervalSecMax = 300; 65 static const int kSbTimerStartIntervalSecMax = 300;
62 66
63 // The maximum time, in seconds, to wait for a response to an update request. 67 // The maximum time, in seconds, to wait for a response to an update request.
64 static const int kSbMaxUpdateWaitSec = 30; 68 static const int kSbMaxUpdateWaitSec = 30;
65 69
66 // Maximum back off multiplier. 70 // Maximum back off multiplier.
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 void SafeBrowsingProtocolManager::GetFullHash( 169 void SafeBrowsingProtocolManager::GetFullHash(
166 const std::vector<SBPrefix>& prefixes, 170 const std::vector<SBPrefix>& prefixes,
167 FullHashCallback callback, 171 FullHashCallback callback,
168 bool is_download) { 172 bool is_download) {
169 DCHECK(CalledOnValidThread()); 173 DCHECK(CalledOnValidThread());
170 // If we are in GetHash backoff, we need to check if we're past the next 174 // If we are in GetHash backoff, we need to check if we're past the next
171 // allowed time. If we are, we can proceed with the request. If not, we are 175 // allowed time. If we are, we can proceed with the request. If not, we are
172 // required to return empty results (i.e. treat the page as safe). 176 // required to return empty results (i.e. treat the page as safe).
173 if (gethash_error_count_ && Time::Now() <= next_gethash_time_) { 177 if (gethash_error_count_ && Time::Now() <= next_gethash_time_) {
174 std::vector<SBFullHashResult> full_hashes; 178 std::vector<SBFullHashResult> full_hashes;
175 callback.Run(full_hashes, false); 179 callback.Run(full_hashes, base::TimeDelta());
176 return; 180 return;
177 } 181 }
178 GURL gethash_url = GetHashUrl(); 182 GURL gethash_url = GetHashUrl();
179 net::URLFetcher* fetcher = net::URLFetcher::Create( 183 net::URLFetcher* fetcher = net::URLFetcher::Create(
180 url_fetcher_id_++, gethash_url, net::URLFetcher::POST, this); 184 url_fetcher_id_++, gethash_url, net::URLFetcher::POST, this);
181 hash_requests_[fetcher] = FullHashDetails(callback, is_download); 185 hash_requests_[fetcher] = FullHashDetails(callback, is_download);
182 186
183 std::string get_hash; 187 std::string get_hash;
184 SafeBrowsingProtocolParser parser; 188 SafeBrowsingProtocolParser parser;
185 parser.FormatGetHash(prefixes, &get_hash); 189 parser.FormatGetHash(prefixes, &get_hash);
(...skipping 25 matching lines...) Expand all
211 DCHECK(CalledOnValidThread()); 215 DCHECK(CalledOnValidThread());
212 scoped_ptr<const net::URLFetcher> fetcher; 216 scoped_ptr<const net::URLFetcher> fetcher;
213 bool parsed_ok = true; 217 bool parsed_ok = true;
214 218
215 HashRequests::iterator it = hash_requests_.find(source); 219 HashRequests::iterator it = hash_requests_.find(source);
216 if (it != hash_requests_.end()) { 220 if (it != hash_requests_.end()) {
217 // GetHash response. 221 // GetHash response.
218 fetcher.reset(it->first); 222 fetcher.reset(it->first);
219 const FullHashDetails& details = it->second; 223 const FullHashDetails& details = it->second;
220 std::vector<SBFullHashResult> full_hashes; 224 std::vector<SBFullHashResult> full_hashes;
221 bool can_cache = false; 225 base::TimeDelta cache_lifetime;
222 if (source->GetStatus().is_success() && 226 if (source->GetStatus().is_success() &&
223 (source->GetResponseCode() == 200 || 227 (source->GetResponseCode() == 200 ||
224 source->GetResponseCode() == 204)) { 228 source->GetResponseCode() == 204)) {
225 // For tracking our GetHash false positive (204) rate, compared to real 229 // For tracking our GetHash false positive (204) rate, compared to real
226 // (200) responses. 230 // (200) responses.
227 if (source->GetResponseCode() == 200) 231 if (source->GetResponseCode() == 200)
228 RecordGetHashResult(details.is_download, GET_HASH_STATUS_200); 232 RecordGetHashResult(details.is_download, GET_HASH_STATUS_200);
229 else 233 else
230 RecordGetHashResult(details.is_download, GET_HASH_STATUS_204); 234 RecordGetHashResult(details.is_download, GET_HASH_STATUS_204);
231 can_cache = true; 235 // In SB 2.3, cache lifetime will be part of the protocol message. For
236 // now, use the old value of 45 minutes.
237 cache_lifetime = base::TimeDelta::FromMinutes(kMaxStalenessMinutes);
232 gethash_error_count_ = 0; 238 gethash_error_count_ = 0;
233 gethash_back_off_mult_ = 1; 239 gethash_back_off_mult_ = 1;
234 SafeBrowsingProtocolParser parser; 240 SafeBrowsingProtocolParser parser;
235 std::string data; 241 std::string data;
236 source->GetResponseAsString(&data); 242 source->GetResponseAsString(&data);
237 parsed_ok = parser.ParseGetHash( 243 parsed_ok = parser.ParseGetHash(
238 data.data(), 244 data.data(),
239 static_cast<int>(data.length()), 245 static_cast<int>(data.length()),
240 &full_hashes); 246 &full_hashes);
241 if (!parsed_ok) { 247 if (!parsed_ok) {
242 full_hashes.clear(); 248 full_hashes.clear();
243 // TODO(cbentzel): Should can_cache be set to false here? 249 // TODO(cbentzel): Should cache_lifetime be set to 0 here?
Scott Hess - ex-Googler 2014/04/03 21:44:03 Is there a tracking thing for this? It seems like
mattm 2014/04/04 23:58:49 Didn't find one, so I filed http://crbug.com/36023
244 } 250 }
245 } else { 251 } else {
246 HandleGetHashError(Time::Now()); 252 HandleGetHashError(Time::Now());
247 if (source->GetStatus().status() == net::URLRequestStatus::FAILED) { 253 if (source->GetStatus().status() == net::URLRequestStatus::FAILED) {
248 VLOG(1) << "SafeBrowsing GetHash request for: " << source->GetURL() 254 VLOG(1) << "SafeBrowsing GetHash request for: " << source->GetURL()
249 << " failed with error: " << source->GetStatus().error(); 255 << " failed with error: " << source->GetStatus().error();
250 } else { 256 } else {
251 VLOG(1) << "SafeBrowsing GetHash request for: " << source->GetURL() 257 VLOG(1) << "SafeBrowsing GetHash request for: " << source->GetURL()
252 << " failed with error: " << source->GetResponseCode(); 258 << " failed with error: " << source->GetResponseCode();
253 } 259 }
254 } 260 }
255 261
256 // Invoke the callback with full_hashes, even if there was a parse error or 262 // Invoke the callback with full_hashes, even if there was a parse error or
257 // an error response code (in which case full_hashes will be empty). The 263 // an error response code (in which case full_hashes will be empty). The
258 // caller can't be blocked indefinitely. 264 // caller can't be blocked indefinitely.
259 details.callback.Run(full_hashes, can_cache); 265 details.callback.Run(full_hashes, cache_lifetime);
260 266
261 hash_requests_.erase(it); 267 hash_requests_.erase(it);
262 } else { 268 } else {
263 // Update or chunk response. 269 // Update or chunk response.
264 fetcher.reset(request_.release()); 270 fetcher.reset(request_.release());
265 271
266 if (request_type_ == UPDATE_REQUEST || 272 if (request_type_ == UPDATE_REQUEST ||
267 request_type_ == BACKUP_UPDATE_REQUEST) { 273 request_type_ == BACKUP_UPDATE_REQUEST) {
268 if (!fetcher.get()) { 274 if (!fetcher.get()) {
269 // We've timed out waiting for an update response, so we've cancelled 275 // We've timed out waiting for an update response, so we've cancelled
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after
772 FullHashCallback callback, bool is_download) 778 FullHashCallback callback, bool is_download)
773 : callback(callback), 779 : callback(callback),
774 is_download(is_download) { 780 is_download(is_download) {
775 } 781 }
776 782
777 SafeBrowsingProtocolManager::FullHashDetails::~FullHashDetails() { 783 SafeBrowsingProtocolManager::FullHashDetails::~FullHashDetails() {
778 } 784 }
779 785
780 SafeBrowsingProtocolManagerDelegate::~SafeBrowsingProtocolManagerDelegate() { 786 SafeBrowsingProtocolManagerDelegate::~SafeBrowsingProtocolManagerDelegate() {
781 } 787 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698