summaryrefslogtreecommitdiffstats
path: root/chrome/browser/download/download_resource_throttle.h
blob: 4798679897adae20791c3e0f9c69a0551e9abc22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_THROTTLE_H_
#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_THROTTLE_H_

#include "base/memory/weak_ptr.h"
#include "chrome/browser/download/download_request_limiter.h"
#include "content/public/browser/resource_throttle.h"

class GURL;

// DownloadResourceThrottle is used to determine if a download should be
// allowed. When a DownloadResourceThrottle is created it pauses the download
// and asks the DownloadRequestLimiter if the download should be allowed. The
// DownloadRequestLimiter notifies us asynchronously as to whether the download
// is allowed or not. If the download is allowed the request is resumed.  If
// the download is not allowed the request is canceled.

class DownloadResourceThrottle
    : public content::ResourceThrottle,
      public base::SupportsWeakPtr<DownloadResourceThrottle> {
 public:
  // Information passed between callbacks to check whether download can proceed.
  struct DownloadRequestInfo {
    DownloadRequestInfo(
        scoped_refptr<DownloadRequestLimiter> limiter,
        const content::ResourceRequestInfo::WebContentsGetter&
            web_contents_getter,
        const GURL& url,
        const std::string& request_method,
        const DownloadRequestLimiter::Callback& continue_callback);
    ~DownloadRequestInfo();

    scoped_refptr<DownloadRequestLimiter> limiter;
    content::ResourceRequestInfo::WebContentsGetter web_contents_getter;
    GURL url;
    std::string request_method;
    DownloadRequestLimiter::Callback continue_callback;
   private:
    DISALLOW_COPY_AND_ASSIGN(DownloadRequestInfo);
  };

  DownloadResourceThrottle(
      scoped_refptr<DownloadRequestLimiter> limiter,
      const content::ResourceRequestInfo::WebContentsGetter&
          web_contents_getter,
      const GURL& url,
      const std::string& request_method);

  // content::ResourceThrottle implementation:
  void WillStartRequest(bool* defer) override;
  void WillRedirectRequest(const net::RedirectInfo& redirect_info,
                           bool* defer) override;
  void WillProcessResponse(bool* defer) override;
  const char* GetNameForLogging() const override;

  void ContinueDownload(bool allow);

 private:
  ~DownloadResourceThrottle() override;

  void WillDownload(bool* defer);

  // Set to true when we are querying the DownloadRequestLimiter.
  bool querying_limiter_;

  // Set to true when we know that the request is allowed to start.
  bool request_allowed_;

  // Set to true when we have deferred the request.
  bool request_deferred_;

  DISALLOW_COPY_AND_ASSIGN(DownloadResourceThrottle);
};

#endif  // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_THROTTLE_H_