diff options
Diffstat (limited to 'chrome/browser/download')
-rw-r--r-- | chrome/browser/download/download_exe.cc | 4 | ||||
-rw-r--r-- | chrome/browser/download/download_item.cc | 9 | ||||
-rw-r--r-- | chrome/browser/download/download_manager.cc | 124 | ||||
-rw-r--r-- | chrome/browser/download/download_manager.h | 43 | ||||
-rw-r--r-- | chrome/browser/download/download_manager_unittest.cc | 5 | ||||
-rw-r--r-- | chrome/browser/download/download_prefs.cc | 137 | ||||
-rw-r--r-- | chrome/browser/download/download_prefs.h | 64 | ||||
-rw-r--r-- | chrome/browser/download/download_util.h | 3 | ||||
-rw-r--r-- | chrome/browser/download/save_package.cc | 1 |
9 files changed, 231 insertions, 159 deletions
diff --git a/chrome/browser/download/download_exe.cc b/chrome/browser/download/download_exe.cc index 5629e3f..c70b534 100644 --- a/chrome/browser/download/download_exe.cc +++ b/chrome/browser/download/download_exe.cc @@ -169,6 +169,10 @@ static const char* const g_executables[] = { #endif }; +bool IsExecutableFile(const FilePath& path) { + return IsExecutableExtension(path.Extension()); +} + bool IsExecutableExtension(const FilePath::StringType& extension) { if (extension.empty()) return false; diff --git a/chrome/browser/download/download_item.cc b/chrome/browser/download/download_item.cc index fb6f854..4422600 100644 --- a/chrome/browser/download/download_item.cc +++ b/chrome/browser/download/download_item.cc @@ -13,6 +13,7 @@ #include "chrome/browser/chrome_thread.h" #include "chrome/browser/download/download_history.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/history/download_types.h" #include "chrome/browser/prefs/pref_service.h" @@ -168,7 +169,7 @@ bool DownloadItem::CanOpenDownload() { file_to_use = original_name(); return !Extension::IsExtension(file_to_use) && - !download_manager_->IsExecutableFile(file_to_use); + !download_util::IsExecutableFile(file_to_use); } bool DownloadItem::ShouldOpenFileBasedOnExtension() { @@ -176,7 +177,11 @@ bool DownloadItem::ShouldOpenFileBasedOnExtension() { } void DownloadItem::OpenFilesBasedOnExtension(bool open) { - return download_manager_->OpenFilesBasedOnExtension(full_path(), open); + DownloadPrefs* prefs = download_manager_->download_prefs(); + if (open) + prefs->EnableAutoOpenBasedOnExtension(full_path()); + else + prefs->DisableAutoOpenBasedOnExtension(full_path()); } void DownloadItem::OpenDownload() { diff --git a/chrome/browser/download/download_manager.cc b/chrome/browser/download/download_manager.cc index 9d5a1a7..d1ec883 100644 --- a/chrome/browser/download/download_manager.cc +++ b/chrome/browser/download/download_manager.cc @@ -22,6 +22,7 @@ #include "chrome/browser/download/download_file_manager.h" #include "chrome/browser/download/download_history.h" #include "chrome/browser/download/download_item.h" +#include "chrome/browser/download/download_prefs.h" #include "chrome/browser/download/download_util.h" #include "chrome/browser/extensions/extensions_service.h" #include "chrome/browser/history/download_types.h" @@ -56,39 +57,6 @@ bool CompareStartTime(DownloadItem* first, DownloadItem* second) { } // namespace -// static -void DownloadManager::RegisterUserPrefs(PrefService* prefs) { - prefs->RegisterBooleanPref(prefs::kPromptForDownload, false); - prefs->RegisterStringPref(prefs::kDownloadExtensionsToOpen, ""); - prefs->RegisterBooleanPref(prefs::kDownloadDirUpgraded, false); - - // The default download path is userprofile\download. - const FilePath& default_download_path = - download_util::GetDefaultDownloadDirectory(); - prefs->RegisterFilePathPref(prefs::kDownloadDefaultDirectory, - default_download_path); -#if defined(OS_CHROMEOS) - // Ensure that the download directory specified in the preferences exists. - ChromeThread::PostTask( - ChromeThread::FILE, FROM_HERE, - NewRunnableFunction(&file_util::CreateDirectory, default_download_path)); -#endif - - // If the download path is dangerous we forcefully reset it. But if we do - // so we set a flag to make sure we only do it once, to avoid fighting - // the user if he really wants it on an unsafe place such as the desktop. - - if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) { - FilePath current_download_dir = prefs->GetFilePath( - prefs::kDownloadDefaultDirectory); - if (download_util::DownloadPathIsDangerous(current_download_dir)) { - prefs->SetFilePath(prefs::kDownloadDefaultDirectory, - default_download_path); - } - prefs->SetBoolean(prefs::kDownloadDirUpgraded, true); - } -} - DownloadManager::DownloadManager() : shutdown_needed_(false), profile_(NULL), @@ -157,9 +125,6 @@ void DownloadManager::Shutdown() { file_manager_ = NULL; - // Save our file extensions to auto open. - SaveAutoOpens(); - // Make sure the save as dialog doesn't notify us back if we're gone before // it returns. if (select_file_dialog_.get()) @@ -249,6 +214,8 @@ bool DownloadManager::Init(Profile* profile) { download_history_->Load( NewCallback(this, &DownloadManager::OnQueryDownloadEntriesComplete)); + download_prefs_.reset(new DownloadPrefs(profile_->GetPrefs())); + // In test mode, there may be no ResourceDispatcherHost. In this case it's // safe to avoid setting |file_manager_| because we only call a small set of // functions, none of which need it. @@ -258,34 +225,6 @@ bool DownloadManager::Init(Profile* profile) { DCHECK(file_manager_); } - // Get our user preference state. - PrefService* prefs = profile_->GetPrefs(); - DCHECK(prefs); - prompt_for_download_.Init(prefs::kPromptForDownload, prefs, NULL); - - download_path_.Init(prefs::kDownloadDefaultDirectory, prefs, NULL); - // Ensure that the download directory specified in the preferences exists. - ChromeThread::PostTask( - ChromeThread::FILE, FROM_HERE, - NewRunnableFunction(&file_util::CreateDirectory, download_path())); - - // We store any file extension that should be opened automatically at - // download completion in this pref. - std::string extensions_to_open = - prefs->GetString(prefs::kDownloadExtensionsToOpen); - std::vector<std::string> extensions; - SplitString(extensions_to_open, ':', &extensions); - - for (size_t i = 0; i < extensions.size(); ++i) { -#if defined(OS_POSIX) - FilePath path(extensions[i]); -#elif defined(OS_WIN) - FilePath path(UTF8ToWide(extensions[i])); -#endif - if (!extensions[i].empty() && !IsExecutableFile(path)) - auto_open_.insert(path.value()); - } - other_download_manager_observer_.reset( new OtherDownloadManagerObserver(this)); @@ -318,7 +257,7 @@ void DownloadManager::StartDownload(DownloadCreateInfo* info) { // Freeze the user's preference for showing a Save As dialog. We're going // to bounce around a bunch of threads and we don't want to worry about race // conditions where the user changes this pref out from under us. - if (*prompt_for_download_) { + if (download_prefs_->prompt_for_download()) { // But ignore the user's preference for the following scenarios: // 1) Extension installation. Note that we only care here about the case // where an extension is installed, not when one is downloaded with @@ -336,7 +275,7 @@ void DownloadManager::StartDownload(DownloadCreateInfo* info) { if (info->prompt_user_for_save_location && !last_download_path_.empty()){ info->suggested_path = last_download_path_; } else { - info->suggested_path = download_path(); + info->suggested_path = download_prefs_->download_path(); } info->suggested_path = info->suggested_path.Append(generated_name); } else { @@ -348,7 +287,7 @@ void DownloadManager::StartDownload(DownloadCreateInfo* info) { // Downloads can be marked as dangerous for two reasons: // a) They have a dangerous-looking filename // b) They are an extension that is not from the gallery - if (IsExecutableFile(info->suggested_path.BaseName())) + if (download_util::IsExecutableFile(info->suggested_path.BaseName())) info->is_dangerous = true; else if (info->is_extension_install && !ExtensionsService::IsDownloadFromGallery(info->url, @@ -949,20 +888,6 @@ void DownloadManager::OpenDownloadInShell(DownloadItem* download, #endif } -void DownloadManager::OpenFilesBasedOnExtension( - const FilePath& path, bool open) { - FilePath::StringType extension = path.Extension(); - if (extension.empty()) - return; - DCHECK(extension[0] == FilePath::kExtensionSeparator); - extension.erase(0, 1); - if (open && !download_util::IsExecutableExtension(extension)) - auto_open_.insert(extension); - else - auto_open_.erase(extension); - SaveAutoOpens(); -} - bool DownloadManager::ShouldOpenFileBasedOnExtension( const FilePath& path) const { FilePath::StringType extension = path.Extension(); @@ -974,42 +899,7 @@ bool DownloadManager::ShouldOpenFileBasedOnExtension( return false; DCHECK(extension[0] == FilePath::kExtensionSeparator); extension.erase(0, 1); - if (auto_open_.find(extension) != auto_open_.end()) - return true; - return false; -} - -bool DownloadManager::IsExecutableFile(const FilePath& path) const { - return download_util::IsExecutableExtension(path.Extension()); -} - -void DownloadManager::ResetAutoOpenFiles() { - auto_open_.clear(); - SaveAutoOpens(); -} - -bool DownloadManager::HasAutoOpenFileTypesRegistered() const { - return !auto_open_.empty(); -} - -void DownloadManager::SaveAutoOpens() { - PrefService* prefs = profile_->GetPrefs(); - if (prefs) { - std::string extensions; - for (AutoOpenSet::iterator it = auto_open_.begin(); - it != auto_open_.end(); ++it) { -#if defined(OS_POSIX) - std::string this_extension = *it; -#elif defined(OS_WIN) - std::string this_extension = base::SysWideToUTF8(*it); -#endif - extensions += this_extension + ":"; - } - if (!extensions.empty()) - extensions.erase(extensions.size() - 1); - - prefs->SetString(prefs::kDownloadExtensionsToOpen, extensions); - } + return download_prefs_->IsAutoOpenEnabledForExtension(extension); } void DownloadManager::FileSelected(const FilePath& path, diff --git a/chrome/browser/download/download_manager.h b/chrome/browser/download/download_manager.h index 86202e0..9d2e9e1 100644 --- a/chrome/browser/download/download_manager.h +++ b/chrome/browser/download/download_manager.h @@ -39,14 +39,13 @@ #include "base/ref_counted.h" #include "base/scoped_ptr.h" #include "base/time.h" -#include "chrome/browser/prefs/pref_member.h" #include "chrome/browser/shell_dialogs.h" class DownloadFileManager; class DownloadHistory; class DownloadItem; +class DownloadPrefs; class GURL; -class PrefService; class Profile; class ResourceDispatcherHost; class URLRequestContextGetter; @@ -64,8 +63,6 @@ class DownloadManager : public base::RefCountedThreadSafe<DownloadManager>, public: DownloadManager(); - static void RegisterUserPrefs(PrefService* prefs); - // Interface to implement for observers that wish to be informed of changes // to the DownloadManager's collection of downloads. class Observer { @@ -175,33 +172,18 @@ class DownloadManager : public base::RefCountedThreadSafe<DownloadManager>, return static_cast<int>(in_progress_.size()); } - FilePath download_path() { return *download_path_; } - Profile* profile() { return profile_; } DownloadHistory* download_history() { return download_history_.get(); } + DownloadPrefs* download_prefs() { return download_prefs_.get(); } + // Clears the last download path, used to initialize "save as" dialogs. void ClearLastDownloadPath(); - // Registers this file extension for automatic opening upon download - // completion if 'open' is true, or prevents the extension from automatic - // opening if 'open' is false. - void OpenFilesBasedOnExtension(const FilePath& path, bool open); - // Tests if a file type should be opened automatically. bool ShouldOpenFileBasedOnExtension(const FilePath& path) const; - // Tests if a file is considered executable, based on its type. - bool IsExecutableFile(const FilePath& path) const; - - // Resets the automatic open preference. - void ResetAutoOpenFiles(); - - // Returns true if there are automatic handlers registered for any file - // types. - bool HasAutoOpenFileTypesRegistered() const; - // Overridden from SelectFileDialog::Listener: virtual void FileSelected(const FilePath& path, int index, void* params); virtual void FileSelectionCanceled(void* params); @@ -258,9 +240,6 @@ class DownloadManager : public base::RefCountedThreadSafe<DownloadManager>, void ContinueStartDownload(DownloadCreateInfo* info, const FilePath& target_path); - // Persist the automatic opening preference. - void SaveAutoOpens(); - // Download cancel helper function. void DownloadCancelledInternal(int download_id, int render_process_id, @@ -341,27 +320,15 @@ class DownloadManager : public base::RefCountedThreadSafe<DownloadManager>, scoped_ptr<DownloadHistory> download_history_; + scoped_ptr<DownloadPrefs> download_prefs_; + // Non-owning pointer for handling file writing on the download_thread_. DownloadFileManager* file_manager_; - // User preferences - BooleanPrefMember prompt_for_download_; - FilePathPrefMember download_path_; - // The user's last choice for download directory. This is only used when the // user wants us to prompt for a save location for each download. FilePath last_download_path_; - // Set of file extensions to open at download completion. - struct AutoOpenCompareFunctor { - inline bool operator()(const FilePath::StringType& a, - const FilePath::StringType& b) const { - return FilePath::CompareLessIgnoreCase(a, b); - } - }; - typedef std::set<FilePath::StringType, AutoOpenCompareFunctor> AutoOpenSet; - AutoOpenSet auto_open_; - // Keep track of downloads that are completed before the user selects the // destination, so that observers are appropriately notified of completion // after this determination is made. diff --git a/chrome/browser/download/download_manager_unittest.cc b/chrome/browser/download/download_manager_unittest.cc index 8a2ed52..d63edc0 100644 --- a/chrome/browser/download/download_manager_unittest.cc +++ b/chrome/browser/download/download_manager_unittest.cc @@ -10,6 +10,7 @@ #include "chrome/browser/download/download_file.h" #include "chrome/browser/download/download_file_manager.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/history/download_types.h" #include "chrome/browser/prefs/pref_service.h" @@ -117,8 +118,8 @@ const struct { TEST_F(DownloadManagerTest, StartDownload) { PrefService* prefs = profile_->GetPrefs(); prefs->SetFilePath(prefs::kDownloadDefaultDirectory, FilePath()); - download_manager_->OpenFilesBasedOnExtension( - FilePath(FILE_PATH_LITERAL("example.pdf")), true); + download_manager_->download_prefs()->EnableAutoOpenBasedOnExtension( + FilePath(FILE_PATH_LITERAL("example.pdf"))); for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kStartDownloadCases); ++i) { prefs->SetBoolean(prefs::kPromptForDownload, diff --git a/chrome/browser/download/download_prefs.cc b/chrome/browser/download/download_prefs.cc new file mode 100644 index 0000000..ece1a57 --- /dev/null +++ b/chrome/browser/download/download_prefs.cc @@ -0,0 +1,137 @@ +// 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. + +#include "chrome/browser/download/download_prefs.h" + +#include "base/file_util.h" +#include "base/string_util.h" +#include "base/sys_string_conversions.h" +#include "base/utf_string_conversions.h" +#include "chrome/browser/download/download_util.h" +#include "chrome/browser/prefs/pref_service.h" +#include "chrome/common/pref_names.h" + +DownloadPrefs::DownloadPrefs(PrefService* prefs) : prefs_(prefs) { + prompt_for_download_.Init(prefs::kPromptForDownload, prefs, NULL); + download_path_.Init(prefs::kDownloadDefaultDirectory, prefs, NULL); + + // We store any file extension that should be opened automatically at + // download completion in this pref. + std::string extensions_to_open = + prefs->GetString(prefs::kDownloadExtensionsToOpen); + std::vector<std::string> extensions; + SplitString(extensions_to_open, ':', &extensions); + + for (size_t i = 0; i < extensions.size(); ++i) { +#if defined(OS_POSIX) + FilePath path(extensions[i]); +#elif defined(OS_WIN) + FilePath path(UTF8ToWide(extensions[i])); +#endif + if (!extensions[i].empty() && !download_util::IsExecutableFile(path)) + auto_open_.insert(path.value()); + } +} + +DownloadPrefs::~DownloadPrefs() { + SaveAutoOpenState(); +} + +// static +void DownloadPrefs::RegisterUserPrefs(PrefService* prefs) { + prefs->RegisterBooleanPref(prefs::kPromptForDownload, false); + prefs->RegisterStringPref(prefs::kDownloadExtensionsToOpen, ""); + prefs->RegisterBooleanPref(prefs::kDownloadDirUpgraded, false); + + // The default download path is userprofile\download. + const FilePath& default_download_path = + download_util::GetDefaultDownloadDirectory(); + prefs->RegisterFilePathPref(prefs::kDownloadDefaultDirectory, + default_download_path); + + // Ensure that the download directory specified in the preferences exists. + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, + NewRunnableFunction(&file_util::CreateDirectory, default_download_path)); + + // If the download path is dangerous we forcefully reset it. But if we do + // so we set a flag to make sure we only do it once, to avoid fighting + // the user if he really wants it on an unsafe place such as the desktop. + if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) { + FilePath current_download_dir = prefs->GetFilePath( + prefs::kDownloadDefaultDirectory); + if (download_util::DownloadPathIsDangerous(current_download_dir)) { + prefs->SetFilePath(prefs::kDownloadDefaultDirectory, + default_download_path); + } + prefs->SetBoolean(prefs::kDownloadDirUpgraded, true); + } +} + +bool DownloadPrefs::IsAutoOpenUsed() const { + return !auto_open_.empty(); +} + +bool DownloadPrefs::IsAutoOpenEnabledForExtension( + const FilePath::StringType& extension) const { + return auto_open_.find(extension) != auto_open_.end(); +} + +bool DownloadPrefs::EnableAutoOpenBasedOnExtension(const FilePath& file_path) { + FilePath::StringType extension = file_path.Extension(); + if (extension.empty()) + return false; + DCHECK(extension[0] == FilePath::kExtensionSeparator); + extension.erase(0, 1); + if (download_util::IsExecutableExtension(extension)) + return false; + + auto_open_.insert(extension); + SaveAutoOpenState(); + return true; +} + +void DownloadPrefs::DisableAutoOpenBasedOnExtension(const FilePath& file_path) { + FilePath::StringType extension = file_path.Extension(); + if (extension.empty()) + return; + DCHECK(extension[0] == FilePath::kExtensionSeparator); + extension.erase(0, 1); + auto_open_.erase(extension); + SaveAutoOpenState(); +} + +void DownloadPrefs::ResetToDefaults() { + // TODO(phajdan.jr): Should we reset rest of prefs here? + ResetAutoOpen(); +} + +void DownloadPrefs::ResetAutoOpen() { + auto_open_.clear(); + SaveAutoOpenState(); +} + +void DownloadPrefs::SaveAutoOpenState() { + std::string extensions; + for (AutoOpenSet::iterator it = auto_open_.begin(); + it != auto_open_.end(); ++it) { +#if defined(OS_POSIX) + std::string this_extension = *it; +#elif defined(OS_WIN) + // TODO(phajdan.jr): Why we're using Sys conversion here, but not in ctor? + std::string this_extension = base::SysWideToUTF8(*it); +#endif + extensions += this_extension + ":"; + } + if (!extensions.empty()) + extensions.erase(extensions.size() - 1); + + prefs_->SetString(prefs::kDownloadExtensionsToOpen, extensions); +} + +bool DownloadPrefs::AutoOpenCompareFunctor::operator()( + const FilePath::StringType& a, + const FilePath::StringType& b) const { + return FilePath::CompareLessIgnoreCase(a, b); +} diff --git a/chrome/browser/download/download_prefs.h b/chrome/browser/download/download_prefs.h new file mode 100644 index 0000000..b3b5823 --- /dev/null +++ b/chrome/browser/download/download_prefs.h @@ -0,0 +1,64 @@ +// 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. + +#ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_PREFS_H_ +#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_PREFS_H_ +#pragma once + +#include <set> + +#include "base/file_path.h" +#include "chrome/browser/prefs/pref_member.h" + +class PrefService; + +// Stores all download-related preferences. +class DownloadPrefs { + public: + explicit DownloadPrefs(PrefService* prefs); + ~DownloadPrefs(); + + static void RegisterUserPrefs(PrefService* prefs); + + // TODO(phajdan.jr): Make these accessors const. + bool prompt_for_download() { return *prompt_for_download_; } + FilePath download_path() { return *download_path_; } + + // Returns true if there is at least one file extension registered + // for auto-open. + bool IsAutoOpenUsed() const; + + bool IsAutoOpenEnabledForExtension( + const FilePath::StringType& extension) const; + + // Enables auto-open based on file extension. Returns true on success. + // TODO(phajdan.jr): Add WARN_UNUSED_RESULT here. + bool EnableAutoOpenBasedOnExtension(const FilePath& file_path); + + // Disables auto-open based on file extension. + void DisableAutoOpenBasedOnExtension(const FilePath& file_path); + + void ResetToDefaults(); + void ResetAutoOpen(); + + private: + void SaveAutoOpenState(); + + PrefService* prefs_; + + BooleanPrefMember prompt_for_download_; + FilePathPrefMember download_path_; + + // Set of file extensions to open at download completion. + struct AutoOpenCompareFunctor { + bool operator()(const FilePath::StringType& a, + const FilePath::StringType& b) const; + }; + typedef std::set<FilePath::StringType, AutoOpenCompareFunctor> AutoOpenSet; + AutoOpenSet auto_open_; + + DISALLOW_COPY_AND_ASSIGN(DownloadPrefs); +}; + +#endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_PREFS_H_ diff --git a/chrome/browser/download/download_util.h b/chrome/browser/download/download_util.h index 73365c3..37e8a82 100644 --- a/chrome/browser/download/download_util.h +++ b/chrome/browser/download/download_util.h @@ -152,6 +152,9 @@ void DragDownload(const DownloadItem* download, // Executable file support ----------------------------------------------------- +// Tests if a file is considered executable, based on its type. +bool IsExecutableFile(const FilePath& path); + // Determine if the specified extension is an executable extension. bool IsExecutableExtension(const FilePath::StringType& extension); diff --git a/chrome/browser/download/save_package.cc b/chrome/browser/download/save_package.cc index 2ce9283..190ba29 100644 --- a/chrome/browser/download/save_package.cc +++ b/chrome/browser/download/save_package.cc @@ -27,6 +27,7 @@ #include "chrome/browser/download/save_item.h" #include "chrome/browser/net/url_fixer_upper.h" #include "chrome/browser/platform_util.h" +#include "chrome/browser/prefs/pref_member.h" #include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/profile.h" #include "chrome/browser/renderer_host/render_process_host.h" |