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 : public ResourceHandler { | 36 class DownloadResourceHandler : public ResourceHandler { |
36 public: | 37 public: |
37 typedef content::DownloadUrlParameters::OnStartedCallback OnStartedCallback; | 38 typedef content::DownloadUrlParameters::OnStartedCallback OnStartedCallback; |
38 | 39 |
39 static const size_t kLoadsToWrite = 100; // number of data buffers queued | |
40 | |
41 // started_cb will be called exactly once on the UI thread. | 40 // started_cb will be called exactly once on the UI thread. |
42 DownloadResourceHandler(int render_process_host_id, | 41 DownloadResourceHandler( |
43 int render_view_id, | 42 int render_process_host_id, |
44 int request_id, | 43 int render_view_id, |
45 const GURL& url, | 44 int request_id, |
46 DownloadFileManager* download_file_manager, | 45 const GURL& url, |
47 net::URLRequest* request, | 46 scoped_refptr<DownloadFileManager> download_file_manager, |
48 const OnStartedCallback& started_cb, | 47 net::URLRequest* request, |
49 const content::DownloadSaveInfo& save_info); | 48 const OnStartedCallback& started_cb, |
| 49 const content::DownloadSaveInfo& save_info); |
50 | 50 |
51 virtual bool OnUploadProgress(int request_id, | 51 virtual bool OnUploadProgress(int request_id, |
52 uint64 position, | 52 uint64 position, |
53 uint64 size) OVERRIDE; | 53 uint64 size) OVERRIDE; |
54 | 54 |
55 // Not needed, as this event handler ought to be the final resource. | 55 // Not needed, as this event handler ought to be the final resource. |
56 virtual bool OnRequestRedirected(int request_id, | 56 virtual bool OnRequestRedirected(int request_id, |
57 const GURL& url, | 57 const GURL& url, |
58 content::ResourceResponse* response, | 58 content::ResourceResponse* response, |
59 bool* defer) OVERRIDE; | 59 bool* defer) OVERRIDE; |
(...skipping 25 matching lines...) Expand all Loading... |
85 | 85 |
86 void PauseRequest(); | 86 void PauseRequest(); |
87 void ResumeRequest(); | 87 void ResumeRequest(); |
88 void CancelRequest(); | 88 void CancelRequest(); |
89 | 89 |
90 std::string DebugString() const; | 90 std::string DebugString() const; |
91 | 91 |
92 private: | 92 private: |
93 virtual ~DownloadResourceHandler(); | 93 virtual ~DownloadResourceHandler(); |
94 | 94 |
95 void OnResponseCompletedInternal(int request_id, | 95 // Arrange for started_cb_ to be called on the UI thread with the |
96 const net::URLRequestStatus& status, | 96 // below values, nulling out started_cb_. Should only be called |
97 const std::string& security_info, | 97 // on the IO thread. |
98 int response_code); | |
99 | |
100 void CheckWriteProgressLater(); | |
101 void CheckWriteProgress(); | |
102 void MaybeResumeRequest(); | |
103 void CallStartedCB(content::DownloadId id, net::Error error); | 98 void CallStartedCB(content::DownloadId id, net::Error error); |
104 | 99 |
105 // Generates a DownloadId and calls DownloadFileManager. | |
106 void StartOnUIThread(scoped_ptr<DownloadCreateInfo> info, | |
107 const DownloadRequestHandle& handle); | |
108 void SetDownloadID(content::DownloadId id); | |
109 | |
110 // If the content-length header is not present (or contains something other | 100 // If the content-length header is not present (or contains something other |
111 // than numbers), the incoming content_length is -1 (unknown size). | 101 // than numbers), the incoming content_length is -1 (unknown size). |
112 // Set the content length to 0 to indicate unknown size to DownloadManager. | 102 // Set the content length to 0 to indicate unknown size to DownloadManager. |
113 void SetContentLength(const int64& content_length); | 103 void SetContentLength(const int64& content_length); |
114 | 104 |
115 void SetContentDisposition(const std::string& content_disposition); | 105 void SetContentDisposition(const std::string& content_disposition); |
116 | 106 |
117 content::DownloadId download_id_; | |
118 content::GlobalRequestID global_id_; | 107 content::GlobalRequestID global_id_; |
119 int render_view_id_; | 108 int render_view_id_; |
120 scoped_refptr<net::IOBuffer> read_buffer_; | |
121 std::string content_disposition_; | 109 std::string content_disposition_; |
122 int64 content_length_; | 110 int64 content_length_; |
123 DownloadFileManager* download_file_manager_; | 111 scoped_refptr<DownloadFileManager> download_file_manager_; |
124 net::URLRequest* request_; | 112 net::URLRequest* request_; |
125 // This is used only on the UI thread. | 113 // This is read only on the IO thread, but may only |
| 114 // be called on the UI thread. |
126 OnStartedCallback started_cb_; | 115 OnStartedCallback started_cb_; |
127 content::DownloadSaveInfo save_info_; | 116 content::DownloadSaveInfo save_info_; |
128 scoped_refptr<content::DownloadBuffer> buffer_; | 117 |
129 base::OneShotTimer<DownloadResourceHandler> check_write_progress_timer_; | 118 // Data flow |
| 119 scoped_refptr<net::IOBuffer> read_buffer_; // From URLRequest. |
| 120 scoped_ptr<content::ByteStreamWriter> stream_writer_; // To rest of system. |
130 | 121 |
131 // The following are used to collect stats. | 122 // The following are used to collect stats. |
132 base::TimeTicks download_start_time_; | 123 base::TimeTicks download_start_time_; |
133 base::TimeTicks last_read_time_; | 124 base::TimeTicks last_read_time_; |
| 125 base::TimeTicks last_stream_pause_time_; |
| 126 base::TimeDelta total_pause_time_; |
134 size_t last_buffer_size_; | 127 size_t last_buffer_size_; |
135 int64 bytes_read_; | 128 int64 bytes_read_; |
136 std::string accept_ranges_; | 129 std::string accept_ranges_; |
137 | 130 |
138 int pause_count_; | 131 int pause_count_; |
139 bool was_deferred_; | 132 bool was_deferred_; |
140 | 133 |
141 // For DCHECKing | 134 // For DCHECKing |
142 bool on_response_started_called_; | 135 bool on_response_started_called_; |
143 | 136 |
144 static const int kReadBufSize = 32768; // bytes | 137 static const int kReadBufSize = 32768; // bytes |
145 static const int kThrottleTimeMs = 200; // milliseconds | 138 static const int kThrottleTimeMs = 200; // milliseconds |
146 | 139 |
147 DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler); | 140 DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler); |
148 }; | 141 }; |
149 | 142 |
150 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_ | 143 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_ |
OLD | NEW |