No dual_mode on Win10+ shortcuts.
[chromium-blink-merge.git] / chrome / browser / download / download_prefs.cc
blobf6a3209acf63e3869ff657a4ea9d8e677193deaf
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
3 // found in the LICENSE file.
5 #include "chrome/browser/download/download_prefs.h"
7 #include <string>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/files/file_util.h"
13 #include "base/lazy_instance.h"
14 #include "base/logging.h"
15 #include "base/path_service.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/strings/string_split.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/sys_string_conversions.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "chrome/browser/download/chrome_download_manager_delegate.h"
22 #include "chrome/browser/download/download_extensions.h"
23 #include "chrome/browser/download/download_service.h"
24 #include "chrome/browser/download/download_service_factory.h"
25 #include "chrome/browser/download/download_target_determiner.h"
26 #include "chrome/browser/profiles/profile.h"
27 #include "chrome/browser/profiles/profile_manager.h"
28 #include "chrome/common/chrome_paths.h"
29 #include "chrome/common/pref_names.h"
30 #include "components/pref_registry/pref_registry_syncable.h"
31 #include "content/public/browser/browser_thread.h"
32 #include "content/public/browser/download_manager.h"
33 #include "content/public/browser/save_page_type.h"
35 #if defined(OS_CHROMEOS)
36 #include "chrome/browser/chromeos/drive/drive_integration_service.h"
37 #include "chrome/browser/chromeos/drive/file_system_util.h"
38 #include "chrome/browser/chromeos/file_manager/path_util.h"
39 #endif
41 #if defined(OS_WIN)
42 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h"
43 #endif
45 using content::BrowserContext;
46 using content::BrowserThread;
47 using content::DownloadManager;
49 namespace {
51 // Consider downloads 'dangerous' if they go to the home directory on Linux and
52 // to the desktop on any platform.
53 bool DownloadPathIsDangerous(const base::FilePath& download_path) {
54 #if defined(OS_LINUX)
55 base::FilePath home_dir = base::GetHomeDir();
56 if (download_path == home_dir) {
57 return true;
59 #endif
61 #if defined(OS_ANDROID)
62 // Android does not have a desktop dir.
63 return false;
64 #else
65 base::FilePath desktop_dir;
66 if (!PathService::Get(base::DIR_USER_DESKTOP, &desktop_dir)) {
67 NOTREACHED();
68 return false;
70 return (download_path == desktop_dir);
71 #endif
74 class DefaultDownloadDirectory {
75 public:
76 const base::FilePath& path() const { return path_; }
78 private:
79 friend struct base::DefaultLazyInstanceTraits<DefaultDownloadDirectory>;
81 DefaultDownloadDirectory() {
82 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &path_)) {
83 NOTREACHED();
85 if (DownloadPathIsDangerous(path_)) {
86 // This is only useful on platforms that support
87 // DIR_DEFAULT_DOWNLOADS_SAFE.
88 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE, &path_)) {
89 NOTREACHED();
94 base::FilePath path_;
96 DISALLOW_COPY_AND_ASSIGN(DefaultDownloadDirectory);
99 base::LazyInstance<DefaultDownloadDirectory>
100 g_default_download_directory = LAZY_INSTANCE_INITIALIZER;
102 } // namespace
104 DownloadPrefs::DownloadPrefs(Profile* profile) : profile_(profile) {
105 PrefService* prefs = profile->GetPrefs();
107 #if defined(OS_CHROMEOS)
108 // On Chrome OS, the default download directory is different for each profile.
109 // If the profile-unaware default path (from GetDefaultDownloadDirectory())
110 // is set (this happens during the initial preference registration in static
111 // RegisterProfilePrefs()), alter by GetDefaultDownloadDirectoryForProfile().
112 // file_manager::util::MigratePathFromOldFormat will do this.
113 const char* path_pref[] = {
114 prefs::kSaveFileDefaultDirectory,
115 prefs::kDownloadDefaultDirectory
117 for (size_t i = 0; i < arraysize(path_pref); ++i) {
118 const base::FilePath current = prefs->GetFilePath(path_pref[i]);
119 base::FilePath migrated;
120 if (!current.empty() &&
121 file_manager::util::MigratePathFromOldFormat(
122 profile_, current, &migrated)) {
123 prefs->SetFilePath(path_pref[i], migrated);
127 // Ensure that the default download directory exists.
128 BrowserThread::PostTask(
129 BrowserThread::FILE, FROM_HERE,
130 base::Bind(base::IgnoreResult(&base::CreateDirectory),
131 GetDefaultDownloadDirectoryForProfile()));
132 #endif // defined(OS_CHROMEOS)
134 #if defined(OS_WIN) || defined(OS_LINUX) || \
135 (defined(OS_MACOSX) && !defined(OS_IOS))
136 should_open_pdf_in_system_reader_ =
137 prefs->GetBoolean(prefs::kOpenPdfDownloadInSystemReader);
138 #endif
140 // If the download path is dangerous we forcefully reset it. But if we do
141 // so we set a flag to make sure we only do it once, to avoid fighting
142 // the user if he really wants it on an unsafe place such as the desktop.
143 if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) {
144 base::FilePath current_download_dir = prefs->GetFilePath(
145 prefs::kDownloadDefaultDirectory);
146 if (DownloadPathIsDangerous(current_download_dir)) {
147 prefs->SetFilePath(prefs::kDownloadDefaultDirectory,
148 GetDefaultDownloadDirectoryForProfile());
150 prefs->SetBoolean(prefs::kDownloadDirUpgraded, true);
153 prompt_for_download_.Init(prefs::kPromptForDownload, prefs);
154 download_path_.Init(prefs::kDownloadDefaultDirectory, prefs);
155 save_file_path_.Init(prefs::kSaveFileDefaultDirectory, prefs);
156 save_file_type_.Init(prefs::kSaveFileType, prefs);
158 // We store any file extension that should be opened automatically at
159 // download completion in this pref.
160 std::string extensions_to_open =
161 prefs->GetString(prefs::kDownloadExtensionsToOpen);
163 for (const auto& extension_string : base::SplitString(
164 extensions_to_open, ":",
165 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
166 #if defined(OS_POSIX)
167 base::FilePath::StringType extension = extension_string;
168 #elif defined(OS_WIN)
169 base::FilePath::StringType extension = base::UTF8ToWide(extension_string);
170 #endif
171 // If it's empty or malformed or not allowed to open automatically, then
172 // skip the entry. Any such entries will be dropped from preferences the
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);
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);
192 DownloadPrefs::~DownloadPrefs() {}
194 // static
195 void DownloadPrefs::RegisterProfilePrefs(
196 user_prefs::PrefRegistrySyncable* registry) {
197 registry->RegisterBooleanPref(
198 prefs::kPromptForDownload,
199 false,
200 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
201 registry->RegisterStringPref(prefs::kDownloadExtensionsToOpen, std::string());
202 registry->RegisterBooleanPref(prefs::kDownloadDirUpgraded, false);
203 registry->RegisterIntegerPref(prefs::kSaveFileType,
204 content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML);
206 const base::FilePath& default_download_path = GetDefaultDownloadDirectory();
207 registry->RegisterFilePathPref(prefs::kDownloadDefaultDirectory,
208 default_download_path);
209 registry->RegisterFilePathPref(prefs::kSaveFileDefaultDirectory,
210 default_download_path);
211 #if defined(OS_WIN) || defined(OS_LINUX) || \
212 (defined(OS_MACOSX) && !defined(OS_IOS))
213 registry->RegisterBooleanPref(prefs::kOpenPdfDownloadInSystemReader, false);
214 #endif
217 base::FilePath DownloadPrefs::GetDefaultDownloadDirectoryForProfile() const {
218 #if defined(OS_CHROMEOS)
219 return file_manager::util::GetDownloadsFolderForProfile(profile_);
220 #else
221 return GetDefaultDownloadDirectory();
222 #endif
225 // static
226 const base::FilePath& DownloadPrefs::GetDefaultDownloadDirectory() {
227 return g_default_download_directory.Get().path();
230 // static
231 DownloadPrefs* DownloadPrefs::FromDownloadManager(
232 DownloadManager* download_manager) {
233 ChromeDownloadManagerDelegate* delegate =
234 static_cast<ChromeDownloadManagerDelegate*>(
235 download_manager->GetDelegate());
236 return delegate->download_prefs();
239 // static
240 DownloadPrefs* DownloadPrefs::FromBrowserContext(
241 content::BrowserContext* context) {
242 return FromDownloadManager(BrowserContext::GetDownloadManager(context));
245 base::FilePath DownloadPrefs::DownloadPath() const {
246 #if defined(OS_CHROMEOS)
247 // If the download path is under /drive, and DriveIntegrationService isn't
248 // available (which it isn't for incognito mode, for instance), use the
249 // default download directory (/Downloads).
250 if (drive::util::IsUnderDriveMountPoint(*download_path_)) {
251 drive::DriveIntegrationService* integration_service =
252 drive::DriveIntegrationServiceFactory::FindForProfile(profile_);
253 if (!integration_service || !integration_service->is_enabled())
254 return GetDefaultDownloadDirectoryForProfile();
256 #endif
257 return *download_path_;
260 void DownloadPrefs::SetDownloadPath(const base::FilePath& path) {
261 download_path_.SetValue(path);
262 SetSaveFilePath(path);
265 base::FilePath DownloadPrefs::SaveFilePath() const {
266 return *save_file_path_;
269 void DownloadPrefs::SetSaveFilePath(const base::FilePath& path) {
270 save_file_path_.SetValue(path);
273 void DownloadPrefs::SetSaveFileType(int type) {
274 save_file_type_.SetValue(type);
277 bool DownloadPrefs::PromptForDownload() const {
278 // If the DownloadDirectory policy is set, then |prompt_for_download_| should
279 // always be false.
280 DCHECK(!download_path_.IsManaged() || !prompt_for_download_.GetValue());
281 return *prompt_for_download_;
284 bool DownloadPrefs::IsDownloadPathManaged() const {
285 return download_path_.IsManaged();
288 bool DownloadPrefs::IsAutoOpenUsed() const {
289 #if defined(OS_WIN) || defined(OS_LINUX) || \
290 (defined(OS_MACOSX) && !defined(OS_IOS))
291 if (ShouldOpenPdfInSystemReader())
292 return true;
293 #endif
294 return !auto_open_.empty();
297 bool DownloadPrefs::IsAutoOpenEnabledBasedOnExtension(
298 const base::FilePath& path) const {
299 base::FilePath::StringType extension = path.Extension();
300 if (extension.empty())
301 return false;
302 DCHECK(extension[0] == base::FilePath::kExtensionSeparator);
303 extension.erase(0, 1);
304 #if defined(OS_WIN) || defined(OS_LINUX) || \
305 (defined(OS_MACOSX) && !defined(OS_IOS))
306 if (extension == FILE_PATH_LITERAL("pdf") && ShouldOpenPdfInSystemReader())
307 return true;
308 #endif
310 return auto_open_.find(extension) != auto_open_.end();
313 bool DownloadPrefs::EnableAutoOpenBasedOnExtension(
314 const base::FilePath& file_name) {
315 base::FilePath::StringType extension = file_name.Extension();
316 if (!download_util::IsAllowedToOpenAutomatically(file_name))
317 return false;
319 DCHECK(extension[0] == base::FilePath::kExtensionSeparator);
320 extension.erase(0, 1);
322 auto_open_.insert(extension);
323 SaveAutoOpenState();
324 return true;
327 void DownloadPrefs::DisableAutoOpenBasedOnExtension(
328 const base::FilePath& file_name) {
329 base::FilePath::StringType extension = file_name.Extension();
330 if (extension.empty())
331 return;
332 DCHECK(extension[0] == base::FilePath::kExtensionSeparator);
333 extension.erase(0, 1);
334 auto_open_.erase(extension);
335 SaveAutoOpenState();
338 #if defined(OS_WIN) || defined(OS_LINUX) || \
339 (defined(OS_MACOSX) && !defined(OS_IOS))
340 void DownloadPrefs::SetShouldOpenPdfInSystemReader(bool should_open) {
341 if (should_open_pdf_in_system_reader_ == should_open)
342 return;
343 should_open_pdf_in_system_reader_ = should_open;
344 profile_->GetPrefs()->SetBoolean(prefs::kOpenPdfDownloadInSystemReader,
345 should_open);
348 bool DownloadPrefs::ShouldOpenPdfInSystemReader() const {
349 #if defined(OS_WIN)
350 if (IsAdobeReaderDefaultPDFViewer() &&
351 !DownloadTargetDeterminer::IsAdobeReaderUpToDate()) {
352 return false;
354 #endif
355 return should_open_pdf_in_system_reader_;
357 #endif
359 void DownloadPrefs::ResetAutoOpen() {
360 #if defined(OS_WIN) || defined(OS_LINUX) || \
361 (defined(OS_MACOSX) && !defined(OS_IOS))
362 SetShouldOpenPdfInSystemReader(false);
363 #endif
364 auto_open_.clear();
365 SaveAutoOpenState();
368 void DownloadPrefs::SaveAutoOpenState() {
369 std::string extensions;
370 for (AutoOpenSet::iterator it = auto_open_.begin();
371 it != auto_open_.end(); ++it) {
372 #if defined(OS_POSIX)
373 std::string this_extension = *it;
374 #elif defined(OS_WIN)
375 // TODO(phajdan.jr): Why we're using Sys conversion here, but not in ctor?
376 std::string this_extension = base::SysWideToUTF8(*it);
377 #endif
378 extensions += this_extension + ":";
380 if (!extensions.empty())
381 extensions.erase(extensions.size() - 1);
383 profile_->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, extensions);
386 bool DownloadPrefs::AutoOpenCompareFunctor::operator()(
387 const base::FilePath::StringType& a,
388 const base::FilePath::StringType& b) const {
389 return base::FilePath::CompareLessIgnoreCase(a, b);