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 19 matching lines...) Expand all Loading... |
30 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
31 #include "content/public/browser/download_manager.h" | 31 #include "content/public/browser/download_manager.h" |
32 #include "content/public/browser/save_page_type.h" | 32 #include "content/public/browser/save_page_type.h" |
33 | 33 |
34 #if defined(OS_CHROMEOS) | 34 #if defined(OS_CHROMEOS) |
35 #include "chrome/browser/chromeos/drive/drive_integration_service.h" | 35 #include "chrome/browser/chromeos/drive/drive_integration_service.h" |
36 #include "chrome/browser/chromeos/drive/file_system_util.h" | 36 #include "chrome/browser/chromeos/drive/file_system_util.h" |
37 #include "chrome/browser/chromeos/file_manager/path_util.h" | 37 #include "chrome/browser/chromeos/file_manager/path_util.h" |
38 #endif | 38 #endif |
39 | 39 |
| 40 #if defined(OS_WIN) |
| 41 #include "chrome/browser/browser_process.h" |
| 42 #include "chrome/browser/plugins/plugin_metadata.h" |
| 43 #include "chrome/browser/plugins/plugin_prefs.h" |
| 44 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h" |
| 45 #include "chrome/common/chrome_content_client.h" |
| 46 #endif |
| 47 |
40 using content::BrowserContext; | 48 using content::BrowserContext; |
41 using content::BrowserThread; | 49 using content::BrowserThread; |
42 using content::DownloadManager; | 50 using content::DownloadManager; |
43 | 51 |
44 namespace { | 52 namespace { |
45 | 53 |
46 // Consider downloads 'dangerous' if they go to the home directory on Linux and | 54 // Consider downloads 'dangerous' if they go to the home directory on Linux and |
47 // to the desktop on any platform. | 55 // to the desktop on any platform. |
48 bool DownloadPathIsDangerous(const base::FilePath& download_path) { | 56 bool DownloadPathIsDangerous(const base::FilePath& download_path) { |
49 #if defined(OS_LINUX) | 57 #if defined(OS_LINUX) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 NOTREACHED(); | 92 NOTREACHED(); |
85 } | 93 } |
86 } | 94 } |
87 } | 95 } |
88 | 96 |
89 base::FilePath path_; | 97 base::FilePath path_; |
90 | 98 |
91 DISALLOW_COPY_AND_ASSIGN(DefaultDownloadDirectory); | 99 DISALLOW_COPY_AND_ASSIGN(DefaultDownloadDirectory); |
92 }; | 100 }; |
93 | 101 |
94 static base::LazyInstance<DefaultDownloadDirectory> | 102 base::LazyInstance<DefaultDownloadDirectory> |
95 g_default_download_directory = LAZY_INSTANCE_INITIALIZER; | 103 g_default_download_directory = LAZY_INSTANCE_INITIALIZER; |
96 | 104 |
| 105 #if defined(OS_WIN) |
| 106 bool g_adobe_reader_up_to_date = false; |
| 107 |
| 108 // Callback when whether Adobe Reader is up to date is known. |
| 109 void OnGotAdobeReaderInfo(Profile* profile, bool up_to_date) { |
| 110 g_adobe_reader_up_to_date = up_to_date; |
| 111 if (!up_to_date) { |
| 112 if (g_browser_process->profile_manager()->IsValidProfile(profile)) { |
| 113 DownloadPrefs* prefs = DownloadPrefs::FromBrowserContext(profile); |
| 114 prefs->SetShouldOpenPdfInAdobeReader(false); |
| 115 } |
| 116 } |
| 117 } |
| 118 #endif |
| 119 |
97 } // namespace | 120 } // namespace |
98 | 121 |
99 DownloadPrefs::DownloadPrefs(Profile* profile) : profile_(profile) { | 122 DownloadPrefs::DownloadPrefs(Profile* profile) : profile_(profile) { |
100 PrefService* prefs = profile->GetPrefs(); | 123 PrefService* prefs = profile->GetPrefs(); |
101 | 124 |
102 #if defined(OS_CHROMEOS) | 125 #if defined(OS_CHROMEOS) |
103 // On Chrome OS, the default download directory is different for each profile. | 126 // On Chrome OS, the default download directory is different for each profile. |
104 // If the profile-unaware default path (from GetDefaultDownloadDirectory()) | 127 // If the profile-unaware default path (from GetDefaultDownloadDirectory()) |
105 // is set (this happens during the initial preference registration in static | 128 // is set (this happens during the initial preference registration in static |
106 // RegisterProfilePrefs()), alter by GetDefaultDownloadDirectoryForProfile(). | 129 // RegisterProfilePrefs()), alter by GetDefaultDownloadDirectoryForProfile(). |
107 // file_manager::util::MigratePathFromOldFormat will do this. | 130 // file_manager::util::MigratePathFromOldFormat will do this. |
108 const char* path_pref[] = { | 131 const char* path_pref[] = { |
109 prefs::kSaveFileDefaultDirectory, | 132 prefs::kSaveFileDefaultDirectory, |
110 prefs::kDownloadDefaultDirectory | 133 prefs::kDownloadDefaultDirectory |
111 }; | 134 }; |
112 for (size_t i = 0; i < arraysize(path_pref); ++i) { | 135 for (size_t i = 0; i < arraysize(path_pref); ++i) { |
113 const base::FilePath current = prefs->GetFilePath(path_pref[i]); | 136 const base::FilePath current = prefs->GetFilePath(path_pref[i]); |
114 base::FilePath migrated; | 137 base::FilePath migrated; |
115 if (!current.empty() && | 138 if (!current.empty() && |
116 file_manager::util::MigratePathFromOldFormat( | 139 file_manager::util::MigratePathFromOldFormat( |
117 profile_, current, &migrated)) { | 140 profile_, current, &migrated)) { |
118 prefs->SetFilePath(path_pref[i], migrated); | 141 prefs->SetFilePath(path_pref[i], migrated); |
119 } | 142 } |
120 } | 143 } |
121 | 144 |
122 // Ensure that the default download directory exists. | 145 // Ensure that the default download directory exists. |
123 BrowserThread::PostTask( | 146 BrowserThread::PostBlockingPoolTask( |
124 BrowserThread::FILE, FROM_HERE, | 147 FROM_HERE, |
125 base::Bind(base::IgnoreResult(&base::CreateDirectory), | 148 base::Bind(base::IgnoreResult(&base::CreateDirectory), |
126 GetDefaultDownloadDirectoryForProfile())); | 149 GetDefaultDownloadDirectoryForProfile())); |
127 #endif // defined(OS_CHROMEOS) | 150 #endif // defined(OS_CHROMEOS) |
128 | 151 |
| 152 #if defined(OS_WIN) |
| 153 if (IsAdobeReaderDefaultPDFViewer()) { |
| 154 base::PostTaskAndReplyWithResult( |
| 155 BrowserThread::GetBlockingPool(), |
| 156 FROM_HERE, |
| 157 base::Bind(&IsAdobeReaderUpToDate), |
| 158 base::Bind(&OnGotAdobeReaderInfo, profile)); |
| 159 } |
| 160 |
| 161 should_open_pdf_in_adobe_reader_ = |
| 162 prefs->GetBoolean(prefs::kOpenPdfDownloadInAdobeReader); |
| 163 #endif |
| 164 |
129 // If the download path is dangerous we forcefully reset it. But if we do | 165 // If the download path is dangerous we forcefully reset it. But if we do |
130 // so we set a flag to make sure we only do it once, to avoid fighting | 166 // so we set a flag to make sure we only do it once, to avoid fighting |
131 // the user if he really wants it on an unsafe place such as the desktop. | 167 // the user if he really wants it on an unsafe place such as the desktop. |
132 if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) { | 168 if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) { |
133 base::FilePath current_download_dir = prefs->GetFilePath( | 169 base::FilePath current_download_dir = prefs->GetFilePath( |
134 prefs::kDownloadDefaultDirectory); | 170 prefs::kDownloadDefaultDirectory); |
135 if (DownloadPathIsDangerous(current_download_dir)) { | 171 if (DownloadPathIsDangerous(current_download_dir)) { |
136 prefs->SetFilePath(prefs::kDownloadDefaultDirectory, | 172 prefs->SetFilePath(prefs::kDownloadDefaultDirectory, |
137 GetDefaultDownloadDirectoryForProfile()); | 173 GetDefaultDownloadDirectoryForProfile()); |
138 } | 174 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 | 223 |
188 const base::FilePath& default_download_path = GetDefaultDownloadDirectory(); | 224 const base::FilePath& default_download_path = GetDefaultDownloadDirectory(); |
189 registry->RegisterFilePathPref( | 225 registry->RegisterFilePathPref( |
190 prefs::kDownloadDefaultDirectory, | 226 prefs::kDownloadDefaultDirectory, |
191 default_download_path, | 227 default_download_path, |
192 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 228 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
193 registry->RegisterFilePathPref( | 229 registry->RegisterFilePathPref( |
194 prefs::kSaveFileDefaultDirectory, | 230 prefs::kSaveFileDefaultDirectory, |
195 default_download_path, | 231 default_download_path, |
196 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 232 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 233 #if defined(OS_WIN) |
| 234 registry->RegisterBooleanPref( |
| 235 prefs::kOpenPdfDownloadInAdobeReader, |
| 236 false, |
| 237 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 238 #endif |
197 } | 239 } |
198 | 240 |
| 241 #if defined(OS_WIN) |
| 242 // static |
| 243 bool DownloadPrefs::IsAdobeReaderUpToDate() { |
| 244 return g_adobe_reader_up_to_date; |
| 245 } |
| 246 #endif |
| 247 |
199 base::FilePath DownloadPrefs::GetDefaultDownloadDirectoryForProfile() const { | 248 base::FilePath DownloadPrefs::GetDefaultDownloadDirectoryForProfile() const { |
200 #if defined(OS_CHROMEOS) | 249 #if defined(OS_CHROMEOS) |
201 return file_manager::util::GetDownloadsFolderForProfile(profile_); | 250 return file_manager::util::GetDownloadsFolderForProfile(profile_); |
202 #else | 251 #else |
203 return GetDefaultDownloadDirectory(); | 252 return GetDefaultDownloadDirectory(); |
204 #endif | 253 #endif |
205 } | 254 } |
206 | 255 |
207 // static | 256 // static |
208 const base::FilePath& DownloadPrefs::GetDefaultDownloadDirectory() { | 257 const base::FilePath& DownloadPrefs::GetDefaultDownloadDirectory() { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 return !auto_open_.empty(); | 320 return !auto_open_.empty(); |
272 } | 321 } |
273 | 322 |
274 bool DownloadPrefs::IsAutoOpenEnabledBasedOnExtension( | 323 bool DownloadPrefs::IsAutoOpenEnabledBasedOnExtension( |
275 const base::FilePath& path) const { | 324 const base::FilePath& path) const { |
276 base::FilePath::StringType extension = path.Extension(); | 325 base::FilePath::StringType extension = path.Extension(); |
277 if (extension.empty()) | 326 if (extension.empty()) |
278 return false; | 327 return false; |
279 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); | 328 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); |
280 extension.erase(0, 1); | 329 extension.erase(0, 1); |
| 330 #if defined(OS_WIN) |
| 331 if (extension == FILE_PATH_LITERAL("pdf") && |
| 332 g_adobe_reader_up_to_date && ShouldOpenPdfInAdobeReader()) |
| 333 return true; |
| 334 #endif |
281 return auto_open_.find(extension) != auto_open_.end(); | 335 return auto_open_.find(extension) != auto_open_.end(); |
282 } | 336 } |
283 | 337 |
284 bool DownloadPrefs::EnableAutoOpenBasedOnExtension( | 338 bool DownloadPrefs::EnableAutoOpenBasedOnExtension( |
285 const base::FilePath& file_name) { | 339 const base::FilePath& file_name) { |
286 base::FilePath::StringType extension = file_name.Extension(); | 340 base::FilePath::StringType extension = file_name.Extension(); |
287 if (extension.empty()) | 341 if (extension.empty()) |
288 return false; | 342 return false; |
289 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); | 343 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); |
290 extension.erase(0, 1); | 344 extension.erase(0, 1); |
291 | 345 |
292 auto_open_.insert(extension); | 346 auto_open_.insert(extension); |
293 SaveAutoOpenState(); | 347 SaveAutoOpenState(); |
294 return true; | 348 return true; |
295 } | 349 } |
296 | 350 |
297 void DownloadPrefs::DisableAutoOpenBasedOnExtension( | 351 void DownloadPrefs::DisableAutoOpenBasedOnExtension( |
298 const base::FilePath& file_name) { | 352 const base::FilePath& file_name) { |
299 base::FilePath::StringType extension = file_name.Extension(); | 353 base::FilePath::StringType extension = file_name.Extension(); |
300 if (extension.empty()) | 354 if (extension.empty()) |
301 return; | 355 return; |
302 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); | 356 DCHECK(extension[0] == base::FilePath::kExtensionSeparator); |
303 extension.erase(0, 1); | 357 extension.erase(0, 1); |
304 auto_open_.erase(extension); | 358 auto_open_.erase(extension); |
305 SaveAutoOpenState(); | 359 SaveAutoOpenState(); |
306 } | 360 } |
307 | 361 |
| 362 #if defined(OS_WIN) |
| 363 void DownloadPrefs::SetShouldOpenPdfInAdobeReader(bool should_open) { |
| 364 if (should_open_pdf_in_adobe_reader_ == should_open) |
| 365 return; |
| 366 UpdatePdfPlugins(should_open); |
| 367 profile_->GetPrefs()->SetBoolean(prefs::kOpenPdfDownloadInAdobeReader, |
| 368 should_open); |
| 369 } |
| 370 |
| 371 bool DownloadPrefs::ShouldOpenPdfInAdobeReader() const { |
| 372 return should_open_pdf_in_adobe_reader_; |
| 373 } |
| 374 |
| 375 void DownloadPrefs::UpdatePdfPlugins(bool should_open) { |
| 376 PluginPrefs* plugin_prefs = PluginPrefs::GetForProfile(profile_).get(); |
| 377 bool enable_chrome_pdf_plugin = !should_open; |
| 378 bool enable_reader_npapi_plugin = false; |
| 379 plugin_prefs->EnablePluginGroup( |
| 380 enable_reader_npapi_plugin, |
| 381 base::ASCIIToUTF16(PluginMetadata::kAdobeReaderGroupName)); |
| 382 plugin_prefs->EnablePluginGroup( |
| 383 enable_chrome_pdf_plugin, |
| 384 base::ASCIIToUTF16(ChromeContentClient::kPDFPluginName)); |
| 385 } |
| 386 #endif |
| 387 |
308 void DownloadPrefs::ResetAutoOpen() { | 388 void DownloadPrefs::ResetAutoOpen() { |
309 auto_open_.clear(); | 389 auto_open_.clear(); |
310 SaveAutoOpenState(); | 390 SaveAutoOpenState(); |
311 } | 391 } |
312 | 392 |
313 void DownloadPrefs::SaveAutoOpenState() { | 393 void DownloadPrefs::SaveAutoOpenState() { |
314 std::string extensions; | 394 std::string extensions; |
315 for (AutoOpenSet::iterator it = auto_open_.begin(); | 395 for (AutoOpenSet::iterator it = auto_open_.begin(); |
316 it != auto_open_.end(); ++it) { | 396 it != auto_open_.end(); ++it) { |
317 #if defined(OS_POSIX) | 397 #if defined(OS_POSIX) |
318 std::string this_extension = *it; | 398 std::string this_extension = *it; |
319 #elif defined(OS_WIN) | 399 #elif defined(OS_WIN) |
320 // TODO(phajdan.jr): Why we're using Sys conversion here, but not in ctor? | 400 // TODO(phajdan.jr): Why we're using Sys conversion here, but not in ctor? |
321 std::string this_extension = base::SysWideToUTF8(*it); | 401 std::string this_extension = base::SysWideToUTF8(*it); |
322 #endif | 402 #endif |
323 extensions += this_extension + ":"; | 403 extensions += this_extension + ":"; |
324 } | 404 } |
325 if (!extensions.empty()) | 405 if (!extensions.empty()) |
326 extensions.erase(extensions.size() - 1); | 406 extensions.erase(extensions.size() - 1); |
327 | 407 |
328 profile_->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, extensions); | 408 profile_->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, extensions); |
329 } | 409 } |
330 | 410 |
331 bool DownloadPrefs::AutoOpenCompareFunctor::operator()( | 411 bool DownloadPrefs::AutoOpenCompareFunctor::operator()( |
332 const base::FilePath::StringType& a, | 412 const base::FilePath::StringType& a, |
333 const base::FilePath::StringType& b) const { | 413 const base::FilePath::StringType& b) const { |
334 return base::FilePath::CompareLessIgnoreCase(a, b); | 414 return base::FilePath::CompareLessIgnoreCase(a, b); |
335 } | 415 } |
OLD | NEW |