750e11b5737877a6676eaba00ab0f35cd1a5b396
[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/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/profiles/profile.h"
26 #include "chrome/browser/profiles/profile_manager.h"
27 #include "chrome/common/chrome_paths.h"
28 #include "chrome/common/pref_names.h"
29 #include "components/pref_registry/pref_registry_syncable.h"
30 #include "content/public/browser/browser_thread.h"
31 #include "content/public/browser/download_manager.h"
32 #include "content/public/browser/save_page_type.h"
33
34 #if defined(OS_CHROMEOS)
35 #include "chrome/browser/chromeos/drive/drive_integration_service.h"
36 #include "chrome/browser/chromeos/drive/file_system_util.h"
37 #include "chrome/browser/chromeos/file_manager/path_util.h"
38 #endif
39
40 using content::BrowserContext;
41 using content::BrowserThread;
42 using content::DownloadManager;
43
44 namespace {
45
46 // Consider downloads 'dangerous' if they go to the home directory on Linux and
47 // to the desktop on any platform.
48 bool DownloadPathIsDangerous(const base::FilePath& download_path) {
49 #if defined(OS_LINUX)
50   base::FilePath home_dir = base::GetHomeDir();
51   if (download_path == home_dir) {
52     return true;
53   }
54 #endif
55
56 #if defined(OS_ANDROID)
57   // Android does not have a desktop dir.
58   return false;
59 #else
60   base::FilePath desktop_dir;
61   if (!PathService::Get(base::DIR_USER_DESKTOP, &desktop_dir)) {
62     NOTREACHED();
63     return false;
64   }
65   return (download_path == desktop_dir);
66 #endif
67 }
68
69 class DefaultDownloadDirectory {
70  public:
71   const base::FilePath& path() const { return path_; }
72
73  private:
74   friend struct base::DefaultLazyInstanceTraits<DefaultDownloadDirectory>;
75
76   DefaultDownloadDirectory() {
77     if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &path_)) {
78       NOTREACHED();
79     }
80     if (DownloadPathIsDangerous(path_)) {
81       // This is only useful on platforms that support
82       // DIR_DEFAULT_DOWNLOADS_SAFE.
83       if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE, &path_)) {
84         NOTREACHED();
85       }
86     }
87   }
88
89   base::FilePath path_;
90
91   DISALLOW_COPY_AND_ASSIGN(DefaultDownloadDirectory);
92 };
93
94 static base::LazyInstance<DefaultDownloadDirectory>
95     g_default_download_directory = LAZY_INSTANCE_INITIALIZER;
96
97 }  // namespace
98
99 DownloadPrefs::DownloadPrefs(Profile* profile) : profile_(profile) {
100   PrefService* prefs = profile->GetPrefs();
101
102 #if defined(OS_CHROMEOS)
103   // On Chrome OS, the default download directory is different for each profile.
104   // If the profile-unaware default path (from GetDefaultDownloadDirectory())
105   // is set (this happens during the initial preference registration in static
106   // RegisterProfilePrefs()), alter by GetDefaultDownloadDirectoryForProfile().
107   // file_manager::util::MigratePathFromOldFormat will do this.
108   const char* path_pref[] = {
109       prefs::kSaveFileDefaultDirectory,
110       prefs::kDownloadDefaultDirectory
111   };
112   for (size_t i = 0; i < arraysize(path_pref); ++i) {
113     const base::FilePath current = prefs->GetFilePath(path_pref[i]);
114     base::FilePath migrated;
115     if (!current.empty() &&
116         file_manager::util::MigratePathFromOldFormat(
117             profile_, current, &migrated)) {
118       prefs->SetFilePath(path_pref[i], migrated);
119     }
120   }
121
122   // Ensure that the default download directory exists.
123   BrowserThread::PostTask(
124       BrowserThread::FILE, FROM_HERE,
125       base::Bind(base::IgnoreResult(&base::CreateDirectory),
126                  GetDefaultDownloadDirectoryForProfile()));
127 #endif  // defined(OS_CHROMEOS)
128
129   // 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
131   // the user if he really wants it on an unsafe place such as the desktop.
132   if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) {
133     base::FilePath current_download_dir = prefs->GetFilePath(
134         prefs::kDownloadDefaultDirectory);
135     if (DownloadPathIsDangerous(current_download_dir)) {
136       prefs->SetFilePath(prefs::kDownloadDefaultDirectory,
137                          GetDefaultDownloadDirectoryForProfile());
138     }
139     prefs->SetBoolean(prefs::kDownloadDirUpgraded, true);
140   }
141
142   prompt_for_download_.Init(prefs::kPromptForDownload, prefs);
143   download_path_.Init(prefs::kDownloadDefaultDirectory, prefs);
144   save_file_path_.Init(prefs::kSaveFileDefaultDirectory, prefs);
145   save_file_type_.Init(prefs::kSaveFileType, prefs);
146
147   // We store any file extension that should be opened automatically at
148   // download completion in this pref.
149   std::string extensions_to_open =
150       prefs->GetString(prefs::kDownloadExtensionsToOpen);
151   std::vector<std::string> extensions;
152   base::SplitString(extensions_to_open, ':', &extensions);
153
154   for (size_t i = 0; i < extensions.size(); ++i) {
155 #if defined(OS_POSIX)
156     base::FilePath path(extensions[i]);
157 #elif defined(OS_WIN)
158     base::FilePath path(base::UTF8ToWide(extensions[i]));
159 #endif
160     if (!extensions[i].empty() &&
161         download_util::GetFileDangerLevel(path) == download_util::NOT_DANGEROUS)
162       auto_open_.insert(path.value());
163   }
164 }
165
166 DownloadPrefs::~DownloadPrefs() {}
167
168 // static
169 void DownloadPrefs::RegisterProfilePrefs(
170     user_prefs::PrefRegistrySyncable* registry) {
171   registry->RegisterBooleanPref(
172       prefs::kPromptForDownload,
173       false,
174       user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
175   registry->RegisterStringPref(
176       prefs::kDownloadExtensionsToOpen,
177       std::string(),
178       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
179   registry->RegisterBooleanPref(
180       prefs::kDownloadDirUpgraded,
181       false,
182       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
183   registry->RegisterIntegerPref(
184       prefs::kSaveFileType,
185       content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML,
186       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
187
188   const base::FilePath& default_download_path = GetDefaultDownloadDirectory();
189   registry->RegisterFilePathPref(
190       prefs::kDownloadDefaultDirectory,
191       default_download_path,
192       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
193   registry->RegisterFilePathPref(
194       prefs::kSaveFileDefaultDirectory,
195       default_download_path,
196       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
197 }
198
199 base::FilePath DownloadPrefs::GetDefaultDownloadDirectoryForProfile() const {
200 #if defined(OS_CHROMEOS)
201   return file_manager::util::GetDownloadsFolderForProfile(profile_);
202 #else
203   return GetDefaultDownloadDirectory();
204 #endif
205 }
206
207 // static
208 const base::FilePath& DownloadPrefs::GetDefaultDownloadDirectory() {
209   return g_default_download_directory.Get().path();
210 }
211
212 // static
213 DownloadPrefs* DownloadPrefs::FromDownloadManager(
214     DownloadManager* download_manager) {
215   ChromeDownloadManagerDelegate* delegate =
216       static_cast<ChromeDownloadManagerDelegate*>(
217           download_manager->GetDelegate());
218   return delegate->download_prefs();
219 }
220
221 // static
222 DownloadPrefs* DownloadPrefs::FromBrowserContext(
223     content::BrowserContext* context) {
224   return FromDownloadManager(BrowserContext::GetDownloadManager(context));
225 }
226
227 base::FilePath DownloadPrefs::DownloadPath() const {
228 #if defined(OS_CHROMEOS)
229   // If the download path is under /drive, and DriveIntegrationService isn't
230   // available (which it isn't for incognito mode, for instance), use the
231   // default download directory (/Downloads).
232   if (drive::util::IsUnderDriveMountPoint(*download_path_)) {
233     drive::DriveIntegrationService* integration_service =
234         drive::DriveIntegrationServiceFactory::FindForProfile(profile_);
235     if (!integration_service || !integration_service->is_enabled())
236       return GetDefaultDownloadDirectoryForProfile();
237   }
238 #endif
239   return *download_path_;
240 }
241
242 void DownloadPrefs::SetDownloadPath(const base::FilePath& path) {
243   download_path_.SetValue(path);
244   SetSaveFilePath(path);
245 }
246
247 base::FilePath DownloadPrefs::SaveFilePath() const {
248   return *save_file_path_;
249 }
250
251 void DownloadPrefs::SetSaveFilePath(const base::FilePath& path) {
252   save_file_path_.SetValue(path);
253 }
254
255 void DownloadPrefs::SetSaveFileType(int type) {
256   save_file_type_.SetValue(type);
257 }
258
259 bool DownloadPrefs::PromptForDownload() const {
260   // If the DownloadDirectory policy is set, then |prompt_for_download_| should
261   // always be false.
262   DCHECK(!download_path_.IsManaged() || !prompt_for_download_.GetValue());
263   return *prompt_for_download_;
264 }
265
266 bool DownloadPrefs::IsDownloadPathManaged() const {
267   return download_path_.IsManaged();
268 }
269
270 bool DownloadPrefs::IsAutoOpenUsed() const {
271   return !auto_open_.empty();
272 }
273
274 bool DownloadPrefs::IsAutoOpenEnabledBasedOnExtension(
275     const base::FilePath& path) const {
276   base::FilePath::StringType extension = path.Extension();
277   if (extension.empty())
278     return false;
279   DCHECK(extension[0] == base::FilePath::kExtensionSeparator);
280   extension.erase(0, 1);
281   return auto_open_.find(extension) != auto_open_.end();
282 }
283
284 bool DownloadPrefs::EnableAutoOpenBasedOnExtension(
285     const base::FilePath& file_name) {
286   base::FilePath::StringType extension = file_name.Extension();
287   if (extension.empty())
288     return false;
289   DCHECK(extension[0] == base::FilePath::kExtensionSeparator);
290   extension.erase(0, 1);
291
292   auto_open_.insert(extension);
293   SaveAutoOpenState();
294   return true;
295 }
296
297 void DownloadPrefs::DisableAutoOpenBasedOnExtension(
298     const base::FilePath& file_name) {
299   base::FilePath::StringType extension = file_name.Extension();
300   if (extension.empty())
301     return;
302   DCHECK(extension[0] == base::FilePath::kExtensionSeparator);
303   extension.erase(0, 1);
304   auto_open_.erase(extension);
305   SaveAutoOpenState();
306 }
307
308 void DownloadPrefs::ResetAutoOpen() {
309   auto_open_.clear();
310   SaveAutoOpenState();
311 }
312
313 void DownloadPrefs::SaveAutoOpenState() {
314   std::string extensions;
315   for (AutoOpenSet::iterator it = auto_open_.begin();
316        it != auto_open_.end(); ++it) {
317 #if defined(OS_POSIX)
318     std::string this_extension = *it;
319 #elif defined(OS_WIN)
320     // TODO(phajdan.jr): Why we're using Sys conversion here, but not in ctor?
321     std::string this_extension = base::SysWideToUTF8(*it);
322 #endif
323     extensions += this_extension + ":";
324   }
325   if (!extensions.empty())
326     extensions.erase(extensions.size() - 1);
327
328   profile_->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, extensions);
329 }
330
331 bool DownloadPrefs::AutoOpenCompareFunctor::operator()(
332     const base::FilePath::StringType& a,
333     const base::FilePath::StringType& b) const {
334   return base::FilePath::CompareLessIgnoreCase(a, b);
335 }