Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: content/browser/download/download_resource_handler.cc

Issue 148133007: [Downloads] Always call DM::StartDownload() for explicit downloads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comment updates Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include "content/browser/download/download_resource_handler.h" 5 #include "content/browser/download/download_resource_handler.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback_helpers.h" 10 #include "base/callback_helpers.h"
(...skipping 30 matching lines...) Expand all
41 scoped_ptr<ByteStreamReader> stream, 41 scoped_ptr<ByteStreamReader> stream,
42 const DownloadUrlParameters::OnStartedCallback& started_cb) { 42 const DownloadUrlParameters::OnStartedCallback& started_cb) {
43 DCHECK_CURRENTLY_ON(BrowserThread::UI); 43 DCHECK_CURRENTLY_ON(BrowserThread::UI);
44 44
45 DownloadManager* download_manager = 45 DownloadManager* download_manager =
46 info->request_handle->GetDownloadManager(); 46 info->request_handle->GetDownloadManager();
47 if (!download_manager) { 47 if (!download_manager) {
48 // NULL in unittests or if the page closed right after starting the 48 // NULL in unittests or if the page closed right after starting the
49 // download. 49 // download.
50 if (!started_cb.is_null()) 50 if (!started_cb.is_null())
51 started_cb.Run(NULL, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED); 51 started_cb.Run(nullptr, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
52 52
53 // |stream| gets deleted on non-FILE thread, but it's ok since 53 if (stream)
54 // we're not using stream_writer_ yet. 54 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE,
55 55 stream.release());
56 return; 56 return;
57 } 57 }
58 58
59 info->tab_url = tab_info->tab_url; 59 info->tab_url = tab_info->tab_url;
60 info->tab_referrer_url = tab_info->tab_referrer_url; 60 info->tab_referrer_url = tab_info->tab_referrer_url;
61 61
62 download_manager->StartDownload(std::move(info), std::move(stream), 62 download_manager->StartDownload(std::move(info), std::move(stream),
63 started_cb); 63 started_cb);
64 } 64 }
65 65
(...skipping 10 matching lines...) Expand all
76 tab_info->tab_referrer_url = entry->GetReferrer().url; 76 tab_info->tab_referrer_url = entry->GetReferrer().url;
77 } 77 }
78 } 78 }
79 } 79 }
80 80
81 void DeleteOnUIThread( 81 void DeleteOnUIThread(
82 scoped_ptr<DownloadResourceHandler::DownloadTabInfo> tab_info) {} 82 scoped_ptr<DownloadResourceHandler::DownloadTabInfo> tab_info) {}
83 83
84 } // namespace 84 } // namespace
85 85
86 DownloadResourceHandler::DownloadResourceHandler( 86 DownloadResourceHandler::DownloadResourceHandler(net::URLRequest* request)
87 uint32_t id,
88 net::URLRequest* request,
89 const DownloadUrlParameters::OnStartedCallback& started_cb,
90 scoped_ptr<DownloadSaveInfo> save_info)
91 : ResourceHandler(request), 87 : ResourceHandler(request),
92 download_id_(id),
93 started_cb_(started_cb),
94 tab_info_(new DownloadTabInfo()), 88 tab_info_(new DownloadTabInfo()),
95 core_(request, 89 core_(request, this) {
96 std::move(save_info),
97 base::Bind(&DownloadResourceHandler::OnCoreReadyToRead,
98 base::Unretained(this))) {
99 // Do UI thread initialization for tab_info_ asap after 90 // Do UI thread initialization for tab_info_ asap after
100 // DownloadResourceHandler creation since the tab could be navigated 91 // DownloadResourceHandler creation since the tab could be navigated
101 // before StartOnUIThread gets called. This is safe because deletion 92 // before StartOnUIThread gets called. This is safe because deletion
102 // will occur via PostTask() as well, which will serialized behind this 93 // will occur via PostTask() as well, which will serialized behind this
103 // PostTask() 94 // PostTask()
104 const ResourceRequestInfoImpl* request_info = GetRequestInfo(); 95 const ResourceRequestInfoImpl* request_info = GetRequestInfo();
105 BrowserThread::PostTask( 96 BrowserThread::PostTask(
106 BrowserThread::UI, FROM_HERE, 97 BrowserThread::UI, FROM_HERE,
107 base::Bind(&InitializeDownloadTabInfoOnUIThread, 98 base::Bind(&InitializeDownloadTabInfoOnUIThread,
108 DownloadRequestHandle(AsWeakPtr(), request_info->GetChildID(), 99 DownloadRequestHandle(AsWeakPtr(), request_info->GetChildID(),
109 request_info->GetRouteID(), 100 request_info->GetRouteID(),
110 request_info->GetRequestID(), 101 request_info->GetRequestID(),
111 request_info->frame_tree_node_id()), 102 request_info->frame_tree_node_id()),
112 tab_info_.get())); 103 tab_info_.get()));
113 } 104 }
114 105
115 DownloadResourceHandler::~DownloadResourceHandler() { 106 DownloadResourceHandler::~DownloadResourceHandler() {
116 // This won't do anything if the callback was called before.
117 // If it goes through, it will likely be because OnWillStart() returned
118 // false somewhere in the chain of resource handlers.
119 CallStartedCB(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED);
120
121 if (tab_info_) { 107 if (tab_info_) {
122 BrowserThread::PostTask( 108 BrowserThread::PostTask(
123 BrowserThread::UI, FROM_HERE, 109 BrowserThread::UI, FROM_HERE,
124 base::Bind(&DeleteOnUIThread, base::Passed(&tab_info_))); 110 base::Bind(&DeleteOnUIThread, base::Passed(&tab_info_)));
125 } 111 }
126 } 112 }
127 113
128 bool DownloadResourceHandler::OnRequestRedirected( 114 bool DownloadResourceHandler::OnRequestRedirected(
129 const net::RedirectInfo& redirect_info, 115 const net::RedirectInfo& redirect_info,
130 ResourceResponse* response, 116 ResourceResponse* response,
131 bool* defer) { 117 bool* defer) {
132 return true; 118 return true;
133 } 119 }
134 120
135 // Send the download creation information to the download thread. 121 // Send the download creation information to the download thread.
136 bool DownloadResourceHandler::OnResponseStarted( 122 bool DownloadResourceHandler::OnResponseStarted(
137 ResourceResponse* response, 123 ResourceResponse* response,
138 bool* defer) { 124 bool* defer) {
139 scoped_ptr<DownloadCreateInfo> create_info;
140 scoped_ptr<ByteStreamReader> stream_reader;
141
142 core_.OnResponseStarted(&create_info, &stream_reader);
143
144 const ResourceRequestInfoImpl* request_info = GetRequestInfo();
145 create_info->download_id = download_id_;
146 create_info->has_user_gesture = request_info->HasUserGesture();
147 create_info->transition_type = request_info->GetPageTransition();
148 create_info->request_handle.reset(new DownloadRequestHandle(
149 AsWeakPtr(), request_info->GetChildID(), request_info->GetRouteID(),
150 request_info->GetRequestID(), request_info->frame_tree_node_id()));
151
152 // The MIME type in ResourceResponse is the product of 125 // The MIME type in ResourceResponse is the product of
153 // MimeTypeResourceHandler. 126 // MimeTypeResourceHandler.
154 create_info->mime_type = response->head.mime_type; 127 return core_.OnResponseStarted(response->head.mime_type);
155
156 BrowserThread::PostTask(
157 BrowserThread::UI, FROM_HERE,
158 base::Bind(&StartOnUIThread, base::Passed(&create_info),
159 base::Passed(&tab_info_), base::Passed(&stream_reader),
160 base::ResetAndReturn(&started_cb_)));
161 return true;
162 }
163
164 void DownloadResourceHandler::CallStartedCB(
165 DownloadInterruptReason interrupt_reason) {
166 DCHECK_CURRENTLY_ON(BrowserThread::IO);
167 if (started_cb_.is_null())
168 return;
169 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
170 base::Bind(base::ResetAndReturn(&started_cb_),
171 nullptr, interrupt_reason));
172 } 128 }
173 129
174 bool DownloadResourceHandler::OnWillStart(const GURL& url, bool* defer) { 130 bool DownloadResourceHandler::OnWillStart(const GURL& url, bool* defer) {
175 return true; 131 return true;
176 } 132 }
177 133
178 bool DownloadResourceHandler::OnBeforeNetworkStart(const GURL& url, 134 bool DownloadResourceHandler::OnBeforeNetworkStart(const GURL& url,
179 bool* defer) { 135 bool* defer) {
180 return true; 136 return true;
181 } 137 }
182 138
183 // Create a new buffer, which will be handed to the download thread for file 139 // Create a new buffer, which will be handed to the download thread for file
184 // writing and deletion. 140 // writing and deletion.
185 bool DownloadResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, 141 bool DownloadResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
186 int* buf_size, 142 int* buf_size,
187 int min_size) { 143 int min_size) {
188 return core_.OnWillRead(buf, buf_size, min_size); 144 return core_.OnWillRead(buf, buf_size, min_size);
189 } 145 }
190 146
191 // Pass the buffer to the download file writer. 147 // Pass the buffer to the download file writer.
192 bool DownloadResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { 148 bool DownloadResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
193 return core_.OnReadCompleted(bytes_read, defer); 149 return core_.OnReadCompleted(bytes_read, defer);
194 } 150 }
195 151
196 void DownloadResourceHandler::OnResponseCompleted( 152 void DownloadResourceHandler::OnResponseCompleted(
197 const net::URLRequestStatus& status, 153 const net::URLRequestStatus& status,
198 const std::string& security_info, 154 const std::string& security_info,
199 bool* defer) { 155 bool* defer) {
200 DownloadInterruptReason result = core_.OnResponseCompleted(status); 156 core_.OnResponseCompleted(status);
201 CallStartedCB(result);
202 } 157 }
203 158
204 void DownloadResourceHandler::OnDataDownloaded(int bytes_downloaded) { 159 void DownloadResourceHandler::OnDataDownloaded(int bytes_downloaded) {
205 NOTREACHED(); 160 NOTREACHED();
206 } 161 }
207 162
208 void DownloadResourceHandler::PauseRequest() { 163 void DownloadResourceHandler::PauseRequest() {
209 core_.PauseRequest(); 164 core_.PauseRequest();
210 } 165 }
211 166
212 void DownloadResourceHandler::ResumeRequest() { 167 void DownloadResourceHandler::ResumeRequest() {
213 core_.ResumeRequest(); 168 core_.ResumeRequest();
214 } 169 }
215 170
216 void DownloadResourceHandler::OnCoreReadyToRead() { 171 void DownloadResourceHandler::OnStart(
172 scoped_ptr<DownloadCreateInfo> create_info,
173 scoped_ptr<ByteStreamReader> stream_reader,
174 const DownloadUrlParameters::OnStartedCallback& callback) {
175 // If the user cancels the download, then don't call start. Instead ignore the
176 // download entirely.
177 if (create_info->result == DOWNLOAD_INTERRUPT_REASON_USER_CANCELED &&
178 create_info->download_id == DownloadItem::kInvalidId) {
179 if (!callback.is_null())
180 BrowserThread::PostTask(
181 BrowserThread::UI, FROM_HERE,
182 base::Bind(callback, nullptr, create_info->result));
183 return;
184 }
185
186 const ResourceRequestInfoImpl* request_info = GetRequestInfo();
187 create_info->has_user_gesture = request_info->HasUserGesture();
188 create_info->transition_type = request_info->GetPageTransition();
189
190 create_info->request_handle.reset(new DownloadRequestHandle(
191 AsWeakPtr(), request_info->GetChildID(), request_info->GetRouteID(),
192 request_info->GetRequestID(), request_info->frame_tree_node_id()));
193
194 BrowserThread::PostTask(
195 BrowserThread::UI, FROM_HERE,
196 base::Bind(&StartOnUIThread, base::Passed(&create_info),
197 base::Passed(&tab_info_), base::Passed(&stream_reader),
198 callback));
199 }
200
201 void DownloadResourceHandler::OnReadyToRead() {
217 DCHECK_CURRENTLY_ON(BrowserThread::IO); 202 DCHECK_CURRENTLY_ON(BrowserThread::IO);
218 controller()->Resume(); 203 controller()->Resume();
219 } 204 }
220 205
221 void DownloadResourceHandler::CancelRequest() { 206 void DownloadResourceHandler::CancelRequest() {
222 DCHECK_CURRENTLY_ON(BrowserThread::IO); 207 DCHECK_CURRENTLY_ON(BrowserThread::IO);
223 208
224 const ResourceRequestInfoImpl* info = GetRequestInfo(); 209 const ResourceRequestInfoImpl* info = GetRequestInfo();
225 ResourceDispatcherHostImpl::Get()->CancelRequest( 210 ResourceDispatcherHostImpl::Get()->CancelRequest(
226 info->GetChildID(), 211 info->GetChildID(),
(...skipping 13 matching lines...) Expand all
240 " }", 225 " }",
241 request() ? 226 request() ?
242 request()->url().spec().c_str() : 227 request()->url().spec().c_str() :
243 "<NULL request>", 228 "<NULL request>",
244 info->GetChildID(), 229 info->GetChildID(),
245 info->GetRequestID(), 230 info->GetRequestID(),
246 info->GetRouteID()); 231 info->GetRouteID());
247 } 232 }
248 233
249 } // namespace content 234 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698