Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(344)

Side by Side Diff: chrome/browser/download/download_prefs.cc

Issue 6973052: When the download folder does not exist, change the download folder to a user's "Downloads" (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Reflected Pawel's comments Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/string_split.h" 9 #include "base/string_split.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 prefs->RegisterStringPref(prefs::kDownloadExtensionsToOpen, 53 prefs->RegisterStringPref(prefs::kDownloadExtensionsToOpen,
54 "", 54 "",
55 PrefService::UNSYNCABLE_PREF); 55 PrefService::UNSYNCABLE_PREF);
56 prefs->RegisterBooleanPref(prefs::kDownloadDirUpgraded, 56 prefs->RegisterBooleanPref(prefs::kDownloadDirUpgraded,
57 false, 57 false,
58 PrefService::UNSYNCABLE_PREF); 58 PrefService::UNSYNCABLE_PREF);
59 prefs->RegisterIntegerPref(prefs::kSaveFileType, 59 prefs->RegisterIntegerPref(prefs::kSaveFileType,
60 SavePackage::SAVE_AS_COMPLETE_HTML, 60 SavePackage::SAVE_AS_COMPLETE_HTML,
61 PrefService::UNSYNCABLE_PREF); 61 PrefService::UNSYNCABLE_PREF);
62 62
63 // The default download path is userprofile\download. 63 // The user's default "Downloads" folder.
64 const FilePath& default_download_path = 64 FilePath default_download_dir =
65 download_util::GetDefaultDownloadDirectory(); 65 download_util::GetDefaultDownloadDirectoryFromPathService();
66 prefs->RegisterFilePathPref(prefs::kDownloadDefaultDirectory, 66 prefs->RegisterFilePathPref(prefs::kDownloadDefaultDirectory,
67 default_download_path, 67 default_download_dir,
68 PrefService::UNSYNCABLE_PREF); 68 PrefService::UNSYNCABLE_PREF);
69 69
70 #if defined(OS_CHROMEOS) 70 #if defined(OS_CHROMEOS)
71 // Ensure that the download directory specified in the preferences exists. 71 // Ensure that the download directory specified in the preferences exists.
72 BrowserThread::PostTask( 72 BrowserThread::PostTask(
73 BrowserThread::FILE, FROM_HERE, 73 BrowserThread::FILE, FROM_HERE,
74 NewRunnableFunction(&file_util::CreateDirectory, default_download_path)); 74 NewRunnableFunction(&file_util::CreateDirectory, default_download_dir));
75 #endif // defined(OS_CHROMEOS) 75 #endif // defined(OS_CHROMEOS)
76 76
77 // If the download path is dangerous we forcefully reset it. But if we do 77 // If the download path is dangerous we forcefully reset it. But if we do
78 // so we set a flag to make sure we only do it once, to avoid fighting 78 // so we set a flag to make sure we only do it once, to avoid fighting
79 // the user if he really wants it on an unsafe place such as the desktop. 79 // the user if he really wants it on an unsafe place such as the desktop.
80 if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) { 80 if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) {
81 FilePath current_download_dir = prefs->GetFilePath( 81 FilePath current_download_dir = prefs->GetFilePath(
82 prefs::kDownloadDefaultDirectory); 82 prefs::kDownloadDefaultDirectory);
83 if (download_util::DownloadPathIsDangerous(current_download_dir)) { 83 if (download_util::DownloadPathIsDangerous(current_download_dir)) {
84 prefs->SetFilePath(prefs::kDownloadDefaultDirectory, 84 prefs->SetFilePath(prefs::kDownloadDefaultDirectory,
85 default_download_path); 85 default_download_dir);
86 } 86 }
87 prefs->SetBoolean(prefs::kDownloadDirUpgraded, true); 87 prefs->SetBoolean(prefs::kDownloadDirUpgraded, true);
88 } 88 }
89 } 89 }
90 90
91 bool DownloadPrefs::PromptForDownload() const { 91 bool DownloadPrefs::PromptForDownload() const {
92 // If the DownloadDirectory policy is set, then |prompt_for_download_| should 92 // If the DownloadDirectory policy is set, then |prompt_for_download_| should
93 // always be false. 93 // always be false.
94 DCHECK(!download_path_.IsManaged() || !prompt_for_download_.GetValue()); 94 DCHECK(!download_path_.IsManaged() || !prompt_for_download_.GetValue());
95 return *prompt_for_download_; 95 return *prompt_for_download_;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 extension.erase(0, 1); 128 extension.erase(0, 1);
129 auto_open_.erase(extension); 129 auto_open_.erase(extension);
130 SaveAutoOpenState(); 130 SaveAutoOpenState();
131 } 131 }
132 132
133 void DownloadPrefs::ResetAutoOpen() { 133 void DownloadPrefs::ResetAutoOpen() {
134 auto_open_.clear(); 134 auto_open_.clear();
135 SaveAutoOpenState(); 135 SaveAutoOpenState();
136 } 136 }
137 137
138 FilePath DownloadPrefs::GetDefaultDownloadDirectory() {
139 if (!override_default_download_dir_.empty())
140 return override_default_download_dir_;
141 return download_util::GetDefaultDownloadDirectoryFromPathService();
142 }
143
138 void DownloadPrefs::SaveAutoOpenState() { 144 void DownloadPrefs::SaveAutoOpenState() {
139 std::string extensions; 145 std::string extensions;
140 for (AutoOpenSet::iterator it = auto_open_.begin(); 146 for (AutoOpenSet::iterator it = auto_open_.begin();
141 it != auto_open_.end(); ++it) { 147 it != auto_open_.end(); ++it) {
142 #if defined(OS_POSIX) 148 #if defined(OS_POSIX)
143 std::string this_extension = *it; 149 std::string this_extension = *it;
144 #elif defined(OS_WIN) 150 #elif defined(OS_WIN)
145 // TODO(phajdan.jr): Why we're using Sys conversion here, but not in ctor? 151 // TODO(phajdan.jr): Why we're using Sys conversion here, but not in ctor?
146 std::string this_extension = base::SysWideToUTF8(*it); 152 std::string this_extension = base::SysWideToUTF8(*it);
147 #endif 153 #endif
148 extensions += this_extension + ":"; 154 extensions += this_extension + ":";
149 } 155 }
150 if (!extensions.empty()) 156 if (!extensions.empty())
151 extensions.erase(extensions.size() - 1); 157 extensions.erase(extensions.size() - 1);
152 158
153 prefs_->SetString(prefs::kDownloadExtensionsToOpen, extensions); 159 prefs_->SetString(prefs::kDownloadExtensionsToOpen, extensions);
154 } 160 }
155 161
162 void DownloadPrefs::OverrideDefaultDownloadDirectory(
Randy Smith (Not in Mondays) 2011/06/15 19:48:59 I wince a bit at the behavior of these functions w
Paweł Hajdan Jr. 2011/06/16 17:41:32 A linked list/stack just for this is obviously an
haraken1 2011/06/22 18:01:58 I documented "Duplicate overriding is not allowed.
163 const FilePath& override_default_download_dir) {
164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
165
166 FilePath default_download_dir = GetDefaultDownloadDirectory();
167 if (*download_path_ == default_download_dir)
168 download_path_.SetValue(override_default_download_dir);
169
170 override_default_download_dir_ = override_default_download_dir;
171 }
172
173 void DownloadPrefs::UnOverrideDefaultDownloadDirectory() {
174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
175
176 DCHECK(!override_default_download_dir_.empty());
177 override_default_download_dir_.clear();
178
179 FilePath default_download_dir = GetDefaultDownloadDirectory();
180 download_path_.SetValue(default_download_dir);
181 }
182
156 bool DownloadPrefs::AutoOpenCompareFunctor::operator()( 183 bool DownloadPrefs::AutoOpenCompareFunctor::operator()(
157 const FilePath::StringType& a, 184 const FilePath::StringType& a,
158 const FilePath::StringType& b) const { 185 const FilePath::StringType& b) const {
159 return FilePath::CompareLessIgnoreCase(a, b); 186 return FilePath::CompareLessIgnoreCase(a, b);
160 } 187 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698