Chromium Code Reviews| 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..45a7f14199dd1b864547e0023bc44d3f5dca4c4a 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,11 @@ |
| #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: "; |
| +} // namespace |
| + |
| namespace content { |
| // The class and all of its members live on the UI thread. Only static methods |
| @@ -101,9 +108,17 @@ 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. |
|
fgorski
2017/03/28 17:01:12
true for success or no data to write.
Pete Williamson
2017/03/28 18:01:58
Done.
|
| + 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 +171,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 +203,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 +377,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,25 +423,36 @@ 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( |
| + MhtmlSaveStatus save_status, |
| + const std::string& boundary, |
| + base::File file, |
| + const MHTMLExtraDataImpl* extra_data) { |
| DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| + int64_t file_size = -1; |
| // If no previous error occurred the boundary should have been provided. |
| if (save_status == MhtmlSaveStatus::SUCCESS) { |
| 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, 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.
|
| + } |
| + |
| 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, file_size); |
| + } |
| } |
| // 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; |
| if (file.IsValid()) { |
| file_size = file.GetLength(); |
| file.Close(); |
| @@ -430,6 +463,41 @@ 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) { |
|
fgorski
2017/03/28 17:01:12
DCHECK thread.
Pete Williamson
2017/03/28 18:01:58
Done.
|
| + // 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, |
| + 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(); |
| } |