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)); |
1272 } | |
1273 | |
1274 // Used in automated testing to explicitly specify the default folder | |
1275 // for saving HTML and the default folder for saving downloaded files. | |
1276 void SavePackage::GetSaveInfo(const FilePath& website_save_dir, | |
Paweł Hajdan Jr.
2011/05/30 14:15:49
This duplicates most of SavePackage::GetSaveInfo()
haraken1
2011/05/31 12:41:31
I also first thought that I should avoid this dupl
Paweł Hajdan Jr.
2011/05/31 17:15:53
Why do we need to have this special variant for te
haraken1
2011/06/02 09:13:22
Done.
| |
1277 const FilePath& download_save_dir, | |
1278 SavePackageType save_type) { | |
1279 // Can't use tab_contents_ in the file thread, so get the data that we need | |
1280 // before calling to it. | |
1281 PrefService* prefs = tab_contents()->profile()->GetPrefs(); | |
1282 GetSaveDirPreference(prefs); | |
1283 prefs->SetInteger(prefs::kSaveFileType, save_type); | |
1284 std::string mime_type = tab_contents()->contents_mime_type(); | |
1285 | |
1286 BrowserThread::PostTask( | |
1287 BrowserThread::FILE, FROM_HERE, | |
1288 NewRunnableMethod(this, &SavePackage::CreateDirectoryOnFileThread, | |
1289 website_save_dir, download_save_dir, mime_type)); | |
1271 } | 1290 } |
1272 | 1291 |
1273 void SavePackage::CreateDirectoryOnFileThread( | 1292 void SavePackage::CreateDirectoryOnFileThread( |
1274 const FilePath& website_save_dir, | 1293 const FilePath& website_save_dir, |
1275 const FilePath& download_save_dir, | 1294 const FilePath& download_save_dir, |
1276 const std::string& mime_type) { | 1295 const std::string& mime_type) { |
1277 FilePath save_dir; | 1296 FilePath save_dir; |
1278 // If the default html/websites save folder doesn't exist... | 1297 if (file_util::DirectoryExists(website_save_dir)) { |
1279 if (!file_util::DirectoryExists(website_save_dir)) { | 1298 // If the default html/websites save folder exists, |
1280 // If the default download dir doesn't exist, create it. | 1299 // then use the default html/websites save folder. |
1281 if (!file_util::DirectoryExists(download_save_dir)) | 1300 save_dir = website_save_dir; |
1282 file_util::CreateDirectory(download_save_dir); | 1301 } else if (file_util::DirectoryExists(download_save_dir)) { |
1302 // If the default html/websites save folder does not exist | |
1303 // but the default download folder exists, | |
1304 // then use the default download folder. | |
1283 save_dir = download_save_dir; | 1305 save_dir = download_save_dir; |
1284 } else { | 1306 } else { |
1285 // If it does exist, use the default save dir param. | 1307 // If both the above folders do not exist, |
1286 save_dir = website_save_dir; | 1308 // use the user's "Downloads" folder. |
1309 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &save_dir)) { | |
1310 // Create the |download_save_dir| folder if we cannot get | |
1311 // DIR_DEFAULT_DOWNLOADS (This will be a rare case). | |
1312 save_dir = download_save_dir; | |
1313 } | |
1314 // Make sure that the folder does exist. | |
1315 file_util::CreateDirectory(save_dir); | |
Paweł Hajdan Jr.
2011/05/30 14:15:49
This call can still fail (for example because of a
haraken1
2011/05/31 12:41:31
I just added an error message when CreateDirectory
| |
1287 } | 1316 } |
1288 | 1317 |
1289 bool can_save_as_complete = CanSaveAsComplete(mime_type); | 1318 bool can_save_as_complete = CanSaveAsComplete(mime_type); |
1290 FilePath suggested_filename = GetSuggestedNameForSaveAs(can_save_as_complete, | 1319 FilePath suggested_filename = GetSuggestedNameForSaveAs(can_save_as_complete, |
1291 mime_type); | 1320 mime_type); |
1292 FilePath::StringType pure_file_name = | 1321 FilePath::StringType pure_file_name = |
1293 suggested_filename.RemoveExtension().BaseName().value(); | 1322 suggested_filename.RemoveExtension().BaseName().value(); |
1294 FilePath::StringType file_name_ext = suggested_filename.Extension(); | 1323 FilePath::StringType file_name_ext = suggested_filename.Extension(); |
1295 | 1324 |
1296 // Need to make sure the suggested file name is not too long. | 1325 // 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 } | 1495 } |
1467 | 1496 |
1468 // SelectFileDialog::Listener interface. | 1497 // SelectFileDialog::Listener interface. |
1469 void SavePackage::FileSelected(const FilePath& path, | 1498 void SavePackage::FileSelected(const FilePath& path, |
1470 int index, void* params) { | 1499 int index, void* params) { |
1471 ContinueSave(path, index); | 1500 ContinueSave(path, index); |
1472 } | 1501 } |
1473 | 1502 |
1474 void SavePackage::FileSelectionCanceled(void* params) { | 1503 void SavePackage::FileSelectionCanceled(void* params) { |
1475 } | 1504 } |
OLD | NEW |