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

Unified Diff: content/browser/download/mhtml_generation_manager.cc

Issue 2683493002: Get signals working in the EXTRA_DATA section of MHTML (Closed)
Patch Set: CR Feedback per FGorski 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/download/mhtml_generation_manager.cc
diff --git a/content/browser/download/mhtml_generation_manager.cc b/content/browser/download/mhtml_generation_manager.cc
index b989af11932a1e2c49ec1451e7d55560874b8f28..7e633396ee0df5efd2c9aa409c2ab0baa3e72bf9 100644
--- a/content/browser/download/mhtml_generation_manager.cc
+++ b/content/browser/download/mhtml_generation_manager.cc
@@ -21,10 +21,12 @@
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "content/browser/bad_message.h"
+#include "content/browser/download/mhtml_extra_data_impl.h"
#include "content/browser/frame_host/frame_tree_node.h"
#include "content/browser/frame_host/render_frame_host_impl.h"
#include "content/common/frame_messages.h"
#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/mhtml_extra_data.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_process_host_observer.h"
@@ -32,6 +34,12 @@
#include "content/public/common/mhtml_generation_params.h"
#include "net/base/mime_util.h"
+namespace {
+const char kContentLocation[] = "Content-Location: ";
+const char kContentType[] = "Content-Type: ";
+int kInvalidFileSize = -1;
+} // namespace
+
namespace content {
// The class and all of its members live on the UI thread. Only static methods
@@ -101,9 +109,18 @@ class MHTMLGenerationManager::Job : public RenderProcessHostObserver {
static std::tuple<MhtmlSaveStatus, int64_t> CloseFileOnFileThread(
MhtmlSaveStatus save_status,
const std::string& boundary,
- base::File file);
+ base::File file,
+ const MHTMLExtraDataImpl* extra_data);
void AddFrame(RenderFrameHost* render_frame_host);
+ // If we have any extra MHTML parts to write out, write them into the file
+ // while on the file thread. Returns true for success, or if there is no data
+ // to write.
+ static bool WriteExtraDataPartsOnFileThread(
+ const std::string& boundary,
+ base::File& file,
+ const MHTMLExtraDataImpl* extra_data);
+
// Creates a new map with values (content ids) the same as in
// |frame_tree_node_to_content_id_| map, but with the keys translated from
// frame_tree_node_id into a |site_instance|-specific routing_id.
@@ -156,6 +173,9 @@ class MHTMLGenerationManager::Job : public RenderProcessHostObserver {
// destroyed by MHTMLGenerationManager::OnFileClosed).
bool is_finished_;
+ // Any extra data parts that should be emitted into the output MHTML.
+ MHTMLExtraDataImpl* extra_data_;
+
// RAII helper for registering this Job as a RenderProcessHost observer.
ScopedObserver<RenderProcessHost, MHTMLGenerationManager::Job>
observed_renderer_process_host_;
@@ -185,6 +205,10 @@ MHTMLGenerationManager::Job::Job(int job_id,
DCHECK(!pending_frame_tree_node_ids_.empty());
DCHECK(FrameTreeNode::GloballyFindByID(pending_frame_tree_node_ids_.front())
->parent() == nullptr);
+
+ // Save off any extra data.
+ extra_data_ = static_cast<MHTMLExtraDataImpl*>(
+ MHTMLExtraData::FromWebContents(web_contents));
}
MHTMLGenerationManager::Job::~Job() {
@@ -355,7 +379,7 @@ void MHTMLGenerationManager::Job::CloseFile(
&MHTMLGenerationManager::Job::CloseFileOnFileThread, save_status,
(save_status == MhtmlSaveStatus::SUCCESS ? mhtml_boundary_marker_
: std::string()),
- base::Passed(&browser_file_)),
+ base::Passed(&browser_file_), extra_data_),
callback);
}
@@ -401,9 +425,11 @@ MhtmlSaveStatus MHTMLGenerationManager::Job::OnSerializeAsMHTMLResponse(
// static
std::tuple<MhtmlSaveStatus, int64_t>
-MHTMLGenerationManager::Job::CloseFileOnFileThread(MhtmlSaveStatus save_status,
- const std::string& boundary,
- base::File file) {
+MHTMLGenerationManager::Job::CloseFileOnFileThread(
Dmitry Titov 2017/03/29 18:53:06 It's adding a side effect to the "CloseFile" funct
Pete Williamson 2017/03/29 22:05:43 Done.
+ MhtmlSaveStatus save_status,
+ const std::string& boundary,
+ base::File file,
+ const MHTMLExtraDataImpl* extra_data) {
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
// If no previous error occurred the boundary should have been provided.
@@ -411,15 +437,24 @@ MHTMLGenerationManager::Job::CloseFileOnFileThread(MhtmlSaveStatus save_status,
TRACE_EVENT0("page-serialization",
"MHTMLGenerationManager::Job MHTML footer writing");
DCHECK(!boundary.empty());
+
+ // Write the extra data into a part of its own, if we have any.
+ if (!WriteExtraDataPartsOnFileThread(boundary, file, extra_data)) {
+ save_status = MhtmlSaveStatus::FILE_WRITTING_ERROR;
+ return std::make_tuple(save_status, kInvalidFileSize);
+ }
+
std::string footer = base::StringPrintf("--%s--\r\n", boundary.c_str());
DCHECK(base::IsStringASCII(footer));
- if (file.WriteAtCurrentPos(footer.data(), footer.size()) < 0)
+ if (file.WriteAtCurrentPos(footer.data(), footer.size()) < 0) {
save_status = MhtmlSaveStatus::FILE_WRITTING_ERROR;
+ return std::make_tuple(save_status, kInvalidFileSize);
+ }
}
// If the file is still valid try to close it. Only update the status if that
// won't hide an earlier error.
- int64_t file_size = -1;
+ int64_t file_size = kInvalidFileSize;
if (file.IsValid()) {
file_size = file.GetLength();
file.Close();
@@ -430,6 +465,42 @@ MHTMLGenerationManager::Job::CloseFileOnFileThread(MhtmlSaveStatus save_status,
return std::make_tuple(save_status, file_size);
}
+// static
+bool MHTMLGenerationManager::Job::WriteExtraDataPartsOnFileThread(
+ const std::string& boundary,
+ base::File& file,
+ const MHTMLExtraDataImpl* extra_data) {
+ DCHECK_CURRENTLY_ON(BrowserThread::FILE);
+ // Don't write an extra data part if there is none.
+ if (extra_data == nullptr)
+ return true;
+
+ const std::vector<MHTMLExtraDataPart>& extra_parts(extra_data->parts());
+ if (extra_parts.empty())
+ return true;
+
+ std::string serialized_extra_data_parts;
+
+ // For each extra part, serialize that part and add to our accumulator
+ // string.
+ for (auto part : extra_parts) {
+ // Write a newline, then a boundary, another newline, then the content
+ // location, another newline, the content type, another newline, the extra
+ // data string, and end with a newline.
+ std::string serialized_extra_data_part = base::StringPrintf(
+ "--%s--\r\n%s%s\r\n%s%s\r\n%s\r\n", boundary.c_str(), kContentLocation,
Dmitry Titov 2017/03/29 18:53:06 the boundary, if it is not the last one in the fil
Pete Williamson 2017/03/29 22:05:43 Done.
+ part.content_location.c_str(), kContentType, part.content_type.c_str(),
+ part.body.c_str());
+ DCHECK(base::IsStringASCII(serialized_extra_data_part));
+
+ serialized_extra_data_parts += serialized_extra_data_part;
+ }
+
+ // Write the string into the file. Returns false if we failed the write.
+ return (file.WriteAtCurrentPos(serialized_extra_data_parts.data(),
+ serialized_extra_data_parts.size()) >= 0);
+}
+
MHTMLGenerationManager* MHTMLGenerationManager::GetInstance() {
return base::Singleton<MHTMLGenerationManager>::get();
}

Powered by Google App Engine
This is Rietveld 408576698