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

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

Powered by Google App Engine
This is Rietveld 408576698