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.
4
5 #include "chrome/browser/android/intercept_download_resource_throttle.h"
6
7 #include "components/data_reduction_proxy/common/data_reduction_proxy_headers.h"
8 #include "content/public/browser/android/download_controller_android.h"
9 #include "content/public/browser/resource_controller.h"
10 #include "net/http/http_request_headers.h"
11 #include "net/http/http_response_headers.h"
12 #include "net/url_request/url_request.h"
13
14 namespace chrome {
15
InterceptDownloadResourceThrottle(net::URLRequest * request,int render_process_id,int render_view_id,int request_id)16 InterceptDownloadResourceThrottle::InterceptDownloadResourceThrottle(
17 net::URLRequest* request,
18 int render_process_id,
19 int render_view_id,
20 int request_id)
21 : request_(request),
22 render_process_id_(render_process_id),
23 render_view_id_(render_view_id),
24 request_id_(request_id) {
25 }
26
~InterceptDownloadResourceThrottle()27 InterceptDownloadResourceThrottle::~InterceptDownloadResourceThrottle() {
28 }
29
WillStartRequest(bool * defer)30 void InterceptDownloadResourceThrottle::WillStartRequest(bool* defer) {
31 ProcessDownloadRequest();
32 }
33
WillProcessResponse(bool * defer)34 void InterceptDownloadResourceThrottle::WillProcessResponse(bool* defer) {
35 ProcessDownloadRequest();
36 }
37
GetNameForLogging() const38 const char* InterceptDownloadResourceThrottle::GetNameForLogging() const {
39 return "InterceptDownloadResourceThrottle";
40 }
41
ProcessDownloadRequest()42 void InterceptDownloadResourceThrottle::ProcessDownloadRequest() {
43 if (request_->method() != net::HttpRequestHeaders::kGetMethod)
44 return;
45
46 // In general, if the request uses HTTP authorization, either with the origin
47 // or a proxy, then the network stack should handle the download. The one
48 // exception is a request that is fetched via the Chrome Proxy and does not
49 // authenticate with the origin.
50 if (request_->response_info().did_use_http_auth) {
51 #if defined(SPDY_PROXY_AUTH_ORIGIN)
52 net::HttpRequestHeaders headers;
53 request_->GetFullRequestHeaders(&headers);
54 if (headers.HasHeader(net::HttpRequestHeaders::kAuthorization) ||
55 !(request_->response_info().headers &&
56 data_reduction_proxy::HasDataReductionProxyViaHeader(
57 request_->response_info().headers))) {
58 return;
59 }
60 #else
61 return;
62 #endif
63 }
64
65 if (request_->url_chain().empty())
66 return;
67
68 GURL url = request_->url_chain().back();
69 if (!url.SchemeIsHTTPOrHTTPS())
70 return;
71
72 content::DownloadControllerAndroid::Get()->CreateGETDownload(
73 render_process_id_, render_view_id_, request_id_);
74 controller()->Cancel();
75 }
76
77 } // namespace chrome
78