Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / chrome / browser / download / download_resource_throttle.cc
blob9578d33917c9b49fb6436628a5c220f5694faab0
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/browser_thread.h"
10 #include "content/public/browser/resource_controller.h"
12 #if defined(OS_ANDROID)
13 #include "content/public/browser/android/download_controller_android.h"
14 #include "content/public/browser/render_view_host.h"
16 using content::DownloadControllerAndroid;
17 #endif
19 using content::BrowserThread;
21 namespace {
23 void OnCanDownloadDecided(base::WeakPtr<DownloadResourceThrottle> throttle,
24 bool allow) {
25 BrowserThread::PostTask(
26 BrowserThread::IO, FROM_HERE,
27 base::Bind(&DownloadResourceThrottle::ContinueDownload, throttle, allow));
30 void CanDownload(
31 scoped_ptr<DownloadResourceThrottle::DownloadRequestInfo> info) {
32 DCHECK_CURRENTLY_ON(BrowserThread::UI);
33 info->limiter->CanDownload(info->render_process_id, info->render_view_id,
34 info->url, info->request_method,
35 info->continue_callback);
38 #if defined(OS_ANDROID)
39 void OnAcquireFileAccessPermissionDone(
40 scoped_ptr<DownloadResourceThrottle::DownloadRequestInfo> info,
41 bool granted) {
42 DCHECK_CURRENTLY_ON(BrowserThread::UI);
43 if (granted)
44 CanDownload(info.Pass());
45 else
46 info->continue_callback.Run(false);
48 #endif
50 void CanDownloadOnUIThread(
51 scoped_ptr<DownloadResourceThrottle::DownloadRequestInfo> info) {
52 DCHECK_CURRENTLY_ON(BrowserThread::UI);
53 #if defined(OS_ANDROID)
54 int process_id = info->render_process_id;
55 int render_view_id = info->render_view_id;
56 content::DownloadControllerAndroid::Get()->AcquireFileAccessPermission(
57 process_id, render_view_id, base::Bind(&OnAcquireFileAccessPermissionDone,
58 base::Passed(info.Pass())));
59 #else
60 CanDownload(info.Pass());
61 #endif
64 } // namespace
66 DownloadResourceThrottle::DownloadRequestInfo::DownloadRequestInfo(
67 scoped_refptr<DownloadRequestLimiter> limiter,
68 int render_process_id,
69 int render_view_id,
70 const GURL& url,
71 const std::string& request_method,
72 const DownloadRequestLimiter::Callback& continue_callback)
73 : limiter(limiter),
74 render_process_id(render_process_id),
75 render_view_id(render_view_id),
76 url(url),
77 request_method(request_method),
78 continue_callback(continue_callback) {}
80 DownloadResourceThrottle::DownloadRequestInfo::~DownloadRequestInfo() {}
82 DownloadResourceThrottle::DownloadResourceThrottle(
83 scoped_refptr<DownloadRequestLimiter> limiter,
84 int render_process_id,
85 int render_view_id,
86 const GURL& url,
87 const std::string& request_method)
88 : querying_limiter_(true),
89 request_allowed_(false),
90 request_deferred_(false) {
91 DCHECK_CURRENTLY_ON(BrowserThread::IO);
92 BrowserThread::PostTask(
93 BrowserThread::UI, FROM_HERE,
94 base::Bind(
95 &CanDownloadOnUIThread,
96 base::Passed(scoped_ptr<DownloadRequestInfo>(new DownloadRequestInfo(
97 limiter, render_process_id, render_view_id, url, request_method,
98 base::Bind(&OnCanDownloadDecided, AsWeakPtr()))))));
101 DownloadResourceThrottle::~DownloadResourceThrottle() {
104 void DownloadResourceThrottle::WillStartRequest(bool* defer) {
105 WillDownload(defer);
108 void DownloadResourceThrottle::WillRedirectRequest(
109 const net::RedirectInfo& redirect_info,
110 bool* defer) {
111 WillDownload(defer);
114 void DownloadResourceThrottle::WillProcessResponse(bool* defer) {
115 WillDownload(defer);
118 const char* DownloadResourceThrottle::GetNameForLogging() const {
119 return "DownloadResourceThrottle";
122 void DownloadResourceThrottle::WillDownload(bool* defer) {
123 DCHECK(!request_deferred_);
125 // Defer the download until we have the DownloadRequestLimiter result.
126 if (querying_limiter_) {
127 request_deferred_ = true;
128 *defer = true;
129 return;
132 if (!request_allowed_)
133 controller()->Cancel();
136 void DownloadResourceThrottle::ContinueDownload(bool allow) {
137 DCHECK_CURRENTLY_ON(BrowserThread::IO);
138 querying_limiter_ = false;
139 request_allowed_ = allow;
141 if (allow) {
142 // Presumes all downloads initiated by navigation use this throttle and
143 // nothing else does.
144 RecordDownloadSource(DOWNLOAD_INITIATED_BY_NAVIGATION);
145 } else {
146 RecordDownloadCount(CHROME_DOWNLOAD_COUNT_BLOCKED_BY_THROTTLING);
149 if (request_deferred_) {
150 request_deferred_ = false;
151 if (allow) {
152 controller()->Resume();
153 } else {
154 controller()->Cancel();