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/android/intercept_download_resource_throttle.h"
7 #include "base/metrics/histogram_macros.h"
8 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_headers.h"
9 #include "content/public/browser/android/download_controller_android.h"
10 #include "content/public/browser/resource_controller.h"
11 #include "net/http/http_request_headers.h"
12 #include "net/http/http_response_headers.h"
13 #include "net/url_request/url_request.h"
17 // UMA histogram for tracking reasons that chrome fails to intercept the
18 // download. Keep this in sync with MobileDownloadInterceptFailureReasons in
20 enum MobileDownloadInterceptFailureReason
{
27 USE_CHANNEL_BOUND_COOKIES
,
28 // FAILURE_REASON_SIZE should always be last - this is a count of the number
29 // of items in this enum.
33 void RecordInterceptFailureReasons(
34 MobileDownloadInterceptFailureReason reason
) {
35 UMA_HISTOGRAM_ENUMERATION("MobileDownload.InterceptFailureReason",
44 InterceptDownloadResourceThrottle::InterceptDownloadResourceThrottle(
45 net::URLRequest
* request
,
46 int render_process_id
,
50 render_process_id_(render_process_id
),
51 render_view_id_(render_view_id
),
52 request_id_(request_id
) {
55 InterceptDownloadResourceThrottle::~InterceptDownloadResourceThrottle() {
58 void InterceptDownloadResourceThrottle::WillProcessResponse(bool* defer
) {
59 ProcessDownloadRequest();
62 const char* InterceptDownloadResourceThrottle::GetNameForLogging() const {
63 return "InterceptDownloadResourceThrottle";
66 void InterceptDownloadResourceThrottle::ProcessDownloadRequest() {
67 if (request_
->url_chain().empty()) {
68 RecordInterceptFailureReasons(EMPTY_URL
);
72 GURL url
= request_
->url_chain().back();
73 if (!url
.SchemeIsHTTPOrHTTPS()) {
74 RecordInterceptFailureReasons(NON_HTTP_OR_HTTPS
);
78 if (request_
->method() != net::HttpRequestHeaders::kGetMethod
) {
79 RecordInterceptFailureReasons(NON_GET_METHODS
);
83 net::HttpRequestHeaders headers
;
84 if (!request_
->GetFullRequestHeaders(&headers
)) {
85 RecordInterceptFailureReasons(NO_REQUEST_HEADERS
);
89 // In general, if the request uses HTTP authorization, either with the origin
90 // or a proxy, then the network stack should handle the download. The one
91 // exception is a request that is fetched via the Chrome Proxy and does not
92 // authenticate with the origin.
93 if (request_
->response_info().did_use_http_auth
) {
94 if (headers
.HasHeader(net::HttpRequestHeaders::kAuthorization
) ||
95 !(request_
->response_info().headers
.get() &&
96 data_reduction_proxy::HasDataReductionProxyViaHeader(
97 request_
->response_info().headers
.get(), NULL
))) {
98 RecordInterceptFailureReasons(USE_HTTP_AUTH
);
103 // If the cookie is possibly channel-bound, don't pass it to android download
105 // TODO(qinmin): add a test for this. http://crbug.com/430541.
106 if (request_
->ssl_info().channel_id_sent
) {
107 RecordInterceptFailureReasons(USE_CHANNEL_BOUND_COOKIES
);
111 content::DownloadControllerAndroid::Get()->CreateGETDownload(
112 render_process_id_
, render_view_id_
, request_id_
);
113 controller()->Cancel();
114 RecordInterceptFailureReasons(NO_FAILURE
);
117 } // namespace chrome