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

Side by Side Diff: content/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: Applied Pawel's comments Created 9 years, 4 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
« no previous file with comments | « content/browser/download/save_package.h ('k') | content/browser/tab_contents/tab_contents.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "content/browser/download/save_package.h" 5 #include "content/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 1127 matching lines...) Expand 10 before | Expand all | Expand 10 after
1138 FilePath::StringType mime_type(UTF8ToWide(contents_mime_type)); 1138 FilePath::StringType mime_type(UTF8ToWide(contents_mime_type));
1139 #endif // OS_WIN 1139 #endif // OS_WIN
1140 for (uint32 i = 0; i < ARRAYSIZE_UNSAFE(extensions); ++i) { 1140 for (uint32 i = 0; i < ARRAYSIZE_UNSAFE(extensions); ++i) {
1141 if (mime_type == extensions[i].mime_type) 1141 if (mime_type == extensions[i].mime_type)
1142 return extensions[i].suggested_extension; 1142 return extensions[i].suggested_extension;
1143 } 1143 }
1144 return FILE_PATH_LITERAL(""); 1144 return FILE_PATH_LITERAL("");
1145 } 1145 }
1146 1146
1147 void SavePackage::GetSaveInfo() { 1147 void SavePackage::GetSaveInfo() {
1148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1149
1148 // Can't use tab_contents_ in the file thread, so get the data that we need 1150 // Can't use tab_contents_ in the file thread, so get the data that we need
1149 // before calling to it. 1151 // before calling to it.
1150 FilePath website_save_dir, download_save_dir; 1152 FilePath website_save_dir, download_save_dir;
1151 DCHECK(download_manager_); 1153 DCHECK(download_manager_);
1152 download_manager_->delegate()->GetSaveDir( 1154 download_manager_->delegate()->GetSaveDir(
1153 tab_contents(), &website_save_dir, &download_save_dir); 1155 tab_contents(), &website_save_dir, &download_save_dir);
1154 std::string mime_type = tab_contents()->contents_mime_type(); 1156 std::string mime_type = tab_contents()->contents_mime_type();
1155 std::string accept_languages = 1157 std::string accept_languages =
1156 content::GetContentClient()->browser()->GetAcceptLangs(tab_contents()); 1158 content::GetContentClient()->browser()->GetAcceptLangs(tab_contents());
1157 1159
1158 BrowserThread::PostTask( 1160 BrowserThread::PostTask(
1159 BrowserThread::FILE, FROM_HERE, 1161 BrowserThread::FILE, FROM_HERE,
1160 NewRunnableMethod(this, &SavePackage::CreateDirectoryOnFileThread, 1162 NewRunnableMethod(this, &SavePackage::CreateDirectoryOnFileThread,
1161 website_save_dir, download_save_dir, mime_type, accept_languages)); 1163 website_save_dir, download_save_dir, mime_type, accept_languages));
1162 } 1164 }
1163 1165
1164 void SavePackage::CreateDirectoryOnFileThread( 1166 void SavePackage::CreateDirectoryOnFileThread(
1165 const FilePath& website_save_dir, 1167 const FilePath& website_save_dir,
1166 const FilePath& download_save_dir, 1168 const FilePath& download_save_dir,
1167 const std::string& mime_type, 1169 const std::string& mime_type,
1168 const std::string& accept_langs) { 1170 const std::string& accept_langs) {
1171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
1172
1173 FilePath default_downloads_dir =
1174 download_util::GetDefaultDownloadDirectoryFromPathService();
1169 FilePath save_dir; 1175 FilePath save_dir;
1170 // If the default html/websites save folder doesn't exist... 1176 // Ignores the returned value since the select file dialog should be
1171 if (!file_util::DirectoryExists(website_save_dir)) { 1177 // displayed in any case.
1172 // If the default download dir doesn't exist, create it. 1178 download_util::ChooseSavableDirectory(
1173 if (!file_util::DirectoryExists(download_save_dir)) 1179 website_save_dir, download_save_dir, default_downloads_dir, &save_dir);
1174 file_util::CreateDirectory(download_save_dir);
1175 save_dir = download_save_dir;
1176 } else {
1177 // If it does exist, use the default save dir param.
1178 save_dir = website_save_dir;
1179 }
1180 1180
1181 bool can_save_as_complete = CanSaveAsComplete(mime_type); 1181 bool can_save_as_complete = CanSaveAsComplete(mime_type);
1182 FilePath suggested_filename = GetSuggestedNameForSaveAs( 1182 FilePath suggested_filename = GetSuggestedNameForSaveAs(
1183 can_save_as_complete, mime_type, accept_langs); 1183 can_save_as_complete, mime_type, accept_langs);
1184 FilePath::StringType pure_file_name = 1184 FilePath::StringType pure_file_name =
1185 suggested_filename.RemoveExtension().BaseName().value(); 1185 suggested_filename.RemoveExtension().BaseName().value();
1186 FilePath::StringType file_name_ext = suggested_filename.Extension(); 1186 FilePath::StringType file_name_ext = suggested_filename.Extension();
1187 1187
1188 // Need to make sure the suggested file name is not too long. 1188 // Need to make sure the suggested file name is not too long.
1189 uint32 max_path = GetMaxPathLengthForDirectory(save_dir); 1189 uint32 max_path = GetMaxPathLengthForDirectory(save_dir);
1190 1190
1191 if (GetSafePureFileName(save_dir, file_name_ext, max_path, &pure_file_name)) { 1191 if (GetSafePureFileName(save_dir, file_name_ext, max_path, &pure_file_name)) {
1192 save_dir = save_dir.Append(pure_file_name + file_name_ext); 1192 save_dir = save_dir.Append(pure_file_name + file_name_ext);
1193 } else { 1193 } else {
1194 // Cannot create a shorter filename. This will cause the save as operation 1194 // Cannot create a shorter filename. This will cause the save as operation
1195 // to fail unless the user pick a shorter name. Continuing even though it 1195 // to fail unless the user pick a shorter name. Continuing even though it
1196 // will fail because returning means no save as popup for the user, which 1196 // will fail because returning means no save as popup for the user, which
1197 // is even more confusing. This case should be rare though. 1197 // is even more confusing. This case should be rare though.
1198 save_dir = save_dir.Append(suggested_filename); 1198 save_dir = save_dir.Append(suggested_filename);
1199 } 1199 }
1200 1200
1201 BrowserThread::PostTask( 1201 BrowserThread::PostTask(
1202 BrowserThread::UI, FROM_HERE, 1202 BrowserThread::UI, FROM_HERE,
1203 NewRunnableMethod(this, &SavePackage::ContinueGetSaveInfo, save_dir, 1203 NewRunnableMethod(this, &SavePackage::ContinueGetSaveInfo, save_dir,
1204 can_save_as_complete)); 1204 can_save_as_complete));
1205 } 1205 }
1206 1206
1207 void SavePackage::ContinueGetSaveInfo(const FilePath& suggested_path, 1207 void SavePackage::ContinueGetSaveInfo(const FilePath& suggested_path,
1208 bool can_save_as_complete) { 1208 bool can_save_as_complete) {
1209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1210
1209 // The TabContents which owns this SavePackage may have disappeared during 1211 // The TabContents which owns this SavePackage may have disappeared during
1210 // the UI->FILE->UI thread hop of 1212 // the UI->FILE->UI thread hop of
1211 // GetSaveInfo->CreateDirectoryOnFileThread->ContinueGetSaveInfo. 1213 // GetSaveInfo->CreateDirectoryOnFileThread->ContinueGetSaveInfo.
1212 if (!tab_contents()) 1214 if (!tab_contents())
1213 return; 1215 return;
1214 1216
1215 download_manager_->delegate()->ChooseSavePath( 1217 download_manager_->delegate()->ChooseSavePath(
1216 AsWeakPtr(), suggested_path, can_save_as_complete); 1218 AsWeakPtr(), suggested_path, can_save_as_complete);
1217 } 1219 }
1218 1220
1219 // Called after the save file dialog box returns. 1221 // Called after the save file dialog box returns.
1220 void SavePackage::OnPathPicked(const FilePath& final_name, 1222 void SavePackage::OnPathPicked(const FilePath& final_name,
1221 SavePackageType type) { 1223 SavePackageType type) {
1224 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1225
1222 // Ensure the filename is safe. 1226 // Ensure the filename is safe.
1223 saved_main_file_path_ = final_name; 1227 saved_main_file_path_ = final_name;
1224 // TODO(asanka): This call may block on IO and shouldn't be made 1228 // TODO(asanka): This call may block on IO and shouldn't be made
1225 // from the UI thread. See http://crbug.com/61827. 1229 // from the UI thread. See http://crbug.com/61827.
1226 net::GenerateSafeFileName(tab_contents()->contents_mime_type(), 1230 net::GenerateSafeFileName(tab_contents()->contents_mime_type(),
1227 &saved_main_file_path_); 1231 &saved_main_file_path_);
1228 1232
1229 saved_main_directory_path_ = saved_main_file_path_.DirName(); 1233 saved_main_directory_path_ = saved_main_file_path_.DirName();
1230 save_type_ = type; 1234 save_type_ = type;
1231 if (save_type_ == SavePackage::SAVE_AS_COMPLETE_HTML) { 1235 if (save_type_ == SavePackage::SAVE_AS_COMPLETE_HTML) {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1279 StopObservation(); 1283 StopObservation();
1280 } 1284 }
1281 1285
1282 void SavePackage::FinalizeDownloadEntry() { 1286 void SavePackage::FinalizeDownloadEntry() {
1283 DCHECK(download_); 1287 DCHECK(download_);
1284 DCHECK(download_manager_); 1288 DCHECK(download_manager_);
1285 1289
1286 download_manager_->SavePageDownloadFinished(download_); 1290 download_manager_->SavePageDownloadFinished(download_);
1287 StopObservation(); 1291 StopObservation();
1288 } 1292 }
OLDNEW
« no previous file with comments | « content/browser/download/save_package.h ('k') | content/browser/tab_contents/tab_contents.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698