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. |
Randy Smith (Not in Mondays)
2016/02/12 18:58:11
nit: comment use of parameter.
asanka
2016/02/12 20:52:29
Done.
| |
58 // passed into DownloadManagerImpl::StartDownload(). | 59 bool OnResponseStarted(const std::string& override_mime_type); |
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); | |
86 | 79 |
87 // Called if the request should suspend reading. A subsequent | 80 // Called if the request should suspend reading. A subsequent |
88 // OnReadCompleted() will result in |defer| being set to true. | 81 // OnReadCompleted() will result in |defer| being set to true. |
89 // | 82 // |
90 // Each PauseRequest() must be balanced with a call to ResumeRequest(). | 83 // Each PauseRequest() must be balanced with a call to ResumeRequest(). |
91 void PauseRequest(); | 84 void PauseRequest(); |
92 | 85 |
93 // Balances a call to PauseRequest(). If no more pauses are outstanding and | 86 // 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, | 87 // the reader end of the ByteStream is ready to receive more data, |
95 // DownloadRequestCore will invoke the |on_ready_to_read_callback| to signal | 88 // DownloadRequestCore will invoke the |on_ready_to_read_callback| to signal |
96 // to the caller that the read cycles should commence. | 89 // to the caller that the read cycles should commence. |
97 void ResumeRequest(); | 90 void ResumeRequest(); |
98 | 91 |
99 std::string DebugString() const; | 92 std::string DebugString() const; |
100 | 93 |
94 static scoped_ptr<net::URLRequest> CreateRequestOnIOThread( | |
95 uint32_t download_id, | |
96 DownloadUrlParameters* params); | |
97 | |
98 // Size of the buffer used between the DownloadRequestCore and the | |
99 // downstream receiver of its output. | |
100 static const int kDownloadByteStreamSize; | |
101 | |
101 protected: | 102 protected: |
102 net::URLRequest* request() const { return request_; } | 103 net::URLRequest* request() const { return request_; } |
103 | 104 |
104 private: | 105 private: |
105 base::Closure on_ready_to_read_callback_; | 106 static DownloadInterruptReason HandleRequestStatus( |
107 const net::URLRequestStatus& status); | |
108 | |
109 static DownloadInterruptReason HandleSuccessfulServerResponse( | |
110 const net::HttpResponseHeaders& http_headers, | |
111 DownloadSaveInfo* save_info); | |
112 | |
113 scoped_ptr<DownloadCreateInfo> CreateDownloadCreateInfo( | |
114 DownloadInterruptReason result, | |
115 const std::string& override_mime_type); | |
116 | |
117 Delegate* delegate_; | |
106 net::URLRequest* request_; | 118 net::URLRequest* request_; |
119 | |
120 // "Passthrough" fields. These are only kept here so that they can be used to | |
121 // populate the DownloadCreateInfo when the time comes. | |
107 scoped_ptr<DownloadSaveInfo> save_info_; | 122 scoped_ptr<DownloadSaveInfo> save_info_; |
123 uint32_t download_id_; | |
124 DownloadUrlParameters::OnStartedCallback on_started_callback_; | |
108 | 125 |
109 // Data flow | 126 // Data flow |
110 scoped_refptr<net::IOBuffer> read_buffer_; // From URLRequest. | 127 scoped_refptr<net::IOBuffer> read_buffer_; // From URLRequest. |
111 scoped_ptr<ByteStreamWriter> stream_writer_; // To rest of system. | 128 scoped_ptr<ByteStreamWriter> stream_writer_; // To rest of system. |
112 | 129 |
113 // Keeps the system from sleeping while this is alive. If the | 130 // 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 | 131 // 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. | 132 // request to fail and the associated download will be interrupted. |
116 scoped_ptr<PowerSaveBlocker> power_save_blocker_; | 133 scoped_ptr<PowerSaveBlocker> power_save_blocker_; |
117 | 134 |
118 // The following are used to collect stats. | 135 // The following are used to collect stats. |
119 base::TimeTicks download_start_time_; | 136 base::TimeTicks download_start_time_; |
120 base::TimeTicks last_read_time_; | 137 base::TimeTicks last_read_time_; |
121 base::TimeTicks last_stream_pause_time_; | 138 base::TimeTicks last_stream_pause_time_; |
122 base::TimeDelta total_pause_time_; | 139 base::TimeDelta total_pause_time_; |
123 size_t last_buffer_size_; | 140 size_t last_buffer_size_; |
124 int64_t bytes_read_; | 141 int64_t bytes_read_; |
125 | 142 |
126 int pause_count_; | 143 int pause_count_; |
127 bool was_deferred_; | 144 bool was_deferred_; |
145 bool is_resumption_request_; | |
146 bool started_; | |
147 | |
148 // When DownloadRequestCore initiates an abort (by blocking a redirect, for | |
149 // example) it expects to eventually receive a OnResponseCompleted() with a | |
150 // status indicating that the request was aborted. When this happens, the | |
151 // interrupt reason in |abort_reason_| will be used instead of USER_CANCELED | |
152 // which is vague. | |
153 DownloadInterruptReason abort_reason_; | |
128 | 154 |
129 // Each successful OnWillRead will yield a buffer of this size. | 155 // Each successful OnWillRead will yield a buffer of this size. |
130 static const int kReadBufSize = 32768; // bytes | 156 static const int kReadBufSize = 32768; // bytes |
131 | 157 |
132 DISALLOW_COPY_AND_ASSIGN(DownloadRequestCore); | 158 DISALLOW_COPY_AND_ASSIGN(DownloadRequestCore); |
133 }; | 159 }; |
134 | 160 |
135 } // namespace content | 161 } // namespace content |
136 | 162 |
137 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_CORE_H_ | 163 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_CORE_H_ |
OLD | NEW |