Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: chrome/browser/download/save_package.cc

Issue 6973052: When the download folder does not exist, change the download folder to a user's "Downloads" (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Add ChooseSavableDirectory() and ScopedDefaultDownloadDirectory Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
1259 // Can't use tab_contents_ in the file thread, so get the data that we need 1261 // Can't use tab_contents_ in the file thread, so get the data that we need
1260 // before calling to it. 1262 // before calling to it.
1261 PrefService* prefs = tab_contents()->profile()->GetPrefs(); 1263 PrefService* prefs = tab_contents()->profile()->GetPrefs();
1262 FilePath website_save_dir = GetSaveDirPreference(prefs); 1264 FilePath website_save_dir = GetSaveDirPreference(prefs);
1263 FilePath download_save_dir = prefs->GetFilePath( 1265 FilePath download_save_dir = prefs->GetFilePath(
1264 prefs::kDownloadDefaultDirectory); 1266 prefs::kDownloadDefaultDirectory);
1265 std::string mime_type = tab_contents()->contents_mime_type(); 1267 std::string mime_type = tab_contents()->contents_mime_type();
1266 1268
1267 BrowserThread::PostTask( 1269 BrowserThread::PostTask(
1268 BrowserThread::FILE, FROM_HERE, 1270 BrowserThread::FILE, FROM_HERE,
1269 NewRunnableMethod(this, &SavePackage::CreateDirectoryOnFileThread, 1271 NewRunnableMethod(this, &SavePackage::CreateDirectoryOnFileThread,
1270 website_save_dir, download_save_dir, mime_type)); 1272 website_save_dir, download_save_dir, mime_type));
1271 } 1273 }
1272 1274
1273 void SavePackage::CreateDirectoryOnFileThread( 1275 void SavePackage::CreateDirectoryOnFileThread(
1274 const FilePath& website_save_dir, 1276 const FilePath& website_save_dir,
1275 const FilePath& download_save_dir, 1277 const FilePath& download_save_dir,
1276 const std::string& mime_type) { 1278 const std::string& mime_type) {
1279 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
1280
1281 FilePath default_downloads_dir =
1282 download_util::DefaultDownloadDirectory::Get();
Randy Smith (Not in Mondays) 2011/06/10 20:58:53 (Reference to other comment.) This is the current
haraken1 2011/06/14 11:10:05 Done.
1277 FilePath save_dir; 1283 FilePath save_dir;
1278 // If the default html/websites save folder doesn't exist... 1284 // Ignores the returned value since the select file dialog should be
1279 if (!file_util::DirectoryExists(website_save_dir)) { 1285 // displayed in any case.
1280 // If the default download dir doesn't exist, create it. 1286 download_util::ChooseSavableDirectory(
1281 if (!file_util::DirectoryExists(download_save_dir)) 1287 website_save_dir, download_save_dir, default_downloads_dir, &save_dir);
1282 file_util::CreateDirectory(download_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
1317 // The TabContents which owns this SavePackage may have disappeared during 1319 // The TabContents which owns this SavePackage may have disappeared during
1318 // the UI->FILE->UI thread hop of 1320 // the UI->FILE->UI thread hop of
1319 // GetSaveInfo->CreateDirectoryOnFileThread->ContinueGetSaveInfo. 1321 // GetSaveInfo->CreateDirectoryOnFileThread->ContinueGetSaveInfo.
1320 if (!tab_contents()) 1322 if (!tab_contents())
1321 return; 1323 return;
1322 DownloadPrefs* download_prefs = 1324 DownloadPrefs* download_prefs =
1323 tab_contents()->profile()->GetDownloadManager()->download_prefs(); 1325 tab_contents()->profile()->GetDownloadManager()->download_prefs();
1324 int file_type_index = 1326 int file_type_index =
1325 SavePackageTypeToIndex( 1327 SavePackageTypeToIndex(
1326 static_cast<SavePackageType>(download_prefs->save_file_type())); 1328 static_cast<SavePackageType>(download_prefs->save_file_type()));
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
1396 NULL); 1398 NULL);
1397 } else { 1399 } else {
1398 // Just use 'suggested_path' instead of opening the dialog prompt. 1400 // Just use 'suggested_path' instead of opening the dialog prompt.
1399 ContinueSave(suggested_path, file_type_index); 1401 ContinueSave(suggested_path, file_type_index);
1400 } 1402 }
1401 } 1403 }
1402 1404
1403 // Called after the save file dialog box returns. 1405 // Called after the save file dialog box returns.
1404 void SavePackage::ContinueSave(const FilePath& final_name, 1406 void SavePackage::ContinueSave(const FilePath& final_name,
1405 int index) { 1407 int index) {
1408 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1409
1406 // Ensure the filename is safe. 1410 // Ensure the filename is safe.
1407 saved_main_file_path_ = final_name; 1411 saved_main_file_path_ = final_name;
1408 download_util::GenerateSafeFileName(tab_contents()->contents_mime_type(), 1412 download_util::GenerateSafeFileName(tab_contents()->contents_mime_type(),
1409 &saved_main_file_path_); 1413 &saved_main_file_path_);
1410 1414
1411 // The option index is not zero-based. 1415 // The option index is not zero-based.
1412 DCHECK(index >= kSelectFileHtmlOnlyIndex && 1416 DCHECK(index >= kSelectFileHtmlOnlyIndex &&
1413 index <= kSelectFileCompleteIndex); 1417 index <= kSelectFileCompleteIndex);
1414 1418
1415 saved_main_directory_path_ = saved_main_file_path_.DirName(); 1419 saved_main_directory_path_ = saved_main_file_path_.DirName();
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
1466 } 1470 }
1467 1471
1468 // SelectFileDialog::Listener interface. 1472 // SelectFileDialog::Listener interface.
1469 void SavePackage::FileSelected(const FilePath& path, 1473 void SavePackage::FileSelected(const FilePath& path,
1470 int index, void* params) { 1474 int index, void* params) {
1471 ContinueSave(path, index); 1475 ContinueSave(path, index);
1472 } 1476 }
1473 1477
1474 void SavePackage::FileSelectionCanceled(void* params) { 1478 void SavePackage::FileSelectionCanceled(void* params) {
1475 } 1479 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698