diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-13 21:17:44 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-13 21:17:44 +0000 |
commit | b74694703df61f710c8946f3256411327051af6c (patch) | |
tree | d919099f50e940dd52d096a237cda9274ab376e3 /chrome/browser/download/save_package.cc | |
parent | 03b612f07003a102f1cbba3707a852d4052e8546 (diff) | |
download | chromium_src-b74694703df61f710c8946f3256411327051af6c.zip chromium_src-b74694703df61f710c8946f3256411327051af6c.tar.gz chromium_src-b74694703df61f710c8946f3256411327051af6c.tar.bz2 |
Fix suggested save file name for title-less pages.
regressed in r48691
BUG=51963
TEST=TODO
Review URL: http://codereview.chromium.org/3150012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@56091 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/download/save_package.cc')
-rw-r--r-- | chrome/browser/download/save_package.cc | 69 |
1 files changed, 34 insertions, 35 deletions
diff --git a/chrome/browser/download/save_package.cc b/chrome/browser/download/save_package.cc index 357c0f7..b8f365b 100644 --- a/chrome/browser/download/save_package.cc +++ b/chrome/browser/download/save_package.cc @@ -25,6 +25,7 @@ #include "chrome/browser/download/save_file.h" #include "chrome/browser/download/save_file_manager.h" #include "chrome/browser/download/save_item.h" +#include "chrome/browser/net/url_fixer_upper.h" #include "chrome/browser/platform_util.h" #include "chrome/browser/pref_service.h" #include "chrome/browser/profile.h" @@ -206,28 +207,27 @@ class CreateDownloadDirectoryTask : public Task { } // namespace -SavePackage::SavePackage(TabContents* web_content, +SavePackage::SavePackage(TabContents* tab_contents, SavePackageType save_type, const FilePath& file_full_path, const FilePath& directory_full_path) : file_manager_(NULL), - tab_contents_(web_content), + tab_contents_(tab_contents), download_(NULL), + page_url_(GetUrlToBeSaved()), saved_main_file_path_(file_full_path), saved_main_directory_path_(directory_full_path), + title_(tab_contents->GetTitle()), finished_(false), user_canceled_(false), disk_error_occurred_(false), save_type_(save_type), all_save_items_count_(0), wait_state_(INITIALIZE), - tab_id_(web_content->GetRenderProcessHost()->id()), + tab_id_(tab_contents->GetRenderProcessHost()->id()), unique_id_(g_save_package_id++), ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { - DCHECK(web_content); - const GURL& current_page_url = GetUrlToBeSaved(); - DCHECK(current_page_url.is_valid()); - page_url_ = current_page_url; + DCHECK(page_url_.is_valid()); DCHECK(save_type_ == SAVE_AS_ONLY_HTML || save_type_ == SAVE_AS_COMPLETE_HTML); DCHECK(!saved_main_file_path_.empty() && @@ -241,6 +241,8 @@ SavePackage::SavePackage(TabContents* tab_contents) : file_manager_(NULL), tab_contents_(tab_contents), download_(NULL), + page_url_(GetUrlToBeSaved()), + title_(tab_contents->GetTitle()), finished_(false), user_canceled_(false), disk_error_occurred_(false), @@ -250,10 +252,7 @@ SavePackage::SavePackage(TabContents* tab_contents) tab_id_(tab_contents->GetRenderProcessHost()->id()), unique_id_(g_save_package_id++), ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { - - const GURL& current_page_url = GetUrlToBeSaved(); - DCHECK(current_page_url.is_valid()); - page_url_ = current_page_url; + DCHECK(page_url_.is_valid()); InternalInit(); } @@ -277,10 +276,6 @@ SavePackage::SavePackage(TabContents* tab_contents, tab_id_(0), unique_id_(g_save_package_id++), ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { - DCHECK(!saved_main_file_path_.empty() && - saved_main_file_path_.value().length() <= kMaxFilePathLength); - DCHECK(!saved_main_directory_path_.empty() && - saved_main_directory_path_.value().length() < kMaxFilePathLength); } SavePackage::~SavePackage() { @@ -329,7 +324,7 @@ GURL SavePackage::GetUrlToBeSaved() { // rather than the displayed one (returned by GetURL) which may be // different (like having "view-source:" on the front). NavigationEntry* active_entry = - tab_contents_->controller().GetActiveEntry(); + tab_contents_->controller().GetActiveEntry(); return active_entry->url(); } @@ -390,8 +385,7 @@ bool SavePackage::Init() { GetAllSavableResourceLinksForCurrentPage(); } else { wait_state_ = NET_FILES; - GURL u(page_url_); - SaveFileCreateInfo::SaveFileSource save_source = u.SchemeIsFile() ? + SaveFileCreateInfo::SaveFileSource save_source = page_url_.SchemeIsFile() ? SaveFileCreateInfo::SAVE_FILE_FROM_FILE : SaveFileCreateInfo::SAVE_FILE_FROM_NET; SaveItem* save_item = new SaveItem(page_url_, @@ -1044,9 +1038,8 @@ void SavePackage::GetAllSavableResourceLinksForCurrentPage() { return; wait_state_ = RESOURCES_LIST; - GURL main_page_url(page_url_); tab_contents_->render_view_host()-> - GetAllSavableResourceLinksForCurrentPage(main_page_url); + GetAllSavableResourceLinksForCurrentPage(page_url_); } // Give backend the lists which contain all resource links that have local @@ -1098,18 +1091,28 @@ void SavePackage::SetShouldPromptUser(bool should_prompt) { g_should_prompt_for_filename = should_prompt; } -// static. -FilePath SavePackage::GetSuggestedNameForSaveAs(const FilePath& name, +FilePath SavePackage::GetSuggestedNameForSaveAs( bool can_save_as_complete, const FilePath::StringType& contents_mime_type) { - // If the name is a URL, try to use the last path component or if there is - // none, the domain as the file name. - FilePath name_with_proper_ext = name; - GURL url(WideToUTF8(name_with_proper_ext.ToWStringHack())); - if (url.is_valid()) { + FilePath name_with_proper_ext = + FilePath::FromWStringHack(UTF16ToWideHack(title_)); + + // If the page's title matches its URL, use the URL. Try to use the last path + // component or if there is none, the domain as the file name. + // Normally we want to base the filename on the page title, or if it doesn't + // exist, on the URL. It's not easy to tell if the page has no title, because + // if the page has no title, TabContents::GetTitle() will return the page's + // URL (adjusted for display purposes). Therefore, we convert the "title" + // back to a URL, and if it matches the original page URL, we know the page + // had no title (or had a title equal to its URL, which is fine to treat + // similarly). + GURL fixed_up_title_url = + URLFixerUpper::FixupURL(UTF16ToUTF8(title_), std::string()); + + if (page_url_ == fixed_up_title_url) { std::string url_path; std::vector<std::string> url_parts; - SplitString(url.path(), '/', &url_parts); + SplitString(page_url_.path(), '/', &url_parts); if (!url_parts.empty()) { for (int i = static_cast<int>(url_parts.size()) - 1; i >= 0; --i) { url_path = url_parts[i]; @@ -1118,7 +1121,7 @@ FilePath SavePackage::GetSuggestedNameForSaveAs(const FilePath& name, } } if (url_path.empty()) - url_path = url.host(); + url_path = page_url_.host(); name_with_proper_ext = FilePath::FromWStringHack(UTF8ToWide(url_path)); } @@ -1235,9 +1238,6 @@ void SavePackage::ContinueGetSaveInfo(FilePath save_dir) { bool can_save_as_complete = CanSaveAsComplete(save_params->current_tab_mime_type); - FilePath title = - FilePath::FromWStringHack(UTF16ToWideHack(tab_contents_->GetTitle())); - #if defined(OS_POSIX) FilePath::StringType mime_type(save_params->current_tab_mime_type); #elif defined(OS_WIN) @@ -1245,9 +1245,8 @@ void SavePackage::ContinueGetSaveInfo(FilePath save_dir) { UTF8ToWide(save_params->current_tab_mime_type)); #endif // OS_WIN - FilePath suggested_path = - save_dir.Append(GetSuggestedNameForSaveAs(title, can_save_as_complete, - mime_type)); + FilePath suggested_path = save_dir.Append( + GetSuggestedNameForSaveAs(can_save_as_complete, mime_type)); // If the contents can not be saved as complete-HTML, do not show the // file filters. |