• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_RENDERER_HOST_DOWNLOAD_THROTTLING_RESOURCE_HANDLER_H_
6 #define CHROME_BROWSER_RENDERER_HOST_DOWNLOAD_THROTTLING_RESOURCE_HANDLER_H_
7 #pragma once
8 
9 #include <string>
10 
11 #include "chrome/browser/download/download_request_limiter.h"
12 #include "content/browser/renderer_host/resource_handler.h"
13 #include "googleurl/src/gurl.h"
14 
15 class DownloadResourceHandler;
16 class ResourceDispatcherHost;
17 
18 namespace net {
19 class URLRequest;
20 }  // namespace net
21 
22 // DownloadThrottlingResourceHandler is used to determine if a download should
23 // be allowed. When a DownloadThrottlingResourceHandler is created it pauses the
24 // download and asks the DownloadRequestLimiter if the download should be
25 // allowed. The DownloadRequestLimiter notifies us asynchronously as to whether
26 // the download is allowed or not. If the download is allowed the request is
27 // resumed, a DownloadResourceHandler is created and all EventHandler methods
28 // are delegated to it. If the download is not allowed the request is canceled.
29 
30 class DownloadThrottlingResourceHandler
31     : public ResourceHandler,
32       public DownloadRequestLimiter::Callback {
33  public:
34   DownloadThrottlingResourceHandler(ResourceDispatcherHost* host,
35                                     net::URLRequest* request,
36                                     const GURL& url,
37                                     int render_process_host_id,
38                                     int render_view_id,
39                                     int request_id,
40                                     bool in_complete);
41 
42   // ResourceHanlder implementation:
43   virtual bool OnUploadProgress(int request_id,
44                                 uint64 position,
45                                 uint64 size);
46   virtual bool OnRequestRedirected(int request_id, const GURL& url,
47                                    ResourceResponse* response, bool* defer);
48   virtual bool OnResponseStarted(int request_id, ResourceResponse* response);
49   virtual bool OnWillStart(int request_id, const GURL& url, bool* defer);
50   virtual bool OnWillRead(int request_id, net::IOBuffer** buf, int* buf_size,
51                           int min_size);
52   virtual bool OnReadCompleted(int request_id, int* bytes_read);
53   virtual bool OnResponseCompleted(int request_id,
54                                    const net::URLRequestStatus& status,
55                                    const std::string& security_info);
56   virtual void OnRequestClosed();
57 
58   // DownloadRequestLimiter::Callback implementation:
59   virtual void CancelDownload();
60   virtual void ContinueDownload();
61 
62  private:
63   virtual ~DownloadThrottlingResourceHandler();
64 
65   void CopyTmpBufferToDownloadHandler();
66 
67   ResourceDispatcherHost* host_;
68   net::URLRequest* request_;
69   GURL url_;
70   int render_process_host_id_;
71   int render_view_id_;
72   int request_id_;
73 
74   // Handles the actual download. This is only created if the download is
75   // allowed to continue.
76   scoped_refptr<DownloadResourceHandler> download_handler_;
77 
78   // Response supplied to OnResponseStarted. Only non-null if OnResponseStarted
79   // is invoked.
80   scoped_refptr<ResourceResponse> response_;
81 
82   // If we're created by way of BufferedEventHandler we'll get one request for
83   // a buffer. This is that buffer.
84   scoped_refptr<net::IOBuffer> tmp_buffer_;
85   int tmp_buffer_length_;
86 
87   // If true the next call to OnReadCompleted is ignored. This is used if we're
88   // paused during a call to OnReadCompleted. Pausing during OnReadCompleted
89   // results in two calls to OnReadCompleted for the same data. This make sure
90   // we ignore one of them.
91   bool ignore_on_read_complete_;
92 
93   // Have we received OnRequestClosed()?  If so, we shouldn't act on
94   // CancelDownload()/ContinueDownload().
95   bool request_closed_;
96 
97   DISALLOW_COPY_AND_ASSIGN(DownloadThrottlingResourceHandler);
98 };
99 
100 #endif  // CHROME_BROWSER_RENDERER_HOST_DOWNLOAD_THROTTLING_RESOURCE_HANDLER_H_
101