Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: content/browser/download/save_package.cc

Issue 1308113008: OOPIFs: Transitioning Get/Send...SavableResourceLinks away from RenderViewHost. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@page-serialization-test
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/download/save_package.h ('k') | content/common/frame_messages.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
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 OnReceivedSavableResourceLinksForFrame)
1020 IPC_MESSAGE_HANDLER(FrameHostMsg_NonSavableResponse,
1021 OnReceivedNonSavableFrameIndication)
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
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, base::Unretained(this)));
1172
1173 if (0 == number_of_frames_with_pending_get_savable_resource_links_) {
1174 // No frames in the web contents - treat it as user cancel.
1175 Cancel(true);
1176 return;
1177 }
1152 } 1178 }
1153 1179
1154 // Give backend the lists which contain all resource links that have local 1180 void SavePackage::GetSavableResourceLinksForFrame(
1155 // storage, after which, render process will serialize DOM for generating 1181 RenderFrameHost* render_frame_host) {
1156 // HTML data. 1182 number_of_frames_with_pending_get_savable_resource_links_++;
1157 void SavePackage::OnReceivedSavableResourceLinksForCurrentPage( 1183 Send(new FrameMsg_GetSavableResourceLinks(render_frame_host->GetRoutingID()));
1184 }
1185
1186 void SavePackage::OnReceivedSavableResourceLinksForFrame(
1187 RenderFrameHost* render_frame_host,
1158 const std::vector<GURL>& resources_list, 1188 const std::vector<GURL>& resources_list,
1159 const std::vector<Referrer>& referrers_list, 1189 const std::vector<Referrer>& referrers_list) {
1160 const std::vector<GURL>& frames_list) {
1161 if (wait_state_ != RESOURCES_LIST) 1190 if (wait_state_ != RESOURCES_LIST)
1162 return; 1191 return;
1163 1192
1164 if (resources_list.size() != referrers_list.size()) 1193 if (resources_list.size() != referrers_list.size())
1165 return; 1194 return;
1166 1195
1167 all_save_items_count_ = static_cast<int>(resources_list.size()) + 1196 // Add all sub-resources to wait list.
1168 static_cast<int>(frames_list.size()); 1197 for (int i = 0; i < static_cast<int>(resources_list.size()); ++i) {
1198 const GURL& u = resources_list[i];
1199 DCHECK(u.is_valid());
1200 if (unique_urls_to_save_.count(u)) {
1201 continue;
Łukasz Anforowicz 2015/09/02 18:03:34 In some cases referrers_list[i] might be preferred
1202 }
1203 unique_urls_to_save_.insert(u);
1204
1205 SaveFileCreateInfo::SaveFileSource save_source =
1206 u.SchemeIsFile() ? SaveFileCreateInfo::SAVE_FILE_FROM_FILE
1207 : SaveFileCreateInfo::SAVE_FILE_FROM_NET;
1208 SaveItem* save_item = new SaveItem(u, referrers_list[i], this, save_source);
1209 waiting_item_queue_.push(save_item);
1210 }
1211
1212 // Add the frame to wait list.
1213 GURL frame_url = render_frame_host->GetLastCommittedURL();
1214 DCHECK(frame_url.is_valid());
1215 if (0 == unique_urls_to_save_.count(frame_url)) {
1216 unique_urls_to_save_.insert(frame_url);
1217 SaveItem* save_item = new SaveItem(frame_url, Referrer(), this,
1218 SaveFileCreateInfo::SAVE_FILE_FROM_DOM);
1219 waiting_item_queue_.push(save_item);
1220 }
1221
1222 CompleteSavableResourceLinksResponseFromFrame();
1223 }
1224
1225 void SavePackage::OnReceivedNonSavableFrameIndication(
1226 RenderFrameHost* render_frame_host) {
1227 CompleteSavableResourceLinksResponseFromFrame();
1228 }
1229
1230 void SavePackage::CompleteSavableResourceLinksResponseFromFrame() {
1231 --number_of_frames_with_pending_get_savable_resource_links_;
1232 if (0 < number_of_frames_with_pending_get_savable_resource_links_) {
1233 return;
1234 }
1235 DCHECK_EQ(0, number_of_frames_with_pending_get_savable_resource_links_);
1236
1237 all_save_items_count_ = static_cast<int>(unique_urls_to_save_.size());
1169 1238
1170 // We use total bytes as the total number of files we want to save. 1239 // We use total bytes as the total number of files we want to save.
1171 // Hack to avoid touching download_ after user cancel. 1240 // Hack to avoid touching download_ after user cancel.
1172 // TODO(rdsmith/benjhayden): Integrate canceling on DownloadItem 1241 // TODO(rdsmith/benjhayden): Integrate canceling on DownloadItem
1173 // with SavePackage flow. 1242 // with SavePackage flow.
1174 if (download_ && (download_->GetState() == DownloadItem::IN_PROGRESS)) 1243 if (download_ && (download_->GetState() == DownloadItem::IN_PROGRESS))
1175 download_->SetTotalBytes(all_save_items_count_); 1244 download_->SetTotalBytes(all_save_items_count_);
1176 1245
1177 if (all_save_items_count_) { 1246 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; 1247 wait_state_ = NET_FILES;
1248
1249 // Give backend the lists which contain all resource links that have local
1250 // storage, after which, render process will serialize DOM for generating
1251 // HTML data.
1198 DoSavingProcess(); 1252 DoSavingProcess();
1199 } else { 1253 } else {
1200 // No resource files need to be saved, treat it as user cancel. 1254 // No resource files need to be saved, treat it as user cancel.
1201 Cancel(true); 1255 Cancel(true);
1202 } 1256 }
1203 } 1257 }
1204 1258
1205 base::FilePath SavePackage::GetSuggestedNameForSaveAs( 1259 base::FilePath SavePackage::GetSuggestedNameForSaveAs(
1206 bool can_save_as_complete, 1260 bool can_save_as_complete,
1207 const std::string& contents_mime_type, 1261 const std::string& contents_mime_type,
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
1437 1491
1438 void SavePackage::FinalizeDownloadEntry() { 1492 void SavePackage::FinalizeDownloadEntry() {
1439 DCHECK(download_); 1493 DCHECK(download_);
1440 DCHECK(download_manager_); 1494 DCHECK(download_manager_);
1441 1495
1442 download_manager_->OnSavePackageSuccessfullyFinished(download_); 1496 download_manager_->OnSavePackageSuccessfullyFinished(download_);
1443 StopObservation(); 1497 StopObservation();
1444 } 1498 }
1445 1499
1446 } // namespace content 1500 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/download/save_package.h ('k') | content/common/frame_messages.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698