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

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: Addressed more CR feedback from Nasko. 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 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
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
1149 wait_state_ = RESOURCES_LIST; 1161 wait_state_ = RESOURCES_LIST;
1150 Send(new ViewMsg_GetAllSavableResourceLinksForCurrentPage(routing_id(), 1162 number_of_frames_with_pending_get_savable_resource_links_ = 0;
1151 page_url_)); 1163 web_contents()->ForEachFrame(base::Bind(
1164 &SavePackage::GetSavableResourceLinksForFrame,
1165 base::Unretained(this))); // Safe, because ForEachFrame is synchronous.
1166 DCHECK_LT(0, number_of_frames_with_pending_get_savable_resource_links_);
1152 } 1167 }
1153 1168
1154 // Give backend the lists which contain all resource links that have local 1169 void SavePackage::GetSavableResourceLinksForFrame(RenderFrameHost* target) {
1155 // storage, after which, render process will serialize DOM for generating 1170 number_of_frames_with_pending_get_savable_resource_links_++;
1156 // HTML data. 1171 Send(new FrameMsg_GetSavableResourceLinks(target->GetRoutingID()));
1157 void SavePackage::OnReceivedSavableResourceLinksForCurrentPage( 1172 }
1173
1174 void SavePackage::OnSavableResourceLinksResponse(
1175 RenderFrameHost* source,
1158 const std::vector<GURL>& resources_list, 1176 const std::vector<GURL>& resources_list,
1159 const std::vector<Referrer>& referrers_list, 1177 const std::vector<Referrer>& referrers_list) {
1160 const std::vector<GURL>& frames_list) {
1161 if (wait_state_ != RESOURCES_LIST) 1178 if (wait_state_ != RESOURCES_LIST)
1162 return; 1179 return;
1163 1180
1164 if (resources_list.size() != referrers_list.size()) 1181 if (resources_list.size() != referrers_list.size())
1165 return; 1182 return;
1166 1183
1167 all_save_items_count_ = static_cast<int>(resources_list.size()) + 1184 // Add all sub-resources to wait list.
1168 static_cast<int>(frames_list.size()); 1185 for (int i = 0; i < static_cast<int>(resources_list.size()); ++i) {
1186 const GURL& u = resources_list[i];
1187 DCHECK(u.is_valid());
1188 if (unique_urls_to_save_.count(u)) {
1189 continue;
1190 }
1191 unique_urls_to_save_.insert(u);
1192
1193 SaveFileCreateInfo::SaveFileSource save_source =
1194 u.SchemeIsFile() ? SaveFileCreateInfo::SAVE_FILE_FROM_FILE
1195 : SaveFileCreateInfo::SAVE_FILE_FROM_NET;
1196 SaveItem* save_item = new SaveItem(u, referrers_list[i], this, save_source);
1197 waiting_item_queue_.push(save_item);
1198 }
1199
1200 // Add the frame to wait list.
1201 GURL frame_url = source->GetLastCommittedURL();
1202 DCHECK(frame_url.is_valid());
1203 if (0 == unique_urls_to_save_.count(frame_url)) {
1204 unique_urls_to_save_.insert(frame_url);
1205 SaveItem* save_item = new SaveItem(frame_url, Referrer(), this,
1206 SaveFileCreateInfo::SAVE_FILE_FROM_DOM);
1207 waiting_item_queue_.push(save_item);
1208 }
1209
1210 CompleteSavableResourceLinksResponseFromFrame();
1211 }
1212
1213 void SavePackage::OnSavableResourceLinksError(RenderFrameHost* source) {
1214 CompleteSavableResourceLinksResponseFromFrame();
1215 }
1216
1217 void SavePackage::CompleteSavableResourceLinksResponseFromFrame() {
1218 --number_of_frames_with_pending_get_savable_resource_links_;
1219 DCHECK_LE(0, number_of_frames_with_pending_get_savable_resource_links_);
1220 if (number_of_frames_with_pending_get_savable_resource_links_ != 0) {
nasko 2015/09/04 16:03:21 nit: Single line if statements don't need {}
Łukasz Anforowicz 2015/09/08 16:23:21 Acknowledged.
1221 return; // Need to wait for more responses from RenderFrames.
1222 }
1223
1224 all_save_items_count_ = static_cast<int>(unique_urls_to_save_.size());
1169 1225
1170 // We use total bytes as the total number of files we want to save. 1226 // We use total bytes as the total number of files we want to save.
1171 // Hack to avoid touching download_ after user cancel. 1227 // Hack to avoid touching download_ after user cancel.
1172 // TODO(rdsmith/benjhayden): Integrate canceling on DownloadItem 1228 // TODO(rdsmith/benjhayden): Integrate canceling on DownloadItem
1173 // with SavePackage flow. 1229 // with SavePackage flow.
1174 if (download_ && (download_->GetState() == DownloadItem::IN_PROGRESS)) 1230 if (download_ && (download_->GetState() == DownloadItem::IN_PROGRESS))
1175 download_->SetTotalBytes(all_save_items_count_); 1231 download_->SetTotalBytes(all_save_items_count_);
1176 1232
1177 if (all_save_items_count_) { 1233 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; 1234 wait_state_ = NET_FILES;
1235
1236 // Give backend the lists which contain all resource links that have local
1237 // storage, after which, render process will serialize DOM for generating
1238 // HTML data.
1198 DoSavingProcess(); 1239 DoSavingProcess();
1199 } else { 1240 } else {
1200 // No resource files need to be saved, treat it as user cancel. 1241 // No resource files need to be saved, treat it as user cancel.
1201 Cancel(true); 1242 Cancel(true);
1202 } 1243 }
1203 } 1244 }
1204 1245
1205 base::FilePath SavePackage::GetSuggestedNameForSaveAs( 1246 base::FilePath SavePackage::GetSuggestedNameForSaveAs(
1206 bool can_save_as_complete, 1247 bool can_save_as_complete,
1207 const std::string& contents_mime_type, 1248 const std::string& contents_mime_type,
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
1437 1478
1438 void SavePackage::FinalizeDownloadEntry() { 1479 void SavePackage::FinalizeDownloadEntry() {
1439 DCHECK(download_); 1480 DCHECK(download_);
1440 DCHECK(download_manager_); 1481 DCHECK(download_manager_);
1441 1482
1442 download_manager_->OnSavePackageSuccessfullyFinished(download_); 1483 download_manager_->OnSavePackageSuccessfullyFinished(download_);
1443 StopObservation(); 1484 StopObservation();
1444 } 1485 }
1445 1486
1446 } // namespace content 1487 } // 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