Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / download / download_prefs.cc
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.
4
5 #include "chrome/browser/download/download_prefs.h"
6
7 #include <string>
8 #include <vector>
9
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"
34
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
40
41 #if defined(OS_WIN)
42 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h"
43 #endif
44
45 using content::BrowserContext;
46 using content::BrowserThread;
47 using content::DownloadManager;
48
49 namespace {
50
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;
58   }
59 #endif
60
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;
69   }
70   return (download_path == desktop_dir);
71 #endif
72 }
73
74 class DefaultDownloadDirectory {
75  public:
76   const base::FilePath& path() const { return path_; }
77
78  private:
79   friend struct base::DefaultLazyInstanceTraits<DefaultDownloadDirectory>;
80
81   DefaultDownloadDirectory() {
82     if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &path_)) {
83       NOTREACHED();
84     }
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();
90       }
91     }
92   }
93
94   base::FilePath path_;
95
96   DISALLOW_COPY_AND_ASSIGN(DefaultDownloadDirectory);
97 };
98
99 base::LazyInstance<DefaultDownloadDirectory>
100     g_default_download_directory = LAZY_INSTANCE_INITIALIZER;
101
102 }  // namespace
103
104 DownloadPrefs::DownloadPrefs(Profile* profile) : profile_(profile) {
105   PrefService* prefs = profile->GetPrefs();
106
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
116   };
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);
124     }
125   }
126
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)
133
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
139
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());
149     }
150     prefs->SetBoolean(prefs::kDownloadDirUpgraded, true);
151   }
152
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);
157
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);
162   std::vector<std::string> extensions;
163   base::SplitString(extensions_to_open, ':', &extensions);
164
165   for (size_t i = 0; i < extensions.size(); ++i) {
166 #if defined(OS_POSIX)
167     base::FilePath path(extensions[i]);
168 #elif defined(OS_WIN)
169     base::FilePath path(base::UTF8ToWide(extensions[i]));
170 #endif
171     if (!extensions[i].empty() &&
172         download_util::GetFileDangerLevel(path) == download_util::NOT_DANGEROUS)
173       auto_open_.insert(path.value());
174   }
175 }
176
177 DownloadPrefs::~DownloadPrefs() {}
178
179 // static
180 void DownloadPrefs::RegisterProfilePrefs(
181     user_prefs::PrefRegistrySyncable* registry) {
182   registry->RegisterBooleanPref(
183       prefs::kPromptForDownload,
184       false,
185       user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
186   registry->RegisterStringPref(
187       prefs::kDownloadExtensionsToOpen,
188       std::string(),
189       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
190   registry->RegisterBooleanPref(
191       prefs::kDownloadDirUpgraded,
192       false,
193       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
194   registry->RegisterIntegerPref(
195       prefs::kSaveFileType,
196       content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML,
197       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
198
199   const base::FilePath& default_download_path = GetDefaultDownloadDirectory();
200   registry->RegisterFilePathPref(
201       prefs::kDownloadDefaultDirectory,
202       default_download_path,
203       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
204   registry->RegisterFilePathPref(
205       prefs::kSaveFileDefaultDirectory,
206       default_download_path,
207       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
208 #if defined(OS_WIN) || defined(OS_LINUX) || \
209     (defined(OS_MACOSX) && !defined(OS_IOS))
210   registry->RegisterBooleanPref(
211       prefs::kOpenPdfDownloadInSystemReader,
212       false,
213       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
214 #endif
215 }
216
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
223 }
224
225 // static
226 const base::FilePath& DownloadPrefs::GetDefaultDownloadDirectory() {
227   return g_default_download_directory.Get().path();
228 }
229
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();
237 }
238
239 // static
240 DownloadPrefs* DownloadPrefs::FromBrowserContext(
241     content::BrowserContext* context) {
242   return FromDownloadManager(BrowserContext::GetDownloadManager(context));
243 }
244
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();
255   }
256 #endif
257   return *download_path_;
258 }
259
260 void DownloadPrefs::SetDownloadPath(const base::FilePath& path) {
261   download_path_.SetValue(path);
262   SetSaveFilePath(path);
263 }
264
265 base::FilePath DownloadPrefs::SaveFilePath() const {
266   return *save_file_path_;
267 }
268
269 void DownloadPrefs::SetSaveFilePath(const base::FilePath& path) {
270   save_file_path_.SetValue(path);
271 }
272
273 void DownloadPrefs::SetSaveFileType(int type) {
274   save_file_type_.SetValue(type);
275 }
276
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_;
282 }
283
284 bool DownloadPrefs::IsDownloadPathManaged() const {
285   return download_path_.IsManaged();
286 }
287
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();
295 }
296
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
309   return auto_open_.find(extension) != auto_open_.end();
310 }
311
312 bool DownloadPrefs::EnableAutoOpenBasedOnExtension(
313     const base::FilePath& file_name) {
314   base::FilePath::StringType extension = file_name.Extension();
315   if (extension.empty())
316     return false;
317   DCHECK(extension[0] == base::FilePath::kExtensionSeparator);
318   extension.erase(0, 1);
319
320   auto_open_.insert(extension);
321   SaveAutoOpenState();
322   return true;
323 }
324
325 void DownloadPrefs::DisableAutoOpenBasedOnExtension(
326     const base::FilePath& file_name) {
327   base::FilePath::StringType extension = file_name.Extension();
328   if (extension.empty())
329     return;
330   DCHECK(extension[0] == base::FilePath::kExtensionSeparator);
331   extension.erase(0, 1);
332   auto_open_.erase(extension);
333   SaveAutoOpenState();
334 }
335
336 #if defined(OS_WIN) || defined(OS_LINUX) || \
337     (defined(OS_MACOSX) && !defined(OS_IOS))
338 void DownloadPrefs::SetShouldOpenPdfInSystemReader(bool should_open) {
339   if (should_open_pdf_in_system_reader_ == should_open)
340     return;
341   should_open_pdf_in_system_reader_ = should_open;
342   profile_->GetPrefs()->SetBoolean(prefs::kOpenPdfDownloadInSystemReader,
343                                    should_open);
344 }
345
346 bool DownloadPrefs::ShouldOpenPdfInSystemReader() const {
347 #if defined(OS_WIN)
348   if (IsAdobeReaderDefaultPDFViewer() &&
349       !DownloadTargetDeterminer::IsAdobeReaderUpToDate()) {
350       return false;
351   }
352 #endif
353   return should_open_pdf_in_system_reader_;
354 }
355 #endif
356
357 void DownloadPrefs::ResetAutoOpen() {
358 #if defined(OS_WIN) || defined(OS_LINUX) || \
359     (defined(OS_MACOSX) && !defined(OS_IOS))
360   SetShouldOpenPdfInSystemReader(false);
361 #endif
362   auto_open_.clear();
363   SaveAutoOpenState();
364 }
365
366 void DownloadPrefs::SaveAutoOpenState() {
367   std::string extensions;
368   for (AutoOpenSet::iterator it = auto_open_.begin();
369        it != auto_open_.end(); ++it) {
370 #if defined(OS_POSIX)
371     std::string this_extension = *it;
372 #elif defined(OS_WIN)
373     // TODO(phajdan.jr): Why we're using Sys conversion here, but not in ctor?
374     std::string this_extension = base::SysWideToUTF8(*it);
375 #endif
376     extensions += this_extension + ":";
377   }
378   if (!extensions.empty())
379     extensions.erase(extensions.size() - 1);
380
381   profile_->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, extensions);
382 }
383
384 bool DownloadPrefs::AutoOpenCompareFunctor::operator()(
385     const base::FilePath::StringType& a,
386     const base::FilePath::StringType& b) const {
387   return base::FilePath::CompareLessIgnoreCase(a, b);
388 }