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