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/url_request/url_request.h"
11
12namespace chrome {
13
14InterceptDownloadResourceThrottle::InterceptDownloadResourceThrottle(
15    net::URLRequest* request,
16    int render_process_id,
17    int render_view_id,
18    int request_id)
19    : request_(request),
20      render_process_id_(render_process_id),
21      render_view_id_(render_view_id),
22      request_id_(request_id) {
23}
24
25InterceptDownloadResourceThrottle::~InterceptDownloadResourceThrottle() {
26}
27
28void InterceptDownloadResourceThrottle::WillStartRequest(bool* defer) {
29  ProcessDownloadRequest();
30}
31
32void InterceptDownloadResourceThrottle::WillProcessResponse(bool* defer) {
33  ProcessDownloadRequest();
34}
35
36void InterceptDownloadResourceThrottle::ProcessDownloadRequest() {
37  if (request_->method() != net::HttpRequestHeaders::kGetMethod ||
38      request_->response_info().did_use_http_auth)
39    return;
40
41  if (request_->url_chain().empty())
42    return;
43
44  GURL url = request_->url_chain().back();
45  if (!url.SchemeIs("http") && !url.SchemeIs("https"))
46    return;
47
48  content::DownloadControllerAndroid::Get()->CreateGETDownload(
49      render_process_id_, render_view_id_, request_id_);
50  controller()->Cancel();
51}
52
53}  // namespace
54