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. The |
58 // passed into DownloadManagerImpl::StartDownload(). | 59 // |override_mime_type| is used as the MIME type for the download when |
59 // | 60 // constructing a DownloadCreateInfo object. |
60 // Only populates the response derived fields of DownloadCreateInfo, with the | 61 bool OnResponseStarted(const std::string& override_mime_type); |
61 // exception of |save_info|. | |
62 void OnResponseStarted(scoped_ptr<DownloadCreateInfo>* info, | |
63 scoped_ptr<ByteStreamReader>* stream_reader); | |
64 | 62 |
65 // Starts a read cycle. Creates a new IOBuffer which can be passed into | 63 // Starts a read cycle. Creates a new IOBuffer which can be passed into |
66 // URLRequest::Read(). Call OnReadCompleted() when the Read operation | 64 // URLRequest::Read(). Call OnReadCompleted() when the Read operation |
67 // completes. | 65 // completes. |
68 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 66 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
69 int* buf_size, | 67 int* buf_size, |
70 int min_size); | 68 int min_size); |
71 | 69 |
72 // Should be called when the Read() operation completes. |defer| will be set | 70 // 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 | 71 // 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|. | 72 // can be read, invokes the |on_ready_to_read_callback|. |
75 bool OnReadCompleted(int bytes_read, bool* defer); | 73 bool OnReadCompleted(int bytes_read, bool* defer); |
76 | 74 |
77 // Called to signal that the response is complete. If the return value is | 75 // 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 // | 76 // |
81 // It is expected that once this method is invoked, the DownloadRequestCore | 77 // It is expected that once this method is invoked, the DownloadRequestCore |
82 // object will be destroyed in short order without invoking any other methods | 78 // object will be destroyed in short order without invoking any other methods |
83 // other than the destructor. | 79 // other than the destructor. |
84 DownloadInterruptReason OnResponseCompleted( | 80 void OnResponseCompleted(const net::URLRequestStatus& status); |
85 const net::URLRequestStatus& status); | |
86 | 81 |
87 // Called if the request should suspend reading. A subsequent | 82 // Called if the request should suspend reading. A subsequent |
88 // OnReadCompleted() will result in |defer| being set to true. | 83 // OnReadCompleted() will result in |defer| being set to true. |
89 // | 84 // |
90 // Each PauseRequest() must be balanced with a call to ResumeRequest(). | 85 // Each PauseRequest() must be balanced with a call to ResumeRequest(). |
91 void PauseRequest(); | 86 void PauseRequest(); |
92 | 87 |
93 // Balances a call to PauseRequest(). If no more pauses are outstanding and | 88 // 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, | 89 // the reader end of the ByteStream is ready to receive more data, |
95 // DownloadRequestCore will invoke the |on_ready_to_read_callback| to signal | 90 // DownloadRequestCore will invoke the |on_ready_to_read_callback| to signal |
96 // to the caller that the read cycles should commence. | 91 // to the caller that the read cycles should commence. |
97 void ResumeRequest(); | 92 void ResumeRequest(); |
98 | 93 |
99 std::string DebugString() const; | 94 std::string DebugString() const; |
100 | 95 |
| 96 static scoped_ptr<net::URLRequest> CreateRequestOnIOThread( |
| 97 uint32_t download_id, |
| 98 DownloadUrlParameters* params); |
| 99 |
| 100 // Size of the buffer used between the DownloadRequestCore and the |
| 101 // downstream receiver of its output. |
| 102 static const int kDownloadByteStreamSize; |
| 103 |
101 protected: | 104 protected: |
102 net::URLRequest* request() const { return request_; } | 105 net::URLRequest* request() const { return request_; } |
103 | 106 |
104 private: | 107 private: |
105 base::Closure on_ready_to_read_callback_; | 108 static DownloadInterruptReason HandleRequestStatus( |
| 109 const net::URLRequestStatus& status); |
| 110 |
| 111 static DownloadInterruptReason HandleSuccessfulServerResponse( |
| 112 const net::HttpResponseHeaders& http_headers, |
| 113 DownloadSaveInfo* save_info); |
| 114 |
| 115 scoped_ptr<DownloadCreateInfo> CreateDownloadCreateInfo( |
| 116 DownloadInterruptReason result); |
| 117 |
| 118 Delegate* delegate_; |
106 net::URLRequest* request_; | 119 net::URLRequest* request_; |
| 120 |
| 121 // "Passthrough" fields. These are only kept here so that they can be used to |
| 122 // populate the DownloadCreateInfo when the time comes. |
107 scoped_ptr<DownloadSaveInfo> save_info_; | 123 scoped_ptr<DownloadSaveInfo> save_info_; |
| 124 uint32_t download_id_; |
| 125 DownloadUrlParameters::OnStartedCallback on_started_callback_; |
108 | 126 |
109 // Data flow | 127 // Data flow |
110 scoped_refptr<net::IOBuffer> read_buffer_; // From URLRequest. | 128 scoped_refptr<net::IOBuffer> read_buffer_; // From URLRequest. |
111 scoped_ptr<ByteStreamWriter> stream_writer_; // To rest of system. | 129 scoped_ptr<ByteStreamWriter> stream_writer_; // To rest of system. |
112 | 130 |
113 // Keeps the system from sleeping while this is alive. If the | 131 // 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 | 132 // 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. | 133 // request to fail and the associated download will be interrupted. |
116 scoped_ptr<PowerSaveBlocker> power_save_blocker_; | 134 scoped_ptr<PowerSaveBlocker> power_save_blocker_; |
117 | 135 |
118 // The following are used to collect stats. | 136 // The following are used to collect stats. |
119 base::TimeTicks download_start_time_; | 137 base::TimeTicks download_start_time_; |
120 base::TimeTicks last_read_time_; | 138 base::TimeTicks last_read_time_; |
121 base::TimeTicks last_stream_pause_time_; | 139 base::TimeTicks last_stream_pause_time_; |
122 base::TimeDelta total_pause_time_; | 140 base::TimeDelta total_pause_time_; |
123 size_t last_buffer_size_; | 141 size_t last_buffer_size_; |
124 int64_t bytes_read_; | 142 int64_t bytes_read_; |
125 | 143 |
126 int pause_count_; | 144 int pause_count_; |
127 bool was_deferred_; | 145 bool was_deferred_; |
| 146 bool is_resumption_request_; |
| 147 bool started_; |
| 148 |
| 149 // When DownloadRequestCore initiates an abort (by blocking a redirect, for |
| 150 // example) it expects to eventually receive a OnResponseCompleted() with a |
| 151 // status indicating that the request was aborted. When this happens, the |
| 152 // interrupt reason in |abort_reason_| will be used instead of USER_CANCELED |
| 153 // which is vague. |
| 154 DownloadInterruptReason abort_reason_; |
128 | 155 |
129 // Each successful OnWillRead will yield a buffer of this size. | 156 // Each successful OnWillRead will yield a buffer of this size. |
130 static const int kReadBufSize = 32768; // bytes | 157 static const int kReadBufSize = 32768; // bytes |
131 | 158 |
132 DISALLOW_COPY_AND_ASSIGN(DownloadRequestCore); | 159 DISALLOW_COPY_AND_ASSIGN(DownloadRequestCore); |
133 }; | 160 }; |
134 | 161 |
135 } // namespace content | 162 } // namespace content |
136 | 163 |
137 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_CORE_H_ | 164 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_CORE_H_ |
OLD | NEW |