OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_ | 5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_ |
6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_ | 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/timer.h" | 13 #include "base/timer.h" |
14 #include "content/browser/renderer_host/resource_handler.h" | 14 #include "content/browser/renderer_host/resource_handler.h" |
15 #include "content/public/browser/download_id.h" | 15 #include "content/public/browser/download_id.h" |
16 #include "content/public/browser/download_manager.h" | 16 #include "content/public/browser/download_manager.h" |
17 #include "content/public/browser/download_save_info.h" | 17 #include "content/public/browser/download_save_info.h" |
18 #include "content/public/browser/download_url_parameters.h" | 18 #include "content/public/browser/download_url_parameters.h" |
19 #include "content/public/browser/global_request_id.h" | 19 #include "content/public/browser/global_request_id.h" |
20 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
21 | 21 |
22 class DownloadFileManager; | 22 class DownloadFileManager; |
23 class DownloadRequestHandle; | 23 class DownloadRequestHandle; |
24 struct DownloadCreateInfo; | 24 struct DownloadCreateInfo; |
25 | 25 |
26 namespace content { | 26 namespace content { |
27 class DownloadBuffer; | 27 class ByteStreamWriter; |
| 28 class ByteStreamReader; |
28 } | 29 } |
29 | 30 |
30 namespace net { | 31 namespace net { |
31 class URLRequest; | 32 class URLRequest; |
32 } // namespace net | 33 } // namespace net |
33 | 34 |
34 // Forwards data to the download thread. | 35 // Forwards data to the download thread. |
35 class DownloadResourceHandler | 36 class DownloadResourceHandler |
36 : public content::ResourceHandler, | 37 : public content::ResourceHandler, |
37 public base::SupportsWeakPtr<DownloadResourceHandler> { | 38 public base::SupportsWeakPtr<DownloadResourceHandler> { |
38 public: | 39 public: |
39 typedef content::DownloadUrlParameters::OnStartedCallback OnStartedCallback; | 40 typedef content::DownloadUrlParameters::OnStartedCallback OnStartedCallback; |
40 | 41 |
41 static const size_t kLoadsToWrite = 100; // number of data buffers queued | |
42 | |
43 // started_cb will be called exactly once on the UI thread. | 42 // started_cb will be called exactly once on the UI thread. |
44 DownloadResourceHandler(int render_process_host_id, | 43 DownloadResourceHandler( |
45 int render_view_id, | 44 int render_process_host_id, |
46 int request_id, | 45 int render_view_id, |
47 const GURL& url, | 46 int request_id, |
48 DownloadFileManager* download_file_manager, | 47 const GURL& url, |
49 net::URLRequest* request, | 48 scoped_refptr<DownloadFileManager> download_file_manager, |
50 const OnStartedCallback& started_cb, | 49 net::URLRequest* request, |
51 const content::DownloadSaveInfo& save_info); | 50 const OnStartedCallback& started_cb, |
| 51 const content::DownloadSaveInfo& save_info); |
52 | 52 |
53 virtual bool OnUploadProgress(int request_id, | 53 virtual bool OnUploadProgress(int request_id, |
54 uint64 position, | 54 uint64 position, |
55 uint64 size) OVERRIDE; | 55 uint64 size) OVERRIDE; |
56 | 56 |
57 // Not needed, as this event handler ought to be the final resource. | 57 // Not needed, as this event handler ought to be the final resource. |
58 virtual bool OnRequestRedirected(int request_id, | 58 virtual bool OnRequestRedirected(int request_id, |
59 const GURL& url, | 59 const GURL& url, |
60 content::ResourceResponse* response, | 60 content::ResourceResponse* response, |
61 bool* defer) OVERRIDE; | 61 bool* defer) OVERRIDE; |
(...skipping 24 matching lines...) Expand all Loading... |
86 | 86 |
87 void PauseRequest(); | 87 void PauseRequest(); |
88 void ResumeRequest(); | 88 void ResumeRequest(); |
89 void CancelRequest(); | 89 void CancelRequest(); |
90 | 90 |
91 std::string DebugString() const; | 91 std::string DebugString() const; |
92 | 92 |
93 private: | 93 private: |
94 virtual ~DownloadResourceHandler(); | 94 virtual ~DownloadResourceHandler(); |
95 | 95 |
96 void OnResponseCompletedInternal(int request_id, | 96 // Arrange for started_cb_ to be called on the UI thread with the |
97 const net::URLRequestStatus& status, | 97 // below values, nulling out started_cb_. Should only be called |
98 const std::string& security_info, | 98 // on the IO thread. |
99 int response_code); | |
100 | |
101 void CheckWriteProgressLater(); | |
102 void CheckWriteProgress(); | |
103 void MaybeResumeRequest(); | |
104 void CallStartedCB(content::DownloadId id, net::Error error); | 99 void CallStartedCB(content::DownloadId id, net::Error error); |
105 | 100 |
106 void SetDownloadID(content::DownloadId id); | |
107 | |
108 // If the content-length header is not present (or contains something other | 101 // If the content-length header is not present (or contains something other |
109 // than numbers), the incoming content_length is -1 (unknown size). | 102 // than numbers), the incoming content_length is -1 (unknown size). |
110 // Set the content length to 0 to indicate unknown size to DownloadManager. | 103 // Set the content length to 0 to indicate unknown size to DownloadManager. |
111 void SetContentLength(const int64& content_length); | 104 void SetContentLength(const int64& content_length); |
112 | 105 |
113 void SetContentDisposition(const std::string& content_disposition); | 106 void SetContentDisposition(const std::string& content_disposition); |
114 | 107 |
115 content::DownloadId download_id_; | |
116 content::GlobalRequestID global_id_; | 108 content::GlobalRequestID global_id_; |
117 int render_view_id_; | 109 int render_view_id_; |
118 scoped_refptr<net::IOBuffer> read_buffer_; | |
119 std::string content_disposition_; | 110 std::string content_disposition_; |
120 int64 content_length_; | 111 int64 content_length_; |
121 scoped_refptr<DownloadFileManager> download_file_manager_; | 112 scoped_refptr<DownloadFileManager> download_file_manager_; |
122 net::URLRequest* request_; | 113 net::URLRequest* request_; |
123 // This is used only on the UI thread. | 114 // This is read only on the IO thread, but may only |
| 115 // be called on the UI thread. |
124 OnStartedCallback started_cb_; | 116 OnStartedCallback started_cb_; |
125 content::DownloadSaveInfo save_info_; | 117 content::DownloadSaveInfo save_info_; |
126 scoped_refptr<content::DownloadBuffer> buffer_; | 118 |
127 base::OneShotTimer<DownloadResourceHandler> check_write_progress_timer_; | 119 // Data flow |
| 120 scoped_refptr<net::IOBuffer> read_buffer_; // From URLRequest. |
| 121 scoped_ptr<content::ByteStreamWriter> stream_writer_; // To rest of system. |
128 | 122 |
129 // The following are used to collect stats. | 123 // The following are used to collect stats. |
130 base::TimeTicks download_start_time_; | 124 base::TimeTicks download_start_time_; |
131 base::TimeTicks last_read_time_; | 125 base::TimeTicks last_read_time_; |
| 126 base::TimeTicks last_stream_pause_time_; |
| 127 base::TimeDelta total_pause_time_; |
132 size_t last_buffer_size_; | 128 size_t last_buffer_size_; |
133 int64 bytes_read_; | 129 int64 bytes_read_; |
134 std::string accept_ranges_; | 130 std::string accept_ranges_; |
135 | 131 |
136 int pause_count_; | 132 int pause_count_; |
137 bool was_deferred_; | 133 bool was_deferred_; |
138 | 134 |
139 // For DCHECKing | 135 // For DCHECKing |
140 bool on_response_started_called_; | 136 bool on_response_started_called_; |
141 | 137 |
142 static const int kReadBufSize = 32768; // bytes | 138 static const int kReadBufSize = 32768; // bytes |
143 static const int kThrottleTimeMs = 200; // milliseconds | 139 static const int kThrottleTimeMs = 200; // milliseconds |
144 | 140 |
145 DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler); | 141 DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler); |
146 }; | 142 }; |
147 | 143 |
148 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_ | 144 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_ |
OLD | NEW |