Index: chrome/browser/ui/download/download_tab_helper.cc |
diff --git a/chrome/browser/ui/download/download_tab_helper.cc b/chrome/browser/ui/download/download_tab_helper.cc |
index 54a95de7cb309ef8540421f3420cc00d67d88237..3770f92ddbd0413992087d302f688651410c5baa 100644 |
--- a/chrome/browser/ui/download/download_tab_helper.cc |
+++ b/chrome/browser/ui/download/download_tab_helper.cc |
@@ -4,15 +4,20 @@ |
#include "chrome/browser/ui/download/download_tab_helper.h" |
+#include "base/path_service.h" |
#include "chrome/browser/browser_process.h" |
#include "chrome/browser/download/download_manager.h" |
+#include "chrome/browser/download/download_prefs.h" |
#include "chrome/browser/download/download_request_limiter.h" |
#include "chrome/browser/download/download_util.h" |
+#include "chrome/browser/prefs/pref_service.h" |
#include "chrome/browser/profiles/profile.h" |
#include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" |
#include "chrome/browser/ui/blocked_content/blocked_content_tab_helper_delegate.h" |
#include "chrome/browser/ui/download/download_tab_helper_delegate.h" |
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
+#include "chrome/common/chrome_paths.h" |
+#include "chrome/common/pref_names.h" |
#include "chrome/common/render_messages.h" |
#include "content/browser/tab_contents/tab_contents.h" |
#include "content/common/view_messages.h" |
@@ -54,9 +59,6 @@ void DownloadTabHelper::OnSaveURL(const GURL& url) { |
dlm->DownloadUrl(url, tab_contents()->GetURL(), "", tab_contents()); |
} |
-// Used in automated testing to bypass prompting the user for file names. |
-// Instead, the names and paths are hard coded rather than running them through |
-// file name sanitation and extension / mime checking. |
Randy Smith (Not in Mondays)
2011/06/02 19:13:57
Why delete this comment? I think it's still valid
haraken1
2011/06/03 06:50:27
Oops. I restored this comment download_tab_helper.
|
bool DownloadTabHelper::SavePage(const FilePath& main_file, |
const FilePath& dir_path, |
SavePackage::SavePackageType save_type) { |
@@ -65,9 +67,74 @@ bool DownloadTabHelper::SavePage(const FilePath& main_file, |
save_package_ = |
new SavePackage(tab_contents_wrapper_, save_type, main_file, dir_path); |
+ // Skips GetSaveInfo() and directly calls Init(). |
+ // We do not have to explicitly disable the select file dialog |
+ // since we skip the process that can show the dialog. |
return save_package_->Init(); |
} |
+string16 DownloadTabHelper::SavePageWithoutDialog() { |
+ tab_contents()->Stop(); |
+ |
+ save_package_ = new SavePackage(tab_contents_wrapper_); |
+ // Disables the select file dialog. |
+ save_package_->SetShouldPromptUser(false); |
+ // This GetSaveInfo() calls save_package_->Init() in the background. |
+ save_package_->GetSaveInfo(); |
+ return tab_contents()->GetTitle(); |
+} |
+ |
+void DownloadTabHelper::ChangeSaveDirectoryPrefs( |
+ const FilePath& website_save_dir, |
+ const FilePath& download_save_dir, |
+ const FilePath& default_downloads_dir, |
+ const SavePackage::SavePackageType save_type) { |
+ PrefService* prefs = tab_contents()->profile()->GetPrefs(); |
+ |
+ DCHECK(prefs->FindPreference(prefs::kDownloadDefaultDirectory)); |
+ prev_download_save_dir_ = prefs->GetFilePath( |
+ prefs::kDownloadDefaultDirectory); |
+ |
+ // Check whether the preference has the default folder for saving HTML. |
+ // If not, initialize it with the default folder for downloaded files. |
+ if (!prefs->FindPreference(prefs::kSaveFileDefaultDirectory)) { |
+ prefs->RegisterFilePathPref(prefs::kSaveFileDefaultDirectory, |
+ prev_download_save_dir_, |
+ PrefService::UNSYNCABLE_PREF); |
+ } |
+ prev_website_save_dir_ = prefs->GetFilePath( |
+ prefs::kSaveFileDefaultDirectory); |
+ |
+ if (!PathService::Get( |
+ chrome::DIR_DEFAULT_DOWNLOADS, &prev_default_downloads_dir_)) { |
+ LOG(ERROR) << "Cannot find the user's \"Downloads\" folder."; |
+ prev_default_downloads_dir_.clear(); |
+ } |
+ |
+ DownloadPrefs* download_prefs = |
+ tab_contents()->profile()->GetDownloadManager()->download_prefs(); |
+ prev_save_type_ = |
+ static_cast<SavePackage::SavePackageType> |
+ (download_prefs->save_file_type()); |
+ |
+ prefs->SetFilePath(prefs::kSaveFileDefaultDirectory, website_save_dir); |
+ prefs->SetFilePath(prefs::kDownloadDefaultDirectory, download_save_dir); |
+ PathService::Set(chrome::DIR_DEFAULT_DOWNLOADS, default_downloads_dir); |
+ prefs->SetInteger(prefs::kSaveFileType, save_type); |
Randy Smith (Not in Mondays)
2011/06/02 19:13:57
It looks to me as if everything in this function c
haraken1
2011/06/03 06:50:27
Makes sense. Done.
|
+} |
+ |
+void DownloadTabHelper::RestoreSaveDirectoryPrefs() { |
+ PrefService* prefs = tab_contents()->profile()->GetPrefs(); |
+ prefs->SetFilePath(prefs::kSaveFileDefaultDirectory, prev_website_save_dir_); |
+ prefs->SetFilePath(prefs::kDownloadDefaultDirectory, prev_download_save_dir_); |
+ // Restores DIR_DEFAULT_DOWNLOADS only when we could get |
+ // |prev_default_downloads_dir_| successfully in ChangeSaveDirectoryPrefs(). |
+ if (!prev_default_downloads_dir_.empty()) |
+ PathService::Set( |
+ chrome::DIR_DEFAULT_DOWNLOADS, prev_default_downloads_dir_); |
+ prefs->SetInteger(prefs::kSaveFileType, prev_save_type_); |
+} |
+ |
bool DownloadTabHelper::CanDownload(int request_id) { |
if (delegate_) |
return delegate_->CanDownload(request_id); |