| 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 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 saved_main_file_path_(file_full_path), | 129 saved_main_file_path_(file_full_path), |
| 130 saved_main_directory_path_(directory_full_path), | 130 saved_main_directory_path_(directory_full_path), |
| 131 title_(web_contents->GetTitle()), | 131 title_(web_contents->GetTitle()), |
| 132 start_tick_(base::TimeTicks::Now()), | 132 start_tick_(base::TimeTicks::Now()), |
| 133 finished_(false), | 133 finished_(false), |
| 134 user_canceled_(false), | 134 user_canceled_(false), |
| 135 disk_error_occurred_(false), | 135 disk_error_occurred_(false), |
| 136 save_type_(save_type), | 136 save_type_(save_type), |
| 137 all_save_items_count_(0), | 137 all_save_items_count_(0), |
| 138 wait_state_(INITIALIZE), | 138 wait_state_(INITIALIZE), |
| 139 tab_id_(web_contents->GetRenderProcessHost()->GetID()), | 139 contents_id_(web_contents->GetRenderProcessHost()->GetID()), |
| 140 unique_id_(g_save_package_id++), | 140 unique_id_(g_save_package_id++), |
| 141 wrote_to_completed_file_(false), | 141 wrote_to_completed_file_(false), |
| 142 wrote_to_failed_file_(false) { | 142 wrote_to_failed_file_(false) { |
| 143 DCHECK(page_url_.is_valid()); | 143 DCHECK(page_url_.is_valid()); |
| 144 DCHECK(save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML || | 144 DCHECK(save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML || |
| 145 save_type_ == content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML); | 145 save_type_ == content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML); |
| 146 DCHECK(!saved_main_file_path_.empty() && | 146 DCHECK(!saved_main_file_path_.empty() && |
| 147 saved_main_file_path_.value().length() <= kMaxFilePathLength); | 147 saved_main_file_path_.value().length() <= kMaxFilePathLength); |
| 148 DCHECK(!saved_main_directory_path_.empty() && | 148 DCHECK(!saved_main_directory_path_.empty() && |
| 149 saved_main_directory_path_.value().length() < kMaxFilePathLength); | 149 saved_main_directory_path_.value().length() < kMaxFilePathLength); |
| 150 InternalInit(); | 150 InternalInit(); |
| 151 } | 151 } |
| 152 | 152 |
| 153 SavePackage::SavePackage(WebContents* web_contents) | 153 SavePackage::SavePackage(WebContents* web_contents) |
| 154 : content::WebContentsObserver(web_contents), | 154 : content::WebContentsObserver(web_contents), |
| 155 file_manager_(NULL), | 155 file_manager_(NULL), |
| 156 download_manager_(NULL), | 156 download_manager_(NULL), |
| 157 download_(NULL), | 157 download_(NULL), |
| 158 page_url_(GetUrlToBeSaved()), | 158 page_url_(GetUrlToBeSaved()), |
| 159 title_(web_contents->GetTitle()), | 159 title_(web_contents->GetTitle()), |
| 160 start_tick_(base::TimeTicks::Now()), | 160 start_tick_(base::TimeTicks::Now()), |
| 161 finished_(false), | 161 finished_(false), |
| 162 user_canceled_(false), | 162 user_canceled_(false), |
| 163 disk_error_occurred_(false), | 163 disk_error_occurred_(false), |
| 164 save_type_(content::SAVE_PAGE_TYPE_UNKNOWN), | 164 save_type_(content::SAVE_PAGE_TYPE_UNKNOWN), |
| 165 all_save_items_count_(0), | 165 all_save_items_count_(0), |
| 166 wait_state_(INITIALIZE), | 166 wait_state_(INITIALIZE), |
| 167 tab_id_(web_contents->GetRenderProcessHost()->GetID()), | 167 contents_id_(web_contents->GetRenderProcessHost()->GetID()), |
| 168 unique_id_(g_save_package_id++), | 168 unique_id_(g_save_package_id++), |
| 169 wrote_to_completed_file_(false), | 169 wrote_to_completed_file_(false), |
| 170 wrote_to_failed_file_(false) { | 170 wrote_to_failed_file_(false) { |
| 171 DCHECK(page_url_.is_valid()); | 171 DCHECK(page_url_.is_valid()); |
| 172 InternalInit(); | 172 InternalInit(); |
| 173 } | 173 } |
| 174 | 174 |
| 175 // This is for testing use. Set |finished_| as true because we don't want | 175 // This is for testing use. Set |finished_| as true because we don't want |
| 176 // method Cancel to be be called in destructor in test mode. | 176 // method Cancel to be be called in destructor in test mode. |
| 177 // We also don't call InternalInit(). | 177 // We also don't call InternalInit(). |
| 178 SavePackage::SavePackage(WebContents* web_contents, | 178 SavePackage::SavePackage(WebContents* web_contents, |
| 179 const FilePath& file_full_path, | 179 const FilePath& file_full_path, |
| 180 const FilePath& directory_full_path) | 180 const FilePath& directory_full_path) |
| 181 : content::WebContentsObserver(web_contents), | 181 : content::WebContentsObserver(web_contents), |
| 182 file_manager_(NULL), | 182 file_manager_(NULL), |
| 183 download_manager_(NULL), | 183 download_manager_(NULL), |
| 184 download_(NULL), | 184 download_(NULL), |
| 185 saved_main_file_path_(file_full_path), | 185 saved_main_file_path_(file_full_path), |
| 186 saved_main_directory_path_(directory_full_path), | 186 saved_main_directory_path_(directory_full_path), |
| 187 start_tick_(base::TimeTicks::Now()), | 187 start_tick_(base::TimeTicks::Now()), |
| 188 finished_(true), | 188 finished_(true), |
| 189 user_canceled_(false), | 189 user_canceled_(false), |
| 190 disk_error_occurred_(false), | 190 disk_error_occurred_(false), |
| 191 save_type_(content::SAVE_PAGE_TYPE_UNKNOWN), | 191 save_type_(content::SAVE_PAGE_TYPE_UNKNOWN), |
| 192 all_save_items_count_(0), | 192 all_save_items_count_(0), |
| 193 wait_state_(INITIALIZE), | 193 wait_state_(INITIALIZE), |
| 194 tab_id_(0), | 194 contents_id_(0), |
| 195 unique_id_(g_save_package_id++), | 195 unique_id_(g_save_package_id++), |
| 196 wrote_to_completed_file_(false), | 196 wrote_to_completed_file_(false), |
| 197 wrote_to_failed_file_(false) { | 197 wrote_to_failed_file_(false) { |
| 198 } | 198 } |
| 199 | 199 |
| 200 SavePackage::~SavePackage() { | 200 SavePackage::~SavePackage() { |
| 201 // Stop receiving saving job's updates | 201 // Stop receiving saving job's updates |
| 202 if (!finished_ && !canceled()) { | 202 if (!finished_ && !canceled()) { |
| 203 // Unexpected quit. | 203 // Unexpected quit. |
| 204 Cancel(true); | 204 Cancel(true); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 219 } | 219 } |
| 220 | 220 |
| 221 STLDeleteValues(&saved_success_items_); | 221 STLDeleteValues(&saved_success_items_); |
| 222 STLDeleteValues(&in_progress_items_); | 222 STLDeleteValues(&in_progress_items_); |
| 223 STLDeleteValues(&saved_failed_items_); | 223 STLDeleteValues(&saved_failed_items_); |
| 224 | 224 |
| 225 file_manager_ = NULL; | 225 file_manager_ = NULL; |
| 226 } | 226 } |
| 227 | 227 |
| 228 GURL SavePackage::GetUrlToBeSaved() { | 228 GURL SavePackage::GetUrlToBeSaved() { |
| 229 // Instead of using tab_contents_.GetURL here, we use url() | 229 // Instead of using web_contents_.GetURL here, we use url() (which is the |
| 230 // (which is the "real" url of the page) | 230 // "real" url of the page) from the NavigationEntry because it reflects its |
| 231 // from the NavigationEntry because it reflects its' origin | 231 // origin rather than the displayed one (returned by GetURL) which may be |
| 232 // rather than the displayed one (returned by GetURL) which may be | |
| 233 // different (like having "view-source:" on the front). | 232 // different (like having "view-source:" on the front). |
| 234 NavigationEntry* active_entry = | 233 NavigationEntry* active_entry = |
| 235 web_contents()->GetController().GetActiveEntry(); | 234 web_contents()->GetController().GetActiveEntry(); |
| 236 return active_entry->GetURL(); | 235 return active_entry->GetURL(); |
| 237 } | 236 } |
| 238 | 237 |
| 239 void SavePackage::Cancel(bool user_action) { | 238 void SavePackage::Cancel(bool user_action) { |
| 240 if (!canceled()) { | 239 if (!canceled()) { |
| 241 if (user_action) | 240 if (user_action) |
| 242 user_canceled_ = true; | 241 user_canceled_ = true; |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 518 | 517 |
| 519 // If the save source is from file system, inform SaveFileManager to copy | 518 // If the save source is from file system, inform SaveFileManager to copy |
| 520 // corresponding file to the file path which this SaveItem specifies. | 519 // corresponding file to the file path which this SaveItem specifies. |
| 521 if (info->save_source == SaveFileCreateInfo::SAVE_FILE_FROM_FILE) { | 520 if (info->save_source == SaveFileCreateInfo::SAVE_FILE_FROM_FILE) { |
| 522 BrowserThread::PostTask( | 521 BrowserThread::PostTask( |
| 523 BrowserThread::FILE, FROM_HERE, | 522 BrowserThread::FILE, FROM_HERE, |
| 524 base::Bind(&SaveFileManager::SaveLocalFile, | 523 base::Bind(&SaveFileManager::SaveLocalFile, |
| 525 file_manager_, | 524 file_manager_, |
| 526 save_item->url(), | 525 save_item->url(), |
| 527 save_item->save_id(), | 526 save_item->save_id(), |
| 528 tab_id())); | 527 contents_id())); |
| 529 return; | 528 return; |
| 530 } | 529 } |
| 531 | 530 |
| 532 // Check whether we begin to require serialized HTML data. | 531 // Check whether we begin to require serialized HTML data. |
| 533 if (save_type_ == content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML && | 532 if (save_type_ == content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML && |
| 534 wait_state_ == HTML_DATA) { | 533 wait_state_ == HTML_DATA) { |
| 535 // Inform backend to serialize the all frames' DOM and send serialized | 534 // Inform backend to serialize the all frames' DOM and send serialized |
| 536 // HTML data back. | 535 // HTML data back. |
| 537 GetSerializedHtmlDataForCurrentPageWithLocalLinks(); | 536 GetSerializedHtmlDataForCurrentPageWithLocalLinks(); |
| 538 } | 537 } |
| (...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 953 // retrieved from render process. | 952 // retrieved from render process. |
| 954 void SavePackage::OnReceivedSerializedHtmlData(const GURL& frame_url, | 953 void SavePackage::OnReceivedSerializedHtmlData(const GURL& frame_url, |
| 955 const std::string& data, | 954 const std::string& data, |
| 956 int32 status) { | 955 int32 status) { |
| 957 WebPageSerializerClient::PageSerializationStatus flag = | 956 WebPageSerializerClient::PageSerializationStatus flag = |
| 958 static_cast<WebPageSerializerClient::PageSerializationStatus>(status); | 957 static_cast<WebPageSerializerClient::PageSerializationStatus>(status); |
| 959 // Check current state. | 958 // Check current state. |
| 960 if (wait_state_ != HTML_DATA) | 959 if (wait_state_ != HTML_DATA) |
| 961 return; | 960 return; |
| 962 | 961 |
| 963 int id = tab_id(); | 962 int id = contents_id(); |
| 964 // If the all frames are finished saving, we need to close the | 963 // If the all frames are finished saving, we need to close the |
| 965 // remaining SaveItems. | 964 // remaining SaveItems. |
| 966 if (flag == WebPageSerializerClient::AllFramesAreFinished) { | 965 if (flag == WebPageSerializerClient::AllFramesAreFinished) { |
| 967 for (SaveUrlItemMap::iterator it = in_progress_items_.begin(); | 966 for (SaveUrlItemMap::iterator it = in_progress_items_.begin(); |
| 968 it != in_progress_items_.end(); ++it) { | 967 it != in_progress_items_.end(); ++it) { |
| 969 VLOG(20) << " " << __FUNCTION__ << "()" | 968 VLOG(20) << " " << __FUNCTION__ << "()" |
| 970 << " save_id = " << it->second->save_id() | 969 << " save_id = " << it->second->save_id() |
| 971 << " url = \"" << it->second->url().spec() << "\""; | 970 << " url = \"" << it->second->url().spec() << "\""; |
| 972 BrowserThread::PostTask( | 971 BrowserThread::PostTask( |
| 973 BrowserThread::FILE, FROM_HERE, | 972 BrowserThread::FILE, FROM_HERE, |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1187 return extensions[i].suggested_extension; | 1186 return extensions[i].suggested_extension; |
| 1188 } | 1187 } |
| 1189 return FILE_PATH_LITERAL(""); | 1188 return FILE_PATH_LITERAL(""); |
| 1190 } | 1189 } |
| 1191 | 1190 |
| 1192 WebContents* SavePackage::web_contents() const { | 1191 WebContents* SavePackage::web_contents() const { |
| 1193 return content::WebContentsObserver::web_contents(); | 1192 return content::WebContentsObserver::web_contents(); |
| 1194 } | 1193 } |
| 1195 | 1194 |
| 1196 void SavePackage::GetSaveInfo() { | 1195 void SavePackage::GetSaveInfo() { |
| 1197 // Can't use tab_contents_ in the file thread, so get the data that we need | 1196 // Can't use web_contents_ in the file thread, so get the data that we need |
| 1198 // before calling to it. | 1197 // before calling to it. |
| 1199 FilePath website_save_dir, download_save_dir; | 1198 FilePath website_save_dir, download_save_dir; |
| 1200 DCHECK(download_manager_); | 1199 DCHECK(download_manager_); |
| 1201 download_manager_->delegate()->GetSaveDir( | 1200 download_manager_->delegate()->GetSaveDir( |
| 1202 web_contents(), &website_save_dir, &download_save_dir); | 1201 web_contents(), &website_save_dir, &download_save_dir); |
| 1203 std::string mime_type = web_contents()->GetContentsMimeType(); | 1202 std::string mime_type = web_contents()->GetContentsMimeType(); |
| 1204 std::string accept_languages = | 1203 std::string accept_languages = |
| 1205 content::GetContentClient()->browser()->GetAcceptLangs( | 1204 content::GetContentClient()->browser()->GetAcceptLangs( |
| 1206 web_contents()->GetBrowserContext()); | 1205 web_contents()->GetBrowserContext()); |
| 1207 | 1206 |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1315 StopObservation(); | 1314 StopObservation(); |
| 1316 } | 1315 } |
| 1317 | 1316 |
| 1318 void SavePackage::FinalizeDownloadEntry() { | 1317 void SavePackage::FinalizeDownloadEntry() { |
| 1319 DCHECK(download_); | 1318 DCHECK(download_); |
| 1320 DCHECK(download_manager_); | 1319 DCHECK(download_manager_); |
| 1321 | 1320 |
| 1322 download_manager_->SavePageDownloadFinished(download_); | 1321 download_manager_->SavePageDownloadFinished(download_); |
| 1323 StopObservation(); | 1322 StopObservation(); |
| 1324 } | 1323 } |
| OLD | NEW |