[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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_RESOURCE_HANDLER_H_ |
| 6 | #define CHROME_BROWSER_RENDERER_HOST_DOWNLOAD_RESOURCE_HANDLER_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "chrome/browser/renderer_host/resource_dispatcher_host.h" |
| 11 | #include "chrome/browser/renderer_host/resource_handler.h" |
| 12 | |
| 13 | struct DownloadBuffer; |
| 14 | |
| 15 | // Forwards data to the download thread. |
| 16 | class DownloadResourceHandler : public ResourceHandler { |
| 17 | public: |
| 18 | DownloadResourceHandler(ResourceDispatcherHost* rdh, |
| 19 | int render_process_host_id, |
| 20 | int render_view_id, |
| 21 | int request_id, |
[email protected] | f6b4853 | 2009-02-12 01:56:32 | [diff] [blame] | 22 | const GURL& url, |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 23 | DownloadFileManager* manager, |
| 24 | URLRequest* request, |
| 25 | bool save_as); |
| 26 | |
| 27 | // Not needed, as this event handler ought to be the final resource. |
[email protected] | 6568a9e3 | 2009-07-30 18:01:39 | [diff] [blame] | 28 | bool OnRequestRedirected(int request_id, const GURL& url, |
| 29 | ResourceResponse* response, bool* defer); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 30 | |
| 31 | // Send the download creation information to the download thread. |
| 32 | bool OnResponseStarted(int request_id, ResourceResponse* response); |
| 33 | |
| 34 | // Create a new buffer, which will be handed to the download thread for file |
| 35 | // writing and deletion. |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 36 | bool OnWillRead(int request_id, net::IOBuffer** buf, int* buf_size, |
| 37 | int min_size); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 38 | |
| 39 | bool OnReadCompleted(int request_id, int* bytes_read); |
| 40 | |
[email protected] | c4891b3 | 2009-03-08 07:41:31 | [diff] [blame] | 41 | bool OnResponseCompleted(int request_id, |
| 42 | const URLRequestStatus& status, |
| 43 | const std::string& security_info); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 44 | |
| 45 | // If the content-length header is not present (or contains something other |
| 46 | // than numbers), the incoming content_length is -1 (unknown size). |
| 47 | // Set the content length to 0 to indicate unknown size to DownloadManager. |
| 48 | void set_content_length(const int64& content_length); |
| 49 | |
| 50 | void set_content_disposition(const std::string& content_disposition); |
| 51 | |
| 52 | void CheckWriteProgress(); |
| 53 | |
| 54 | private: |
[email protected] | 84bbb2b5 | 2009-11-07 00:23:14 | [diff] [blame^] | 55 | ~DownloadResourceHandler() {} |
| 56 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 57 | void StartPauseTimer(); |
| 58 | |
| 59 | int download_id_; |
| 60 | ResourceDispatcherHost::GlobalRequestID global_id_; |
| 61 | int render_view_id_; |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 62 | scoped_refptr<net::IOBuffer> read_buffer_; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 63 | std::string content_disposition_; |
[email protected] | f6b4853 | 2009-02-12 01:56:32 | [diff] [blame] | 64 | GURL url_; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 65 | int64 content_length_; |
| 66 | DownloadFileManager* download_manager_; |
| 67 | URLRequest* request_; |
| 68 | bool save_as_; // Request was initiated via "Save As" by the user. |
| 69 | DownloadBuffer* buffer_; |
| 70 | ResourceDispatcherHost* rdh_; |
| 71 | bool is_paused_; |
| 72 | base::OneShotTimer<DownloadResourceHandler> pause_timer_; |
| 73 | |
| 74 | static const int kReadBufSize = 32768; // bytes |
[email protected] | 3c2fe4e | 2009-01-08 12:07:23 | [diff] [blame] | 75 | static const size_t kLoadsToWrite = 100; // number of data buffers queued |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 76 | static const int kThrottleTimeMs = 200; // milliseconds |
| 77 | |
| 78 | DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler); |
| 79 | }; |
| 80 | |
| 81 | #endif // CHROME_BROWSER_RENDERER_HOST_DOWNLOAD_RESOURCE_HANDLER_H_ |