| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 1216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1227 // Give backend the lists which contain all resource links that have local | 1227 // Give backend the lists which contain all resource links that have local |
| 1228 // storage, after which, render process will serialize DOM for generating | 1228 // storage, after which, render process will serialize DOM for generating |
| 1229 // HTML data. | 1229 // HTML data. |
| 1230 DoSavingProcess(); | 1230 DoSavingProcess(); |
| 1231 } else { | 1231 } else { |
| 1232 // No savable frames and/or resources - treat it as user cancel. | 1232 // No savable frames and/or resources - treat it as user cancel. |
| 1233 Cancel(true); | 1233 Cancel(true); |
| 1234 } | 1234 } |
| 1235 } | 1235 } |
| 1236 | 1236 |
| 1237 // static |
| 1237 base::FilePath SavePackage::GetSuggestedNameForSaveAs( | 1238 base::FilePath SavePackage::GetSuggestedNameForSaveAs( |
| 1239 const base::string16& title, |
| 1240 const GURL& page_url, |
| 1238 bool can_save_as_complete, | 1241 bool can_save_as_complete, |
| 1239 const std::string& contents_mime_type) { | 1242 const std::string& contents_mime_type) { |
| 1240 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1243 base::FilePath name_with_proper_ext = base::FilePath::FromUTF16Unsafe(title); |
| 1241 base::FilePath name_with_proper_ext = base::FilePath::FromUTF16Unsafe(title_); | |
| 1242 | 1244 |
| 1243 // If the page's title matches its URL, use the URL. Try to use the last path | 1245 // If the page's title matches its URL, use the URL. Try to use the last path |
| 1244 // component or if there is none, the domain as the file name. | 1246 // component or if there is none, the domain as the file name. |
| 1245 // Normally we want to base the filename on the page title, or if it doesn't | 1247 // Normally we want to base the filename on the page title, or if it doesn't |
| 1246 // exist, on the URL. It's not easy to tell if the page has no title, because | 1248 // exist, on the URL. It's not easy to tell if the page has no title, because |
| 1247 // if the page has no title, WebContents::GetTitle() will return the page's | 1249 // if the page has no title, WebContents::GetTitle() will return the page's |
| 1248 // URL (adjusted for display purposes). Therefore, we convert the "title" | 1250 // URL (adjusted for display purposes). Therefore, we convert the "title" |
| 1249 // back to a URL, and if it matches the original page URL, we know the page | 1251 // back to a URL, and if it matches the original page URL, we know the page |
| 1250 // had no title (or had a title equal to its URL, which is fine to treat | 1252 // had no title (or had a title equal to its URL, which is fine to treat |
| 1251 // similarly). | 1253 // similarly). |
| 1252 if (title_ == url_formatter::FormatUrl(page_url_)) { | 1254 if (title == url_formatter::FormatUrl(page_url)) { |
| 1253 std::string url_path; | 1255 std::string url_path; |
| 1254 if (!page_url_.SchemeIs(url::kDataScheme)) { | 1256 if (!page_url.SchemeIs(url::kDataScheme)) { |
| 1255 std::vector<std::string> url_parts = base::SplitString( | 1257 std::vector<std::string> url_parts = base::SplitString( |
| 1256 page_url_.path(), "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 1258 page_url.path(), "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 1257 if (!url_parts.empty()) { | 1259 if (!url_parts.empty()) { |
| 1258 for (int i = static_cast<int>(url_parts.size()) - 1; i >= 0; --i) { | 1260 for (int i = static_cast<int>(url_parts.size()) - 1; i >= 0; --i) { |
| 1259 url_path = url_parts[i]; | 1261 url_path = url_parts[i]; |
| 1260 if (!url_path.empty()) | 1262 if (!url_path.empty()) |
| 1261 break; | 1263 break; |
| 1262 } | 1264 } |
| 1263 } | 1265 } |
| 1264 if (url_path.empty()) | 1266 if (url_path.empty()) |
| 1265 url_path = page_url_.host(); | 1267 url_path = page_url.host(); |
| 1266 } else { | 1268 } else { |
| 1267 url_path = "dataurl"; | 1269 url_path = "dataurl"; |
| 1268 } | 1270 } |
| 1269 name_with_proper_ext = base::FilePath::FromUTF8Unsafe(url_path); | 1271 name_with_proper_ext = base::FilePath::FromUTF8Unsafe(url_path); |
| 1270 } | 1272 } |
| 1271 | 1273 |
| 1272 // Ask user for getting final saving name. | 1274 // Ask user for getting final saving name. |
| 1273 name_with_proper_ext = EnsureMimeExtension(name_with_proper_ext, | 1275 name_with_proper_ext = EnsureMimeExtension(name_with_proper_ext, |
| 1274 contents_mime_type); | 1276 contents_mime_type); |
| 1275 // Adjust extension for complete types. | 1277 // Adjust extension for complete types. |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1342 base::FilePath download_save_dir; | 1344 base::FilePath download_save_dir; |
| 1343 bool skip_dir_check = false; | 1345 bool skip_dir_check = false; |
| 1344 auto* delegate = download_manager_->GetDelegate(); | 1346 auto* delegate = download_manager_->GetDelegate(); |
| 1345 if (delegate) { | 1347 if (delegate) { |
| 1346 delegate->GetSaveDir( | 1348 delegate->GetSaveDir( |
| 1347 web_contents()->GetBrowserContext(), &website_save_dir, | 1349 web_contents()->GetBrowserContext(), &website_save_dir, |
| 1348 &download_save_dir, &skip_dir_check); | 1350 &download_save_dir, &skip_dir_check); |
| 1349 } | 1351 } |
| 1350 std::string mime_type = web_contents()->GetContentsMimeType(); | 1352 std::string mime_type = web_contents()->GetContentsMimeType(); |
| 1351 bool can_save_as_complete = CanSaveAsComplete(mime_type); | 1353 bool can_save_as_complete = CanSaveAsComplete(mime_type); |
| 1352 base::FilePath suggested_filename = | |
| 1353 GetSuggestedNameForSaveAs(can_save_as_complete, mime_type); | |
| 1354 BrowserThread::PostTaskAndReplyWithResult( | 1354 BrowserThread::PostTaskAndReplyWithResult( |
| 1355 BrowserThread::FILE, FROM_HERE, | 1355 BrowserThread::FILE, FROM_HERE, |
| 1356 base::Bind(&SavePackage::CreateDirectoryOnFileThread, website_save_dir, | 1356 base::Bind(&SavePackage::CreateDirectoryOnFileThread, title_, page_url_, |
| 1357 download_save_dir, suggested_filename, skip_dir_check), | 1357 can_save_as_complete, mime_type, website_save_dir, |
| 1358 download_save_dir, skip_dir_check), |
| 1358 base::Bind(&SavePackage::ContinueGetSaveInfo, this, | 1359 base::Bind(&SavePackage::ContinueGetSaveInfo, this, |
| 1359 can_save_as_complete)); | 1360 can_save_as_complete)); |
| 1360 } | 1361 } |
| 1361 | 1362 |
| 1362 // static | 1363 // static |
| 1363 base::FilePath SavePackage::CreateDirectoryOnFileThread( | 1364 base::FilePath SavePackage::CreateDirectoryOnFileThread( |
| 1365 const base::string16& title, |
| 1366 const GURL& page_url, |
| 1367 bool can_save_as_complete, |
| 1368 const std::string& mime_type, |
| 1364 const base::FilePath& website_save_dir, | 1369 const base::FilePath& website_save_dir, |
| 1365 const base::FilePath& download_save_dir, | 1370 const base::FilePath& download_save_dir, |
| 1366 const base::FilePath& suggested_filename, | |
| 1367 bool skip_dir_check) { | 1371 bool skip_dir_check) { |
| 1368 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 1372 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 1369 | 1373 |
| 1374 base::FilePath suggested_filename = GetSuggestedNameForSaveAs( |
| 1375 title, page_url, can_save_as_complete, mime_type); |
| 1376 |
| 1370 base::FilePath save_dir; | 1377 base::FilePath save_dir; |
| 1371 // If the default html/websites save folder doesn't exist... | 1378 // If the default html/websites save folder doesn't exist... |
| 1372 // We skip the directory check for gdata directories on ChromeOS. | 1379 // We skip the directory check for gdata directories on ChromeOS. |
| 1373 if (!skip_dir_check && !base::DirectoryExists(website_save_dir)) { | 1380 if (!skip_dir_check && !base::DirectoryExists(website_save_dir)) { |
| 1374 // If the default download dir doesn't exist, create it. | 1381 // If the default download dir doesn't exist, create it. |
| 1375 if (!base::DirectoryExists(download_save_dir)) { | 1382 if (!base::DirectoryExists(download_save_dir)) { |
| 1376 bool res = base::CreateDirectory(download_save_dir); | 1383 bool res = base::CreateDirectory(download_save_dir); |
| 1377 DCHECK(res); | 1384 DCHECK(res); |
| 1378 } | 1385 } |
| 1379 save_dir = download_save_dir; | 1386 save_dir = download_save_dir; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1464 } | 1471 } |
| 1465 | 1472 |
| 1466 void SavePackage::FinalizeDownloadEntry() { | 1473 void SavePackage::FinalizeDownloadEntry() { |
| 1467 DCHECK(download_); | 1474 DCHECK(download_); |
| 1468 | 1475 |
| 1469 download_manager_->OnSavePackageSuccessfullyFinished(download_); | 1476 download_manager_->OnSavePackageSuccessfullyFinished(download_); |
| 1470 RemoveObservers(); | 1477 RemoveObservers(); |
| 1471 } | 1478 } |
| 1472 | 1479 |
| 1473 } // namespace content | 1480 } // namespace content |
| OLD | NEW |