| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/renderer/mhtml_generator.h" | 5 #include "content/renderer/mhtml_generator.h" |
| 6 | 6 |
| 7 #include "base/platform_file.h" | |
| 8 #include "content/common/view_messages.h" | 7 #include "content/common/view_messages.h" |
| 9 #include "content/renderer/render_view_impl.h" | 8 #include "content/renderer/render_view_impl.h" |
| 10 #include "third_party/WebKit/public/platform/WebCString.h" | 9 #include "third_party/WebKit/public/platform/WebCString.h" |
| 11 #include "third_party/WebKit/public/web/WebPageSerializer.h" | 10 #include "third_party/WebKit/public/web/WebPageSerializer.h" |
| 12 | 11 |
| 13 namespace content { | 12 namespace content { |
| 14 | 13 |
| 15 MHTMLGenerator::MHTMLGenerator(RenderViewImpl* render_view) | 14 MHTMLGenerator::MHTMLGenerator(RenderViewImpl* render_view) |
| 16 : RenderViewObserver(render_view), | 15 : RenderViewObserver(render_view) { |
| 17 file_(base::kInvalidPlatformFileValue) { | |
| 18 } | 16 } |
| 19 | 17 |
| 20 MHTMLGenerator::~MHTMLGenerator() { | 18 MHTMLGenerator::~MHTMLGenerator() { |
| 21 } | 19 } |
| 22 | 20 |
| 23 // RenderViewObserver implementation: | 21 // RenderViewObserver implementation: |
| 24 bool MHTMLGenerator::OnMessageReceived(const IPC::Message& message) { | 22 bool MHTMLGenerator::OnMessageReceived(const IPC::Message& message) { |
| 25 bool handled = true; | 23 bool handled = true; |
| 26 IPC_BEGIN_MESSAGE_MAP(MHTMLGenerator, message) | 24 IPC_BEGIN_MESSAGE_MAP(MHTMLGenerator, message) |
| 27 IPC_MESSAGE_HANDLER(ViewMsg_SavePageAsMHTML, OnSavePageAsMHTML) | 25 IPC_MESSAGE_HANDLER(ViewMsg_SavePageAsMHTML, OnSavePageAsMHTML) |
| 28 IPC_MESSAGE_UNHANDLED(handled = false) | 26 IPC_MESSAGE_UNHANDLED(handled = false) |
| 29 IPC_END_MESSAGE_MAP() | 27 IPC_END_MESSAGE_MAP() |
| 30 return handled; | 28 return handled; |
| 31 } | 29 } |
| 32 | 30 |
| 33 void MHTMLGenerator::OnSavePageAsMHTML( | 31 void MHTMLGenerator::OnSavePageAsMHTML( |
| 34 int job_id, IPC::PlatformFileForTransit file_for_transit) { | 32 int job_id, IPC::PlatformFileForTransit file_for_transit) { |
| 35 base::PlatformFile file = | 33 file_ = IPC::PlatformFileForTransitToFile(file_for_transit); |
| 36 IPC::PlatformFileForTransitToPlatformFile(file_for_transit); | |
| 37 file_ = file; | |
| 38 int64 size = GenerateMHTML(); | 34 int64 size = GenerateMHTML(); |
| 39 base::ClosePlatformFile(file); | 35 file_.Close(); |
| 40 NotifyBrowser(job_id, size); | 36 NotifyBrowser(job_id, size); |
| 41 } | 37 } |
| 42 | 38 |
| 43 void MHTMLGenerator::NotifyBrowser(int job_id, int64 data_size) { | 39 void MHTMLGenerator::NotifyBrowser(int job_id, int64 data_size) { |
| 44 render_view()->Send(new ViewHostMsg_SavedPageAsMHTML(job_id, data_size)); | 40 render_view()->Send(new ViewHostMsg_SavedPageAsMHTML(job_id, data_size)); |
| 45 file_ = base::kInvalidPlatformFileValue; | |
| 46 } | 41 } |
| 47 | 42 |
| 48 // TODO(jcivelli): write the chunks in deferred tasks to give a chance to the | 43 // TODO(jcivelli): write the chunks in deferred tasks to give a chance to the |
| 49 // message loop to process other events. | 44 // message loop to process other events. |
| 50 int64 MHTMLGenerator::GenerateMHTML() { | 45 int64 MHTMLGenerator::GenerateMHTML() { |
| 51 blink::WebCString mhtml = | 46 blink::WebCString mhtml = |
| 52 blink::WebPageSerializer::serializeToMHTML(render_view()->GetWebView()); | 47 blink::WebPageSerializer::serializeToMHTML(render_view()->GetWebView()); |
| 53 const size_t chunk_size = 1024; | 48 const size_t chunk_size = 1024; |
| 54 const char* data = mhtml.data(); | 49 const char* data = mhtml.data(); |
| 55 size_t total_bytes_written = 0; | 50 size_t total_bytes_written = 0; |
| 56 while (total_bytes_written < mhtml.length()) { | 51 while (total_bytes_written < mhtml.length()) { |
| 57 size_t copy_size = | 52 size_t copy_size = |
| 58 std::min(mhtml.length() - total_bytes_written, chunk_size); | 53 std::min(mhtml.length() - total_bytes_written, chunk_size); |
| 59 int bytes_written = base::WritePlatformFile(file_, total_bytes_written, | 54 int bytes_written = file_.Write(total_bytes_written, |
| 60 data + total_bytes_written, | 55 data + total_bytes_written, copy_size); |
| 61 copy_size); | |
| 62 if (bytes_written == -1) | 56 if (bytes_written == -1) |
| 63 return -1; | 57 return -1; |
| 64 total_bytes_written += bytes_written; | 58 total_bytes_written += bytes_written; |
| 65 } | 59 } |
| 66 return total_bytes_written; | 60 return total_bytes_written; |
| 67 } | 61 } |
| 68 | 62 |
| 69 } // namespace content | 63 } // namespace content |
| OLD | NEW |