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_data_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_data.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 } // namespace | |
| 41 | |
| 35 namespace content { | 42 namespace content { |
| 36 | 43 |
| 37 // The class and all of its members live on the UI thread. Only static methods | 44 // The class and all of its members live on the UI thread. Only static methods |
| 38 // are executed on other threads. | 45 // are executed on other threads. |
| 39 class MHTMLGenerationManager::Job : public RenderProcessHostObserver { | 46 class MHTMLGenerationManager::Job : public RenderProcessHostObserver { |
| 40 public: | 47 public: |
| 41 Job(int job_id, | 48 Job(int job_id, |
| 42 WebContents* web_contents, | 49 WebContents* web_contents, |
| 43 const MHTMLGenerationParams& params, | 50 const MHTMLGenerationParams& params, |
| 44 const GenerateMHTMLCallback& callback); | 51 const GenerateMHTMLCallback& callback); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 94 | 101 |
| 95 private: | 102 private: |
| 96 // Writes the MHTML footer to the file and closes it. | 103 // Writes the MHTML footer to the file and closes it. |
| 97 // | 104 // |
| 98 // Note: The same |boundary| marker must be used for all "boundaries" -- in | 105 // 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 | 106 // the header, parts and footer -- that belong to the same MHTML document (see |
| 100 // also rfc1341, section 7.2.1, "boundary" description). | 107 // also rfc1341, section 7.2.1, "boundary" description). |
| 101 static std::tuple<MhtmlSaveStatus, int64_t> CloseFileOnFileThread( | 108 static std::tuple<MhtmlSaveStatus, int64_t> CloseFileOnFileThread( |
| 102 MhtmlSaveStatus save_status, | 109 MhtmlSaveStatus save_status, |
| 103 const std::string& boundary, | 110 const std::string& boundary, |
| 104 base::File file); | 111 base::File file, |
| 112 const MHTMLExtraDataImpl* extra_data); | |
| 105 void AddFrame(RenderFrameHost* render_frame_host); | 113 void AddFrame(RenderFrameHost* render_frame_host); |
| 106 | 114 |
| 115 // If we have any extra MHTML parts to write out, write them into the file | |
| 116 // while on the file thread. Returns true for success. | |
|
fgorski
2017/03/28 17:01:12
true for success or no data to write.
Pete Williamson
2017/03/28 18:01:58
Done.
| |
| 117 static bool WriteExtraDataPartsOnFileThread( | |
| 118 const std::string& boundary, | |
| 119 base::File& file, | |
| 120 const MHTMLExtraDataImpl* extra_data); | |
| 121 | |
| 107 // Creates a new map with values (content ids) the same as in | 122 // 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 | 123 // |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. | 124 // frame_tree_node_id into a |site_instance|-specific routing_id. |
| 110 std::map<int, std::string> CreateFrameRoutingIdToContentId( | 125 std::map<int, std::string> CreateFrameRoutingIdToContentId( |
| 111 SiteInstance* site_instance); | 126 SiteInstance* site_instance); |
| 112 | 127 |
| 113 // Id used to map renderer responses to jobs. | 128 // Id used to map renderer responses to jobs. |
| 114 // See also MHTMLGenerationManager::id_to_job_ map. | 129 // See also MHTMLGenerationManager::id_to_job_ map. |
| 115 const int job_id_; | 130 const int job_id_; |
| 116 | 131 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 149 std::string salt_; | 164 std::string salt_; |
| 150 | 165 |
| 151 // The callback to call once generation is complete. | 166 // The callback to call once generation is complete. |
| 152 const GenerateMHTMLCallback callback_; | 167 const GenerateMHTMLCallback callback_; |
| 153 | 168 |
| 154 // Whether the job is finished (set to true only for the short duration of | 169 // 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 | 170 // time between MHTMLGenerationManager::JobFinished is called and the job is |
| 156 // destroyed by MHTMLGenerationManager::OnFileClosed). | 171 // destroyed by MHTMLGenerationManager::OnFileClosed). |
| 157 bool is_finished_; | 172 bool is_finished_; |
| 158 | 173 |
| 174 // Any extra data parts that should be emitted into the output MHTML. | |
| 175 MHTMLExtraDataImpl* extra_data_; | |
| 176 | |
| 159 // RAII helper for registering this Job as a RenderProcessHost observer. | 177 // RAII helper for registering this Job as a RenderProcessHost observer. |
| 160 ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job> | 178 ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job> |
| 161 observed_renderer_process_host_; | 179 observed_renderer_process_host_; |
| 162 | 180 |
| 163 DISALLOW_COPY_AND_ASSIGN(Job); | 181 DISALLOW_COPY_AND_ASSIGN(Job); |
| 164 }; | 182 }; |
| 165 | 183 |
| 166 MHTMLGenerationManager::Job::Job(int job_id, | 184 MHTMLGenerationManager::Job::Job(int job_id, |
| 167 WebContents* web_contents, | 185 WebContents* web_contents, |
| 168 const MHTMLGenerationParams& params, | 186 const MHTMLGenerationParams& params, |
| 169 const GenerateMHTMLCallback& callback) | 187 const GenerateMHTMLCallback& callback) |
| 170 : job_id_(job_id), | 188 : job_id_(job_id), |
| 171 creation_time_(base::TimeTicks::Now()), | 189 creation_time_(base::TimeTicks::Now()), |
| 172 params_(params), | 190 params_(params), |
| 173 frame_tree_node_id_of_busy_frame_(FrameTreeNode::kFrameTreeNodeInvalidId), | 191 frame_tree_node_id_of_busy_frame_(FrameTreeNode::kFrameTreeNodeInvalidId), |
| 174 mhtml_boundary_marker_(net::GenerateMimeMultipartBoundary()), | 192 mhtml_boundary_marker_(net::GenerateMimeMultipartBoundary()), |
| 175 salt_(base::GenerateGUID()), | 193 salt_(base::GenerateGUID()), |
| 176 callback_(callback), | 194 callback_(callback), |
| 177 is_finished_(false), | 195 is_finished_(false), |
| 178 observed_renderer_process_host_(this) { | 196 observed_renderer_process_host_(this) { |
| 179 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 197 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 180 web_contents->ForEachFrame(base::Bind( | 198 web_contents->ForEachFrame(base::Bind( |
| 181 &MHTMLGenerationManager::Job::AddFrame, | 199 &MHTMLGenerationManager::Job::AddFrame, |
| 182 base::Unretained(this))); // Safe because ForEachFrame is synchronous. | 200 base::Unretained(this))); // Safe because ForEachFrame is synchronous. |
| 183 | 201 |
| 184 // Main frame needs to be processed first. | 202 // Main frame needs to be processed first. |
| 185 DCHECK(!pending_frame_tree_node_ids_.empty()); | 203 DCHECK(!pending_frame_tree_node_ids_.empty()); |
| 186 DCHECK(FrameTreeNode::GloballyFindByID(pending_frame_tree_node_ids_.front()) | 204 DCHECK(FrameTreeNode::GloballyFindByID(pending_frame_tree_node_ids_.front()) |
| 187 ->parent() == nullptr); | 205 ->parent() == nullptr); |
| 206 | |
| 207 // Save off any extra data. | |
| 208 extra_data_ = static_cast<MHTMLExtraDataImpl*>( | |
| 209 MHTMLExtraData::FromWebContents(web_contents)); | |
| 188 } | 210 } |
| 189 | 211 |
| 190 MHTMLGenerationManager::Job::~Job() { | 212 MHTMLGenerationManager::Job::~Job() { |
| 191 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 213 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 192 } | 214 } |
| 193 | 215 |
| 194 std::map<int, std::string> | 216 std::map<int, std::string> |
| 195 MHTMLGenerationManager::Job::CreateFrameRoutingIdToContentId( | 217 MHTMLGenerationManager::Job::CreateFrameRoutingIdToContentId( |
| 196 SiteInstance* site_instance) { | 218 SiteInstance* site_instance) { |
| 197 std::map<int, std::string> result; | 219 std::map<int, std::string> result; |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 348 return; | 370 return; |
| 349 } | 371 } |
| 350 | 372 |
| 351 // If no previous error occurred the boundary should be sent. | 373 // If no previous error occurred the boundary should be sent. |
| 352 BrowserThread::PostTaskAndReplyWithResult( | 374 BrowserThread::PostTaskAndReplyWithResult( |
| 353 BrowserThread::FILE, FROM_HERE, | 375 BrowserThread::FILE, FROM_HERE, |
| 354 base::Bind( | 376 base::Bind( |
| 355 &MHTMLGenerationManager::Job::CloseFileOnFileThread, save_status, | 377 &MHTMLGenerationManager::Job::CloseFileOnFileThread, save_status, |
| 356 (save_status == MhtmlSaveStatus::SUCCESS ? mhtml_boundary_marker_ | 378 (save_status == MhtmlSaveStatus::SUCCESS ? mhtml_boundary_marker_ |
| 357 : std::string()), | 379 : std::string()), |
| 358 base::Passed(&browser_file_)), | 380 base::Passed(&browser_file_), extra_data_), |
| 359 callback); | 381 callback); |
| 360 } | 382 } |
| 361 | 383 |
| 362 bool MHTMLGenerationManager::Job::IsMessageFromFrameExpected( | 384 bool MHTMLGenerationManager::Job::IsMessageFromFrameExpected( |
| 363 RenderFrameHostImpl* sender) { | 385 RenderFrameHostImpl* sender) { |
| 364 int sender_id = sender->frame_tree_node()->frame_tree_node_id(); | 386 int sender_id = sender->frame_tree_node()->frame_tree_node_id(); |
| 365 if (sender_id != frame_tree_node_id_of_busy_frame_) | 387 if (sender_id != frame_tree_node_id_of_busy_frame_) |
| 366 return false; | 388 return false; |
| 367 | 389 |
| 368 // We only expect one message per frame - let's make sure subsequent messages | 390 // We only expect one message per frame - let's make sure subsequent messages |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 394 | 416 |
| 395 // Report success if all frames have been processed. | 417 // Report success if all frames have been processed. |
| 396 if (pending_frame_tree_node_ids_.empty()) | 418 if (pending_frame_tree_node_ids_.empty()) |
| 397 return MhtmlSaveStatus::SUCCESS; | 419 return MhtmlSaveStatus::SUCCESS; |
| 398 | 420 |
| 399 return SendToNextRenderFrame(); | 421 return SendToNextRenderFrame(); |
| 400 } | 422 } |
| 401 | 423 |
| 402 // static | 424 // static |
| 403 std::tuple<MhtmlSaveStatus, int64_t> | 425 std::tuple<MhtmlSaveStatus, int64_t> |
| 404 MHTMLGenerationManager::Job::CloseFileOnFileThread(MhtmlSaveStatus save_status, | 426 MHTMLGenerationManager::Job::CloseFileOnFileThread( |
| 405 const std::string& boundary, | 427 MhtmlSaveStatus save_status, |
| 406 base::File file) { | 428 const std::string& boundary, |
| 429 base::File file, | |
| 430 const MHTMLExtraDataImpl* extra_data) { | |
| 407 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 431 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 432 int64_t file_size = -1; | |
| 408 | 433 |
| 409 // If no previous error occurred the boundary should have been provided. | 434 // If no previous error occurred the boundary should have been provided. |
| 410 if (save_status == MhtmlSaveStatus::SUCCESS) { | 435 if (save_status == MhtmlSaveStatus::SUCCESS) { |
| 411 TRACE_EVENT0("page-serialization", | 436 TRACE_EVENT0("page-serialization", |
| 412 "MHTMLGenerationManager::Job MHTML footer writing"); | 437 "MHTMLGenerationManager::Job MHTML footer writing"); |
| 413 DCHECK(!boundary.empty()); | 438 DCHECK(!boundary.empty()); |
| 439 | |
| 440 // Write the extra data into a part of its own, if we have any. | |
| 441 if (!WriteExtraDataPartsOnFileThread(boundary, file, extra_data)) { | |
| 442 save_status = MhtmlSaveStatus::FILE_WRITTING_ERROR; | |
| 443 return std::make_tuple(save_status, file_size); | |
|
fgorski
2017/03/28 17:01:12
-1 would work better here. (Or some constant). Thi
Pete Williamson
2017/03/28 18:01:58
Done.
| |
| 444 } | |
| 445 | |
| 414 std::string footer = base::StringPrintf("--%s--\r\n", boundary.c_str()); | 446 std::string footer = base::StringPrintf("--%s--\r\n", boundary.c_str()); |
| 415 DCHECK(base::IsStringASCII(footer)); | 447 DCHECK(base::IsStringASCII(footer)); |
| 416 if (file.WriteAtCurrentPos(footer.data(), footer.size()) < 0) | 448 if (file.WriteAtCurrentPos(footer.data(), footer.size()) < 0) { |
| 417 save_status = MhtmlSaveStatus::FILE_WRITTING_ERROR; | 449 save_status = MhtmlSaveStatus::FILE_WRITTING_ERROR; |
| 450 return std::make_tuple(save_status, file_size); | |
| 451 } | |
| 418 } | 452 } |
| 419 | 453 |
| 420 // If the file is still valid try to close it. Only update the status if that | 454 // If the file is still valid try to close it. Only update the status if that |
| 421 // won't hide an earlier error. | 455 // won't hide an earlier error. |
| 422 int64_t file_size = -1; | |
| 423 if (file.IsValid()) { | 456 if (file.IsValid()) { |
| 424 file_size = file.GetLength(); | 457 file_size = file.GetLength(); |
| 425 file.Close(); | 458 file.Close(); |
| 426 } else if (save_status == MhtmlSaveStatus::SUCCESS) { | 459 } else if (save_status == MhtmlSaveStatus::SUCCESS) { |
| 427 save_status = MhtmlSaveStatus::FILE_CLOSING_ERROR; | 460 save_status = MhtmlSaveStatus::FILE_CLOSING_ERROR; |
| 428 } | 461 } |
| 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::WriteExtraDataPartsOnFileThread( | |
| 468 const std::string& boundary, | |
| 469 base::File& file, | |
| 470 const MHTMLExtraDataImpl* extra_data) { | |
|
fgorski
2017/03/28 17:01:12
DCHECK thread.
Pete Williamson
2017/03/28 18:01:58
Done.
| |
| 471 // Don't write an extra data part if there is none. | |
| 472 if (extra_data == nullptr) | |
| 473 return true; | |
| 474 | |
| 475 const std::vector<MHTMLExtraDataPart>& extra_parts(extra_data->parts()); | |
| 476 if (extra_parts.empty()) | |
| 477 return true; | |
| 478 | |
| 479 std::string serialized_extra_data_parts; | |
| 480 | |
| 481 // For each extra part, serialize that part and add to our accumulator | |
| 482 // string. | |
| 483 for (auto part : extra_parts) { | |
| 484 // Write a newline, then a boundary, another newline, then the content | |
| 485 // location, another newline, the content type, another newline, the extra | |
| 486 // data string, and end with a newline. | |
| 487 std::string serialized_extra_data_part = base::StringPrintf( | |
| 488 "--%s--\r\n%s%s\r\n%s%s\r\n%s\r\n", boundary.c_str(), kContentLocation, | |
| 489 part.content_location.c_str(), kContentType, part.content_type.c_str(), | |
| 490 part.body.c_str()); | |
| 491 DCHECK(base::IsStringASCII(serialized_extra_data_part)); | |
| 492 | |
| 493 serialized_extra_data_parts += serialized_extra_data_part; | |
| 494 } | |
| 495 | |
| 496 // Write the string into the file. Returns false if we failed the write. | |
| 497 return (file.WriteAtCurrentPos(serialized_extra_data_parts.data(), | |
| 498 serialized_extra_data_parts.size()) >= 0); | |
| 499 } | |
| 500 | |
| 433 MHTMLGenerationManager* MHTMLGenerationManager::GetInstance() { | 501 MHTMLGenerationManager* MHTMLGenerationManager::GetInstance() { |
| 434 return base::Singleton<MHTMLGenerationManager>::get(); | 502 return base::Singleton<MHTMLGenerationManager>::get(); |
| 435 } | 503 } |
| 436 | 504 |
| 437 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {} | 505 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {} |
| 438 | 506 |
| 439 MHTMLGenerationManager::~MHTMLGenerationManager() { | 507 MHTMLGenerationManager::~MHTMLGenerationManager() { |
| 440 } | 508 } |
| 441 | 509 |
| 442 void MHTMLGenerationManager::SaveMHTML(WebContents* web_contents, | 510 void MHTMLGenerationManager::SaveMHTML(WebContents* web_contents, |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 592 return iter->second.get(); | 660 return iter->second.get(); |
| 593 } | 661 } |
| 594 | 662 |
| 595 void MHTMLGenerationManager::RenderProcessExited(Job* job) { | 663 void MHTMLGenerationManager::RenderProcessExited(Job* job) { |
| 596 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 664 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 597 DCHECK(job); | 665 DCHECK(job); |
| 598 JobFinished(job, MhtmlSaveStatus::RENDER_PROCESS_EXITED); | 666 JobFinished(job, MhtmlSaveStatus::RENDER_PROCESS_EXITED); |
| 599 } | 667 } |
| 600 | 668 |
| 601 } // namespace content | 669 } // namespace content |
| OLD | NEW |