Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / download / download_resource_throttle.cc
blob9d9ae92daf0efa8104b9dcb57dc8f80ab277d6bb
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
3 // found in the LICENSE file.
5 #include "chrome/browser/download/download_resource_throttle.h"
7 #include "base/bind.h"
8 #include "chrome/browser/download/download_stats.h"
9 #include "content/public/browser/resource_controller.h"
11 DownloadResourceThrottle::DownloadResourceThrottle(
12 DownloadRequestLimiter* limiter,
13 int render_process_id,
14 int render_view_id,
15 const GURL& url,
16 const std::string& request_method)
17 : querying_limiter_(true),
18 request_allowed_(false),
19 request_deferred_(false) {
20 limiter->CanDownloadOnIOThread(
21 render_process_id,
22 render_view_id,
23 url,
24 request_method,
25 base::Bind(&DownloadResourceThrottle::ContinueDownload,
26 AsWeakPtr()));
29 DownloadResourceThrottle::~DownloadResourceThrottle() {
32 void DownloadResourceThrottle::WillStartRequest(bool* defer) {
33 WillDownload(defer);
36 void DownloadResourceThrottle::WillRedirectRequest(const GURL& new_url,
37 bool* defer) {
38 WillDownload(defer);
41 void DownloadResourceThrottle::WillProcessResponse(bool* defer) {
42 WillDownload(defer);
45 const char* DownloadResourceThrottle::GetNameForLogging() const {
46 return "DownloadResourceThrottle";
49 void DownloadResourceThrottle::WillDownload(bool* defer) {
50 DCHECK(!request_deferred_);
52 // Defer the download until we have the DownloadRequestLimiter result.
53 if (querying_limiter_) {
54 request_deferred_ = true;
55 *defer = true;
56 return;
59 if (!request_allowed_)
60 controller()->Cancel();
63 void DownloadResourceThrottle::ContinueDownload(bool allow) {
64 querying_limiter_ = false;
65 request_allowed_ = allow;
67 if (allow) {
68 // Presumes all downloads initiated by navigation use this throttle and
69 // nothing else does.
70 RecordDownloadSource(DOWNLOAD_INITIATED_BY_NAVIGATION);
71 } else {
72 RecordDownloadCount(CHROME_DOWNLOAD_COUNT_BLOCKED_BY_THROTTLING);
75 if (request_deferred_) {
76 request_deferred_ = false;
77 if (allow) {
78 controller()->Resume();
79 } else {
80 controller()->Cancel();