| 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 #include "content/browser/download/download_manager_impl.h" | 5 #include "content/browser/download/download_manager_impl.h" |
| 6 | 6 |
| 7 #include <iterator> | 7 #include <iterator> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/debug/alias.h" | 11 #include "base/debug/alias.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/i18n/case_conversion.h" | 13 #include "base/i18n/case_conversion.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
| 16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 17 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
| 18 #include "base/supports_user_data.h" | 18 #include "base/supports_user_data.h" |
| 19 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
| 20 #include "base/sys_string_conversions.h" | 20 #include "base/sys_string_conversions.h" |
| 21 #include "build/build_config.h" | 21 #include "build/build_config.h" |
| 22 #include "content/browser/download/byte_stream.h" | 22 #include "content/browser/download/byte_stream.h" |
| 23 #include "content/browser/download/download_create_info.h" | 23 #include "content/browser/download/download_create_info.h" |
| 24 #include "content/browser/download/download_file_manager.h" | 24 #include "content/browser/download/download_file_factory.h" |
| 25 #include "content/browser/download/download_item_factory.h" |
| 25 #include "content/browser/download/download_item_impl.h" | 26 #include "content/browser/download/download_item_impl.h" |
| 26 #include "content/browser/download/download_stats.h" | 27 #include "content/browser/download/download_stats.h" |
| 27 #include "content/browser/renderer_host/render_view_host_impl.h" | 28 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 28 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" | 29 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" |
| 29 #include "content/browser/web_contents/web_contents_impl.h" | 30 #include "content/browser/web_contents/web_contents_impl.h" |
| 30 #include "content/public/browser/browser_context.h" | 31 #include "content/public/browser/browser_context.h" |
| 31 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
| 32 #include "content/public/browser/content_browser_client.h" | 33 #include "content/public/browser/content_browser_client.h" |
| 33 #include "content/public/browser/download_interrupt_reasons.h" | 34 #include "content/public/browser/download_interrupt_reasons.h" |
| 34 #include "content/public/browser/download_manager_delegate.h" | 35 #include "content/public/browser/download_manager_delegate.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 45 | 46 |
| 46 using content::BrowserThread; | 47 using content::BrowserThread; |
| 47 using content::DownloadId; | 48 using content::DownloadId; |
| 48 using content::DownloadItem; | 49 using content::DownloadItem; |
| 49 using content::DownloadPersistentStoreInfo; | 50 using content::DownloadPersistentStoreInfo; |
| 50 using content::ResourceDispatcherHostImpl; | 51 using content::ResourceDispatcherHostImpl; |
| 51 using content::WebContents; | 52 using content::WebContents; |
| 52 | 53 |
| 53 namespace { | 54 namespace { |
| 54 | 55 |
| 55 // This is just used to remember which DownloadItems come from SavePage. | |
| 56 class SavePageData : public base::SupportsUserData::Data { | |
| 57 public: | |
| 58 // A spoonful of syntactic sugar. | |
| 59 static bool Get(DownloadItem* item) { | |
| 60 return item->GetUserData(kKey) != NULL; | |
| 61 } | |
| 62 | |
| 63 explicit SavePageData(DownloadItem* item) { | |
| 64 item->SetUserData(kKey, this); | |
| 65 } | |
| 66 | |
| 67 virtual ~SavePageData() {} | |
| 68 | |
| 69 private: | |
| 70 static const char kKey[]; | |
| 71 | |
| 72 DISALLOW_COPY_AND_ASSIGN(SavePageData); | |
| 73 }; | |
| 74 | |
| 75 const char SavePageData::kKey[] = "DownloadItem SavePageData"; | |
| 76 | |
| 77 void BeginDownload(content::DownloadUrlParameters* params) { | 56 void BeginDownload(content::DownloadUrlParameters* params) { |
| 78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 79 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and | 58 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and |
| 80 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so | 59 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so |
| 81 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4. | 60 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4. |
| 82 scoped_ptr<net::URLRequest> request(new net::URLRequest( | 61 scoped_ptr<net::URLRequest> request(new net::URLRequest( |
| 83 params->url(), | 62 params->url(), |
| 84 NULL, | 63 NULL, |
| 85 params->resource_context()->GetRequestContext())); | 64 params->resource_context()->GetRequestContext())); |
| 86 request->set_referrer(params->referrer().url.spec()); | 65 request->set_referrer(params->referrer().url.spec()); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 | 116 |
| 138 bool operator!=(const MapValueIteratorAdapter& that) const { | 117 bool operator!=(const MapValueIteratorAdapter& that) const { |
| 139 return iter_ != that.iter_; | 118 return iter_ != that.iter_; |
| 140 } | 119 } |
| 141 | 120 |
| 142 private: | 121 private: |
| 143 base::hash_map<int64, DownloadItem*>::const_iterator iter_; | 122 base::hash_map<int64, DownloadItem*>::const_iterator iter_; |
| 144 // Allow copy and assign. | 123 // Allow copy and assign. |
| 145 }; | 124 }; |
| 146 | 125 |
| 147 void EnsureNoPendingDownloadsOnFile(scoped_refptr<DownloadFileManager> dfm, | 126 void EnsureNoPendingDownloadJobsOnFile(bool* result) { |
| 148 bool* result) { | 127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 149 if (dfm->NumberOfActiveDownloads()) | 128 *result = (content::DownloadFile::GetNumberOfDownloadFiles() == 0); |
| 150 *result = false; | |
| 151 BrowserThread::PostTask( | 129 BrowserThread::PostTask( |
| 152 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); | 130 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); |
| 153 } | 131 } |
| 154 | 132 |
| 155 void EnsureNoPendingDownloadJobsOnIO(bool* result) { | |
| 156 scoped_refptr<DownloadFileManager> download_file_manager = | |
| 157 ResourceDispatcherHostImpl::Get()->download_file_manager(); | |
| 158 BrowserThread::PostTask( | |
| 159 BrowserThread::FILE, FROM_HERE, | |
| 160 base::Bind(&EnsureNoPendingDownloadsOnFile, | |
| 161 download_file_manager, result)); | |
| 162 } | |
| 163 | |
| 164 class DownloadItemFactoryImpl : public content::DownloadItemFactory { | 133 class DownloadItemFactoryImpl : public content::DownloadItemFactory { |
| 165 public: | 134 public: |
| 166 DownloadItemFactoryImpl() {} | 135 DownloadItemFactoryImpl() {} |
| 167 virtual ~DownloadItemFactoryImpl() {} | 136 virtual ~DownloadItemFactoryImpl() {} |
| 168 | 137 |
| 169 virtual DownloadItemImpl* CreatePersistedItem( | 138 virtual DownloadItemImpl* CreatePersistedItem( |
| 170 DownloadItemImplDelegate* delegate, | 139 DownloadItemImplDelegate* delegate, |
| 171 content::DownloadId download_id, | 140 content::DownloadId download_id, |
| 172 const content::DownloadPersistentStoreInfo& info, | 141 const content::DownloadPersistentStoreInfo& info, |
| 173 const net::BoundNetLog& bound_net_log) OVERRIDE { | 142 const net::BoundNetLog& bound_net_log) OVERRIDE { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 196 }; | 165 }; |
| 197 | 166 |
| 198 } // namespace | 167 } // namespace |
| 199 | 168 |
| 200 namespace content { | 169 namespace content { |
| 201 | 170 |
| 202 bool DownloadManager::EnsureNoPendingDownloadsForTesting() { | 171 bool DownloadManager::EnsureNoPendingDownloadsForTesting() { |
| 203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 204 bool result = true; | 173 bool result = true; |
| 205 BrowserThread::PostTask( | 174 BrowserThread::PostTask( |
| 206 BrowserThread::IO, FROM_HERE, | 175 BrowserThread::FILE, FROM_HERE, |
| 207 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); | 176 base::Bind(&EnsureNoPendingDownloadJobsOnFile, &result)); |
| 208 MessageLoop::current()->Run(); | 177 MessageLoop::current()->Run(); |
| 209 return result; | 178 return result; |
| 210 } | 179 } |
| 211 | 180 |
| 212 } // namespace content | 181 } // namespace content |
| 213 | 182 |
| 214 DownloadManagerImpl::DownloadManagerImpl( | 183 DownloadManagerImpl::DownloadManagerImpl( |
| 215 DownloadFileManager* file_manager, | 184 scoped_ptr<content::DownloadItemFactory> item_factory, |
| 216 scoped_ptr<content::DownloadItemFactory> factory, | 185 scoped_ptr<content::DownloadFileFactory> file_factory, |
| 217 net::NetLog* net_log) | 186 net::NetLog* net_log) |
| 218 : factory_(factory.Pass()), | 187 : item_factory_(item_factory.Pass()), |
| 188 file_factory_(file_factory.Pass()), |
| 219 history_size_(0), | 189 history_size_(0), |
| 220 shutdown_needed_(false), | 190 shutdown_needed_(false), |
| 221 browser_context_(NULL), | 191 browser_context_(NULL), |
| 222 file_manager_(file_manager), | |
| 223 delegate_(NULL), | 192 delegate_(NULL), |
| 224 net_log_(net_log) { | 193 net_log_(net_log) { |
| 225 DCHECK(file_manager); | 194 if (!item_factory_.get()) |
| 226 if (!factory_.get()) | 195 item_factory_.reset(new DownloadItemFactoryImpl()); |
| 227 factory_.reset(new DownloadItemFactoryImpl()); | 196 if (!file_factory_.get()) |
| 197 file_factory_.reset(new content::DownloadFileFactory()); |
| 228 } | 198 } |
| 229 | 199 |
| 230 DownloadManagerImpl::~DownloadManagerImpl() { | 200 DownloadManagerImpl::~DownloadManagerImpl() { |
| 231 DCHECK(!shutdown_needed_); | 201 DCHECK(!shutdown_needed_); |
| 232 } | 202 } |
| 233 | 203 |
| 234 DownloadId DownloadManagerImpl::GetNextId() { | 204 DownloadId DownloadManagerImpl::GetNextId() { |
| 235 DownloadId id; | 205 DownloadId id; |
| 236 if (delegate_) | 206 if (delegate_) |
| 237 id = delegate_->GetNextId(); | 207 id = delegate_->GetNextId(); |
| 238 if (!id.IsValid()) { | 208 if (!id.IsValid()) { |
| 239 static int next_id; | 209 static int next_id; |
| 240 id = DownloadId(browser_context_, ++next_id); | 210 id = DownloadId(browser_context_, ++next_id); |
| 241 } | 211 } |
| 242 | 212 |
| 243 return id; | 213 return id; |
| 244 } | 214 } |
| 245 | 215 |
| 246 DownloadFileManager* DownloadManagerImpl::GetDownloadFileManager() { | 216 void DownloadManagerImpl::DelegateStart(DownloadItemImpl* item) { |
| 247 return file_manager_; | 217 content::DownloadTargetCallback callback = |
| 218 base::Bind(&DownloadManagerImpl::OnDownloadTargetDetermined, |
| 219 this, item->GetId()); |
| 220 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) { |
| 221 FilePath target_path = item->GetForcedFilePath(); |
| 222 // TODO(asanka): Determine a useful path if |target_path| is empty. |
| 223 callback.Run(target_path, |
| 224 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 225 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 226 target_path); |
| 227 } |
| 248 } | 228 } |
| 249 | 229 |
| 250 bool DownloadManagerImpl::ShouldOpenDownload(DownloadItemImpl* item) { | 230 bool DownloadManagerImpl::ShouldOpenDownload(DownloadItemImpl* item) { |
| 251 if (!delegate_) | 231 if (!delegate_) |
| 252 return true; | 232 return true; |
| 253 | 233 |
| 254 return delegate_->ShouldOpenDownload(item); | 234 return delegate_->ShouldOpenDownload(item); |
| 255 } | 235 } |
| 256 | 236 |
| 257 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) { | 237 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 273 void DownloadManagerImpl::Shutdown() { | 253 void DownloadManagerImpl::Shutdown() { |
| 274 VLOG(20) << __FUNCTION__ << "()" | 254 VLOG(20) << __FUNCTION__ << "()" |
| 275 << " shutdown_needed_ = " << shutdown_needed_; | 255 << " shutdown_needed_ = " << shutdown_needed_; |
| 276 if (!shutdown_needed_) | 256 if (!shutdown_needed_) |
| 277 return; | 257 return; |
| 278 shutdown_needed_ = false; | 258 shutdown_needed_ = false; |
| 279 | 259 |
| 280 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); | 260 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); |
| 281 // TODO(benjhayden): Consider clearing observers_. | 261 // TODO(benjhayden): Consider clearing observers_. |
| 282 | 262 |
| 283 DCHECK(file_manager_); | 263 // The DownloadFiles will be canceled and deleted by their DownloadItems. |
| 284 BrowserThread::PostTask( | |
| 285 BrowserThread::FILE, FROM_HERE, | |
| 286 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown, | |
| 287 file_manager_, make_scoped_refptr(this))); | |
| 288 | 264 |
| 289 AssertContainersConsistent(); | 265 AssertContainersConsistent(); |
| 290 | 266 |
| 291 // Go through all downloads in downloads_. Dangerous ones we need to | 267 // Go through all downloads in downloads_. Dangerous ones we need to |
| 292 // remove on disk, and in progress ones we need to cancel. | 268 // remove on disk, and in progress ones we need to cancel. |
| 293 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) { | 269 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) { |
| 294 DownloadItemImpl* download = it->second; | 270 DownloadItemImpl* download = it->second; |
| 295 | 271 |
| 296 // Save iterator from potential erases in this set done by called code. | 272 // Save iterator from potential erases in this set done by called code. |
| 297 // Iterators after an erasure point are still valid for lists and | 273 // Iterators after an erasure point are still valid for lists and |
| (...skipping 22 matching lines...) Expand all Loading... |
| 320 // and all in progress downloads have been cancelled. We can now delete | 296 // and all in progress downloads have been cancelled. We can now delete |
| 321 // anything left. | 297 // anything left. |
| 322 | 298 |
| 323 active_downloads_.clear(); | 299 active_downloads_.clear(); |
| 324 STLDeleteValues(&downloads_); | 300 STLDeleteValues(&downloads_); |
| 325 downloads_.clear(); | 301 downloads_.clear(); |
| 326 | 302 |
| 327 // We'll have nothing more to report to the observers after this point. | 303 // We'll have nothing more to report to the observers after this point. |
| 328 observers_.Clear(); | 304 observers_.Clear(); |
| 329 | 305 |
| 330 file_manager_ = NULL; | |
| 331 if (delegate_) | 306 if (delegate_) |
| 332 delegate_->Shutdown(); | 307 delegate_->Shutdown(); |
| 333 delegate_ = NULL; | 308 delegate_ = NULL; |
| 334 } | 309 } |
| 335 | 310 |
| 336 void DownloadManagerImpl::GetTemporaryDownloads( | 311 void DownloadManagerImpl::GetTemporaryDownloads( |
| 337 const FilePath& dir_path, DownloadVector* result) { | 312 const FilePath& dir_path, DownloadVector* result) { |
| 338 DCHECK(result); | 313 DCHECK(result); |
| 339 | 314 |
| 340 for (DownloadMap::iterator it = downloads_.begin(); | 315 for (DownloadMap::iterator it = downloads_.begin(); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { | 359 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { |
| 385 DCHECK(browser_context); | 360 DCHECK(browser_context); |
| 386 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; | 361 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
| 387 shutdown_needed_ = true; | 362 shutdown_needed_ = true; |
| 388 | 363 |
| 389 browser_context_ = browser_context; | 364 browser_context_ = browser_context; |
| 390 | 365 |
| 391 return true; | 366 return true; |
| 392 } | 367 } |
| 393 | 368 |
| 394 // We have received a message from DownloadFileManager about a new download. | |
| 395 content::DownloadId DownloadManagerImpl::StartDownload( | 369 content::DownloadId DownloadManagerImpl::StartDownload( |
| 396 scoped_ptr<DownloadCreateInfo> info, | 370 scoped_ptr<DownloadCreateInfo> info, |
| 397 scoped_ptr<content::ByteStreamReader> stream) { | 371 scoped_ptr<content::ByteStreamReader> stream) { |
| 398 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 372 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 399 | 373 |
| 400 // |bound_net_log| will be used for logging both the download item's and | 374 net::BoundNetLog bound_net_log = |
| 401 // the download file's events. | 375 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
| 402 net::BoundNetLog bound_net_log = CreateDownloadItem(info.get()); | |
| 403 | 376 |
| 404 // If info->download_id was unknown on entry to this function, it was | 377 // We create the DownloadItem before the DownloadFile because the |
| 405 // assigned in CreateDownloadItem. | 378 // DownloadItem already needs to handle a state in which there is |
| 406 DownloadId download_id = info->download_id; | 379 // no associated DownloadFile (history downloads, !IN_PROGRESS downloads) |
| 380 DownloadItemImpl* download = |
| 381 CreateDownloadItem(info.get(), bound_net_log); |
| 382 scoped_ptr<content::DownloadFile> download_file( |
| 383 file_factory_->CreateFile( |
| 384 info->save_info, info->url(), info->referrer_url, |
| 385 info->received_bytes, GenerateFileHash(), |
| 386 stream.Pass(), bound_net_log, |
| 387 download->DestinationObserverAsWeakPtr())); |
| 388 download->Start(download_file.Pass()); |
| 407 | 389 |
| 408 DownloadFileManager::CreateDownloadFileCallback callback( | 390 return download->GetGlobalId(); |
| 409 base::Bind(&DownloadManagerImpl::OnDownloadFileCreated, | |
| 410 this, download_id.local())); | |
| 411 | |
| 412 BrowserThread::PostTask( | |
| 413 BrowserThread::FILE, FROM_HERE, | |
| 414 base::Bind(&DownloadFileManager::CreateDownloadFile, | |
| 415 file_manager_, base::Passed(info.Pass()), | |
| 416 base::Passed(stream.Pass()), | |
| 417 make_scoped_refptr(this), | |
| 418 GenerateFileHash(), bound_net_log, | |
| 419 callback)); | |
| 420 | |
| 421 return download_id; | |
| 422 } | |
| 423 | |
| 424 void DownloadManagerImpl::OnDownloadFileCreated( | |
| 425 int32 download_id, content::DownloadInterruptReason reason) { | |
| 426 if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) { | |
| 427 OnDownloadInterrupted(download_id, reason); | |
| 428 // TODO(rdsmith): It makes no sense to continue along the | |
| 429 // regular download path after we've gotten an error. But it's | |
| 430 // the way the code has historically worked, and this allows us | |
| 431 // to get the download persisted and observers of the download manager | |
| 432 // notified, so tests work. When we execute all side effects of cancel | |
| 433 // (including queue removal) immedately rather than waiting for | |
| 434 // persistence we should replace this comment with a "return;". | |
| 435 } | |
| 436 | |
| 437 DownloadMap::iterator download_iter = active_downloads_.find(download_id); | |
| 438 if (download_iter == active_downloads_.end()) | |
| 439 return; | |
| 440 | |
| 441 DownloadItemImpl* download = download_iter->second; | |
| 442 content::DownloadTargetCallback callback = | |
| 443 base::Bind(&DownloadManagerImpl::OnDownloadTargetDetermined, | |
| 444 this, download_id); | |
| 445 if (!delegate_ || !delegate_->DetermineDownloadTarget(download, callback)) { | |
| 446 FilePath target_path = download->GetForcedFilePath(); | |
| 447 // TODO(asanka): Determine a useful path if |target_path| is empty. | |
| 448 callback.Run(target_path, | |
| 449 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | |
| 450 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
| 451 target_path); | |
| 452 } | |
| 453 } | 391 } |
| 454 | 392 |
| 455 void DownloadManagerImpl::OnDownloadTargetDetermined( | 393 void DownloadManagerImpl::OnDownloadTargetDetermined( |
| 456 int32 download_id, | 394 int32 download_id, |
| 457 const FilePath& target_path, | 395 const FilePath& target_path, |
| 458 DownloadItem::TargetDisposition disposition, | 396 DownloadItem::TargetDisposition disposition, |
| 459 content::DownloadDangerType danger_type, | 397 content::DownloadDangerType danger_type, |
| 460 const FilePath& intermediate_path) { | 398 const FilePath& intermediate_path) { |
| 461 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 399 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 462 DownloadMap::iterator download_iter = active_downloads_.find(download_id); | 400 DownloadMap::iterator download_iter = active_downloads_.find(download_id); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { | 444 void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { |
| 507 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 445 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 508 if (ContainsKey(downloads_, download_id)) | 446 if (ContainsKey(downloads_, download_id)) |
| 509 downloads_[download_id]->OnDownloadedFileRemoved(); | 447 downloads_[download_id]->OnDownloadedFileRemoved(); |
| 510 } | 448 } |
| 511 | 449 |
| 512 content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const { | 450 content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const { |
| 513 return browser_context_; | 451 return browser_context_; |
| 514 } | 452 } |
| 515 | 453 |
| 516 net::BoundNetLog DownloadManagerImpl::CreateDownloadItem( | 454 DownloadItemImpl* DownloadManagerImpl::CreateDownloadItem( |
| 517 DownloadCreateInfo* info) { | 455 DownloadCreateInfo* info, const net::BoundNetLog& bound_net_log) { |
| 518 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 456 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 519 | 457 |
| 520 net::BoundNetLog bound_net_log = | |
| 521 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | |
| 522 if (!info->download_id.IsValid()) | 458 if (!info->download_id.IsValid()) |
| 523 info->download_id = GetNextId(); | 459 info->download_id = GetNextId(); |
| 524 DownloadItemImpl* download = factory_->CreateActiveItem( | 460 DownloadItemImpl* download = item_factory_->CreateActiveItem( |
| 525 this, *info, | 461 this, *info, |
| 526 scoped_ptr<DownloadRequestHandleInterface>( | 462 scoped_ptr<DownloadRequestHandleInterface>( |
| 527 new DownloadRequestHandle(info->request_handle)).Pass(), | 463 new DownloadRequestHandle(info->request_handle)).Pass(), |
| 528 bound_net_log); | 464 bound_net_log); |
| 529 | 465 |
| 530 DCHECK(!ContainsKey(downloads_, download->GetId())); | 466 DCHECK(!ContainsKey(downloads_, download->GetId())); |
| 531 downloads_[download->GetId()] = download; | 467 downloads_[download->GetId()] = download; |
| 532 DCHECK(!ContainsKey(active_downloads_, download->GetId())); | 468 DCHECK(!ContainsKey(active_downloads_, download->GetId())); |
| 533 active_downloads_[download->GetId()] = download; | 469 active_downloads_[download->GetId()] = download; |
| 534 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 470 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
| 535 | 471 |
| 536 return bound_net_log; | 472 return download; |
| 537 } | 473 } |
| 538 | 474 |
| 539 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( | 475 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( |
| 540 const FilePath& main_file_path, | 476 const FilePath& main_file_path, |
| 541 const GURL& page_url, | 477 const GURL& page_url, |
| 542 const std::string& mime_type, | 478 const std::string& mime_type, |
| 543 DownloadItem::Observer* observer) { | 479 DownloadItem::Observer* observer) { |
| 544 net::BoundNetLog bound_net_log = | 480 net::BoundNetLog bound_net_log = |
| 545 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 481 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
| 546 DownloadItemImpl* download = factory_->CreateSavePageItem( | 482 DownloadItemImpl* download = item_factory_->CreateSavePageItem( |
| 547 this, | 483 this, |
| 548 main_file_path, | 484 main_file_path, |
| 549 page_url, | 485 page_url, |
| 550 GetNextId(), | 486 GetNextId(), |
| 551 mime_type, | 487 mime_type, |
| 552 bound_net_log); | 488 bound_net_log); |
| 553 | 489 |
| 554 download->AddObserver(observer); | 490 download->AddObserver(observer); |
| 555 | 491 |
| 556 DCHECK(!ContainsKey(downloads_, download->GetId())); | 492 DCHECK(!ContainsKey(downloads_, download->GetId())); |
| 557 downloads_[download->GetId()] = download; | 493 downloads_[download->GetId()] = download; |
| 558 DCHECK(!SavePageData::Get(download)); | |
| 559 new SavePageData(download); | |
| 560 DCHECK(SavePageData::Get(download)); | |
| 561 | 494 |
| 562 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 495 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
| 563 | 496 |
| 564 // Will notify the observer in the callback. | 497 // Will notify the observer in the callback. |
| 565 if (delegate_) | 498 if (delegate_) |
| 566 delegate_->AddItemToPersistentStore(download); | 499 delegate_->AddItemToPersistentStore(download); |
| 567 | 500 |
| 568 return download; | 501 return download; |
| 569 } | 502 } |
| 570 | 503 |
| 571 void DownloadManagerImpl::UpdateDownload(int32 download_id, | |
| 572 int64 bytes_so_far, | |
| 573 int64 bytes_per_sec, | |
| 574 const std::string& hash_state) { | |
| 575 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 576 DownloadMap::iterator it = active_downloads_.find(download_id); | |
| 577 if (it != active_downloads_.end()) { | |
| 578 DownloadItemImpl* download = it->second; | |
| 579 if (download->IsInProgress()) { | |
| 580 download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state); | |
| 581 if (delegate_) | |
| 582 delegate_->UpdateItemInPersistentStore(download); | |
| 583 } | |
| 584 } | |
| 585 } | |
| 586 | |
| 587 void DownloadManagerImpl::OnResponseCompleted(int32 download_id, | |
| 588 int64 size, | |
| 589 const std::string& hash) { | |
| 590 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 591 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id | |
| 592 << " size = " << size; | |
| 593 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 594 | |
| 595 // If it's not in active_downloads_, that means it was cancelled; just | |
| 596 // ignore the notification. | |
| 597 if (active_downloads_.count(download_id) == 0) | |
| 598 return; | |
| 599 | |
| 600 DownloadItemImpl* download = active_downloads_[download_id]; | |
| 601 download->OnAllDataSaved(size, hash); | |
| 602 MaybeCompleteDownload(download); | |
| 603 } | |
| 604 | |
| 605 void DownloadManagerImpl::AssertStateConsistent( | 504 void DownloadManagerImpl::AssertStateConsistent( |
| 606 DownloadItemImpl* download) const { | 505 DownloadItemImpl* download) const { |
| 607 CHECK(ContainsKey(downloads_, download->GetId())); | 506 CHECK(ContainsKey(downloads_, download->GetId())); |
| 608 | 507 |
| 609 int64 state = download->GetState(); | 508 int64 state = download->GetState(); |
| 610 base::debug::Alias(&state); | 509 base::debug::Alias(&state); |
| 611 if (ContainsKey(active_downloads_, download->GetId())) { | 510 if (ContainsKey(active_downloads_, download->GetId())) { |
| 612 if (download->IsPersisted()) | 511 if (download->IsPersisted()) |
| 613 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 512 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
| 614 if (DownloadItem::IN_PROGRESS != download->GetState()) | 513 if (DownloadItem::IN_PROGRESS != download->GetState()) |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 716 void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) { | 615 void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) { |
| 717 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 616 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 718 | 617 |
| 719 VLOG(20) << __FUNCTION__ << "()" | 618 VLOG(20) << __FUNCTION__ << "()" |
| 720 << " download = " << download->DebugString(true); | 619 << " download = " << download->DebugString(true); |
| 721 | 620 |
| 722 RemoveFromActiveList(download); | 621 RemoveFromActiveList(download); |
| 723 // This function is called from the DownloadItem, so DI state | 622 // This function is called from the DownloadItem, so DI state |
| 724 // should already have been updated. | 623 // should already have been updated. |
| 725 AssertStateConsistent(download); | 624 AssertStateConsistent(download); |
| 726 | |
| 727 DCHECK(file_manager_); | |
| 728 download->OffThreadCancel(); | |
| 729 } | |
| 730 | |
| 731 void DownloadManagerImpl::OnDownloadInterrupted( | |
| 732 int32 download_id, | |
| 733 content::DownloadInterruptReason reason) { | |
| 734 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 735 | |
| 736 if (!ContainsKey(active_downloads_, download_id)) | |
| 737 return; | |
| 738 active_downloads_[download_id]->Interrupt(reason); | |
| 739 } | 625 } |
| 740 | 626 |
| 741 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { | 627 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { |
| 742 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 628 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 743 DCHECK(download); | 629 DCHECK(download); |
| 744 | 630 |
| 745 // Clean up will happen when the history system create callback runs if we | 631 // Clean up will happen when the history system create callback runs if we |
| 746 // don't have a valid db_handle yet. | 632 // don't have a valid db_handle yet. |
| 747 if (download->IsPersisted()) { | 633 if (download->IsPersisted()) { |
| 748 active_downloads_.erase(download->GetId()); | 634 active_downloads_.erase(download->GetId()); |
| 749 if (delegate_) | 635 if (delegate_) |
| 750 delegate_->UpdateItemInPersistentStore(download); | 636 delegate_->UpdateItemInPersistentStore(download); |
| 751 } | 637 } |
| 752 } | 638 } |
| 753 | 639 |
| 754 bool DownloadManagerImpl::GenerateFileHash() { | 640 bool DownloadManagerImpl::GenerateFileHash() { |
| 755 return delegate_ && delegate_->GenerateFileHash(); | 641 return delegate_ && delegate_->GenerateFileHash(); |
| 756 } | 642 } |
| 757 | 643 |
| 644 void DownloadManagerImpl::SetDownloadFileFactoryForTesting( |
| 645 scoped_ptr<content::DownloadFileFactory> file_factory) { |
| 646 file_factory_ = file_factory.Pass(); |
| 647 } |
| 648 |
| 649 content::DownloadFileFactory* |
| 650 DownloadManagerImpl::GetDownloadFileFactoryForTesting() { |
| 651 return file_factory_.get(); |
| 652 } |
| 653 |
| 758 int DownloadManagerImpl::RemoveDownloadItems( | 654 int DownloadManagerImpl::RemoveDownloadItems( |
| 759 const DownloadItemImplVector& pending_deletes) { | 655 const DownloadItemImplVector& pending_deletes) { |
| 760 if (pending_deletes.empty()) | 656 if (pending_deletes.empty()) |
| 761 return 0; | 657 return 0; |
| 762 | 658 |
| 763 // Delete from internal maps. | 659 // Delete from internal maps. |
| 764 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); | 660 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); |
| 765 it != pending_deletes.end(); | 661 it != pending_deletes.end(); |
| 766 ++it) { | 662 ++it) { |
| 767 DownloadItemImpl* download = *it; | 663 DownloadItemImpl* download = *it; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 855 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). | 751 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). |
| 856 void DownloadManagerImpl::OnPersistentStoreQueryComplete( | 752 void DownloadManagerImpl::OnPersistentStoreQueryComplete( |
| 857 std::vector<DownloadPersistentStoreInfo>* entries) { | 753 std::vector<DownloadPersistentStoreInfo>* entries) { |
| 858 history_size_ = entries->size(); | 754 history_size_ = entries->size(); |
| 859 for (size_t i = 0; i < entries->size(); ++i) { | 755 for (size_t i = 0; i < entries->size(); ++i) { |
| 860 int64 db_handle = entries->at(i).db_handle; | 756 int64 db_handle = entries->at(i).db_handle; |
| 861 base::debug::Alias(&db_handle); | 757 base::debug::Alias(&db_handle); |
| 862 | 758 |
| 863 net::BoundNetLog bound_net_log = | 759 net::BoundNetLog bound_net_log = |
| 864 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 760 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
| 865 DownloadItemImpl* download = factory_->CreatePersistedItem( | 761 DownloadItemImpl* download = item_factory_->CreatePersistedItem( |
| 866 this, GetNextId(), entries->at(i), bound_net_log); | 762 this, GetNextId(), entries->at(i), bound_net_log); |
| 867 DCHECK(!ContainsKey(downloads_, download->GetId())); | 763 DCHECK(!ContainsKey(downloads_, download->GetId())); |
| 868 downloads_[download->GetId()] = download; | 764 downloads_[download->GetId()] = download; |
| 869 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 765 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
| 870 VLOG(20) << __FUNCTION__ << "()" << i << ">" | 766 VLOG(20) << __FUNCTION__ << "()" << i << ">" |
| 871 << " download = " << download->DebugString(true); | 767 << " download = " << download->DebugString(true); |
| 872 } | 768 } |
| 873 NotifyModelChanged(); | 769 NotifyModelChanged(); |
| 874 CheckForHistoryFilesRemoval(); | 770 CheckForHistoryFilesRemoval(); |
| 875 } | 771 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 896 | 792 |
| 897 | 793 |
| 898 void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id, | 794 void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id, |
| 899 int64 db_handle) { | 795 int64 db_handle) { |
| 900 // It's valid that we don't find a matching item, i.e. on shutdown. | 796 // It's valid that we don't find a matching item, i.e. on shutdown. |
| 901 if (!ContainsKey(downloads_, download_id)) | 797 if (!ContainsKey(downloads_, download_id)) |
| 902 return; | 798 return; |
| 903 | 799 |
| 904 DownloadItemImpl* item = downloads_[download_id]; | 800 DownloadItemImpl* item = downloads_[download_id]; |
| 905 AddDownloadItemToHistory(item, db_handle); | 801 AddDownloadItemToHistory(item, db_handle); |
| 906 if (SavePageData::Get(item)) { | 802 if (item->IsSavePackageDownload()) { |
| 907 OnSavePageItemAddedToPersistentStore(item); | 803 OnSavePageItemAddedToPersistentStore(item); |
| 908 } else { | 804 } else { |
| 909 OnDownloadItemAddedToPersistentStore(item); | 805 OnDownloadItemAddedToPersistentStore(item); |
| 910 } | 806 } |
| 911 } | 807 } |
| 912 | 808 |
| 913 // Once the new DownloadItem has been committed to the persistent store, | 809 // Once the new DownloadItem has been committed to the persistent store, |
| 914 // associate it with its db_handle (TODO(benjhayden) merge db_handle with id), | 810 // associate it with its db_handle (TODO(benjhayden) merge db_handle with id), |
| 915 // show it in the browser (TODO(benjhayden) the ui should observe us instead), | 811 // show it in the browser (TODO(benjhayden) the ui should observe us instead), |
| 916 // and notify observers (TODO(benjhayden) observers should be able to see the | 812 // and notify observers (TODO(benjhayden) observers should be able to see the |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1043 // Finalize this download if it finished before the history callback. | 939 // Finalize this download if it finished before the history callback. |
| 1044 if (!item->IsInProgress()) | 940 if (!item->IsInProgress()) |
| 1045 SavePageDownloadFinished(item); | 941 SavePageDownloadFinished(item); |
| 1046 } | 942 } |
| 1047 | 943 |
| 1048 void DownloadManagerImpl::SavePageDownloadFinished( | 944 void DownloadManagerImpl::SavePageDownloadFinished( |
| 1049 content::DownloadItem* download) { | 945 content::DownloadItem* download) { |
| 1050 if (download->IsPersisted()) { | 946 if (download->IsPersisted()) { |
| 1051 if (delegate_) | 947 if (delegate_) |
| 1052 delegate_->UpdateItemInPersistentStore(download); | 948 delegate_->UpdateItemInPersistentStore(download); |
| 1053 if (download->IsComplete()) | |
| 1054 content::NotificationService::current()->Notify( | |
| 1055 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | |
| 1056 content::Source<DownloadManager>(this), | |
| 1057 content::Details<DownloadItem>(download)); | |
| 1058 } | 949 } |
| 1059 } | 950 } |
| 1060 | 951 |
| 1061 void DownloadManagerImpl::DownloadOpened(DownloadItemImpl* download) { | 952 void DownloadManagerImpl::DownloadOpened(DownloadItemImpl* download) { |
| 1062 if (delegate_) | 953 if (delegate_) |
| 1063 delegate_->UpdateItemInPersistentStore(download); | 954 delegate_->UpdateItemInPersistentStore(download); |
| 1064 int num_unopened = 0; | 955 int num_unopened = 0; |
| 1065 for (DownloadMap::iterator it = downloads_.begin(); | 956 for (DownloadMap::iterator it = downloads_.begin(); |
| 1066 it != downloads_.end(); ++it) { | 957 it != downloads_.end(); ++it) { |
| 1067 DownloadItemImpl* item = it->second; | 958 DownloadItemImpl* item = it->second; |
| 1068 if (item->IsComplete() && | 959 if (item->IsComplete() && |
| 1069 !item->GetOpened()) | 960 !item->GetOpened()) |
| 1070 ++num_unopened; | 961 ++num_unopened; |
| 1071 } | 962 } |
| 1072 download_stats::RecordOpensOutstanding(num_unopened); | 963 download_stats::RecordOpensOutstanding(num_unopened); |
| 1073 } | 964 } |
| 1074 | 965 |
| 1075 void DownloadManagerImpl::DownloadRenamedToIntermediateName( | 966 void DownloadManagerImpl::DownloadRenamedToIntermediateName( |
| 1076 DownloadItemImpl* download) { | 967 DownloadItemImpl* download) { |
| 1077 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 968 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1078 // download->GetFullPath() is only expected to be meaningful after this | 969 // download->GetFullPath() is only expected to be meaningful after this |
| 1079 // callback is received. Therefore we can now add the download to a persistent | 970 // callback is received. Therefore we can now add the download to a persistent |
| 1080 // store. If the rename failed, we receive an OnDownloadInterrupted() call | 971 // store. If the rename failed, we processed an interrupt |
| 1081 // before we receive the DownloadRenamedToIntermediateName() call. | 972 // before we receive the DownloadRenamedToIntermediateName() call. |
| 1082 if (delegate_) { | 973 if (delegate_) { |
| 1083 delegate_->AddItemToPersistentStore(download); | 974 delegate_->AddItemToPersistentStore(download); |
| 1084 } else { | 975 } else { |
| 1085 OnItemAddedToPersistentStore(download->GetId(), | 976 OnItemAddedToPersistentStore(download->GetId(), |
| 1086 DownloadItem::kUninitializedHandle); | 977 DownloadItem::kUninitializedHandle); |
| 1087 } | 978 } |
| 1088 } | 979 } |
| 1089 | 980 |
| 1090 void DownloadManagerImpl::DownloadRenamedToFinalName( | 981 void DownloadManagerImpl::DownloadRenamedToFinalName( |
| 1091 DownloadItemImpl* download) { | 982 DownloadItemImpl* download) { |
| 1092 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 983 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1093 // If the rename failed, we receive an OnDownloadInterrupted() call before we | 984 // If the rename failed, we processed an interrupt before we get here. |
| 1094 // receive the DownloadRenamedToFinalName() call. | |
| 1095 if (delegate_) { | 985 if (delegate_) { |
| 1096 delegate_->UpdatePathForItemInPersistentStore( | 986 delegate_->UpdatePathForItemInPersistentStore( |
| 1097 download, download->GetFullPath()); | 987 download, download->GetFullPath()); |
| 1098 } | 988 } |
| 1099 } | 989 } |
| OLD | NEW |