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/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "content/browser/download/download_item_impl.h" | 22 #include "content/browser/download/download_item_impl.h" |
23 #include "content/browser/download/download_manager_impl.h" | 23 #include "content/browser/download/download_manager_impl.h" |
24 #include "content/browser/download/download_stats.h" | 24 #include "content/browser/download/download_stats.h" |
25 #include "content/browser/download/save_file.h" | 25 #include "content/browser/download/save_file.h" |
26 #include "content/browser/download/save_file_manager.h" | 26 #include "content/browser/download/save_file_manager.h" |
27 #include "content/browser/download/save_item.h" | 27 #include "content/browser/download/save_item.h" |
28 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 28 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
29 #include "content/browser/renderer_host/render_process_host_impl.h" | 29 #include "content/browser/renderer_host/render_process_host_impl.h" |
30 #include "content/browser/renderer_host/render_view_host_delegate.h" | 30 #include "content/browser/renderer_host/render_view_host_delegate.h" |
31 #include "content/browser/renderer_host/render_view_host_impl.h" | 31 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 32 #include "content/common/frame_messages.h" |
32 #include "content/common/view_messages.h" | 33 #include "content/common/view_messages.h" |
33 #include "content/public/browser/browser_context.h" | 34 #include "content/public/browser/browser_context.h" |
34 #include "content/public/browser/browser_thread.h" | 35 #include "content/public/browser/browser_thread.h" |
35 #include "content/public/browser/content_browser_client.h" | 36 #include "content/public/browser/content_browser_client.h" |
36 #include "content/public/browser/download_manager_delegate.h" | 37 #include "content/public/browser/download_manager_delegate.h" |
37 #include "content/public/browser/navigation_entry.h" | 38 #include "content/public/browser/navigation_entry.h" |
38 #include "content/public/browser/notification_service.h" | 39 #include "content/public/browser/notification_service.h" |
39 #include "content/public/browser/notification_types.h" | 40 #include "content/public/browser/notification_types.h" |
40 #include "content/public/browser/render_frame_host.h" | 41 #include "content/public/browser/render_frame_host.h" |
41 #include "content/public/browser/resource_context.h" | 42 #include "content/public/browser/resource_context.h" |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 download_ = item; | 330 download_ = item; |
330 download_->AddObserver(this); | 331 download_->AddObserver(this); |
331 // Confirm above didn't delete the tab out from under us. | 332 // Confirm above didn't delete the tab out from under us. |
332 if (!download_created_callback.is_null()) | 333 if (!download_created_callback.is_null()) |
333 download_created_callback.Run(download_); | 334 download_created_callback.Run(download_); |
334 | 335 |
335 // Check save type and process the save page job. | 336 // Check save type and process the save page job. |
336 if (save_type_ == SAVE_PAGE_TYPE_AS_COMPLETE_HTML) { | 337 if (save_type_ == SAVE_PAGE_TYPE_AS_COMPLETE_HTML) { |
337 // Get directory | 338 // Get directory |
338 DCHECK(!saved_main_directory_path_.empty()); | 339 DCHECK(!saved_main_directory_path_.empty()); |
339 GetAllSavableResourceLinksForCurrentPage(); | 340 GetSavableResourceLinksForCurrentPage(); |
340 } else if (save_type_ == SAVE_PAGE_TYPE_AS_MHTML) { | 341 } else if (save_type_ == SAVE_PAGE_TYPE_AS_MHTML) { |
341 web_contents()->GenerateMHTML(saved_main_file_path_, base::Bind( | 342 web_contents()->GenerateMHTML(saved_main_file_path_, base::Bind( |
342 &SavePackage::OnMHTMLGenerated, this)); | 343 &SavePackage::OnMHTMLGenerated, this)); |
343 } else { | 344 } else { |
344 DCHECK_EQ(SAVE_PAGE_TYPE_AS_ONLY_HTML, save_type_) << save_type_; | 345 DCHECK_EQ(SAVE_PAGE_TYPE_AS_ONLY_HTML, save_type_) << save_type_; |
345 wait_state_ = NET_FILES; | 346 wait_state_ = NET_FILES; |
346 SaveFileCreateInfo::SaveFileSource save_source = page_url_.SchemeIsFile() ? | 347 SaveFileCreateInfo::SaveFileSource save_source = page_url_.SchemeIsFile() ? |
347 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : | 348 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : |
348 SaveFileCreateInfo::SAVE_FILE_FROM_NET; | 349 SaveFileCreateInfo::SAVE_FILE_FROM_NET; |
349 SaveItem* save_item = new SaveItem(page_url_, | 350 SaveItem* save_item = new SaveItem(page_url_, |
(...skipping 646 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
996 if (waiting_item_queue_.size()) { | 997 if (waiting_item_queue_.size()) { |
997 DCHECK(all_save_items_count_ == waiting_item_queue_.size()); | 998 DCHECK(all_save_items_count_ == waiting_item_queue_.size()); |
998 SaveNextFile(false); | 999 SaveNextFile(false); |
999 } | 1000 } |
1000 } | 1001 } |
1001 } | 1002 } |
1002 | 1003 |
1003 bool SavePackage::OnMessageReceived(const IPC::Message& message) { | 1004 bool SavePackage::OnMessageReceived(const IPC::Message& message) { |
1004 bool handled = true; | 1005 bool handled = true; |
1005 IPC_BEGIN_MESSAGE_MAP(SavePackage, message) | 1006 IPC_BEGIN_MESSAGE_MAP(SavePackage, message) |
1006 IPC_MESSAGE_HANDLER(ViewHostMsg_SendCurrentPageAllSavableResourceLinks, | |
1007 OnReceivedSavableResourceLinksForCurrentPage) | |
1008 IPC_MESSAGE_HANDLER(ViewHostMsg_SendSerializedHtmlData, | 1007 IPC_MESSAGE_HANDLER(ViewHostMsg_SendSerializedHtmlData, |
1009 OnReceivedSerializedHtmlData) | 1008 OnReceivedSerializedHtmlData) |
1010 IPC_MESSAGE_UNHANDLED(handled = false) | 1009 IPC_MESSAGE_UNHANDLED(handled = false) |
1011 IPC_END_MESSAGE_MAP() | 1010 IPC_END_MESSAGE_MAP() |
1012 return handled; | 1011 return handled; |
1013 } | 1012 } |
1014 | 1013 |
| 1014 bool SavePackage::OnMessageReceived(const IPC::Message& message, |
| 1015 RenderFrameHost* render_frame_host) { |
| 1016 bool handled = true; |
| 1017 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(SavePackage, message, render_frame_host) |
| 1018 IPC_MESSAGE_HANDLER(FrameHostMsg_SavableResourceLinksResponse, |
| 1019 OnSavableResourceLinksResponse) |
| 1020 IPC_MESSAGE_HANDLER(FrameHostMsg_SavableResourceLinksError, |
| 1021 OnSavableResourceLinksError) |
| 1022 IPC_MESSAGE_UNHANDLED(handled = false) |
| 1023 IPC_END_MESSAGE_MAP() |
| 1024 return handled; |
| 1025 } |
| 1026 |
1015 // After finishing all SaveItems which need to get data from net. | 1027 // After finishing all SaveItems which need to get data from net. |
1016 // We collect all URLs which have local storage and send the | 1028 // We collect all URLs which have local storage and send the |
1017 // map:(originalURL:currentLocalPath) to render process (backend). | 1029 // map:(originalURL:currentLocalPath) to render process (backend). |
1018 // Then render process will serialize DOM and send data to us. | 1030 // Then render process will serialize DOM and send data to us. |
1019 void SavePackage::GetSerializedHtmlDataForCurrentPageWithLocalLinks() { | 1031 void SavePackage::GetSerializedHtmlDataForCurrentPageWithLocalLinks() { |
1020 if (wait_state_ != HTML_DATA) | 1032 if (wait_state_ != HTML_DATA) |
1021 return; | 1033 return; |
1022 std::vector<GURL> saved_links; | 1034 std::vector<GURL> saved_links; |
1023 std::vector<base::FilePath> saved_file_paths; | 1035 std::vector<base::FilePath> saved_file_paths; |
1024 int successful_started_items_count = 0; | 1036 int successful_started_items_count = 0; |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1135 file_manager_, | 1147 file_manager_, |
1136 save_item->save_id(), | 1148 save_item->save_id(), |
1137 save_item->url(), | 1149 save_item->url(), |
1138 id, | 1150 id, |
1139 true)); | 1151 true)); |
1140 } | 1152 } |
1141 } | 1153 } |
1142 | 1154 |
1143 // Ask for all savable resource links from backend, include main frame and | 1155 // Ask for all savable resource links from backend, include main frame and |
1144 // sub-frame. | 1156 // sub-frame. |
1145 void SavePackage::GetAllSavableResourceLinksForCurrentPage() { | 1157 void SavePackage::GetSavableResourceLinksForCurrentPage() { |
1146 if (wait_state_ != START_PROCESS) | 1158 if (wait_state_ != START_PROCESS) |
1147 return; | 1159 return; |
1148 | 1160 |
| 1161 WebContents* web_contents = WebContentsObserver::web_contents(); |
| 1162 if (!web_contents) { |
| 1163 // Tab got closed - treat it as user cancel. |
| 1164 Cancel(true); |
| 1165 return; |
| 1166 } |
| 1167 |
1149 wait_state_ = RESOURCES_LIST; | 1168 wait_state_ = RESOURCES_LIST; |
1150 Send(new ViewMsg_GetAllSavableResourceLinksForCurrentPage(routing_id(), | 1169 number_of_frames_with_pending_get_savable_resource_links_ = 0; |
1151 page_url_)); | 1170 web_contents->ForEachFrame(base::Bind( |
| 1171 &SavePackage::GetSavableResourceLinksForFrame, |
| 1172 base::Unretained(this))); // Safe, because ForEachFrame is synchronous. |
| 1173 DCHECK_LT(0, number_of_frames_with_pending_get_savable_resource_links_); |
1152 } | 1174 } |
1153 | 1175 |
1154 // Give backend the lists which contain all resource links that have local | 1176 void SavePackage::GetSavableResourceLinksForFrame(RenderFrameHost* target) { |
1155 // storage, after which, render process will serialize DOM for generating | 1177 number_of_frames_with_pending_get_savable_resource_links_++; |
1156 // HTML data. | 1178 Send(new FrameMsg_GetSavableResourceLinks(target->GetRoutingID())); |
1157 void SavePackage::OnReceivedSavableResourceLinksForCurrentPage( | 1179 } |
| 1180 |
| 1181 void SavePackage::OnSavableResourceLinksResponse( |
| 1182 RenderFrameHost* source, |
1158 const std::vector<GURL>& resources_list, | 1183 const std::vector<GURL>& resources_list, |
1159 const std::vector<Referrer>& referrers_list, | 1184 const std::vector<Referrer>& referrers_list) { |
1160 const std::vector<GURL>& frames_list) { | |
1161 if (wait_state_ != RESOURCES_LIST) | 1185 if (wait_state_ != RESOURCES_LIST) |
1162 return; | 1186 return; |
1163 | 1187 |
1164 if (resources_list.size() != referrers_list.size()) | 1188 if (resources_list.size() != referrers_list.size()) |
1165 return; | 1189 return; |
1166 | 1190 |
1167 all_save_items_count_ = static_cast<int>(resources_list.size()) + | 1191 // Add all sub-resources to wait list. |
1168 static_cast<int>(frames_list.size()); | 1192 for (int i = 0; i < static_cast<int>(resources_list.size()); ++i) { |
| 1193 const GURL& u = resources_list[i]; |
| 1194 DCHECK(u.is_valid()); |
| 1195 if (unique_urls_to_save_.count(u)) { |
| 1196 continue; |
| 1197 } |
| 1198 unique_urls_to_save_.insert(u); |
| 1199 |
| 1200 SaveFileCreateInfo::SaveFileSource save_source = |
| 1201 u.SchemeIsFile() ? SaveFileCreateInfo::SAVE_FILE_FROM_FILE |
| 1202 : SaveFileCreateInfo::SAVE_FILE_FROM_NET; |
| 1203 SaveItem* save_item = new SaveItem(u, referrers_list[i], this, save_source); |
| 1204 waiting_item_queue_.push(save_item); |
| 1205 } |
| 1206 |
| 1207 // Add the frame to wait list. |
| 1208 GURL frame_url = source->GetLastCommittedURL(); |
| 1209 DCHECK(frame_url.is_valid()); |
| 1210 if (0 == unique_urls_to_save_.count(frame_url)) { |
| 1211 unique_urls_to_save_.insert(frame_url); |
| 1212 SaveItem* save_item = new SaveItem(frame_url, Referrer(), this, |
| 1213 SaveFileCreateInfo::SAVE_FILE_FROM_DOM); |
| 1214 waiting_item_queue_.push(save_item); |
| 1215 } |
| 1216 |
| 1217 CompleteSavableResourceLinksResponseFromFrame(); |
| 1218 } |
| 1219 |
| 1220 void SavePackage::OnSavableResourceLinksError(RenderFrameHost* source) { |
| 1221 CompleteSavableResourceLinksResponseFromFrame(); |
| 1222 } |
| 1223 |
| 1224 void SavePackage::CompleteSavableResourceLinksResponseFromFrame() { |
| 1225 --number_of_frames_with_pending_get_savable_resource_links_; |
| 1226 if (0 < number_of_frames_with_pending_get_savable_resource_links_) { |
| 1227 return; |
| 1228 } |
| 1229 DCHECK_EQ(0, number_of_frames_with_pending_get_savable_resource_links_); |
| 1230 |
| 1231 all_save_items_count_ = static_cast<int>(unique_urls_to_save_.size()); |
1169 | 1232 |
1170 // We use total bytes as the total number of files we want to save. | 1233 // We use total bytes as the total number of files we want to save. |
1171 // Hack to avoid touching download_ after user cancel. | 1234 // Hack to avoid touching download_ after user cancel. |
1172 // TODO(rdsmith/benjhayden): Integrate canceling on DownloadItem | 1235 // TODO(rdsmith/benjhayden): Integrate canceling on DownloadItem |
1173 // with SavePackage flow. | 1236 // with SavePackage flow. |
1174 if (download_ && (download_->GetState() == DownloadItem::IN_PROGRESS)) | 1237 if (download_ && (download_->GetState() == DownloadItem::IN_PROGRESS)) |
1175 download_->SetTotalBytes(all_save_items_count_); | 1238 download_->SetTotalBytes(all_save_items_count_); |
1176 | 1239 |
1177 if (all_save_items_count_) { | 1240 if (all_save_items_count_) { |
1178 // Put all sub-resources to wait list. | |
1179 for (int i = 0; i < static_cast<int>(resources_list.size()); ++i) { | |
1180 const GURL& u = resources_list[i]; | |
1181 DCHECK(u.is_valid()); | |
1182 SaveFileCreateInfo::SaveFileSource save_source = u.SchemeIsFile() ? | |
1183 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : | |
1184 SaveFileCreateInfo::SAVE_FILE_FROM_NET; | |
1185 SaveItem* save_item = new SaveItem(u, referrers_list[i], | |
1186 this, save_source); | |
1187 waiting_item_queue_.push(save_item); | |
1188 } | |
1189 // Put all HTML resources to wait list. | |
1190 for (int i = 0; i < static_cast<int>(frames_list.size()); ++i) { | |
1191 const GURL& u = frames_list[i]; | |
1192 DCHECK(u.is_valid()); | |
1193 SaveItem* save_item = new SaveItem( | |
1194 u, Referrer(), this, SaveFileCreateInfo::SAVE_FILE_FROM_DOM); | |
1195 waiting_item_queue_.push(save_item); | |
1196 } | |
1197 wait_state_ = NET_FILES; | 1241 wait_state_ = NET_FILES; |
| 1242 |
| 1243 // Give backend the lists which contain all resource links that have local |
| 1244 // storage, after which, render process will serialize DOM for generating |
| 1245 // HTML data. |
1198 DoSavingProcess(); | 1246 DoSavingProcess(); |
1199 } else { | 1247 } else { |
1200 // No resource files need to be saved, treat it as user cancel. | 1248 // No resource files need to be saved, treat it as user cancel. |
1201 Cancel(true); | 1249 Cancel(true); |
1202 } | 1250 } |
1203 } | 1251 } |
1204 | 1252 |
1205 base::FilePath SavePackage::GetSuggestedNameForSaveAs( | 1253 base::FilePath SavePackage::GetSuggestedNameForSaveAs( |
1206 bool can_save_as_complete, | 1254 bool can_save_as_complete, |
1207 const std::string& contents_mime_type, | 1255 const std::string& contents_mime_type, |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1437 | 1485 |
1438 void SavePackage::FinalizeDownloadEntry() { | 1486 void SavePackage::FinalizeDownloadEntry() { |
1439 DCHECK(download_); | 1487 DCHECK(download_); |
1440 DCHECK(download_manager_); | 1488 DCHECK(download_manager_); |
1441 | 1489 |
1442 download_manager_->OnSavePackageSuccessfullyFinished(download_); | 1490 download_manager_->OnSavePackageSuccessfullyFinished(download_); |
1443 StopObservation(); | 1491 StopObservation(); |
1444 } | 1492 } |
1445 | 1493 |
1446 } // namespace content | 1494 } // namespace content |
OLD | NEW |