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

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: Added URLRequestMockHTTPJob.test_dir_ and URLRequestMockHTTPJob.temp_dir_ 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
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 1135 matching lines...) Expand 10 before | Expand all | Expand 10 after
1146 FilePath::StringType mime_type(UTF8ToWide(contents_mime_type)); 1146 FilePath::StringType mime_type(UTF8ToWide(contents_mime_type));
1147 #endif // OS_WIN 1147 #endif // OS_WIN
1148 for (uint32 i = 0; i < ARRAYSIZE_UNSAFE(extensions); ++i) { 1148 for (uint32 i = 0; i < ARRAYSIZE_UNSAFE(extensions); ++i) {
1149 if (mime_type == extensions[i].mime_type) 1149 if (mime_type == extensions[i].mime_type)
1150 return extensions[i].suggested_extension; 1150 return extensions[i].suggested_extension;
1151 } 1151 }
1152 return FILE_PATH_LITERAL(""); 1152 return FILE_PATH_LITERAL("");
1153 } 1153 }
1154 1154
1155 void SavePackage::GetSaveInfo() { 1155 void SavePackage::GetSaveInfo() {
1156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1157
1156 // Can't use tab_contents_ in the file thread, so get the data that we need 1158 // Can't use tab_contents_ in the file thread, so get the data that we need
1157 // before calling to it. 1159 // before calling to it.
1158 FilePath website_save_dir, download_save_dir; 1160 FilePath website_save_dir, download_save_dir;
1159 content::GetContentClient()->browser()->GetSaveDir( 1161 content::GetContentClient()->browser()->GetSaveDir(
1160 tab_contents(), &website_save_dir, &download_save_dir); 1162 tab_contents(), &website_save_dir, &download_save_dir);
1161 std::string mime_type = tab_contents()->contents_mime_type(); 1163 std::string mime_type = tab_contents()->contents_mime_type();
1162 std::string accept_languages = 1164 std::string accept_languages =
1163 content::GetContentClient()->browser()->GetAcceptLangs(tab_contents()); 1165 content::GetContentClient()->browser()->GetAcceptLangs(tab_contents());
1164 1166
1165 BrowserThread::PostTask( 1167 BrowserThread::PostTask(
1166 BrowserThread::FILE, FROM_HERE, 1168 BrowserThread::FILE, FROM_HERE,
1167 NewRunnableMethod(this, &SavePackage::CreateDirectoryOnFileThread, 1169 NewRunnableMethod(this, &SavePackage::CreateDirectoryOnFileThread,
1168 website_save_dir, download_save_dir, mime_type, accept_languages)); 1170 website_save_dir, download_save_dir, mime_type, accept_languages));
1169 } 1171 }
1170 1172
1171 void SavePackage::CreateDirectoryOnFileThread( 1173 void SavePackage::CreateDirectoryOnFileThread(
1172 const FilePath& website_save_dir, 1174 const FilePath& website_save_dir,
1173 const FilePath& download_save_dir, 1175 const FilePath& download_save_dir,
1174 const std::string& mime_type, 1176 const std::string& mime_type,
1175 const std::string& accept_langs) { 1177 const std::string& accept_langs) {
1178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
1179
1180 FilePath default_downloads_dir =
1181 download_util::GetDefaultDownloadDirectoryFromPathService();
1176 FilePath save_dir; 1182 FilePath save_dir;
1177 // If the default html/websites save folder doesn't exist... 1183 // Ignores the returned value since the select file dialog should be
1178 if (!file_util::DirectoryExists(website_save_dir)) { 1184 // displayed in any case.
1179 // If the default download dir doesn't exist, create it. 1185 download_util::ChooseSavableDirectory(
1180 if (!file_util::DirectoryExists(download_save_dir)) 1186 website_save_dir, download_save_dir, default_downloads_dir, &save_dir);
1181 file_util::CreateDirectory(download_save_dir);
1182 save_dir = download_save_dir;
1183 } else {
1184 // If it does exist, use the default save dir param.
1185 save_dir = website_save_dir;
1186 }
1187 1187
1188 bool can_save_as_complete = CanSaveAsComplete(mime_type); 1188 bool can_save_as_complete = CanSaveAsComplete(mime_type);
1189 FilePath suggested_filename = GetSuggestedNameForSaveAs( 1189 FilePath suggested_filename = GetSuggestedNameForSaveAs(
1190 can_save_as_complete, mime_type, accept_langs); 1190 can_save_as_complete, mime_type, accept_langs);
1191 FilePath::StringType pure_file_name = 1191 FilePath::StringType pure_file_name =
1192 suggested_filename.RemoveExtension().BaseName().value(); 1192 suggested_filename.RemoveExtension().BaseName().value();
1193 FilePath::StringType file_name_ext = suggested_filename.Extension(); 1193 FilePath::StringType file_name_ext = suggested_filename.Extension();
1194 1194
1195 // Need to make sure the suggested file name is not too long. 1195 // Need to make sure the suggested file name is not too long.
1196 uint32 max_path = GetMaxPathLengthForDirectory(save_dir); 1196 uint32 max_path = GetMaxPathLengthForDirectory(save_dir);
1197 1197
1198 if (GetSafePureFileName(save_dir, file_name_ext, max_path, &pure_file_name)) { 1198 if (GetSafePureFileName(save_dir, file_name_ext, max_path, &pure_file_name)) {
1199 save_dir = save_dir.Append(pure_file_name + file_name_ext); 1199 save_dir = save_dir.Append(pure_file_name + file_name_ext);
1200 } else { 1200 } else {
1201 // Cannot create a shorter filename. This will cause the save as operation 1201 // Cannot create a shorter filename. This will cause the save as operation
1202 // to fail unless the user pick a shorter name. Continuing even though it 1202 // to fail unless the user pick a shorter name. Continuing even though it
1203 // will fail because returning means no save as popup for the user, which 1203 // will fail because returning means no save as popup for the user, which
1204 // is even more confusing. This case should be rare though. 1204 // is even more confusing. This case should be rare though.
1205 save_dir = save_dir.Append(suggested_filename); 1205 save_dir = save_dir.Append(suggested_filename);
1206 } 1206 }
1207 1207
1208 BrowserThread::PostTask( 1208 BrowserThread::PostTask(
1209 BrowserThread::UI, FROM_HERE, 1209 BrowserThread::UI, FROM_HERE,
1210 NewRunnableMethod(this, &SavePackage::ContinueGetSaveInfo, save_dir, 1210 NewRunnableMethod(this, &SavePackage::ContinueGetSaveInfo, save_dir,
1211 can_save_as_complete)); 1211 can_save_as_complete));
1212 } 1212 }
1213 1213
1214 void SavePackage::ContinueGetSaveInfo(const FilePath& suggested_path, 1214 void SavePackage::ContinueGetSaveInfo(const FilePath& suggested_path,
1215 bool can_save_as_complete) { 1215 bool can_save_as_complete) {
1216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1217
1216 // The TabContents which owns this SavePackage may have disappeared during 1218 // The TabContents which owns this SavePackage may have disappeared during
1217 // the UI->FILE->UI thread hop of 1219 // the UI->FILE->UI thread hop of
1218 // GetSaveInfo->CreateDirectoryOnFileThread->ContinueGetSaveInfo. 1220 // GetSaveInfo->CreateDirectoryOnFileThread->ContinueGetSaveInfo.
1219 if (!tab_contents()) 1221 if (!tab_contents())
1220 return; 1222 return;
1221 1223
1222 content::GetContentClient()->browser()->ChooseSavePath( 1224 content::GetContentClient()->browser()->ChooseSavePath(
1223 AsWeakPtr(), suggested_path, can_save_as_complete); 1225 AsWeakPtr(), suggested_path, can_save_as_complete);
1224 } 1226 }
1225 1227
1226 // Called after the save file dialog box returns. 1228 // Called after the save file dialog box returns.
1227 void SavePackage::OnPathPicked(const FilePath& final_name, 1229 void SavePackage::OnPathPicked(const FilePath& final_name,
1228 SavePackageType type) { 1230 SavePackageType type) {
1231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1232
1229 // Ensure the filename is safe. 1233 // Ensure the filename is safe.
1230 saved_main_file_path_ = final_name; 1234 saved_main_file_path_ = final_name;
1231 download_util::GenerateSafeFileName(tab_contents()->contents_mime_type(), 1235 download_util::GenerateSafeFileName(tab_contents()->contents_mime_type(),
1232 &saved_main_file_path_); 1236 &saved_main_file_path_);
1233 1237
1234 saved_main_directory_path_ = saved_main_file_path_.DirName(); 1238 saved_main_directory_path_ = saved_main_file_path_.DirName();
1235 save_type_ = type; 1239 save_type_ = type;
1236 if (save_type_ == SavePackage::SAVE_AS_COMPLETE_HTML) { 1240 if (save_type_ == SavePackage::SAVE_AS_COMPLETE_HTML) {
1237 // Make new directory for saving complete file. 1241 // Make new directory for saving complete file.
1238 saved_main_directory_path_ = saved_main_directory_path_.Append( 1242 saved_main_directory_path_ = saved_main_directory_path_.Append(
(...skipping 18 matching lines...) Expand all
1257 bool SavePackage::IsSavableContents(const std::string& contents_mime_type) { 1261 bool SavePackage::IsSavableContents(const std::string& contents_mime_type) {
1258 // WebKit creates Document object when MIME type is application/xhtml+xml, 1262 // WebKit creates Document object when MIME type is application/xhtml+xml,
1259 // so we also support this MIME type. 1263 // so we also support this MIME type.
1260 return contents_mime_type == "text/html" || 1264 return contents_mime_type == "text/html" ||
1261 contents_mime_type == "text/xml" || 1265 contents_mime_type == "text/xml" ||
1262 contents_mime_type == "application/xhtml+xml" || 1266 contents_mime_type == "application/xhtml+xml" ||
1263 contents_mime_type == "text/plain" || 1267 contents_mime_type == "text/plain" ||
1264 contents_mime_type == "text/css" || 1268 contents_mime_type == "text/css" ||
1265 net::IsSupportedJavascriptMimeType(contents_mime_type.c_str()); 1269 net::IsSupportedJavascriptMimeType(contents_mime_type.c_str());
1266 } 1270 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698