[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, |
| 22 | const std::string& url, |
| 23 | DownloadFileManager* manager, |
| 24 | URLRequest* request, |
| 25 | bool save_as); |
| 26 | |
| 27 | // Not needed, as this event handler ought to be the final resource. |
| 28 | bool OnRequestRedirected(int request_id, const GURL& url); |
| 29 | |
| 30 | // Send the download creation information to the download thread. |
| 31 | bool OnResponseStarted(int request_id, ResourceResponse* response); |
| 32 | |
| 33 | // Create a new buffer, which will be handed to the download thread for file |
| 34 | // writing and deletion. |
| 35 | bool OnWillRead(int request_id, char** buf, int* buf_size, int min_size); |
| 36 | |
| 37 | bool OnReadCompleted(int request_id, int* bytes_read); |
| 38 | |
| 39 | bool OnResponseCompleted(int request_id, const URLRequestStatus& status); |
| 40 | |
| 41 | // If the content-length header is not present (or contains something other |
| 42 | // than numbers), the incoming content_length is -1 (unknown size). |
| 43 | // Set the content length to 0 to indicate unknown size to DownloadManager. |
| 44 | void set_content_length(const int64& content_length); |
| 45 | |
| 46 | void set_content_disposition(const std::string& content_disposition); |
| 47 | |
| 48 | void CheckWriteProgress(); |
| 49 | |
| 50 | private: |
| 51 | void StartPauseTimer(); |
| 52 | |
| 53 | int download_id_; |
| 54 | ResourceDispatcherHost::GlobalRequestID global_id_; |
| 55 | int render_view_id_; |
| 56 | char* read_buffer_; |
| 57 | std::string content_disposition_; |
| 58 | std::wstring url_; |
| 59 | int64 content_length_; |
| 60 | DownloadFileManager* download_manager_; |
| 61 | URLRequest* request_; |
| 62 | bool save_as_; // Request was initiated via "Save As" by the user. |
| 63 | DownloadBuffer* buffer_; |
| 64 | ResourceDispatcherHost* rdh_; |
| 65 | bool is_paused_; |
| 66 | base::OneShotTimer<DownloadResourceHandler> pause_timer_; |
| 67 | |
| 68 | static const int kReadBufSize = 32768; // bytes |
[email protected] | 3c2fe4e | 2009-01-08 12:07:23 | [diff] [blame^] | 69 | static const size_t kLoadsToWrite = 100; // number of data buffers queued |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 70 | static const int kThrottleTimeMs = 200; // milliseconds |
| 71 | |
| 72 | DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler); |
| 73 | }; |
| 74 | |
| 75 | #endif // CHROME_BROWSER_RENDERER_HOST_DOWNLOAD_RESOURCE_HANDLER_H_ |