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/shell/shell_download_manager_delegate.h" | 5 #include "content/shell/shell_download_manager_delegate.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windows.h> | 8 #include <windows.h> |
9 #include <commdlg.h> | 9 #include <commdlg.h> |
10 #endif | 10 #endif |
(...skipping 17 matching lines...) Expand all Loading... |
28 | 28 |
29 ShellDownloadManagerDelegate::~ShellDownloadManagerDelegate(){ | 29 ShellDownloadManagerDelegate::~ShellDownloadManagerDelegate(){ |
30 } | 30 } |
31 | 31 |
32 | 32 |
33 void ShellDownloadManagerDelegate::SetDownloadManager( | 33 void ShellDownloadManagerDelegate::SetDownloadManager( |
34 DownloadManager* download_manager) { | 34 DownloadManager* download_manager) { |
35 download_manager_ = download_manager; | 35 download_manager_ = download_manager; |
36 } | 36 } |
37 | 37 |
38 bool ShellDownloadManagerDelegate::ShouldStartDownload(int32 download_id) { | 38 bool ShellDownloadManagerDelegate::DetermineDownloadTarget( |
39 DownloadItem* download = | 39 DownloadItem* download, |
40 download_manager_->GetActiveDownloadItem(download_id); | 40 const DownloadTargetCallback& callback) { |
41 | |
42 if (!download->GetForcedFilePath().empty()) { | 41 if (!download->GetForcedFilePath().empty()) { |
43 download->OnTargetPathDetermined( | 42 callback.Run(download->GetForcedFilePath(), |
44 download->GetForcedFilePath(), | 43 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
45 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 44 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
46 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 45 download->GetForcedFilePath()); |
47 return true; | 46 return true; |
48 } | 47 } |
49 | 48 |
50 FilePath generated_name = net::GenerateFileName( | 49 FilePath generated_name = net::GenerateFileName( |
51 download->GetURL(), | 50 download->GetURL(), |
52 download->GetContentDisposition(), | 51 download->GetContentDisposition(), |
53 download->GetReferrerCharset(), | 52 download->GetReferrerCharset(), |
54 download->GetSuggestedFilename(), | 53 download->GetSuggestedFilename(), |
55 download->GetMimeType(), | 54 download->GetMimeType(), |
56 "download"); | 55 "download"); |
57 | 56 |
58 BrowserThread::PostTask( | 57 BrowserThread::PostTask( |
59 BrowserThread::FILE, | 58 BrowserThread::FILE, |
60 FROM_HERE, | 59 FROM_HERE, |
61 base::Bind( | 60 base::Bind( |
62 &ShellDownloadManagerDelegate::GenerateFilename, | 61 &ShellDownloadManagerDelegate::GenerateFilename, |
63 this, download_id, generated_name)); | 62 this, download->GetId(), callback, generated_name)); |
64 return false; | 63 return false; |
65 } | 64 } |
66 | 65 |
67 void ShellDownloadManagerDelegate::GenerateFilename( | 66 void ShellDownloadManagerDelegate::GenerateFilename( |
68 int32 download_id, | 67 int32 download_id, |
| 68 const DownloadTargetCallback& callback, |
69 const FilePath& generated_name) { | 69 const FilePath& generated_name) { |
70 FilePath suggested_path = download_manager_->GetBrowserContext()->GetPath(). | 70 FilePath suggested_path = download_manager_->GetBrowserContext()->GetPath(). |
71 Append(FILE_PATH_LITERAL("Downloads")); | 71 Append(FILE_PATH_LITERAL("Downloads")); |
72 if (!file_util::PathExists(suggested_path)) | 72 if (!file_util::PathExists(suggested_path)) |
73 file_util::CreateDirectory(suggested_path); | 73 file_util::CreateDirectory(suggested_path); |
74 | 74 |
75 suggested_path = suggested_path.Append(generated_name); | 75 suggested_path = suggested_path.Append(generated_name); |
76 BrowserThread::PostTask( | 76 BrowserThread::PostTask( |
77 BrowserThread::UI, | 77 BrowserThread::UI, |
78 FROM_HERE, | 78 FROM_HERE, |
79 base::Bind( | 79 base::Bind( |
80 &ShellDownloadManagerDelegate::RestartDownload, | 80 &ShellDownloadManagerDelegate::ChooseDownloadPath, |
81 this, download_id, suggested_path)); | 81 this, download_id, callback, suggested_path)); |
82 } | 82 } |
83 | 83 |
84 void ShellDownloadManagerDelegate::RestartDownload( | 84 void ShellDownloadManagerDelegate::ChooseDownloadPath( |
85 int32 download_id, | 85 int32 download_id, |
| 86 const DownloadTargetCallback& callback, |
86 const FilePath& suggested_path) { | 87 const FilePath& suggested_path) { |
87 DownloadItem* download = | 88 DownloadItem* item = |
88 download_manager_->GetActiveDownloadItem(download_id); | 89 download_manager_->GetActiveDownloadItem(download_id); |
89 if (!download) | 90 if (!item) |
90 return; | 91 return; |
91 | 92 |
92 // Since we have no download UI, show the user a dialog always. | |
93 download->OnTargetPathDetermined(suggested_path, | |
94 DownloadItem::TARGET_DISPOSITION_PROMPT, | |
95 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | |
96 download_manager_->RestartDownload(download_id); | |
97 } | |
98 | |
99 void ShellDownloadManagerDelegate::ChooseDownloadPath(DownloadItem* item) { | |
100 FilePath result; | 93 FilePath result; |
101 #if defined(OS_WIN) && !defined(USE_AURA) | 94 #if defined(OS_WIN) && !defined(USE_AURA) |
102 const FilePath suggested_path(item->GetTargetFilePath()); | |
103 std::wstring file_part = FilePath(suggested_path).BaseName().value(); | 95 std::wstring file_part = FilePath(suggested_path).BaseName().value(); |
104 wchar_t file_name[MAX_PATH]; | 96 wchar_t file_name[MAX_PATH]; |
105 base::wcslcpy(file_name, file_part.c_str(), arraysize(file_name)); | 97 base::wcslcpy(file_name, file_part.c_str(), arraysize(file_name)); |
106 OPENFILENAME save_as; | 98 OPENFILENAME save_as; |
107 ZeroMemory(&save_as, sizeof(save_as)); | 99 ZeroMemory(&save_as, sizeof(save_as)); |
108 save_as.lStructSize = sizeof(OPENFILENAME); | 100 save_as.lStructSize = sizeof(OPENFILENAME); |
109 save_as.hwndOwner = item->GetWebContents()->GetNativeView(); | 101 save_as.hwndOwner = item->GetWebContents()->GetNativeView(); |
110 save_as.lpstrFile = file_name; | 102 save_as.lpstrFile = file_name; |
111 save_as.nMaxFile = arraysize(file_name); | 103 save_as.nMaxFile = arraysize(file_name); |
112 | 104 |
113 std::wstring directory; | 105 std::wstring directory; |
114 if (!suggested_path.empty()) | 106 if (!suggested_path.empty()) |
115 directory = suggested_path.DirName().value(); | 107 directory = suggested_path.DirName().value(); |
116 | 108 |
117 save_as.lpstrInitialDir = directory.c_str(); | 109 save_as.lpstrInitialDir = directory.c_str(); |
118 save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING | | 110 save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING | |
119 OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST; | 111 OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST; |
120 | 112 |
121 if (GetSaveFileName(&save_as)) | 113 if (GetSaveFileName(&save_as)) |
122 result = FilePath(std::wstring(save_as.lpstrFile)); | 114 result = FilePath(std::wstring(save_as.lpstrFile)); |
123 #else | 115 #else |
124 NOTIMPLEMENTED(); | 116 NOTIMPLEMENTED(); |
125 #endif | 117 #endif |
126 | 118 |
127 if (result.empty()) { | 119 callback.Run(result, DownloadItem::TARGET_DISPOSITION_PROMPT, |
128 download_manager_->FileSelectionCanceled(item->GetId()); | 120 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, result); |
129 } else { | |
130 download_manager_->FileSelected(result, item->GetId()); | |
131 } | |
132 } | 121 } |
133 | 122 |
134 } // namespace content | 123 } // namespace content |
OLD | NEW |