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

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: Correct typo 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"
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
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 void SavePackage::GetSaveInfo(const FilePath& website_save_dir,
Randy Smith (Not in Mondays) 2011/05/31 23:03:10 It's a minor point, but it would seem relatively e
haraken1 2011/06/02 09:13:22 I removed this GetSaveInfo(). Instead, I added Dow
1275 const FilePath& download_save_dir,
1276 SavePackageType save_type) {
1277 PrefService* prefs = tab_contents()->profile()->GetPrefs();
1278 // Ignore the return value
1279 GetSaveDirPreference(prefs);
Randy Smith (Not in Mondays) 2011/05/31 23:03:10 Could you document the side effect for which you'r
haraken1 2011/06/02 09:13:22 This is now unnecessary since I removed this code.
1280 prefs->SetInteger(prefs::kSaveFileType, save_type);
Randy Smith (Not in Mondays) 2011/05/31 23:03:10 This looks like a global state change that isn't d
haraken1 2011/06/02 09:13:22 This is now unnecessary since I removed this code.
1281 std::string mime_type = tab_contents()->contents_mime_type();
1282
1283 BrowserThread::PostTask(
1284 BrowserThread::FILE, FROM_HERE,
1285 NewRunnableMethod(this, &SavePackage::CreateDirectoryOnFileThread,
1286 website_save_dir, download_save_dir, mime_type));
1271 } 1287 }
1272 1288
1273 void SavePackage::CreateDirectoryOnFileThread( 1289 void SavePackage::CreateDirectoryOnFileThread(
1274 const FilePath& website_save_dir, 1290 const FilePath& website_save_dir,
1275 const FilePath& download_save_dir, 1291 const FilePath& download_save_dir,
1276 const std::string& mime_type) { 1292 const std::string& mime_type) {
1277 FilePath save_dir; 1293 FilePath save_dir;
1278 // If the default html/websites save folder doesn't exist... 1294 if (file_util::DirectoryExists(website_save_dir)) {
1279 if (!file_util::DirectoryExists(website_save_dir)) { 1295 // If the default html/websites save folder exists,
1280 // If the default download dir doesn't exist, create it. 1296 // then use the default html/websites save folder.
1281 if (!file_util::DirectoryExists(download_save_dir)) 1297 save_dir = website_save_dir;
1282 file_util::CreateDirectory(download_save_dir); 1298 } else if (file_util::DirectoryExists(download_save_dir)) {
1299 // If the default html/websites save folder does not exist
1300 // but the default download folder exists,
1301 // then use the default download folder.
1283 save_dir = download_save_dir; 1302 save_dir = download_save_dir;
1284 } else { 1303 } else {
1285 // If it does exist, use the default save dir param. 1304 // If both the above folders do not exist,
1286 save_dir = website_save_dir; 1305 // use the user's "Downloads" folder.
1306 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &save_dir)) {
1307 // Create the |download_save_dir| folder if we cannot get
1308 // DIR_DEFAULT_DOWNLOADS (This will be a rare case).
1309 save_dir = download_save_dir;
1310 }
1311 // Make sure that the folder does exist.
1312 if (!file_util::CreateDirectory(save_dir))
1313 LOG(ERROR) << "Failed to create " << save_dir.value();
1287 } 1314 }
1288 1315
1289 bool can_save_as_complete = CanSaveAsComplete(mime_type); 1316 bool can_save_as_complete = CanSaveAsComplete(mime_type);
1290 FilePath suggested_filename = GetSuggestedNameForSaveAs(can_save_as_complete, 1317 FilePath suggested_filename = GetSuggestedNameForSaveAs(can_save_as_complete,
1291 mime_type); 1318 mime_type);
1292 FilePath::StringType pure_file_name = 1319 FilePath::StringType pure_file_name =
1293 suggested_filename.RemoveExtension().BaseName().value(); 1320 suggested_filename.RemoveExtension().BaseName().value();
1294 FilePath::StringType file_name_ext = suggested_filename.Extension(); 1321 FilePath::StringType file_name_ext = suggested_filename.Extension();
1295 1322
1296 // Need to make sure the suggested file name is not too long. 1323 // 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 } 1493 }
1467 1494
1468 // SelectFileDialog::Listener interface. 1495 // SelectFileDialog::Listener interface.
1469 void SavePackage::FileSelected(const FilePath& path, 1496 void SavePackage::FileSelected(const FilePath& path,
1470 int index, void* params) { 1497 int index, void* params) {
1471 ContinueSave(path, index); 1498 ContinueSave(path, index);
1472 } 1499 }
1473 1500
1474 void SavePackage::FileSelectionCanceled(void* params) { 1501 void SavePackage::FileSelectionCanceled(void* params) {
1475 } 1502 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698