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 "content/common/view_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. | |
Randy Smith (Not in Mondays)
2011/06/10 18:45:38
Another area where I'm feeling my lack of competen
Jay Civelli
2011/06/10 22:38:22
I think it is OK too, but I'll try to find someone
| |
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 for (size_t i = 0; i < mhtml.length(); i += chunk_size) { | |
54 size_t copy_size = std::min(mhtml.length() - i, chunk_size); | |
55 if (base::WritePlatformFile(file_, i, data + i, copy_size) == -1) | |
Randy Smith (Not in Mondays)
2011/06/10 18:45:38
Shouldn't you handle the case where WritePlatformF
Jay Civelli
2011/06/10 22:38:22
Good point, now taking the returned number of byte
| |
56 return false; | |
57 } | |
58 return true; | |
59 } | |
OLD | NEW |