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