summaryrefslogtreecommitdiffstats
path: root/chrome/browser/download
diff options
context:
space:
mode:
authorkinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-09 04:01:36 +0000
committerkinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-09 04:01:36 +0000
commit290c970f0d8d0322c175cb9f6bef4e653a9862b1 (patch)
treeb98e161fc521596f00b83407435df059e9001b24 /chrome/browser/download
parent50515598543ee038584a9d6f19518842b480a911 (diff)
downloadchromium_src-290c970f0d8d0322c175cb9f6bef4e653a9862b1.zip
chromium_src-290c970f0d8d0322c175cb9f6bef4e653a9862b1.tar.gz
chromium_src-290c970f0d8d0322c175cb9f6bef4e653a9862b1.tar.bz2
Start a download in the user's download directory to lower rename failure possibilities
BUG=8737 TEST=Check 'Ask where to save each file before downloading' true in the 'Under the Hood' preference and start a download. A temporary file org.chromium.XXXXXX or com.google.chrome.XXXXXX should appear in the user's download directory (e.g. ~/Downloads). After selecting the final destination file the temporary file should go away (and be renamed to the final name). Review URL: http://codereview.chromium.org/672020 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@40996 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/download')
-rw-r--r--chrome/browser/download/download_file.cc4
-rw-r--r--chrome/browser/download/download_manager.cc24
-rw-r--r--chrome/browser/download/download_util.cc42
-rw-r--r--chrome/browser/download/download_util.h13
4 files changed, 61 insertions, 22 deletions
diff --git a/chrome/browser/download/download_file.cc b/chrome/browser/download/download_file.cc
index 50af0f5..269d108 100644
--- a/chrome/browser/download/download_file.cc
+++ b/chrome/browser/download/download_file.cc
@@ -13,6 +13,7 @@
#include "build/build_config.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/download/download_manager.h"
+#include "chrome/browser/download/download_util.h"
#include "chrome/browser/net/chrome_url_request_context.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
@@ -77,7 +78,8 @@ DownloadFile::~DownloadFile() {
}
bool DownloadFile::Initialize() {
- if (!full_path_.empty() || file_util::CreateTemporaryFile(&full_path_))
+ if (!full_path_.empty() ||
+ download_util::CreateTemporaryFileForDownload(&full_path_))
return Open();
return false;
}
diff --git a/chrome/browser/download/download_manager.cc b/chrome/browser/download/download_manager.cc
index c89c77b..423f037 100644
--- a/chrome/browser/download/download_manager.cc
+++ b/chrome/browser/download/download_manager.cc
@@ -107,15 +107,6 @@ int GetUniquePathNumber(const FilePath& path) {
return -1;
}
-static bool DownloadPathIsDangerous(const FilePath& download_path) {
- FilePath desktop_dir;
- if (!PathService::Get(chrome::DIR_USER_DESKTOP, &desktop_dir)) {
- NOTREACHED();
- return false;
- }
- return (download_path == desktop_dir);
-}
-
// Used to sort download items based on descending start time.
bool CompareStartTime(DownloadItem* first, DownloadItem* second) {
return first->start_time() > second->start_time();
@@ -348,17 +339,8 @@ void DownloadManager::RegisterUserPrefs(PrefService* prefs) {
prefs->RegisterBooleanPref(prefs::kDownloadDirUpgraded, false);
// The default download path is userprofile\download.
- FilePath default_download_path;
- if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS,
- &default_download_path)) {
- NOTREACHED();
- }
- if (DownloadPathIsDangerous(default_download_path)) {
- if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE,
- &default_download_path)) {
- NOTREACHED();
- }
- }
+ const FilePath& default_download_path =
+ download_util::GetDefaultDownloadDirectory();
prefs->RegisterFilePathPref(prefs::kDownloadDefaultDirectory,
default_download_path);
@@ -369,7 +351,7 @@ void DownloadManager::RegisterUserPrefs(PrefService* prefs) {
if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) {
FilePath current_download_dir = FilePath::FromWStringHack(
prefs->GetString(prefs::kDownloadDefaultDirectory));
- if (DownloadPathIsDangerous(current_download_dir)) {
+ if (download_util::DownloadPathIsDangerous(current_download_dir)) {
prefs->SetString(prefs::kDownloadDefaultDirectory,
default_download_path.ToWStringHack());
}
diff --git a/chrome/browser/download/download_util.cc b/chrome/browser/download/download_util.cc
index 145856d2..7f9b928 100644
--- a/chrome/browser/download/download_util.cc
+++ b/chrome/browser/download/download_util.cc
@@ -14,11 +14,15 @@
#include "base/file_util.h"
#include "base/gfx/rect.h"
#include "base/i18n/time_formatting.h"
+#include "base/path_service.h"
+#include "base/singleton.h"
+#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/download/download_item_model.h"
#include "chrome/browser/download/download_manager.h"
+#include "chrome/common/chrome_paths.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/time_format.h"
#include "grit/generated_resources.h"
@@ -74,6 +78,44 @@ void OpenDownload(DownloadItem* download) {
}
}
+// Download temporary file creation --------------------------------------------
+
+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();
+ }
+ }
+ }
+ friend struct DefaultSingletonTraits<DefaultDownloadDirectory>;
+ FilePath path_;
+};
+
+const FilePath& GetDefaultDownloadDirectory() {
+ return Singleton<DefaultDownloadDirectory>::get()->path();
+}
+
+bool CreateTemporaryFileForDownload(FilePath* temp_file) {
+ return file_util::CreateTemporaryFileInDir(
+ GetDefaultDownloadDirectory(), temp_file);
+}
+
+bool DownloadPathIsDangerous(const FilePath& download_path) {
+ FilePath desktop_dir;
+ if (!PathService::Get(chrome::DIR_USER_DESKTOP, &desktop_dir)) {
+ NOTREACHED();
+ return false;
+ }
+ return (download_path == desktop_dir);
+}
+
// Download progress painting --------------------------------------------------
// Common bitmaps used for download progress animations. We load them once the
diff --git a/chrome/browser/download/download_util.h b/chrome/browser/download/download_util.h
index f14be9a..9ba209b 100644
--- a/chrome/browser/download/download_util.h
+++ b/chrome/browser/download/download_util.h
@@ -25,6 +25,7 @@ class Canvas;
class BaseDownloadItemModel;
class DictionaryValue;
class DownloadItem;
+class FilePath;
class SkBitmap;
namespace download_util {
@@ -55,6 +56,18 @@ bool CanOpenDownload(DownloadItem* download);
// complete if it is in progress).
void OpenDownload(DownloadItem* download);
+// Download temporary file creation --------------------------------------------
+
+// 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);
+
// Download progress animations ------------------------------------------------
// Arc sweep angle for use with downloads of unknown size