diff options
author | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-30 22:16:32 +0000 |
---|---|---|
committer | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-30 22:16:32 +0000 |
commit | e5dc422007e189a20bda164cf67b9a20a753270c (patch) | |
tree | faab861400db3868ae341091271a39e5793d25e0 | |
parent | 6239dc641a11eeb997035a1d8f862b9781f702ed (diff) | |
download | chromium_src-e5dc422007e189a20bda164cf67b9a20a753270c.zip chromium_src-e5dc422007e189a20bda164cf67b9a20a753270c.tar.gz chromium_src-e5dc422007e189a20bda164cf67b9a20a753270c.tar.bz2 |
GTTF: Extract all preferences-related code from DownloadManager to DownloadPrefs.
TEST=unit_tests, browser_tests, ui_tests
BUG=48913
Review URL: http://codereview.chromium.org/3226010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@57916 0039d316-1c4b-4281-b951-d872f2087c98
18 files changed, 254 insertions, 172 deletions
diff --git a/chrome/browser/automation/testing_automation_provider.cc b/chrome/browser/automation/testing_automation_provider.cc index e7abd68..0b95a1f 100644 --- a/chrome/browser/automation/testing_automation_provider.cc +++ b/chrome/browser/automation/testing_automation_provider.cc @@ -25,6 +25,7 @@ #include "chrome/browser/browser_process.h" #include "chrome/browser/browser_window.h" #include "chrome/browser/debugger/devtools_manager.h" +#include "chrome/browser/download/download_prefs.h" #include "chrome/browser/download/download_shelf.h" #include "chrome/browser/find_bar.h" #include "chrome/browser/location_bar.h" @@ -1171,7 +1172,7 @@ void TestingAutomationProvider::GetDownloadDirectory( if (tab_tracker_->ContainsHandle(handle)) { NavigationController* tab = tab_tracker_->GetResource(handle); DownloadManager* dlm = tab->profile()->GetDownloadManager(); - *download_directory = dlm->download_path(); + *download_directory = dlm->download_prefs()->download_path(); } } diff --git a/chrome/browser/cocoa/preferences_window_controller.mm b/chrome/browser/cocoa/preferences_window_controller.mm index 9b0f11a..a2ab751 100644 --- a/chrome/browser/cocoa/preferences_window_controller.mm +++ b/chrome/browser/cocoa/preferences_window_controller.mm @@ -32,6 +32,7 @@ #import "chrome/browser/cocoa/vertical_gradient_view.h" #import "chrome/browser/cocoa/window_size_autosaver.h" #include "chrome/browser/download/download_manager.h" +#include "chrome/browser/download/download_prefs.h" #include "chrome/browser/extensions/extensions_service.h" #include "chrome/browser/metrics/metrics_service.h" #include "chrome/browser/metrics/user_metrics.h" @@ -1518,7 +1519,7 @@ const int kDisabledIndex = 1; } - (IBAction)resetAutoOpenFiles:(id)sender { - profile_->GetDownloadManager()->ResetAutoOpenFiles(); + profile_->GetDownloadManager()->download_prefs()->ResetAutoOpen(); [self recordUserAction:UserMetricsAction("Options_ResetAutoOpenFiles")]; } @@ -1688,7 +1689,7 @@ const int kDisabledIndex = 1; - (BOOL)fileHandlerUIEnabled { if (!profile_->GetDownloadManager()) // Not set in unit tests. return NO; - return profile_->GetDownloadManager()->HasAutoOpenFileTypesRegistered(); + return profile_->GetDownloadManager()->download_prefs()->IsAutoOpenUsed(); } - (void)setFileHandlerUIEnabled:(BOOL)value { diff --git a/chrome/browser/dom_ui/advanced_options_handler.cc b/chrome/browser/dom_ui/advanced_options_handler.cc index fc3c7b2..92d26f2 100644 --- a/chrome/browser/dom_ui/advanced_options_handler.cc +++ b/chrome/browser/dom_ui/advanced_options_handler.cc @@ -11,6 +11,7 @@ #include "base/values.h" #include "chrome/browser/dom_ui/options_managed_banner_handler.h" #include "chrome/browser/download/download_manager.h" +#include "chrome/browser/download/download_prefs.h" #include "chrome/browser/metrics/user_metrics.h" #include "chrome/browser/options_util.h" #include "chrome/browser/options_window.h" @@ -241,7 +242,8 @@ void AdvancedOptionsHandler::HandleAutoOpenButton(const ListValue* args) { NULL); DCHECK(dom_ui_); DownloadManager* manager = dom_ui_->GetProfile()->GetDownloadManager(); - if (manager) manager->ResetAutoOpenFiles(); + if (manager) + manager->download_prefs()->ResetAutoOpen(); } void AdvancedOptionsHandler::HandleResetToDefaults(const ListValue* args) { @@ -302,7 +304,7 @@ void AdvancedOptionsHandler::SetupAutoOpenFileTypesDisabledAttribute() { // We enable the button if the user has any auto-open file types registered. DCHECK(dom_ui_); DownloadManager* manager = dom_ui_->GetProfile()->GetDownloadManager(); - bool disabled = !(manager && manager->HasAutoOpenFileTypesRegistered()); + bool disabled = !(manager && manager->download_prefs()->IsAutoOpenUsed()); FundamentalValue value(disabled); dom_ui_->CallJavascriptFunction( L"options.AdvancedOptions.SetAutoOpenFileTypesDisabledAttribute", value); 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" diff --git a/chrome/browser/gtk/options/advanced_contents_gtk.cc b/chrome/browser/gtk/options/advanced_contents_gtk.cc index 67369a9..50c5752 100644 --- a/chrome/browser/gtk/options/advanced_contents_gtk.cc +++ b/chrome/browser/gtk/options/advanced_contents_gtk.cc @@ -24,6 +24,7 @@ #include "chrome/browser/browser_list.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/download/download_manager.h" +#include "chrome/browser/download/download_prefs.h" #include "chrome/browser/fonts_languages_window.h" #include "chrome/browser/gtk/accessible_widget_helper_gtk.h" #include "chrome/browser/gtk/clear_browsing_data_dialog_gtk.h" @@ -285,8 +286,8 @@ void DownloadSection::NotifyPrefChanged(const std::string* pref_name) { } if (!pref_name || *pref_name == prefs::kDownloadExtensionsToOpen) { - bool enabled = - profile()->GetDownloadManager()->HasAutoOpenFileTypesRegistered(); + DownloadPrefs* prefs = profile()->GetDownloadManager()->download_prefs(); + bool enabled = prefs->IsAutoOpenUsed(); gtk_widget_set_sensitive(reset_file_handlers_label_, enabled); gtk_widget_set_sensitive(reset_file_handlers_button_, enabled); } @@ -333,7 +334,7 @@ void DownloadSection::OnDownloadAskForSaveLocationChanged( // static void DownloadSection::OnResetFileHandlersClicked(GtkButton *button, DownloadSection* section) { - section->profile()->GetDownloadManager()->ResetAutoOpenFiles(); + section->profile()->GetDownloadManager()->download_prefs()->ResetAutoOpen(); section->UserMetricsRecordAction( UserMetricsAction("Options_ResetAutoOpenFiles"), section->profile()->GetPrefs()); diff --git a/chrome/browser/options_util.cc b/chrome/browser/options_util.cc index 91393d5..e63c66e 100644 --- a/chrome/browser/options_util.cc +++ b/chrome/browser/options_util.cc @@ -6,6 +6,7 @@ #include "chrome/browser/browser_process.h" #include "chrome/browser/download/download_manager.h" +#include "chrome/browser/download/download_prefs.h" #include "chrome/browser/geolocation/geolocation_content_settings_map.h" #include "chrome/browser/host_content_settings_map.h" #include "chrome/browser/host_zoom_map.h" @@ -68,7 +69,7 @@ void OptionsUtil::ResetToDefaults(Profile* profile) { prefs::kWebKitSerifFontFamily, prefs::kWebkitTabsToLinks, }; - profile->GetDownloadManager()->ResetAutoOpenFiles(); + profile->GetDownloadManager()->download_prefs()->ResetToDefaults(); profile->GetHostContentSettingsMap()->ResetToDefaults(); profile->GetGeolocationContentSettingsMap()->ResetToDefault(); profile->GetHostZoomMap()->ResetToDefaults(); diff --git a/chrome/browser/prefs/browser_prefs.cc b/chrome/browser/prefs/browser_prefs.cc index ea744ad..3752ddc 100644 --- a/chrome/browser/prefs/browser_prefs.cc +++ b/chrome/browser/prefs/browser_prefs.cc @@ -15,7 +15,7 @@ #include "chrome/browser/dom_ui/labs_ui.h" #include "chrome/browser/dom_ui/new_tab_ui.h" #include "chrome/browser/dom_ui/plugins_ui.h" -#include "chrome/browser/download/download_manager.h" +#include "chrome/browser/download/download_prefs.h" #include "chrome/browser/extensions/extension_dom_ui.h" #include "chrome/browser/extensions/extension_prefs.h" #include "chrome/browser/extensions/extensions_ui.h" @@ -109,7 +109,7 @@ void RegisterUserPrefs(PrefService* user_prefs) { Browser::RegisterUserPrefs(user_prefs); PasswordManager::RegisterUserPrefs(user_prefs); chrome_browser_net::RegisterUserPrefs(user_prefs); - DownloadManager::RegisterUserPrefs(user_prefs); + DownloadPrefs::RegisterUserPrefs(user_prefs); bookmark_utils::RegisterUserPrefs(user_prefs); TabContents::RegisterUserPrefs(user_prefs); TemplateURLPrepopulateData::RegisterUserPrefs(user_prefs); diff --git a/chrome/browser/tab_contents/render_view_context_menu.cc b/chrome/browser/tab_contents/render_view_context_menu.cc index 70f19b0..5b8151d 100644 --- a/chrome/browser/tab_contents/render_view_context_menu.cc +++ b/chrome/browser/tab_contents/render_view_context_menu.cc @@ -28,6 +28,7 @@ #include "chrome/browser/net/browser_url_util.h" #include "chrome/browser/page_info_window.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_view_host.h" diff --git a/chrome/browser/views/options/advanced_contents_view.cc b/chrome/browser/views/options/advanced_contents_view.cc index 665ec53..6270e4f 100644 --- a/chrome/browser/views/options/advanced_contents_view.cc +++ b/chrome/browser/views/options/advanced_contents_view.cc @@ -25,6 +25,7 @@ #include "chrome/browser/browser_list.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/download/download_manager.h" +#include "chrome/browser/download/download_prefs.h" #include "chrome/browser/gears_integration.h" #include "chrome/browser/net/predictor_api.h" #include "chrome/browser/options_util.h" @@ -1103,7 +1104,7 @@ void DownloadSection::ButtonPressed( } ask_for_save_location_.SetValue(enabled); } else if (sender == reset_file_handlers_button_) { - profile()->GetDownloadManager()->ResetAutoOpenFiles(); + profile()->GetDownloadManager()->download_prefs()->ResetAutoOpen(); UserMetricsRecordAction(UserMetricsAction("Options_ResetAutoOpenFiles"), profile()->GetPrefs()); } @@ -1200,7 +1201,7 @@ void DownloadSection::NotifyPrefChanged(const std::string* pref_name) { if (!pref_name || *pref_name == prefs::kDownloadExtensionsToOpen) { bool enabled = - profile()->GetDownloadManager()->HasAutoOpenFileTypesRegistered(); + profile()->GetDownloadManager()->download_prefs()->IsAutoOpenUsed(); reset_file_handlers_label_->SetEnabled(enabled); reset_file_handlers_button_->SetEnabled(enabled); } diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index c5143df..9d8dcab 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1296,6 +1296,8 @@ 'browser/download/download_item_model.h', 'browser/download/download_manager.cc', 'browser/download/download_manager.h', + 'browser/download/download_prefs.cc', + 'browser/download/download_prefs.h', 'browser/download/download_request_infobar_delegate.h', 'browser/download/download_request_infobar_delegate.cc', 'browser/download/download_request_limiter.cc', |