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" |
11 #include "base/i18n/file_util_icu.h" | 11 #include "base/i18n/file_util_icu.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
14 #include "base/stl_util-inl.h" | 14 #include "base/stl_util-inl.h" |
15 #include "base/string_piece.h" | 15 #include "base/string_piece.h" |
16 #include "base/string_split.h" | 16 #include "base/string_split.h" |
17 #include "base/sys_string_conversions.h" | 17 #include "base/sys_string_conversions.h" |
18 #include "base/task.h" | 18 #include "base/task.h" |
19 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
20 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
21 #include "chrome/browser/browser_process.h" | 21 #include "chrome/browser/browser_process.h" |
22 #include "chrome/browser/download/download_item.h" | 22 #include "chrome/browser/download/download_item.h" |
23 #include "chrome/browser/download/download_item_model.h" | 23 #include "chrome/browser/download/download_item_model.h" |
24 #include "chrome/browser/download/download_history.h" | |
24 #include "chrome/browser/download/download_manager.h" | 25 #include "chrome/browser/download/download_manager.h" |
25 #include "chrome/browser/download/download_prefs.h" | 26 #include "chrome/browser/download/download_prefs.h" |
26 #include "chrome/browser/download/download_util.h" | 27 #include "chrome/browser/download/download_util.h" |
27 #include "chrome/browser/download/save_file.h" | 28 #include "chrome/browser/download/save_file.h" |
28 #include "chrome/browser/download/save_file_manager.h" | 29 #include "chrome/browser/download/save_file_manager.h" |
29 #include "chrome/browser/download/save_item.h" | 30 #include "chrome/browser/download/save_item.h" |
30 #include "chrome/browser/net/url_fixer_upper.h" | 31 #include "chrome/browser/net/url_fixer_upper.h" |
31 #include "chrome/browser/platform_util.h" | 32 #include "chrome/browser/platform_util.h" |
32 #include "chrome/browser/prefs/pref_member.h" | 33 #include "chrome/browser/prefs/pref_member.h" |
33 #include "chrome/browser/prefs/pref_service.h" | 34 #include "chrome/browser/prefs/pref_service.h" |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 saved_main_directory_path_(directory_full_path), | 167 saved_main_directory_path_(directory_full_path), |
167 title_(tab_contents()->GetTitle()), | 168 title_(tab_contents()->GetTitle()), |
168 finished_(false), | 169 finished_(false), |
169 user_canceled_(false), | 170 user_canceled_(false), |
170 disk_error_occurred_(false), | 171 disk_error_occurred_(false), |
171 save_type_(save_type), | 172 save_type_(save_type), |
172 all_save_items_count_(0), | 173 all_save_items_count_(0), |
173 wait_state_(INITIALIZE), | 174 wait_state_(INITIALIZE), |
174 tab_id_(tab_contents()->GetRenderProcessHost()->id()), | 175 tab_id_(tab_contents()->GetRenderProcessHost()->id()), |
175 unique_id_(g_save_package_id++), | 176 unique_id_(g_save_package_id++), |
177 download_manager_(tab_contents()->profile()->GetDownloadManager()), | |
176 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 178 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
177 DCHECK(page_url_.is_valid()); | 179 DCHECK(page_url_.is_valid()); |
178 DCHECK(save_type_ == SAVE_AS_ONLY_HTML || | 180 DCHECK(save_type_ == SAVE_AS_ONLY_HTML || |
179 save_type_ == SAVE_AS_COMPLETE_HTML); | 181 save_type_ == SAVE_AS_COMPLETE_HTML); |
180 DCHECK(!saved_main_file_path_.empty() && | 182 DCHECK(!saved_main_file_path_.empty() && |
181 saved_main_file_path_.value().length() <= kMaxFilePathLength); | 183 saved_main_file_path_.value().length() <= kMaxFilePathLength); |
182 DCHECK(!saved_main_directory_path_.empty() && | 184 DCHECK(!saved_main_directory_path_.empty() && |
183 saved_main_directory_path_.value().length() < kMaxFilePathLength); | 185 saved_main_directory_path_.value().length() < kMaxFilePathLength); |
184 InternalInit(); | 186 InternalInit(); |
185 } | 187 } |
186 | 188 |
187 SavePackage::SavePackage(TabContentsWrapper* wrapper) | 189 SavePackage::SavePackage(TabContentsWrapper* wrapper) |
188 : TabContentsObserver(wrapper->tab_contents()), | 190 : TabContentsObserver(wrapper->tab_contents()), |
189 wrapper_(wrapper), | 191 wrapper_(wrapper), |
190 file_manager_(NULL), | 192 file_manager_(NULL), |
191 download_(NULL), | 193 download_(NULL), |
192 page_url_(GetUrlToBeSaved()), | 194 page_url_(GetUrlToBeSaved()), |
193 title_(tab_contents()->GetTitle()), | 195 title_(tab_contents()->GetTitle()), |
194 finished_(false), | 196 finished_(false), |
195 user_canceled_(false), | 197 user_canceled_(false), |
196 disk_error_occurred_(false), | 198 disk_error_occurred_(false), |
197 save_type_(SAVE_TYPE_UNKNOWN), | 199 save_type_(SAVE_TYPE_UNKNOWN), |
198 all_save_items_count_(0), | 200 all_save_items_count_(0), |
199 wait_state_(INITIALIZE), | 201 wait_state_(INITIALIZE), |
200 tab_id_(tab_contents()->GetRenderProcessHost()->id()), | 202 tab_id_(tab_contents()->GetRenderProcessHost()->id()), |
201 unique_id_(g_save_package_id++), | 203 unique_id_(g_save_package_id++), |
204 download_manager_(tab_contents()->profile()->GetDownloadManager()), | |
202 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 205 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
203 DCHECK(page_url_.is_valid()); | 206 DCHECK(page_url_.is_valid()); |
204 InternalInit(); | 207 InternalInit(); |
205 } | 208 } |
206 | 209 |
207 // This is for testing use. Set |finished_| as true because we don't want | 210 // 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. | 211 // method Cancel to be be called in destructor in test mode. |
209 // We also don't call InternalInit(). | 212 // We also don't call InternalInit(). |
210 SavePackage::SavePackage(TabContentsWrapper* wrapper, | 213 SavePackage::SavePackage(TabContentsWrapper* wrapper, |
211 const FilePath& file_full_path, | 214 const FilePath& file_full_path, |
212 const FilePath& directory_full_path) | 215 const FilePath& directory_full_path) |
213 : TabContentsObserver(wrapper->tab_contents()), | 216 : TabContentsObserver(wrapper->tab_contents()), |
214 wrapper_(wrapper), | 217 wrapper_(wrapper), |
215 file_manager_(NULL), | 218 file_manager_(NULL), |
216 download_(NULL), | 219 download_(NULL), |
217 saved_main_file_path_(file_full_path), | 220 saved_main_file_path_(file_full_path), |
218 saved_main_directory_path_(directory_full_path), | 221 saved_main_directory_path_(directory_full_path), |
219 finished_(true), | 222 finished_(true), |
220 user_canceled_(false), | 223 user_canceled_(false), |
221 disk_error_occurred_(false), | 224 disk_error_occurred_(false), |
222 save_type_(SAVE_TYPE_UNKNOWN), | 225 save_type_(SAVE_TYPE_UNKNOWN), |
223 all_save_items_count_(0), | 226 all_save_items_count_(0), |
224 wait_state_(INITIALIZE), | 227 wait_state_(INITIALIZE), |
225 tab_id_(0), | 228 tab_id_(0), |
226 unique_id_(g_save_package_id++), | 229 unique_id_(g_save_package_id++), |
230 download_manager_(tab_contents()->profile()->GetDownloadManager()), | |
227 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 231 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
228 } | 232 } |
229 | 233 |
230 SavePackage::~SavePackage() { | 234 SavePackage::~SavePackage() { |
231 // Stop receiving saving job's updates | 235 // Stop receiving saving job's updates |
232 if (!finished_ && !canceled()) { | 236 if (!finished_ && !canceled()) { |
233 // Unexpected quit. | 237 // Unexpected quit. |
234 Cancel(true); | 238 Cancel(true); |
235 } | 239 } |
236 | 240 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
308 wait_state_ = START_PROCESS; | 312 wait_state_ = START_PROCESS; |
309 | 313 |
310 // Initialize the request context and resource dispatcher. | 314 // Initialize the request context and resource dispatcher. |
311 Profile* profile = tab_contents()->profile(); | 315 Profile* profile = tab_contents()->profile(); |
312 if (!profile) { | 316 if (!profile) { |
313 NOTREACHED(); | 317 NOTREACHED(); |
314 return false; | 318 return false; |
315 } | 319 } |
316 | 320 |
317 // Create the fake DownloadItem and display the view. | 321 // Create the fake DownloadItem and display the view. |
318 DownloadManager* download_manager = | 322 if (!download_manager_) { |
319 tab_contents()->profile()->GetDownloadManager(); | 323 NOTREACHED(); |
320 download_ = new DownloadItem(download_manager, | 324 return false; |
325 } | |
326 download_ = new DownloadItem(download_manager_, | |
321 saved_main_file_path_, | 327 saved_main_file_path_, |
322 page_url_, | 328 page_url_, |
323 profile->IsOffTheRecord()); | 329 profile->IsOffTheRecord()); |
324 | 330 |
325 // Transfer the ownership to the download manager. We need the DownloadItem | 331 // Transfer the ownership to the download manager. We need the DownloadItem |
326 // to be alive as long as the Profile is alive. | 332 // to be alive as long as the Profile is alive. |
327 download_manager->SavePageAsDownloadStarted(download_); | 333 download_manager_->SavePageAsDownloadStarted(download_); |
328 | 334 |
329 wrapper_->download_tab_helper()->OnStartDownload(download_); | 335 // Add this entry to the history service, which also notifies the UI. |
336 download_manager_->download_history()->AddEntry(download_, | |
337 NewCallback(this, &SavePackage::OnDownloadEntryAdded)); | |
Randy Smith (Not in Mondays)
2011/07/08 16:56:25
I'm concerned that there's a race here if SavePack
achuithb
2011/07/08 21:23:54
Done.
| |
330 | 338 |
331 // Check save type and process the save page job. | 339 // Check save type and process the save page job. |
332 if (save_type_ == SAVE_AS_COMPLETE_HTML) { | 340 if (save_type_ == SAVE_AS_COMPLETE_HTML) { |
333 // Get directory | 341 // Get directory |
334 DCHECK(!saved_main_directory_path_.empty()); | 342 DCHECK(!saved_main_directory_path_.empty()); |
335 GetAllSavableResourceLinksForCurrentPage(); | 343 GetAllSavableResourceLinksForCurrentPage(); |
336 } else { | 344 } else { |
337 wait_state_ = NET_FILES; | 345 wait_state_ = NET_FILES; |
338 SaveFileCreateInfo::SaveFileSource save_source = page_url_.SchemeIsFile() ? | 346 SaveFileCreateInfo::SaveFileSource save_source = page_url_.SchemeIsFile() ? |
339 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : | 347 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : |
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
684 BrowserThread::FILE, FROM_HERE, | 692 BrowserThread::FILE, FROM_HERE, |
685 NewRunnableMethod(file_manager_, | 693 NewRunnableMethod(file_manager_, |
686 &SaveFileManager::RemoveSavedFileFromFileMap, | 694 &SaveFileManager::RemoveSavedFileFromFileMap, |
687 save_ids)); | 695 save_ids)); |
688 | 696 |
689 finished_ = true; | 697 finished_ = true; |
690 wait_state_ = FAILED; | 698 wait_state_ = FAILED; |
691 | 699 |
692 // Inform the DownloadItem we have canceled whole save page job. | 700 // Inform the DownloadItem we have canceled whole save page job. |
693 download_->Cancel(false); | 701 download_->Cancel(false); |
702 if (download_manager_) | |
703 download_manager_->download_history()->UpdateEntry(download_); | |
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 if (download_manager_) | |
759 download_manager_->download_history()->UpdateEntry(download_); | |
748 | 760 |
749 NotificationService::current()->Notify( | 761 NotificationService::current()->Notify( |
750 NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | 762 NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
751 Source<SavePackage>(this), | 763 Source<SavePackage>(this), |
752 Details<GURL>(&page_url_)); | 764 Details<GURL>(&page_url_)); |
753 } | 765 } |
754 | 766 |
755 // Called for updating end state. | 767 // Called for updating end state. |
756 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { | 768 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { |
757 // Because we might have canceled this saving job before, | 769 // Because we might have canceled this saving job before, |
(...skipping 709 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1467 } | 1479 } |
1468 | 1480 |
1469 // SelectFileDialog::Listener interface. | 1481 // SelectFileDialog::Listener interface. |
1470 void SavePackage::FileSelected(const FilePath& path, | 1482 void SavePackage::FileSelected(const FilePath& path, |
1471 int index, void* params) { | 1483 int index, void* params) { |
1472 ContinueSave(path, index); | 1484 ContinueSave(path, index); |
1473 } | 1485 } |
1474 | 1486 |
1475 void SavePackage::FileSelectionCanceled(void* params) { | 1487 void SavePackage::FileSelectionCanceled(void* params) { |
1476 } | 1488 } |
1489 | |
1490 void SavePackage::ManagerGoingDown() { | |
Randy Smith (Not in Mondays)
2011/07/08 16:56:25
Where is the SavePackage registered as an observer
achuithb
2011/07/08 21:23:54
I left it out by mistake. I've added the calls to
| |
1491 download_manager_ = NULL; | |
Randy Smith (Not in Mondays)
2011/07/08 16:56:25
I believe that it isn't possible for the download
achuithb
2011/07/08 21:23:54
Makes sense. DCHECKs added. Done.
| |
1492 } | |
1493 | |
1494 void SavePackage::OnDownloadEntryAdded(int32 download_id, int64 db_handle) { | |
1495 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
1496 if (download_manager_) | |
1497 download_manager_->AddDownloadItemToHistory(download_, db_handle); | |
1498 } | |
1499 | |
OLD | NEW |