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