| 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 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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)); |
| 227 TRACE_EVENT_NESTABLE_ASYNC_BEGIN1("page-serialization", "WaitingOnRenderer", |
| 228 this, "frame tree node id", |
| 229 frame_tree_node_id); |
| 226 return true; | 230 return true; |
| 227 } | 231 } |
| 228 | 232 |
| 229 void MHTMLGenerationManager::Job::RenderProcessExited( | 233 void MHTMLGenerationManager::Job::RenderProcessExited( |
| 230 RenderProcessHost* host, | 234 RenderProcessHost* host, |
| 231 base::TerminationStatus status, | 235 base::TerminationStatus status, |
| 232 int exit_code) { | 236 int exit_code) { |
| 233 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 237 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 234 MHTMLGenerationManager::GetInstance()->RenderProcessExited(this); | 238 MHTMLGenerationManager::GetInstance()->RenderProcessExited(this); |
| 235 } | 239 } |
| 236 | 240 |
| 237 void MHTMLGenerationManager::Job::MarkAsFinished() { | 241 void MHTMLGenerationManager::Job::MarkAsFinished() { |
| 238 DCHECK(!is_finished_); | 242 DCHECK(!is_finished_); |
| 239 is_finished_ = true; | 243 is_finished_ = true; |
| 244 TRACE_EVENT_NESTABLE_ASYNC_INSTANT0("page-serialization", "JobFinished", |
| 245 this); |
| 240 | 246 |
| 241 // Stopping RenderProcessExited notifications is needed to avoid calling | 247 // Stopping RenderProcessExited notifications is needed to avoid calling |
| 242 // JobFinished twice. See also https://crbug.com/612098. | 248 // JobFinished twice. See also https://crbug.com/612098. |
| 243 observed_renderer_process_host_.RemoveAll(); | 249 observed_renderer_process_host_.RemoveAll(); |
| 244 } | 250 } |
| 245 | 251 |
| 246 void MHTMLGenerationManager::Job::AddFrame(RenderFrameHost* render_frame_host) { | 252 void MHTMLGenerationManager::Job::AddFrame(RenderFrameHost* render_frame_host) { |
| 247 auto* rfhi = static_cast<RenderFrameHostImpl*>(render_frame_host); | 253 auto* rfhi = static_cast<RenderFrameHostImpl*>(render_frame_host); |
| 248 int frame_tree_node_id = rfhi->frame_tree_node()->frame_tree_node_id(); | 254 int frame_tree_node_id = rfhi->frame_tree_node()->frame_tree_node_id(); |
| 249 pending_frame_tree_node_ids_.push(frame_tree_node_id); | 255 pending_frame_tree_node_ids_.push(frame_tree_node_id); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 | 328 |
| 323 MHTMLGenerationManager::~MHTMLGenerationManager() { | 329 MHTMLGenerationManager::~MHTMLGenerationManager() { |
| 324 base::STLDeleteValues(&id_to_job_); | 330 base::STLDeleteValues(&id_to_job_); |
| 325 } | 331 } |
| 326 | 332 |
| 327 void MHTMLGenerationManager::SaveMHTML(WebContents* web_contents, | 333 void MHTMLGenerationManager::SaveMHTML(WebContents* web_contents, |
| 328 const MHTMLGenerationParams& params, | 334 const MHTMLGenerationParams& params, |
| 329 const GenerateMHTMLCallback& callback) { | 335 const GenerateMHTMLCallback& callback) { |
| 330 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 336 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 331 | 337 |
| 332 int job_id = NewJob(web_contents, params, callback); | 338 Job* job = NewJob(web_contents, params, callback); |
| 339 TRACE_EVENT_NESTABLE_ASYNC_BEGIN2( |
| 340 "page-serialization", "SavingMhtmlJob", job, "url", |
| 341 web_contents->GetLastCommittedURL().possibly_invalid_spec().c_str(), |
| 342 "file", params.file_path.value().c_str()); |
| 333 | 343 |
| 334 BrowserThread::PostTaskAndReplyWithResult( | 344 BrowserThread::PostTaskAndReplyWithResult( |
| 335 BrowserThread::FILE, FROM_HERE, | 345 BrowserThread::FILE, FROM_HERE, |
| 336 base::Bind(&MHTMLGenerationManager::CreateFile, params.file_path), | 346 base::Bind(&MHTMLGenerationManager::CreateFile, params.file_path), |
| 337 base::Bind(&MHTMLGenerationManager::OnFileAvailable, | 347 base::Bind(&MHTMLGenerationManager::OnFileAvailable, |
| 338 base::Unretained(this), // Safe b/c |this| is a singleton. | 348 base::Unretained(this), // Safe b/c |this| is a singleton. |
| 339 job_id)); | 349 job->id())); |
| 340 } | 350 } |
| 341 | 351 |
| 342 void MHTMLGenerationManager::OnSerializeAsMHTMLResponse( | 352 void MHTMLGenerationManager::OnSerializeAsMHTMLResponse( |
| 343 RenderFrameHostImpl* sender, | 353 RenderFrameHostImpl* sender, |
| 344 int job_id, | 354 int job_id, |
| 345 bool mhtml_generation_in_renderer_succeeded, | 355 bool mhtml_generation_in_renderer_succeeded, |
| 346 const std::set<std::string>& digests_of_uris_of_serialized_resources) { | 356 const std::set<std::string>& digests_of_uris_of_serialized_resources) { |
| 347 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 357 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 348 | 358 |
| 349 Job* job = FindJob(job_id); | 359 Job* job = FindJob(job_id); |
| 350 if (!job || !job->IsMessageFromFrameExpected(sender)) { | 360 if (!job || !job->IsMessageFromFrameExpected(sender)) { |
| 351 NOTREACHED(); | 361 NOTREACHED(); |
| 352 ReceivedBadMessage(sender->GetProcess(), | 362 ReceivedBadMessage(sender->GetProcess(), |
| 353 bad_message::DWNLD_INVALID_SERIALIZE_AS_MHTML_RESPONSE); | 363 bad_message::DWNLD_INVALID_SERIALIZE_AS_MHTML_RESPONSE); |
| 354 return; | 364 return; |
| 355 } | 365 } |
| 356 | 366 |
| 367 TRACE_EVENT_NESTABLE_ASYNC_END0("page-serialization", "WaitingOnRenderer", |
| 368 job); |
| 369 |
| 357 if (!mhtml_generation_in_renderer_succeeded) { | 370 if (!mhtml_generation_in_renderer_succeeded) { |
| 358 JobFinished(job, JobStatus::FAILURE); | 371 JobFinished(job, JobStatus::FAILURE); |
| 359 return; | 372 return; |
| 360 } | 373 } |
| 361 | 374 |
| 362 if (!job->OnSerializeAsMHTMLResponse( | 375 if (!job->OnSerializeAsMHTMLResponse( |
| 363 digests_of_uris_of_serialized_resources)) { | 376 digests_of_uris_of_serialized_resources)) { |
| 364 JobFinished(job, JobStatus::FAILURE); | 377 JobFinished(job, JobStatus::FAILURE); |
| 365 return; | 378 return; |
| 366 } | 379 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 base::Unretained(this), // Safe b/c |this| is a singleton. | 432 base::Unretained(this), // Safe b/c |this| is a singleton. |
| 420 job->id(), job_status)); | 433 job->id(), job_status)); |
| 421 } | 434 } |
| 422 | 435 |
| 423 void MHTMLGenerationManager::OnFileClosed(int job_id, | 436 void MHTMLGenerationManager::OnFileClosed(int job_id, |
| 424 JobStatus job_status, | 437 JobStatus job_status, |
| 425 int64_t file_size) { | 438 int64_t file_size) { |
| 426 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 439 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 427 | 440 |
| 428 Job* job = FindJob(job_id); | 441 Job* job = FindJob(job_id); |
| 442 TRACE_EVENT_NESTABLE_ASYNC_END2( |
| 443 "page-serialization", "SavingMhtmlJob", job, "job result", |
| 444 job_status == JobStatus::SUCCESS ? "success" : "failure", "file size", |
| 445 file_size); |
| 429 job->callback().Run(job_status == JobStatus::SUCCESS ? file_size : -1); | 446 job->callback().Run(job_status == JobStatus::SUCCESS ? file_size : -1); |
| 430 id_to_job_.erase(job_id); | 447 id_to_job_.erase(job_id); |
| 431 delete job; | 448 delete job; |
| 432 } | 449 } |
| 433 | 450 |
| 434 int MHTMLGenerationManager::NewJob(WebContents* web_contents, | 451 MHTMLGenerationManager::Job* MHTMLGenerationManager::NewJob( |
| 435 const MHTMLGenerationParams& params, | 452 WebContents* web_contents, |
| 436 const GenerateMHTMLCallback& callback) { | 453 const MHTMLGenerationParams& params, |
| 454 const GenerateMHTMLCallback& callback) { |
| 437 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 455 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 438 | 456 |
| 439 int job_id = next_job_id_++; | 457 Job* job = new Job(++next_job_id_, web_contents, params, callback); |
| 440 id_to_job_[job_id] = new Job(job_id, web_contents, params, callback); | 458 id_to_job_[job->id()] = job; |
| 441 return job_id; | 459 return job; |
| 442 } | 460 } |
| 443 | 461 |
| 444 MHTMLGenerationManager::Job* MHTMLGenerationManager::FindJob(int job_id) { | 462 MHTMLGenerationManager::Job* MHTMLGenerationManager::FindJob(int job_id) { |
| 445 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 463 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 446 | 464 |
| 447 IDToJobMap::iterator iter = id_to_job_.find(job_id); | 465 IDToJobMap::iterator iter = id_to_job_.find(job_id); |
| 448 if (iter == id_to_job_.end()) { | 466 if (iter == id_to_job_.end()) { |
| 449 NOTREACHED(); | 467 NOTREACHED(); |
| 450 return nullptr; | 468 return nullptr; |
| 451 } | 469 } |
| 452 return iter->second; | 470 return iter->second; |
| 453 } | 471 } |
| 454 | 472 |
| 455 void MHTMLGenerationManager::RenderProcessExited(Job* job) { | 473 void MHTMLGenerationManager::RenderProcessExited(Job* job) { |
| 456 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 474 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 457 DCHECK(job); | 475 DCHECK(job); |
| 458 JobFinished(job, JobStatus::FAILURE); | 476 JobFinished(job, JobStatus::FAILURE); |
| 459 } | 477 } |
| 460 | 478 |
| 461 } // namespace content | 479 } // namespace content |
| OLD | NEW |