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_REQUEST_CORE_H_ |
6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_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/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" |
12 #include "content/browser/loader/resource_handler.h" | 14 #include "content/browser/loader/resource_handler.h" |
13 #include "content/public/browser/download_interrupt_reasons.h" | 15 #include "content/public/browser/download_interrupt_reasons.h" |
14 #include "content/public/browser/download_manager.h" | |
15 #include "content/public/browser/download_save_info.h" | 16 #include "content/public/browser/download_save_info.h" |
16 #include "content/public/browser/download_url_parameters.h" | 17 #include "content/public/browser/download_url_parameters.h" |
17 | 18 |
18 namespace net { | 19 namespace net { |
19 class URLRequest; | 20 class URLRequest; |
20 } // namespace net | 21 } // namespace net |
21 | 22 |
22 namespace content { | 23 namespace content { |
| 24 class DownloadManagerImpl; |
23 class ByteStreamReader; | 25 class ByteStreamReader; |
24 class ByteStreamWriter; | 26 class ByteStreamWriter; |
25 class DownloadRequestHandle; | |
26 class PowerSaveBlocker; | 27 class PowerSaveBlocker; |
| 28 class UrlDownloader; |
27 struct DownloadCreateInfo; | 29 struct DownloadCreateInfo; |
28 | 30 |
29 // Forwards data to the download thread. | 31 // Forwards data to the download thread. |
30 class CONTENT_EXPORT DownloadResourceHandler | 32 class CONTENT_EXPORT DownloadRequestCore |
31 : public ResourceHandler, | 33 : public base::SupportsWeakPtr<DownloadRequestCore> { |
32 public base::SupportsWeakPtr<DownloadResourceHandler> { | |
33 public: | 34 public: |
34 struct DownloadTabInfo; | 35 // Size of the buffer used between the DownloadRequestCore and the |
35 | |
36 // Size of the buffer used between the DownloadResourceHandler and the | |
37 // downstream receiver of its output. | 36 // downstream receiver of its output. |
38 static const int kDownloadByteStreamSize; | 37 static const int kDownloadByteStreamSize; |
39 | 38 |
40 // started_cb will be called exactly once on the UI thread. | 39 // started_cb will be called exactly once on the UI thread. |
41 // |id| should be invalid if the id should be automatically assigned. | 40 // |id| should be invalid if the id should be automatically assigned. |
42 DownloadResourceHandler( | 41 DownloadRequestCore( |
43 uint32 id, | 42 uint32 id, |
44 net::URLRequest* request, | 43 net::URLRequest* request, |
45 const DownloadUrlParameters::OnStartedCallback& started_cb, | 44 const DownloadUrlParameters::OnStartedCallback& started_cb, |
46 scoped_ptr<DownloadSaveInfo> save_info); | 45 scoped_ptr<DownloadSaveInfo> save_info, |
47 | 46 base::WeakPtr<DownloadManagerImpl> download_manager); |
48 bool OnRequestRedirected(const net::RedirectInfo& redirect_info, | 47 ~DownloadRequestCore(); |
49 ResourceResponse* response, | |
50 bool* defer) override; | |
51 | 48 |
52 // Send the download creation information to the download thread. | 49 // Send the download creation information to the download thread. |
53 bool OnResponseStarted(ResourceResponse* response, bool* defer) override; | 50 bool OnResponseStarted(); |
54 | |
55 // Pass-through implementation. | |
56 bool OnWillStart(const GURL& url, bool* defer) override; | |
57 | |
58 // Pass-through implementation. | |
59 bool OnBeforeNetworkStart(const GURL& url, bool* defer) override; | |
60 | 51 |
61 // Create a new buffer, which will be handed to the download thread for file | 52 // Create a new buffer, which will be handed to the download thread for file |
62 // writing and deletion. | 53 // writing and deletion. |
63 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 54 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
64 int* buf_size, | 55 int* buf_size, |
65 int min_size) override; | 56 int min_size); |
66 | 57 |
67 bool OnReadCompleted(int bytes_read, bool* defer) override; | 58 bool OnReadCompleted(int bytes_read, bool* defer); |
68 | 59 |
69 void OnResponseCompleted(const net::URLRequestStatus& status, | 60 void OnResponseCompleted(const net::URLRequestStatus& status); |
70 const std::string& security_info, | |
71 bool* defer) override; | |
72 | |
73 // N/A to this flavor of DownloadHandler. | |
74 void OnDataDownloaded(int bytes_downloaded) override; | |
75 | 61 |
76 void PauseRequest(); | 62 void PauseRequest(); |
77 void ResumeRequest(); | 63 void ResumeRequest(); |
78 | 64 |
79 // May result in this object being deleted by its owner. | |
80 void CancelRequest(); | |
81 | |
82 std::string DebugString() const; | 65 std::string DebugString() const; |
83 | 66 |
| 67 void set_downloader(UrlDownloader* downloader) { downloader_ = downloader; } |
| 68 |
| 69 protected: |
| 70 net::URLRequest* request() const { return request_; } |
| 71 |
84 private: | 72 private: |
85 ~DownloadResourceHandler() override; | |
86 | |
87 // Arrange for started_cb_ to be called on the UI thread with the | 73 // Arrange for started_cb_ to be called on the UI thread with the |
88 // below values, nulling out started_cb_. Should only be called | 74 // below values, nulling out started_cb_. Should only be called |
89 // on the IO thread. | 75 // on the IO thread. |
90 void CallStartedCB(DownloadItem* item, | 76 void CallStartedCB(DownloadItem* item, |
91 DownloadInterruptReason interrupt_reason); | 77 DownloadInterruptReason interrupt_reason); |
92 | 78 |
| 79 net::URLRequest* request_; |
93 uint32 download_id_; | 80 uint32 download_id_; |
| 81 |
94 // This is read only on the IO thread, but may only | 82 // This is read only on the IO thread, but may only |
95 // be called on the UI thread. | 83 // be called on the UI thread. |
96 DownloadUrlParameters::OnStartedCallback started_cb_; | 84 DownloadUrlParameters::OnStartedCallback started_cb_; |
97 scoped_ptr<DownloadSaveInfo> save_info_; | 85 scoped_ptr<DownloadSaveInfo> save_info_; |
98 | 86 |
99 // Stores information about the download that must be acquired on the UI | 87 // Data flow |
100 // thread before StartOnUIThread is called. | 88 scoped_refptr<net::IOBuffer> read_buffer_; // From URLRequest. |
101 // Created on IO thread, but only accessed on UI thread. |tab_info_| holds | 89 scoped_ptr<ByteStreamWriter> stream_writer_; // To rest of system. |
102 // the pointer until we pass it off to StartOnUIThread or DeleteSoon. | |
103 // Marked as a scoped_ptr<> to indicate ownership of the structure, but | |
104 // deletion must occur on the IO thread. | |
105 scoped_ptr<DownloadTabInfo> tab_info_; | |
106 | 90 |
107 // Data flow | 91 // Keeps the system from sleeping while this is alive. If the |
108 scoped_refptr<net::IOBuffer> read_buffer_; // From URLRequest. | |
109 scoped_ptr<ByteStreamWriter> stream_writer_; // To rest of system. | |
110 | |
111 // Keeps the system from sleeping while this ResourceHandler is alive. If the | |
112 // system enters power saving mode while a request is alive, it can cause the | 92 // system enters power saving mode while a request is alive, it can cause the |
113 // request to fail and the associated download will be interrupted. | 93 // request to fail and the associated download will be interrupted. |
114 scoped_ptr<PowerSaveBlocker> power_save_blocker_; | 94 scoped_ptr<PowerSaveBlocker> power_save_blocker_; |
115 | 95 |
116 // The following are used to collect stats. | 96 // The following are used to collect stats. |
117 base::TimeTicks download_start_time_; | 97 base::TimeTicks download_start_time_; |
118 base::TimeTicks last_read_time_; | 98 base::TimeTicks last_read_time_; |
119 base::TimeTicks last_stream_pause_time_; | 99 base::TimeTicks last_stream_pause_time_; |
120 base::TimeDelta total_pause_time_; | 100 base::TimeDelta total_pause_time_; |
121 size_t last_buffer_size_; | 101 size_t last_buffer_size_; |
122 int64 bytes_read_; | 102 int64 bytes_read_; |
123 | 103 |
124 int pause_count_; | 104 int pause_count_; |
125 bool was_deferred_; | 105 bool was_deferred_; |
126 | 106 |
127 // For DCHECKing | 107 // For DCHECKing |
128 bool on_response_started_called_; | 108 bool on_response_started_called_; |
129 | 109 |
130 static const int kReadBufSize = 32768; // bytes | 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 |
131 static const int kThrottleTimeMs = 200; // milliseconds | 116 static const int kThrottleTimeMs = 200; // milliseconds |
132 | 117 |
133 DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler); | 118 DISALLOW_COPY_AND_ASSIGN(DownloadRequestCore); |
134 }; | 119 }; |
135 | 120 |
136 } // namespace content | 121 } // namespace content |
137 | 122 |
138 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_ | 123 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_REQUEST_CORE_H_ |
OLD | NEW |