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

Side by Side Diff: content/shell/shell_download_manager_delegate.cc

Issue 10867060: Move some download tests to content from chrome. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Incorporated comments. Created 8 years, 3 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 | Annotate | Revision Log
OLDNEW
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/shell/shell_download_manager_delegate.h" 5 #include "content/shell/shell_download_manager_delegate.h"
6 6
7 #if defined(TOOLKIT_GTK) 7 #if defined(TOOLKIT_GTK)
8 #include <gtk/gtk.h> 8 #include <gtk/gtk.h>
9 #endif 9 #endif
10 10
(...skipping 10 matching lines...) Expand all
21 #include "content/public/browser/browser_context.h" 21 #include "content/public/browser/browser_context.h"
22 #include "content/public/browser/browser_thread.h" 22 #include "content/public/browser/browser_thread.h"
23 #include "content/public/browser/download_manager.h" 23 #include "content/public/browser/download_manager.h"
24 #include "content/public/browser/web_contents.h" 24 #include "content/public/browser/web_contents.h"
25 #include "content/public/browser/web_contents_view.h" 25 #include "content/public/browser/web_contents_view.h"
26 #include "net/base/net_util.h" 26 #include "net/base/net_util.h"
27 27
28 namespace content { 28 namespace content {
29 29
30 ShellDownloadManagerDelegate::ShellDownloadManagerDelegate() 30 ShellDownloadManagerDelegate::ShellDownloadManagerDelegate()
31 : download_manager_(NULL) { 31 : download_manager_(NULL),
32 suppress_prompting_(false),
33 last_download_db_handle_(DownloadItem::kUninitializedHandle) {
32 // Balanced in Shutdown(); 34 // Balanced in Shutdown();
33 AddRef(); 35 AddRef();
34 } 36 }
35 37
36 ShellDownloadManagerDelegate::~ShellDownloadManagerDelegate(){ 38 ShellDownloadManagerDelegate::~ShellDownloadManagerDelegate(){
37 } 39 }
38 40
39 41
40 void ShellDownloadManagerDelegate::SetDownloadManager( 42 void ShellDownloadManagerDelegate::SetDownloadManager(
41 DownloadManager* download_manager) { 43 DownloadManager* download_manager) {
42 download_manager_ = download_manager; 44 download_manager_ = download_manager;
43 } 45 }
44 46
45 void ShellDownloadManagerDelegate::Shutdown() { 47 void ShellDownloadManagerDelegate::Shutdown() {
46 Release(); 48 Release();
47 } 49 }
48 50
49 bool ShellDownloadManagerDelegate::DetermineDownloadTarget( 51 bool ShellDownloadManagerDelegate::DetermineDownloadTarget(
50 DownloadItem* download, 52 DownloadItem* download,
51 const DownloadTargetCallback& callback) { 53 const DownloadTargetCallback& callback) {
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
55 // This assignment needs to be here because even at the call to
56 // SetDownloadManager, the system is not fully initialized.
57 if (default_download_path_.empty()) {
58 default_download_path_ = download_manager_->GetBrowserContext()->GetPath().
59 Append(FILE_PATH_LITERAL("Downloads"));
60 }
61
52 if (!download->GetForcedFilePath().empty()) { 62 if (!download->GetForcedFilePath().empty()) {
53 callback.Run(download->GetForcedFilePath(), 63 callback.Run(download->GetForcedFilePath(),
54 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 64 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
55 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 65 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
56 download->GetForcedFilePath()); 66 download->GetForcedFilePath());
57 return true; 67 return true;
58 } 68 }
59 69
60 FilePath generated_name = net::GenerateFileName( 70 FilePath generated_name = net::GenerateFileName(
61 download->GetURL(), 71 download->GetURL(),
62 download->GetContentDisposition(), 72 download->GetContentDisposition(),
63 download->GetReferrerCharset(), 73 download->GetReferrerCharset(),
64 download->GetSuggestedFilename(), 74 download->GetSuggestedFilename(),
65 download->GetMimeType(), 75 download->GetMimeType(),
66 "download"); 76 "download");
67 77
68 BrowserThread::PostTask( 78 BrowserThread::PostTask(
69 BrowserThread::FILE, 79 BrowserThread::FILE,
70 FROM_HERE, 80 FROM_HERE,
71 base::Bind( 81 base::Bind(
72 &ShellDownloadManagerDelegate::GenerateFilename, 82 &ShellDownloadManagerDelegate::GenerateFilename,
73 this, download->GetId(), callback, generated_name)); 83 this, download->GetId(), callback, generated_name,
74 return false; 84 default_download_path_));
85 return true;
75 } 86 }
76 87
77 void ShellDownloadManagerDelegate::GenerateFilename( 88 void ShellDownloadManagerDelegate::GenerateFilename(
78 int32 download_id, 89 int32 download_id,
79 const DownloadTargetCallback& callback, 90 const DownloadTargetCallback& callback,
80 const FilePath& generated_name) { 91 const FilePath& generated_name,
81 FilePath suggested_path = download_manager_->GetBrowserContext()->GetPath(). 92 const FilePath& suggested_directory) {
82 Append(FILE_PATH_LITERAL("Downloads")); 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
83 if (!file_util::PathExists(suggested_path)) 94 if (!file_util::PathExists(suggested_directory))
84 file_util::CreateDirectory(suggested_path); 95 file_util::CreateDirectory(suggested_directory);
85 96
86 suggested_path = suggested_path.Append(generated_name); 97 FilePath suggested_path(suggested_directory.Append(generated_name));
87 BrowserThread::PostTask( 98 BrowserThread::PostTask(
88 BrowserThread::UI, 99 BrowserThread::UI,
89 FROM_HERE, 100 FROM_HERE,
90 base::Bind( 101 base::Bind(
91 &ShellDownloadManagerDelegate::ChooseDownloadPath, 102 &ShellDownloadManagerDelegate::OnDownloadPathGenerated,
92 this, download_id, callback, suggested_path)); 103 this, download_id, callback, suggested_path));
93 } 104 }
94 105
106 void ShellDownloadManagerDelegate::OnDownloadPathGenerated(
107 int32 download_id,
108 const DownloadTargetCallback& callback,
109 const FilePath& suggested_path) {
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
111 if (suppress_prompting_) {
112 // Testing exit.
113 callback.Run(suggested_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
114 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, suggested_path);
115 return;
116 }
117
118 ChooseDownloadPath(download_id, callback, suggested_path);
119 }
120
95 void ShellDownloadManagerDelegate::ChooseDownloadPath( 121 void ShellDownloadManagerDelegate::ChooseDownloadPath(
96 int32 download_id, 122 int32 download_id,
97 const DownloadTargetCallback& callback, 123 const DownloadTargetCallback& callback,
98 const FilePath& suggested_path) { 124 const FilePath& suggested_path) {
125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
99 DownloadItem* item = 126 DownloadItem* item =
100 download_manager_->GetActiveDownloadItem(download_id); 127 download_manager_->GetActiveDownloadItem(download_id);
101 if (!item) 128 if (!item)
102 return; 129 return;
103 130
104 FilePath result; 131 FilePath result;
105 #if defined(OS_WIN) && !defined(USE_AURA) 132 #if defined(OS_WIN) && !defined(USE_AURA)
106 std::wstring file_part = FilePath(suggested_path).BaseName().value(); 133 std::wstring file_part = FilePath(suggested_path).BaseName().value();
107 wchar_t file_name[MAX_PATH]; 134 wchar_t file_name[MAX_PATH];
108 base::wcslcpy(file_name, file_part.c_str(), arraysize(file_name)); 135 base::wcslcpy(file_name, file_part.c_str(), arraysize(file_name));
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 } 174 }
148 gtk_widget_destroy(dialog); 175 gtk_widget_destroy(dialog);
149 #else 176 #else
150 NOTIMPLEMENTED(); 177 NOTIMPLEMENTED();
151 #endif 178 #endif
152 179
153 callback.Run(result, DownloadItem::TARGET_DISPOSITION_PROMPT, 180 callback.Run(result, DownloadItem::TARGET_DISPOSITION_PROMPT,
154 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, result); 181 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, result);
155 } 182 }
156 183
184 void ShellDownloadManagerDelegate::AddItemToPersistentStore(
185 DownloadItem* item) {
186 download_manager_->OnItemAddedToPersistentStore(
187 item->GetId(), --last_download_db_handle_);
188 }
189
190 void ShellDownloadManagerDelegate::SetDownloadBehaviorForTesting(
191 const FilePath& default_download_path) {
192 default_download_path_ = default_download_path;
193 suppress_prompting_ = true;
194 }
195
157 } // namespace content 196 } // namespace content
OLDNEW
« no previous file with comments | « content/shell/shell_download_manager_delegate.h ('k') | content/test/data/download/download-test.lib » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698