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

Unified Diff: chrome/browser/download/chrome_download_manager_delegate.cc

Issue 11574006: Implement chrome.downloads.onDeterminingFilename() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: @r176665 Created 7 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/download/chrome_download_manager_delegate.cc
diff --git a/chrome/browser/download/chrome_download_manager_delegate.cc b/chrome/browser/download/chrome_download_manager_delegate.cc
index 488793ba6aefc5db30db210f328cc7e8a5f77a3b..6dec3425cfe6af25a66bdcbc41882a2f303e1f83 100644
--- a/chrome/browser/download/chrome_download_manager_delegate.cc
+++ b/chrome/browser/download/chrome_download_manager_delegate.cc
@@ -23,6 +23,8 @@
#include "chrome/browser/download/download_history.h"
#include "chrome/browser/download/download_path_reservation_tracker.h"
#include "chrome/browser/download/download_prefs.h"
+#include "chrome/browser/download/download_service.h"
+#include "chrome/browser/download/download_service_factory.h"
#include "chrome/browser/download/download_status_updater.h"
#include "chrome/browser/download/download_util.h"
#include "chrome/browser/download/save_package_file_picker.h"
@@ -172,17 +174,10 @@ ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() {
void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) {
download_manager_ = dm;
-#if !defined(OS_ANDROID)
- extension_event_router_.reset(new ExtensionDownloadsEventRouter(
- profile_, download_manager_));
-#endif
}
void ChromeDownloadManagerDelegate::Shutdown() {
download_prefs_.reset();
-#if !defined(OS_ANDROID)
- extension_event_router_.reset();
-#endif
}
DownloadId ChromeDownloadManagerDelegate::GetNextId() {
@@ -668,6 +663,11 @@ void ChromeDownloadManagerDelegate::Observe(
callback.Run(installer->did_handle_successfully());
}
+ChromeDownloadManagerDelegate::ContinueFilenameDeterminationInfo::
+ ContinueFilenameDeterminationInfo() {}
+ChromeDownloadManagerDelegate::ContinueFilenameDeterminationInfo::
+ ~ContinueFilenameDeterminationInfo() {}
+
void ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone(
int32 download_id,
const content::DownloadTargetCallback& callback,
@@ -683,12 +683,12 @@ void ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone(
bool should_prompt = (download->GetTargetDisposition() ==
DownloadItem::TARGET_DISPOSITION_PROMPT);
bool is_forced_path = !download->GetForcedFilePath().empty();
+ FilePath generated_name;
FilePath suggested_path;
// Check whether this download is for an extension install or not.
// Allow extensions to be explicitly saved.
if (!is_forced_path) {
- FilePath generated_name;
GenerateFileNameFromRequest(
*download,
&generated_name,
@@ -733,6 +733,81 @@ void ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone(
suggested_path = suggested_path.AddExtension(kWebIntentsFileExtension);
}
+ ContinueFilenameDeterminationInfo continue_info;
+ continue_info.download_id = download_id;
+ continue_info.callback = callback;
+ continue_info.danger_type = danger_type;
+ continue_info.visited_referrer_before = visited_referrer_before;
+ continue_info.should_prompt = should_prompt;
+
+ base::Closure filename_determined = base::Bind(
+ &ChromeDownloadManagerDelegate::OnExtensionsDeterminedFilename,
+ this,
+ continue_info,
+ suggested_path,
+ is_forced_path);
+#if !defined(OS_ANDROID)
+ if (is_forced_path ||
+ ShouldOpenWithWebIntents(download) ||
+ !DownloadServiceFactory::GetForProfile(profile_)
+ ->GetExtensionEventRouter()) {
+ filename_determined.Run();
+ } else {
+ DownloadServiceFactory::GetForProfile(profile_)->GetExtensionEventRouter()
+ ->OnDownloadFilenameDetermined(
+ download,
+ generated_name,
+ filename_determined,
+ base::Bind(&ChromeDownloadManagerDelegate::OnExtensionsChangingFilename,
+ this, continue_info));
+ }
+#else
+ filename_determined.Run();
+#endif
+}
+
+void ChromeDownloadManagerDelegate::OnExtensionsChangingFilename(
+ const ContinueFilenameDeterminationInfo& continue_info,
+ const FilePath& changed_filename,
+ bool overwrite) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ CHECK(!changed_filename.empty());
+ CHECK(!changed_filename.IsAbsolute());
+ DownloadItem* download =
+ download_manager_->GetDownload(continue_info.download_id);
+ if (!download || (download->GetState() != DownloadItem::IN_PROGRESS))
+ return;
+ // If an extension overrides the filename, then the target directory will be
+ // forced to download_prefs_->DownloadPath() since extensions cannot place
+ // downloaded files anywhere except there. This prevents subdirectories from
+ // accumulating: if an extension is allowed to say that a file should go in
+ // last_download_path/music/foo.mp3, then last_download_path will accumulate
+ // the subdirectory /music/ so that the next download may end up in
+ // last_download_path/music/music/bar.mp3.
+ FilePath temp_filename(download_prefs_->DownloadPath().Append(
+ changed_filename));
Randy Smith (Not in Mondays) 2013/01/14 21:02:29 Where are we stripping out .. and similar?
benjhayden 2013/01/17 02:52:05 See EDERD::DeterminerCallback > ValidateFilename i
+ net::GenerateSafeFileName(download->GetMimeType(), false, &temp_filename);
+ // If |is_forced_path| were true, then extensions would not have been
+ // consulted, so use |overwrite| instead of |is_forced_path|. This does NOT
+ // set DownloadItem::GetForcedFilePath()!
+ OnExtensionsDeterminedFilename(continue_info, temp_filename, overwrite);
+}
+
+void ChromeDownloadManagerDelegate::OnExtensionsDeterminedFilename(
+ const ContinueFilenameDeterminationInfo& continue_info,
+ const FilePath& suggested_path,
+ bool is_forced_path) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ int32 download_id = continue_info.download_id;
+ const content::DownloadTargetCallback& callback = continue_info.callback;
+ content::DownloadDangerType danger_type = continue_info.danger_type;
+ bool visited_referrer_before = continue_info.visited_referrer_before;
+ bool should_prompt = continue_info.should_prompt;
+ DownloadItem* download =
+ download_manager_->GetDownload(download_id);
+ if (!download || (download->GetState() != DownloadItem::IN_PROGRESS))
+ return;
+
// If the download hasn't already been marked dangerous (could be
// DANGEROUS_URL), check if it is a dangerous file.
if (danger_type == content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) {

Powered by Google App Engine
This is Rietveld 408576698