Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Side by Side Diff: content/browser/download/mhtml_generation_manager.cc

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

Powered by Google App Engine
This is Rietveld 408576698