OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <time.h> | 5 #include <time.h> |
6 | 6 |
7 #include "chrome/browser/download_manager.h" | 7 #include "chrome/browser/download_manager.h" |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 | 75 |
76 if (!file_util::PathExists(new_path)) { | 76 if (!file_util::PathExists(new_path)) { |
77 path->swap(new_path); | 77 path->swap(new_path); |
78 return true; | 78 return true; |
79 } | 79 } |
80 } | 80 } |
81 | 81 |
82 return false; | 82 return false; |
83 } | 83 } |
84 | 84 |
| 85 static bool DownloadPathIsDangerous(const std::wstring& download_path) { |
| 86 std::wstring desktop_dir; |
| 87 if (!PathService::Get(chrome::DIR_USER_DESKTOP, &desktop_dir)) { |
| 88 NOTREACHED(); |
| 89 return false; |
| 90 } |
| 91 return (download_path == desktop_dir); |
| 92 } |
| 93 |
85 // DownloadItem implementation ------------------------------------------------- | 94 // DownloadItem implementation ------------------------------------------------- |
86 | 95 |
87 // Constructor for reading from the history service. | 96 // Constructor for reading from the history service. |
88 DownloadItem::DownloadItem(const DownloadCreateInfo& info) | 97 DownloadItem::DownloadItem(const DownloadCreateInfo& info) |
89 : id_(-1), | 98 : id_(-1), |
90 full_path_(info.path), | 99 full_path_(info.path), |
91 url_(info.url), | 100 url_(info.url), |
92 total_bytes_(info.total_bytes), | 101 total_bytes_(info.total_bytes), |
93 received_bytes_(info.received_bytes), | 102 received_bytes_(info.received_bytes), |
94 start_tick_(0), | 103 start_tick_(0), |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 is_paused_ = !is_paused_; | 254 is_paused_ = !is_paused_; |
246 UpdateObservers(); | 255 UpdateObservers(); |
247 } | 256 } |
248 | 257 |
249 // DownloadManager implementation ---------------------------------------------- | 258 // DownloadManager implementation ---------------------------------------------- |
250 | 259 |
251 // static | 260 // static |
252 void DownloadManager::RegisterUserPrefs(PrefService* prefs) { | 261 void DownloadManager::RegisterUserPrefs(PrefService* prefs) { |
253 prefs->RegisterBooleanPref(prefs::kPromptForDownload, false); | 262 prefs->RegisterBooleanPref(prefs::kPromptForDownload, false); |
254 prefs->RegisterStringPref(prefs::kDownloadExtensionsToOpen, L""); | 263 prefs->RegisterStringPref(prefs::kDownloadExtensionsToOpen, L""); |
| 264 prefs->RegisterBooleanPref(prefs::kDownloadDirUpgraded, false); |
| 265 |
| 266 // The default download path is userprofile\download. |
| 267 std::wstring default_download_path; |
| 268 if (!PathService::Get(chrome::DIR_USER_DOCUMENTS, &default_download_path)) { |
| 269 NOTREACHED(); |
| 270 } |
| 271 file_util::AppendToPath(&default_download_path, |
| 272 l10n_util::GetString(IDS_DOWNLOAD_DIRECTORY)); |
| 273 prefs->RegisterStringPref(prefs::kDownloadDefaultDirectory, |
| 274 default_download_path); |
| 275 |
| 276 // If the download path is dangerous we forcefully reset it. But if we do |
| 277 // so we set a flag to make sure we only do it once, to avoid fighting |
| 278 // the user if he really wants it on an unsafe place such as the desktop. |
| 279 |
| 280 if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) { |
| 281 std::wstring current_download_dir = |
| 282 prefs->GetString(prefs::kDownloadDefaultDirectory); |
| 283 if (DownloadPathIsDangerous(current_download_dir)) { |
| 284 prefs->SetString(prefs::kDownloadDefaultDirectory, |
| 285 default_download_path); |
| 286 } |
| 287 prefs->SetBoolean(prefs::kDownloadDirUpgraded, true); |
| 288 } |
255 } | 289 } |
256 | 290 |
257 DownloadManager::DownloadManager() | 291 DownloadManager::DownloadManager() |
258 : shutdown_needed_(false), | 292 : shutdown_needed_(false), |
259 profile_(NULL), | 293 profile_(NULL), |
260 file_manager_(NULL), | 294 file_manager_(NULL), |
261 ui_loop_(MessageLoop::current()), | 295 ui_loop_(MessageLoop::current()), |
262 file_loop_(NULL) { | 296 file_loop_(NULL) { |
263 } | 297 } |
264 | 298 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 if (!file_loop_) { | 414 if (!file_loop_) { |
381 NOTREACHED(); | 415 NOTREACHED(); |
382 return false; | 416 return false; |
383 } | 417 } |
384 | 418 |
385 // Get our user preference state. | 419 // Get our user preference state. |
386 PrefService* prefs = profile_->GetPrefs(); | 420 PrefService* prefs = profile_->GetPrefs(); |
387 DCHECK(prefs); | 421 DCHECK(prefs); |
388 prompt_for_download_.Init(prefs::kPromptForDownload, prefs, NULL); | 422 prompt_for_download_.Init(prefs::kPromptForDownload, prefs, NULL); |
389 | 423 |
390 // Use the IE download directory on Vista, if available. | |
391 std::wstring default_download_path; | |
392 if (win_util::GetWinVersion() == win_util::WINVERSION_VISTA) { | |
393 RegKey vista_reg(HKEY_CURRENT_USER, | |
394 L"Software\\Microsoft\\Internet Explorer", KEY_READ); | |
395 const wchar_t* const vista_dir = L"Download Directory"; | |
396 if (vista_reg.ValueExists(vista_dir)) | |
397 vista_reg.ReadValue(vista_dir, &default_download_path); | |
398 } | |
399 if (default_download_path.empty()) { | |
400 PathService::Get(chrome::DIR_USER_DOCUMENTS, &default_download_path); | |
401 file_util::AppendToPath(&default_download_path, | |
402 l10n_util::GetString(IDS_DOWNLOAD_DIRECTORY)); | |
403 } | |
404 // Check if the pref has already been registered, as the user profile and the | |
405 // "off the record" profile might register it. | |
406 if (!prefs->IsPrefRegistered(prefs::kDownloadDefaultDirectory)) | |
407 prefs->RegisterStringPref(prefs::kDownloadDefaultDirectory, | |
408 default_download_path); | |
409 download_path_.Init(prefs::kDownloadDefaultDirectory, prefs, NULL); | 424 download_path_.Init(prefs::kDownloadDefaultDirectory, prefs, NULL); |
410 | 425 |
411 // Ensure that the download directory specified in the preferences exists. | 426 // Ensure that the download directory specified in the preferences exists. |
412 file_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 427 file_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
413 file_manager_, &DownloadFileManager::CreateDirectory, *download_path_)); | 428 file_manager_, &DownloadFileManager::CreateDirectory, *download_path_)); |
414 | 429 |
415 // We store any file extension that should be opened automatically at | 430 // We store any file extension that should be opened automatically at |
416 // download completion in this pref. | 431 // download completion in this pref. |
417 download_util::InitializeExeTypes(&exe_types_); | 432 download_util::InitializeExeTypes(&exe_types_); |
418 | 433 |
(...skipping 661 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1080 for (std::vector<int64>::iterator it = results->begin(); | 1095 for (std::vector<int64>::iterator it = results->begin(); |
1081 it != results->end(); ++it) { | 1096 it != results->end(); ++it) { |
1082 DownloadMap::iterator dit = downloads_.find(*it); | 1097 DownloadMap::iterator dit = downloads_.find(*it); |
1083 if (dit != downloads_.end()) | 1098 if (dit != downloads_.end()) |
1084 searched_downloads.push_back(dit->second); | 1099 searched_downloads.push_back(dit->second); |
1085 } | 1100 } |
1086 | 1101 |
1087 requestor->SetDownloads(searched_downloads); | 1102 requestor->SetDownloads(searched_downloads); |
1088 } | 1103 } |
1089 | 1104 |
OLD | NEW |