| 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/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
| 17 #include "base/scoped_observer.h" | 17 #include "base/scoped_observer.h" |
| 18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
| 21 #include "base/task_runner_util.h" |
| 21 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 22 #include "base/trace_event/trace_event.h" | 23 #include "base/trace_event/trace_event.h" |
| 23 #include "content/browser/bad_message.h" | 24 #include "content/browser/bad_message.h" |
| 25 #include "content/browser/download/download_task_runner.h" |
| 24 #include "content/browser/download/mhtml_extra_parts_impl.h" | 26 #include "content/browser/download/mhtml_extra_parts_impl.h" |
| 25 #include "content/browser/frame_host/frame_tree_node.h" | 27 #include "content/browser/frame_host/frame_tree_node.h" |
| 26 #include "content/browser/frame_host/render_frame_host_impl.h" | 28 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 27 #include "content/common/frame_messages.h" | 29 #include "content/common/frame_messages.h" |
| 28 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
| 29 #include "content/public/browser/mhtml_extra_parts.h" | 31 #include "content/public/browser/mhtml_extra_parts.h" |
| 30 #include "content/public/browser/render_frame_host.h" | 32 #include "content/public/browser/render_frame_host.h" |
| 31 #include "content/public/browser/render_process_host.h" | 33 #include "content/public/browser/render_process_host.h" |
| 32 #include "content/public/browser/render_process_host_observer.h" | 34 #include "content/public/browser/render_process_host_observer.h" |
| 33 #include "content/public/browser/web_contents.h" | 35 #include "content/public/browser/web_contents.h" |
| (...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 | 374 |
| 373 if (!browser_file_.IsValid()) { | 375 if (!browser_file_.IsValid()) { |
| 374 // Only update the status if that won't hide an earlier error. | 376 // Only update the status if that won't hide an earlier error. |
| 375 if (save_status == MhtmlSaveStatus::SUCCESS) | 377 if (save_status == MhtmlSaveStatus::SUCCESS) |
| 376 save_status = MhtmlSaveStatus::FILE_WRITTING_ERROR; | 378 save_status = MhtmlSaveStatus::FILE_WRITTING_ERROR; |
| 377 callback.Run(std::make_tuple(save_status, -1)); | 379 callback.Run(std::make_tuple(save_status, -1)); |
| 378 return; | 380 return; |
| 379 } | 381 } |
| 380 | 382 |
| 381 // If no previous error occurred the boundary should be sent. | 383 // If no previous error occurred the boundary should be sent. |
| 382 BrowserThread::PostTaskAndReplyWithResult( | 384 PostTaskAndReplyWithResult( |
| 383 BrowserThread::FILE, FROM_HERE, | 385 GetDownloadTaskRunner().get(), FROM_HERE, |
| 384 base::Bind( | 386 base::Bind( |
| 385 &MHTMLGenerationManager::Job::FinalizeAndCloseFileOnFileThread, | 387 &MHTMLGenerationManager::Job::FinalizeAndCloseFileOnFileThread, |
| 386 save_status, | 388 save_status, |
| 387 (save_status == MhtmlSaveStatus::SUCCESS ? mhtml_boundary_marker_ | 389 (save_status == MhtmlSaveStatus::SUCCESS ? mhtml_boundary_marker_ |
| 388 : std::string()), | 390 : std::string()), |
| 389 base::Passed(&browser_file_), extra_parts_), | 391 base::Passed(&browser_file_), extra_parts_), |
| 390 callback); | 392 callback); |
| 391 } | 393 } |
| 392 | 394 |
| 393 bool MHTMLGenerationManager::Job::IsMessageFromFrameExpected( | 395 bool MHTMLGenerationManager::Job::IsMessageFromFrameExpected( |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 return SendToNextRenderFrame(); | 432 return SendToNextRenderFrame(); |
| 431 } | 433 } |
| 432 | 434 |
| 433 // static | 435 // static |
| 434 std::tuple<MhtmlSaveStatus, int64_t> | 436 std::tuple<MhtmlSaveStatus, int64_t> |
| 435 MHTMLGenerationManager::Job::FinalizeAndCloseFileOnFileThread( | 437 MHTMLGenerationManager::Job::FinalizeAndCloseFileOnFileThread( |
| 436 MhtmlSaveStatus save_status, | 438 MhtmlSaveStatus save_status, |
| 437 const std::string& boundary, | 439 const std::string& boundary, |
| 438 base::File file, | 440 base::File file, |
| 439 const MHTMLExtraPartsImpl* extra_parts) { | 441 const MHTMLExtraPartsImpl* extra_parts) { |
| 440 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 442 DCHECK(GetDownloadTaskRunner()->RunsTasksInCurrentSequence()); |
| 441 | 443 |
| 442 // If no previous error occurred the boundary should have been provided. | 444 // If no previous error occurred the boundary should have been provided. |
| 443 if (save_status == MhtmlSaveStatus::SUCCESS) { | 445 if (save_status == MhtmlSaveStatus::SUCCESS) { |
| 444 TRACE_EVENT0("page-serialization", | 446 TRACE_EVENT0("page-serialization", |
| 445 "MHTMLGenerationManager::Job MHTML footer writing"); | 447 "MHTMLGenerationManager::Job MHTML footer writing"); |
| 446 DCHECK(!boundary.empty()); | 448 DCHECK(!boundary.empty()); |
| 447 | 449 |
| 448 // Write the extra data into a part of its own, if we have any. | 450 // Write the extra data into a part of its own, if we have any. |
| 449 if (!WriteExtraDataParts(boundary, file, extra_parts)) { | 451 if (!WriteExtraDataParts(boundary, file, extra_parts)) { |
| 450 save_status = MhtmlSaveStatus::FILE_WRITTING_ERROR; | 452 save_status = MhtmlSaveStatus::FILE_WRITTING_ERROR; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 466 } | 468 } |
| 467 | 469 |
| 468 return std::make_tuple(save_status, file_size); | 470 return std::make_tuple(save_status, file_size); |
| 469 } | 471 } |
| 470 | 472 |
| 471 // static | 473 // static |
| 472 bool MHTMLGenerationManager::Job::WriteExtraDataParts( | 474 bool MHTMLGenerationManager::Job::WriteExtraDataParts( |
| 473 const std::string& boundary, | 475 const std::string& boundary, |
| 474 base::File& file, | 476 base::File& file, |
| 475 const MHTMLExtraPartsImpl* extra_parts) { | 477 const MHTMLExtraPartsImpl* extra_parts) { |
| 476 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 478 DCHECK(GetDownloadTaskRunner()->RunsTasksInCurrentSequence()); |
| 477 // Don't write an extra data part if there is none. | 479 // Don't write an extra data part if there is none. |
| 478 if (extra_parts == nullptr) | 480 if (extra_parts == nullptr) |
| 479 return true; | 481 return true; |
| 480 | 482 |
| 481 const std::vector<MHTMLExtraDataPart>& extra_data_parts(extra_parts->parts()); | 483 const std::vector<MHTMLExtraDataPart>& extra_data_parts(extra_parts->parts()); |
| 482 if (extra_data_parts.empty()) | 484 if (extra_data_parts.empty()) |
| 483 return true; | 485 return true; |
| 484 | 486 |
| 485 std::string serialized_extra_data_parts; | 487 std::string serialized_extra_data_parts; |
| 486 | 488 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 501 } | 503 } |
| 502 | 504 |
| 503 // Write the string into the file. Returns false if we failed the write. | 505 // Write the string into the file. Returns false if we failed the write. |
| 504 return (file.WriteAtCurrentPos(serialized_extra_data_parts.data(), | 506 return (file.WriteAtCurrentPos(serialized_extra_data_parts.data(), |
| 505 serialized_extra_data_parts.size()) >= 0); | 507 serialized_extra_data_parts.size()) >= 0); |
| 506 } | 508 } |
| 507 | 509 |
| 508 // static | 510 // static |
| 509 bool MHTMLGenerationManager::Job::WriteFooter(const std::string& boundary, | 511 bool MHTMLGenerationManager::Job::WriteFooter(const std::string& boundary, |
| 510 base::File& file) { | 512 base::File& file) { |
| 511 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 513 DCHECK(GetDownloadTaskRunner()->RunsTasksInCurrentSequence()); |
| 512 std::string footer = base::StringPrintf("--%s--\r\n", boundary.c_str()); | 514 std::string footer = base::StringPrintf("--%s--\r\n", boundary.c_str()); |
| 513 DCHECK(base::IsStringASCII(footer)); | 515 DCHECK(base::IsStringASCII(footer)); |
| 514 return (file.WriteAtCurrentPos(footer.data(), footer.size()) >= 0); | 516 return (file.WriteAtCurrentPos(footer.data(), footer.size()) >= 0); |
| 515 } | 517 } |
| 516 | 518 |
| 517 // static | 519 // static |
| 518 bool MHTMLGenerationManager::Job::CloseFileIfValid(base::File& file, | 520 bool MHTMLGenerationManager::Job::CloseFileIfValid(base::File& file, |
| 519 int64_t* file_size) { | 521 int64_t* file_size) { |
| 520 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 522 DCHECK(GetDownloadTaskRunner()->RunsTasksInCurrentSequence()); |
| 521 DCHECK(file_size); | 523 DCHECK(file_size); |
| 522 if (file.IsValid()) { | 524 if (file.IsValid()) { |
| 523 *file_size = file.GetLength(); | 525 *file_size = file.GetLength(); |
| 524 file.Close(); | 526 file.Close(); |
| 525 return true; | 527 return true; |
| 526 } | 528 } |
| 527 | 529 |
| 528 return false; | 530 return false; |
| 529 } | 531 } |
| 530 | 532 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 541 const MHTMLGenerationParams& params, | 543 const MHTMLGenerationParams& params, |
| 542 const GenerateMHTMLCallback& callback) { | 544 const GenerateMHTMLCallback& callback) { |
| 543 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 545 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 544 | 546 |
| 545 Job* job = NewJob(web_contents, params, callback); | 547 Job* job = NewJob(web_contents, params, callback); |
| 546 TRACE_EVENT_NESTABLE_ASYNC_BEGIN2( | 548 TRACE_EVENT_NESTABLE_ASYNC_BEGIN2( |
| 547 "page-serialization", "SavingMhtmlJob", job, "url", | 549 "page-serialization", "SavingMhtmlJob", job, "url", |
| 548 web_contents->GetLastCommittedURL().possibly_invalid_spec(), | 550 web_contents->GetLastCommittedURL().possibly_invalid_spec(), |
| 549 "file", params.file_path.AsUTF8Unsafe()); | 551 "file", params.file_path.AsUTF8Unsafe()); |
| 550 | 552 |
| 551 BrowserThread::PostTaskAndReplyWithResult( | 553 PostTaskAndReplyWithResult( |
| 552 BrowserThread::FILE, FROM_HERE, | 554 GetDownloadTaskRunner().get(), FROM_HERE, |
| 553 base::Bind(&MHTMLGenerationManager::CreateFile, params.file_path), | 555 base::Bind(&MHTMLGenerationManager::CreateFile, params.file_path), |
| 554 base::Bind(&MHTMLGenerationManager::OnFileAvailable, | 556 base::Bind(&MHTMLGenerationManager::OnFileAvailable, |
| 555 base::Unretained(this), // Safe b/c |this| is a singleton. | 557 base::Unretained(this), // Safe b/c |this| is a singleton. |
| 556 job->id())); | 558 job->id())); |
| 557 } | 559 } |
| 558 | 560 |
| 559 void MHTMLGenerationManager::OnSerializeAsMHTMLResponse( | 561 void MHTMLGenerationManager::OnSerializeAsMHTMLResponse( |
| 560 RenderFrameHostImpl* sender, | 562 RenderFrameHostImpl* sender, |
| 561 int job_id, | 563 int job_id, |
| 562 MhtmlSaveStatus save_status, | 564 MhtmlSaveStatus save_status, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 589 return; | 591 return; |
| 590 } | 592 } |
| 591 | 593 |
| 592 // Otherwise report completion if the job is done. | 594 // Otherwise report completion if the job is done. |
| 593 if (job->IsDone()) | 595 if (job->IsDone()) |
| 594 JobFinished(job, MhtmlSaveStatus::SUCCESS); | 596 JobFinished(job, MhtmlSaveStatus::SUCCESS); |
| 595 } | 597 } |
| 596 | 598 |
| 597 // static | 599 // static |
| 598 base::File MHTMLGenerationManager::CreateFile(const base::FilePath& file_path) { | 600 base::File MHTMLGenerationManager::CreateFile(const base::FilePath& file_path) { |
| 599 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 601 DCHECK(GetDownloadTaskRunner()->RunsTasksInCurrentSequence()); |
| 600 | 602 |
| 601 // SECURITY NOTE: A file descriptor to the file created below will be passed | 603 // SECURITY NOTE: A file descriptor to the file created below will be passed |
| 602 // to multiple renderer processes which (in out-of-process iframes mode) can | 604 // to multiple renderer processes which (in out-of-process iframes mode) can |
| 603 // act on behalf of separate web principals. Therefore it is important to | 605 // act on behalf of separate web principals. Therefore it is important to |
| 604 // only allow writing to the file and forbid reading from the file (as this | 606 // only allow writing to the file and forbid reading from the file (as this |
| 605 // would allow reading content generated by other renderers / other web | 607 // would allow reading content generated by other renderers / other web |
| 606 // principals). | 608 // principals). |
| 607 uint32_t file_flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE; | 609 uint32_t file_flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE; |
| 608 | 610 |
| 609 base::File browser_file(file_path, file_flags); | 611 base::File browser_file(file_path, file_flags); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 690 return iter->second.get(); | 692 return iter->second.get(); |
| 691 } | 693 } |
| 692 | 694 |
| 693 void MHTMLGenerationManager::RenderProcessExited(Job* job) { | 695 void MHTMLGenerationManager::RenderProcessExited(Job* job) { |
| 694 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 696 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 695 DCHECK(job); | 697 DCHECK(job); |
| 696 JobFinished(job, MhtmlSaveStatus::RENDER_PROCESS_EXITED); | 698 JobFinished(job, MhtmlSaveStatus::RENDER_PROCESS_EXITED); |
| 697 } | 699 } |
| 698 | 700 |
| 699 } // namespace content | 701 } // namespace content |
| OLD | NEW |