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" |
(...skipping 1238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1249 | 1249 |
1250 // Get the directory from preference. | 1250 // Get the directory from preference. |
1251 FilePath save_file_path = prefs->GetFilePath( | 1251 FilePath save_file_path = prefs->GetFilePath( |
1252 prefs::kSaveFileDefaultDirectory); | 1252 prefs::kSaveFileDefaultDirectory); |
1253 DCHECK(!save_file_path.empty()); | 1253 DCHECK(!save_file_path.empty()); |
1254 | 1254 |
1255 return save_file_path; | 1255 return save_file_path; |
1256 } | 1256 } |
1257 | 1257 |
1258 void SavePackage::GetSaveInfo() { | 1258 void SavePackage::GetSaveInfo() { |
1259 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
1260 | |
1261 // Can't use tab_contents_ in the file thread, so get the data that we need | 1259 // Can't use tab_contents_ in the file thread, so get the data that we need |
1262 // before calling to it. | 1260 // before calling to it. |
1263 PrefService* prefs = tab_contents()->profile()->GetPrefs(); | 1261 PrefService* prefs = tab_contents()->profile()->GetPrefs(); |
1264 FilePath website_save_dir = GetSaveDirPreference(prefs); | 1262 FilePath website_save_dir = GetSaveDirPreference(prefs); |
1265 FilePath download_save_dir = prefs->GetFilePath( | 1263 FilePath download_save_dir = prefs->GetFilePath( |
1266 prefs::kDownloadDefaultDirectory); | 1264 prefs::kDownloadDefaultDirectory); |
1267 std::string mime_type = tab_contents()->contents_mime_type(); | 1265 std::string mime_type = tab_contents()->contents_mime_type(); |
1268 | 1266 |
1269 BrowserThread::PostTask( | 1267 BrowserThread::PostTask( |
1270 BrowserThread::FILE, FROM_HERE, | 1268 BrowserThread::FILE, FROM_HERE, |
1271 NewRunnableMethod(this, &SavePackage::CreateDirectoryOnFileThread, | 1269 NewRunnableMethod(this, &SavePackage::CreateDirectoryOnFileThread, |
1272 website_save_dir, download_save_dir, mime_type)); | 1270 website_save_dir, download_save_dir, mime_type)); |
1273 } | 1271 } |
1274 | 1272 |
1275 void SavePackage::CreateDirectoryOnFileThread( | 1273 void SavePackage::CreateDirectoryOnFileThread( |
1276 const FilePath& website_save_dir, | 1274 const FilePath& website_save_dir, |
1277 const FilePath& download_save_dir, | 1275 const FilePath& download_save_dir, |
1278 const std::string& mime_type) { | 1276 const std::string& mime_type) { |
1279 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
1280 | |
1281 FilePath save_dir; | 1277 FilePath save_dir; |
1282 FilePath default_download_dir = | 1278 // If the default html/websites save folder doesn't exist... |
1283 download_util::GetDefaultDownloadDirectoryFromPathService(); | 1279 if (!file_util::DirectoryExists(website_save_dir)) { |
1284 // Ignores the returned value since the select file dialog should be | 1280 // If the default download dir doesn't exist, create it. |
1285 // displayed in any case. | 1281 if (!file_util::DirectoryExists(download_save_dir)) |
1286 download_util::ChooseSavableDirectory( | 1282 file_util::CreateDirectory(download_save_dir); |
1287 website_save_dir, download_save_dir, default_download_dir, &save_dir); | 1283 save_dir = download_save_dir; |
| 1284 } else { |
| 1285 // If it does exist, use the default save dir param. |
| 1286 save_dir = website_save_dir; |
| 1287 } |
1288 | 1288 |
1289 bool can_save_as_complete = CanSaveAsComplete(mime_type); | 1289 bool can_save_as_complete = CanSaveAsComplete(mime_type); |
1290 FilePath suggested_filename = GetSuggestedNameForSaveAs(can_save_as_complete, | 1290 FilePath suggested_filename = GetSuggestedNameForSaveAs(can_save_as_complete, |
1291 mime_type); | 1291 mime_type); |
1292 FilePath::StringType pure_file_name = | 1292 FilePath::StringType pure_file_name = |
1293 suggested_filename.RemoveExtension().BaseName().value(); | 1293 suggested_filename.RemoveExtension().BaseName().value(); |
1294 FilePath::StringType file_name_ext = suggested_filename.Extension(); | 1294 FilePath::StringType file_name_ext = suggested_filename.Extension(); |
1295 | 1295 |
1296 // Need to make sure the suggested file name is not too long. | 1296 // Need to make sure the suggested file name is not too long. |
1297 uint32 max_path = GetMaxPathLengthForDirectory(save_dir); | 1297 uint32 max_path = GetMaxPathLengthForDirectory(save_dir); |
1298 | 1298 |
1299 if (GetSafePureFileName(save_dir, file_name_ext, max_path, &pure_file_name)) { | 1299 if (GetSafePureFileName(save_dir, file_name_ext, max_path, &pure_file_name)) { |
1300 save_dir = save_dir.Append(pure_file_name + file_name_ext); | 1300 save_dir = save_dir.Append(pure_file_name + file_name_ext); |
1301 } else { | 1301 } else { |
1302 // Cannot create a shorter filename. This will cause the save as operation | 1302 // Cannot create a shorter filename. This will cause the save as operation |
1303 // to fail unless the user pick a shorter name. Continuing even though it | 1303 // to fail unless the user pick a shorter name. Continuing even though it |
1304 // will fail because returning means no save as popup for the user, which | 1304 // will fail because returning means no save as popup for the user, which |
1305 // is even more confusing. This case should be rare though. | 1305 // is even more confusing. This case should be rare though. |
1306 save_dir = save_dir.Append(suggested_filename); | 1306 save_dir = save_dir.Append(suggested_filename); |
1307 } | 1307 } |
1308 | 1308 |
1309 BrowserThread::PostTask( | 1309 BrowserThread::PostTask( |
1310 BrowserThread::UI, FROM_HERE, | 1310 BrowserThread::UI, FROM_HERE, |
1311 NewRunnableMethod(this, &SavePackage::ContinueGetSaveInfo, save_dir, | 1311 NewRunnableMethod(this, &SavePackage::ContinueGetSaveInfo, save_dir, |
1312 can_save_as_complete)); | 1312 can_save_as_complete)); |
1313 } | 1313 } |
1314 | 1314 |
1315 void SavePackage::ContinueGetSaveInfo(const FilePath& suggested_path, | 1315 void SavePackage::ContinueGetSaveInfo(const FilePath& suggested_path, |
1316 bool can_save_as_complete) { | 1316 bool can_save_as_complete) { |
1317 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
1318 | |
1319 // The TabContents which owns this SavePackage may have disappeared during | 1317 // The TabContents which owns this SavePackage may have disappeared during |
1320 // the UI->FILE->UI thread hop of | 1318 // the UI->FILE->UI thread hop of |
1321 // GetSaveInfo->CreateDirectoryOnFileThread->ContinueGetSaveInfo. | 1319 // GetSaveInfo->CreateDirectoryOnFileThread->ContinueGetSaveInfo. |
1322 if (!tab_contents()) | 1320 if (!tab_contents()) |
1323 return; | 1321 return; |
1324 DownloadPrefs* download_prefs = | 1322 DownloadPrefs* download_prefs = |
1325 tab_contents()->profile()->GetDownloadManager()->download_prefs(); | 1323 tab_contents()->profile()->GetDownloadManager()->download_prefs(); |
1326 int file_type_index = | 1324 int file_type_index = |
1327 SavePackageTypeToIndex( | 1325 SavePackageTypeToIndex( |
1328 static_cast<SavePackageType>(download_prefs->save_file_type())); | 1326 static_cast<SavePackageType>(download_prefs->save_file_type())); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1399 NULL); | 1397 NULL); |
1400 } else { | 1398 } else { |
1401 // Just use 'suggested_path' instead of opening the dialog prompt. | 1399 // Just use 'suggested_path' instead of opening the dialog prompt. |
1402 ContinueSave(suggested_path, file_type_index); | 1400 ContinueSave(suggested_path, file_type_index); |
1403 } | 1401 } |
1404 } | 1402 } |
1405 | 1403 |
1406 // Called after the save file dialog box returns. | 1404 // Called after the save file dialog box returns. |
1407 void SavePackage::ContinueSave(const FilePath& final_name, | 1405 void SavePackage::ContinueSave(const FilePath& final_name, |
1408 int index) { | 1406 int index) { |
1409 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
1410 | |
1411 // Ensure the filename is safe. | 1407 // Ensure the filename is safe. |
1412 saved_main_file_path_ = final_name; | 1408 saved_main_file_path_ = final_name; |
1413 download_util::GenerateSafeFileName(tab_contents()->contents_mime_type(), | 1409 download_util::GenerateSafeFileName(tab_contents()->contents_mime_type(), |
1414 &saved_main_file_path_); | 1410 &saved_main_file_path_); |
1415 | 1411 |
1416 // The option index is not zero-based. | 1412 // The option index is not zero-based. |
1417 DCHECK(index >= kSelectFileHtmlOnlyIndex && | 1413 DCHECK(index >= kSelectFileHtmlOnlyIndex && |
1418 index <= kSelectFileCompleteIndex); | 1414 index <= kSelectFileCompleteIndex); |
1419 | 1415 |
1420 saved_main_directory_path_ = saved_main_file_path_.DirName(); | 1416 saved_main_directory_path_ = saved_main_file_path_.DirName(); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1471 } | 1467 } |
1472 | 1468 |
1473 // SelectFileDialog::Listener interface. | 1469 // SelectFileDialog::Listener interface. |
1474 void SavePackage::FileSelected(const FilePath& path, | 1470 void SavePackage::FileSelected(const FilePath& path, |
1475 int index, void* params) { | 1471 int index, void* params) { |
1476 ContinueSave(path, index); | 1472 ContinueSave(path, index); |
1477 } | 1473 } |
1478 | 1474 |
1479 void SavePackage::FileSelectionCanceled(void* params) { | 1475 void SavePackage::FileSelectionCanceled(void* params) { |
1480 } | 1476 } |
OLD | NEW |