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" |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 scoped_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo()); | 118 scoped_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo()); |
119 save_info->file_path = params->file_path(); | 119 save_info->file_path = params->file_path(); |
120 save_info->suggested_name = params->suggested_name(); | 120 save_info->suggested_name = params->suggested_name(); |
121 save_info->offset = params->offset(); | 121 save_info->offset = params->offset(); |
122 save_info->hash_state = params->hash_state(); | 122 save_info->hash_state = params->hash_state(); |
123 save_info->prompt_for_save_location = params->prompt(); | 123 save_info->prompt_for_save_location = params->prompt(); |
124 save_info->file = params->GetFile(); | 124 save_info->file = params->GetFile(); |
125 | 125 |
126 if (params->render_process_host_id() != -1) { | 126 if (params->render_process_host_id() != -1) { |
127 ResourceDispatcherHost::Get()->BeginDownload( | 127 ResourceDispatcherHost::Get()->BeginDownload( |
128 request.Pass(), params->referrer(), params->content_initiated(), | 128 std::move(request), params->referrer(), params->content_initiated(), |
129 params->resource_context(), params->render_process_host_id(), | 129 params->resource_context(), params->render_process_host_id(), |
130 params->render_view_host_routing_id(), | 130 params->render_view_host_routing_id(), |
131 params->render_frame_host_routing_id(), params->prefer_cache(), | 131 params->render_frame_host_routing_id(), params->prefer_cache(), |
132 params->do_not_prompt_for_login(), save_info.Pass(), download_id, | 132 params->do_not_prompt_for_login(), std::move(save_info), download_id, |
133 params->callback()); | 133 params->callback()); |
134 return nullptr; | 134 return nullptr; |
135 } | 135 } |
136 return scoped_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread>( | 136 return scoped_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread>( |
137 UrlDownloader::BeginDownload( | 137 UrlDownloader::BeginDownload(download_manager, std::move(request), |
138 download_manager, request.Pass(), params->referrer(), false, | 138 params->referrer(), params->prefer_cache(), |
139 params->prefer_cache(), true, save_info.Pass(), download_id, | 139 std::move(save_info), download_id, |
140 params->callback()) | 140 params->callback()) |
141 .release()); | 141 .release()); |
142 } | 142 } |
143 | 143 |
144 class DownloadItemFactoryImpl : public DownloadItemFactory { | 144 class DownloadItemFactoryImpl : public DownloadItemFactory { |
145 public: | 145 public: |
146 DownloadItemFactoryImpl() {} | 146 DownloadItemFactoryImpl() {} |
147 ~DownloadItemFactoryImpl() override {} | 147 ~DownloadItemFactoryImpl() override {} |
148 | 148 |
149 DownloadItemImpl* CreatePersistedItem( | 149 DownloadItemImpl* CreatePersistedItem( |
150 DownloadItemImplDelegate* delegate, | 150 DownloadItemImplDelegate* delegate, |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 scoped_ptr<DownloadCreateInfo> info, | 341 scoped_ptr<DownloadCreateInfo> info, |
342 scoped_ptr<ByteStreamReader> stream, | 342 scoped_ptr<ByteStreamReader> stream, |
343 const DownloadUrlParameters::OnStartedCallback& on_started) { | 343 const DownloadUrlParameters::OnStartedCallback& on_started) { |
344 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 344 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
345 DCHECK(info); | 345 DCHECK(info); |
346 uint32 download_id = info->download_id; | 346 uint32 download_id = info->download_id; |
347 const bool new_download = (download_id == content::DownloadItem::kInvalidId); | 347 const bool new_download = (download_id == content::DownloadItem::kInvalidId); |
348 base::Callback<void(uint32)> got_id(base::Bind( | 348 base::Callback<void(uint32)> got_id(base::Bind( |
349 &DownloadManagerImpl::StartDownloadWithId, | 349 &DownloadManagerImpl::StartDownloadWithId, |
350 weak_factory_.GetWeakPtr(), | 350 weak_factory_.GetWeakPtr(), |
351 base::Passed(info.Pass()), | 351 base::Passed(&info), |
352 base::Passed(stream.Pass()), | 352 base::Passed(&stream), |
353 on_started, | 353 on_started, |
354 new_download)); | 354 new_download)); |
355 if (new_download) { | 355 if (new_download) { |
356 GetNextId(got_id); | 356 GetNextId(got_id); |
357 } else { | 357 } else { |
358 got_id.Run(download_id); | 358 got_id.Run(download_id); |
359 } | 359 } |
360 } | 360 } |
361 | 361 |
362 void DownloadManagerImpl::StartDownloadWithId( | 362 void DownloadManagerImpl::StartDownloadWithId( |
363 scoped_ptr<DownloadCreateInfo> info, | 363 scoped_ptr<DownloadCreateInfo> info, |
364 scoped_ptr<ByteStreamReader> stream, | 364 scoped_ptr<ByteStreamReader> stream, |
365 const DownloadUrlParameters::OnStartedCallback& on_started, | 365 const DownloadUrlParameters::OnStartedCallback& on_started, |
366 bool new_download, | 366 bool new_download, |
367 uint32 id) { | 367 uint32 id) { |
368 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 368 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
369 DCHECK_NE(content::DownloadItem::kInvalidId, id); | 369 DCHECK_NE(content::DownloadItem::kInvalidId, id); |
370 DownloadItemImpl* download = NULL; | 370 DownloadItemImpl* download = NULL; |
371 if (new_download) { | 371 if (new_download) { |
372 download = CreateActiveItem(id, *info); | 372 download = CreateActiveItem(id, *info); |
373 } else { | 373 } else { |
374 DownloadMap::iterator item_iterator = downloads_.find(id); | 374 DownloadMap::iterator item_iterator = downloads_.find(id); |
375 // Trying to resume an interrupted download. | 375 // Trying to resume an interrupted download. |
376 if (item_iterator == downloads_.end() || | 376 if (item_iterator == downloads_.end() || |
377 (item_iterator->second->GetState() == DownloadItem::CANCELLED)) { | 377 (item_iterator->second->GetState() == DownloadItem::CANCELLED)) { |
378 // If the download is no longer known to the DownloadManager, then it was | 378 // If the download is no longer known to the DownloadManager, then it was |
379 // removed after it was resumed. Ignore. If the download is cancelled | 379 // removed after it was resumed. Ignore. If the download is cancelled |
380 // while resuming, then also ignore the request. | 380 // while resuming, then also ignore the request. |
381 info->request_handle.CancelRequest(); | 381 info->request_handle->CancelRequest(); |
382 if (!on_started.is_null()) | 382 if (!on_started.is_null()) |
383 on_started.Run(NULL, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED); | 383 on_started.Run(NULL, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED); |
384 return; | 384 return; |
385 } | 385 } |
386 download = item_iterator->second; | 386 download = item_iterator->second; |
387 DCHECK_EQ(DownloadItem::INTERRUPTED, download->GetState()); | 387 DCHECK_EQ(DownloadItem::INTERRUPTED, download->GetState()); |
388 download->MergeOriginInfoOnResume(*info); | 388 download->MergeOriginInfoOnResume(*info); |
389 } | 389 } |
390 | 390 |
391 base::FilePath default_download_directory; | 391 base::FilePath default_download_directory; |
392 if (delegate_) { | 392 if (delegate_) { |
393 base::FilePath website_save_directory; // Unused | 393 base::FilePath website_save_directory; // Unused |
394 bool skip_dir_check = false; // Unused | 394 bool skip_dir_check = false; // Unused |
395 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, | 395 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, |
396 &default_download_directory, &skip_dir_check); | 396 &default_download_directory, &skip_dir_check); |
397 } | 397 } |
398 | 398 |
399 // Create the download file and start the download. | 399 // Create the download file and start the download. |
400 scoped_ptr<DownloadFile> download_file( | 400 scoped_ptr<DownloadFile> download_file(file_factory_->CreateFile( |
401 file_factory_->CreateFile( | 401 std::move(info->save_info), default_download_directory, info->url(), |
402 info->save_info.Pass(), default_download_directory, | 402 info->referrer_url, delegate_ && delegate_->GenerateFileHash(), |
403 info->url(), info->referrer_url, | 403 std::move(stream), download->GetBoundNetLog(), |
404 delegate_ && delegate_->GenerateFileHash(), | 404 download->DestinationObserverAsWeakPtr())); |
405 stream.Pass(), download->GetBoundNetLog(), | |
406 download->DestinationObserverAsWeakPtr())); | |
407 | 405 |
408 // Attach the client ID identifying the app to the AV system. | 406 // Attach the client ID identifying the app to the AV system. |
409 if (download_file.get() && delegate_) { | 407 if (download_file.get() && delegate_) { |
410 download_file->SetClientGuid( | 408 download_file->SetClientGuid( |
411 delegate_->ApplicationClientIdForFileScanning()); | 409 delegate_->ApplicationClientIdForFileScanning()); |
412 } | 410 } |
413 | 411 |
414 scoped_ptr<DownloadRequestHandleInterface> req_handle( | 412 download->Start(std::move(download_file), std::move(info->request_handle)); |
415 new DownloadRequestHandle(info->request_handle)); | |
416 download->Start(download_file.Pass(), req_handle.Pass()); | |
417 | 413 |
418 // For interrupted downloads, Start() will transition the state to | 414 // For interrupted downloads, Start() will transition the state to |
419 // IN_PROGRESS and consumers will be notified via OnDownloadUpdated(). | 415 // IN_PROGRESS and consumers will be notified via OnDownloadUpdated(). |
420 // For new downloads, we notify here, rather than earlier, so that | 416 // For new downloads, we notify here, rather than earlier, so that |
421 // the download_file is bound to download and all the usual | 417 // the download_file is bound to download and all the usual |
422 // setters (e.g. Cancel) work. | 418 // setters (e.g. Cancel) work. |
423 if (new_download) | 419 if (new_download) |
424 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 420 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
425 | 421 |
426 if (!on_started.is_null()) | 422 if (!on_started.is_null()) |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
751 if (delegate_) | 747 if (delegate_) |
752 delegate_->OpenDownload(download); | 748 delegate_->OpenDownload(download); |
753 } | 749 } |
754 | 750 |
755 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { | 751 void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { |
756 if (delegate_) | 752 if (delegate_) |
757 delegate_->ShowDownloadInShell(download); | 753 delegate_->ShowDownloadInShell(download); |
758 } | 754 } |
759 | 755 |
760 } // namespace content | 756 } // namespace content |
OLD | NEW |