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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 // Ensure that the default download directory exists. | 127 // Ensure that the default download directory exists. |
128 BrowserThread::PostTask( | 128 BrowserThread::PostTask( |
129 BrowserThread::FILE, FROM_HERE, | 129 BrowserThread::FILE, FROM_HERE, |
130 base::Bind(base::IgnoreResult(&base::CreateDirectory), | 130 base::Bind(base::IgnoreResult(&base::CreateDirectory), |
131 GetDefaultDownloadDirectoryForProfile())); | 131 GetDefaultDownloadDirectoryForProfile())); |
132 #endif // defined(OS_CHROMEOS) | 132 #endif // defined(OS_CHROMEOS) |
133 | 133 |
134 #if defined(OS_WIN) | 134 #if defined(OS_WIN) |
135 should_open_pdf_in_adobe_reader_ = | 135 should_open_pdf_in_adobe_reader_ = |
136 prefs->GetBoolean(prefs::kOpenPdfDownloadInAdobeReader); | 136 prefs->GetBoolean(prefs::kOpenPdfDownloadInAdobeReader); |
| 137 #elif defined(OS_MACOSX) || defined(OS_LINUX) |
| 138 should_open_pdf_in_system_reader_ = |
| 139 prefs->GetBoolean(prefs::kOpenPdfDownloadInSystemReader); |
137 #endif | 140 #endif |
138 | 141 |
139 // If the download path is dangerous we forcefully reset it. But if we do | 142 // If the download path is dangerous we forcefully reset it. But if we do |
140 // so we set a flag to make sure we only do it once, to avoid fighting | 143 // so we set a flag to make sure we only do it once, to avoid fighting |
141 // the user if he really wants it on an unsafe place such as the desktop. | 144 // the user if he really wants it on an unsafe place such as the desktop. |
142 if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) { | 145 if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) { |
143 base::FilePath current_download_dir = prefs->GetFilePath( | 146 base::FilePath current_download_dir = prefs->GetFilePath( |
144 prefs::kDownloadDefaultDirectory); | 147 prefs::kDownloadDefaultDirectory); |
145 if (DownloadPathIsDangerous(current_download_dir)) { | 148 if (DownloadPathIsDangerous(current_download_dir)) { |
146 prefs->SetFilePath(prefs::kDownloadDefaultDirectory, | 149 prefs->SetFilePath(prefs::kDownloadDefaultDirectory, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 205 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
203 registry->RegisterFilePathPref( | 206 registry->RegisterFilePathPref( |
204 prefs::kSaveFileDefaultDirectory, | 207 prefs::kSaveFileDefaultDirectory, |
205 default_download_path, | 208 default_download_path, |
206 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 209 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
207 #if defined(OS_WIN) | 210 #if defined(OS_WIN) |
208 registry->RegisterBooleanPref( | 211 registry->RegisterBooleanPref( |
209 prefs::kOpenPdfDownloadInAdobeReader, | 212 prefs::kOpenPdfDownloadInAdobeReader, |
210 false, | 213 false, |
211 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 214 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 215 #elif defined(OS_MACOSX) || defined(OS_LINUX) |
| 216 registry->RegisterBooleanPref( |
| 217 prefs::kOpenPdfDownloadInSystemReader, |
| 218 false, |
| 219 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
212 #endif | 220 #endif |
213 } | 221 } |
214 | 222 |
215 base::FilePath DownloadPrefs::GetDefaultDownloadDirectoryForProfile() const { | 223 base::FilePath DownloadPrefs::GetDefaultDownloadDirectoryForProfile() const { |
216 #if defined(OS_CHROMEOS) | 224 #if defined(OS_CHROMEOS) |
217 return file_manager::util::GetDownloadsFolderForProfile(profile_); | 225 return file_manager::util::GetDownloadsFolderForProfile(profile_); |
218 #else | 226 #else |
219 return GetDefaultDownloadDirectory(); | 227 return GetDefaultDownloadDirectory(); |
220 #endif | 228 #endif |
221 } | 229 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 } | 288 } |
281 | 289 |
282 bool DownloadPrefs::IsDownloadPathManaged() const { | 290 bool DownloadPrefs::IsDownloadPathManaged() const { |
283 return download_path_.IsManaged(); | 291 return download_path_.IsManaged(); |
284 } | 292 } |
285 | 293 |
286 bool DownloadPrefs::IsAutoOpenUsed() const { | 294 bool DownloadPrefs::IsAutoOpenUsed() const { |
287 #if defined(OS_WIN) | 295 #if defined(OS_WIN) |
288 if (ShouldOpenPdfInAdobeReader()) | 296 if (ShouldOpenPdfInAdobeReader()) |
289 return true; | 297 return true; |
| 298 #elif defined(OS_MACOSX) || defined(OS_LINUX) |
| 299 if (ShouldOpenPdfInSystemReader()) |
| 300 return true; |
290 #endif | 301 #endif |
291 return !auto_open_.empty(); | 302 return !auto_open_.empty(); |
292 } | 303 } |
293 | 304 |
294 bool DownloadPrefs::IsAutoOpenEnabledBasedOnExtension( | 305 bool DownloadPrefs::IsAutoOpenEnabledBasedOnExtension( |
295 const base::FilePath& path) const { | 306 const base::FilePath& path) const { |
296 base::FilePath::StringType extension = path.Extension(); | 307 base::FilePath::StringType extension = path.Extension(); |
297 if (extension.empty()) | 308 if (extension.empty()) |
298 return false; | 309 return false; |
299 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); | 310 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); |
300 extension.erase(0, 1); | 311 extension.erase(0, 1); |
301 #if defined(OS_WIN) | 312 #if defined(OS_WIN) |
302 if (extension == FILE_PATH_LITERAL("pdf") && | 313 if (extension == FILE_PATH_LITERAL("pdf") && |
303 DownloadTargetDeterminer::IsAdobeReaderUpToDate() && | 314 DownloadTargetDeterminer::IsAdobeReaderUpToDate() && |
304 ShouldOpenPdfInAdobeReader()) | 315 ShouldOpenPdfInAdobeReader()) |
305 return true; | 316 return true; |
| 317 #elif defined(OS_MACOSX) || defined(OS_LINUX) |
| 318 if (extension == FILE_PATH_LITERAL("pdf") && ShouldOpenPdfInSystemReader()) |
| 319 return true; |
306 #endif | 320 #endif |
307 return auto_open_.find(extension) != auto_open_.end(); | 321 return auto_open_.find(extension) != auto_open_.end(); |
308 } | 322 } |
309 | 323 |
310 bool DownloadPrefs::EnableAutoOpenBasedOnExtension( | 324 bool DownloadPrefs::EnableAutoOpenBasedOnExtension( |
311 const base::FilePath& file_name) { | 325 const base::FilePath& file_name) { |
312 base::FilePath::StringType extension = file_name.Extension(); | 326 base::FilePath::StringType extension = file_name.Extension(); |
313 if (extension.empty()) | 327 if (extension.empty()) |
314 return false; | 328 return false; |
315 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); | 329 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); |
(...skipping 20 matching lines...) Expand all Loading... |
336 if (should_open_pdf_in_adobe_reader_ == should_open) | 350 if (should_open_pdf_in_adobe_reader_ == should_open) |
337 return; | 351 return; |
338 should_open_pdf_in_adobe_reader_ = should_open; | 352 should_open_pdf_in_adobe_reader_ = should_open; |
339 profile_->GetPrefs()->SetBoolean(prefs::kOpenPdfDownloadInAdobeReader, | 353 profile_->GetPrefs()->SetBoolean(prefs::kOpenPdfDownloadInAdobeReader, |
340 should_open); | 354 should_open); |
341 } | 355 } |
342 | 356 |
343 bool DownloadPrefs::ShouldOpenPdfInAdobeReader() const { | 357 bool DownloadPrefs::ShouldOpenPdfInAdobeReader() const { |
344 return should_open_pdf_in_adobe_reader_; | 358 return should_open_pdf_in_adobe_reader_; |
345 } | 359 } |
| 360 #elif defined(OS_MACOSX) || defined(OS_LINUX) |
| 361 void DownloadPrefs::SetShouldOpenPdfInSystemReader(bool should_open) { |
| 362 if (should_open_pdf_in_system_reader_ == should_open) |
| 363 return; |
| 364 should_open_pdf_in_system_reader_ = should_open; |
| 365 profile_->GetPrefs()->SetBoolean(prefs::kOpenPdfDownloadInSystemReader, |
| 366 should_open); |
| 367 } |
| 368 |
| 369 bool DownloadPrefs::ShouldOpenPdfInSystemReader() const { |
| 370 return should_open_pdf_in_system_reader_; |
| 371 } |
346 #endif | 372 #endif |
347 | 373 |
348 void DownloadPrefs::ResetAutoOpen() { | 374 void DownloadPrefs::ResetAutoOpen() { |
349 #if defined(OS_WIN) | 375 #if defined(OS_WIN) |
350 SetShouldOpenPdfInAdobeReader(false); | 376 SetShouldOpenPdfInAdobeReader(false); |
| 377 #elif defined(OS_MACOSX) || defined(OS_LINUX) |
| 378 SetShouldOpenPdfInSystemReader(false); |
351 #endif | 379 #endif |
352 auto_open_.clear(); | 380 auto_open_.clear(); |
353 SaveAutoOpenState(); | 381 SaveAutoOpenState(); |
354 } | 382 } |
355 | 383 |
356 void DownloadPrefs::SaveAutoOpenState() { | 384 void DownloadPrefs::SaveAutoOpenState() { |
357 std::string extensions; | 385 std::string extensions; |
358 for (AutoOpenSet::iterator it = auto_open_.begin(); | 386 for (AutoOpenSet::iterator it = auto_open_.begin(); |
359 it != auto_open_.end(); ++it) { | 387 it != auto_open_.end(); ++it) { |
360 #if defined(OS_POSIX) | 388 #if defined(OS_POSIX) |
361 std::string this_extension = *it; | 389 std::string this_extension = *it; |
362 #elif defined(OS_WIN) | 390 #elif defined(OS_WIN) |
363 // TODO(phajdan.jr): Why we're using Sys conversion here, but not in ctor? | 391 // TODO(phajdan.jr): Why we're using Sys conversion here, but not in ctor? |
364 std::string this_extension = base::SysWideToUTF8(*it); | 392 std::string this_extension = base::SysWideToUTF8(*it); |
365 #endif | 393 #endif |
366 extensions += this_extension + ":"; | 394 extensions += this_extension + ":"; |
367 } | 395 } |
368 if (!extensions.empty()) | 396 if (!extensions.empty()) |
369 extensions.erase(extensions.size() - 1); | 397 extensions.erase(extensions.size() - 1); |
370 | 398 |
371 profile_->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, extensions); | 399 profile_->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, extensions); |
372 } | 400 } |
373 | 401 |
374 bool DownloadPrefs::AutoOpenCompareFunctor::operator()( | 402 bool DownloadPrefs::AutoOpenCompareFunctor::operator()( |
375 const base::FilePath::StringType& a, | 403 const base::FilePath::StringType& a, |
376 const base::FilePath::StringType& b) const { | 404 const base::FilePath::StringType& b) const { |
377 return base::FilePath::CompareLessIgnoreCase(a, b); | 405 return base::FilePath::CompareLessIgnoreCase(a, b); |
378 } | 406 } |
OLD | NEW |