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 ByteStreamInput; |
| 28 class ByteStreamOutput; |
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(int render_process_host_id, |
43 int render_view_id, | 42 int render_view_id, |
44 int request_id, | 43 int request_id, |
45 const GURL& url, | 44 const GURL& url, |
46 DownloadFileManager* download_file_manager, | 45 DownloadFileManager* download_file_manager, |
47 net::URLRequest* request, | 46 net::URLRequest* request, |
48 const OnStartedCallback& started_cb, | 47 const OnStartedCallback& started_cb, |
49 const content::DownloadSaveInfo& save_info); | 48 const content::DownloadSaveInfo& save_info); |
50 | 49 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 | 84 |
86 void PauseRequest(); | 85 void PauseRequest(); |
87 void ResumeRequest(); | 86 void ResumeRequest(); |
88 void CancelRequest(); | 87 void CancelRequest(); |
89 | 88 |
90 std::string DebugString() const; | 89 std::string DebugString() const; |
91 | 90 |
92 private: | 91 private: |
93 virtual ~DownloadResourceHandler(); | 92 virtual ~DownloadResourceHandler(); |
94 | 93 |
95 void OnResponseCompletedInternal(int request_id, | 94 void MaybeResumeRequest(); |
96 const net::URLRequestStatus& status, | |
97 const std::string& security_info, | |
98 int response_code); | |
99 | 95 |
100 void CheckWriteProgressLater(); | 96 // Arrange for started_cb_ to be called on the UI thread with the |
101 void CheckWriteProgress(); | 97 // below values, nulling out started_cb_. Should only be called |
102 void MaybeResumeRequest(); | 98 // on the IO thread. |
103 void CallStartedCB(content::DownloadId id, net::Error error); | 99 void CallStartedCB(content::DownloadId id, net::Error error); |
104 | 100 |
105 // Generates a DownloadId and calls DownloadFileManager. | 101 // Generates a DownloadId and calls DownloadFileManager. |
106 void StartOnUIThread(scoped_ptr<DownloadCreateInfo> info, | 102 void StartOnUIThread(scoped_ptr<DownloadCreateInfo> info, |
107 const DownloadRequestHandle& handle); | 103 scoped_ptr<content::ByteStreamOutput> pipe, |
| 104 const DownloadRequestHandle& handle, |
| 105 const OnStartedCallback& started_cb); |
108 void SetDownloadID(content::DownloadId id); | 106 void SetDownloadID(content::DownloadId id); |
109 | 107 |
110 // If the content-length header is not present (or contains something other | 108 // If the content-length header is not present (or contains something other |
111 // than numbers), the incoming content_length is -1 (unknown size). | 109 // than numbers), the incoming content_length is -1 (unknown size). |
112 // Set the content length to 0 to indicate unknown size to DownloadManager. | 110 // Set the content length to 0 to indicate unknown size to DownloadManager. |
113 void SetContentLength(const int64& content_length); | 111 void SetContentLength(const int64& content_length); |
114 | 112 |
115 void SetContentDisposition(const std::string& content_disposition); | 113 void SetContentDisposition(const std::string& content_disposition); |
116 | 114 |
117 content::DownloadId download_id_; | 115 content::DownloadId download_id_; |
118 content::GlobalRequestID global_id_; | 116 content::GlobalRequestID global_id_; |
119 int render_view_id_; | 117 int render_view_id_; |
120 scoped_refptr<net::IOBuffer> read_buffer_; | |
121 std::string content_disposition_; | 118 std::string content_disposition_; |
122 int64 content_length_; | 119 int64 content_length_; |
123 DownloadFileManager* download_file_manager_; | 120 DownloadFileManager* download_file_manager_; |
124 net::URLRequest* request_; | 121 net::URLRequest* request_; |
125 // This is used only on the UI thread. | 122 // This is read only on the IO thread, but may only |
| 123 // be called on the UI thread. |
126 OnStartedCallback started_cb_; | 124 OnStartedCallback started_cb_; |
127 content::DownloadSaveInfo save_info_; | 125 content::DownloadSaveInfo save_info_; |
128 scoped_refptr<content::DownloadBuffer> buffer_; | 126 |
129 base::OneShotTimer<DownloadResourceHandler> check_write_progress_timer_; | 127 // Data flow |
| 128 scoped_refptr<net::IOBuffer> read_buffer_; // From URLRequest. |
| 129 scoped_ptr<content::ByteStreamInput> pipe_input_; // To rest of system. |
130 | 130 |
131 // The following are used to collect stats. | 131 // The following are used to collect stats. |
132 base::TimeTicks download_start_time_; | 132 base::TimeTicks download_start_time_; |
133 base::TimeTicks last_read_time_; | 133 base::TimeTicks last_read_time_; |
| 134 base::TimeTicks last_pause_time_; |
| 135 base::TimeDelta total_pause_time_; |
134 size_t last_buffer_size_; | 136 size_t last_buffer_size_; |
135 int64 bytes_read_; | 137 int64 bytes_read_; |
136 std::string accept_ranges_; | 138 std::string accept_ranges_; |
137 | 139 |
138 int pause_count_; | 140 int pause_count_; |
139 bool was_deferred_; | 141 bool was_deferred_; |
140 | 142 |
141 static const int kReadBufSize = 32768; // bytes | 143 static const int kReadBufSize = 32768; // bytes |
142 static const int kThrottleTimeMs = 200; // milliseconds | 144 static const int kThrottleTimeMs = 200; // milliseconds |
143 | 145 |
144 DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler); | 146 DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler); |
145 }; | 147 }; |
146 | 148 |
147 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_ | 149 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_ |
OLD | NEW |