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

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: Remove PathServiceWrapper and add DefaultDownloadDirectory 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 1249 matching lines...) Expand 10 before | Expand all | Expand 10 after
1260 // before calling to it. 1260 // before calling to it.
1261 PrefService* prefs = tab_contents()->profile()->GetPrefs(); 1261 PrefService* prefs = tab_contents()->profile()->GetPrefs();
1262 FilePath website_save_dir = GetSaveDirPreference(prefs); 1262 FilePath website_save_dir = GetSaveDirPreference(prefs);
1263 FilePath download_save_dir = prefs->GetFilePath( 1263 FilePath download_save_dir = prefs->GetFilePath(
1264 prefs::kDownloadDefaultDirectory); 1264 prefs::kDownloadDefaultDirectory);
1265 std::string mime_type = tab_contents()->contents_mime_type(); 1265 std::string mime_type = tab_contents()->contents_mime_type();
1266 1266
1267 BrowserThread::PostTask( 1267 BrowserThread::PostTask(
1268 BrowserThread::FILE, FROM_HERE, 1268 BrowserThread::FILE, FROM_HERE,
1269 NewRunnableMethod(this, &SavePackage::CreateDirectoryOnFileThread, 1269 NewRunnableMethod(this, &SavePackage::CreateDirectoryOnFileThread,
1270 website_save_dir, download_save_dir, mime_type)); 1270 website_save_dir, download_save_dir, mime_type));
1271 } 1271 }
1272 1272
1273 void SavePackage::CreateDirectoryOnFileThread( 1273 void SavePackage::CreateDirectoryOnFileThread(
Paweł Hajdan Jr. 2011/06/08 09:50:23 Interesting, this code seems much duplicated betwe
haraken1 2011/06/09 10:16:56 I collected up the duplicated code into ChooseSava
1274 const FilePath& website_save_dir, 1274 const FilePath& website_save_dir,
1275 const FilePath& download_save_dir, 1275 const FilePath& download_save_dir,
1276 const std::string& mime_type) { 1276 const std::string& mime_type) {
1277 FilePath save_dir; 1277 FilePath save_dir;
1278 // If the default html/websites save folder doesn't exist... 1278 if (file_util::PathIsWritable(website_save_dir)) {
1279 if (!file_util::DirectoryExists(website_save_dir)) { 1279 // If the default html/websites save folder exists,
1280 // If the default download dir doesn't exist, create it. 1280 // then use the default html/websites save folder.
1281 if (!file_util::DirectoryExists(download_save_dir)) 1281 save_dir = website_save_dir;
1282 file_util::CreateDirectory(download_save_dir); 1282 } else if (file_util::PathIsWritable(download_save_dir)) {
1283 // If the default html/websites save folder does not exist
1284 // but the default download folder exists,
1285 // then use the default download folder.
1283 save_dir = download_save_dir; 1286 save_dir = download_save_dir;
1284 } else { 1287 } else {
1285 // If it does exist, use the default save dir param. 1288 // If both the above folders do not exist,
1286 save_dir = website_save_dir; 1289 // use the user's "Downloads" folder.
1290 if (!download_util::DefaultDownloadDirectory::Get(&save_dir) ||
1291 !file_util::PathIsWritable(save_dir)) {
1292 VLOG(1) << "Cannot find the user's writable \"Downloads\" folder.";
1293 // Create the |download_save_dir| folder if we cannot get
1294 // the user's writable "Downloads" folder (This will be a rare case).
1295 save_dir = download_save_dir;
1296 }
1297 // Make sure that the folder does exist.
1298 if (!file_util::CreateDirectory(save_dir))
1299 LOG(ERROR) << "Failed to create " << save_dir.value();
1287 } 1300 }
1288 1301
1289 bool can_save_as_complete = CanSaveAsComplete(mime_type); 1302 bool can_save_as_complete = CanSaveAsComplete(mime_type);
1290 FilePath suggested_filename = GetSuggestedNameForSaveAs(can_save_as_complete, 1303 FilePath suggested_filename = GetSuggestedNameForSaveAs(can_save_as_complete,
1291 mime_type); 1304 mime_type);
1292 FilePath::StringType pure_file_name = 1305 FilePath::StringType pure_file_name =
1293 suggested_filename.RemoveExtension().BaseName().value(); 1306 suggested_filename.RemoveExtension().BaseName().value();
1294 FilePath::StringType file_name_ext = suggested_filename.Extension(); 1307 FilePath::StringType file_name_ext = suggested_filename.Extension();
1295 1308
1296 // Need to make sure the suggested file name is not too long. 1309 // Need to make sure the suggested file name is not too long.
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
1466 } 1479 }
1467 1480
1468 // SelectFileDialog::Listener interface. 1481 // SelectFileDialog::Listener interface.
1469 void SavePackage::FileSelected(const FilePath& path, 1482 void SavePackage::FileSelected(const FilePath& path,
1470 int index, void* params) { 1483 int index, void* params) {
1471 ContinueSave(path, index); 1484 ContinueSave(path, index);
1472 } 1485 }
1473 1486
1474 void SavePackage::FileSelectionCanceled(void* params) { 1487 void SavePackage::FileSelectionCanceled(void* params) {
1475 } 1488 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698