OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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_path_.empty()) | |
140 return override_path_; | |
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( | |
163 const FilePath& override_path) { | |
164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
165 | |
166 FilePath default_download_path = GetDefaultDownloadDirectory(); | |
167 if (*download_path_ == default_download_path) { | |
168 download_path_.SetValue(override_path); | |
Paweł Hajdan Jr.
2011/06/15 09:31:33
Aren't those two lines (SetValue and SetFilePath)
haraken1
2011/06/15 10:39:45
Done. Just SetValue() is OK.
| |
169 prefs_->SetFilePath(prefs::kDownloadDefaultDirectory, override_path); | |
170 } | |
171 | |
172 override_path_ = override_path; | |
173 } | |
174 | |
175 void DownloadPrefs::UnOverrideDefaultDownloadDirectory() { | |
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
177 | |
178 bool overridden = !override_path_.empty(); | |
Paweł Hajdan Jr.
2011/06/15 09:31:33
I think we can always assume (DCHECK if needed) th
haraken1
2011/06/15 10:39:45
Done.
| |
179 override_path_.clear(); | |
180 | |
181 if (overridden) { | |
Paweł Hajdan Jr.
2011/06/15 09:31:33
For symmetry, shouldn't we only reset the pref to
haraken1
2011/06/15 10:39:45
Done, although I am not sure if it is the fix you
| |
182 FilePath default_download_path = GetDefaultDownloadDirectory(); | |
183 download_path_.SetValue(default_download_path); | |
184 prefs_->SetFilePath(prefs::kDownloadDefaultDirectory, | |
185 default_download_path); | |
186 } | |
187 } | |
188 | |
156 bool DownloadPrefs::AutoOpenCompareFunctor::operator()( | 189 bool DownloadPrefs::AutoOpenCompareFunctor::operator()( |
157 const FilePath::StringType& a, | 190 const FilePath::StringType& a, |
158 const FilePath::StringType& b) const { | 191 const FilePath::StringType& b) const { |
159 return FilePath::CompareLessIgnoreCase(a, b); | 192 return FilePath::CompareLessIgnoreCase(a, b); |
160 } | 193 } |
OLD | NEW |