| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/download/download_prefs.h" | 5 #include "chrome/browser/download/download_prefs.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/string_split.h" | 8 #include "base/string_split.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/browser_thread.h" | 12 #include "chrome/browser/browser_thread.h" |
| 13 #include "chrome/browser/download/download_extensions.h" |
| 13 #include "chrome/browser/download/download_util.h" | 14 #include "chrome/browser/download/download_util.h" |
| 14 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
| 15 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 16 | 17 |
| 17 DownloadPrefs::DownloadPrefs(PrefService* prefs) : prefs_(prefs) { | 18 DownloadPrefs::DownloadPrefs(PrefService* prefs) : prefs_(prefs) { |
| 18 prompt_for_download_.Init(prefs::kPromptForDownload, prefs, NULL); | 19 prompt_for_download_.Init(prefs::kPromptForDownload, prefs, NULL); |
| 19 download_path_.Init(prefs::kDownloadDefaultDirectory, prefs, NULL); | 20 download_path_.Init(prefs::kDownloadDefaultDirectory, prefs, NULL); |
| 20 | 21 |
| 21 // We store any file extension that should be opened automatically at | 22 // We store any file extension that should be opened automatically at |
| 22 // download completion in this pref. | 23 // download completion in this pref. |
| 23 std::string extensions_to_open = | 24 std::string extensions_to_open = |
| 24 prefs->GetString(prefs::kDownloadExtensionsToOpen); | 25 prefs->GetString(prefs::kDownloadExtensionsToOpen); |
| 25 std::vector<std::string> extensions; | 26 std::vector<std::string> extensions; |
| 26 base::SplitString(extensions_to_open, ':', &extensions); | 27 base::SplitString(extensions_to_open, ':', &extensions); |
| 27 | 28 |
| 28 for (size_t i = 0; i < extensions.size(); ++i) { | 29 for (size_t i = 0; i < extensions.size(); ++i) { |
| 29 #if defined(OS_POSIX) | 30 #if defined(OS_POSIX) |
| 30 FilePath path(extensions[i]); | 31 FilePath path(extensions[i]); |
| 31 #elif defined(OS_WIN) | 32 #elif defined(OS_WIN) |
| 32 FilePath path(UTF8ToWide(extensions[i])); | 33 FilePath path(UTF8ToWide(extensions[i])); |
| 33 #endif | 34 #endif |
| 34 if (!extensions[i].empty() && !download_util::IsExecutableFile(path)) | 35 if (!extensions[i].empty() && download_util::IsFileSafe(path)) |
| 35 auto_open_.insert(path.value()); | 36 auto_open_.insert(path.value()); |
| 36 } | 37 } |
| 37 } | 38 } |
| 38 | 39 |
| 39 DownloadPrefs::~DownloadPrefs() { | 40 DownloadPrefs::~DownloadPrefs() { |
| 40 SaveAutoOpenState(); | 41 SaveAutoOpenState(); |
| 41 } | 42 } |
| 42 | 43 |
| 43 // static | 44 // static |
| 44 void DownloadPrefs::RegisterUserPrefs(PrefService* prefs) { | 45 void DownloadPrefs::RegisterUserPrefs(PrefService* prefs) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 const FilePath::StringType& extension) const { | 82 const FilePath::StringType& extension) const { |
| 82 return auto_open_.find(extension) != auto_open_.end(); | 83 return auto_open_.find(extension) != auto_open_.end(); |
| 83 } | 84 } |
| 84 | 85 |
| 85 bool DownloadPrefs::EnableAutoOpenBasedOnExtension(const FilePath& file_name) { | 86 bool DownloadPrefs::EnableAutoOpenBasedOnExtension(const FilePath& file_name) { |
| 86 FilePath::StringType extension = file_name.Extension(); | 87 FilePath::StringType extension = file_name.Extension(); |
| 87 if (extension.empty()) | 88 if (extension.empty()) |
| 88 return false; | 89 return false; |
| 89 DCHECK(extension[0] == FilePath::kExtensionSeparator); | 90 DCHECK(extension[0] == FilePath::kExtensionSeparator); |
| 90 extension.erase(0, 1); | 91 extension.erase(0, 1); |
| 91 if (download_util::IsExecutableExtension(extension)) | 92 if (!download_util::IsFileExtensionSafe(extension)) |
| 92 return false; | 93 return false; |
| 93 | 94 |
| 94 auto_open_.insert(extension); | 95 auto_open_.insert(extension); |
| 95 SaveAutoOpenState(); | 96 SaveAutoOpenState(); |
| 96 return true; | 97 return true; |
| 97 } | 98 } |
| 98 | 99 |
| 99 void DownloadPrefs::DisableAutoOpenBasedOnExtension(const FilePath& file_name) { | 100 void DownloadPrefs::DisableAutoOpenBasedOnExtension(const FilePath& file_name) { |
| 100 FilePath::StringType extension = file_name.Extension(); | 101 FilePath::StringType extension = file_name.Extension(); |
| 101 if (extension.empty()) | 102 if (extension.empty()) |
| (...skipping 30 matching lines...) Expand all Loading... |
| 132 extensions.erase(extensions.size() - 1); | 133 extensions.erase(extensions.size() - 1); |
| 133 | 134 |
| 134 prefs_->SetString(prefs::kDownloadExtensionsToOpen, extensions); | 135 prefs_->SetString(prefs::kDownloadExtensionsToOpen, extensions); |
| 135 } | 136 } |
| 136 | 137 |
| 137 bool DownloadPrefs::AutoOpenCompareFunctor::operator()( | 138 bool DownloadPrefs::AutoOpenCompareFunctor::operator()( |
| 138 const FilePath::StringType& a, | 139 const FilePath::StringType& a, |
| 139 const FilePath::StringType& b) const { | 140 const FilePath::StringType& b) const { |
| 140 return FilePath::CompareLessIgnoreCase(a, b); | 141 return FilePath::CompareLessIgnoreCase(a, b); |
| 141 } | 142 } |
| OLD | NEW |