summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjohnme@google.com <johnme@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2011-07-08 17:26:50 +0000
committerjohnme@google.com <johnme@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2011-07-08 17:26:50 +0000
commitfed382594bf40fa64df79cd71ec43f64f963fa07 (patch)
tree0b46305af9bc9f24ca4815a47094dee0f2c7bf4b
parent061b7475532b4fca9c35022c74405044ff1be3d2 (diff)
downloadchromium_src-fed382594bf40fa64df79cd71ec43f64f963fa07.zip
chromium_src-fed382594bf40fa64df79cd71ec43f64f963fa07.tar.gz
chromium_src-fed382594bf40fa64df79cd71ec43f64f963fa07.tar.bz2
Revert 91861 - When the download folder does not exist, change the download folder to a user's "Downloads" (or something depending on the platform)
Description of reverted commit follows: - Set the download folder to Foo. You can download a PDF file to Foo without being asked anything (if you do not choose "Ask where to save each file before downloading"). Then delete Foo. Now, if you try to download the PDF file, then a file chooser dialog is displayed suggesting your "Downloads" folder. - Set the download folder to Foo. Save an HTML to Bar by right-clicking and choosing "Save as ..." on the tab. If you try to save the HTML, the file chooser dialog is displayed suggesting Bar. Then delete Bar. Now, if you try to save the HTML, the file chooser dialog is displayed suggesting Foo. Then delete Foo. If you try to save the HTML, the file chooser dialog is displayed suggesting your "Downloads" folder. - Set the download folder to Foo. Observe that Foo is displayed as the download location in the preferences page. Then delete Foo and reload the preferences page. Observe that your "Downloads" folder is displayed as the download location in the preferences page. If you are opening multiple preferences pages, then this change is automatically reflected to all the preferences pages. BUG=32552 TEST=SavePageBrowserTest.SavedFolder1,SavePageBrowserTest.SavedFolder2,SavePageBrowserTest.SavedFolder3,DownloadTest.DownloadedFolder,DownloadManagerTest.StartDownload,BaseFileTest.*,DownloadFileTest.RenameFileFinal Review URL: http://codereview.chromium.org/6973052 TBR=haraken@google.com Review URL: http://codereview.chromium.org/7324031 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@91869 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/download/base_file.cc10
-rw-r--r--chrome/browser/download/base_file.h2
-rw-r--r--chrome/browser/download/download_browsertest.cc156
-rw-r--r--chrome/browser/download/download_file_manager.cc2
-rw-r--r--chrome/browser/download/download_manager.cc34
-rw-r--r--chrome/browser/download/download_manager.h6
-rw-r--r--chrome/browser/download/download_manager_unittest.cc8
-rw-r--r--chrome/browser/download/download_prefs.cc12
-rw-r--r--chrome/browser/download/download_util.cc65
-rw-r--r--chrome/browser/download/download_util.h20
-rw-r--r--chrome/browser/download/download_util_unittest.cc3
-rw-r--r--chrome/browser/download/save_package.cc26
-rw-r--r--chrome/browser/download/save_package.h4
-rw-r--r--chrome/browser/download/save_page_browsertest.cc244
-rw-r--r--chrome/browser/ui/download/download_tab_helper.cc17
-rw-r--r--chrome/browser/ui/download/download_tab_helper.h18
-rw-r--r--chrome/browser/ui/webui/options/advanced_options_handler.cc52
-rw-r--r--chrome/browser/ui/webui/options/advanced_options_handler.h35
18 files changed, 138 insertions, 576 deletions
diff --git a/chrome/browser/download/base_file.cc b/chrome/browser/download/base_file.cc
index 5b2d6d4..e52f08a 100644
--- a/chrome/browser/download/base_file.cc
+++ b/chrome/browser/download/base_file.cc
@@ -55,12 +55,10 @@ bool BaseFile::Initialize(bool calculate_hash) {
if (calculate_hash_)
secure_hash_.reset(crypto::SecureHash::Create(crypto::SecureHash::SHA256));
- FilePath save_path =
- download_util::GetDefaultDownloadDirectoryFromPathService();
- if (full_path_.empty() &&
- !file_util::CreateTemporaryFileInDir(save_path, &full_path_))
- return false;
- return Open();
+ if (!full_path_.empty() ||
+ download_util::CreateTemporaryFileForDownload(&full_path_))
+ return Open();
+ return false;
}
bool BaseFile::AppendDataToFile(const char* data, size_t data_len) {
diff --git a/chrome/browser/download/base_file.h b/chrome/browser/download/base_file.h
index 88e2afb..02563b7 100644
--- a/chrome/browser/download/base_file.h
+++ b/chrome/browser/download/base_file.h
@@ -32,7 +32,7 @@ class BaseFile {
const linked_ptr<net::FileStream>& file_stream);
virtual ~BaseFile();
- // If |calculate_hash| is true, sha256 hash will be calculated.
+ // If calculate_hash is true, sha256 hash will be calculated.
bool Initialize(bool calculate_hash);
// Write a new chunk of data to the file. Returns true on success (all bytes
diff --git a/chrome/browser/download/download_browsertest.cc b/chrome/browser/download/download_browsertest.cc
index 8f88636..5248ecb 100644
--- a/chrome/browser/download/download_browsertest.cc
+++ b/chrome/browser/download/download_browsertest.cc
@@ -127,7 +127,6 @@ class DownloadsObserver : public DownloadManager::Observer,
// State accessors.
bool select_file_dialog_seen() { return select_file_dialog_seen_; }
- const FilePath& suggested_path() { return suggested_path_; }
// Wait for whatever state was specified in the constructor.
void WaitForFinished() {
@@ -240,10 +239,8 @@ class DownloadsObserver : public DownloadManager::Observer,
}
}
- virtual void SelectFileDialogDisplayed(
- int32 /* id */, const FilePath& suggested_path) {
+ virtual void SelectFileDialogDisplayed(int32 /* id */) {
select_file_dialog_seen_ = true;
- suggested_path_ = suggested_path;
SignalIfFinished();
}
@@ -319,9 +316,6 @@ class DownloadsObserver : public DownloadManager::Observer,
// Holds the download ids which were dangerous.
std::set<int32> dangerous_downloads_seen_;
- // The suggested file path in the select file dialog.
- FilePath suggested_path_;
-
DISALLOW_COPY_AND_ASSIGN(DownloadsObserver);
};
@@ -497,6 +491,12 @@ class CancelTestDataCollector
class DownloadTest : public InProcessBrowserTest {
public:
+ enum SelectExpectation {
+ EXPECT_NO_SELECT_DIALOG = -1,
+ EXPECT_NOTHING,
+ EXPECT_SELECT_DIALOG
+ };
+
DownloadTest() {
EnableDOMAutomation();
}
@@ -544,7 +544,7 @@ class DownloadTest : public InProcessBrowserTest {
// Location of the file destination (place to which it is downloaded).
FilePath DestinationFile(Browser* browser, FilePath file) {
- return GetDownloadSaveDirectory(browser).Append(file);
+ return GetDownloadDirectory(browser).Append(file);
}
// Must be called after browser creation. Creates a temporary
@@ -565,16 +565,11 @@ class DownloadTest : public InProcessBrowserTest {
return true;
}
- // Delete the default folder for downloaded files.
- bool DeleteDownloadsDirectory() {
- return downloads_directory_.Delete();
- }
-
DownloadPrefs* GetDownloadPrefs(Browser* browser) {
return browser->profile()->GetDownloadManager()->download_prefs();
}
- FilePath GetDownloadSaveDirectory(Browser* browser) {
+ FilePath GetDownloadDirectory(Browser* browser) {
DownloadManager* download_mananger =
browser->profile()->GetDownloadManager();
return download_mananger->download_prefs()->download_path();
@@ -582,17 +577,13 @@ class DownloadTest : public InProcessBrowserTest {
// Create a DownloadsObserver that will wait for the
// specified number of downloads to finish.
- // If |finish_on_select_file| is true, the object will also be
- // considered finished when the select file dialog is displayed.
- DownloadsObserver* CreateWaiter(Browser* browser,
- int num_downloads,
- bool finish_on_select_file) {
+ DownloadsObserver* CreateWaiter(Browser* browser, int num_downloads) {
DownloadManager* download_manager =
browser->profile()->GetDownloadManager();
return new DownloadsObserver(
download_manager, num_downloads,
DownloadItem::COMPLETE, // Really done
- finish_on_select_file, // Bail on select file
+ false, // Bail on select file
ON_DANGEROUS_DOWNLOAD_FAIL);
}
@@ -629,25 +620,19 @@ class DownloadTest : public InProcessBrowserTest {
// Download |url|, then wait for the download to finish.
// |disposition| indicates where the navigation occurs (current tab, new
// foreground tab, etc).
- // |expect_file_dialog| indicates whether a select file dialog should be
+ // |expectation| indicates whether or not a Select File dialog should be
// open when the download is finished, or if we don't care.
- // If the dialog appears, the routine exits. The only effect
- // |expect_file_dialog| has is whether or not the test succeeds.
- // |expected_suggested_path| is the path expected to be suggested in the
- // select file dialog. This |expected_suggested_path| must be specified
- // if |expect_file_dialog| is true. If |expect_file_dialog| is false,
- // |expected_suggested_path| is ignored.
+ // If the dialog appears, the routine exits. The only effect |expectation|
+ // has is whether or not the test succeeds.
// |browser_test_flags| indicate what to wait for, and is an OR of 0 or more
// values in the ui_test_utils::BrowserTestWaitFlags enum.
void DownloadAndWaitWithDisposition(Browser* browser,
const GURL& url,
WindowOpenDisposition disposition,
- bool expect_file_dialog,
- const FilePath& expected_suggested_path,
+ SelectExpectation expectation,
int browser_test_flags) {
// Setup notification, navigate, and block.
- scoped_ptr<DownloadsObserver> observer(
- CreateWaiter(browser, 1, expect_file_dialog));
+ scoped_ptr<DownloadsObserver> observer(CreateWaiter(browser, 1));
// This call will block until the condition specified by
// |browser_test_flags|, but will not wait for the download to finish.
ui_test_utils::NavigateToURLWithDisposition(browser,
@@ -657,31 +642,22 @@ class DownloadTest : public InProcessBrowserTest {
// Waits for the download to complete.
observer->WaitForFinished();
- // Checks if the select file dialog was displayed as expected.
- // If displayed, checks the suggested path in the dialog.
- if (expect_file_dialog) {
- EXPECT_TRUE(observer->select_file_dialog_seen());
- EXPECT_EQ(observer->suggested_path(), expected_suggested_path);
- } else {
- EXPECT_FALSE(observer->select_file_dialog_seen());
+ // If specified, check the state of the select file dialog.
+ if (expectation != EXPECT_NOTHING) {
+ EXPECT_EQ(expectation == EXPECT_SELECT_DIALOG,
+ observer->select_file_dialog_seen());
}
}
// Download a file in the current tab, then wait for the download to finish.
- // Expect that no select file dialog is displayed.
- void DownloadAndWait(Browser* browser, const GURL& url) {
- DownloadAndWaitWithDisposition(
- browser, url, CURRENT_TAB, false, FilePath(),
- ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
- }
-
- // Download a file in the current tab, then wait for the download to finish.
- // Expect that a select file dialog suggesting |expected_suggested_path|
- // is displayed.
- void DownloadAndWaitWithDialog(Browser* browser, const GURL& url,
- const FilePath& expected_suggested_path) {
+ void DownloadAndWait(Browser* browser,
+ const GURL& url,
+ SelectExpectation expectation) {
DownloadAndWaitWithDisposition(
- browser, url, CURRENT_TAB, true, expected_suggested_path,
+ browser,
+ url,
+ CURRENT_TAB,
+ expectation,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
}
@@ -747,7 +723,7 @@ class DownloadTest : public InProcessBrowserTest {
// Download a partial web page in a background tab and wait.
// The mock system will not complete until it gets a special URL.
- scoped_ptr<DownloadsObserver> observer(CreateWaiter(browser, 1, false));
+ scoped_ptr<DownloadsObserver> observer(CreateWaiter(browser, 1));
ui_test_utils::NavigateToURL(browser, url);
// TODO(ahendrickson): check download status text before downloading.
@@ -972,8 +948,8 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadMimeType) {
FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
- // Download the file and wait.
- DownloadAndWait(browser(), url);
+ // Download the file and wait. We do not expect the Select File dialog.
+ DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
// Check state.
EXPECT_EQ(1, browser()->tab_count());
@@ -981,31 +957,6 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadMimeType) {
CheckDownloadUI(browser(), true, true, file);
}
-// Checks if a file is saved to the user's "Downloads" folder
-// in the following situation:
-// The default folder for downloaded files does not exist.
-// The user's "Downloads" folder exists.
-IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadFolder) {
- ASSERT_TRUE(InitialSetup(false));
- FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
- GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
-
- FilePath default_download_dir =
- download_util::GetDefaultDownloadDirectoryFromPathService();
- FilePath downloaded_file = default_download_dir.Append(file);
-
- // Delete the default folder for downloaded files.
- ASSERT_TRUE(DeleteDownloadsDirectory());
- ASSERT_FALSE(file_util::PathExists(GetDownloadSaveDirectory(browser())));
-
- // Download the file and wait.
- DownloadAndWaitWithDialog(browser(), url, downloaded_file);
-
- EXPECT_FALSE(file_util::PathExists(downloaded_file));
- EXPECT_FALSE(file_util::PathExists(GetDownloadSaveDirectory(browser())));
- EXPECT_EQ(1, browser()->tab_count());
-}
-
#if defined(OS_WIN)
// Download a file and confirm that the zone identifier (on windows)
// is set to internet.
@@ -1014,8 +965,8 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, CheckInternetZone) {
FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
- // Download the file and wait.
- DownloadAndWait(browser(), url);
+ // Download the file and wait. We do not expect the Select File dialog.
+ DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
// Check state. Special file state must be checked before CheckDownload,
// as CheckDownload will delete the output file.
@@ -1028,7 +979,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, CheckInternetZone) {
}
#endif
-// Put up a select file dialog when the file is downloaded, due to its MIME
+// Put up a Select File dialog when the file is downloaded, due to its MIME
// type.
//
// This test runs correctly, but leaves behind turds in the test user's
@@ -1039,15 +990,14 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DISABLED_DownloadMimeTypeSelect) {
ASSERT_TRUE(InitialSetup(true));
FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
- FilePath file_path(DestinationFile(browser(), file));
- // Download the file and wait. We expect the select file dialog to appear
+ // Download the file and wait. We expect the Select File dialog to appear
// due to the MIME type.
- DownloadAndWaitWithDialog(browser(), url, file_path);
+ DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG);
// Check state.
EXPECT_EQ(1, browser()->tab_count());
- // Since we exited while the select file dialog was visible, there should not
+ // Since we exited while the Select File dialog was visible, there should not
// be anything in the download shelf and so it should not be visible.
CheckDownloadUI(browser(), false, false, FilePath());
}
@@ -1082,7 +1032,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, ContentDisposition) {
FilePath download_file(FILE_PATH_LITERAL("download-test3-attachment.gif"));
// Download a file and wait.
- DownloadAndWait(browser(), url);
+ DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
CheckDownload(browser(), download_file, file);
@@ -1102,7 +1052,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, PerWindowShelf) {
FilePath download_file(FILE_PATH_LITERAL("download-test3-attachment.gif"));
// Download a file and wait.
- DownloadAndWait(browser(), url);
+ DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
CheckDownload(browser(), download_file, file);
@@ -1167,7 +1117,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, IncognitoDownload) {
// Since |incognito| is a separate browser, we have to set it up explicitly.
incognito->profile()->GetPrefs()->SetBoolean(prefs::kPromptForDownload,
false);
- DownloadAndWait(incognito, url);
+ DownloadAndWait(incognito, url, EXPECT_NO_SELECT_DIALOG);
// We should still have 2 windows.
ExpectWindowCountAfterDownload(2);
@@ -1234,8 +1184,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, CloseNewTab1) {
browser(),
url,
NEW_BACKGROUND_TAB,
- false,
- FilePath(),
+ EXPECT_NO_SELECT_DIALOG,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
// When the download finishes, we should still have one tab.
@@ -1267,8 +1216,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DontCloseNewTab2) {
DownloadAndWaitWithDisposition(browser(),
GURL("javascript:openNew()"),
CURRENT_TAB,
- false,
- FilePath(),
+ EXPECT_NO_SELECT_DIALOG,
ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
// When the download finishes, we should have two tabs.
@@ -1310,8 +1258,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DontCloseNewTab3) {
DownloadAndWaitWithDisposition(browser(),
url,
CURRENT_TAB,
- false,
- FilePath(),
+ EXPECT_NO_SELECT_DIALOG,
ui_test_utils::BROWSER_TEST_NONE);
// When the download finishes, we should have two tabs.
@@ -1344,8 +1291,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, CloseNewTab2) {
DownloadAndWaitWithDisposition(browser(),
GURL("javascript:openNew()"),
CURRENT_TAB,
- false,
- FilePath(),
+ EXPECT_NO_SELECT_DIALOG,
ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
// When the download finishes, we should still have one tab.
@@ -1380,8 +1326,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, CloseNewTab3) {
browser(),
GURL("javascript:document.getElementById('form').submit()"),
CURRENT_TAB,
- false,
- FilePath(),
+ EXPECT_NO_SELECT_DIALOG,
ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
// When the download finishes, we should still have one tab.
@@ -1411,8 +1356,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, NewWindow) {
DownloadAndWaitWithDisposition(browser(),
url,
NEW_WINDOW,
- false,
- FilePath(),
+ EXPECT_NO_SELECT_DIALOG,
ui_test_utils::BROWSER_TEST_NONE);
// When the download finishes, the download shelf SHOULD NOT be visible in
@@ -1519,8 +1463,8 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) {
int64 origin_size;
file_util::GetFileSize(origin_file, &origin_size);
- // Download the file and wait.
- DownloadAndWait(browser(), url);
+ // Download the file and wait. We do not expect the Select File dialog.
+ DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
// Get details of what downloads have just happened.
std::vector<DownloadItem*> downloads;
@@ -1557,7 +1501,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, ChromeURLAfterDownload) {
GURL extensions_url(chrome::kChromeUIExtensionsURL);
ui_test_utils::NavigateToURL(browser(), flags_url);
- DownloadAndWait(browser(), download_url);
+ DownloadAndWait(browser(), download_url, EXPECT_NO_SELECT_DIALOG);
ui_test_utils::NavigateToURL(browser(), extensions_url);
TabContents* contents = browser()->GetSelectedTabContents();
ASSERT_TRUE(contents);
@@ -1594,7 +1538,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DISABLED_BrowserCloseAfterDownload) {
&result));
EXPECT_TRUE(result);
- DownloadAndWait(browser(), download_url);
+ DownloadAndWait(browser(), download_url, EXPECT_NO_SELECT_DIALOG);
ui_test_utils::WindowedNotificationObserver signal(
NotificationType::BROWSER_CLOSED,
@@ -1616,7 +1560,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, AutoOpen) {
MockDownloadOpeningObserver observer(
browser()->profile()->GetDownloadManager());
- DownloadAndWait(browser(), url);
+ DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
// Find the download and confirm it was opened.
std::vector<DownloadItem*> downloads;
diff --git a/chrome/browser/download/download_file_manager.cc b/chrome/browser/download/download_file_manager.cc
index 7915d4b..16a0ac7 100644
--- a/chrome/browser/download/download_file_manager.cc
+++ b/chrome/browser/download/download_file_manager.cc
@@ -274,7 +274,7 @@ void DownloadFileManager::OnDownloadManagerShutdown(DownloadManager* manager) {
//
// There are 2 possible rename cases where this method can be called:
// 1. tmp -> foo.crdownload (not final, safe)
-// 2. tmp -> Unconfirmed.xxx.crdownload (not final, dangerous)
+// 2. tmp-> Unconfirmed.xxx.crdownload (not final, dangerous)
void DownloadFileManager::RenameInProgressDownloadFile(
int id, const FilePath& full_path) {
VLOG(20) << __FUNCTION__ << "()" << " id = " << id
diff --git a/chrome/browser/download/download_manager.cc b/chrome/browser/download/download_manager.cc
index 28c04a2..43d76a9 100644
--- a/chrome/browser/download/download_manager.cc
+++ b/chrome/browser/download/download_manager.cc
@@ -8,6 +8,7 @@
#include "base/file_util.h"
#include "base/i18n/case_conversion.h"
#include "base/logging.h"
+#include "base/path_service.h"
#include "base/rand_util.h"
#include "base/stl_util-inl.h"
#include "base/stringprintf.h"
@@ -409,23 +410,30 @@ void DownloadManager::CheckVisitedReferrerBeforeDone(
download_prefs()->download_path()));
}
-void DownloadManager::CheckIfSuggestedPathExists(
- int32 download_id,
- DownloadStateInfo state,
- const FilePath& download_save_dir) {
+void DownloadManager::CheckIfSuggestedPathExists(int32 download_id,
+ DownloadStateInfo state,
+ const FilePath& default_path) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
- FilePath default_download_dir =
- download_util::GetDefaultDownloadDirectoryFromPathService();
- FilePath save_dir;
- if (download_util::ChooseSavableDirectory(
- FilePath(), download_save_dir, default_download_dir, &save_dir))
+ // Make sure the default download directory exists.
+ // TODO(phajdan.jr): only create the directory when we're sure the user
+ // is going to save there and not to another directory of his choice.
+ file_util::CreateDirectory(default_path);
+
+ // Check writability of the suggested path. If we can't write to it, default
+ // to the user's "My Documents" directory. We'll prompt them in this case.
+ FilePath dir = state.suggested_path.DirName();
+ FilePath filename = state.suggested_path.BaseName();
+ if (!file_util::PathIsWritable(dir)) {
+ VLOG(1) << "Unable to write to directory \"" << dir.value() << "\"";
state.prompt_user_for_save_location = true;
- state.suggested_path = save_dir.Append(state.suggested_path.BaseName());
+ PathService::Get(chrome::DIR_USER_DOCUMENTS, &state.suggested_path);
+ state.suggested_path = state.suggested_path.Append(filename);
+ }
// If the download is deemed dangerous, we'll use a temporary name for it.
if (state.IsDangerous()) {
- state.target_name = state.suggested_path.BaseName();
+ state.target_name = FilePath(state.suggested_path).BaseName();
// Create a temporary file to hold the file until the user approves its
// download.
FilePath::StringType file_name;
@@ -444,7 +452,7 @@ void DownloadManager::CheckIfSuggestedPathExists(
unconfirmed_prefix.append(
FILE_PATH_LITERAL(" %d.crdownload")).c_str(),
base::RandInt(0, 100000));
- path = state.suggested_path.DirName().Append(file_name);
+ path = dir.Append(file_name);
if (file_util::PathExists(path))
path = FilePath();
}
@@ -534,7 +542,7 @@ void DownloadManager::OnPathExistenceAvailable(
contents, owning_window,
reinterpret_cast<void*>(id_ptr));
FOR_EACH_OBSERVER(Observer, observers_,
- SelectFileDialogDisplayed(download_id, suggested_path));
+ SelectFileDialogDisplayed(download_id));
} else {
// No prompting for download, just continue with the suggested name.
ContinueDownloadWithPath(download, suggested_path);
diff --git a/chrome/browser/download/download_manager.h b/chrome/browser/download/download_manager.h
index 52fb9e2..241c894 100644
--- a/chrome/browser/download/download_manager.h
+++ b/chrome/browser/download/download_manager.h
@@ -87,9 +87,7 @@ class DownloadManager
// Called immediately after the DownloadManager puts up a select file
// dialog.
// |id| indicates which download opened the dialog.
- // |suggested_path| indicates the path suggested in the dialog.
- virtual void SelectFileDialogDisplayed(
- int32 id, const FilePath& suggested_path) {}
+ virtual void SelectFileDialogDisplayed(int32 id) {}
protected:
virtual ~Observer() {}
@@ -313,7 +311,7 @@ class DownloadManager
// stalls from interacting with the file system.
void CheckIfSuggestedPathExists(int32 download_id,
DownloadStateInfo state,
- const FilePath& download_save_dir);
+ const FilePath& default_path);
// Called on the UI thread once the DownloadManager has determined whether the
// suggested file path exists.
diff --git a/chrome/browser/download/download_manager_unittest.cc b/chrome/browser/download/download_manager_unittest.cc
index 501ad19..1b80445 100644
--- a/chrome/browser/download/download_manager_unittest.cc
+++ b/chrome/browser/download/download_manager_unittest.cc
@@ -235,8 +235,7 @@ class SelectFileObserver : public DownloadManager::Observer {
// Downloadmanager::Observer functions.
virtual void ModelChanged() {}
virtual void ManagerGoingDown() {}
- virtual void SelectFileDialogDisplayed(int32 id,
- const FilePath& suggested_path) {
+ virtual void SelectFileDialogDisplayed(int32 id) {
file_dialog_ids_.insert(id);
}
@@ -293,11 +292,8 @@ class ItemObserver : public DownloadItem::Observer {
TEST_F(DownloadManagerTest, StartDownload) {
BrowserThread io_thread(BrowserThread::IO, &message_loop_);
- ScopedTempDir download_save_dir;
- ASSERT_TRUE(download_save_dir.CreateUniqueTempDir());
PrefService* prefs = profile_->GetPrefs();
- prefs->SetFilePath(prefs::kDownloadDefaultDirectory,
- download_save_dir.path());
+ prefs->SetFilePath(prefs::kDownloadDefaultDirectory, FilePath());
download_manager_->download_prefs()->EnableAutoOpenBasedOnExtension(
FilePath(FILE_PATH_LITERAL("example.pdf")));
diff --git a/chrome/browser/download/download_prefs.cc b/chrome/browser/download/download_prefs.cc
index c627bf7..e9e0b76 100644
--- a/chrome/browser/download/download_prefs.cc
+++ b/chrome/browser/download/download_prefs.cc
@@ -60,18 +60,18 @@ void DownloadPrefs::RegisterUserPrefs(PrefService* prefs) {
SavePackage::SAVE_AS_COMPLETE_HTML,
PrefService::UNSYNCABLE_PREF);
- // The user's default "Downloads" folder.
- FilePath default_download_dir =
- download_util::GetDefaultDownloadDirectoryFromPathService();
+ // The default download path is userprofile\download.
+ const FilePath& default_download_path =
+ download_util::GetDefaultDownloadDirectory();
prefs->RegisterFilePathPref(prefs::kDownloadDefaultDirectory,
- default_download_dir,
+ default_download_path,
PrefService::UNSYNCABLE_PREF);
#if defined(OS_CHROMEOS)
// Ensure that the download directory specified in the preferences exists.
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
- NewRunnableFunction(&file_util::CreateDirectory, default_download_dir));
+ NewRunnableFunction(&file_util::CreateDirectory, default_download_path));
#endif // defined(OS_CHROMEOS)
// If the download path is dangerous we forcefully reset it. But if we do
@@ -82,7 +82,7 @@ void DownloadPrefs::RegisterUserPrefs(PrefService* prefs) {
prefs::kDownloadDefaultDirectory);
if (download_util::DownloadPathIsDangerous(current_download_dir)) {
prefs->SetFilePath(prefs::kDownloadDefaultDirectory,
- default_download_dir);
+ default_download_path);
}
prefs->SetBoolean(prefs::kDownloadDirUpgraded, true);
}
diff --git a/chrome/browser/download/download_util.cc b/chrome/browser/download/download_util.cc
index 1cb2d47..83acdb7 100644
--- a/chrome/browser/download/download_util.cc
+++ b/chrome/browser/download/download_util.cc
@@ -209,49 +209,36 @@ const int kAllNetErrorCodes[] = {
// Download temporary file creation --------------------------------------------
-bool ChooseSavableDirectory(
- const FilePath& website_save_dir,
- const FilePath& download_save_dir,
- const FilePath& default_download_dir,
- FilePath* save_dir) {
- bool prompt_dialog = false;
- if (file_util::PathIsWritable(website_save_dir)) {
- // If the default html/websites save folder exists,
- // then use the default html/websites save folder.
- *save_dir = website_save_dir;
- } else if (file_util::PathIsWritable(download_save_dir)) {
- // If the default html/websites save folder does not exist
- // but the default download folder exists,
- // then use the default download folder.
- *save_dir = download_save_dir;
- } else {
- // If both the above folders do not exist,
- // use the user's "Downloads" folder.
- *save_dir = default_download_dir;
- prompt_dialog = true;
- if (!file_util::PathIsWritable(*save_dir)) {
- VLOG(1) << "Cannot find the user's writable \"Downloads\" folder.";
- // Create the |download_save_dir| folder if we cannot get
- // the user's writable "Downloads" folder (This will be a rare case).
- *save_dir = download_save_dir;
+class DefaultDownloadDirectory {
+ public:
+ const FilePath& path() const { return path_; }
+ private:
+ DefaultDownloadDirectory() {
+ if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &path_)) {
+ NOTREACHED();
+ }
+ if (DownloadPathIsDangerous(path_)) {
+ if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE, &path_)) {
+ NOTREACHED();
+ }
}
- // Make sure that the folder does exist.
- if (!file_util::CreateDirectory(*save_dir))
- LOG(ERROR) << "Failed to create " << (*save_dir).value();
}
- return prompt_dialog;
+ friend struct base::DefaultLazyInstanceTraits<DefaultDownloadDirectory>;
+ FilePath path_;
+};
+
+static base::LazyInstance<DefaultDownloadDirectory>
+ g_default_download_directory(base::LINKER_INITIALIZED);
+
+const FilePath& GetDefaultDownloadDirectory() {
+ return g_default_download_directory.Get().path();
}
-FilePath GetDefaultDownloadDirectoryFromPathService() {
- FilePath default_download_dir;
- if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &default_download_dir))
- NOTREACHED();
- if (DownloadPathIsDangerous(default_download_dir)) {
- if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE,
- &default_download_dir))
- NOTREACHED();
- }
- return default_download_dir;
+bool CreateTemporaryFileForDownload(FilePath* temp_file) {
+ if (file_util::CreateTemporaryFileInDir(GetDefaultDownloadDirectory(),
+ temp_file))
+ return true;
+ return file_util::CreateTemporaryFile(temp_file);
}
bool DownloadPathIsDangerous(const FilePath& download_path) {
diff --git a/chrome/browser/download/download_util.h b/chrome/browser/download/download_util.h
index 07653c7..20a5693 100644
--- a/chrome/browser/download/download_util.h
+++ b/chrome/browser/download/download_util.h
@@ -50,20 +50,12 @@ namespace download_util {
// Download temporary file creation --------------------------------------------
-// Chooses a writable directory from |website_save_dir|, |download_save_dir|
-// and |default_download_dir| in this order of priority.
-// If none of them is writable, creates |download_save_dir| and
-// chooses |download_save_dir|. The choosed directory is stored to |save_dir|.
-// Returns true if neither |website_save_dir| nor |download_save_dir|
-// is writable, which indicates the select file dialog should be displayed.
-bool ChooseSavableDirectory(const FilePath& website_save_dir,
- const FilePath& download_save_dir,
- const FilePath& default_download_dir,
- FilePath* save_dir);
-
-// Returns the user's default "Downloads" folder provided by PathService.
-// This method returns the folder provided by PathService.
-FilePath GetDefaultDownloadDirectoryFromPathService();
+// Return the default download directory.
+const FilePath& GetDefaultDownloadDirectory();
+
+// Create a temporary file for a download in the user's default download
+// directory and return true if was successful in creating the file.
+bool CreateTemporaryFileForDownload(FilePath* path);
// Return true if the |download_path| is dangerous path.
bool DownloadPathIsDangerous(const FilePath& download_path);
diff --git a/chrome/browser/download/download_util_unittest.cc b/chrome/browser/download/download_util_unittest.cc
index 455abc1..c8143e2 100644
--- a/chrome/browser/download/download_util_unittest.cc
+++ b/chrome/browser/download/download_util_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -579,3 +579,4 @@ TEST(DownloadUtilTest, GenerateSafeFileName) {
}
} // namespace
+
diff --git a/chrome/browser/download/save_package.cc b/chrome/browser/download/save_package.cc
index 6a26c646..ddc12b1 100644
--- a/chrome/browser/download/save_package.cc
+++ b/chrome/browser/download/save_package.cc
@@ -1256,8 +1256,6 @@ FilePath SavePackage::GetSaveDirPreference(PrefService* prefs) {
}
void SavePackage::GetSaveInfo() {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
-
// Can't use tab_contents_ in the file thread, so get the data that we need
// before calling to it.
PrefService* prefs = tab_contents()->profile()->GetPrefs();
@@ -1269,22 +1267,24 @@ void SavePackage::GetSaveInfo() {
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
NewRunnableMethod(this, &SavePackage::CreateDirectoryOnFileThread,
- website_save_dir, download_save_dir, mime_type));
+ website_save_dir, download_save_dir, mime_type));
}
void SavePackage::CreateDirectoryOnFileThread(
const FilePath& website_save_dir,
const FilePath& download_save_dir,
const std::string& mime_type) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
-
FilePath save_dir;
- FilePath default_download_dir =
- download_util::GetDefaultDownloadDirectoryFromPathService();
- // Ignores the returned value since the select file dialog should be
- // displayed in any case.
- download_util::ChooseSavableDirectory(
- website_save_dir, download_save_dir, default_download_dir, &save_dir);
+ // If the default html/websites save folder doesn't exist...
+ if (!file_util::DirectoryExists(website_save_dir)) {
+ // If the default download dir doesn't exist, create it.
+ if (!file_util::DirectoryExists(download_save_dir))
+ file_util::CreateDirectory(download_save_dir);
+ save_dir = download_save_dir;
+ } else {
+ // If it does exist, use the default save dir param.
+ save_dir = website_save_dir;
+ }
bool can_save_as_complete = CanSaveAsComplete(mime_type);
FilePath suggested_filename = GetSuggestedNameForSaveAs(can_save_as_complete,
@@ -1314,8 +1314,6 @@ void SavePackage::CreateDirectoryOnFileThread(
void SavePackage::ContinueGetSaveInfo(const FilePath& suggested_path,
bool can_save_as_complete) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
-
// The TabContents which owns this SavePackage may have disappeared during
// the UI->FILE->UI thread hop of
// GetSaveInfo->CreateDirectoryOnFileThread->ContinueGetSaveInfo.
@@ -1406,8 +1404,6 @@ void SavePackage::ContinueGetSaveInfo(const FilePath& suggested_path,
// Called after the save file dialog box returns.
void SavePackage::ContinueSave(const FilePath& final_name,
int index) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
-
// Ensure the filename is safe.
saved_main_file_path_ = final_name;
download_util::GenerateSafeFileName(tab_contents()->contents_mime_type(),
diff --git a/chrome/browser/download/save_package.h b/chrome/browser/download/save_package.h
index 03966a9..74f5526 100644
--- a/chrome/browser/download/save_package.h
+++ b/chrome/browser/download/save_package.h
@@ -124,10 +124,6 @@ class SavePackage : public base::RefCountedThreadSafe<SavePackage>,
int tab_id() const { return tab_id_; }
int id() const { return unique_id_; }
- // Determines the saved file name based on the information of the
- // current page, and then starts to download the page. This method
- // runs in the background and may finish asynchronously after this
- // method returns.
void GetSaveInfo();
// Statics -------------------------------------------------------------------
diff --git a/chrome/browser/download/save_page_browsertest.cc b/chrome/browser/download/save_page_browsertest.cc
index d0cbbad..88dd702 100644
--- a/chrome/browser/download/save_page_browsertest.cc
+++ b/chrome/browser/download/save_page_browsertest.cc
@@ -4,25 +4,16 @@
#include "base/file_path.h"
#include "base/file_util.h"
-#include "base/i18n/file_util_icu.h"
#include "base/path_service.h"
#include "base/scoped_temp_dir.h"
-#include "base/string_util.h"
-#include "base/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/download/download_item.h"
-#include "chrome/browser/download/download_manager.h"
-#include "chrome/browser/download/download_prefs.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/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/download/download_tab_helper.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "chrome/browser/ui/webui/active_downloads_ui.h"
#include "chrome/common/chrome_paths.h"
-#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/in_process_browser_test.h"
#include "chrome/test/ui_test_utils.h"
@@ -82,73 +73,13 @@ class SavePageBrowserTest : public InProcessBrowserTest {
#endif
}
- // Changes the default folder prefs. This method saves the current folder
- // for saving HTML, the current folder for saving downloaded files,
- // the current user's "Downloads" folder and a save type (HTML only or
- // complete HTML files), and then changes them to |website_save_dir|,
- // |download_save_dir| and |save_type|, respectively.
- // If we call this method, we must call RestoreDirectoryPrefs()
- // after the test to restore the default folder prefs.
- void ChangeDirectoryPrefs(
- Profile* profile,
- const FilePath& website_save_dir,
- const FilePath& download_save_dir,
- const SavePackage::SavePackageType save_type) {
- DCHECK(profile);
- PrefService* prefs = 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);
-
- DownloadPrefs* download_prefs =
- 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);
- prefs->SetInteger(prefs::kSaveFileType, save_type);
- }
-
- // Restores the default folder prefs.
- void RestoreDirectoryPrefs(Profile* profile) {
- DCHECK(profile);
- PrefService* prefs = profile->GetPrefs();
- prefs->SetFilePath(
- prefs::kSaveFileDefaultDirectory, prev_website_save_dir_);
- prefs->SetFilePath(
- prefs::kDownloadDefaultDirectory, prev_download_save_dir_);
- prefs->SetInteger(prefs::kSaveFileType, prev_save_type_);
- }
-
// Path to directory containing test data.
FilePath test_dir_;
// Temporary directory we will save pages to.
ScopedTempDir save_dir_;
-
- // Temporarily stores the default folder prefs.
- FilePath prev_website_save_dir_;
- FilePath prev_download_save_dir_;
- SavePackage::SavePackageType prev_save_type_;
};
-} // namespace
-
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) {
FilePath file_name(FILE_PATH_LITERAL("a.htm"));
GURL url = URLRequestMockHTTPJob::GetMockUrl(
@@ -233,179 +164,6 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) {
dir.AppendASCII("1.css")));
}
-// Checks if an HTML page is saved to the default folder for saving HTML
-// in the following situation:
-// The default folder for saving HTML exists.
-// The default folder for downloaded files exists.
-// The user's "Downloads" folder exists.
-IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder1) {
- FilePath file_name(FILE_PATH_LITERAL("a.htm"));
- GURL url = URLRequestMockHTTPJob::GetMockUrl(
- FilePath(kTestDir).Append(file_name));
- ui_test_utils::NavigateToURL(browser(), url);
-
- TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
- ASSERT_TRUE(current_tab);
- ASSERT_TRUE(current_tab->tab_contents());
- ASSERT_TRUE(current_tab->tab_contents()->profile());
-
- ScopedTempDir website_save_dir, download_save_dir;
- // Prepare the default folder for saving HTML.
- ASSERT_TRUE(website_save_dir.CreateUniqueTempDir());
- // Prepare the default folder for downloaded files.
- ASSERT_TRUE(download_save_dir.CreateUniqueTempDir());
-
- // Changes the default prefs.
- ChangeDirectoryPrefs(
- current_tab->tab_contents()->profile(),
- website_save_dir.path(),
- download_save_dir.path(),
- SavePackage::SAVE_AS_ONLY_HTML);
-
-
- std::string ascii_basename =
- UTF16ToASCII(current_tab->download_tab_helper()->
- SavePageBasedOnDefaultPrefs()) + ".html";
- FilePath::StringType basename;
-#if defined(OS_WIN)
- basename = ASCIIToWide(ascii_basename);
-#else
- basename = ascii_basename;
-#endif
- file_util::ReplaceIllegalCharactersInPath(&basename, ' ');
- FilePath full_file_name = website_save_dir.path().Append(FilePath(basename));
-
- EXPECT_EQ(url, WaitForSavePackageToFinish());
-
- CheckDownloadUI(full_file_name);
-
- // Is the file downloaded to the default folder for saving HTML?
- EXPECT_TRUE(file_util::PathExists(full_file_name));
- EXPECT_TRUE(file_util::ContentsEqual(
- test_dir_.Append(FilePath(kTestDir)).Append(file_name),
- full_file_name));
-
- RestoreDirectoryPrefs(current_tab->tab_contents()->profile());
-}
-
-// Checks if an HTML page is saved to the default folder for downloaded files
-// in the following situation:
-// The default folder for saving HTML does not exist.
-// The default folder for downloaded files exists.
-// The user's "Downloads" folder exists.
-IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder2) {
- FilePath file_name(FILE_PATH_LITERAL("a.htm"));
- GURL url = URLRequestMockHTTPJob::GetMockUrl(
- FilePath(kTestDir).Append(file_name));
- ui_test_utils::NavigateToURL(browser(), url);
-
- TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
- ASSERT_TRUE(current_tab);
- ASSERT_TRUE(current_tab->tab_contents());
- ASSERT_TRUE(current_tab->tab_contents()->profile());
-
- ScopedTempDir download_save_dir;
- // Prepare the default folder for saving downloaded files.
- ASSERT_TRUE(download_save_dir.CreateUniqueTempDir());
- // Prepare non-existent folder.
- FilePath nonexistent_path(
- FILE_PATH_LITERAL("/tmp/koakuma_mikity_moemoe_nyannyan"));
- ASSERT_FALSE(file_util::PathExists(nonexistent_path));
-
- // Changes the default prefs.
- ChangeDirectoryPrefs(
- current_tab->tab_contents()->profile(),
- nonexistent_path,
- download_save_dir.path(),
- SavePackage::SAVE_AS_ONLY_HTML);
-
- std::string ascii_basename =
- UTF16ToASCII(current_tab->download_tab_helper()->
- SavePageBasedOnDefaultPrefs()) + ".html";
- FilePath::StringType basename;
-#if defined(OS_WIN)
- basename = ASCIIToWide(ascii_basename);
-#else
- basename = ascii_basename;
-#endif
- file_util::ReplaceIllegalCharactersInPath(&basename, ' ');
- FilePath full_file_name = download_save_dir.path().Append(FilePath(basename));
-
- EXPECT_EQ(url, WaitForSavePackageToFinish());
-
- CheckDownloadUI(full_file_name);
-
- // Is the file downloaded to the default folder for downloaded files?
- EXPECT_TRUE(file_util::PathExists(full_file_name));
- EXPECT_FALSE(file_util::PathExists(nonexistent_path));
- EXPECT_TRUE(file_util::ContentsEqual(
- test_dir_.Append(FilePath(kTestDir)).Append(file_name),
- full_file_name));
-
- RestoreDirectoryPrefs(current_tab->tab_contents()->profile());
-}
-
-// Checks if an HTML page is saved to the user's "Downloads" directory
-// in the following situation:
-// The default folder for saving HTML does not exist.
-// The default folder for downloaded files does not exist.
-// The user's "Downloads" folder exists.
-IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder3) {
- FilePath file_name(FILE_PATH_LITERAL("a.htm"));
- GURL url = URLRequestMockHTTPJob::GetMockUrl(
- FilePath(kTestDir).Append(file_name));
- ui_test_utils::NavigateToURL(browser(), url);
-
- TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
- ASSERT_TRUE(current_tab);
- ASSERT_TRUE(current_tab->tab_contents());
- ASSERT_TRUE(current_tab->tab_contents()->profile());
-
- // Prepare non-existent folder.
- FilePath nonexistent_path1(
- FILE_PATH_LITERAL("/tmp/koakuma_mikity_moemoe_nyannyan"));
- FilePath nonexistent_path2(
- FILE_PATH_LITERAL("/tmp/koakuma_mikity_moemoe_pyonpyon"));
- ASSERT_FALSE(file_util::PathExists(nonexistent_path1));
- ASSERT_FALSE(file_util::PathExists(nonexistent_path2));
-
- // Changes the default prefs.
- ChangeDirectoryPrefs(
- current_tab->tab_contents()->profile(),
- nonexistent_path1,
- nonexistent_path2,
- SavePackage::SAVE_AS_ONLY_HTML);
-
- std::string ascii_basename =
- UTF16ToASCII(current_tab->download_tab_helper()->
- SavePageBasedOnDefaultPrefs()) + ".html";
- FilePath::StringType basename;
-#if defined(OS_WIN)
- basename = ASCIIToWide(ascii_basename);
-#else
- basename = ascii_basename;
-#endif
- file_util::ReplaceIllegalCharactersInPath(&basename, ' ');
- FilePath default_download_dir =
- download_util::GetDefaultDownloadDirectoryFromPathService();
- FilePath full_file_name =
- default_download_dir.Append(FilePath(basename));
-
- EXPECT_EQ(url, WaitForSavePackageToFinish());
-
- CheckDownloadUI(full_file_name);
-
- // Is the file downloaded to the user's "Downloads" directory?
- EXPECT_TRUE(file_util::PathExists(full_file_name));
- EXPECT_FALSE(file_util::PathExists(nonexistent_path1));
- EXPECT_FALSE(file_util::PathExists(nonexistent_path2));
- EXPECT_TRUE(file_util::ContentsEqual(
- test_dir_.Append(FilePath(kTestDir)).Append(file_name),
- full_file_name));
-
- RestoreDirectoryPrefs(current_tab->tab_contents()->profile());
-}
-
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) {
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL));
ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE));
@@ -446,3 +204,5 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) {
test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
dir.AppendASCII("1.css")));
}
+
+} // namespace
diff --git a/chrome/browser/ui/download/download_tab_helper.cc b/chrome/browser/ui/download/download_tab_helper.cc
index e402523..54a95de 100644
--- a/chrome/browser/ui/download/download_tab_helper.cc
+++ b/chrome/browser/ui/download/download_tab_helper.cc
@@ -54,6 +54,9 @@ 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.
bool DownloadTabHelper::SavePage(const FilePath& main_file,
const FilePath& dir_path,
SavePackage::SavePackageType save_type) {
@@ -62,23 +65,9 @@ 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::SavePageBasedOnDefaultPrefs() {
- 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();
-}
-
bool DownloadTabHelper::CanDownload(int request_id) {
if (delegate_)
return delegate_->CanDownload(request_id);
diff --git a/chrome/browser/ui/download/download_tab_helper.h b/chrome/browser/ui/download/download_tab_helper.h
index 760a35f..ac55913 100644
--- a/chrome/browser/ui/download/download_tab_helper.h
+++ b/chrome/browser/ui/download/download_tab_helper.h
@@ -7,7 +7,6 @@
#pragma once
#include "base/basictypes.h"
-#include "base/gtest_prod_util.h"
#include "chrome/browser/download/save_package.h"
#include "content/browser/tab_contents/tab_contents_observer.h"
@@ -35,9 +34,6 @@ class DownloadTabHelper : public TabContentsObserver {
// Save page with the main HTML file path, the directory for saving resources,
// and the save type: HTML only or complete web page. Returns true if the
// saving process has been initiated successfully.
- // This method is 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.
bool SavePage(const FilePath& main_file, const FilePath& dir_path,
SavePackage::SavePackageType save_type);
@@ -52,20 +48,6 @@ class DownloadTabHelper : public TabContentsObserver {
void OnStartDownload(DownloadItem* download);
private:
- FRIEND_TEST_ALL_PREFIXES(SavePageBrowserTest, SaveFolder1);
- FRIEND_TEST_ALL_PREFIXES(SavePageBrowserTest, SaveFolder2);
- FRIEND_TEST_ALL_PREFIXES(SavePageBrowserTest, SaveFolder3);
-
- // Used in automated testing to bypass prompting the user for file names.
- // The difference between SavePageBasedOnDefaultPrefs() and SavePage()
- // is whether the default folder prefs are used. In case of SavePage(),
- // we need to give it the file path to which the file is saved.
- // On the other hand, in case of SavePageBasedOnDefaultPrefs(),
- // we need not to give the file path since the file path is determined
- // based on the default folder prefs. This method returns the title
- // of the current tab.
- string16 SavePageBasedOnDefaultPrefs();
-
// TabContentsObserver overrides.
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
virtual void DidGetUserGesture() OVERRIDE;
diff --git a/chrome/browser/ui/webui/options/advanced_options_handler.cc b/chrome/browser/ui/webui/options/advanced_options_handler.cc
index 20033f9..fd9e0c2 100644
--- a/chrome/browser/ui/webui/options/advanced_options_handler.cc
+++ b/chrome/browser/ui/webui/options/advanced_options_handler.cc
@@ -9,8 +9,6 @@
#include "base/basictypes.h"
#include "base/callback.h"
#include "base/command_line.h"
-#include "base/file_util.h"
-#include "base/path_service.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
@@ -25,7 +23,6 @@
#include "chrome/browser/service/service_process_control.h"
#include "chrome/browser/ui/options/options_util.h"
#include "chrome/browser/ui/webui/options/options_managed_banner_handler.h"
-#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
@@ -189,9 +186,6 @@ void AdvancedOptionsHandler::Initialize() {
SetupMetricsReportingSettingVisibility();
SetupFontSizeLabel();
SetupDownloadLocationPath();
- download_path_checker_ = new DownloadPathChecker(this);
- download_path_checker_->CheckIfDownloadPathExists(
- default_download_location_.GetValue());
SetupPromptForDownload();
SetupAutoOpenFileTypesDisabledAttribute();
SetupProxySettingsSection();
@@ -647,49 +641,3 @@ void AdvancedOptionsHandler::SetupSSLConfigSettings() {
"options.AdvancedOptions.SetUseTLS1CheckboxState", checked, disabled);
}
}
-
-AdvancedOptionsHandler::DownloadPathChecker::DownloadPathChecker(
- AdvancedOptionsHandler* handler)
- : handler_(handler) {
-}
-
-AdvancedOptionsHandler::DownloadPathChecker::~DownloadPathChecker() {
-}
-
-void AdvancedOptionsHandler::DownloadPathChecker::
- CheckIfDownloadPathExists(const FilePath& path) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
- BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
- NewRunnableMethod(this,
- &DownloadPathChecker::CheckIfDownloadPathExistsOnFileThread, path));
-}
-
-void AdvancedOptionsHandler::DownloadPathChecker::
- CheckIfDownloadPathExistsOnFileThread(
- const FilePath& path) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
- if (!file_util::PathExists(path)) {
- FilePath new_path;
- if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &new_path)) {
- // Create the |path| folder if we cannot get DIR_DEFAULT_DOWNLOADS
- // (This will be a rare case).
- new_path = path;
- }
- // Make sure that the folder does exist.
- if (!file_util::CreateDirectory(new_path))
- LOG(ERROR) << "Failed to create " << new_path.value();
-
- BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
- NewRunnableMethod(this,
- &DownloadPathChecker::OnDownloadPathChanged, new_path));
- }
-}
-
-void AdvancedOptionsHandler::DownloadPathChecker::
- OnDownloadPathChanged(const FilePath path) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
- if (handler_) {
- handler_->default_download_location_.SetValue(path);
- handler_->SetupDownloadLocationPath();
- }
-}
diff --git a/chrome/browser/ui/webui/options/advanced_options_handler.h b/chrome/browser/ui/webui/options/advanced_options_handler.h
index cae51e8..ef3848df 100644
--- a/chrome/browser/ui/webui/options/advanced_options_handler.h
+++ b/chrome/browser/ui/webui/options/advanced_options_handler.h
@@ -21,37 +21,6 @@ class AdvancedOptionsHandler
public SelectFileDialog::Listener,
public CloudPrintSetupHandlerDelegate {
public:
-
- class DownloadPathChecker
- : public base::RefCountedThreadSafe<DownloadPathChecker> {
- public:
- explicit DownloadPathChecker(AdvancedOptionsHandler* handler);
-
- // Check if the download folder still exists. If the download folder
- // does not exist, the download folder is changed to the user's
- // "Downloads" folder. This check runs in the background and may finish
- // asynchronously after this method returns.
- void CheckIfDownloadPathExists(const FilePath& path);
-
- private:
- friend class base::RefCountedThreadSafe<DownloadPathChecker>;
- ~DownloadPathChecker();
-
- // Called on the FILE thread to check the existence of the download folder.
- // If it does not exist, this method tells the user's "Downloads"
- // to OnDownloadPathChanged().
- void CheckIfDownloadPathExistsOnFileThread(const FilePath& path);
-
- // Called on the UI thread when the FILE thread finds that the download
- // folder does not exist. This method changes the download folder to
- // the user's "Downloads" folder and notifies this preference change
- // to observers.
- void OnDownloadPathChanged(const FilePath path);
-
- // The handler we will report back to.
- AdvancedOptionsHandler* handler_;
- };
-
AdvancedOptionsHandler();
virtual ~AdvancedOptionsHandler();
@@ -164,7 +133,7 @@ class AdvancedOptionsHandler
void SetupFontSizeLabel();
- // Setup the download folder based on user preferences.
+ // Setup the download path based on user preferences.
void SetupDownloadLocationPath();
// Setup the pref whether to prompt for download location every time.
@@ -179,8 +148,6 @@ class AdvancedOptionsHandler
// Setup the checked state for SSL related checkboxes.
void SetupSSLConfigSettings();
- scoped_refptr<DownloadPathChecker> download_path_checker_;
-
scoped_refptr<SelectFileDialog> select_folder_dialog_;
#if !defined(OS_CHROMEOS)