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/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/time/time.h" | 21 #include "base/time/time.h" |
| 22 #include "base/trace_event/trace_event.h" | 22 #include "base/trace_event/trace_event.h" |
| 23 #include "content/browser/bad_message.h" | 23 #include "content/browser/bad_message.h" |
| 24 #include "content/browser/download/mhtml_extra_parts_impl.h" | |
| 24 #include "content/browser/frame_host/frame_tree_node.h" | 25 #include "content/browser/frame_host/frame_tree_node.h" |
| 25 #include "content/browser/frame_host/render_frame_host_impl.h" | 26 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 26 #include "content/common/frame_messages.h" | 27 #include "content/common/frame_messages.h" |
| 27 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 29 #include "content/public/browser/mhtml_extra_parts.h" | |
| 28 #include "content/public/browser/render_frame_host.h" | 30 #include "content/public/browser/render_frame_host.h" |
| 29 #include "content/public/browser/render_process_host.h" | 31 #include "content/public/browser/render_process_host.h" |
| 30 #include "content/public/browser/render_process_host_observer.h" | 32 #include "content/public/browser/render_process_host_observer.h" |
| 31 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
| 32 #include "content/public/common/mhtml_generation_params.h" | 34 #include "content/public/common/mhtml_generation_params.h" |
| 33 #include "net/base/mime_util.h" | 35 #include "net/base/mime_util.h" |
| 34 | 36 |
| 37 namespace { | |
| 38 const char kContentLocation[] = "Content-Location: "; | |
| 39 const char kContentType[] = "Content-Type: "; | |
| 40 int kInvalidFileSize = -1; | |
| 41 } // namespace | |
| 42 | |
| 35 namespace content { | 43 namespace content { |
| 36 | 44 |
| 37 // The class and all of its members live on the UI thread. Only static methods | 45 // The class and all of its members live on the UI thread. Only static methods |
| 38 // are executed on other threads. | 46 // are executed on other threads. |
| 39 class MHTMLGenerationManager::Job : public RenderProcessHostObserver { | 47 class MHTMLGenerationManager::Job : public RenderProcessHostObserver { |
| 40 public: | 48 public: |
| 41 Job(int job_id, | 49 Job(int job_id, |
| 42 WebContents* web_contents, | 50 WebContents* web_contents, |
| 43 const MHTMLGenerationParams& params, | 51 const MHTMLGenerationParams& params, |
| 44 const GenerateMHTMLCallback& callback); | 52 const GenerateMHTMLCallback& callback); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 void MarkAsFinished(); | 99 void MarkAsFinished(); |
| 92 | 100 |
| 93 void ReportRendererMainThreadTime(base::TimeDelta renderer_main_thread_time); | 101 void ReportRendererMainThreadTime(base::TimeDelta renderer_main_thread_time); |
| 94 | 102 |
| 95 private: | 103 private: |
| 96 // Writes the MHTML footer to the file and closes it. | 104 // Writes the MHTML footer to the file and closes it. |
| 97 // | 105 // |
| 98 // Note: The same |boundary| marker must be used for all "boundaries" -- in | 106 // Note: The same |boundary| marker must be used for all "boundaries" -- in |
| 99 // the header, parts and footer -- that belong to the same MHTML document (see | 107 // the header, parts and footer -- that belong to the same MHTML document (see |
| 100 // also rfc1341, section 7.2.1, "boundary" description). | 108 // also rfc1341, section 7.2.1, "boundary" description). |
| 101 static std::tuple<MhtmlSaveStatus, int64_t> CloseFileOnFileThread( | 109 static std::tuple<MhtmlSaveStatus, int64_t> FinalizeAndCloseFileOnFileThread( |
| 102 MhtmlSaveStatus save_status, | 110 MhtmlSaveStatus save_status, |
| 103 const std::string& boundary, | 111 const std::string& boundary, |
| 104 base::File file); | 112 base::File file, |
| 113 const MHTMLExtraPartsImpl* extra_parts); | |
| 105 void AddFrame(RenderFrameHost* render_frame_host); | 114 void AddFrame(RenderFrameHost* render_frame_host); |
| 106 | 115 |
| 116 // If we have any extra MHTML parts to write out, write them into the file | |
| 117 // while on the file thread. Returns true for success, or if there is no data | |
| 118 // to write. | |
| 119 static bool WriteExtraDataParts(const std::string& boundary, | |
| 120 base::File& file, | |
| 121 const MHTMLExtraPartsImpl* extra_parts); | |
| 122 | |
| 123 static bool WriteFooter(const std::string& boundary, base::File& file); | |
| 124 | |
| 125 static MhtmlSaveStatus CloseFileIfValid(base::File& file, int64_t& file_size); | |
| 126 | |
| 107 // Creates a new map with values (content ids) the same as in | 127 // Creates a new map with values (content ids) the same as in |
| 108 // |frame_tree_node_to_content_id_| map, but with the keys translated from | 128 // |frame_tree_node_to_content_id_| map, but with the keys translated from |
| 109 // frame_tree_node_id into a |site_instance|-specific routing_id. | 129 // frame_tree_node_id into a |site_instance|-specific routing_id. |
| 110 std::map<int, std::string> CreateFrameRoutingIdToContentId( | 130 std::map<int, std::string> CreateFrameRoutingIdToContentId( |
| 111 SiteInstance* site_instance); | 131 SiteInstance* site_instance); |
| 112 | 132 |
| 113 // Id used to map renderer responses to jobs. | 133 // Id used to map renderer responses to jobs. |
| 114 // See also MHTMLGenerationManager::id_to_job_ map. | 134 // See also MHTMLGenerationManager::id_to_job_ map. |
| 115 const int job_id_; | 135 const int job_id_; |
| 116 | 136 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 149 std::string salt_; | 169 std::string salt_; |
| 150 | 170 |
| 151 // The callback to call once generation is complete. | 171 // The callback to call once generation is complete. |
| 152 const GenerateMHTMLCallback callback_; | 172 const GenerateMHTMLCallback callback_; |
| 153 | 173 |
| 154 // Whether the job is finished (set to true only for the short duration of | 174 // Whether the job is finished (set to true only for the short duration of |
| 155 // time between MHTMLGenerationManager::JobFinished is called and the job is | 175 // time between MHTMLGenerationManager::JobFinished is called and the job is |
| 156 // destroyed by MHTMLGenerationManager::OnFileClosed). | 176 // destroyed by MHTMLGenerationManager::OnFileClosed). |
| 157 bool is_finished_; | 177 bool is_finished_; |
| 158 | 178 |
| 179 // Any extra data parts that should be emitted into the output MHTML. | |
| 180 MHTMLExtraPartsImpl* extra_parts_; | |
| 181 | |
| 159 // RAII helper for registering this Job as a RenderProcessHost observer. | 182 // RAII helper for registering this Job as a RenderProcessHost observer. |
| 160 ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job> | 183 ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job> |
| 161 observed_renderer_process_host_; | 184 observed_renderer_process_host_; |
| 162 | 185 |
| 163 DISALLOW_COPY_AND_ASSIGN(Job); | 186 DISALLOW_COPY_AND_ASSIGN(Job); |
| 164 }; | 187 }; |
| 165 | 188 |
| 166 MHTMLGenerationManager::Job::Job(int job_id, | 189 MHTMLGenerationManager::Job::Job(int job_id, |
| 167 WebContents* web_contents, | 190 WebContents* web_contents, |
| 168 const MHTMLGenerationParams& params, | 191 const MHTMLGenerationParams& params, |
| 169 const GenerateMHTMLCallback& callback) | 192 const GenerateMHTMLCallback& callback) |
| 170 : job_id_(job_id), | 193 : job_id_(job_id), |
| 171 creation_time_(base::TimeTicks::Now()), | 194 creation_time_(base::TimeTicks::Now()), |
| 172 params_(params), | 195 params_(params), |
| 173 frame_tree_node_id_of_busy_frame_(FrameTreeNode::kFrameTreeNodeInvalidId), | 196 frame_tree_node_id_of_busy_frame_(FrameTreeNode::kFrameTreeNodeInvalidId), |
| 174 mhtml_boundary_marker_(net::GenerateMimeMultipartBoundary()), | 197 mhtml_boundary_marker_(net::GenerateMimeMultipartBoundary()), |
| 175 salt_(base::GenerateGUID()), | 198 salt_(base::GenerateGUID()), |
| 176 callback_(callback), | 199 callback_(callback), |
| 177 is_finished_(false), | 200 is_finished_(false), |
| 178 observed_renderer_process_host_(this) { | 201 observed_renderer_process_host_(this) { |
| 179 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 202 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 180 web_contents->ForEachFrame(base::Bind( | 203 web_contents->ForEachFrame(base::Bind( |
| 181 &MHTMLGenerationManager::Job::AddFrame, | 204 &MHTMLGenerationManager::Job::AddFrame, |
| 182 base::Unretained(this))); // Safe because ForEachFrame is synchronous. | 205 base::Unretained(this))); // Safe because ForEachFrame is synchronous. |
| 183 | 206 |
| 184 // Main frame needs to be processed first. | 207 // Main frame needs to be processed first. |
| 185 DCHECK(!pending_frame_tree_node_ids_.empty()); | 208 DCHECK(!pending_frame_tree_node_ids_.empty()); |
| 186 DCHECK(FrameTreeNode::GloballyFindByID(pending_frame_tree_node_ids_.front()) | 209 DCHECK(FrameTreeNode::GloballyFindByID(pending_frame_tree_node_ids_.front()) |
| 187 ->parent() == nullptr); | 210 ->parent() == nullptr); |
| 211 | |
| 212 // Save off any extra data. | |
| 213 extra_parts_ = static_cast<MHTMLExtraPartsImpl*>( | |
| 214 MHTMLExtraParts::FromWebContents(web_contents)); | |
| 188 } | 215 } |
| 189 | 216 |
| 190 MHTMLGenerationManager::Job::~Job() { | 217 MHTMLGenerationManager::Job::~Job() { |
| 191 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 218 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 192 } | 219 } |
| 193 | 220 |
| 194 std::map<int, std::string> | 221 std::map<int, std::string> |
| 195 MHTMLGenerationManager::Job::CreateFrameRoutingIdToContentId( | 222 MHTMLGenerationManager::Job::CreateFrameRoutingIdToContentId( |
| 196 SiteInstance* site_instance) { | 223 SiteInstance* site_instance) { |
| 197 std::map<int, std::string> result; | 224 std::map<int, std::string> result; |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 345 if (save_status == MhtmlSaveStatus::SUCCESS) | 372 if (save_status == MhtmlSaveStatus::SUCCESS) |
| 346 save_status = MhtmlSaveStatus::FILE_WRITTING_ERROR; | 373 save_status = MhtmlSaveStatus::FILE_WRITTING_ERROR; |
| 347 callback.Run(std::make_tuple(save_status, -1)); | 374 callback.Run(std::make_tuple(save_status, -1)); |
| 348 return; | 375 return; |
| 349 } | 376 } |
| 350 | 377 |
| 351 // If no previous error occurred the boundary should be sent. | 378 // If no previous error occurred the boundary should be sent. |
| 352 BrowserThread::PostTaskAndReplyWithResult( | 379 BrowserThread::PostTaskAndReplyWithResult( |
| 353 BrowserThread::FILE, FROM_HERE, | 380 BrowserThread::FILE, FROM_HERE, |
| 354 base::Bind( | 381 base::Bind( |
| 355 &MHTMLGenerationManager::Job::CloseFileOnFileThread, save_status, | 382 &MHTMLGenerationManager::Job::FinalizeAndCloseFileOnFileThread, |
| 383 save_status, | |
| 356 (save_status == MhtmlSaveStatus::SUCCESS ? mhtml_boundary_marker_ | 384 (save_status == MhtmlSaveStatus::SUCCESS ? mhtml_boundary_marker_ |
| 357 : std::string()), | 385 : std::string()), |
| 358 base::Passed(&browser_file_)), | 386 base::Passed(&browser_file_), extra_parts_), |
| 359 callback); | 387 callback); |
| 360 } | 388 } |
| 361 | 389 |
| 362 bool MHTMLGenerationManager::Job::IsMessageFromFrameExpected( | 390 bool MHTMLGenerationManager::Job::IsMessageFromFrameExpected( |
| 363 RenderFrameHostImpl* sender) { | 391 RenderFrameHostImpl* sender) { |
| 364 int sender_id = sender->frame_tree_node()->frame_tree_node_id(); | 392 int sender_id = sender->frame_tree_node()->frame_tree_node_id(); |
| 365 if (sender_id != frame_tree_node_id_of_busy_frame_) | 393 if (sender_id != frame_tree_node_id_of_busy_frame_) |
| 366 return false; | 394 return false; |
| 367 | 395 |
| 368 // We only expect one message per frame - let's make sure subsequent messages | 396 // We only expect one message per frame - let's make sure subsequent messages |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 394 | 422 |
| 395 // Report success if all frames have been processed. | 423 // Report success if all frames have been processed. |
| 396 if (pending_frame_tree_node_ids_.empty()) | 424 if (pending_frame_tree_node_ids_.empty()) |
| 397 return MhtmlSaveStatus::SUCCESS; | 425 return MhtmlSaveStatus::SUCCESS; |
| 398 | 426 |
| 399 return SendToNextRenderFrame(); | 427 return SendToNextRenderFrame(); |
| 400 } | 428 } |
| 401 | 429 |
| 402 // static | 430 // static |
| 403 std::tuple<MhtmlSaveStatus, int64_t> | 431 std::tuple<MhtmlSaveStatus, int64_t> |
| 404 MHTMLGenerationManager::Job::CloseFileOnFileThread(MhtmlSaveStatus save_status, | 432 MHTMLGenerationManager::Job::FinalizeAndCloseFileOnFileThread( |
| 405 const std::string& boundary, | 433 MhtmlSaveStatus save_status, |
| 406 base::File file) { | 434 const std::string& boundary, |
| 435 base::File file, | |
| 436 const MHTMLExtraPartsImpl* extra_parts) { | |
| 407 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 437 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 408 | 438 |
| 409 // If no previous error occurred the boundary should have been provided. | 439 // If no previous error occurred the boundary should have been provided. |
| 410 if (save_status == MhtmlSaveStatus::SUCCESS) { | 440 if (save_status == MhtmlSaveStatus::SUCCESS) { |
| 411 TRACE_EVENT0("page-serialization", | 441 TRACE_EVENT0("page-serialization", |
| 412 "MHTMLGenerationManager::Job MHTML footer writing"); | 442 "MHTMLGenerationManager::Job MHTML footer writing"); |
| 413 DCHECK(!boundary.empty()); | 443 DCHECK(!boundary.empty()); |
| 414 std::string footer = base::StringPrintf("--%s--\r\n", boundary.c_str()); | 444 |
| 415 DCHECK(base::IsStringASCII(footer)); | 445 // Write the extra data into a part of its own, if we have any. |
| 416 if (file.WriteAtCurrentPos(footer.data(), footer.size()) < 0) | 446 if (!WriteExtraDataParts(boundary, file, extra_parts)) { |
| 417 save_status = MhtmlSaveStatus::FILE_WRITTING_ERROR; | 447 save_status = MhtmlSaveStatus::FILE_WRITTING_ERROR; |
| 448 return std::make_tuple(save_status, kInvalidFileSize); | |
| 449 } | |
| 450 | |
| 451 // Write out the footer at the bottom of the file. | |
| 452 if (!WriteFooter(boundary, file)) { | |
| 453 save_status = MhtmlSaveStatus::FILE_WRITTING_ERROR; | |
| 454 return std::make_tuple(save_status, kInvalidFileSize); | |
| 455 } | |
| 418 } | 456 } |
| 419 | 457 |
| 420 // If the file is still valid try to close it. Only update the status if that | 458 // If the file is still valid try to close it. Only update the status if that |
| 421 // won't hide an earlier error. | 459 // won't hide an earlier error. |
| 422 int64_t file_size = -1; | 460 int64_t file_size = kInvalidFileSize; |
| 423 if (file.IsValid()) { | 461 save_status = CloseFileIfValid(file, file_size); |
| 424 file_size = file.GetLength(); | |
| 425 file.Close(); | |
| 426 } else if (save_status == MhtmlSaveStatus::SUCCESS) { | |
| 427 save_status = MhtmlSaveStatus::FILE_CLOSING_ERROR; | |
| 428 } | |
| 429 | 462 |
| 430 return std::make_tuple(save_status, file_size); | 463 return std::make_tuple(save_status, file_size); |
| 431 } | 464 } |
| 432 | 465 |
| 466 // static | |
| 467 bool MHTMLGenerationManager::Job::WriteExtraDataParts( | |
| 468 const std::string& boundary, | |
| 469 base::File& file, | |
| 470 const MHTMLExtraPartsImpl* extra_parts) { | |
| 471 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
| 472 // Don't write an extra data part if there is none. | |
| 473 if (extra_parts == nullptr) | |
| 474 return true; | |
| 475 | |
| 476 const std::vector<MHTMLExtraDataPart>& extra_data_parts(extra_parts->parts()); | |
| 477 if (extra_data_parts.empty()) | |
| 478 return true; | |
| 479 | |
| 480 std::string serialized_extra_data_parts; | |
| 481 | |
| 482 // For each extra part, serialize that part and add to our accumulator | |
| 483 // string. | |
| 484 for (auto part : extra_data_parts) { | |
| 485 // Write a newline, then a boundary, another newline, then the content | |
| 486 // location, another newline, the content type, another newline, the extra | |
| 487 // data string, and end with a newline. | |
| 488 std::string serialized_extra_data_part = base::StringPrintf( | |
| 489 "--%s\r\n%s%s\r\n%s%s\r\n%s\r\n", boundary.c_str(), kContentLocation, | |
| 490 part.content_location.c_str(), kContentType, part.content_type.c_str(), | |
| 491 part.body.c_str()); | |
| 492 DCHECK(base::IsStringASCII(serialized_extra_data_part)); | |
| 493 | |
| 494 serialized_extra_data_parts += serialized_extra_data_part; | |
| 495 } | |
| 496 | |
| 497 // Write the string into the file. Returns false if we failed the write. | |
| 498 return (file.WriteAtCurrentPos(serialized_extra_data_parts.data(), | |
| 499 serialized_extra_data_parts.size()) >= 0); | |
| 500 } | |
| 501 | |
| 502 // static | |
| 503 bool MHTMLGenerationManager::Job::WriteFooter(const std::string& boundary, | |
| 504 base::File& file) { | |
| 505 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
| 506 std::string footer = base::StringPrintf("--%s--\r\n", boundary.c_str()); | |
| 507 DCHECK(base::IsStringASCII(footer)); | |
| 508 if (file.WriteAtCurrentPos(footer.data(), footer.size()) < 0) | |
|
David Trainor- moved to gerrit
2017/03/30 05:26:40
Keep this consistent with the return style above?
Pete Williamson
2017/03/31 00:29:49
Done.
| |
| 509 return false; | |
| 510 | |
| 511 return true; | |
| 512 } | |
| 513 | |
| 514 // static | |
| 515 MhtmlSaveStatus MHTMLGenerationManager::Job::CloseFileIfValid( | |
| 516 base::File& file, | |
| 517 int64_t& file_size) { | |
| 518 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
| 519 if (file.IsValid()) { | |
| 520 file_size = file.GetLength(); | |
| 521 file.Close(); | |
| 522 return MhtmlSaveStatus::SUCCESS; | |
| 523 } | |
| 524 | |
| 525 return MhtmlSaveStatus::FILE_CLOSING_ERROR; | |
| 526 } | |
| 527 | |
| 433 MHTMLGenerationManager* MHTMLGenerationManager::GetInstance() { | 528 MHTMLGenerationManager* MHTMLGenerationManager::GetInstance() { |
| 434 return base::Singleton<MHTMLGenerationManager>::get(); | 529 return base::Singleton<MHTMLGenerationManager>::get(); |
| 435 } | 530 } |
| 436 | 531 |
| 437 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {} | 532 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {} |
| 438 | 533 |
| 439 MHTMLGenerationManager::~MHTMLGenerationManager() { | 534 MHTMLGenerationManager::~MHTMLGenerationManager() { |
| 440 } | 535 } |
| 441 | 536 |
| 442 void MHTMLGenerationManager::SaveMHTML(WebContents* web_contents, | 537 void MHTMLGenerationManager::SaveMHTML(WebContents* web_contents, |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 592 return iter->second.get(); | 687 return iter->second.get(); |
| 593 } | 688 } |
| 594 | 689 |
| 595 void MHTMLGenerationManager::RenderProcessExited(Job* job) { | 690 void MHTMLGenerationManager::RenderProcessExited(Job* job) { |
| 596 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 691 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 597 DCHECK(job); | 692 DCHECK(job); |
| 598 JobFinished(job, MhtmlSaveStatus::RENDER_PROCESS_EXITED); | 693 JobFinished(job, MhtmlSaveStatus::RENDER_PROCESS_EXITED); |
| 599 } | 694 } |
| 600 | 695 |
| 601 } // namespace content | 696 } // namespace content |
| OLD | NEW |