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/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.h" | 14 #include "base/stl_util.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/download/download_history.h" |
21 #include "chrome/browser/download/download_item.h" | 22 #include "chrome/browser/download/download_item.h" |
22 #include "chrome/browser/download/download_item_model.h" | 23 #include "chrome/browser/download/download_item_model.h" |
23 #include "chrome/browser/download/download_manager.h" | 24 #include "chrome/browser/download/download_manager.h" |
24 #include "chrome/browser/download/download_manager_delegate.h" | 25 #include "chrome/browser/download/download_manager_delegate.h" |
25 #include "chrome/browser/download/download_util.h" | 26 #include "chrome/browser/download/download_util.h" |
26 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
27 #include "content/browser/browser_thread.h" | 28 #include "content/browser/browser_thread.h" |
28 #include "content/browser/content_browser_client.h" | 29 #include "content/browser/content_browser_client.h" |
29 #include "content/browser/download/save_file.h" | 30 #include "content/browser/download/save_file.h" |
30 #include "content/browser/download/save_file_manager.h" | 31 #include "content/browser/download/save_file_manager.h" |
31 #include "content/browser/download/save_item.h" | 32 #include "content/browser/download/save_item.h" |
32 #include "content/browser/renderer_host/render_process_host.h" | 33 #include "content/browser/renderer_host/render_process_host.h" |
33 #include "content/browser/renderer_host/render_view_host.h" | 34 #include "content/browser/renderer_host/render_view_host.h" |
34 #include "content/browser/renderer_host/render_view_host_delegate.h" | 35 #include "content/browser/renderer_host/render_view_host_delegate.h" |
35 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 36 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
36 #include "content/browser/tab_contents/tab_contents.h" | 37 #include "content/browser/tab_contents/tab_contents.h" |
37 #include "content/common/content_notification_types.h" | |
38 #include "content/common/notification_service.h" | |
39 #include "content/common/url_constants.h" | 38 #include "content/common/url_constants.h" |
40 #include "content/common/view_messages.h" | 39 #include "content/common/view_messages.h" |
41 #include "net/base/io_buffer.h" | 40 #include "net/base/io_buffer.h" |
42 #include "net/base/mime_util.h" | 41 #include "net/base/mime_util.h" |
43 #include "net/base/net_util.h" | 42 #include "net/base/net_util.h" |
44 #include "net/url_request/url_request_context.h" | 43 #include "net/url_request/url_request_context.h" |
45 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPageSerializerClie
nt.h" | 44 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPageSerializerClie
nt.h" |
46 | 45 |
47 using base::Time; | 46 using base::Time; |
48 using WebKit::WebPageSerializerClient; | 47 using WebKit::WebPageSerializerClient; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 #else | 110 #else |
112 FILE_PATH_LITERAL("html"); | 111 FILE_PATH_LITERAL("html"); |
113 #endif | 112 #endif |
114 | 113 |
115 SavePackage::SavePackage(TabContents* tab_contents, | 114 SavePackage::SavePackage(TabContents* tab_contents, |
116 SavePackageType save_type, | 115 SavePackageType save_type, |
117 const FilePath& file_full_path, | 116 const FilePath& file_full_path, |
118 const FilePath& directory_full_path) | 117 const FilePath& directory_full_path) |
119 : TabContentsObserver(tab_contents), | 118 : TabContentsObserver(tab_contents), |
120 file_manager_(NULL), | 119 file_manager_(NULL), |
| 120 download_manager_(NULL), |
121 download_(NULL), | 121 download_(NULL), |
122 page_url_(GetUrlToBeSaved()), | 122 page_url_(GetUrlToBeSaved()), |
123 saved_main_file_path_(file_full_path), | 123 saved_main_file_path_(file_full_path), |
124 saved_main_directory_path_(directory_full_path), | 124 saved_main_directory_path_(directory_full_path), |
125 title_(tab_contents->GetTitle()), | 125 title_(tab_contents->GetTitle()), |
126 finished_(false), | 126 finished_(false), |
127 user_canceled_(false), | 127 user_canceled_(false), |
128 disk_error_occurred_(false), | 128 disk_error_occurred_(false), |
129 save_type_(save_type), | 129 save_type_(save_type), |
130 all_save_items_count_(0), | 130 all_save_items_count_(0), |
131 wait_state_(INITIALIZE), | 131 wait_state_(INITIALIZE), |
132 tab_id_(tab_contents->GetRenderProcessHost()->id()), | 132 tab_id_(tab_contents->GetRenderProcessHost()->id()), |
133 unique_id_(g_save_package_id++), | 133 unique_id_(g_save_package_id++), |
134 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 134 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
135 DCHECK(page_url_.is_valid()); | 135 DCHECK(page_url_.is_valid()); |
136 DCHECK(save_type_ == SAVE_AS_ONLY_HTML || | 136 DCHECK(save_type_ == SAVE_AS_ONLY_HTML || |
137 save_type_ == SAVE_AS_COMPLETE_HTML); | 137 save_type_ == SAVE_AS_COMPLETE_HTML); |
138 DCHECK(!saved_main_file_path_.empty() && | 138 DCHECK(!saved_main_file_path_.empty() && |
139 saved_main_file_path_.value().length() <= kMaxFilePathLength); | 139 saved_main_file_path_.value().length() <= kMaxFilePathLength); |
140 DCHECK(!saved_main_directory_path_.empty() && | 140 DCHECK(!saved_main_directory_path_.empty() && |
141 saved_main_directory_path_.value().length() < kMaxFilePathLength); | 141 saved_main_directory_path_.value().length() < kMaxFilePathLength); |
142 InternalInit(); | 142 InternalInit(); |
143 } | 143 } |
144 | 144 |
145 SavePackage::SavePackage(TabContents* tab_contents) | 145 SavePackage::SavePackage(TabContents* tab_contents) |
146 : TabContentsObserver(tab_contents), | 146 : TabContentsObserver(tab_contents), |
147 file_manager_(NULL), | 147 file_manager_(NULL), |
| 148 download_manager_(NULL), |
148 download_(NULL), | 149 download_(NULL), |
149 page_url_(GetUrlToBeSaved()), | 150 page_url_(GetUrlToBeSaved()), |
150 title_(tab_contents->GetTitle()), | 151 title_(tab_contents->GetTitle()), |
151 finished_(false), | 152 finished_(false), |
152 user_canceled_(false), | 153 user_canceled_(false), |
153 disk_error_occurred_(false), | 154 disk_error_occurred_(false), |
154 save_type_(SAVE_TYPE_UNKNOWN), | 155 save_type_(SAVE_TYPE_UNKNOWN), |
155 all_save_items_count_(0), | 156 all_save_items_count_(0), |
156 wait_state_(INITIALIZE), | 157 wait_state_(INITIALIZE), |
157 tab_id_(tab_contents->GetRenderProcessHost()->id()), | 158 tab_id_(tab_contents->GetRenderProcessHost()->id()), |
158 unique_id_(g_save_package_id++), | 159 unique_id_(g_save_package_id++), |
159 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 160 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
160 DCHECK(page_url_.is_valid()); | 161 DCHECK(page_url_.is_valid()); |
161 InternalInit(); | 162 InternalInit(); |
162 } | 163 } |
163 | 164 |
164 // This is for testing use. Set |finished_| as true because we don't want | 165 // This is for testing use. Set |finished_| as true because we don't want |
165 // method Cancel to be be called in destructor in test mode. | 166 // method Cancel to be be called in destructor in test mode. |
166 // We also don't call InternalInit(). | 167 // We also don't call InternalInit(). |
167 SavePackage::SavePackage(TabContents* tab_contents, | 168 SavePackage::SavePackage(TabContents* tab_contents, |
168 const FilePath& file_full_path, | 169 const FilePath& file_full_path, |
169 const FilePath& directory_full_path) | 170 const FilePath& directory_full_path) |
170 : TabContentsObserver(tab_contents), | 171 : TabContentsObserver(tab_contents), |
171 file_manager_(NULL), | 172 file_manager_(NULL), |
| 173 download_manager_(NULL), |
172 download_(NULL), | 174 download_(NULL), |
173 saved_main_file_path_(file_full_path), | 175 saved_main_file_path_(file_full_path), |
174 saved_main_directory_path_(directory_full_path), | 176 saved_main_directory_path_(directory_full_path), |
175 finished_(true), | 177 finished_(true), |
176 user_canceled_(false), | 178 user_canceled_(false), |
177 disk_error_occurred_(false), | 179 disk_error_occurred_(false), |
178 save_type_(SAVE_TYPE_UNKNOWN), | 180 save_type_(SAVE_TYPE_UNKNOWN), |
179 all_save_items_count_(0), | 181 all_save_items_count_(0), |
180 wait_state_(INITIALIZE), | 182 wait_state_(INITIALIZE), |
181 tab_id_(0), | 183 tab_id_(0), |
182 unique_id_(g_save_package_id++), | 184 unique_id_(g_save_package_id++), |
183 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 185 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
184 } | 186 } |
185 | 187 |
186 SavePackage::~SavePackage() { | 188 SavePackage::~SavePackage() { |
187 // Stop receiving saving job's updates | 189 // Stop receiving saving job's updates |
188 if (!finished_ && !canceled()) { | 190 if (!finished_ && !canceled()) { |
189 // Unexpected quit. | 191 // Unexpected quit. |
190 Cancel(true); | 192 Cancel(true); |
191 } | 193 } |
192 | 194 |
| 195 // We should no longer be observing the DownloadManager at this point. |
| 196 CHECK(!download_manager_); |
| 197 CHECK(!download_); |
| 198 |
193 DCHECK(all_save_items_count_ == (waiting_item_queue_.size() + | 199 DCHECK(all_save_items_count_ == (waiting_item_queue_.size() + |
194 completed_count() + | 200 completed_count() + |
195 in_process_count())); | 201 in_process_count())); |
196 // Free all SaveItems. | 202 // Free all SaveItems. |
197 while (!waiting_item_queue_.empty()) { | 203 while (!waiting_item_queue_.empty()) { |
198 // We still have some items which are waiting for start to save. | 204 // We still have some items which are waiting for start to save. |
199 SaveItem* save_item = waiting_item_queue_.front(); | 205 SaveItem* save_item = waiting_item_queue_.front(); |
200 waiting_item_queue_.pop(); | 206 waiting_item_queue_.pop(); |
201 delete save_item; | 207 delete save_item; |
202 } | 208 } |
203 | 209 |
204 STLDeleteValues(&saved_success_items_); | 210 STLDeleteValues(&saved_success_items_); |
205 STLDeleteValues(&in_progress_items_); | 211 STLDeleteValues(&in_progress_items_); |
206 STLDeleteValues(&saved_failed_items_); | 212 STLDeleteValues(&saved_failed_items_); |
207 | 213 |
208 // The DownloadItem is owned by DownloadManager. | |
209 download_ = NULL; | |
210 | |
211 file_manager_ = NULL; | 214 file_manager_ = NULL; |
212 } | 215 } |
213 | 216 |
214 GURL SavePackage::GetUrlToBeSaved() { | 217 GURL SavePackage::GetUrlToBeSaved() { |
215 // Instead of using tab_contents_.GetURL here, we use url() | 218 // Instead of using tab_contents_.GetURL here, we use url() |
216 // (which is the "real" url of the page) | 219 // (which is the "real" url of the page) |
217 // from the NavigationEntry because it reflects its' origin | 220 // from the NavigationEntry because it reflects its' origin |
218 // rather than the displayed one (returned by GetURL) which may be | 221 // rather than the displayed one (returned by GetURL) which may be |
219 // different (like having "view-source:" on the front). | 222 // different (like having "view-source:" on the front). |
220 NavigationEntry* active_entry = | 223 NavigationEntry* active_entry = |
(...skipping 15 matching lines...) Expand all Loading... |
236 // cases, we need file_manager_ to be initialized, so we do this first. | 239 // cases, we need file_manager_ to be initialized, so we do this first. |
237 void SavePackage::InternalInit() { | 240 void SavePackage::InternalInit() { |
238 ResourceDispatcherHost* rdh = | 241 ResourceDispatcherHost* rdh = |
239 content::GetContentClient()->browser()->GetResourceDispatcherHost(); | 242 content::GetContentClient()->browser()->GetResourceDispatcherHost(); |
240 if (!rdh) { | 243 if (!rdh) { |
241 NOTREACHED(); | 244 NOTREACHED(); |
242 return; | 245 return; |
243 } | 246 } |
244 | 247 |
245 file_manager_ = rdh->save_file_manager(); | 248 file_manager_ = rdh->save_file_manager(); |
246 if (!file_manager_) { | 249 DCHECK(file_manager_); |
247 NOTREACHED(); | 250 |
248 return; | 251 download_manager_ = tab_contents()->browser_context()->GetDownloadManager(); |
249 } | 252 DCHECK(download_manager_); |
250 } | 253 } |
251 | 254 |
252 bool SavePackage::Init() { | 255 bool SavePackage::Init() { |
253 // Set proper running state. | 256 // Set proper running state. |
254 if (wait_state_ != INITIALIZE) | 257 if (wait_state_ != INITIALIZE) |
255 return false; | 258 return false; |
256 | 259 |
257 wait_state_ = START_PROCESS; | 260 wait_state_ = START_PROCESS; |
258 | 261 |
259 // Initialize the request context and resource dispatcher. | 262 // Initialize the request context and resource dispatcher. |
260 content::BrowserContext* browser_context = tab_contents()->browser_context(); | 263 content::BrowserContext* browser_context = tab_contents()->browser_context(); |
261 if (!browser_context) { | 264 if (!browser_context) { |
262 NOTREACHED(); | 265 NOTREACHED(); |
263 return false; | 266 return false; |
264 } | 267 } |
265 | 268 |
266 // Create the fake DownloadItem and display the view. | 269 DCHECK(download_manager_); |
267 download_ = new DownloadItem(GetDownloadManager(), | 270 // Create the download item, and add ourself as an observer. |
| 271 download_ = new DownloadItem(download_manager_, |
268 saved_main_file_path_, | 272 saved_main_file_path_, |
269 page_url_, | 273 page_url_, |
270 browser_context->IsOffTheRecord()); | 274 browser_context->IsOffTheRecord()); |
| 275 download_->AddObserver(this); |
271 | 276 |
272 // Transfer the ownership to the download manager. We need the DownloadItem | 277 // Transfer ownership to the download manager. |
273 // to be alive as long as the Profile is alive. | 278 download_manager_->SavePageDownloadStarted(download_); |
274 GetDownloadManager()->SavePageAsDownloadStarted(download_); | |
275 | |
276 tab_contents()->OnStartDownload(download_); | |
277 | 279 |
278 // Check save type and process the save page job. | 280 // Check save type and process the save page job. |
279 if (save_type_ == SAVE_AS_COMPLETE_HTML) { | 281 if (save_type_ == SAVE_AS_COMPLETE_HTML) { |
280 // Get directory | 282 // Get directory |
281 DCHECK(!saved_main_directory_path_.empty()); | 283 DCHECK(!saved_main_directory_path_.empty()); |
282 GetAllSavableResourceLinksForCurrentPage(); | 284 GetAllSavableResourceLinksForCurrentPage(); |
283 } else { | 285 } else { |
284 wait_state_ = NET_FILES; | 286 wait_state_ = NET_FILES; |
285 SaveFileCreateInfo::SaveFileSource save_source = page_url_.SchemeIsFile() ? | 287 SaveFileCreateInfo::SaveFileSource save_source = page_url_.SchemeIsFile() ? |
286 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : | 288 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : |
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
628 BrowserThread::PostTask( | 630 BrowserThread::PostTask( |
629 BrowserThread::FILE, FROM_HERE, | 631 BrowserThread::FILE, FROM_HERE, |
630 NewRunnableMethod(file_manager_, | 632 NewRunnableMethod(file_manager_, |
631 &SaveFileManager::RemoveSavedFileFromFileMap, | 633 &SaveFileManager::RemoveSavedFileFromFileMap, |
632 save_ids)); | 634 save_ids)); |
633 | 635 |
634 finished_ = true; | 636 finished_ = true; |
635 wait_state_ = FAILED; | 637 wait_state_ = FAILED; |
636 | 638 |
637 // Inform the DownloadItem we have canceled whole save page job. | 639 // Inform the DownloadItem we have canceled whole save page job. |
638 download_->Cancel(false); | 640 if (download_) { |
| 641 download_->Cancel(false); |
| 642 FinalizeDownloadEntry(); |
| 643 } |
639 } | 644 } |
640 | 645 |
641 void SavePackage::CheckFinish() { | 646 void SavePackage::CheckFinish() { |
642 if (in_process_count() || finished_) | 647 if (in_process_count() || finished_) |
643 return; | 648 return; |
644 | 649 |
645 FilePath dir = (save_type_ == SAVE_AS_COMPLETE_HTML && | 650 FilePath dir = (save_type_ == SAVE_AS_COMPLETE_HTML && |
646 saved_success_items_.size() > 1) ? | 651 saved_success_items_.size() > 1) ? |
647 saved_main_directory_path_ : FilePath(); | 652 saved_main_directory_path_ : FilePath(); |
648 | 653 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
681 for (SaveUrlItemMap::iterator it = saved_failed_items_.begin(); | 686 for (SaveUrlItemMap::iterator it = saved_failed_items_.begin(); |
682 it != saved_failed_items_.end(); ++it) | 687 it != saved_failed_items_.end(); ++it) |
683 save_ids.push_back(it->second->save_id()); | 688 save_ids.push_back(it->second->save_id()); |
684 | 689 |
685 BrowserThread::PostTask( | 690 BrowserThread::PostTask( |
686 BrowserThread::FILE, FROM_HERE, | 691 BrowserThread::FILE, FROM_HERE, |
687 NewRunnableMethod(file_manager_, | 692 NewRunnableMethod(file_manager_, |
688 &SaveFileManager::RemoveSavedFileFromFileMap, | 693 &SaveFileManager::RemoveSavedFileFromFileMap, |
689 save_ids)); | 694 save_ids)); |
690 | 695 |
691 download_->OnAllDataSaved(all_save_items_count_); | 696 if (download_) { |
692 download_->MarkAsComplete(); | 697 download_->OnAllDataSaved(all_save_items_count_); |
693 | 698 download_->MarkAsComplete(); |
694 NotificationService::current()->Notify( | 699 FinalizeDownloadEntry(); |
695 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | 700 } |
696 Source<SavePackage>(this), | |
697 Details<GURL>(&page_url_)); | |
698 } | 701 } |
699 | 702 |
700 // Called for updating end state. | 703 // Called for updating end state. |
701 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { | 704 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { |
702 // Because we might have canceled this saving job before, | 705 // Because we might have canceled this saving job before, |
703 // so we might not find corresponding SaveItem. Just ignore it. | 706 // so we might not find corresponding SaveItem. Just ignore it. |
704 SaveItem* save_item = LookupItemInProcessBySaveId(save_id); | 707 SaveItem* save_item = LookupItemInProcessBySaveId(save_id); |
705 if (!save_item) | 708 if (!save_item) |
706 return; | 709 return; |
707 | 710 |
708 // Let SaveItem set end state. | 711 // Let SaveItem set end state. |
709 save_item->Finish(size, is_success); | 712 save_item->Finish(size, is_success); |
710 // Remove the associated save id and SavePackage. | 713 // Remove the associated save id and SavePackage. |
711 file_manager_->RemoveSaveFile(save_id, save_item->url(), this); | 714 file_manager_->RemoveSaveFile(save_id, save_item->url(), this); |
712 | 715 |
713 PutInProgressItemToSavedMap(save_item); | 716 PutInProgressItemToSavedMap(save_item); |
714 | 717 |
715 // Inform the DownloadItem to update UI. | 718 // Inform the DownloadItem to update UI. |
716 // We use the received bytes as number of saved files. | 719 // We use the received bytes as number of saved files. |
717 download_->Update(completed_count()); | 720 if (download_) |
| 721 download_->Update(completed_count()); |
718 | 722 |
719 if (save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM && | 723 if (save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM && |
720 save_item->url() == page_url_ && !save_item->received_bytes()) { | 724 save_item->url() == page_url_ && !save_item->received_bytes()) { |
721 // If size of main HTML page is 0, treat it as disk error. | 725 // If size of main HTML page is 0, treat it as disk error. |
722 Cancel(false); | 726 Cancel(false); |
723 return; | 727 return; |
724 } | 728 } |
725 | 729 |
726 if (canceled()) { | 730 if (canceled()) { |
727 DCHECK(finished_); | 731 DCHECK(finished_); |
(...skipping 20 matching lines...) Expand all Loading... |
748 return; | 752 return; |
749 } | 753 } |
750 SaveItem* save_item = it->second; | 754 SaveItem* save_item = it->second; |
751 | 755 |
752 save_item->Finish(0, false); | 756 save_item->Finish(0, false); |
753 | 757 |
754 PutInProgressItemToSavedMap(save_item); | 758 PutInProgressItemToSavedMap(save_item); |
755 | 759 |
756 // Inform the DownloadItem to update UI. | 760 // Inform the DownloadItem to update UI. |
757 // We use the received bytes as number of saved files. | 761 // We use the received bytes as number of saved files. |
758 download_->Update(completed_count()); | 762 if (download_) |
| 763 download_->Update(completed_count()); |
759 | 764 |
760 if (save_type_ == SAVE_AS_ONLY_HTML || | 765 if (save_type_ == SAVE_AS_ONLY_HTML || |
761 save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM) { | 766 save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM) { |
762 // We got error when saving page. Treat it as disk error. | 767 // We got error when saving page. Treat it as disk error. |
763 Cancel(true); | 768 Cancel(true); |
764 } | 769 } |
765 | 770 |
766 if (canceled()) { | 771 if (canceled()) { |
767 DCHECK(finished_); | 772 DCHECK(finished_); |
768 return; | 773 return; |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1013 const std::vector<GURL>& referrers_list, | 1018 const std::vector<GURL>& referrers_list, |
1014 const std::vector<GURL>& frames_list) { | 1019 const std::vector<GURL>& frames_list) { |
1015 if (wait_state_ != RESOURCES_LIST) | 1020 if (wait_state_ != RESOURCES_LIST) |
1016 return; | 1021 return; |
1017 | 1022 |
1018 DCHECK(resources_list.size() == referrers_list.size()); | 1023 DCHECK(resources_list.size() == referrers_list.size()); |
1019 all_save_items_count_ = static_cast<int>(resources_list.size()) + | 1024 all_save_items_count_ = static_cast<int>(resources_list.size()) + |
1020 static_cast<int>(frames_list.size()); | 1025 static_cast<int>(frames_list.size()); |
1021 | 1026 |
1022 // We use total bytes as the total number of files we want to save. | 1027 // We use total bytes as the total number of files we want to save. |
1023 download_->set_total_bytes(all_save_items_count_); | 1028 if (download_) |
| 1029 download_->set_total_bytes(all_save_items_count_); |
1024 | 1030 |
1025 if (all_save_items_count_) { | 1031 if (all_save_items_count_) { |
1026 // Put all sub-resources to wait list. | 1032 // Put all sub-resources to wait list. |
1027 for (int i = 0; i < static_cast<int>(resources_list.size()); ++i) { | 1033 for (int i = 0; i < static_cast<int>(resources_list.size()); ++i) { |
1028 const GURL& u = resources_list[i]; | 1034 const GURL& u = resources_list[i]; |
1029 DCHECK(u.is_valid()); | 1035 DCHECK(u.is_valid()); |
1030 SaveFileCreateInfo::SaveFileSource save_source = u.SchemeIsFile() ? | 1036 SaveFileCreateInfo::SaveFileSource save_source = u.SchemeIsFile() ? |
1031 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : | 1037 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : |
1032 SaveFileCreateInfo::SAVE_FILE_FROM_NET; | 1038 SaveFileCreateInfo::SAVE_FILE_FROM_NET; |
1033 SaveItem* save_item = new SaveItem(u, referrers_list[i], | 1039 SaveItem* save_item = new SaveItem(u, referrers_list[i], |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1148 if (mime_type == extensions[i].mime_type) | 1154 if (mime_type == extensions[i].mime_type) |
1149 return extensions[i].suggested_extension; | 1155 return extensions[i].suggested_extension; |
1150 } | 1156 } |
1151 return FILE_PATH_LITERAL(""); | 1157 return FILE_PATH_LITERAL(""); |
1152 } | 1158 } |
1153 | 1159 |
1154 void SavePackage::GetSaveInfo() { | 1160 void SavePackage::GetSaveInfo() { |
1155 // Can't use tab_contents_ in the file thread, so get the data that we need | 1161 // Can't use tab_contents_ in the file thread, so get the data that we need |
1156 // before calling to it. | 1162 // before calling to it. |
1157 FilePath website_save_dir, download_save_dir; | 1163 FilePath website_save_dir, download_save_dir; |
1158 GetDownloadManager()->delegate()->GetSaveDir( | 1164 DCHECK(download_manager_); |
| 1165 download_manager_->delegate()->GetSaveDir( |
1159 tab_contents(), &website_save_dir, &download_save_dir); | 1166 tab_contents(), &website_save_dir, &download_save_dir); |
1160 std::string mime_type = tab_contents()->contents_mime_type(); | 1167 std::string mime_type = tab_contents()->contents_mime_type(); |
1161 std::string accept_languages = | 1168 std::string accept_languages = |
1162 content::GetContentClient()->browser()->GetAcceptLangs(tab_contents()); | 1169 content::GetContentClient()->browser()->GetAcceptLangs(tab_contents()); |
1163 | 1170 |
1164 BrowserThread::PostTask( | 1171 BrowserThread::PostTask( |
1165 BrowserThread::FILE, FROM_HERE, | 1172 BrowserThread::FILE, FROM_HERE, |
1166 NewRunnableMethod(this, &SavePackage::CreateDirectoryOnFileThread, | 1173 NewRunnableMethod(this, &SavePackage::CreateDirectoryOnFileThread, |
1167 website_save_dir, download_save_dir, mime_type, accept_languages)); | 1174 website_save_dir, download_save_dir, mime_type, accept_languages)); |
1168 } | 1175 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1211 } | 1218 } |
1212 | 1219 |
1213 void SavePackage::ContinueGetSaveInfo(const FilePath& suggested_path, | 1220 void SavePackage::ContinueGetSaveInfo(const FilePath& suggested_path, |
1214 bool can_save_as_complete) { | 1221 bool can_save_as_complete) { |
1215 // The TabContents which owns this SavePackage may have disappeared during | 1222 // The TabContents which owns this SavePackage may have disappeared during |
1216 // the UI->FILE->UI thread hop of | 1223 // the UI->FILE->UI thread hop of |
1217 // GetSaveInfo->CreateDirectoryOnFileThread->ContinueGetSaveInfo. | 1224 // GetSaveInfo->CreateDirectoryOnFileThread->ContinueGetSaveInfo. |
1218 if (!tab_contents()) | 1225 if (!tab_contents()) |
1219 return; | 1226 return; |
1220 | 1227 |
1221 GetDownloadManager()->delegate()->ChooseSavePath( | 1228 download_manager_->delegate()->ChooseSavePath( |
1222 AsWeakPtr(), suggested_path, can_save_as_complete); | 1229 AsWeakPtr(), suggested_path, can_save_as_complete); |
1223 } | 1230 } |
1224 | 1231 |
1225 // Called after the save file dialog box returns. | 1232 // Called after the save file dialog box returns. |
1226 void SavePackage::OnPathPicked(const FilePath& final_name, | 1233 void SavePackage::OnPathPicked(const FilePath& final_name, |
1227 SavePackageType type) { | 1234 SavePackageType type) { |
1228 // Ensure the filename is safe. | 1235 // Ensure the filename is safe. |
1229 saved_main_file_path_ = final_name; | 1236 saved_main_file_path_ = final_name; |
1230 download_util::GenerateSafeFileName(tab_contents()->contents_mime_type(), | 1237 download_util::GenerateSafeFileName(tab_contents()->contents_mime_type(), |
1231 &saved_main_file_path_); | 1238 &saved_main_file_path_); |
(...skipping 25 matching lines...) Expand all Loading... |
1257 // WebKit creates Document object when MIME type is application/xhtml+xml, | 1264 // WebKit creates Document object when MIME type is application/xhtml+xml, |
1258 // so we also support this MIME type. | 1265 // so we also support this MIME type. |
1259 return contents_mime_type == "text/html" || | 1266 return contents_mime_type == "text/html" || |
1260 contents_mime_type == "text/xml" || | 1267 contents_mime_type == "text/xml" || |
1261 contents_mime_type == "application/xhtml+xml" || | 1268 contents_mime_type == "application/xhtml+xml" || |
1262 contents_mime_type == "text/plain" || | 1269 contents_mime_type == "text/plain" || |
1263 contents_mime_type == "text/css" || | 1270 contents_mime_type == "text/css" || |
1264 net::IsSupportedJavascriptMimeType(contents_mime_type.c_str()); | 1271 net::IsSupportedJavascriptMimeType(contents_mime_type.c_str()); |
1265 } | 1272 } |
1266 | 1273 |
1267 DownloadManager* SavePackage::GetDownloadManager() { | 1274 void SavePackage::StopObservation() { |
1268 return tab_contents()->browser_context()->GetDownloadManager();; | 1275 DCHECK(download_); |
| 1276 DCHECK(download_manager_); |
| 1277 |
| 1278 download_->RemoveObserver(this); |
| 1279 download_ = NULL; |
| 1280 download_manager_ = NULL; |
1269 } | 1281 } |
| 1282 |
| 1283 void SavePackage::OnDownloadUpdated(DownloadItem* download) { |
| 1284 DCHECK(download_); |
| 1285 DCHECK(download_ == download); |
| 1286 DCHECK(download_manager_); |
| 1287 |
| 1288 // Check for removal. |
| 1289 if (download->state() == DownloadItem::REMOVING) |
| 1290 StopObservation(); |
| 1291 } |
| 1292 |
| 1293 void SavePackage::FinalizeDownloadEntry() { |
| 1294 DCHECK(download_); |
| 1295 DCHECK(download_manager_); |
| 1296 |
| 1297 download_manager_->SavePageDownloadFinished(download_); |
| 1298 StopObservation(); |
| 1299 } |
OLD | NEW |