chrome.bluetoothSocket: clean-up Listen functions
[chromium-blink-merge.git] / content / renderer / mhtml_generator.cc
blob046c66b0c407cc36b6f688437f0db74ec19b3287
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.
5 #include "content/renderer/mhtml_generator.h"
7 #include "content/common/view_messages.h"
8 #include "content/renderer/render_view_impl.h"
9 #include "third_party/WebKit/public/platform/WebCString.h"
10 #include "third_party/WebKit/public/web/WebPageSerializer.h"
12 namespace content {
14 MHTMLGenerator::MHTMLGenerator(RenderViewImpl* render_view)
15 : RenderViewObserver(render_view) {
18 MHTMLGenerator::~MHTMLGenerator() {
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;
31 void MHTMLGenerator::OnSavePageAsMHTML(
32 int job_id, IPC::PlatformFileForTransit file_for_transit) {
33 file_ = IPC::PlatformFileForTransitToFile(file_for_transit);
34 int64 size = GenerateMHTML();
35 file_.Close();
36 NotifyBrowser(job_id, size);
39 void MHTMLGenerator::NotifyBrowser(int job_id, int64 data_size) {
40 render_view()->Send(new ViewHostMsg_SavedPageAsMHTML(job_id, data_size));
43 // TODO(jcivelli): write the chunks in deferred tasks to give a chance to the
44 // message loop to process other events.
45 int64 MHTMLGenerator::GenerateMHTML() {
46 blink::WebCString mhtml =
47 blink::WebPageSerializer::serializeToMHTML(render_view()->GetWebView());
48 const size_t chunk_size = 1024;
49 const char* data = mhtml.data();
50 size_t total_bytes_written = 0;
51 while (total_bytes_written < mhtml.length()) {
52 size_t copy_size =
53 std::min(mhtml.length() - total_bytes_written, chunk_size);
54 int bytes_written = file_.Write(total_bytes_written,
55 data + total_bytes_written, copy_size);
56 if (bytes_written == -1)
57 return -1;
58 total_bytes_written += bytes_written;
60 return total_bytes_written;
63 } // namespace content