| 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 // 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 "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
| 11 #include "content/browser/download/download_file_factory.h" | 11 #include "content/browser/download/download_file_factory.h" |
| 12 #include "content/browser/download/download_file_impl.h" | 12 #include "content/browser/download/download_file_impl.h" |
| 13 #include "content/browser/download/download_file_manager.h" | |
| 14 #include "content/browser/download/download_item_impl.h" | 13 #include "content/browser/download/download_item_impl.h" |
| 15 #include "content/browser/download/download_manager_impl.h" | 14 #include "content/browser/download/download_manager_impl.h" |
| 16 #include "content/browser/power_save_blocker.h" | 15 #include "content/browser/power_save_blocker.h" |
| 17 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" | |
| 18 #include "content/browser/web_contents/web_contents_impl.h" | 16 #include "content/browser/web_contents/web_contents_impl.h" |
| 19 #include "content/public/test/download_test_observer.h" | 17 #include "content/public/test/download_test_observer.h" |
| 20 #include "content/public/test/test_utils.h" | 18 #include "content/public/test/test_utils.h" |
| 21 #include "content/shell/shell.h" | 19 #include "content/shell/shell.h" |
| 22 #include "content/shell/shell_browser_context.h" | 20 #include "content/shell/shell_browser_context.h" |
| 23 #include "content/shell/shell_download_manager_delegate.h" | 21 #include "content/shell/shell_download_manager_delegate.h" |
| 24 #include "content/test/content_browser_test.h" | 22 #include "content/test/content_browser_test.h" |
| 25 #include "content/test/content_browser_test_utils.h" | 23 #include "content/test/content_browser_test_utils.h" |
| 26 #include "content/test/net/url_request_mock_http_job.h" | 24 #include "content/test/net/url_request_mock_http_job.h" |
| 27 #include "content/test/net/url_request_slow_download_job.h" | 25 #include "content/test/net/url_request_slow_download_job.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 // We're in a content_browsertest; we know that the DownloadManager | 66 // We're in a content_browsertest; we know that the DownloadManager |
| 69 // is a DownloadManagerImpl. | 67 // is a DownloadManagerImpl. |
| 70 return static_cast<DownloadManagerImpl*>( | 68 return static_cast<DownloadManagerImpl*>( |
| 71 BrowserContext::GetDownloadManager( | 69 BrowserContext::GetDownloadManager( |
| 72 shell->web_contents()->GetBrowserContext())); | 70 shell->web_contents()->GetBrowserContext())); |
| 73 } | 71 } |
| 74 | 72 |
| 75 class DownloadFileWithDelay : public DownloadFileImpl { | 73 class DownloadFileWithDelay : public DownloadFileImpl { |
| 76 public: | 74 public: |
| 77 DownloadFileWithDelay( | 75 DownloadFileWithDelay( |
| 78 scoped_ptr<DownloadCreateInfo> info, | 76 scoped_ptr<DownloadSaveInfo> save_info, |
| 79 scoped_ptr<content::ByteStreamReader> stream, | 77 const FilePath& default_download_directory, |
| 80 scoped_ptr<DownloadRequestHandleInterface> request_handle, | 78 const GURL& url, |
| 81 scoped_refptr<content::DownloadManager> download_manager, | 79 const GURL& referrer_url, |
| 80 int64 received_bytes, |
| 82 bool calculate_hash, | 81 bool calculate_hash, |
| 83 scoped_ptr<content::PowerSaveBlocker> power_save_blocker, | 82 scoped_ptr<ByteStreamReader> stream, |
| 84 const net::BoundNetLog& bound_net_log, | 83 const net::BoundNetLog& bound_net_log, |
| 85 // |owner| is required to outlive the DownloadFileWithDelay. | 84 scoped_ptr<PowerSaveBlocker> power_save_blocker, |
| 86 DownloadFileWithDelayFactory* owner); | 85 base::WeakPtr<DownloadDestinationObserver> observer, |
| 86 base::WeakPtr<DownloadFileWithDelayFactory> owner); |
| 87 | 87 |
| 88 virtual ~DownloadFileWithDelay(); | 88 virtual ~DownloadFileWithDelay(); |
| 89 | 89 |
| 90 // Wraps DownloadFileImpl::Rename and intercepts the return callback, | 90 // Wraps DownloadFileImpl::Rename and intercepts the return callback, |
| 91 // storing it in the factory that produced this object for later | 91 // storing it in the factory that produced this object for later |
| 92 // retrieval. | 92 // retrieval. |
| 93 virtual void Rename(const FilePath& full_path, | 93 virtual void Rename(const FilePath& full_path, |
| 94 bool overwrite_existing_file, | 94 bool overwrite_existing_file, |
| 95 const RenameCompletionCallback& callback) OVERRIDE; | 95 const RenameCompletionCallback& callback) OVERRIDE; |
| 96 | 96 |
| 97 // Wraps DownloadFileImpl::Detach and intercepts the return callback, | 97 // Wraps DownloadFileImpl::Detach and intercepts the return callback, |
| 98 // storing it in the factory that produced this object for later | 98 // storing it in the factory that produced this object for later |
| 99 // retrieval. | 99 // retrieval. |
| 100 virtual void Detach(base::Closure callback) OVERRIDE; | 100 virtual void Detach(base::Closure callback) OVERRIDE; |
| 101 | 101 |
| 102 private: | 102 private: |
| 103 static void RenameCallbackWrapper( | 103 static void RenameCallbackWrapper( |
| 104 DownloadFileWithDelayFactory* factory, | 104 DownloadFileWithDelayFactory* factory, |
| 105 const RenameCompletionCallback& original_callback, | 105 const RenameCompletionCallback& original_callback, |
| 106 content::DownloadInterruptReason reason, | 106 DownloadInterruptReason reason, |
| 107 const FilePath& path); | 107 const FilePath& path); |
| 108 | 108 |
| 109 static void DetachCallbackWrapper( | 109 static void DetachCallbackWrapper( |
| 110 DownloadFileWithDelayFactory* factory, | 110 DownloadFileWithDelayFactory* factory, |
| 111 const base::Closure& original_callback); | 111 const base::Closure& original_callback); |
| 112 | 112 |
| 113 // May only be used on the UI thread. | 113 // This variable may only be read on the FILE thread, and may only be |
| 114 DownloadFileWithDelayFactory* owner_; | 114 // indirected through (e.g. methods on DownloadFileWithDelayFactory called) |
| 115 // on the UI thread. This is because after construction, |
| 116 // DownloadFileWithDelay lives on the file thread, but |
| 117 // DownloadFileWithDelayFactory is purely a UI thread object. |
| 118 base::WeakPtr<DownloadFileWithDelayFactory> owner_; |
| 115 | 119 |
| 116 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelay); | 120 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelay); |
| 117 }; | 121 }; |
| 118 | 122 |
| 123 // All routines on this class must be called on the UI thread. |
| 119 class DownloadFileWithDelayFactory : public DownloadFileFactory { | 124 class DownloadFileWithDelayFactory : public DownloadFileFactory { |
| 120 public: | 125 public: |
| 121 DownloadFileWithDelayFactory(); | 126 DownloadFileWithDelayFactory(); |
| 122 virtual ~DownloadFileWithDelayFactory(); | 127 virtual ~DownloadFileWithDelayFactory(); |
| 123 | 128 |
| 124 // DownloadFileFactory interface. | 129 // DownloadFileFactory interface. |
| 125 virtual content::DownloadFile* CreateFile( | 130 virtual DownloadFile* CreateFile( |
| 126 scoped_ptr<DownloadCreateInfo> info, | 131 scoped_ptr<DownloadSaveInfo> save_info, |
| 127 scoped_ptr<content::ByteStreamReader> stream, | 132 const FilePath& default_download_directory, |
| 128 DownloadManager* download_manager, | 133 const GURL& url, |
| 134 const GURL& referrer_url, |
| 135 int64 received_bytes, |
| 129 bool calculate_hash, | 136 bool calculate_hash, |
| 130 const net::BoundNetLog& bound_net_log) OVERRIDE; | 137 scoped_ptr<ByteStreamReader> stream, |
| 138 const net::BoundNetLog& bound_net_log, |
| 139 base::WeakPtr<DownloadDestinationObserver> observer) OVERRIDE; |
| 131 | 140 |
| 132 // Must all be called on the UI thread. | |
| 133 void AddRenameCallback(base::Closure callback); | 141 void AddRenameCallback(base::Closure callback); |
| 134 void AddDetachCallback(base::Closure callback); | 142 void AddDetachCallback(base::Closure callback); |
| 135 void GetAllRenameCallbacks(std::vector<base::Closure>* results); | 143 void GetAllRenameCallbacks(std::vector<base::Closure>* results); |
| 136 void GetAllDetachCallbacks(std::vector<base::Closure>* results); | 144 void GetAllDetachCallbacks(std::vector<base::Closure>* results); |
| 137 | 145 |
| 138 // Do not return until either GetAllRenameCallbacks() or | 146 // Do not return until either GetAllRenameCallbacks() or |
| 139 // GetAllDetachCallbacks() will return a non-empty list. | 147 // GetAllDetachCallbacks() will return a non-empty list. |
| 140 void WaitForSomeCallback(); | 148 void WaitForSomeCallback(); |
| 141 | 149 |
| 142 private: | 150 private: |
| 151 base::WeakPtrFactory<DownloadFileWithDelayFactory> weak_ptr_factory_; |
| 143 std::vector<base::Closure> rename_callbacks_; | 152 std::vector<base::Closure> rename_callbacks_; |
| 144 std::vector<base::Closure> detach_callbacks_; | 153 std::vector<base::Closure> detach_callbacks_; |
| 145 bool waiting_; | 154 bool waiting_; |
| 146 | 155 |
| 147 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelayFactory); | 156 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelayFactory); |
| 148 }; | 157 }; |
| 149 | 158 |
| 150 DownloadFileWithDelay::DownloadFileWithDelay( | 159 DownloadFileWithDelay::DownloadFileWithDelay( |
| 151 scoped_ptr<DownloadCreateInfo> info, | 160 scoped_ptr<DownloadSaveInfo> save_info, |
| 152 scoped_ptr<content::ByteStreamReader> stream, | 161 const FilePath& default_download_directory, |
| 153 scoped_ptr<DownloadRequestHandleInterface> request_handle, | 162 const GURL& url, |
| 154 scoped_refptr<content::DownloadManager> download_manager, | 163 const GURL& referrer_url, |
| 164 int64 received_bytes, |
| 155 bool calculate_hash, | 165 bool calculate_hash, |
| 156 scoped_ptr<content::PowerSaveBlocker> power_save_blocker, | 166 scoped_ptr<ByteStreamReader> stream, |
| 157 const net::BoundNetLog& bound_net_log, | 167 const net::BoundNetLog& bound_net_log, |
| 158 DownloadFileWithDelayFactory* owner) | 168 scoped_ptr<PowerSaveBlocker> power_save_blocker, |
| 159 : DownloadFileImpl(info.Pass(), stream.Pass(), request_handle.Pass(), | 169 base::WeakPtr<DownloadDestinationObserver> observer, |
| 160 download_manager, calculate_hash, | 170 base::WeakPtr<DownloadFileWithDelayFactory> owner) |
| 161 power_save_blocker.Pass(), bound_net_log), | 171 : DownloadFileImpl( |
| 172 save_info.Pass(), default_download_directory, url, referrer_url, |
| 173 received_bytes, calculate_hash, stream.Pass(), bound_net_log, |
| 174 power_save_blocker.Pass(), observer), |
| 162 owner_(owner) {} | 175 owner_(owner) {} |
| 163 | 176 |
| 164 DownloadFileWithDelay::~DownloadFileWithDelay() {} | 177 DownloadFileWithDelay::~DownloadFileWithDelay() {} |
| 165 | 178 |
| 166 void DownloadFileWithDelay::Rename(const FilePath& full_path, | 179 void DownloadFileWithDelay::Rename(const FilePath& full_path, |
| 167 bool overwrite_existing_file, | 180 bool overwrite_existing_file, |
| 168 const RenameCompletionCallback& callback) { | 181 const RenameCompletionCallback& callback) { |
| 169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 170 DownloadFileImpl::Rename( | 183 DownloadFileImpl::Rename( |
| 171 full_path, overwrite_existing_file, | 184 full_path, overwrite_existing_file, |
| 172 base::Bind(DownloadFileWithDelay::RenameCallbackWrapper, | 185 base::Bind(DownloadFileWithDelay::RenameCallbackWrapper, |
| 173 base::Unretained(owner_), callback)); | 186 owner_, callback)); |
| 174 } | 187 } |
| 175 | 188 |
| 176 void DownloadFileWithDelay::Detach(base::Closure callback) { | 189 void DownloadFileWithDelay::Detach(base::Closure callback) { |
| 177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 178 DownloadFileImpl::Detach( | 191 DownloadFileImpl::Detach( |
| 179 base::Bind(DownloadFileWithDelay::DetachCallbackWrapper, | 192 base::Bind(DownloadFileWithDelay::DetachCallbackWrapper, |
| 180 base::Unretained(owner_), callback)); | 193 owner_, callback)); |
| 181 } | 194 } |
| 182 | 195 |
| 183 // static | 196 // static |
| 184 void DownloadFileWithDelay::RenameCallbackWrapper( | 197 void DownloadFileWithDelay::RenameCallbackWrapper( |
| 185 DownloadFileWithDelayFactory* factory, | 198 DownloadFileWithDelayFactory* factory, |
| 186 const RenameCompletionCallback& original_callback, | 199 const RenameCompletionCallback& original_callback, |
| 187 content::DownloadInterruptReason reason, | 200 DownloadInterruptReason reason, |
| 188 const FilePath& path) { | 201 const FilePath& path) { |
| 189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 190 factory->AddRenameCallback(base::Bind(original_callback, reason, path)); | 203 factory->AddRenameCallback(base::Bind(original_callback, reason, path)); |
| 191 } | 204 } |
| 192 | 205 |
| 193 // static | 206 // static |
| 194 void DownloadFileWithDelay::DetachCallbackWrapper( | 207 void DownloadFileWithDelay::DetachCallbackWrapper( |
| 195 DownloadFileWithDelayFactory* factory, | 208 DownloadFileWithDelayFactory* factory, |
| 196 const base::Closure& original_callback) { | 209 const base::Closure& original_callback) { |
| 197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 198 factory->AddDetachCallback(original_callback); | 211 factory->AddDetachCallback(original_callback); |
| 199 } | 212 } |
| 200 | 213 |
| 201 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory() | 214 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory() |
| 202 : waiting_(false) {} | 215 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 216 waiting_(false) {} |
| 203 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {} | 217 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {} |
| 204 | 218 |
| 205 DownloadFile* DownloadFileWithDelayFactory::CreateFile( | 219 DownloadFile* DownloadFileWithDelayFactory::CreateFile( |
| 206 scoped_ptr<DownloadCreateInfo> info, | 220 scoped_ptr<DownloadSaveInfo> save_info, |
| 207 scoped_ptr<content::ByteStreamReader> stream, | 221 const FilePath& default_download_directory, |
| 208 DownloadManager* download_manager, | 222 const GURL& url, |
| 223 const GURL& referrer_url, |
| 224 int64 received_bytes, |
| 209 bool calculate_hash, | 225 bool calculate_hash, |
| 210 const net::BoundNetLog& bound_net_log) { | 226 scoped_ptr<ByteStreamReader> stream, |
| 211 // Ownership will be taken by DownloadFileWithDelay. | 227 const net::BoundNetLog& bound_net_log, |
| 212 scoped_ptr<DownloadRequestHandleInterface> request_handle( | 228 base::WeakPtr<DownloadDestinationObserver> observer) { |
| 213 new DownloadRequestHandle(info->request_handle)); | 229 scoped_ptr<PowerSaveBlocker> psb( |
| 214 | 230 new PowerSaveBlocker( |
| 231 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, |
| 232 "Download in progress")); |
| 215 return new DownloadFileWithDelay( | 233 return new DownloadFileWithDelay( |
| 216 info.Pass(), stream.Pass(), request_handle.Pass(), download_manager, | 234 save_info.Pass(), default_download_directory, url, referrer_url, |
| 217 calculate_hash, | 235 received_bytes, calculate_hash, stream.Pass(), bound_net_log, |
| 218 scoped_ptr<content::PowerSaveBlocker>( | 236 psb.Pass(), observer, weak_ptr_factory_.GetWeakPtr()); |
| 219 new content::PowerSaveBlocker( | |
| 220 content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, | |
| 221 "Download in progress")).Pass(), | |
| 222 bound_net_log, this); | |
| 223 } | 237 } |
| 224 | 238 |
| 225 void DownloadFileWithDelayFactory::AddRenameCallback(base::Closure callback) { | 239 void DownloadFileWithDelayFactory::AddRenameCallback(base::Closure callback) { |
| 226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 227 rename_callbacks_.push_back(callback); | 241 rename_callbacks_.push_back(callback); |
| 228 if (waiting_) | 242 if (waiting_) |
| 229 MessageLoopForUI::current()->Quit(); | 243 MessageLoopForUI::current()->Quit(); |
| 230 } | 244 } |
| 231 | 245 |
| 232 void DownloadFileWithDelayFactory::AddDetachCallback(base::Closure callback) { | 246 void DownloadFileWithDelayFactory::AddDetachCallback(base::Closure callback) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 258 } | 272 } |
| 259 } | 273 } |
| 260 | 274 |
| 261 bool WasPersisted(DownloadItem* item) { | 275 bool WasPersisted(DownloadItem* item) { |
| 262 return item->IsPersisted(); | 276 return item->IsPersisted(); |
| 263 } | 277 } |
| 264 | 278 |
| 265 class CountingDownloadFile : public DownloadFileImpl { | 279 class CountingDownloadFile : public DownloadFileImpl { |
| 266 public: | 280 public: |
| 267 CountingDownloadFile( | 281 CountingDownloadFile( |
| 268 scoped_ptr<DownloadCreateInfo> info, | 282 scoped_ptr<content::DownloadSaveInfo> save_info, |
| 269 scoped_ptr<content::ByteStreamReader> stream, | 283 const FilePath& default_downloads_directory, |
| 270 scoped_ptr<DownloadRequestHandleInterface> request_handle, | 284 const GURL& url, |
| 271 scoped_refptr<content::DownloadManager> download_manager, | 285 const GURL& referrer_url, |
| 272 bool calculate_hash, | 286 int64 received_bytes, |
| 273 scoped_ptr<content::PowerSaveBlocker> power_save_blocker, | 287 bool calculate_hash, |
| 274 const net::BoundNetLog& bound_net_log) | 288 scoped_ptr<content::ByteStreamReader> stream, |
| 275 : DownloadFileImpl(info.Pass(), stream.Pass(), request_handle.Pass(), | 289 const net::BoundNetLog& bound_net_log, |
| 276 download_manager, calculate_hash, | 290 scoped_ptr<content::PowerSaveBlocker> power_save_blocker, |
| 277 power_save_blocker.Pass(), bound_net_log) {} | 291 base::WeakPtr<content::DownloadDestinationObserver> observer) |
| 292 : DownloadFileImpl(save_info.Pass(), default_downloads_directory, |
| 293 url, referrer_url, received_bytes, calculate_hash, |
| 294 stream.Pass(), bound_net_log, |
| 295 power_save_blocker.Pass(), observer) {} |
| 278 | 296 |
| 279 virtual ~CountingDownloadFile() { | 297 virtual ~CountingDownloadFile() { |
| 280 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 298 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 281 active_files_--; | 299 active_files_--; |
| 282 } | 300 } |
| 283 | 301 |
| 284 virtual content::DownloadInterruptReason Initialize() OVERRIDE { | 302 virtual void Initialize(const InitializeCallback& callback) OVERRIDE { |
| 285 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 303 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 286 active_files_++; | 304 active_files_++; |
| 287 return DownloadFileImpl::Initialize(); | 305 return DownloadFileImpl::Initialize(callback); |
| 288 } | 306 } |
| 289 | 307 |
| 290 static void GetNumberActiveFiles(int* result) { | 308 static void GetNumberActiveFiles(int* result) { |
| 291 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 309 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 292 *result = active_files_; | 310 *result = active_files_; |
| 293 } | 311 } |
| 294 | 312 |
| 295 // Can be called on any thread, and will block (running message loop) | 313 // Can be called on any thread, and will block (running message loop) |
| 296 // until data is returned. | 314 // until data is returned. |
| 297 static int GetNumberActiveFilesFromFileThread() { | 315 static int GetNumberActiveFilesFromFileThread() { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 310 | 328 |
| 311 int CountingDownloadFile::active_files_ = 0; | 329 int CountingDownloadFile::active_files_ = 0; |
| 312 | 330 |
| 313 class CountingDownloadFileFactory : public DownloadFileFactory { | 331 class CountingDownloadFileFactory : public DownloadFileFactory { |
| 314 public: | 332 public: |
| 315 CountingDownloadFileFactory() {} | 333 CountingDownloadFileFactory() {} |
| 316 virtual ~CountingDownloadFileFactory() {} | 334 virtual ~CountingDownloadFileFactory() {} |
| 317 | 335 |
| 318 // DownloadFileFactory interface. | 336 // DownloadFileFactory interface. |
| 319 virtual content::DownloadFile* CreateFile( | 337 virtual content::DownloadFile* CreateFile( |
| 320 scoped_ptr<DownloadCreateInfo> info, | 338 scoped_ptr<content::DownloadSaveInfo> save_info, |
| 321 scoped_ptr<content::ByteStreamReader> stream, | 339 const FilePath& default_downloads_directory, |
| 322 DownloadManager* download_manager, | 340 const GURL& url, |
| 323 bool calculate_hash, | 341 const GURL& referrer_url, |
| 324 const net::BoundNetLog& bound_net_log) OVERRIDE { | 342 int64 received_bytes, |
| 325 scoped_ptr<DownloadRequestHandleInterface> request_handle( | 343 bool calculate_hash, |
| 326 new DownloadRequestHandle(info->request_handle)); | 344 scoped_ptr<content::ByteStreamReader> stream, |
| 327 | 345 const net::BoundNetLog& bound_net_log, |
| 346 base::WeakPtr<content::DownloadDestinationObserver> observer) OVERRIDE { |
| 347 scoped_ptr<PowerSaveBlocker> psb( |
| 348 new PowerSaveBlocker( |
| 349 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, |
| 350 "Download in progress")); |
| 328 return new CountingDownloadFile( | 351 return new CountingDownloadFile( |
| 329 info.Pass(), stream.Pass(), | 352 save_info.Pass(), default_downloads_directory, url, referrer_url, |
| 330 request_handle.Pass(), | 353 received_bytes, calculate_hash, stream.Pass(), bound_net_log, |
| 331 download_manager, calculate_hash, | 354 psb.Pass(), observer); |
| 332 scoped_ptr<content::PowerSaveBlocker>( | |
| 333 new content::PowerSaveBlocker( | |
| 334 content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, | |
| 335 "Download in progress")).Pass(), | |
| 336 bound_net_log); | |
| 337 } | 355 } |
| 338 }; | 356 }; |
| 339 | 357 |
| 340 } // namespace | 358 } // namespace |
| 341 | 359 |
| 342 class DownloadContentTest : public ContentBrowserTest { | 360 class DownloadContentTest : public ContentBrowserTest { |
| 343 protected: | 361 protected: |
| 344 virtual void SetUpOnMainThread() OVERRIDE { | 362 virtual void SetUpOnMainThread() OVERRIDE { |
| 345 ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir()); | 363 ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir()); |
| 346 | 364 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 371 // Create a DownloadTestObserverInProgress that will wait for the | 389 // Create a DownloadTestObserverInProgress that will wait for the |
| 372 // specified number of downloads to start. | 390 // specified number of downloads to start. |
| 373 DownloadTestObserver* CreateInProgressWaiter( | 391 DownloadTestObserver* CreateInProgressWaiter( |
| 374 Shell* shell, int num_downloads) { | 392 Shell* shell, int num_downloads) { |
| 375 DownloadManager* download_manager = DownloadManagerForShell(shell); | 393 DownloadManager* download_manager = DownloadManagerForShell(shell); |
| 376 return new DownloadTestObserverInProgress(download_manager, num_downloads); | 394 return new DownloadTestObserverInProgress(download_manager, num_downloads); |
| 377 } | 395 } |
| 378 | 396 |
| 379 // Note: Cannot be used with other alternative DownloadFileFactorys | 397 // Note: Cannot be used with other alternative DownloadFileFactorys |
| 380 void SetupEnsureNoPendingDownloads() { | 398 void SetupEnsureNoPendingDownloads() { |
| 381 GetDownloadFileManager()->SetFileFactoryForTesting( | 399 DownloadManagerForShell(shell())->SetDownloadFileFactoryForTesting( |
| 382 scoped_ptr<content::DownloadFileFactory>( | 400 scoped_ptr<content::DownloadFileFactory>( |
| 383 new CountingDownloadFileFactory()).Pass()); | 401 new CountingDownloadFileFactory()).Pass()); |
| 384 } | 402 } |
| 385 | 403 |
| 386 bool EnsureNoPendingDownloads() { | 404 bool EnsureNoPendingDownloads() { |
| 387 bool result = true; | 405 bool result = true; |
| 388 BrowserThread::PostTask( | 406 BrowserThread::PostTask( |
| 389 BrowserThread::IO, FROM_HERE, | 407 BrowserThread::IO, FROM_HERE, |
| 390 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); | 408 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); |
| 391 MessageLoop::current()->Run(); | 409 MessageLoop::current()->Run(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 422 return false; | 440 return false; |
| 423 | 441 |
| 424 // Check the contents. | 442 // Check the contents. |
| 425 EXPECT_EQ(value, file_contents); | 443 EXPECT_EQ(value, file_contents); |
| 426 if (memcmp(file_contents.c_str(), value.c_str(), expected_size) != 0) | 444 if (memcmp(file_contents.c_str(), value.c_str(), expected_size) != 0) |
| 427 return false; | 445 return false; |
| 428 | 446 |
| 429 return true; | 447 return true; |
| 430 } | 448 } |
| 431 | 449 |
| 432 DownloadFileManager* GetDownloadFileManager() { | |
| 433 ResourceDispatcherHostImpl* rdh(ResourceDispatcherHostImpl::Get()); | |
| 434 return rdh->download_file_manager(); | |
| 435 } | |
| 436 | |
| 437 private: | 450 private: |
| 438 static void EnsureNoPendingDownloadJobsOnIO(bool* result) { | 451 static void EnsureNoPendingDownloadJobsOnIO(bool* result) { |
| 439 if (URLRequestSlowDownloadJob::NumberOutstandingRequests()) | 452 if (URLRequestSlowDownloadJob::NumberOutstandingRequests()) |
| 440 *result = false; | 453 *result = false; |
| 441 BrowserThread::PostTask( | 454 BrowserThread::PostTask( |
| 442 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); | 455 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); |
| 443 } | 456 } |
| 444 | 457 |
| 445 // Location of the downloads directory for these tests | 458 // Location of the downloads directory for these tests |
| 446 ScopedTempDir downloads_directory_; | 459 ScopedTempDir downloads_directory_; |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 543 ASSERT_TRUE(file_util::ContentsEqual( | 556 ASSERT_TRUE(file_util::ContentsEqual( |
| 544 file2, GetTestFilePath("download", "download-test.lib"))); | 557 file2, GetTestFilePath("download", "download-test.lib"))); |
| 545 } | 558 } |
| 546 | 559 |
| 547 // Try to cancel just before we release the download file, by delaying final | 560 // Try to cancel just before we release the download file, by delaying final |
| 548 // rename callback. | 561 // rename callback. |
| 549 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelAtFinalRename) { | 562 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelAtFinalRename) { |
| 550 // Setup new factory. | 563 // Setup new factory. |
| 551 DownloadFileWithDelayFactory* file_factory = | 564 DownloadFileWithDelayFactory* file_factory = |
| 552 new DownloadFileWithDelayFactory(); | 565 new DownloadFileWithDelayFactory(); |
| 553 GetDownloadFileManager()->SetFileFactoryForTesting( | 566 DownloadManagerImpl* download_manager(DownloadManagerForShell(shell())); |
| 554 scoped_ptr<content::DownloadFileFactory>(file_factory).Pass()); | 567 download_manager->SetDownloadFileFactoryForTesting( |
| 568 scoped_ptr<DownloadFileFactory>(file_factory).Pass()); |
| 555 | 569 |
| 556 // Create a download | 570 // Create a download |
| 557 FilePath file(FILE_PATH_LITERAL("download-test.lib")); | 571 FilePath file(FILE_PATH_LITERAL("download-test.lib")); |
| 558 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); | 572 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); |
| 559 | 573 |
| 560 // Wait until the first (intermediate file) rename and execute the callback. | 574 // Wait until the first (intermediate file) rename and execute the callback. |
| 561 file_factory->WaitForSomeCallback(); | 575 file_factory->WaitForSomeCallback(); |
| 562 std::vector<base::Closure> callbacks; | 576 std::vector<base::Closure> callbacks; |
| 563 file_factory->GetAllDetachCallbacks(&callbacks); | 577 file_factory->GetAllDetachCallbacks(&callbacks); |
| 564 ASSERT_TRUE(callbacks.empty()); | 578 ASSERT_TRUE(callbacks.empty()); |
| 565 file_factory->GetAllRenameCallbacks(&callbacks); | 579 file_factory->GetAllRenameCallbacks(&callbacks); |
| 566 ASSERT_EQ(1u, callbacks.size()); | 580 ASSERT_EQ(1u, callbacks.size()); |
| 567 callbacks[0].Run(); | 581 callbacks[0].Run(); |
| 568 callbacks.clear(); | 582 callbacks.clear(); |
| 569 | 583 |
| 570 // Wait until the second (final) rename callback is posted. | 584 // Wait until the second (final) rename callback is posted. |
| 571 file_factory->WaitForSomeCallback(); | 585 file_factory->WaitForSomeCallback(); |
| 572 file_factory->GetAllDetachCallbacks(&callbacks); | 586 file_factory->GetAllDetachCallbacks(&callbacks); |
| 573 ASSERT_TRUE(callbacks.empty()); | 587 ASSERT_TRUE(callbacks.empty()); |
| 574 file_factory->GetAllRenameCallbacks(&callbacks); | 588 file_factory->GetAllRenameCallbacks(&callbacks); |
| 575 ASSERT_EQ(1u, callbacks.size()); | 589 ASSERT_EQ(1u, callbacks.size()); |
| 576 | 590 |
| 577 // Cancel it. | 591 // Cancel it. |
| 578 std::vector<DownloadItem*> items; | 592 std::vector<DownloadItem*> items; |
| 579 DownloadManagerForShell(shell())->GetAllDownloads(&items); | 593 download_manager->GetAllDownloads(&items); |
| 580 ASSERT_EQ(1u, items.size()); | 594 ASSERT_EQ(1u, items.size()); |
| 581 items[0]->Cancel(true); | 595 items[0]->Cancel(true); |
| 582 RunAllPendingInMessageLoop(); | 596 RunAllPendingInMessageLoop(); |
| 583 | 597 |
| 584 // Check state. | 598 // Check state. |
| 585 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState()); | 599 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState()); |
| 586 | 600 |
| 587 // Run final rename callback. | 601 // Run final rename callback. |
| 588 callbacks[0].Run(); | 602 callbacks[0].Run(); |
| 589 callbacks.clear(); | 603 callbacks.clear(); |
| 590 | 604 |
| 591 // Check state. | 605 // Check state. |
| 592 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState()); | 606 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState()); |
| 593 } | 607 } |
| 594 | 608 |
| 595 // Try to cancel just after we release the download file, by delaying | 609 // Try to cancel just after we release the download file, by delaying |
| 596 // release. | 610 // release. |
| 597 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelAtRelease) { | 611 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelAtRelease) { |
| 598 // Setup new factory. | 612 // Setup new factory. |
| 599 DownloadFileWithDelayFactory* file_factory = | 613 DownloadFileWithDelayFactory* file_factory = |
| 600 new DownloadFileWithDelayFactory(); | 614 new DownloadFileWithDelayFactory(); |
| 601 GetDownloadFileManager()->SetFileFactoryForTesting( | 615 DownloadManagerImpl* download_manager(DownloadManagerForShell(shell())); |
| 602 scoped_ptr<content::DownloadFileFactory>(file_factory).Pass()); | 616 download_manager->SetDownloadFileFactoryForTesting( |
| 617 scoped_ptr<DownloadFileFactory>(file_factory).Pass()); |
| 603 | 618 |
| 604 // Create a download | 619 // Create a download |
| 605 FilePath file(FILE_PATH_LITERAL("download-test.lib")); | 620 FilePath file(FILE_PATH_LITERAL("download-test.lib")); |
| 606 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); | 621 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); |
| 607 | 622 |
| 608 // Wait until the first (intermediate file) rename and execute the callback. | 623 // Wait until the first (intermediate file) rename and execute the callback. |
| 609 file_factory->WaitForSomeCallback(); | 624 file_factory->WaitForSomeCallback(); |
| 610 std::vector<base::Closure> callbacks; | 625 std::vector<base::Closure> callbacks; |
| 611 file_factory->GetAllDetachCallbacks(&callbacks); | 626 file_factory->GetAllDetachCallbacks(&callbacks); |
| 612 ASSERT_TRUE(callbacks.empty()); | 627 ASSERT_TRUE(callbacks.empty()); |
| 613 file_factory->GetAllRenameCallbacks(&callbacks); | 628 file_factory->GetAllRenameCallbacks(&callbacks); |
| 614 ASSERT_EQ(1u, callbacks.size()); | 629 ASSERT_EQ(1u, callbacks.size()); |
| 615 callbacks[0].Run(); | 630 callbacks[0].Run(); |
| 616 callbacks.clear(); | 631 callbacks.clear(); |
| 617 | 632 |
| 618 // Wait until the second (final) rename callback is posted. | 633 // Wait until the second (final) rename callback is posted. |
| 619 file_factory->WaitForSomeCallback(); | 634 file_factory->WaitForSomeCallback(); |
| 620 file_factory->GetAllDetachCallbacks(&callbacks); | 635 file_factory->GetAllDetachCallbacks(&callbacks); |
| 621 ASSERT_TRUE(callbacks.empty()); | 636 ASSERT_TRUE(callbacks.empty()); |
| 622 file_factory->GetAllRenameCallbacks(&callbacks); | 637 file_factory->GetAllRenameCallbacks(&callbacks); |
| 623 ASSERT_EQ(1u, callbacks.size()); | 638 ASSERT_EQ(1u, callbacks.size()); |
| 624 | 639 |
| 625 // Call it. | 640 // Call it. |
| 626 callbacks[0].Run(); | 641 callbacks[0].Run(); |
| 627 callbacks.clear(); | 642 callbacks.clear(); |
| 628 | 643 |
| 629 // Confirm download isn't complete yet. | 644 // Confirm download still IN_PROGRESS. |
| 630 std::vector<DownloadItem*> items; | 645 std::vector<DownloadItem*> items; |
| 631 DownloadManagerForShell(shell())->GetAllDownloads(&items); | 646 download_manager->GetAllDownloads(&items); |
| 632 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); | 647 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); |
| 633 | 648 |
| 634 // Cancel the download; confirm cancel fails anyway. | 649 // Cancel the download; confirm cancel fails. |
| 635 ASSERT_EQ(1u, items.size()); | 650 ASSERT_EQ(1u, items.size()); |
| 636 items[0]->Cancel(true); | 651 items[0]->Cancel(true); |
| 637 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); | 652 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); |
| 638 RunAllPendingInMessageLoop(); | |
| 639 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); | |
| 640 | 653 |
| 641 // Confirm detach callback and run it. | 654 // Confirm detach callback and run it. |
| 642 file_factory->WaitForSomeCallback(); | 655 file_factory->WaitForSomeCallback(); |
| 643 file_factory->GetAllRenameCallbacks(&callbacks); | 656 file_factory->GetAllRenameCallbacks(&callbacks); |
| 644 ASSERT_TRUE(callbacks.empty()); | 657 ASSERT_TRUE(callbacks.empty()); |
| 645 file_factory->GetAllDetachCallbacks(&callbacks); | 658 file_factory->GetAllDetachCallbacks(&callbacks); |
| 646 ASSERT_EQ(1u, callbacks.size()); | 659 ASSERT_EQ(1u, callbacks.size()); |
| 647 callbacks[0].Run(); | 660 callbacks[0].Run(); |
| 648 callbacks.clear(); | 661 callbacks.clear(); |
| 662 |
| 663 // *Now* the download should be complete. |
| 649 EXPECT_EQ(DownloadItem::COMPLETE, items[0]->GetState()); | 664 EXPECT_EQ(DownloadItem::COMPLETE, items[0]->GetState()); |
| 650 } | 665 } |
| 651 | 666 |
| 652 // Try to shutdown with a download in progress to make sure shutdown path | 667 // Try to shutdown with a download in progress to make sure shutdown path |
| 653 // works properly. | 668 // works properly. |
| 654 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownInProgress) { | 669 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownInProgress) { |
| 655 // Create a download that won't complete. | 670 // Create a download that won't complete. |
| 656 scoped_ptr<DownloadTestObserver> observer(CreateInProgressWaiter(shell(), 1)); | 671 scoped_ptr<DownloadTestObserver> observer(CreateInProgressWaiter(shell(), 1)); |
| 657 NavigateToURL(shell(), GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl)); | 672 NavigateToURL(shell(), GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl)); |
| 658 observer->WaitForFinished(); | 673 observer->WaitForFinished(); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 692 DownloadManagerForShell(shell())->Shutdown(); | 707 DownloadManagerForShell(shell())->Shutdown(); |
| 693 items.clear(); | 708 items.clear(); |
| 694 } | 709 } |
| 695 | 710 |
| 696 // Try to shutdown just after we release the download file, by delaying | 711 // Try to shutdown just after we release the download file, by delaying |
| 697 // release. | 712 // release. |
| 698 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownAtRelease) { | 713 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownAtRelease) { |
| 699 // Setup new factory. | 714 // Setup new factory. |
| 700 DownloadFileWithDelayFactory* file_factory = | 715 DownloadFileWithDelayFactory* file_factory = |
| 701 new DownloadFileWithDelayFactory(); | 716 new DownloadFileWithDelayFactory(); |
| 702 GetDownloadFileManager()->SetFileFactoryForTesting( | 717 DownloadManagerForShell(shell())->SetDownloadFileFactoryForTesting( |
| 703 scoped_ptr<content::DownloadFileFactory>(file_factory).Pass()); | 718 scoped_ptr<content::DownloadFileFactory>(file_factory).Pass()); |
| 704 | 719 |
| 705 // Create a download | 720 // Create a download |
| 706 FilePath file(FILE_PATH_LITERAL("download-test.lib")); | 721 FilePath file(FILE_PATH_LITERAL("download-test.lib")); |
| 707 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); | 722 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); |
| 708 | 723 |
| 709 // Wait until the first (intermediate file) rename and execute the callback. | 724 // Wait until the first (intermediate file) rename and execute the callback. |
| 710 file_factory->WaitForSomeCallback(); | 725 file_factory->WaitForSomeCallback(); |
| 711 std::vector<base::Closure> callbacks; | 726 std::vector<base::Closure> callbacks; |
| 712 file_factory->GetAllDetachCallbacks(&callbacks); | 727 file_factory->GetAllDetachCallbacks(&callbacks); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 749 // Shutdown the download manager. Mostly this is confirming a lack of | 764 // Shutdown the download manager. Mostly this is confirming a lack of |
| 750 // crashes. | 765 // crashes. |
| 751 DownloadManagerForShell(shell())->Shutdown(); | 766 DownloadManagerForShell(shell())->Shutdown(); |
| 752 | 767 |
| 753 // Run the detach callback; shouldn't cause any problems. | 768 // Run the detach callback; shouldn't cause any problems. |
| 754 callbacks[0].Run(); | 769 callbacks[0].Run(); |
| 755 callbacks.clear(); | 770 callbacks.clear(); |
| 756 } | 771 } |
| 757 | 772 |
| 758 } // namespace content | 773 } // namespace content |
| OLD | NEW |