OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 save_file_path_.Init(prefs::kSaveFileDefaultDirectory, prefs); | 155 save_file_path_.Init(prefs::kSaveFileDefaultDirectory, prefs); |
156 save_file_type_.Init(prefs::kSaveFileType, prefs); | 156 save_file_type_.Init(prefs::kSaveFileType, prefs); |
157 | 157 |
158 // We store any file extension that should be opened automatically at | 158 // We store any file extension that should be opened automatically at |
159 // download completion in this pref. | 159 // download completion in this pref. |
160 std::string extensions_to_open = | 160 std::string extensions_to_open = |
161 prefs->GetString(prefs::kDownloadExtensionsToOpen); | 161 prefs->GetString(prefs::kDownloadExtensionsToOpen); |
162 std::vector<std::string> extensions; | 162 std::vector<std::string> extensions; |
163 base::SplitString(extensions_to_open, ':', &extensions); | 163 base::SplitString(extensions_to_open, ':', &extensions); |
164 | 164 |
165 for (size_t i = 0; i < extensions.size(); ++i) { | 165 for (const auto& extension_string : extensions) { |
166 #if defined(OS_POSIX) | 166 #if defined(OS_POSIX) |
167 base::FilePath path(extensions[i]); | 167 base::FilePath::StringType extension = extension_string; |
168 #elif defined(OS_WIN) | 168 #elif defined(OS_WIN) |
169 base::FilePath path(base::UTF8ToWide(extensions[i])); | 169 base::FilePath::StringType extension = base::UTF8ToWide(extension_string); |
170 #endif | 170 #endif |
171 if (!extensions[i].empty() && | 171 // If it's empty or malformed or not allowed to open automatically, then |
172 download_util::GetFileDangerLevel(path) == download_util::NOT_DANGEROUS) | 172 // skip the entry. Any such entries will be dropped from preferences the |
173 auto_open_.insert(path.value()); | 173 // next time SaveAutoOpenState() is called. |
| 174 if (extension.empty() || |
| 175 *extension.begin() == base::FilePath::kExtensionSeparator) |
| 176 continue; |
| 177 // Construct something like ".<extension>", since |
| 178 // IsAllowedToOpenAutomatically() needs a filename. |
| 179 base::FilePath filename_with_extension = base::FilePath( |
| 180 base::FilePath::StringType(1, base::FilePath::kExtensionSeparator) + |
| 181 extension); |
| 182 |
| 183 // Note that the list of file types that are not allowed to open |
| 184 // automatically can change in the future. When the list is tightened, it is |
| 185 // expected that some entries in the users' auto open list will get dropped |
| 186 // permanently as a result. |
| 187 if (download_util::IsAllowedToOpenAutomatically(filename_with_extension)) |
| 188 auto_open_.insert(extension); |
174 } | 189 } |
175 } | 190 } |
176 | 191 |
177 DownloadPrefs::~DownloadPrefs() {} | 192 DownloadPrefs::~DownloadPrefs() {} |
178 | 193 |
179 // static | 194 // static |
180 void DownloadPrefs::RegisterProfilePrefs( | 195 void DownloadPrefs::RegisterProfilePrefs( |
181 user_prefs::PrefRegistrySyncable* registry) { | 196 user_prefs::PrefRegistrySyncable* registry) { |
182 registry->RegisterBooleanPref( | 197 registry->RegisterBooleanPref( |
183 prefs::kPromptForDownload, | 198 prefs::kPromptForDownload, |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 base::FilePath::StringType extension = path.Extension(); | 299 base::FilePath::StringType extension = path.Extension(); |
285 if (extension.empty()) | 300 if (extension.empty()) |
286 return false; | 301 return false; |
287 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); | 302 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); |
288 extension.erase(0, 1); | 303 extension.erase(0, 1); |
289 #if defined(OS_WIN) || defined(OS_LINUX) || \ | 304 #if defined(OS_WIN) || defined(OS_LINUX) || \ |
290 (defined(OS_MACOSX) && !defined(OS_IOS)) | 305 (defined(OS_MACOSX) && !defined(OS_IOS)) |
291 if (extension == FILE_PATH_LITERAL("pdf") && ShouldOpenPdfInSystemReader()) | 306 if (extension == FILE_PATH_LITERAL("pdf") && ShouldOpenPdfInSystemReader()) |
292 return true; | 307 return true; |
293 #endif | 308 #endif |
| 309 |
294 return auto_open_.find(extension) != auto_open_.end(); | 310 return auto_open_.find(extension) != auto_open_.end(); |
295 } | 311 } |
296 | 312 |
297 bool DownloadPrefs::EnableAutoOpenBasedOnExtension( | 313 bool DownloadPrefs::EnableAutoOpenBasedOnExtension( |
298 const base::FilePath& file_name) { | 314 const base::FilePath& file_name) { |
299 base::FilePath::StringType extension = file_name.Extension(); | 315 base::FilePath::StringType extension = file_name.Extension(); |
300 if (extension.empty()) | 316 if (!download_util::IsAllowedToOpenAutomatically(file_name)) |
301 return false; | 317 return false; |
| 318 |
302 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); | 319 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); |
303 extension.erase(0, 1); | 320 extension.erase(0, 1); |
304 | 321 |
305 auto_open_.insert(extension); | 322 auto_open_.insert(extension); |
306 SaveAutoOpenState(); | 323 SaveAutoOpenState(); |
307 return true; | 324 return true; |
308 } | 325 } |
309 | 326 |
310 void DownloadPrefs::DisableAutoOpenBasedOnExtension( | 327 void DownloadPrefs::DisableAutoOpenBasedOnExtension( |
311 const base::FilePath& file_name) { | 328 const base::FilePath& file_name) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 extensions.erase(extensions.size() - 1); | 381 extensions.erase(extensions.size() - 1); |
365 | 382 |
366 profile_->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, extensions); | 383 profile_->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, extensions); |
367 } | 384 } |
368 | 385 |
369 bool DownloadPrefs::AutoOpenCompareFunctor::operator()( | 386 bool DownloadPrefs::AutoOpenCompareFunctor::operator()( |
370 const base::FilePath::StringType& a, | 387 const base::FilePath::StringType& a, |
371 const base::FilePath::StringType& b) const { | 388 const base::FilePath::StringType& b) const { |
372 return base::FilePath::CompareLessIgnoreCase(a, b); | 389 return base::FilePath::CompareLessIgnoreCase(a, b); |
373 } | 390 } |
OLD | NEW |