Chromium Code Reviews| 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/mhtml_generation_manager.h" | 5 #include "content/browser/download/mhtml_generation_manager.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <queue> | 8 #include <queue> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/files/file.h" | 12 #include "base/files/file.h" |
| 13 #include "base/guid.h" | 13 #include "base/guid.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/scoped_observer.h" | 15 #include "base/scoped_observer.h" |
| 16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "base/trace_event/trace_event.h" | |
| 18 #include "content/browser/bad_message.h" | 19 #include "content/browser/bad_message.h" |
| 19 #include "content/browser/frame_host/frame_tree_node.h" | 20 #include "content/browser/frame_host/frame_tree_node.h" |
| 20 #include "content/browser/frame_host/render_frame_host_impl.h" | 21 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 21 #include "content/common/frame_messages.h" | 22 #include "content/common/frame_messages.h" |
| 22 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 23 #include "content/public/browser/render_frame_host.h" | 24 #include "content/public/browser/render_frame_host.h" |
| 24 #include "content/public/browser/render_process_host.h" | 25 #include "content/public/browser/render_process_host.h" |
| 25 #include "content/public/browser/render_process_host_observer.h" | 26 #include "content/public/browser/render_process_host_observer.h" |
| 26 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 27 #include "content/public/common/mhtml_generation_params.h" | 28 #include "content/public/common/mhtml_generation_params.h" |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 | 216 |
| 216 ipc_params.destination_file = IPC::GetPlatformFileForTransit( | 217 ipc_params.destination_file = IPC::GetPlatformFileForTransit( |
| 217 browser_file_.GetPlatformFile(), false); // |close_source_handle|. | 218 browser_file_.GetPlatformFile(), false); // |close_source_handle|. |
| 218 ipc_params.frame_routing_id_to_content_id = | 219 ipc_params.frame_routing_id_to_content_id = |
| 219 CreateFrameRoutingIdToContentId(rfh->GetSiteInstance()); | 220 CreateFrameRoutingIdToContentId(rfh->GetSiteInstance()); |
| 220 | 221 |
| 221 // Send the IPC asking the renderer to serialize the frame. | 222 // Send the IPC asking the renderer to serialize the frame. |
| 222 DCHECK_EQ(FrameTreeNode::kFrameTreeNodeInvalidId, | 223 DCHECK_EQ(FrameTreeNode::kFrameTreeNodeInvalidId, |
| 223 frame_tree_node_id_of_busy_frame_); | 224 frame_tree_node_id_of_busy_frame_); |
| 224 frame_tree_node_id_of_busy_frame_ = frame_tree_node_id; | 225 frame_tree_node_id_of_busy_frame_ = frame_tree_node_id; |
| 225 rfh->Send(new FrameMsg_SerializeAsMHTML(rfh->GetRoutingID(), ipc_params)); | 226 rfh->Send(new FrameMsg_SerializeAsMHTML(rfh->GetRoutingID(), ipc_params)); |
|
esprehn
2016/09/21 00:14:57
we really need to rewrite this with mojo and move
carlosk
2016/09/21 16:47:19
Acknowledged.
| |
| 227 TRACE_EVENT_NESTABLE_ASYNC_BEGIN1("page-serialization", "WaitingOnRenderer", | |
| 228 this, "tree node id", frame_tree_node_id); | |
|
Łukasz Anforowicz
2016/09/20 22:44:59
nit: Would it be worth spelling out "frame tree no
carlosk
2016/09/21 16:47:20
Done.
| |
| 226 return true; | 229 return true; |
| 227 } | 230 } |
| 228 | 231 |
| 229 void MHTMLGenerationManager::Job::RenderProcessExited( | 232 void MHTMLGenerationManager::Job::RenderProcessExited( |
| 230 RenderProcessHost* host, | 233 RenderProcessHost* host, |
| 231 base::TerminationStatus status, | 234 base::TerminationStatus status, |
| 232 int exit_code) { | 235 int exit_code) { |
| 233 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 236 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 234 MHTMLGenerationManager::GetInstance()->RenderProcessExited(this); | 237 MHTMLGenerationManager::GetInstance()->RenderProcessExited(this); |
| 235 } | 238 } |
| 236 | 239 |
| 237 void MHTMLGenerationManager::Job::MarkAsFinished() { | 240 void MHTMLGenerationManager::Job::MarkAsFinished() { |
| 238 DCHECK(!is_finished_); | 241 DCHECK(!is_finished_); |
| 239 is_finished_ = true; | 242 is_finished_ = true; |
| 243 TRACE_EVENT_NESTABLE_ASYNC_INSTANT0("page-serialization", "JobFinished", | |
| 244 this); | |
| 240 | 245 |
| 241 // Stopping RenderProcessExited notifications is needed to avoid calling | 246 // Stopping RenderProcessExited notifications is needed to avoid calling |
| 242 // JobFinished twice. See also https://crbug.com/612098. | 247 // JobFinished twice. See also https://crbug.com/612098. |
| 243 observed_renderer_process_host_.RemoveAll(); | 248 observed_renderer_process_host_.RemoveAll(); |
| 244 } | 249 } |
| 245 | 250 |
| 246 void MHTMLGenerationManager::Job::AddFrame(RenderFrameHost* render_frame_host) { | 251 void MHTMLGenerationManager::Job::AddFrame(RenderFrameHost* render_frame_host) { |
| 247 auto* rfhi = static_cast<RenderFrameHostImpl*>(render_frame_host); | 252 auto* rfhi = static_cast<RenderFrameHostImpl*>(render_frame_host); |
| 248 int frame_tree_node_id = rfhi->frame_tree_node()->frame_tree_node_id(); | 253 int frame_tree_node_id = rfhi->frame_tree_node()->frame_tree_node_id(); |
| 249 pending_frame_tree_node_ids_.push(frame_tree_node_id); | 254 pending_frame_tree_node_ids_.push(frame_tree_node_id); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 323 MHTMLGenerationManager::~MHTMLGenerationManager() { | 328 MHTMLGenerationManager::~MHTMLGenerationManager() { |
| 324 base::STLDeleteValues(&id_to_job_); | 329 base::STLDeleteValues(&id_to_job_); |
| 325 } | 330 } |
| 326 | 331 |
| 327 void MHTMLGenerationManager::SaveMHTML(WebContents* web_contents, | 332 void MHTMLGenerationManager::SaveMHTML(WebContents* web_contents, |
| 328 const MHTMLGenerationParams& params, | 333 const MHTMLGenerationParams& params, |
| 329 const GenerateMHTMLCallback& callback) { | 334 const GenerateMHTMLCallback& callback) { |
| 330 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 335 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 331 | 336 |
| 332 int job_id = NewJob(web_contents, params, callback); | 337 int job_id = NewJob(web_contents, params, callback); |
| 338 TRACE_EVENT_NESTABLE_ASYNC_BEGIN2( | |
| 339 "page-serialization", "SavingMhtmlJob", FindJob(job_id), "url", | |
|
Łukasz Anforowicz
2016/09/20 22:44:59
FindJob above looks weird (maybe just to me :-).
carlosk
2016/09/21 16:47:19
Sounds better to me too. Done.
This mapping shoul
| |
| 340 web_contents->GetLastCommittedURL().spec().c_str(), "file", | |
|
Łukasz Anforowicz
2016/09/20 22:44:59
I don't know if GetLastCommittedURL is guaranteed
carlosk
2016/09/21 16:47:19
If the URL was committed it must be valid otherwis
| |
| 341 params.file_path.value().c_str()); | |
| 333 | 342 |
| 334 BrowserThread::PostTaskAndReplyWithResult( | 343 BrowserThread::PostTaskAndReplyWithResult( |
| 335 BrowserThread::FILE, FROM_HERE, | 344 BrowserThread::FILE, FROM_HERE, |
| 336 base::Bind(&MHTMLGenerationManager::CreateFile, params.file_path), | 345 base::Bind(&MHTMLGenerationManager::CreateFile, params.file_path), |
| 337 base::Bind(&MHTMLGenerationManager::OnFileAvailable, | 346 base::Bind(&MHTMLGenerationManager::OnFileAvailable, |
| 338 base::Unretained(this), // Safe b/c |this| is a singleton. | 347 base::Unretained(this), // Safe b/c |this| is a singleton. |
| 339 job_id)); | 348 job_id)); |
| 340 } | 349 } |
| 341 | 350 |
| 342 void MHTMLGenerationManager::OnSerializeAsMHTMLResponse( | 351 void MHTMLGenerationManager::OnSerializeAsMHTMLResponse( |
| 343 RenderFrameHostImpl* sender, | 352 RenderFrameHostImpl* sender, |
| 344 int job_id, | 353 int job_id, |
| 345 bool mhtml_generation_in_renderer_succeeded, | 354 bool mhtml_generation_in_renderer_succeeded, |
| 346 const std::set<std::string>& digests_of_uris_of_serialized_resources) { | 355 const std::set<std::string>& digests_of_uris_of_serialized_resources) { |
| 347 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 356 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 348 | 357 |
| 349 Job* job = FindJob(job_id); | 358 Job* job = FindJob(job_id); |
| 350 if (!job || !job->IsMessageFromFrameExpected(sender)) { | 359 if (!job || !job->IsMessageFromFrameExpected(sender)) { |
| 351 NOTREACHED(); | 360 NOTREACHED(); |
| 352 ReceivedBadMessage(sender->GetProcess(), | 361 ReceivedBadMessage(sender->GetProcess(), |
| 353 bad_message::DWNLD_INVALID_SERIALIZE_AS_MHTML_RESPONSE); | 362 bad_message::DWNLD_INVALID_SERIALIZE_AS_MHTML_RESPONSE); |
| 354 return; | 363 return; |
| 355 } | 364 } |
| 356 | 365 |
| 366 TRACE_EVENT_NESTABLE_ASYNC_END0("page-serialization", "WaitingOnRenderer", | |
| 367 job); | |
| 368 | |
| 357 if (!mhtml_generation_in_renderer_succeeded) { | 369 if (!mhtml_generation_in_renderer_succeeded) { |
| 358 JobFinished(job, JobStatus::FAILURE); | 370 JobFinished(job, JobStatus::FAILURE); |
| 359 return; | 371 return; |
| 360 } | 372 } |
| 361 | 373 |
| 362 if (!job->OnSerializeAsMHTMLResponse( | 374 if (!job->OnSerializeAsMHTMLResponse( |
| 363 digests_of_uris_of_serialized_resources)) { | 375 digests_of_uris_of_serialized_resources)) { |
| 364 JobFinished(job, JobStatus::FAILURE); | 376 JobFinished(job, JobStatus::FAILURE); |
| 365 return; | 377 return; |
| 366 } | 378 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 419 base::Unretained(this), // Safe b/c |this| is a singleton. | 431 base::Unretained(this), // Safe b/c |this| is a singleton. |
| 420 job->id(), job_status)); | 432 job->id(), job_status)); |
| 421 } | 433 } |
| 422 | 434 |
| 423 void MHTMLGenerationManager::OnFileClosed(int job_id, | 435 void MHTMLGenerationManager::OnFileClosed(int job_id, |
| 424 JobStatus job_status, | 436 JobStatus job_status, |
| 425 int64_t file_size) { | 437 int64_t file_size) { |
| 426 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 438 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 427 | 439 |
| 428 Job* job = FindJob(job_id); | 440 Job* job = FindJob(job_id); |
| 441 TRACE_EVENT_NESTABLE_ASYNC_END2( | |
| 442 "page-serialization", "SavingMhtmlJob", job, "job result", | |
| 443 job_status == JobStatus::SUCCESS ? "success" : "failure", "file size", | |
| 444 file_size); | |
| 429 job->callback().Run(job_status == JobStatus::SUCCESS ? file_size : -1); | 445 job->callback().Run(job_status == JobStatus::SUCCESS ? file_size : -1); |
| 430 id_to_job_.erase(job_id); | 446 id_to_job_.erase(job_id); |
| 431 delete job; | 447 delete job; |
| 432 } | 448 } |
| 433 | 449 |
| 434 int MHTMLGenerationManager::NewJob(WebContents* web_contents, | 450 int MHTMLGenerationManager::NewJob(WebContents* web_contents, |
| 435 const MHTMLGenerationParams& params, | 451 const MHTMLGenerationParams& params, |
| 436 const GenerateMHTMLCallback& callback) { | 452 const GenerateMHTMLCallback& callback) { |
| 437 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 453 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 438 | 454 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 452 return iter->second; | 468 return iter->second; |
| 453 } | 469 } |
| 454 | 470 |
| 455 void MHTMLGenerationManager::RenderProcessExited(Job* job) { | 471 void MHTMLGenerationManager::RenderProcessExited(Job* job) { |
| 456 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 472 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 457 DCHECK(job); | 473 DCHECK(job); |
| 458 JobFinished(job, JobStatus::FAILURE); | 474 JobFinished(job, JobStatus::FAILURE); |
| 459 } | 475 } |
| 460 | 476 |
| 461 } // namespace content | 477 } // namespace content |
| OLD | NEW |