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

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

Issue 148133007: [Downloads] Always call DM::StartDownload() for explicit downloads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix typos 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 // This file contains download browser tests that are known to be runnable 5 // This file contains download browser tests that are known to be runnable
6 // in a pure content context. Over time tests should be migrated here. 6 // in a pure content context. Over time tests should be migrated here.
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 #include <utility> 10 #include <utility>
(...skipping 11 matching lines...) Expand all
22 #include "base/strings/utf_string_conversions.h" 22 #include "base/strings/utf_string_conversions.h"
23 #include "base/threading/platform_thread.h" 23 #include "base/threading/platform_thread.h"
24 #include "base/time/time.h" 24 #include "base/time/time.h"
25 #include "build/build_config.h" 25 #include "build/build_config.h"
26 #include "content/browser/byte_stream.h" 26 #include "content/browser/byte_stream.h"
27 #include "content/browser/download/download_file_factory.h" 27 #include "content/browser/download/download_file_factory.h"
28 #include "content/browser/download/download_file_impl.h" 28 #include "content/browser/download/download_file_impl.h"
29 #include "content/browser/download/download_item_impl.h" 29 #include "content/browser/download/download_item_impl.h"
30 #include "content/browser/download/download_manager_impl.h" 30 #include "content/browser/download/download_manager_impl.h"
31 #include "content/browser/download/download_resource_handler.h" 31 #include "content/browser/download/download_resource_handler.h"
32 #include "content/browser/loader/resource_dispatcher_host_impl.h"
32 #include "content/browser/web_contents/web_contents_impl.h" 33 #include "content/browser/web_contents/web_contents_impl.h"
33 #include "content/public/browser/power_save_blocker.h" 34 #include "content/public/browser/power_save_blocker.h"
35 #include "content/public/browser/resource_dispatcher_host_delegate.h"
36 #include "content/public/browser/resource_throttle.h"
34 #include "content/public/common/content_features.h" 37 #include "content/public/common/content_features.h"
35 #include "content/public/common/webplugininfo.h" 38 #include "content/public/common/webplugininfo.h"
36 #include "content/public/test/browser_test_utils.h" 39 #include "content/public/test/browser_test_utils.h"
37 #include "content/public/test/content_browser_test.h" 40 #include "content/public/test/content_browser_test.h"
38 #include "content/public/test/content_browser_test_utils.h" 41 #include "content/public/test/content_browser_test_utils.h"
39 #include "content/public/test/download_test_observer.h" 42 #include "content/public/test/download_test_observer.h"
40 #include "content/public/test/test_download_request_handler.h" 43 #include "content/public/test/test_download_request_handler.h"
41 #include "content/public/test/test_file_error_injector.h" 44 #include "content/public/test/test_file_error_injector.h"
42 #include "content/public/test/test_utils.h" 45 #include "content/public/test/test_utils.h"
43 #include "content/shell/browser/shell.h" 46 #include "content/shell/browser/shell.h"
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 static DownloadManagerImpl* DownloadManagerForShell(Shell* shell) { 114 static DownloadManagerImpl* DownloadManagerForShell(Shell* shell) {
112 // We're in a content_browsertest; we know that the DownloadManager 115 // We're in a content_browsertest; we know that the DownloadManager
113 // is a DownloadManagerImpl. 116 // is a DownloadManagerImpl.
114 return static_cast<DownloadManagerImpl*>( 117 return static_cast<DownloadManagerImpl*>(
115 BrowserContext::GetDownloadManager( 118 BrowserContext::GetDownloadManager(
116 shell->web_contents()->GetBrowserContext())); 119 shell->web_contents()->GetBrowserContext()));
117 } 120 }
118 121
119 class DownloadFileWithDelay : public DownloadFileImpl { 122 class DownloadFileWithDelay : public DownloadFileImpl {
120 public: 123 public:
121 DownloadFileWithDelay( 124 DownloadFileWithDelay(const DownloadSaveInfo& save_info,
122 scoped_ptr<DownloadSaveInfo> save_info, 125 const base::FilePath& default_download_directory,
123 const base::FilePath& default_download_directory, 126 const GURL& url,
124 const GURL& url, 127 const GURL& referrer_url,
125 const GURL& referrer_url, 128 bool calculate_hash,
126 bool calculate_hash, 129 base::File file,
127 scoped_ptr<ByteStreamReader> stream, 130 scoped_ptr<ByteStreamReader> stream,
128 const net::BoundNetLog& bound_net_log, 131 const net::BoundNetLog& bound_net_log,
129 scoped_ptr<PowerSaveBlocker> power_save_blocker, 132 scoped_ptr<PowerSaveBlocker> power_save_blocker,
130 base::WeakPtr<DownloadDestinationObserver> observer, 133 base::WeakPtr<DownloadDestinationObserver> observer,
131 base::WeakPtr<DownloadFileWithDelayFactory> owner); 134 base::WeakPtr<DownloadFileWithDelayFactory> owner);
132 135
133 ~DownloadFileWithDelay() override; 136 ~DownloadFileWithDelay() override;
134 137
135 // Wraps DownloadFileImpl::Rename* and intercepts the return callback, 138 // Wraps DownloadFileImpl::Rename* and intercepts the return callback,
136 // storing it in the factory that produced this object for later 139 // storing it in the factory that produced this object for later
137 // retrieval. 140 // retrieval.
138 void RenameAndUniquify(const base::FilePath& full_path, 141 void RenameAndUniquify(const base::FilePath& full_path,
139 const RenameCompletionCallback& callback) override; 142 const RenameCompletionCallback& callback) override;
140 void RenameAndAnnotate(const base::FilePath& full_path, 143 void RenameAndAnnotate(const base::FilePath& full_path,
141 const RenameCompletionCallback& callback) override; 144 const RenameCompletionCallback& callback) override;
(...skipping 16 matching lines...) Expand all
158 }; 161 };
159 162
160 // All routines on this class must be called on the UI thread. 163 // All routines on this class must be called on the UI thread.
161 class DownloadFileWithDelayFactory : public DownloadFileFactory { 164 class DownloadFileWithDelayFactory : public DownloadFileFactory {
162 public: 165 public:
163 DownloadFileWithDelayFactory(); 166 DownloadFileWithDelayFactory();
164 ~DownloadFileWithDelayFactory() override; 167 ~DownloadFileWithDelayFactory() override;
165 168
166 // DownloadFileFactory interface. 169 // DownloadFileFactory interface.
167 DownloadFile* CreateFile( 170 DownloadFile* CreateFile(
168 scoped_ptr<DownloadSaveInfo> save_info, 171 const DownloadSaveInfo& save_info,
169 const base::FilePath& default_download_directory, 172 const base::FilePath& default_download_directory,
170 const GURL& url, 173 const GURL& url,
171 const GURL& referrer_url, 174 const GURL& referrer_url,
172 bool calculate_hash, 175 bool calculate_hash,
176 base::File file,
173 scoped_ptr<ByteStreamReader> stream, 177 scoped_ptr<ByteStreamReader> stream,
174 const net::BoundNetLog& bound_net_log, 178 const net::BoundNetLog& bound_net_log,
175 base::WeakPtr<DownloadDestinationObserver> observer) override; 179 base::WeakPtr<DownloadDestinationObserver> observer) override;
176 180
177 void AddRenameCallback(base::Closure callback); 181 void AddRenameCallback(base::Closure callback);
178 void GetAllRenameCallbacks(std::vector<base::Closure>* results); 182 void GetAllRenameCallbacks(std::vector<base::Closure>* results);
179 183
180 // Do not return until GetAllRenameCallbacks() will return a non-empty list. 184 // Do not return until GetAllRenameCallbacks() will return a non-empty list.
181 void WaitForSomeCallback(); 185 void WaitForSomeCallback();
182 186
183 private: 187 private:
184 std::vector<base::Closure> rename_callbacks_; 188 std::vector<base::Closure> rename_callbacks_;
185 bool waiting_; 189 bool waiting_;
186 base::WeakPtrFactory<DownloadFileWithDelayFactory> weak_ptr_factory_; 190 base::WeakPtrFactory<DownloadFileWithDelayFactory> weak_ptr_factory_;
187 191
188 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelayFactory); 192 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelayFactory);
189 }; 193 };
190 194
191 DownloadFileWithDelay::DownloadFileWithDelay( 195 DownloadFileWithDelay::DownloadFileWithDelay(
192 scoped_ptr<DownloadSaveInfo> save_info, 196 const DownloadSaveInfo& save_info,
193 const base::FilePath& default_download_directory, 197 const base::FilePath& default_download_directory,
194 const GURL& url, 198 const GURL& url,
195 const GURL& referrer_url, 199 const GURL& referrer_url,
196 bool calculate_hash, 200 bool calculate_hash,
201 base::File file,
197 scoped_ptr<ByteStreamReader> stream, 202 scoped_ptr<ByteStreamReader> stream,
198 const net::BoundNetLog& bound_net_log, 203 const net::BoundNetLog& bound_net_log,
199 scoped_ptr<PowerSaveBlocker> power_save_blocker, 204 scoped_ptr<PowerSaveBlocker> power_save_blocker,
200 base::WeakPtr<DownloadDestinationObserver> observer, 205 base::WeakPtr<DownloadDestinationObserver> observer,
201 base::WeakPtr<DownloadFileWithDelayFactory> owner) 206 base::WeakPtr<DownloadFileWithDelayFactory> owner)
202 : DownloadFileImpl(std::move(save_info), 207 : DownloadFileImpl(save_info,
203 default_download_directory, 208 default_download_directory,
204 url, 209 url,
205 referrer_url, 210 referrer_url,
206 calculate_hash, 211 calculate_hash,
212 std::move(file),
207 std::move(stream), 213 std::move(stream),
208 bound_net_log, 214 bound_net_log,
209 observer), 215 observer),
210 owner_(owner) {} 216 owner_(owner) {}
211 217
212 DownloadFileWithDelay::~DownloadFileWithDelay() {} 218 DownloadFileWithDelay::~DownloadFileWithDelay() {}
213 219
214 void DownloadFileWithDelay::RenameAndUniquify( 220 void DownloadFileWithDelay::RenameAndUniquify(
215 const base::FilePath& full_path, 221 const base::FilePath& full_path,
216 const RenameCompletionCallback& callback) { 222 const RenameCompletionCallback& callback) {
(...skipping 23 matching lines...) Expand all
240 factory->AddRenameCallback(base::Bind(original_callback, reason, path)); 246 factory->AddRenameCallback(base::Bind(original_callback, reason, path));
241 } 247 }
242 248
243 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory() 249 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory()
244 : waiting_(false), 250 : waiting_(false),
245 weak_ptr_factory_(this) {} 251 weak_ptr_factory_(this) {}
246 252
247 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {} 253 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {}
248 254
249 DownloadFile* DownloadFileWithDelayFactory::CreateFile( 255 DownloadFile* DownloadFileWithDelayFactory::CreateFile(
250 scoped_ptr<DownloadSaveInfo> save_info, 256 const DownloadSaveInfo& save_info,
251 const base::FilePath& default_download_directory, 257 const base::FilePath& default_download_directory,
252 const GURL& url, 258 const GURL& url,
253 const GURL& referrer_url, 259 const GURL& referrer_url,
254 bool calculate_hash, 260 bool calculate_hash,
261 base::File file,
255 scoped_ptr<ByteStreamReader> stream, 262 scoped_ptr<ByteStreamReader> stream,
256 const net::BoundNetLog& bound_net_log, 263 const net::BoundNetLog& bound_net_log,
257 base::WeakPtr<DownloadDestinationObserver> observer) { 264 base::WeakPtr<DownloadDestinationObserver> observer) {
258 scoped_ptr<PowerSaveBlocker> psb(PowerSaveBlocker::Create( 265 scoped_ptr<PowerSaveBlocker> psb(PowerSaveBlocker::Create(
259 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, 266 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension,
260 PowerSaveBlocker::kReasonOther, "Download in progress")); 267 PowerSaveBlocker::kReasonOther, "Download in progress"));
261 return new DownloadFileWithDelay( 268 return new DownloadFileWithDelay(
262 std::move(save_info), default_download_directory, url, referrer_url, 269 save_info, default_download_directory, url, referrer_url, calculate_hash,
263 calculate_hash, std::move(stream), bound_net_log, std::move(psb), 270 std::move(file), std::move(stream), bound_net_log, std::move(psb),
264 observer, weak_ptr_factory_.GetWeakPtr()); 271 observer, weak_ptr_factory_.GetWeakPtr());
265 } 272 }
266 273
267 void DownloadFileWithDelayFactory::AddRenameCallback(base::Closure callback) { 274 void DownloadFileWithDelayFactory::AddRenameCallback(base::Closure callback) {
268 DCHECK_CURRENTLY_ON(BrowserThread::UI); 275 DCHECK_CURRENTLY_ON(BrowserThread::UI);
269 rename_callbacks_.push_back(callback); 276 rename_callbacks_.push_back(callback);
270 if (waiting_) 277 if (waiting_)
271 base::MessageLoopForUI::current()->QuitWhenIdle(); 278 base::MessageLoopForUI::current()->QuitWhenIdle();
272 } 279 }
273 280
274 void DownloadFileWithDelayFactory::GetAllRenameCallbacks( 281 void DownloadFileWithDelayFactory::GetAllRenameCallbacks(
275 std::vector<base::Closure>* results) { 282 std::vector<base::Closure>* results) {
276 DCHECK_CURRENTLY_ON(BrowserThread::UI); 283 DCHECK_CURRENTLY_ON(BrowserThread::UI);
277 results->swap(rename_callbacks_); 284 results->swap(rename_callbacks_);
278 } 285 }
279 286
280 void DownloadFileWithDelayFactory::WaitForSomeCallback() { 287 void DownloadFileWithDelayFactory::WaitForSomeCallback() {
281 DCHECK_CURRENTLY_ON(BrowserThread::UI); 288 DCHECK_CURRENTLY_ON(BrowserThread::UI);
282 289
283 if (rename_callbacks_.empty()) { 290 if (rename_callbacks_.empty()) {
284 waiting_ = true; 291 waiting_ = true;
285 RunMessageLoop(); 292 RunMessageLoop();
286 waiting_ = false; 293 waiting_ = false;
287 } 294 }
288 } 295 }
289 296
290 class CountingDownloadFile : public DownloadFileImpl { 297 class CountingDownloadFile : public DownloadFileImpl {
291 public: 298 public:
292 CountingDownloadFile(scoped_ptr<DownloadSaveInfo> save_info, 299 CountingDownloadFile(const DownloadSaveInfo& save_info,
293 const base::FilePath& default_downloads_directory, 300 const base::FilePath& default_downloads_directory,
294 const GURL& url, 301 const GURL& url,
295 const GURL& referrer_url, 302 const GURL& referrer_url,
296 bool calculate_hash, 303 bool calculate_hash,
304 base::File file,
297 scoped_ptr<ByteStreamReader> stream, 305 scoped_ptr<ByteStreamReader> stream,
298 const net::BoundNetLog& bound_net_log, 306 const net::BoundNetLog& bound_net_log,
299 scoped_ptr<PowerSaveBlocker> power_save_blocker, 307 scoped_ptr<PowerSaveBlocker> power_save_blocker,
300 base::WeakPtr<DownloadDestinationObserver> observer) 308 base::WeakPtr<DownloadDestinationObserver> observer)
301 : DownloadFileImpl(std::move(save_info), 309 : DownloadFileImpl(save_info,
302 default_downloads_directory, 310 default_downloads_directory,
303 url, 311 url,
304 referrer_url, 312 referrer_url,
305 calculate_hash, 313 calculate_hash,
314 std::move(file),
306 std::move(stream), 315 std::move(stream),
307 bound_net_log, 316 bound_net_log,
308 observer) {} 317 observer) {}
309 318
310 ~CountingDownloadFile() override { 319 ~CountingDownloadFile() override {
311 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 320 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
312 active_files_--; 321 active_files_--;
313 } 322 }
314 323
315 void Initialize(const InitializeCallback& callback) override { 324 void Initialize(const InitializeCallback& callback) override {
(...skipping 26 matching lines...) Expand all
342 351
343 int CountingDownloadFile::active_files_ = 0; 352 int CountingDownloadFile::active_files_ = 0;
344 353
345 class CountingDownloadFileFactory : public DownloadFileFactory { 354 class CountingDownloadFileFactory : public DownloadFileFactory {
346 public: 355 public:
347 CountingDownloadFileFactory() {} 356 CountingDownloadFileFactory() {}
348 ~CountingDownloadFileFactory() override {} 357 ~CountingDownloadFileFactory() override {}
349 358
350 // DownloadFileFactory interface. 359 // DownloadFileFactory interface.
351 DownloadFile* CreateFile( 360 DownloadFile* CreateFile(
352 scoped_ptr<DownloadSaveInfo> save_info, 361 const DownloadSaveInfo& save_info,
353 const base::FilePath& default_downloads_directory, 362 const base::FilePath& default_downloads_directory,
354 const GURL& url, 363 const GURL& url,
355 const GURL& referrer_url, 364 const GURL& referrer_url,
356 bool calculate_hash, 365 bool calculate_hash,
366 base::File file,
357 scoped_ptr<ByteStreamReader> stream, 367 scoped_ptr<ByteStreamReader> stream,
358 const net::BoundNetLog& bound_net_log, 368 const net::BoundNetLog& bound_net_log,
359 base::WeakPtr<DownloadDestinationObserver> observer) override { 369 base::WeakPtr<DownloadDestinationObserver> observer) override {
360 scoped_ptr<PowerSaveBlocker> psb(PowerSaveBlocker::Create( 370 scoped_ptr<PowerSaveBlocker> psb(PowerSaveBlocker::Create(
361 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, 371 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension,
362 PowerSaveBlocker::kReasonOther, "Download in progress")); 372 PowerSaveBlocker::kReasonOther, "Download in progress"));
363 return new CountingDownloadFile( 373 return new CountingDownloadFile(save_info, default_downloads_directory, url,
364 std::move(save_info), default_downloads_directory, url, referrer_url, 374 referrer_url, calculate_hash,
365 calculate_hash, std::move(stream), bound_net_log, std::move(psb), 375 std::move(file), std::move(stream),
366 observer); 376 bound_net_log, std::move(psb), observer);
367 } 377 }
368 }; 378 };
369 379
370 class TestShellDownloadManagerDelegate : public ShellDownloadManagerDelegate { 380 class TestShellDownloadManagerDelegate : public ShellDownloadManagerDelegate {
371 public: 381 public:
372 TestShellDownloadManagerDelegate() 382 TestShellDownloadManagerDelegate()
373 : delay_download_open_(false) {} 383 : delay_download_open_(false) {}
374 ~TestShellDownloadManagerDelegate() override {} 384 ~TestShellDownloadManagerDelegate() override {}
375 385
376 bool ShouldOpenDownload( 386 bool ShouldOpenDownload(
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 download, base::Bind(&IsDownloadInState, DownloadItem::IN_PROGRESS)) 605 download, base::Bind(&IsDownloadInState, DownloadItem::IN_PROGRESS))
596 .WaitForEvent(); 606 .WaitForEvent();
597 } 607 }
598 608
599 void WaitForCompletion(DownloadItem* download) { 609 void WaitForCompletion(DownloadItem* download) {
600 DownloadUpdatedObserver( 610 DownloadUpdatedObserver(
601 download, base::Bind(&IsDownloadInState, DownloadItem::COMPLETE)) 611 download, base::Bind(&IsDownloadInState, DownloadItem::COMPLETE))
602 .WaitForEvent(); 612 .WaitForEvent();
603 } 613 }
604 614
615 void WaitForCancel(DownloadItem* download) {
616 DownloadUpdatedObserver(
617 download, base::Bind(&IsDownloadInState, DownloadItem::CANCELLED))
618 .WaitForEvent();
619 }
620
605 // Note: Cannot be used with other alternative DownloadFileFactorys 621 // Note: Cannot be used with other alternative DownloadFileFactorys
606 void SetupEnsureNoPendingDownloads() { 622 void SetupEnsureNoPendingDownloads() {
607 DownloadManagerForShell(shell())->SetDownloadFileFactoryForTesting( 623 DownloadManagerForShell(shell())->SetDownloadFileFactoryForTesting(
608 scoped_ptr<DownloadFileFactory>(new CountingDownloadFileFactory())); 624 scoped_ptr<DownloadFileFactory>(new CountingDownloadFileFactory()));
609 } 625 }
610 626
611 bool EnsureNoPendingDownloads() { 627 bool EnsureNoPendingDownloads() {
612 bool result = true; 628 bool result = true;
613 BrowserThread::PostTask( 629 BrowserThread::PostTask(
614 BrowserThread::IO, FROM_HERE, 630 BrowserThread::IO, FROM_HERE,
(...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after
1116 ASSERT_TRUE(requests[1].request_headers.GetHeader( 1132 ASSERT_TRUE(requests[1].request_headers.GetHeader(
1117 net::HttpRequestHeaders::kIfRange, &value)); 1133 net::HttpRequestHeaders::kIfRange, &value));
1118 EXPECT_EQ(parameters.etag, value); 1134 EXPECT_EQ(parameters.etag, value);
1119 1135
1120 ASSERT_TRUE(requests[1].request_headers.GetHeader( 1136 ASSERT_TRUE(requests[1].request_headers.GetHeader(
1121 net::HttpRequestHeaders::kRange, &value)); 1137 net::HttpRequestHeaders::kRange, &value));
1122 EXPECT_EQ(base::StringPrintf("bytes=%" PRId64 "-", interruption.offset), 1138 EXPECT_EQ(base::StringPrintf("bytes=%" PRId64 "-", interruption.offset),
1123 value); 1139 value);
1124 } 1140 }
1125 1141
1142 // Resumption should only attempt to contact the final URL if the download has a
1143 // URL chain.
1144 IN_PROC_BROWSER_TEST_P(DownloadResumptionContentTest, RedirectBeforeResume) {
1145 TestDownloadRequestHandler request_handler_1(
1146 GURL("http://example.com/first-url"));
1147 request_handler_1.StartServingStaticResponse(
1148 "HTTP/1.1 302 Redirect\r\n"
1149 "Location: http://example.com/second-url\r\n"
1150 "\r\n");
1151
1152 TestDownloadRequestHandler request_handler_2(
1153 GURL("http://example.com/second-url"));
1154 request_handler_2.StartServingStaticResponse(
1155 "HTTP/1.1 302 Redirect\r\n"
1156 "Location: http://example.com/third-url\r\n"
1157 "\r\n");
1158
1159 TestDownloadRequestHandler request_handler_3(
1160 GURL("http://example.com/third-url"));
1161 request_handler_3.StartServingStaticResponse(
1162 "HTTP/1.1 302 Redirect\r\n"
1163 "Location: http://example.com/download\r\n"
1164 "\r\n");
1165
1166 TestDownloadRequestHandler resumable_request_handler(
1167 GURL("http://example.com/download"));
1168 TestDownloadRequestHandler::Parameters parameters =
1169 TestDownloadRequestHandler::Parameters::WithSingleInterruption();
1170 resumable_request_handler.StartServing(parameters);
1171
1172 DownloadItem* download = StartDownloadAndReturnItem(
1173 initiator_shell_for_resumption(), request_handler_1.url());
1174 WaitForInterrupt(download);
1175
1176 EXPECT_EQ(4u, download->GetUrlChain().size());
1177 EXPECT_EQ(request_handler_1.url(), download->GetOriginalUrl());
1178 EXPECT_EQ(resumable_request_handler.url(), download->GetURL());
1179
1180 // Now that the download is interrupted, make all intermediate servers return
1181 // a 404. The only way a resumption request would succeed if the resumption
1182 // request is sent to the final server in the chain.
1183 const char k404Response[] = "HTTP/1.1 404 Not found\r\n\r\n";
1184 request_handler_1.StartServingStaticResponse(k404Response);
1185 request_handler_2.StartServingStaticResponse(k404Response);
1186 request_handler_3.StartServingStaticResponse(k404Response);
1187
1188 PrepareToResume();
1189 download->Resume();
1190 WaitForCompletion(download);
1191
1192 ASSERT_NO_FATAL_FAILURE(ReadAndVerifyFileContents(
1193 parameters.pattern_generator_seed, parameters.size,
1194 download->GetTargetFilePath()));
1195 }
1196
1197 // If the server response for the resumption request specifies a bad range (i.e.
1198 // not the range that was requested or an invalid or missing Content-Range
1199 // header), then the download should be marked as interrupted again without
1200 // discarding the partial state.
1201 IN_PROC_BROWSER_TEST_P(DownloadResumptionContentTest, BadRangeHeader) {
1202 TestDownloadRequestHandler request_handler;
1203 TestDownloadRequestHandler::Parameters parameters =
1204 TestDownloadRequestHandler::Parameters::WithSingleInterruption();
1205 request_handler.StartServing(parameters);
1206
1207 DownloadItem* download = StartDownloadAndReturnItem(
1208 initiator_shell_for_resumption(), request_handler.url());
1209 WaitForInterrupt(download);
1210
1211 // Upon resumption, the server starts responding with a bad range header.
1212 request_handler.StartServingStaticResponse(
1213 "HTTP/1.1 206 Partial Content\r\n"
1214 "Content-Range: bytes 1000000-2000000/3000000\r\n"
1215 "\r\n");
1216 PrepareToResume();
1217 download->Resume();
1218 WaitForInterrupt(download);
1219 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT,
1220 download->GetLastReason());
1221
1222 // Or this time, the server sends a response with an invalid Content-Range
1223 // header.
1224 request_handler.StartServingStaticResponse(
1225 "HTTP/1.1 206 Partial Content\r\n"
1226 "Content-Range: ooga-booga-booga-booga\r\n"
1227 "\r\n");
1228 download->Resume();
1229 WaitForInterrupt(download);
1230 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT,
1231 download->GetLastReason());
1232
1233 // Or no Content-Range header at all.
1234 request_handler.StartServingStaticResponse(
1235 "HTTP/1.1 206 Partial Content\r\n"
1236 "Some-Headers: ooga-booga-booga-booga\r\n"
1237 "\r\n");
1238 download->Resume();
1239 WaitForInterrupt(download);
1240 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT,
1241 download->GetLastReason());
1242
1243 // Back to the original request handler. Resumption should now succeed, and
1244 // use the partial data it had prior to the first interruption.
1245 request_handler.StartServing(parameters);
1246 download->Resume();
1247 WaitForCompletion(download);
1248
1249 ASSERT_EQ(parameters.size, download->GetReceivedBytes());
1250 ASSERT_EQ(parameters.size, download->GetTotalBytes());
1251 ASSERT_NO_FATAL_FAILURE(ReadAndVerifyFileContents(
1252 parameters.pattern_generator_seed, parameters.size,
1253 download->GetTargetFilePath()));
1254
1255 // Characterization risk: The next portion of the test examines the requests
1256 // that were sent out while downloading our resource. These requests
1257 // correspond to the requests that were generated by the browser and the
1258 // downloads system and may change as implementation details change.
1259 TestDownloadRequestHandler::CompletedRequests requests;
1260 request_handler.GetCompletedRequestInfo(&requests);
1261
1262 ASSERT_EQ(5u, requests.size());
1263
1264 // None of the request should have transferred the entire resource.
1265 EXPECT_GT(parameters.size, requests[0].transferred_byte_count);
1266 EXPECT_EQ(0, requests[1].transferred_byte_count);
1267 EXPECT_EQ(0, requests[2].transferred_byte_count);
1268 EXPECT_EQ(0, requests[3].transferred_byte_count);
1269 EXPECT_GT(parameters.size, requests[4].transferred_byte_count);
1270 }
1271
1126 // A partial resumption results in an HTTP 200 response. I.e. the server ignored 1272 // A partial resumption results in an HTTP 200 response. I.e. the server ignored
1127 // the range request and sent the entire resource instead. For If-Range requests 1273 // the range request and sent the entire resource instead. For If-Range requests
1128 // (as opposed to If-Match), the behavior for a precondition failure is also to 1274 // (as opposed to If-Match), the behavior for a precondition failure is also to
1129 // respond with a 200. So this test case covers both validation failure and 1275 // respond with a 200. So this test case covers both validation failure and
1130 // ignoring the range request. 1276 // ignoring the range request.
1131 IN_PROC_BROWSER_TEST_P(DownloadResumptionContentTest, 1277 IN_PROC_BROWSER_TEST_P(DownloadResumptionContentTest,
1132 RestartIfNotPartialResponse) { 1278 RestartIfNotPartialResponse) {
1133 const int kOriginalPatternGeneratorSeed = 1; 1279 const int kOriginalPatternGeneratorSeed = 1;
1134 const int kNewPatternGeneratorSeed = 2; 1280 const int kNewPatternGeneratorSeed = 2;
1135 1281
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
1282 1428
1283 // Start and watch for interrupt. 1429 // Start and watch for interrupt.
1284 DownloadItem* download(StartDownloadAndReturnItem( 1430 DownloadItem* download(StartDownloadAndReturnItem(
1285 initiator_shell_for_resumption(), request_handler.url())); 1431 initiator_shell_for_resumption(), request_handler.url()));
1286 WaitForInterrupt(download); 1432 WaitForInterrupt(download);
1287 ASSERT_EQ(DownloadItem::INTERRUPTED, download->GetState()); 1433 ASSERT_EQ(DownloadItem::INTERRUPTED, download->GetState());
1288 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE, 1434 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE,
1289 download->GetLastReason()); 1435 download->GetLastReason());
1290 EXPECT_EQ(0, download->GetReceivedBytes()); 1436 EXPECT_EQ(0, download->GetReceivedBytes());
1291 EXPECT_TRUE(download->GetFullPath().empty()); 1437 EXPECT_TRUE(download->GetFullPath().empty());
1292 EXPECT_TRUE(download->GetTargetFilePath().empty()); 1438 EXPECT_FALSE(download->GetTargetFilePath().empty());
1293 1439
1294 // We need to make sure that any cross-thread downloads communication has 1440 // We need to make sure that any cross-thread downloads communication has
1295 // quiesced before clearing and injecting the new errors, as the 1441 // quiesced before clearing and injecting the new errors, as the
1296 // InjectErrors() routine alters the currently in use download file 1442 // InjectErrors() routine alters the currently in use download file
1297 // factory, which is a file thread object. 1443 // factory, which is a file thread object.
1298 RunAllPendingInMessageLoop(BrowserThread::FILE); 1444 RunAllPendingInMessageLoop(BrowserThread::FILE);
1299 RunAllPendingInMessageLoop(); 1445 RunAllPendingInMessageLoop();
1300 1446
1301 // Clear the old errors list. 1447 // Clear the old errors list.
1302 injector->ClearErrors(); 1448 injector->ClearErrors();
(...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after
1827 std::vector<DownloadItem*> downloads; 1973 std::vector<DownloadItem*> downloads;
1828 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); 1974 DownloadManagerForShell(shell())->GetAllDownloads(&downloads);
1829 ASSERT_EQ(1u, downloads.size()); 1975 ASSERT_EQ(1u, downloads.size());
1830 1976
1831 EXPECT_EQ(FILE_PATH_LITERAL("suggested-filename"), 1977 EXPECT_EQ(FILE_PATH_LITERAL("suggested-filename"),
1832 downloads[0]->GetTargetFilePath().BaseName().value()); 1978 downloads[0]->GetTargetFilePath().BaseName().value());
1833 ASSERT_TRUE(origin_one.ShutdownAndWaitUntilComplete()); 1979 ASSERT_TRUE(origin_one.ShutdownAndWaitUntilComplete());
1834 ASSERT_TRUE(origin_two.ShutdownAndWaitUntilComplete()); 1980 ASSERT_TRUE(origin_two.ShutdownAndWaitUntilComplete());
1835 } 1981 }
1836 1982
1983 // A request for a non-existent resource should still result in a DownloadItem
1984 // that's created in an interrupted state.
1985 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadAttributeServerError) {
1986 ASSERT_TRUE(embedded_test_server()->Start());
1987
1988 GURL download_url =
1989 embedded_test_server()->GetURL("/download/does-not-exist");
1990 GURL document_url = embedded_test_server()->GetURL(
1991 std::string("/download/download-attribute.html?target=") +
1992 download_url.spec());
1993
1994 DownloadItem* download = StartDownloadAndReturnItem(shell(), document_url);
1995 WaitForInterrupt(download);
1996
1997 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT,
1998 download->GetLastReason());
1999 }
2000
2001 namespace {
2002
2003 void ErrorReturningRequestHandler(
2004 const net::HttpRequestHeaders& headers,
2005 const TestDownloadRequestHandler::OnStartResponseCallback& callback) {
2006 callback.Run(std::string(), net::ERR_INTERNET_DISCONNECTED);
2007 }
2008
2009 } // namespace
2010
2011 // A request that fails before it gets a response from the server should also
2012 // result in a DownloadItem that's created in an interrupted state.
2013 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadAttributeNetworkError) {
2014 ASSERT_TRUE(embedded_test_server()->Start());
2015 TestDownloadRequestHandler request_handler;
2016 TestDownloadRequestHandler::Parameters parameters;
2017
2018 parameters.on_start_handler = base::Bind(&ErrorReturningRequestHandler);
2019 request_handler.StartServing(parameters);
2020
2021 GURL document_url = embedded_test_server()->GetURL(
2022 std::string("/download/download-attribute.html?target=") +
2023 request_handler.url().spec());
2024 DownloadItem* download = StartDownloadAndReturnItem(shell(), document_url);
2025 WaitForInterrupt(download);
2026
2027 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED,
2028 download->GetLastReason());
2029 }
2030
2031 // A request that fails due to it being rejected by policy should result in a
2032 // DownloadItem that's marked as interrupted.
2033 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadAttributeInvalidURL) {
2034 ASSERT_TRUE(embedded_test_server()->Start());
2035
2036 GURL document_url = embedded_test_server()->GetURL(
2037 "/download/download-attribute.html?target=about:version");
2038 DownloadItem* download = StartDownloadAndReturnItem(shell(), document_url);
2039 WaitForInterrupt(download);
2040
2041 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST,
2042 download->GetLastReason());
2043 EXPECT_FALSE(download->CanResume());
2044 }
2045
1837 // The file empty.bin is served with a MIME type of application/octet-stream. 2046 // The file empty.bin is served with a MIME type of application/octet-stream.
1838 // The content body is empty. Make sure this case is handled properly and we 2047 // The content body is empty. Make sure this case is handled properly and we
1839 // don't regress on http://crbug.com/320394. 2048 // don't regress on http://crbug.com/320394.
1840 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadGZipWithNoContent) { 2049 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadGZipWithNoContent) {
1841 GURL url = net::URLRequestMockHTTPJob::GetMockUrl("empty.bin"); 2050 GURL url = net::URLRequestMockHTTPJob::GetMockUrl("empty.bin");
1842 NavigateToURLAndWaitForDownload(shell(), url, DownloadItem::COMPLETE); 2051 NavigateToURLAndWaitForDownload(shell(), url, DownloadItem::COMPLETE);
1843 // That's it. This should work without crashing. 2052 // That's it. This should work without crashing.
1844 } 2053 }
1845 2054
1846 // Make sure that sniffed MIME types are correctly passed through to the 2055 // Make sure that sniffed MIME types are correctly passed through to the
1847 // download item. 2056 // download item.
1848 IN_PROC_BROWSER_TEST_F(DownloadContentTest, SniffedMimeType) { 2057 IN_PROC_BROWSER_TEST_F(DownloadContentTest, SniffedMimeType) {
1849 GURL url = net::URLRequestMockHTTPJob::GetMockUrl("gzip-content.gz"); 2058 GURL url = net::URLRequestMockHTTPJob::GetMockUrl("gzip-content.gz");
1850 DownloadItem* item = StartDownloadAndReturnItem(shell(), url); 2059 DownloadItem* item = StartDownloadAndReturnItem(shell(), url);
1851 WaitForCompletion(item); 2060 WaitForCompletion(item);
1852 2061
1853 EXPECT_STREQ("application/x-gzip", item->GetMimeType().c_str()); 2062 EXPECT_STREQ("application/x-gzip", item->GetMimeType().c_str());
1854 EXPECT_TRUE(item->GetOriginalMimeType().empty()); 2063 EXPECT_TRUE(item->GetOriginalMimeType().empty());
1855 } 2064 }
1856 2065
1857 IN_PROC_BROWSER_TEST_F(DownloadContentTest, Spam) { 2066 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DuplicateContentDisposition) {
1858 ASSERT_TRUE(embedded_test_server()->Start()); 2067 ASSERT_TRUE(embedded_test_server()->Start());
1859 2068
2069 // double-content-disposition.txt is served with two Content-Disposition
2070 // headers, both of which are identical.
1860 NavigateToURLAndWaitForDownload( 2071 NavigateToURLAndWaitForDownload(
1861 shell(), 2072 shell(),
1862 embedded_test_server()->GetURL( 2073 embedded_test_server()->GetURL(
1863 "/download/double-content-disposition.txt"), 2074 "/download/double-content-disposition.txt"),
1864 DownloadItem::COMPLETE); 2075 DownloadItem::COMPLETE);
1865 2076
1866 std::vector<DownloadItem*> downloads; 2077 std::vector<DownloadItem*> downloads;
1867 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); 2078 DownloadManagerForShell(shell())->GetAllDownloads(&downloads);
1868 ASSERT_EQ(1u, downloads.size()); 2079 ASSERT_EQ(1u, downloads.size());
1869 2080
1870 EXPECT_EQ(FILE_PATH_LITERAL("Jumboshrimp.txt"), 2081 EXPECT_EQ(FILE_PATH_LITERAL("Jumboshrimp.txt"),
1871 downloads[0]->GetTargetFilePath().BaseName().value()); 2082 downloads[0]->GetTargetFilePath().BaseName().value());
1872 } 2083 }
1873 2084
1874 } // namespace content 2085 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698