[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 5 | #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_ |
| 6 | #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 8 | |
| 9 | #include <string> |
| 10 | |
[email protected] | 8e3ae68c | 2011-09-16 22:15:47 | [diff] [blame] | 11 | #include "base/callback.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 12 | #include "base/memory/scoped_ptr.h" |
[email protected] | 92b24c1 | 2009-12-10 20:04:35 | [diff] [blame] | 13 | #include "base/timer.h" |
[email protected] | 71bf3f5e | 2011-08-15 21:05:22 | [diff] [blame] | 14 | #include "content/browser/download/download_file.h" |
[email protected] | eda5840 | 2011-09-21 19:32:02 | [diff] [blame] | 15 | #include "content/browser/download/download_id.h" |
[email protected] | 5de63471 | 2011-03-02 00:20:19 | [diff] [blame] | 16 | #include "content/browser/renderer_host/global_request_id.h" |
| 17 | #include "content/browser/renderer_host/resource_handler.h" |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 18 | |
[email protected] | 92b24c1 | 2009-12-10 20:04:35 | [diff] [blame] | 19 | class DownloadFileManager; |
| 20 | class ResourceDispatcherHost; |
[email protected] | 2606e5a9 | 2011-10-21 00:09:52 | [diff] [blame] | 21 | |
| 22 | namespace content { |
| 23 | class DownloadBuffer; |
| 24 | } |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 25 | |
[email protected] | edfe7fab | 2010-11-28 13:11:52 | [diff] [blame] | 26 | namespace net { |
| 27 | class URLRequest; |
| 28 | } // namespace net |
| 29 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 30 | // Forwards data to the download thread. |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 31 | class DownloadResourceHandler : public ResourceHandler { |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 32 | public: |
[email protected] | eda5840 | 2011-09-21 19:32:02 | [diff] [blame] | 33 | typedef base::Callback<void(DownloadId, net::Error)> |
[email protected] | 8e3ae68c | 2011-09-16 22:15:47 | [diff] [blame] | 34 | OnStartedCallback; |
| 35 | |
| 36 | // started_cb will be called exactly once. |
[email protected] | c2475df | 2011-09-07 23:52:22 | [diff] [blame] | 37 | DownloadResourceHandler(ResourceDispatcherHost* rdh, |
| 38 | int render_process_host_id, |
| 39 | int render_view_id, |
| 40 | int request_id, |
| 41 | const GURL& url, |
[email protected] | eda5840 | 2011-09-21 19:32:02 | [diff] [blame] | 42 | DownloadId dl_id, |
[email protected] | c2475df | 2011-09-07 23:52:22 | [diff] [blame] | 43 | DownloadFileManager* download_file_manager, |
| 44 | net::URLRequest* request, |
| 45 | bool save_as, |
[email protected] | 8e3ae68c | 2011-09-16 22:15:47 | [diff] [blame] | 46 | const OnStartedCallback& started_cb, |
[email protected] | c2475df | 2011-09-07 23:52:22 | [diff] [blame] | 47 | const DownloadSaveInfo& save_info); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 48 | |
[email protected] | 78994ab0 | 2010-12-08 18:06:44 | [diff] [blame] | 49 | virtual bool OnUploadProgress(int request_id, uint64 position, uint64 size); |
[email protected] | afd832c | 2010-03-02 04:53:31 | [diff] [blame] | 50 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 51 | // Not needed, as this event handler ought to be the final resource. |
[email protected] | 78994ab0 | 2010-12-08 18:06:44 | [diff] [blame] | 52 | virtual bool OnRequestRedirected(int request_id, const GURL& url, |
| 53 | ResourceResponse* response, bool* defer); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 54 | |
| 55 | // Send the download creation information to the download thread. |
[email protected] | 78994ab0 | 2010-12-08 18:06:44 | [diff] [blame] | 56 | virtual bool OnResponseStarted(int request_id, ResourceResponse* response); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 57 | |
[email protected] | afd832c | 2010-03-02 04:53:31 | [diff] [blame] | 58 | // Pass-through implementation. |
[email protected] | 78994ab0 | 2010-12-08 18:06:44 | [diff] [blame] | 59 | virtual bool OnWillStart(int request_id, const GURL& url, bool* defer); |
[email protected] | afd832c | 2010-03-02 04:53:31 | [diff] [blame] | 60 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 61 | // Create a new buffer, which will be handed to the download thread for file |
| 62 | // writing and deletion. |
[email protected] | 78994ab0 | 2010-12-08 18:06:44 | [diff] [blame] | 63 | virtual bool OnWillRead(int request_id, net::IOBuffer** buf, int* buf_size, |
| 64 | int min_size); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 65 | |
[email protected] | 78994ab0 | 2010-12-08 18:06:44 | [diff] [blame] | 66 | virtual bool OnReadCompleted(int request_id, int* bytes_read); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 67 | |
[email protected] | 78994ab0 | 2010-12-08 18:06:44 | [diff] [blame] | 68 | virtual bool OnResponseCompleted(int request_id, |
[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 69 | const net::URLRequestStatus& status, |
[email protected] | 78994ab0 | 2010-12-08 18:06:44 | [diff] [blame] | 70 | const std::string& security_info); |
| 71 | virtual void OnRequestClosed(); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 72 | |
| 73 | // If the content-length header is not present (or contains something other |
| 74 | // than numbers), the incoming content_length is -1 (unknown size). |
| 75 | // Set the content length to 0 to indicate unknown size to DownloadManager. |
| 76 | void set_content_length(const int64& content_length); |
| 77 | |
| 78 | void set_content_disposition(const std::string& content_disposition); |
| 79 | |
| 80 | void CheckWriteProgress(); |
| 81 | |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 82 | std::string DebugString() const; |
| 83 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 84 | private: |
[email protected] | 3690ebe0 | 2011-05-25 09:08:19 | [diff] [blame] | 85 | virtual ~DownloadResourceHandler(); |
[email protected] | 84bbb2b5 | 2009-11-07 00:23:14 | [diff] [blame] | 86 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 87 | void StartPauseTimer(); |
[email protected] | 8e3ae68c | 2011-09-16 22:15:47 | [diff] [blame] | 88 | void CallStartedCB(net::Error error); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 89 | |
[email protected] | eda5840 | 2011-09-21 19:32:02 | [diff] [blame] | 90 | DownloadId download_id_; |
[email protected] | 92b24c1 | 2009-12-10 20:04:35 | [diff] [blame] | 91 | GlobalRequestID global_id_; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 92 | int render_view_id_; |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 93 | scoped_refptr<net::IOBuffer> read_buffer_; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 94 | std::string content_disposition_; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 95 | int64 content_length_; |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 96 | DownloadFileManager* download_file_manager_; |
[email protected] | edfe7fab | 2010-11-28 13:11:52 | [diff] [blame] | 97 | net::URLRequest* request_; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 98 | bool save_as_; // Request was initiated via "Save As" by the user. |
[email protected] | 8e3ae68c | 2011-09-16 22:15:47 | [diff] [blame] | 99 | OnStartedCallback started_cb_; |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 100 | DownloadSaveInfo save_info_; |
[email protected] | 2606e5a9 | 2011-10-21 00:09:52 | [diff] [blame] | 101 | scoped_refptr<content::DownloadBuffer> buffer_; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 102 | ResourceDispatcherHost* rdh_; |
| 103 | bool is_paused_; |
| 104 | base::OneShotTimer<DownloadResourceHandler> pause_timer_; |
[email protected] | 9fc1e6c1 | 2010-12-22 21:07:05 | [diff] [blame] | 105 | base::TimeTicks download_start_time_; // used to collect stats. |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 106 | static const int kReadBufSize = 32768; // bytes |
[email protected] | 3c2fe4e | 2009-01-08 12:07:23 | [diff] [blame] | 107 | static const size_t kLoadsToWrite = 100; // number of data buffers queued |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 108 | static const int kThrottleTimeMs = 200; // milliseconds |
| 109 | |
| 110 | DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler); |
| 111 | }; |
| 112 | |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 113 | #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_ |