OLD | NEW |
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/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 file_manager_ = NULL; | 217 file_manager_ = NULL; |
218 } | 218 } |
219 | 219 |
220 GURL SavePackage::GetUrlToBeSaved() { | 220 GURL SavePackage::GetUrlToBeSaved() { |
221 // Instead of using tab_contents_.GetURL here, we use url() | 221 // Instead of using tab_contents_.GetURL here, we use url() |
222 // (which is the "real" url of the page) | 222 // (which is the "real" url of the page) |
223 // from the NavigationEntry because it reflects its' origin | 223 // from the NavigationEntry because it reflects its' origin |
224 // rather than the displayed one (returned by GetURL) which may be | 224 // rather than the displayed one (returned by GetURL) which may be |
225 // different (like having "view-source:" on the front). | 225 // different (like having "view-source:" on the front). |
226 NavigationEntry* active_entry = | 226 NavigationEntry* active_entry = |
227 tab_contents()->GetController().GetActiveEntry(); | 227 web_contents()->GetController().GetActiveEntry(); |
228 return active_entry->GetURL(); | 228 return active_entry->GetURL(); |
229 } | 229 } |
230 | 230 |
231 void SavePackage::Cancel(bool user_action) { | 231 void SavePackage::Cancel(bool user_action) { |
232 if (!canceled()) { | 232 if (!canceled()) { |
233 if (user_action) | 233 if (user_action) |
234 user_canceled_ = true; | 234 user_canceled_ = true; |
235 else | 235 else |
236 disk_error_occurred_ = true; | 236 disk_error_occurred_ = true; |
237 Stop(); | 237 Stop(); |
238 } | 238 } |
239 } | 239 } |
240 | 240 |
241 // Init() can be called directly, or indirectly via GetSaveInfo(). In both | 241 // Init() can be called directly, or indirectly via GetSaveInfo(). In both |
242 // cases, we need file_manager_ to be initialized, so we do this first. | 242 // cases, we need file_manager_ to be initialized, so we do this first. |
243 void SavePackage::InternalInit() { | 243 void SavePackage::InternalInit() { |
244 ResourceDispatcherHost* rdh = | 244 ResourceDispatcherHost* rdh = |
245 content::GetContentClient()->browser()->GetResourceDispatcherHost(); | 245 content::GetContentClient()->browser()->GetResourceDispatcherHost(); |
246 if (!rdh) { | 246 if (!rdh) { |
247 NOTREACHED(); | 247 NOTREACHED(); |
248 return; | 248 return; |
249 } | 249 } |
250 | 250 |
251 file_manager_ = rdh->save_file_manager(); | 251 file_manager_ = rdh->save_file_manager(); |
252 DCHECK(file_manager_); | 252 DCHECK(file_manager_); |
253 | 253 |
254 download_manager_ = tab_contents()->GetBrowserContext()->GetDownloadManager(); | 254 download_manager_ = web_contents()->GetBrowserContext()->GetDownloadManager(); |
255 DCHECK(download_manager_); | 255 DCHECK(download_manager_); |
256 } | 256 } |
257 | 257 |
258 bool SavePackage::Init() { | 258 bool SavePackage::Init() { |
259 // Set proper running state. | 259 // Set proper running state. |
260 if (wait_state_ != INITIALIZE) | 260 if (wait_state_ != INITIALIZE) |
261 return false; | 261 return false; |
262 | 262 |
263 wait_state_ = START_PROCESS; | 263 wait_state_ = START_PROCESS; |
264 | 264 |
265 // Initialize the request context and resource dispatcher. | 265 // Initialize the request context and resource dispatcher. |
266 content::BrowserContext* browser_context = | 266 content::BrowserContext* browser_context = |
267 tab_contents()->GetBrowserContext(); | 267 web_contents()->GetBrowserContext(); |
268 if (!browser_context) { | 268 if (!browser_context) { |
269 NOTREACHED(); | 269 NOTREACHED(); |
270 return false; | 270 return false; |
271 } | 271 } |
272 | 272 |
273 // The download manager keeps ownership but adds us as an observer. | 273 // The download manager keeps ownership but adds us as an observer. |
274 download_ = download_manager_->CreateSavePackageDownloadItem( | 274 download_ = download_manager_->CreateSavePackageDownloadItem( |
275 saved_main_file_path_, page_url_, | 275 saved_main_file_path_, page_url_, |
276 browser_context->IsOffTheRecord(), this); | 276 browser_context->IsOffTheRecord(), this); |
277 | 277 |
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
645 it != saved_success_items_.end(); ++it) | 645 it != saved_success_items_.end(); ++it) |
646 final_names.push_back(std::make_pair(it->first, | 646 final_names.push_back(std::make_pair(it->first, |
647 it->second->full_path())); | 647 it->second->full_path())); |
648 | 648 |
649 BrowserThread::PostTask( | 649 BrowserThread::PostTask( |
650 BrowserThread::FILE, FROM_HERE, | 650 BrowserThread::FILE, FROM_HERE, |
651 base::Bind(&SaveFileManager::RenameAllFiles, | 651 base::Bind(&SaveFileManager::RenameAllFiles, |
652 file_manager_, | 652 file_manager_, |
653 final_names, | 653 final_names, |
654 dir, | 654 dir, |
655 tab_contents()->GetRenderProcessHost()->GetID(), | 655 web_contents()->GetRenderProcessHost()->GetID(), |
656 tab_contents()->GetRenderViewHost()->routing_id(), | 656 web_contents()->GetRenderViewHost()->routing_id(), |
657 id())); | 657 id())); |
658 } | 658 } |
659 | 659 |
660 // Successfully finished all items of this SavePackage. | 660 // Successfully finished all items of this SavePackage. |
661 void SavePackage::Finish() { | 661 void SavePackage::Finish() { |
662 // User may cancel the job when we're moving files to the final directory. | 662 // User may cancel the job when we're moving files to the final directory. |
663 if (canceled()) | 663 if (canceled()) |
664 return; | 664 return; |
665 | 665 |
666 wait_state_ = SUCCESSFUL; | 666 wait_state_ = SUCCESSFUL; |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
777 base::Bind(&SaveFileManager::CancelSave, | 777 base::Bind(&SaveFileManager::CancelSave, |
778 file_manager_, | 778 file_manager_, |
779 save_item->save_id())); | 779 save_item->save_id())); |
780 } | 780 } |
781 | 781 |
782 // Initiate a saving job of a specific URL. We send the request to | 782 // Initiate a saving job of a specific URL. We send the request to |
783 // SaveFileManager, which will dispatch it to different approach according to | 783 // SaveFileManager, which will dispatch it to different approach according to |
784 // the save source. Parameter process_all_remaining_items indicates whether | 784 // the save source. Parameter process_all_remaining_items indicates whether |
785 // we need to save all remaining items. | 785 // we need to save all remaining items. |
786 void SavePackage::SaveNextFile(bool process_all_remaining_items) { | 786 void SavePackage::SaveNextFile(bool process_all_remaining_items) { |
787 DCHECK(tab_contents()); | 787 DCHECK(web_contents()); |
788 DCHECK(waiting_item_queue_.size()); | 788 DCHECK(waiting_item_queue_.size()); |
789 | 789 |
790 do { | 790 do { |
791 // Pop SaveItem from waiting list. | 791 // Pop SaveItem from waiting list. |
792 SaveItem* save_item = waiting_item_queue_.front(); | 792 SaveItem* save_item = waiting_item_queue_.front(); |
793 waiting_item_queue_.pop(); | 793 waiting_item_queue_.pop(); |
794 | 794 |
795 // Add the item to in_progress_items_. | 795 // Add the item to in_progress_items_. |
796 SaveUrlItemMap::iterator it = in_progress_items_.find( | 796 SaveUrlItemMap::iterator it = in_progress_items_.find( |
797 save_item->url().spec()); | 797 save_item->url().spec()); |
798 DCHECK(it == in_progress_items_.end()); | 798 DCHECK(it == in_progress_items_.end()); |
799 in_progress_items_[save_item->url().spec()] = save_item; | 799 in_progress_items_[save_item->url().spec()] = save_item; |
800 save_item->Start(); | 800 save_item->Start(); |
801 file_manager_->SaveURL(save_item->url(), | 801 file_manager_->SaveURL(save_item->url(), |
802 save_item->referrer(), | 802 save_item->referrer(), |
803 tab_contents()->GetRenderProcessHost()->GetID(), | 803 web_contents()->GetRenderProcessHost()->GetID(), |
804 routing_id(), | 804 routing_id(), |
805 save_item->save_source(), | 805 save_item->save_source(), |
806 save_item->full_path(), | 806 save_item->full_path(), |
807 tab_contents()-> | 807 web_contents()-> |
808 GetBrowserContext()->GetResourceContext(), | 808 GetBrowserContext()->GetResourceContext(), |
809 this); | 809 this); |
810 } while (process_all_remaining_items && waiting_item_queue_.size()); | 810 } while (process_all_remaining_items && waiting_item_queue_.size()); |
811 } | 811 } |
812 | 812 |
813 // Calculate the percentage of whole save page job. | 813 // Calculate the percentage of whole save page job. |
814 int SavePackage::PercentComplete() { | 814 int SavePackage::PercentComplete() { |
815 if (!all_save_items_count_) | 815 if (!all_save_items_count_) |
816 return 0; | 816 return 0; |
817 else if (!in_process_count()) | 817 else if (!in_process_count()) |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1155 static_cast<TabContents*>(content::WebContentsObserver::web_contents()); | 1155 static_cast<TabContents*>(content::WebContentsObserver::web_contents()); |
1156 } | 1156 } |
1157 | 1157 |
1158 void SavePackage::GetSaveInfo() { | 1158 void SavePackage::GetSaveInfo() { |
1159 // Can't use tab_contents_ in the file thread, so get the data that we need | 1159 // Can't use tab_contents_ in the file thread, so get the data that we need |
1160 // before calling to it. | 1160 // before calling to it. |
1161 FilePath website_save_dir, download_save_dir; | 1161 FilePath website_save_dir, download_save_dir; |
1162 DCHECK(download_manager_); | 1162 DCHECK(download_manager_); |
1163 download_manager_->delegate()->GetSaveDir( | 1163 download_manager_->delegate()->GetSaveDir( |
1164 tab_contents(), &website_save_dir, &download_save_dir); | 1164 tab_contents(), &website_save_dir, &download_save_dir); |
1165 std::string mime_type = tab_contents()->GetContentsMimeType(); | 1165 std::string mime_type = web_contents()->GetContentsMimeType(); |
1166 std::string accept_languages = | 1166 std::string accept_languages = |
1167 content::GetContentClient()->browser()->GetAcceptLangs( | 1167 content::GetContentClient()->browser()->GetAcceptLangs( |
1168 tab_contents()->GetBrowserContext()); | 1168 web_contents()->GetBrowserContext()); |
1169 | 1169 |
1170 BrowserThread::PostTask( | 1170 BrowserThread::PostTask( |
1171 BrowserThread::FILE, FROM_HERE, | 1171 BrowserThread::FILE, FROM_HERE, |
1172 base::Bind(&SavePackage::CreateDirectoryOnFileThread, this, | 1172 base::Bind(&SavePackage::CreateDirectoryOnFileThread, this, |
1173 website_save_dir, download_save_dir, mime_type, accept_languages)); | 1173 website_save_dir, download_save_dir, mime_type, accept_languages)); |
1174 } | 1174 } |
1175 | 1175 |
1176 void SavePackage::CreateDirectoryOnFileThread( | 1176 void SavePackage::CreateDirectoryOnFileThread( |
1177 const FilePath& website_save_dir, | 1177 const FilePath& website_save_dir, |
1178 const FilePath& download_save_dir, | 1178 const FilePath& download_save_dir, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1216 BrowserThread::UI, FROM_HERE, | 1216 BrowserThread::UI, FROM_HERE, |
1217 base::Bind(&SavePackage::ContinueGetSaveInfo, this, save_dir, | 1217 base::Bind(&SavePackage::ContinueGetSaveInfo, this, save_dir, |
1218 can_save_as_complete)); | 1218 can_save_as_complete)); |
1219 } | 1219 } |
1220 | 1220 |
1221 void SavePackage::ContinueGetSaveInfo(const FilePath& suggested_path, | 1221 void SavePackage::ContinueGetSaveInfo(const FilePath& suggested_path, |
1222 bool can_save_as_complete) { | 1222 bool can_save_as_complete) { |
1223 // The TabContents which owns this SavePackage may have disappeared during | 1223 // The TabContents which owns this SavePackage may have disappeared during |
1224 // the UI->FILE->UI thread hop of | 1224 // the UI->FILE->UI thread hop of |
1225 // GetSaveInfo->CreateDirectoryOnFileThread->ContinueGetSaveInfo. | 1225 // GetSaveInfo->CreateDirectoryOnFileThread->ContinueGetSaveInfo. |
1226 if (!tab_contents()) | 1226 if (!web_contents()) |
1227 return; | 1227 return; |
1228 | 1228 |
1229 download_manager_->delegate()->ChooseSavePath( | 1229 download_manager_->delegate()->ChooseSavePath( |
1230 AsWeakPtr(), suggested_path, can_save_as_complete); | 1230 AsWeakPtr(), suggested_path, can_save_as_complete); |
1231 } | 1231 } |
1232 | 1232 |
1233 // Called after the save file dialog box returns. | 1233 // Called after the save file dialog box returns. |
1234 void SavePackage::OnPathPicked(const FilePath& final_name, | 1234 void SavePackage::OnPathPicked(const FilePath& final_name, |
1235 SavePackageType type) { | 1235 SavePackageType type) { |
1236 // Ensure the filename is safe. | 1236 // Ensure the filename is safe. |
1237 saved_main_file_path_ = final_name; | 1237 saved_main_file_path_ = final_name; |
1238 // TODO(asanka): This call may block on IO and shouldn't be made | 1238 // TODO(asanka): This call may block on IO and shouldn't be made |
1239 // from the UI thread. See http://crbug.com/61827. | 1239 // from the UI thread. See http://crbug.com/61827. |
1240 net::GenerateSafeFileName(tab_contents()->GetContentsMimeType(), false, | 1240 net::GenerateSafeFileName(web_contents()->GetContentsMimeType(), false, |
1241 &saved_main_file_path_); | 1241 &saved_main_file_path_); |
1242 | 1242 |
1243 saved_main_directory_path_ = saved_main_file_path_.DirName(); | 1243 saved_main_directory_path_ = saved_main_file_path_.DirName(); |
1244 save_type_ = type; | 1244 save_type_ = type; |
1245 if (save_type_ == SavePackage::SAVE_AS_COMPLETE_HTML) { | 1245 if (save_type_ == SavePackage::SAVE_AS_COMPLETE_HTML) { |
1246 // Make new directory for saving complete file. | 1246 // Make new directory for saving complete file. |
1247 saved_main_directory_path_ = saved_main_directory_path_.Append( | 1247 saved_main_directory_path_ = saved_main_directory_path_.Append( |
1248 saved_main_file_path_.RemoveExtension().BaseName().value() + | 1248 saved_main_file_path_.RemoveExtension().BaseName().value() + |
1249 FILE_PATH_LITERAL("_files")); | 1249 FILE_PATH_LITERAL("_files")); |
1250 } | 1250 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1293 StopObservation(); | 1293 StopObservation(); |
1294 } | 1294 } |
1295 | 1295 |
1296 void SavePackage::FinalizeDownloadEntry() { | 1296 void SavePackage::FinalizeDownloadEntry() { |
1297 DCHECK(download_); | 1297 DCHECK(download_); |
1298 DCHECK(download_manager_); | 1298 DCHECK(download_manager_); |
1299 | 1299 |
1300 download_manager_->SavePageDownloadFinished(download_); | 1300 download_manager_->SavePageDownloadFinished(download_); |
1301 StopObservation(); | 1301 StopObservation(); |
1302 } | 1302 } |
OLD | NEW |