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 <string> | 8 #include <string> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "content/browser/loader/resource_handler.h" | 14 #include "content/browser/loader/resource_handler.h" |
15 #include "content/public/browser/download_interrupt_reasons.h" | 15 #include "content/public/browser/download_interrupt_reasons.h" |
16 #include "content/public/browser/download_save_info.h" | 16 #include "content/public/browser/download_save_info.h" |
17 #include "content/public/browser/download_url_parameters.h" | 17 #include "content/public/browser/download_url_parameters.h" |
18 | 18 |
19 namespace net { | 19 namespace net { |
20 class URLRequest; | 20 class URLRequest; |
21 } // namespace net | 21 } // namespace net |
22 | 22 |
23 namespace content { | 23 namespace content { |
24 class DownloadManagerImpl; | 24 class DownloadManagerImpl; |
25 class ByteStreamReader; | 25 class ByteStreamReader; |
26 class ByteStreamWriter; | 26 class ByteStreamWriter; |
27 class PowerSaveBlocker; | 27 class PowerSaveBlocker; |
28 class UrlDownloader; | |
29 struct DownloadCreateInfo; | 28 struct DownloadCreateInfo; |
30 | 29 |
31 // Forwards data to the download thread. | 30 // This class encapsulates the core logic for reading data from a URLRequest and |
| 31 // writing it into a ByteStream. It's common to both DownloadResourceHandler and |
| 32 // UrlDownloader. |
| 33 // |
| 34 // Created, lives on and dies on the IO thread. |
32 class CONTENT_EXPORT DownloadRequestCore | 35 class CONTENT_EXPORT DownloadRequestCore |
33 : public base::SupportsWeakPtr<DownloadRequestCore> { | 36 : public base::SupportsWeakPtr<DownloadRequestCore> { |
34 public: | 37 public: |
35 // Size of the buffer used between the DownloadRequestCore and the | 38 // Size of the buffer used between the DownloadRequestCore and the |
36 // downstream receiver of its output. | 39 // downstream receiver of its output. |
37 static const int kDownloadByteStreamSize; | 40 static const int kDownloadByteStreamSize; |
38 | 41 |
39 // started_cb will be called exactly once on the UI thread. | 42 // |request| *must* outlive the DownloadRequestCore. |save_info| must be |
40 // |id| should be invalid if the id should be automatically assigned. | 43 // valid. |
41 DownloadRequestCore( | 44 // |
42 uint32 id, | 45 // Invokes |on_ready_to_read_callback| if a previous call to OnReadCompleted() |
43 net::URLRequest* request, | 46 // resulted in |defer| being set to true, and DownloadRequestCore is now ready |
44 const DownloadUrlParameters::OnStartedCallback& started_cb, | 47 // to commence reading. |
45 scoped_ptr<DownloadSaveInfo> save_info, | 48 DownloadRequestCore(net::URLRequest* request, |
46 base::WeakPtr<DownloadManagerImpl> download_manager); | 49 scoped_ptr<DownloadSaveInfo> save_info, |
| 50 const base::Closure& on_ready_to_read_callback); |
47 ~DownloadRequestCore(); | 51 ~DownloadRequestCore(); |
48 | 52 |
49 // Send the download creation information to the download thread. | 53 // Should be called when the URLRequest::Delegate receives OnResponseStarted. |
50 bool OnResponseStarted(); | 54 // Constructs a DownloadCreateInfo and a ByteStreamReader that should be |
| 55 // passed into DownloadManagerImpl::StartDownload(). |
| 56 // |
| 57 // Only populates the response derived fields of DownloadCreateInfo, with the |
| 58 // exception of |save_info|. |
| 59 void OnResponseStarted(scoped_ptr<DownloadCreateInfo>* info, |
| 60 scoped_ptr<ByteStreamReader>* stream_reader); |
51 | 61 |
52 // Create a new buffer, which will be handed to the download thread for file | 62 // Starts a read cycle. Creates a new IOBuffer which can be passed into |
53 // writing and deletion. | 63 // URLRequest::Read(). Call OnReadCompleted() when the Read operation |
| 64 // completes. |
54 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 65 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
55 int* buf_size, | 66 int* buf_size, |
56 int min_size); | 67 int min_size); |
57 | 68 |
| 69 // Should be called when the Read() operation completes. |defer| will be set |
| 70 // to true if reading is to be suspended. In the latter case, once more data |
| 71 // can be read, invokes the |on_ready_to_read_callback|. |
58 bool OnReadCompleted(int bytes_read, bool* defer); | 72 bool OnReadCompleted(int bytes_read, bool* defer); |
59 | 73 |
60 void OnResponseCompleted(const net::URLRequestStatus& status); | 74 // Called to signal that the response is complete. If the return value is |
| 75 // something other than DOWNLOAD_INTERRUPT_REASON_NONE, then the download |
| 76 // should be considered interrupted. |
| 77 // |
| 78 // It is expected that once this method is invoked, the DownloadRequestCore |
| 79 // object will be destroyed in short order without invoking any other methods |
| 80 // other than the destructor. |
| 81 DownloadInterruptReason OnResponseCompleted( |
| 82 const net::URLRequestStatus& status); |
61 | 83 |
| 84 // Called if the request should suspend reading. A subsequent |
| 85 // OnReadCompleted() will result in |defer| being set to true. |
| 86 // |
| 87 // Each PauseRequest() must be balanced with a call to ResumeRequest(). |
62 void PauseRequest(); | 88 void PauseRequest(); |
| 89 |
| 90 // Balances a call to PauseRequest(). If no more pauses are outstanding and |
| 91 // the reader end of the ByteStream is ready to receive more data, |
| 92 // DownloadRequestCore will invoke the |on_ready_to_read_callback| to signal |
| 93 // to the caller that the read cycles should commence. |
63 void ResumeRequest(); | 94 void ResumeRequest(); |
64 | 95 |
65 std::string DebugString() const; | 96 std::string DebugString() const; |
66 | 97 |
67 void set_downloader(UrlDownloader* downloader) { downloader_ = downloader; } | |
68 | |
69 protected: | 98 protected: |
70 net::URLRequest* request() const { return request_; } | 99 net::URLRequest* request() const { return request_; } |
71 | 100 |
72 private: | 101 private: |
73 // Arrange for started_cb_ to be called on the UI thread with the | 102 base::Closure on_ready_to_read_callback_; |
74 // below values, nulling out started_cb_. Should only be called | |
75 // on the IO thread. | |
76 void CallStartedCB(DownloadItem* item, | |
77 DownloadInterruptReason interrupt_reason); | |
78 | |
79 net::URLRequest* request_; | 103 net::URLRequest* request_; |
80 uint32 download_id_; | |
81 | |
82 // This is read only on the IO thread, but may only | |
83 // be called on the UI thread. | |
84 DownloadUrlParameters::OnStartedCallback started_cb_; | |
85 scoped_ptr<DownloadSaveInfo> save_info_; | 104 scoped_ptr<DownloadSaveInfo> save_info_; |
86 | 105 |
87 // Data flow | 106 // Data flow |
88 scoped_refptr<net::IOBuffer> read_buffer_; // From URLRequest. | 107 scoped_refptr<net::IOBuffer> read_buffer_; // From URLRequest. |
89 scoped_ptr<ByteStreamWriter> stream_writer_; // To rest of system. | 108 scoped_ptr<ByteStreamWriter> stream_writer_; // To rest of system. |
90 | 109 |
91 // Keeps the system from sleeping while this is alive. If the | 110 // Keeps the system from sleeping while this is alive. If the |
92 // system enters power saving mode while a request is alive, it can cause the | 111 // system enters power saving mode while a request is alive, it can cause the |
93 // request to fail and the associated download will be interrupted. | 112 // request to fail and the associated download will be interrupted. |
94 scoped_ptr<PowerSaveBlocker> power_save_blocker_; | 113 scoped_ptr<PowerSaveBlocker> power_save_blocker_; |
95 | 114 |
96 // The following are used to collect stats. | 115 // The following are used to collect stats. |
97 base::TimeTicks download_start_time_; | 116 base::TimeTicks download_start_time_; |
98 base::TimeTicks last_read_time_; | 117 base::TimeTicks last_read_time_; |
99 base::TimeTicks last_stream_pause_time_; | 118 base::TimeTicks last_stream_pause_time_; |
100 base::TimeDelta total_pause_time_; | 119 base::TimeDelta total_pause_time_; |
101 size_t last_buffer_size_; | 120 size_t last_buffer_size_; |
102 int64 bytes_read_; | 121 int64 bytes_read_; |
103 | 122 |
104 int pause_count_; | 123 int pause_count_; |
105 bool was_deferred_; | 124 bool was_deferred_; |
106 | 125 |
107 // For DCHECKing | 126 // Each successful OnWillRead will yield a buffer of this size. |
108 bool on_response_started_called_; | |
109 | |
110 UrlDownloader* downloader_; | |
111 | |
112 // DownloadManager passed in by the owner of DownloadRequestCore. | |
113 base::WeakPtr<DownloadManagerImpl> download_manager_; | |
114 | |
115 static const int kReadBufSize = 32768; // bytes | 127 static const int kReadBufSize = 32768; // bytes |
116 static const int kThrottleTimeMs = 200; // milliseconds | |
117 | 128 |
118 DISALLOW_COPY_AND_ASSIGN(DownloadRequestCore); | 129 DISALLOW_COPY_AND_ASSIGN(DownloadRequestCore); |
119 }; | 130 }; |
120 | 131 |
121 } // namespace content | 132 } // namespace content |
122 | 133 |
123 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_CORE_H_ | 134 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_CORE_H_ |
OLD | NEW |