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

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: Don't forget the new .h file 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
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.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"
28 #include "content/public/browser/render_frame_host.h" 29 #include "content/public/browser/render_frame_host.h"
29 #include "content/public/browser/render_process_host.h" 30 #include "content/public/browser/render_process_host.h"
30 #include "content/public/browser/render_process_host_observer.h" 31 #include "content/public/browser/render_process_host_observer.h"
31 #include "content/public/browser/web_contents.h" 32 #include "content/public/browser/web_contents.h"
32 #include "content/public/common/mhtml_generation_params.h" 33 #include "content/public/common/mhtml_generation_params.h"
33 #include "net/base/mime_util.h" 34 #include "net/base/mime_util.h"
34 35
36 namespace {
37 const char kContentLocation[] = "Content-Location: ";
38 } // namespace
39
35 namespace content { 40 namespace content {
36 41
37 // The class and all of its members live on the UI thread. Only static methods 42 // The class and all of its members live on the UI thread. Only static methods
38 // are executed on other threads. 43 // are executed on other threads.
39 class MHTMLGenerationManager::Job : public RenderProcessHostObserver { 44 class MHTMLGenerationManager::Job : public RenderProcessHostObserver {
40 public: 45 public:
41 Job(int job_id, 46 Job(int job_id,
42 WebContents* web_contents, 47 WebContents* web_contents,
43 const MHTMLGenerationParams& params, 48 const MHTMLGenerationParams& params,
44 const GenerateMHTMLCallback& callback); 49 const GenerateMHTMLCallback& callback);
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 99
95 private: 100 private:
96 // Writes the MHTML footer to the file and closes it. 101 // Writes the MHTML footer to the file and closes it.
97 // 102 //
98 // Note: The same |boundary| marker must be used for all "boundaries" -- in 103 // 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 104 // the header, parts and footer -- that belong to the same MHTML document (see
100 // also rfc1341, section 7.2.1, "boundary" description). 105 // also rfc1341, section 7.2.1, "boundary" description).
101 static std::tuple<MhtmlSaveStatus, int64_t> CloseFileOnFileThread( 106 static std::tuple<MhtmlSaveStatus, int64_t> CloseFileOnFileThread(
102 MhtmlSaveStatus save_status, 107 MhtmlSaveStatus save_status,
103 const std::string& boundary, 108 const std::string& boundary,
104 base::File file); 109 base::File file,
110 const std::string& extra_data);
105 void AddFrame(RenderFrameHost* render_frame_host); 111 void AddFrame(RenderFrameHost* render_frame_host);
106 112
113 // If we have any extra data sections to write out, write them into the file
114 // while on the file thread. Returns true for success.
115 static bool WriteExtraDataSectionOnFileThread(const std::string& boundary,
116 base::File& file,
117 const std::string& extra_data);
118
107 // Creates a new map with values (content ids) the same as in 119 // 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 120 // |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. 121 // frame_tree_node_id into a |site_instance|-specific routing_id.
110 std::map<int, std::string> CreateFrameRoutingIdToContentId( 122 std::map<int, std::string> CreateFrameRoutingIdToContentId(
111 SiteInstance* site_instance); 123 SiteInstance* site_instance);
112 124
113 // Id used to map renderer responses to jobs. 125 // Id used to map renderer responses to jobs.
114 // See also MHTMLGenerationManager::id_to_job_ map. 126 // See also MHTMLGenerationManager::id_to_job_ map.
115 const int job_id_; 127 const int job_id_;
116 128
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 std::string salt_; 161 std::string salt_;
150 162
151 // The callback to call once generation is complete. 163 // The callback to call once generation is complete.
152 const GenerateMHTMLCallback callback_; 164 const GenerateMHTMLCallback callback_;
153 165
154 // Whether the job is finished (set to true only for the short duration of 166 // 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 167 // time between MHTMLGenerationManager::JobFinished is called and the job is
156 // destroyed by MHTMLGenerationManager::OnFileClosed). 168 // destroyed by MHTMLGenerationManager::OnFileClosed).
157 bool is_finished_; 169 bool is_finished_;
158 170
171 // Any extra data section that should be emitted into the output MHTML.
172 std::string extra_data_;
173
159 // RAII helper for registering this Job as a RenderProcessHost observer. 174 // RAII helper for registering this Job as a RenderProcessHost observer.
160 ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job> 175 ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job>
161 observed_renderer_process_host_; 176 observed_renderer_process_host_;
162 177
163 DISALLOW_COPY_AND_ASSIGN(Job); 178 DISALLOW_COPY_AND_ASSIGN(Job);
164 }; 179 };
165 180
166 MHTMLGenerationManager::Job::Job(int job_id, 181 MHTMLGenerationManager::Job::Job(int job_id,
167 WebContents* web_contents, 182 WebContents* web_contents,
168 const MHTMLGenerationParams& params, 183 const MHTMLGenerationParams& params,
169 const GenerateMHTMLCallback& callback) 184 const GenerateMHTMLCallback& callback)
170 : job_id_(job_id), 185 : job_id_(job_id),
171 creation_time_(base::TimeTicks::Now()), 186 creation_time_(base::TimeTicks::Now()),
172 params_(params), 187 params_(params),
173 frame_tree_node_id_of_busy_frame_(FrameTreeNode::kFrameTreeNodeInvalidId), 188 frame_tree_node_id_of_busy_frame_(FrameTreeNode::kFrameTreeNodeInvalidId),
174 mhtml_boundary_marker_(net::GenerateMimeMultipartBoundary()), 189 mhtml_boundary_marker_(net::GenerateMimeMultipartBoundary()),
175 salt_(base::GenerateGUID()), 190 salt_(base::GenerateGUID()),
176 callback_(callback), 191 callback_(callback),
177 is_finished_(false), 192 is_finished_(false),
178 observed_renderer_process_host_(this) { 193 observed_renderer_process_host_(this) {
179 DCHECK_CURRENTLY_ON(BrowserThread::UI); 194 DCHECK_CURRENTLY_ON(BrowserThread::UI);
180 web_contents->ForEachFrame(base::Bind( 195 web_contents->ForEachFrame(base::Bind(
181 &MHTMLGenerationManager::Job::AddFrame, 196 &MHTMLGenerationManager::Job::AddFrame,
182 base::Unretained(this))); // Safe because ForEachFrame is synchronous. 197 base::Unretained(this))); // Safe because ForEachFrame is synchronous.
183 198
184 // Main frame needs to be processed first. 199 // Main frame needs to be processed first.
185 DCHECK(!pending_frame_tree_node_ids_.empty()); 200 DCHECK(!pending_frame_tree_node_ids_.empty());
186 DCHECK(FrameTreeNode::GloballyFindByID(pending_frame_tree_node_ids_.front()) 201 DCHECK(FrameTreeNode::GloballyFindByID(pending_frame_tree_node_ids_.front())
187 ->parent() == nullptr); 202 ->parent() == nullptr);
203
204 // Save off any extra data
205 base::SupportsUserData::Data* found_user_data =
206 web_contents->GetUserData(content::kMHTMLExtraDataKey);
Dmitry Titov 2017/03/16 02:28:21 It is better to follow other examples that general
fgorski 2017/03/16 17:46:32 +1. Details in my earlier comment.
Pete Williamson 2017/03/17 23:28:32 Done.
Pete Williamson 2017/03/17 23:28:32 Done.
207 MHTMLExtraData* found_data = static_cast<MHTMLExtraData*>(found_user_data);
208
209 if (found_data != nullptr)
210 extra_data_ = found_data->data_string;
188 } 211 }
189 212
190 MHTMLGenerationManager::Job::~Job() { 213 MHTMLGenerationManager::Job::~Job() {
191 DCHECK_CURRENTLY_ON(BrowserThread::UI); 214 DCHECK_CURRENTLY_ON(BrowserThread::UI);
192 } 215 }
193 216
194 std::map<int, std::string> 217 std::map<int, std::string>
195 MHTMLGenerationManager::Job::CreateFrameRoutingIdToContentId( 218 MHTMLGenerationManager::Job::CreateFrameRoutingIdToContentId(
196 SiteInstance* site_instance) { 219 SiteInstance* site_instance) {
197 std::map<int, std::string> result; 220 std::map<int, std::string> result;
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 return; 371 return;
349 } 372 }
350 373
351 // If no previous error occurred the boundary should be sent. 374 // If no previous error occurred the boundary should be sent.
352 BrowserThread::PostTaskAndReplyWithResult( 375 BrowserThread::PostTaskAndReplyWithResult(
353 BrowserThread::FILE, FROM_HERE, 376 BrowserThread::FILE, FROM_HERE,
354 base::Bind( 377 base::Bind(
355 &MHTMLGenerationManager::Job::CloseFileOnFileThread, save_status, 378 &MHTMLGenerationManager::Job::CloseFileOnFileThread, save_status,
356 (save_status == MhtmlSaveStatus::SUCCESS ? mhtml_boundary_marker_ 379 (save_status == MhtmlSaveStatus::SUCCESS ? mhtml_boundary_marker_
357 : std::string()), 380 : std::string()),
358 base::Passed(&browser_file_)), 381 base::Passed(&browser_file_), extra_data_),
359 callback); 382 callback);
360 } 383 }
361 384
362 bool MHTMLGenerationManager::Job::IsMessageFromFrameExpected( 385 bool MHTMLGenerationManager::Job::IsMessageFromFrameExpected(
363 RenderFrameHostImpl* sender) { 386 RenderFrameHostImpl* sender) {
364 int sender_id = sender->frame_tree_node()->frame_tree_node_id(); 387 int sender_id = sender->frame_tree_node()->frame_tree_node_id();
365 if (sender_id != frame_tree_node_id_of_busy_frame_) 388 if (sender_id != frame_tree_node_id_of_busy_frame_)
366 return false; 389 return false;
367 390
368 // We only expect one message per frame - let's make sure subsequent messages 391 // We only expect one message per frame - let's make sure subsequent messages
(...skipping 25 matching lines...) Expand all
394 417
395 // Report success if all frames have been processed. 418 // Report success if all frames have been processed.
396 if (pending_frame_tree_node_ids_.empty()) 419 if (pending_frame_tree_node_ids_.empty())
397 return MhtmlSaveStatus::SUCCESS; 420 return MhtmlSaveStatus::SUCCESS;
398 421
399 return SendToNextRenderFrame(); 422 return SendToNextRenderFrame();
400 } 423 }
401 424
402 // static 425 // static
403 std::tuple<MhtmlSaveStatus, int64_t> 426 std::tuple<MhtmlSaveStatus, int64_t>
404 MHTMLGenerationManager::Job::CloseFileOnFileThread(MhtmlSaveStatus save_status, 427 MHTMLGenerationManager::Job::CloseFileOnFileThread(
405 const std::string& boundary, 428 MhtmlSaveStatus save_status,
406 base::File file) { 429 const std::string& boundary,
430 base::File file,
431 const std::string& extra_data) {
407 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 432 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
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 section of its own, if we have any.
441 if (!WriteExtraDataSectionOnFileThread(boundary, file, extra_data))
442 save_status = MhtmlSaveStatus::FILE_WRITTING_ERROR;
Dmitry Titov 2017/03/16 02:28:21 I think once there is a FILE_WRITING_ERROR, there
Pete Williamson 2017/03/17 23:28:32 Done.
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;
418 } 448 }
419 449
420 // If the file is still valid try to close it. Only update the status if that 450 // If the file is still valid try to close it. Only update the status if that
421 // won't hide an earlier error. 451 // won't hide an earlier error.
422 int64_t file_size = -1; 452 int64_t file_size = -1;
423 if (file.IsValid()) { 453 if (file.IsValid()) {
424 file_size = file.GetLength(); 454 file_size = file.GetLength();
425 file.Close(); 455 file.Close();
426 } else if (save_status == MhtmlSaveStatus::SUCCESS) { 456 } else if (save_status == MhtmlSaveStatus::SUCCESS) {
427 save_status = MhtmlSaveStatus::FILE_CLOSING_ERROR; 457 save_status = MhtmlSaveStatus::FILE_CLOSING_ERROR;
428 } 458 }
429 459
430 return std::make_tuple(save_status, file_size); 460 return std::make_tuple(save_status, file_size);
431 } 461 }
432 462
463 // static
464 bool MHTMLGenerationManager::Job::WriteExtraDataSectionOnFileThread(
465 const std::string& boundary,
466 base::File& file,
467 const std::string& extra_data) {
468 // Don't write an extra data section if there is none.
469 if (extra_data.empty())
470 return true;
471
472 // TODO: What value do I use for content location?
473 std::string content_location_value;
Dmitry Titov 2017/03/16 02:28:20 line 345 in this file is an example of how we gene
Pete Williamson 2017/03/17 23:28:33 Done.
474
475 // Write a newline, then a boundary, then the content location, another
476 // newline, the extra data string, and end with a newline.
477 std::string extra_data_section = base::StringPrintf(
478 "--%s--\r\n%s%s\r\n%s\r\n", boundary.c_str(), kContentLocation,
479 content_location_value.c_str(), extra_data.c_str());
480 // Now actually write the string into the file.
481 DCHECK(base::IsStringASCII(extra_data_section));
482 if (file.WriteAtCurrentPos(extra_data_section.data(),
fgorski 2017/03/16 17:46:32 return file.Write... >= 0;
Pete Williamson 2017/03/17 23:28:32 Done.
483 extra_data_section.size()) < 0)
fgorski 2017/03/16 17:46:32 nit: {} if you don't apply above.
Pete Williamson 2017/03/17 23:28:32 Acknowledged.
484 return false;
485
486 return true;
487 }
488
433 MHTMLGenerationManager* MHTMLGenerationManager::GetInstance() { 489 MHTMLGenerationManager* MHTMLGenerationManager::GetInstance() {
434 return base::Singleton<MHTMLGenerationManager>::get(); 490 return base::Singleton<MHTMLGenerationManager>::get();
435 } 491 }
436 492
437 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {} 493 MHTMLGenerationManager::MHTMLGenerationManager() : next_job_id_(0) {}
438 494
439 MHTMLGenerationManager::~MHTMLGenerationManager() { 495 MHTMLGenerationManager::~MHTMLGenerationManager() {
440 } 496 }
441 497
442 void MHTMLGenerationManager::SaveMHTML(WebContents* web_contents, 498 void MHTMLGenerationManager::SaveMHTML(WebContents* web_contents,
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 return iter->second.get(); 648 return iter->second.get();
593 } 649 }
594 650
595 void MHTMLGenerationManager::RenderProcessExited(Job* job) { 651 void MHTMLGenerationManager::RenderProcessExited(Job* job) {
596 DCHECK_CURRENTLY_ON(BrowserThread::UI); 652 DCHECK_CURRENTLY_ON(BrowserThread::UI);
597 DCHECK(job); 653 DCHECK(job);
598 JobFinished(job, MhtmlSaveStatus::RENDER_PROCESS_EXITED); 654 JobFinished(job, MhtmlSaveStatus::RENDER_PROCESS_EXITED);
599 } 655 }
600 656
601 } // namespace content 657 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698