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 "chrome/browser/download/save_package.h" | 5 #include "chrome/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 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 saved_main_directory_path_(directory_full_path), | 166 saved_main_directory_path_(directory_full_path), |
167 title_(tab_contents()->GetTitle()), | 167 title_(tab_contents()->GetTitle()), |
168 finished_(false), | 168 finished_(false), |
169 user_canceled_(false), | 169 user_canceled_(false), |
170 disk_error_occurred_(false), | 170 disk_error_occurred_(false), |
171 save_type_(save_type), | 171 save_type_(save_type), |
172 all_save_items_count_(0), | 172 all_save_items_count_(0), |
173 wait_state_(INITIALIZE), | 173 wait_state_(INITIALIZE), |
174 tab_id_(tab_contents()->GetRenderProcessHost()->id()), | 174 tab_id_(tab_contents()->GetRenderProcessHost()->id()), |
175 unique_id_(g_save_package_id++), | 175 unique_id_(g_save_package_id++), |
| 176 download_manager_(NULL), |
176 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 177 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
177 DCHECK(page_url_.is_valid()); | 178 DCHECK(page_url_.is_valid()); |
178 DCHECK(save_type_ == SAVE_AS_ONLY_HTML || | 179 DCHECK(save_type_ == SAVE_AS_ONLY_HTML || |
179 save_type_ == SAVE_AS_COMPLETE_HTML); | 180 save_type_ == SAVE_AS_COMPLETE_HTML); |
180 DCHECK(!saved_main_file_path_.empty() && | 181 DCHECK(!saved_main_file_path_.empty() && |
181 saved_main_file_path_.value().length() <= kMaxFilePathLength); | 182 saved_main_file_path_.value().length() <= kMaxFilePathLength); |
182 DCHECK(!saved_main_directory_path_.empty() && | 183 DCHECK(!saved_main_directory_path_.empty() && |
183 saved_main_directory_path_.value().length() < kMaxFilePathLength); | 184 saved_main_directory_path_.value().length() < kMaxFilePathLength); |
184 InternalInit(); | 185 InternalInit(); |
185 } | 186 } |
186 | 187 |
187 SavePackage::SavePackage(TabContentsWrapper* wrapper) | 188 SavePackage::SavePackage(TabContentsWrapper* wrapper) |
188 : TabContentsObserver(wrapper->tab_contents()), | 189 : TabContentsObserver(wrapper->tab_contents()), |
189 wrapper_(wrapper), | 190 wrapper_(wrapper), |
190 file_manager_(NULL), | 191 file_manager_(NULL), |
191 download_(NULL), | 192 download_(NULL), |
192 page_url_(GetUrlToBeSaved()), | 193 page_url_(GetUrlToBeSaved()), |
193 title_(tab_contents()->GetTitle()), | 194 title_(tab_contents()->GetTitle()), |
194 finished_(false), | 195 finished_(false), |
195 user_canceled_(false), | 196 user_canceled_(false), |
196 disk_error_occurred_(false), | 197 disk_error_occurred_(false), |
197 save_type_(SAVE_TYPE_UNKNOWN), | 198 save_type_(SAVE_TYPE_UNKNOWN), |
198 all_save_items_count_(0), | 199 all_save_items_count_(0), |
199 wait_state_(INITIALIZE), | 200 wait_state_(INITIALIZE), |
200 tab_id_(tab_contents()->GetRenderProcessHost()->id()), | 201 tab_id_(tab_contents()->GetRenderProcessHost()->id()), |
201 unique_id_(g_save_package_id++), | 202 unique_id_(g_save_package_id++), |
| 203 download_manager_(NULL), |
202 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 204 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
203 DCHECK(page_url_.is_valid()); | 205 DCHECK(page_url_.is_valid()); |
204 InternalInit(); | 206 InternalInit(); |
205 } | 207 } |
206 | 208 |
207 // This is for testing use. Set |finished_| as true because we don't want | 209 // This is for testing use. Set |finished_| as true because we don't want |
208 // method Cancel to be be called in destructor in test mode. | 210 // method Cancel to be be called in destructor in test mode. |
209 // We also don't call InternalInit(). | 211 // We also don't call InternalInit(). |
210 SavePackage::SavePackage(TabContentsWrapper* wrapper, | 212 SavePackage::SavePackage(TabContentsWrapper* wrapper, |
211 const FilePath& file_full_path, | 213 const FilePath& file_full_path, |
212 const FilePath& directory_full_path) | 214 const FilePath& directory_full_path) |
213 : TabContentsObserver(wrapper->tab_contents()), | 215 : TabContentsObserver(wrapper->tab_contents()), |
214 wrapper_(wrapper), | 216 wrapper_(wrapper), |
215 file_manager_(NULL), | 217 file_manager_(NULL), |
216 download_(NULL), | 218 download_(NULL), |
217 saved_main_file_path_(file_full_path), | 219 saved_main_file_path_(file_full_path), |
218 saved_main_directory_path_(directory_full_path), | 220 saved_main_directory_path_(directory_full_path), |
219 finished_(true), | 221 finished_(true), |
220 user_canceled_(false), | 222 user_canceled_(false), |
221 disk_error_occurred_(false), | 223 disk_error_occurred_(false), |
222 save_type_(SAVE_TYPE_UNKNOWN), | 224 save_type_(SAVE_TYPE_UNKNOWN), |
223 all_save_items_count_(0), | 225 all_save_items_count_(0), |
224 wait_state_(INITIALIZE), | 226 wait_state_(INITIALIZE), |
225 tab_id_(0), | 227 tab_id_(0), |
226 unique_id_(g_save_package_id++), | 228 unique_id_(g_save_package_id++), |
| 229 download_manager_(NULL), |
227 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 230 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
228 } | 231 } |
229 | 232 |
230 SavePackage::~SavePackage() { | 233 SavePackage::~SavePackage() { |
231 // Stop receiving saving job's updates | 234 // Stop receiving saving job's updates |
232 if (!finished_ && !canceled()) { | 235 if (!finished_ && !canceled()) { |
233 // Unexpected quit. | 236 // Unexpected quit. |
234 Cancel(true); | 237 Cancel(true); |
235 } | 238 } |
236 | 239 |
| 240 // We should no longer be observing the DownloadManager. |
| 241 CHECK(!download_manager_); |
| 242 |
237 DCHECK(all_save_items_count_ == (waiting_item_queue_.size() + | 243 DCHECK(all_save_items_count_ == (waiting_item_queue_.size() + |
238 completed_count() + | 244 completed_count() + |
239 in_process_count())); | 245 in_process_count())); |
240 // Free all SaveItems. | 246 // Free all SaveItems. |
241 while (!waiting_item_queue_.empty()) { | 247 while (!waiting_item_queue_.empty()) { |
242 // We still have some items which are waiting for start to save. | 248 // We still have some items which are waiting for start to save. |
243 SaveItem* save_item = waiting_item_queue_.front(); | 249 SaveItem* save_item = waiting_item_queue_.front(); |
244 waiting_item_queue_.pop(); | 250 waiting_item_queue_.pop(); |
245 delete save_item; | 251 delete save_item; |
246 } | 252 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 | 313 |
308 wait_state_ = START_PROCESS; | 314 wait_state_ = START_PROCESS; |
309 | 315 |
310 // Initialize the request context and resource dispatcher. | 316 // Initialize the request context and resource dispatcher. |
311 Profile* profile = tab_contents()->profile(); | 317 Profile* profile = tab_contents()->profile(); |
312 if (!profile) { | 318 if (!profile) { |
313 NOTREACHED(); | 319 NOTREACHED(); |
314 return false; | 320 return false; |
315 } | 321 } |
316 | 322 |
317 // Create the fake DownloadItem and display the view. | 323 // Get the download manager and add ourselves as an observer. |
318 DownloadManager* download_manager = | 324 download_manager_ = tab_contents()->profile()->GetDownloadManager(); |
319 tab_contents()->profile()->GetDownloadManager(); | 325 if (!download_manager_) { |
320 download_ = new DownloadItem(download_manager, | 326 NOTREACHED(); |
| 327 return false; |
| 328 } |
| 329 download_manager_->AddObserver(this); |
| 330 |
| 331 // Create the download item. |
| 332 download_ = new DownloadItem(download_manager_, |
321 saved_main_file_path_, | 333 saved_main_file_path_, |
322 page_url_, | 334 page_url_, |
323 profile->IsOffTheRecord()); | 335 profile->IsOffTheRecord()); |
324 | 336 |
325 // Transfer the ownership to the download manager. We need the DownloadItem | 337 // Transfer ownership of download item to DownloadManager. |
326 // to be alive as long as the Profile is alive. | 338 download_manager_->SavePageDownloadStarted(download_); |
327 download_manager->SavePageAsDownloadStarted(download_); | |
328 | |
329 wrapper_->download_tab_helper()->OnStartDownload(download_); | |
330 | 339 |
331 // Check save type and process the save page job. | 340 // Check save type and process the save page job. |
332 if (save_type_ == SAVE_AS_COMPLETE_HTML) { | 341 if (save_type_ == SAVE_AS_COMPLETE_HTML) { |
333 // Get directory | 342 // Get directory |
334 DCHECK(!saved_main_directory_path_.empty()); | 343 DCHECK(!saved_main_directory_path_.empty()); |
335 GetAllSavableResourceLinksForCurrentPage(); | 344 GetAllSavableResourceLinksForCurrentPage(); |
336 } else { | 345 } else { |
337 wait_state_ = NET_FILES; | 346 wait_state_ = NET_FILES; |
338 SaveFileCreateInfo::SaveFileSource save_source = page_url_.SchemeIsFile() ? | 347 SaveFileCreateInfo::SaveFileSource save_source = page_url_.SchemeIsFile() ? |
339 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : | 348 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : |
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
684 BrowserThread::FILE, FROM_HERE, | 693 BrowserThread::FILE, FROM_HERE, |
685 NewRunnableMethod(file_manager_, | 694 NewRunnableMethod(file_manager_, |
686 &SaveFileManager::RemoveSavedFileFromFileMap, | 695 &SaveFileManager::RemoveSavedFileFromFileMap, |
687 save_ids)); | 696 save_ids)); |
688 | 697 |
689 finished_ = true; | 698 finished_ = true; |
690 wait_state_ = FAILED; | 699 wait_state_ = FAILED; |
691 | 700 |
692 // Inform the DownloadItem we have canceled whole save page job. | 701 // Inform the DownloadItem we have canceled whole save page job. |
693 download_->Cancel(false); | 702 download_->Cancel(false); |
| 703 FinalizeDownloadEntry(); |
694 } | 704 } |
695 | 705 |
696 void SavePackage::CheckFinish() { | 706 void SavePackage::CheckFinish() { |
697 if (in_process_count() || finished_) | 707 if (in_process_count() || finished_) |
698 return; | 708 return; |
699 | 709 |
700 FilePath dir = (save_type_ == SAVE_AS_COMPLETE_HTML && | 710 FilePath dir = (save_type_ == SAVE_AS_COMPLETE_HTML && |
701 saved_success_items_.size() > 1) ? | 711 saved_success_items_.size() > 1) ? |
702 saved_main_directory_path_ : FilePath(); | 712 saved_main_directory_path_ : FilePath(); |
703 | 713 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
738 save_ids.push_back(it->second->save_id()); | 748 save_ids.push_back(it->second->save_id()); |
739 | 749 |
740 BrowserThread::PostTask( | 750 BrowserThread::PostTask( |
741 BrowserThread::FILE, FROM_HERE, | 751 BrowserThread::FILE, FROM_HERE, |
742 NewRunnableMethod(file_manager_, | 752 NewRunnableMethod(file_manager_, |
743 &SaveFileManager::RemoveSavedFileFromFileMap, | 753 &SaveFileManager::RemoveSavedFileFromFileMap, |
744 save_ids)); | 754 save_ids)); |
745 | 755 |
746 download_->OnAllDataSaved(all_save_items_count_); | 756 download_->OnAllDataSaved(all_save_items_count_); |
747 download_->MarkAsComplete(); | 757 download_->MarkAsComplete(); |
| 758 FinalizeDownloadEntry(); |
748 | 759 |
749 NotificationService::current()->Notify( | 760 NotificationService::current()->Notify( |
750 chrome::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | 761 chrome::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
751 Source<SavePackage>(this), | 762 Source<SavePackage>(this), |
752 Details<GURL>(&page_url_)); | 763 Details<GURL>(&page_url_)); |
753 } | 764 } |
754 | 765 |
755 // Called for updating end state. | 766 // Called for updating end state. |
756 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { | 767 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { |
757 // Because we might have canceled this saving job before, | 768 // Because we might have canceled this saving job before, |
(...skipping 709 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1467 } | 1478 } |
1468 | 1479 |
1469 // SelectFileDialog::Listener interface. | 1480 // SelectFileDialog::Listener interface. |
1470 void SavePackage::FileSelected(const FilePath& path, | 1481 void SavePackage::FileSelected(const FilePath& path, |
1471 int index, void* params) { | 1482 int index, void* params) { |
1472 ContinueSave(path, index); | 1483 ContinueSave(path, index); |
1473 } | 1484 } |
1474 | 1485 |
1475 void SavePackage::FileSelectionCanceled(void* params) { | 1486 void SavePackage::FileSelectionCanceled(void* params) { |
1476 } | 1487 } |
| 1488 |
| 1489 void SavePackage::ManagerGoingDown() { |
| 1490 download_ = NULL; |
| 1491 download_manager_ = NULL; |
| 1492 } |
| 1493 |
| 1494 void SavePackage::FinalizeDownloadEntry() { |
| 1495 DCHECK(download_manager_); |
| 1496 if (download_manager_) { |
| 1497 download_manager_->SavePageDownloadFinished(download_); |
| 1498 download_manager_->RemoveObserver(this); |
| 1499 download_manager_ = NULL; |
| 1500 } |
| 1501 } |
| 1502 |
OLD | NEW |