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"
8 #include "base/profiler/scoped_tracker.h"
9 #include "chrome/browser/download/download_stats.h"
10 #include "content/public/browser/resource_controller.h"
12 DownloadResourceThrottle::DownloadResourceThrottle(
13 DownloadRequestLimiter
* limiter
,
14 int render_process_id
,
17 const std::string
& request_method
)
18 : querying_limiter_(true),
19 request_allowed_(false),
20 request_deferred_(false) {
21 limiter
->CanDownloadOnIOThread(
26 base::Bind(&DownloadResourceThrottle::ContinueDownload
,
30 DownloadResourceThrottle::~DownloadResourceThrottle() {
33 void DownloadResourceThrottle::WillStartRequest(bool* defer
) {
37 void DownloadResourceThrottle::WillRedirectRequest(
38 const net::RedirectInfo
& redirect_info
,
43 void DownloadResourceThrottle::WillProcessResponse(bool* defer
) {
44 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
45 tracked_objects::ScopedTracker
tracking_profile(
46 FROM_HERE_WITH_EXPLICIT_FUNCTION(
47 "422516 DownloadResourceThrottle::WillProcessResponse"));
52 const char* DownloadResourceThrottle::GetNameForLogging() const {
53 return "DownloadResourceThrottle";
56 void DownloadResourceThrottle::WillDownload(bool* defer
) {
57 DCHECK(!request_deferred_
);
59 // Defer the download until we have the DownloadRequestLimiter result.
60 if (querying_limiter_
) {
61 request_deferred_
= true;
66 if (!request_allowed_
)
67 controller()->Cancel();
70 void DownloadResourceThrottle::ContinueDownload(bool allow
) {
71 querying_limiter_
= false;
72 request_allowed_
= allow
;
75 // Presumes all downloads initiated by navigation use this throttle and
77 RecordDownloadSource(DOWNLOAD_INITIATED_BY_NAVIGATION
);
79 RecordDownloadCount(CHROME_DOWNLOAD_COUNT_BLOCKED_BY_THROTTLING
);
82 if (request_deferred_
) {
83 request_deferred_
= false;
85 controller()->Resume();
87 controller()->Cancel();