Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: headless/lib/browser/headless_download_manager_delegate.cc

Issue 2886693002: initial version of the headless download manager delegate
Patch Set: initial version of the headless download manager delegate Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_(NULL),
Sami 2017/05/17 13:20:40 nullptr (below too)
18 download_behavior_(DownloadBehavior::DENY),
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(NULL);
26 download_manager_ = NULL;
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_ = NULL;
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 false;
90 ;
Sami 2017/05/17 13:20:40 nit: extra ;
91 }
92
93 void HeadlessDownloadManagerDelegate::GetNextId(
94 const content::DownloadIdCallback& callback) {
95 static uint32_t next_id = content::DownloadItem::kInvalidId + 1;
96 callback.Run(next_id++);
97 }
98
99 // static
100 void HeadlessDownloadManagerDelegate::GenerateFilename(
101 const GURL& url,
102 const std::string& content_disposition,
103 const std::string& suggested_filename,
104 const std::string& mime_type,
105 const base::FilePath& suggested_directory,
106 const FilenameDeterminedCallback& callback) {
107 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
108 base::FilePath generated_name =
109 net::GenerateFileName(url, content_disposition, std::string(),
110 suggested_filename, mime_type, "download");
111
112 if (!base::PathExists(suggested_directory))
113 base::CreateDirectory(suggested_directory);
114
115 base::FilePath suggested_path(suggested_directory.Append(generated_name));
116 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
117 base::Bind(callback, suggested_path));
118 }
119
120 void HeadlessDownloadManagerDelegate::OnDownloadPathGenerated(
121 uint32_t download_id,
122 const content::DownloadTargetCallback& callback,
123 const base::FilePath& suggested_path) {
124 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
125
126 // Testing exit.
Sami 2017/05/17 13:20:40 ?
127 callback.Run(suggested_path,
128 content::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
129 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
130 suggested_path.AddExtension(FILE_PATH_LITERAL(".crdownload")),
131 content::DOWNLOAD_INTERRUPT_REASON_NONE);
132 return;
133 }
134
135 void HeadlessDownloadManagerDelegate::SetDownloadBehaviorForTesting(
136 const base::FilePath& default_download_path) {
137 default_download_path_ = default_download_path;
138 }
139
140 void HeadlessDownloadManagerDelegate::SetDownloadBehavior(
141 const std::string& behavior) {
142 if (behavior == "allow") {
143 download_behavior_ = DownloadBehavior::ALLOW;
144 } else if (behavior == "deny") {
145 download_behavior_ = DownloadBehavior::DENY;
146 }
147 }
148
149 void HeadlessDownloadManagerDelegate::SetDownloadDirectory(
150 const std::string& path) {
151 default_download_path_ = base::FilePath(path);
152 }
153
154 } // namespace headless
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698