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