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_REQUEST_CORE_H_ | 5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_CORE_H_ |
6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_CORE_H_ | 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_CORE_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
17 #include "content/browser/loader/resource_handler.h" | 17 #include "content/browser/loader/resource_handler.h" |
18 #include "content/public/browser/download_interrupt_reasons.h" | 18 #include "content/public/browser/download_interrupt_reasons.h" |
19 #include "content/public/browser/download_save_info.h" | 19 #include "content/public/browser/download_save_info.h" |
20 #include "content/public/browser/download_url_parameters.h" | 20 #include "content/public/browser/download_url_parameters.h" |
21 | 21 |
22 namespace net { | 22 namespace net { |
23 class HttpResponseHeaders; | |
23 class URLRequest; | 24 class URLRequest; |
25 class URLRequestStatus; | |
24 } // namespace net | 26 } // namespace net |
25 | 27 |
26 namespace content { | 28 namespace content { |
27 class DownloadManagerImpl; | 29 class DownloadManagerImpl; |
28 class ByteStreamReader; | 30 class ByteStreamReader; |
29 class ByteStreamWriter; | 31 class ByteStreamWriter; |
30 class PowerSaveBlocker; | 32 class PowerSaveBlocker; |
31 struct DownloadCreateInfo; | 33 struct DownloadCreateInfo; |
32 | 34 |
33 // This class encapsulates the core logic for reading data from a URLRequest and | 35 // This class encapsulates the core logic for reading data from a URLRequest and |
34 // writing it into a ByteStream. It's common to both DownloadResourceHandler and | 36 // writing it into a ByteStream. It's common to both DownloadResourceHandler and |
35 // UrlDownloader. | 37 // UrlDownloader. |
36 // | 38 // |
37 // Created, lives on and dies on the IO thread. | 39 // Created, lives on and dies on the IO thread. |
38 class CONTENT_EXPORT DownloadRequestCore | 40 class CONTENT_EXPORT DownloadRequestCore |
39 : public base::SupportsWeakPtr<DownloadRequestCore> { | 41 : public base::SupportsWeakPtr<DownloadRequestCore> { |
40 public: | 42 public: |
41 // Size of the buffer used between the DownloadRequestCore and the | 43 class Delegate { |
42 // downstream receiver of its output. | 44 public: |
43 static const int kDownloadByteStreamSize; | 45 virtual void OnReadyToRead() = 0; |
46 virtual void OnStart( | |
47 scoped_ptr<DownloadCreateInfo> download_create_info, | |
48 scoped_ptr<ByteStreamReader> stream_reader, | |
49 const DownloadUrlParameters::OnStartedCallback& callback) = 0; | |
50 }; | |
44 | 51 |
45 // |request| *must* outlive the DownloadRequestCore. |save_info| must be | 52 // All parameters are required. |request| and |delegate| must outlive |
46 // valid. | 53 // DownloadRequestCore. |
47 // | 54 DownloadRequestCore(net::URLRequest* request, Delegate* delegate); |
48 // Invokes |on_ready_to_read_callback| if a previous call to OnReadCompleted() | |
49 // resulted in |defer| being set to true, and DownloadRequestCore is now ready | |
50 // to commence reading. | |
51 DownloadRequestCore(net::URLRequest* request, | |
52 scoped_ptr<DownloadSaveInfo> save_info, | |
53 const base::Closure& on_ready_to_read_callback); | |
54 ~DownloadRequestCore(); | 55 ~DownloadRequestCore(); |
55 | 56 |
56 // Should be called when the URLRequest::Delegate receives OnResponseStarted. | 57 // Should be called when the URLRequest::Delegate receives OnResponseStarted. |
57 // Constructs a DownloadCreateInfo and a ByteStreamReader that should be | 58 // Invokes Delegate::OnStart() with download start parameters. |
58 // passed into DownloadManagerImpl::StartDownload(). | 59 bool OnResponseStarted(); |
59 // | |
60 // Only populates the response derived fields of DownloadCreateInfo, with the | |
61 // exception of |save_info|. | |
62 void OnResponseStarted(scoped_ptr<DownloadCreateInfo>* info, | |
63 scoped_ptr<ByteStreamReader>* stream_reader); | |
64 | 60 |
65 // Starts a read cycle. Creates a new IOBuffer which can be passed into | 61 // Starts a read cycle. Creates a new IOBuffer which can be passed into |
66 // URLRequest::Read(). Call OnReadCompleted() when the Read operation | 62 // URLRequest::Read(). Call OnReadCompleted() when the Read operation |
67 // completes. | 63 // completes. |
68 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 64 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
69 int* buf_size, | 65 int* buf_size, |
70 int min_size); | 66 int min_size); |
71 | 67 |
72 // Should be called when the Read() operation completes. |defer| will be set | 68 // Should be called when the Read() operation completes. |defer| will be set |
73 // to true if reading is to be suspended. In the latter case, once more data | 69 // to true if reading is to be suspended. In the latter case, once more data |
74 // can be read, invokes the |on_ready_to_read_callback|. | 70 // can be read, invokes the |on_ready_to_read_callback|. |
75 bool OnReadCompleted(int bytes_read, bool* defer); | 71 bool OnReadCompleted(int bytes_read, bool* defer); |
76 | 72 |
77 // Called to signal that the response is complete. If the return value is | 73 // Called to signal that the response is complete. |
78 // something other than DOWNLOAD_INTERRUPT_REASON_NONE, then the download | |
79 // should be considered interrupted. | |
80 // | 74 // |
81 // It is expected that once this method is invoked, the DownloadRequestCore | 75 // It is expected that once this method is invoked, the DownloadRequestCore |
82 // object will be destroyed in short order without invoking any other methods | 76 // object will be destroyed in short order without invoking any other methods |
83 // other than the destructor. | 77 // other than the destructor. |
84 DownloadInterruptReason OnResponseCompleted( | 78 void OnResponseCompleted(const net::URLRequestStatus& status); |
85 const net::URLRequestStatus& status); | 79 |
80 // Used to notify DownloadRequestCore that the caller is about to abort the | |
81 // outer request. |reason| will be used as the final interrupt reason when | |
82 // OnResponseCompleted() is called. | |
83 void OnWillAbort(DownloadInterruptReason reason); | |
84 | |
85 void OnAbortedBeforeStart(DownloadInterruptReason reason); | |
Randy Smith (Not in Mondays)
2016/02/11 22:08:08
Um ... this is probably my being clueless, but I c
Randy Smith (Not in Mondays)
2016/02/12 18:01:19
Ping? git grep on a patched checkout seems to agr
asanka
2016/02/12 18:31:46
Removed. There are for the dependent CL.
| |
86 | 86 |
87 // Called if the request should suspend reading. A subsequent | 87 // Called if the request should suspend reading. A subsequent |
88 // OnReadCompleted() will result in |defer| being set to true. | 88 // OnReadCompleted() will result in |defer| being set to true. |
89 // | 89 // |
90 // Each PauseRequest() must be balanced with a call to ResumeRequest(). | 90 // Each PauseRequest() must be balanced with a call to ResumeRequest(). |
91 void PauseRequest(); | 91 void PauseRequest(); |
92 | 92 |
93 // Balances a call to PauseRequest(). If no more pauses are outstanding and | 93 // Balances a call to PauseRequest(). If no more pauses are outstanding and |
94 // the reader end of the ByteStream is ready to receive more data, | 94 // the reader end of the ByteStream is ready to receive more data, |
95 // DownloadRequestCore will invoke the |on_ready_to_read_callback| to signal | 95 // DownloadRequestCore will invoke the |on_ready_to_read_callback| to signal |
96 // to the caller that the read cycles should commence. | 96 // to the caller that the read cycles should commence. |
97 void ResumeRequest(); | 97 void ResumeRequest(); |
98 | 98 |
99 std::string DebugString() const; | 99 std::string DebugString() const; |
100 | 100 |
101 static scoped_ptr<net::URLRequest> CreateRequestOnIOThread( | |
102 uint32_t download_id, | |
103 DownloadUrlParameters* params); | |
104 | |
105 // Size of the buffer used between the DownloadRequestCore and the | |
106 // downstream receiver of its output. | |
107 static const int kDownloadByteStreamSize; | |
108 | |
101 protected: | 109 protected: |
102 net::URLRequest* request() const { return request_; } | 110 net::URLRequest* request() const { return request_; } |
103 | 111 |
104 private: | 112 private: |
105 base::Closure on_ready_to_read_callback_; | 113 static DownloadInterruptReason HandleRequestStatus( |
114 const net::URLRequestStatus& status); | |
115 | |
116 static DownloadInterruptReason HandleSuccessfulServerResponse( | |
117 const net::HttpResponseHeaders& http_headers, | |
118 DownloadSaveInfo* save_info); | |
119 | |
120 scoped_ptr<DownloadCreateInfo> CreateDownloadCreateInfo( | |
121 DownloadInterruptReason result); | |
122 | |
123 Delegate* delegate_; | |
106 net::URLRequest* request_; | 124 net::URLRequest* request_; |
125 | |
126 // "Passthrough" fields. These are only kept here so that they can be used to | |
127 // populate the DownloadCreateInfo when the time comes. | |
Randy Smith (Not in Mondays)
2016/02/11 22:08:08
nit, suggestion: Given that we have the URLRequest
Randy Smith (Not in Mondays)
2016/02/12 18:01:19
Ping?
asanka
2016/02/12 18:31:46
I started out doing that, but it ended up not bein
Randy Smith (Not in Mondays)
2016/02/12 18:58:10
Ok with this not being in this CL, but: Aren't tho
asanka
2016/02/12 20:52:28
True. But there would also not be a UserData. So w
| |
107 scoped_ptr<DownloadSaveInfo> save_info_; | 128 scoped_ptr<DownloadSaveInfo> save_info_; |
129 uint32_t download_id_; | |
130 DownloadUrlParameters::OnStartedCallback on_started_callback_; | |
108 | 131 |
109 // Data flow | 132 // Data flow |
110 scoped_refptr<net::IOBuffer> read_buffer_; // From URLRequest. | 133 scoped_refptr<net::IOBuffer> read_buffer_; // From URLRequest. |
111 scoped_ptr<ByteStreamWriter> stream_writer_; // To rest of system. | 134 scoped_ptr<ByteStreamWriter> stream_writer_; // To rest of system. |
112 | 135 |
113 // Keeps the system from sleeping while this is alive. If the | 136 // Keeps the system from sleeping while this is alive. If the |
114 // system enters power saving mode while a request is alive, it can cause the | 137 // system enters power saving mode while a request is alive, it can cause the |
115 // request to fail and the associated download will be interrupted. | 138 // request to fail and the associated download will be interrupted. |
116 scoped_ptr<PowerSaveBlocker> power_save_blocker_; | 139 scoped_ptr<PowerSaveBlocker> power_save_blocker_; |
117 | 140 |
118 // The following are used to collect stats. | 141 // The following are used to collect stats. |
119 base::TimeTicks download_start_time_; | 142 base::TimeTicks download_start_time_; |
120 base::TimeTicks last_read_time_; | 143 base::TimeTicks last_read_time_; |
121 base::TimeTicks last_stream_pause_time_; | 144 base::TimeTicks last_stream_pause_time_; |
122 base::TimeDelta total_pause_time_; | 145 base::TimeDelta total_pause_time_; |
123 size_t last_buffer_size_; | 146 size_t last_buffer_size_; |
124 int64_t bytes_read_; | 147 int64_t bytes_read_; |
125 | 148 |
126 int pause_count_; | 149 int pause_count_; |
127 bool was_deferred_; | 150 bool was_deferred_; |
151 bool is_resumption_request_; | |
152 bool started_; | |
153 | |
154 // When DownloadRequestCore initiates an abort (by blocking a redirect, for | |
155 // example) it expects to eventually receive a OnResponseCompleted() with a | |
156 // status indicating that the request was aborted. When this happens, the | |
157 // interrupt reason in |abort_reason_| will be used instead of USER_CANCELED | |
158 // which is vague. | |
159 DownloadInterruptReason abort_reason_; | |
128 | 160 |
129 // Each successful OnWillRead will yield a buffer of this size. | 161 // Each successful OnWillRead will yield a buffer of this size. |
130 static const int kReadBufSize = 32768; // bytes | 162 static const int kReadBufSize = 32768; // bytes |
131 | 163 |
132 DISALLOW_COPY_AND_ASSIGN(DownloadRequestCore); | 164 DISALLOW_COPY_AND_ASSIGN(DownloadRequestCore); |
133 }; | 165 }; |
134 | 166 |
135 } // namespace content | 167 } // namespace content |
136 | 168 |
137 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_CORE_H_ | 169 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_CORE_H_ |
OLD | NEW |