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/save_package.h" | 5 #include "chrome/browser/download/save_package.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/i18n/file_util_icu.h" | 11 #include "base/i18n/file_util_icu.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 14 #include "base/path_service.h" |
14 #include "base/stl_util-inl.h" | 15 #include "base/stl_util-inl.h" |
15 #include "base/string_piece.h" | 16 #include "base/string_piece.h" |
16 #include "base/string_split.h" | 17 #include "base/string_split.h" |
17 #include "base/sys_string_conversions.h" | 18 #include "base/sys_string_conversions.h" |
18 #include "base/task.h" | 19 #include "base/task.h" |
19 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
20 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
21 #include "chrome/browser/browser_process.h" | 22 #include "chrome/browser/browser_process.h" |
22 #include "chrome/browser/download/download_item.h" | 23 #include "chrome/browser/download/download_item.h" |
23 #include "chrome/browser/download/download_item_model.h" | 24 #include "chrome/browser/download/download_item_model.h" |
(...skipping 1236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1260 // before calling to it. | 1261 // before calling to it. |
1261 PrefService* prefs = tab_contents()->profile()->GetPrefs(); | 1262 PrefService* prefs = tab_contents()->profile()->GetPrefs(); |
1262 FilePath website_save_dir = GetSaveDirPreference(prefs); | 1263 FilePath website_save_dir = GetSaveDirPreference(prefs); |
1263 FilePath download_save_dir = prefs->GetFilePath( | 1264 FilePath download_save_dir = prefs->GetFilePath( |
1264 prefs::kDownloadDefaultDirectory); | 1265 prefs::kDownloadDefaultDirectory); |
1265 std::string mime_type = tab_contents()->contents_mime_type(); | 1266 std::string mime_type = tab_contents()->contents_mime_type(); |
1266 | 1267 |
1267 BrowserThread::PostTask( | 1268 BrowserThread::PostTask( |
1268 BrowserThread::FILE, FROM_HERE, | 1269 BrowserThread::FILE, FROM_HERE, |
1269 NewRunnableMethod(this, &SavePackage::CreateDirectoryOnFileThread, | 1270 NewRunnableMethod(this, &SavePackage::CreateDirectoryOnFileThread, |
1270 website_save_dir, download_save_dir, mime_type)); | 1271 website_save_dir, download_save_dir, mime_type)); |
1271 } | 1272 } |
1272 | 1273 |
1273 void SavePackage::CreateDirectoryOnFileThread( | 1274 void SavePackage::CreateDirectoryOnFileThread( |
1274 const FilePath& website_save_dir, | 1275 const FilePath& website_save_dir, |
1275 const FilePath& download_save_dir, | 1276 const FilePath& download_save_dir, |
1276 const std::string& mime_type) { | 1277 const std::string& mime_type) { |
1277 FilePath save_dir; | 1278 FilePath save_dir; |
1278 // If the default html/websites save folder doesn't exist... | 1279 if (file_util::DirectoryExists(website_save_dir)) { |
1279 if (!file_util::DirectoryExists(website_save_dir)) { | 1280 // If the default html/websites save folder exists, |
1280 // If the default download dir doesn't exist, create it. | 1281 // then use the default html/websites save folder. |
1281 if (!file_util::DirectoryExists(download_save_dir)) | 1282 save_dir = website_save_dir; |
1282 file_util::CreateDirectory(download_save_dir); | 1283 } else if (file_util::DirectoryExists(download_save_dir)) { |
| 1284 // If the default html/websites save folder does not exist |
| 1285 // but the default download folder exists, |
| 1286 // then use the default download folder. |
1283 save_dir = download_save_dir; | 1287 save_dir = download_save_dir; |
1284 } else { | 1288 } else { |
1285 // If it does exist, use the default save dir param. | 1289 // If both the above folders do not exist, |
1286 save_dir = website_save_dir; | 1290 // use the user's "Downloads" folder. |
| 1291 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &save_dir)) { |
| 1292 // Create the |download_save_dir| folder if we cannot get |
| 1293 // DIR_DEFAULT_DOWNLOADS (This will be a rare case). |
| 1294 save_dir = download_save_dir; |
| 1295 } |
| 1296 // Make sure that the folder does exist. |
| 1297 if (!file_util::CreateDirectory(save_dir)) |
| 1298 LOG(ERROR) << "Failed to create " << save_dir.value(); |
1287 } | 1299 } |
1288 | 1300 |
1289 bool can_save_as_complete = CanSaveAsComplete(mime_type); | 1301 bool can_save_as_complete = CanSaveAsComplete(mime_type); |
1290 FilePath suggested_filename = GetSuggestedNameForSaveAs(can_save_as_complete, | 1302 FilePath suggested_filename = GetSuggestedNameForSaveAs(can_save_as_complete, |
1291 mime_type); | 1303 mime_type); |
1292 FilePath::StringType pure_file_name = | 1304 FilePath::StringType pure_file_name = |
1293 suggested_filename.RemoveExtension().BaseName().value(); | 1305 suggested_filename.RemoveExtension().BaseName().value(); |
1294 FilePath::StringType file_name_ext = suggested_filename.Extension(); | 1306 FilePath::StringType file_name_ext = suggested_filename.Extension(); |
1295 | 1307 |
1296 // Need to make sure the suggested file name is not too long. | 1308 // Need to make sure the suggested file name is not too long. |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1466 } | 1478 } |
1467 | 1479 |
1468 // SelectFileDialog::Listener interface. | 1480 // SelectFileDialog::Listener interface. |
1469 void SavePackage::FileSelected(const FilePath& path, | 1481 void SavePackage::FileSelected(const FilePath& path, |
1470 int index, void* params) { | 1482 int index, void* params) { |
1471 ContinueSave(path, index); | 1483 ContinueSave(path, index); |
1472 } | 1484 } |
1473 | 1485 |
1474 void SavePackage::FileSelectionCanceled(void* params) { | 1486 void SavePackage::FileSelectionCanceled(void* params) { |
1475 } | 1487 } |
OLD | NEW |