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