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