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

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: 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
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; 125 response_ = response;
140 scoped_ptr<ByteStreamReader> stream_reader; 126 return core_.OnResponseStarted();
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
153 // MimeTypeResourceHandler.
154 create_info->mime_type = 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 } 127 }
173 128
174 bool DownloadResourceHandler::OnWillStart(const GURL& url, bool* defer) { 129 bool DownloadResourceHandler::OnWillStart(const GURL& url, bool* defer) {
175 return true; 130 return true;
176 } 131 }
177 132
178 bool DownloadResourceHandler::OnBeforeNetworkStart(const GURL& url, 133 bool DownloadResourceHandler::OnBeforeNetworkStart(const GURL& url,
179 bool* defer) { 134 bool* defer) {
180 return true; 135 return true;
181 } 136 }
182 137
183 // Create a new buffer, which will be handed to the download thread for file 138 // Create a new buffer, which will be handed to the download thread for file
184 // writing and deletion. 139 // writing and deletion.
185 bool DownloadResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, 140 bool DownloadResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
186 int* buf_size, 141 int* buf_size,
187 int min_size) { 142 int min_size) {
188 return core_.OnWillRead(buf, buf_size, min_size); 143 return core_.OnWillRead(buf, buf_size, min_size);
189 } 144 }
190 145
191 // Pass the buffer to the download file writer. 146 // Pass the buffer to the download file writer.
192 bool DownloadResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { 147 bool DownloadResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
193 return core_.OnReadCompleted(bytes_read, defer); 148 return core_.OnReadCompleted(bytes_read, defer);
194 } 149 }
195 150
196 void DownloadResourceHandler::OnResponseCompleted( 151 void DownloadResourceHandler::OnResponseCompleted(
197 const net::URLRequestStatus& status, 152 const net::URLRequestStatus& status,
198 const std::string& security_info, 153 const std::string& security_info,
199 bool* defer) { 154 bool* defer) {
200 DownloadInterruptReason result = core_.OnResponseCompleted(status); 155 core_.OnResponseCompleted(status);
201 CallStartedCB(result);
202 } 156 }
203 157
204 void DownloadResourceHandler::OnDataDownloaded(int bytes_downloaded) { 158 void DownloadResourceHandler::OnDataDownloaded(int bytes_downloaded) {
205 NOTREACHED(); 159 NOTREACHED();
206 } 160 }
207 161
208 void DownloadResourceHandler::PauseRequest() { 162 void DownloadResourceHandler::PauseRequest() {
209 core_.PauseRequest(); 163 core_.PauseRequest();
210 } 164 }
211 165
212 void DownloadResourceHandler::ResumeRequest() { 166 void DownloadResourceHandler::ResumeRequest() {
213 core_.ResumeRequest(); 167 core_.ResumeRequest();
214 } 168 }
215 169
216 void DownloadResourceHandler::OnCoreReadyToRead() { 170 void DownloadResourceHandler::OnStart(
171 scoped_ptr<DownloadCreateInfo> create_info,
172 scoped_ptr<ByteStreamReader> stream_reader,
173 const DownloadUrlParameters::OnStartedCallback& callback) {
174 // If the user cancels the download, then don't call start. Instead ignore the
175 // download entirely.
176 if (create_info->result == DOWNLOAD_INTERRUPT_REASON_USER_CANCELED) {
177 if (!callback.is_null())
178 BrowserThread::PostTask(
179 BrowserThread::UI, FROM_HERE,
180 base::Bind(callback, nullptr, create_info->result));
181 return;
182 }
183
184 const ResourceRequestInfoImpl* request_info = GetRequestInfo();
185 create_info->has_user_gesture = request_info->HasUserGesture();
186 create_info->transition_type = request_info->GetPageTransition();
187
188 create_info->request_handle.reset(new DownloadRequestHandle(
189 AsWeakPtr(), request_info->GetChildID(), request_info->GetRouteID(),
190 request_info->GetRequestID(), request_info->frame_tree_node_id()));
191
192 // The MIME type in ResourceResponse is the product of
193 // MimeTypeResourceHandler.
194 if (create_info->result == DOWNLOAD_INTERRUPT_REASON_NONE)
195 create_info->mime_type = response_->head.mime_type;
196
197 BrowserThread::PostTask(
198 BrowserThread::UI, FROM_HERE,
199 base::Bind(&StartOnUIThread, base::Passed(&create_info),
200 base::Passed(&tab_info_), base::Passed(&stream_reader),
201 callback));
202 }
203
204 void DownloadResourceHandler::OnReadyToRead() {
217 DCHECK_CURRENTLY_ON(BrowserThread::IO); 205 DCHECK_CURRENTLY_ON(BrowserThread::IO);
218 controller()->Resume(); 206 controller()->Resume();
219 } 207 }
220 208
221 void DownloadResourceHandler::CancelRequest() { 209 void DownloadResourceHandler::CancelRequest() {
222 DCHECK_CURRENTLY_ON(BrowserThread::IO); 210 DCHECK_CURRENTLY_ON(BrowserThread::IO);
223 211
224 const ResourceRequestInfoImpl* info = GetRequestInfo(); 212 const ResourceRequestInfoImpl* info = GetRequestInfo();
225 ResourceDispatcherHostImpl::Get()->CancelRequest( 213 ResourceDispatcherHostImpl::Get()->CancelRequest(
226 info->GetChildID(), 214 info->GetChildID(),
(...skipping 13 matching lines...) Expand all
240 " }", 228 " }",
241 request() ? 229 request() ?
242 request()->url().spec().c_str() : 230 request()->url().spec().c_str() :
243 "<NULL request>", 231 "<NULL request>",
244 info->GetChildID(), 232 info->GetChildID(),
245 info->GetRequestID(), 233 info->GetRequestID(),
246 info->GetRouteID()); 234 info->GetRouteID());
247 } 235 }
248 236
249 } // namespace content 237 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698