OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "headless/lib/browser/headless_download_manager_delegate.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "content/public/browser/browser_context.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/download_manager.h" |
| 12 #include "net/base/filename_util.h" |
| 13 |
| 14 namespace headless { |
| 15 |
| 16 HeadlessDownloadManagerDelegate::HeadlessDownloadManagerDelegate() |
| 17 : download_manager_(nullptr), |
| 18 download_behavior_(DownloadBehavior::ALLOW), |
| 19 weak_ptr_factory_(this) {} |
| 20 |
| 21 HeadlessDownloadManagerDelegate::~HeadlessDownloadManagerDelegate() { |
| 22 if (download_manager_) { |
| 23 DCHECK_EQ(static_cast<content::DownloadManagerDelegate*>(this), |
| 24 download_manager_->GetDelegate()); |
| 25 download_manager_->SetDelegate(nullptr); |
| 26 download_manager_ = nullptr; |
| 27 } |
| 28 } |
| 29 |
| 30 void HeadlessDownloadManagerDelegate::SetDownloadManager( |
| 31 content::DownloadManager* download_manager) { |
| 32 download_manager_ = download_manager; |
| 33 } |
| 34 |
| 35 void HeadlessDownloadManagerDelegate::Shutdown() { |
| 36 // Revoke any pending callbacks. download_manager_ et. al. are no longer safe |
| 37 // to access after this point. |
| 38 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 39 download_manager_ = nullptr; |
| 40 } |
| 41 |
| 42 bool HeadlessDownloadManagerDelegate::DetermineDownloadTarget( |
| 43 content::DownloadItem* download, |
| 44 const content::DownloadTargetCallback& callback) { |
| 45 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 46 |
| 47 if (download_behavior_ == DownloadBehavior::DENY) { |
| 48 base::FilePath empty_path = base::FilePath(); |
| 49 callback.Run(empty_path, |
| 50 content::DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 51 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, empty_path, |
| 52 content::DOWNLOAD_INTERRUPT_REASON_NONE); |
| 53 return true; |
| 54 } |
| 55 |
| 56 // This assignment needs to be here because even at the call to |
| 57 // SetDownloadManager, the system is not fully initialized. |
| 58 if (default_download_path_.empty()) { |
| 59 default_download_path_ = |
| 60 download_manager_->GetBrowserContext()->GetPath().Append( |
| 61 FILE_PATH_LITERAL("Downloads")); |
| 62 } |
| 63 |
| 64 if (!download->GetForcedFilePath().empty()) { |
| 65 callback.Run(download->GetForcedFilePath(), |
| 66 content::DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 67 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 68 download->GetForcedFilePath(), |
| 69 content::DOWNLOAD_INTERRUPT_REASON_NONE); |
| 70 return true; |
| 71 } |
| 72 |
| 73 FilenameDeterminedCallback filename_determined_callback = |
| 74 base::Bind(&HeadlessDownloadManagerDelegate::OnDownloadPathGenerated, |
| 75 weak_ptr_factory_.GetWeakPtr(), download->GetId(), callback); |
| 76 |
| 77 content::BrowserThread::PostTask( |
| 78 content::BrowserThread::FILE, FROM_HERE, |
| 79 base::Bind(&HeadlessDownloadManagerDelegate::GenerateFilename, |
| 80 download->GetURL(), download->GetContentDisposition(), |
| 81 download->GetSuggestedFilename(), download->GetMimeType(), |
| 82 default_download_path_, filename_determined_callback)); |
| 83 return true; |
| 84 } |
| 85 |
| 86 bool HeadlessDownloadManagerDelegate::ShouldOpenDownload( |
| 87 content::DownloadItem* item, |
| 88 const content::DownloadOpenDelayedCallback& callback) { |
| 89 return true; |
| 90 } |
| 91 |
| 92 void HeadlessDownloadManagerDelegate::GetNextId( |
| 93 const content::DownloadIdCallback& callback) { |
| 94 static uint32_t next_id = content::DownloadItem::kInvalidId + 1; |
| 95 callback.Run(next_id++); |
| 96 } |
| 97 |
| 98 // static |
| 99 void HeadlessDownloadManagerDelegate::GenerateFilename( |
| 100 const GURL& url, |
| 101 const std::string& content_disposition, |
| 102 const std::string& suggested_filename, |
| 103 const std::string& mime_type, |
| 104 const base::FilePath& suggested_directory, |
| 105 const FilenameDeterminedCallback& callback) { |
| 106 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); |
| 107 base::FilePath generated_name = |
| 108 net::GenerateFileName(url, content_disposition, std::string(), |
| 109 suggested_filename, mime_type, "download"); |
| 110 |
| 111 if (!base::PathExists(suggested_directory)) |
| 112 base::CreateDirectory(suggested_directory); |
| 113 |
| 114 base::FilePath suggested_path(suggested_directory.Append(generated_name)); |
| 115 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 116 base::Bind(callback, suggested_path)); |
| 117 } |
| 118 |
| 119 void HeadlessDownloadManagerDelegate::OnDownloadPathGenerated( |
| 120 uint32_t download_id, |
| 121 const content::DownloadTargetCallback& callback, |
| 122 const base::FilePath& suggested_path) { |
| 123 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 124 |
| 125 callback.Run(suggested_path, |
| 126 content::DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 127 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 128 suggested_path.AddExtension(FILE_PATH_LITERAL(".crdownload")), |
| 129 content::DOWNLOAD_INTERRUPT_REASON_NONE); |
| 130 } |
| 131 |
| 132 void HeadlessDownloadManagerDelegate::SetDownloadBehavior( |
| 133 DownloadBehavior behavior) { |
| 134 download_behavior_ = behavior; |
| 135 } |
| 136 |
| 137 void HeadlessDownloadManagerDelegate::SetDownloadDirectory( |
| 138 const base::FilePath& path) { |
| 139 default_download_path_ = path; |
| 140 } |
| 141 |
| 142 void HeadlessDownloadManagerDelegate::SetDownloadBehaviorForTesting( |
| 143 const base::FilePath& default_download_path) { |
| 144 default_download_path_ = default_download_path; |
| 145 } |
| 146 |
| 147 } // namespace headless |
OLD | NEW |