Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / android / intercept_download_resource_throttle.cc
blob1592233f81895c4a8ad36a5e77f949d2378b0100
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"
15 namespace {
17 // UMA histogram for tracking reasons that chrome fails to intercept the
18 // download. Keep this in sync with MobileDownloadInterceptFailureReasons in
19 // histograms.xml.
20 enum MobileDownloadInterceptFailureReason {
21 NO_FAILURE = 0,
22 EMPTY_URL,
23 NON_HTTP_OR_HTTPS,
24 NON_GET_METHODS,
25 NO_REQUEST_HEADERS,
26 USE_HTTP_AUTH,
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.
30 FAILURE_REASON_SIZE,
33 void RecordInterceptFailureReasons(
34 MobileDownloadInterceptFailureReason reason) {
35 UMA_HISTOGRAM_ENUMERATION("MobileDownload.InterceptFailureReason",
36 reason,
37 FAILURE_REASON_SIZE);
40 } // namespace
42 namespace chrome {
44 InterceptDownloadResourceThrottle::InterceptDownloadResourceThrottle(
45 net::URLRequest* request,
46 int render_process_id,
47 int render_view_id,
48 int request_id)
49 : request_(request),
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);
69 return;
72 GURL url = request_->url_chain().back();
73 if (!url.SchemeIsHTTPOrHTTPS()) {
74 RecordInterceptFailureReasons(NON_HTTP_OR_HTTPS);
75 return;
78 if (request_->method() != net::HttpRequestHeaders::kGetMethod) {
79 RecordInterceptFailureReasons(NON_GET_METHODS);
80 return;
83 net::HttpRequestHeaders headers;
84 if (!request_->GetFullRequestHeaders(&headers)) {
85 RecordInterceptFailureReasons(NO_REQUEST_HEADERS);
86 return;
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);
99 return;
103 // If the cookie is possibly channel-bound, don't pass it to android download
104 // manager.
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);
108 return;
111 content::DownloadControllerAndroid::Get()->CreateGETDownload(
112 render_process_id_, render_view_id_, request_id_);
113 controller()->Cancel();
114 RecordInterceptFailureReasons(NO_FAILURE);
117 } // namespace chrome