OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 16 matching lines...) Expand all Loading... | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "public/web/WebPageSerializer.h" | 32 #include "public/web/WebPageSerializer.h" |
33 | 33 |
34 #include "core/HTMLNames.h" | 34 #include "core/HTMLNames.h" |
35 #include "core/dom/Document.h" | 35 #include "core/dom/Document.h" |
36 #include "core/dom/Element.h" | 36 #include "core/dom/Element.h" |
37 #include "core/frame/Frame.h" | |
37 #include "core/frame/LocalFrame.h" | 38 #include "core/frame/LocalFrame.h" |
39 #include "core/frame/RemoteFrame.h" | |
38 #include "core/html/HTMLAllCollection.h" | 40 #include "core/html/HTMLAllCollection.h" |
39 #include "core/html/HTMLFrameElementBase.h" | 41 #include "core/html/HTMLFrameElementBase.h" |
40 #include "core/html/HTMLFrameOwnerElement.h" | 42 #include "core/html/HTMLFrameOwnerElement.h" |
41 #include "core/html/HTMLInputElement.h" | 43 #include "core/html/HTMLInputElement.h" |
42 #include "core/html/HTMLTableElement.h" | 44 #include "core/html/HTMLTableElement.h" |
43 #include "core/loader/DocumentLoader.h" | 45 #include "core/loader/DocumentLoader.h" |
44 #include "core/page/Page.h" | |
45 #include "core/page/PageSerializer.h" | 46 #include "core/page/PageSerializer.h" |
46 #include "platform/SerializedResource.h" | 47 #include "platform/SerializedResource.h" |
48 #include "platform/SharedBuffer.h" | |
47 #include "platform/mhtml/MHTMLArchive.h" | 49 #include "platform/mhtml/MHTMLArchive.h" |
48 #include "platform/mhtml/MHTMLParser.h" | 50 #include "platform/mhtml/MHTMLParser.h" |
49 #include "platform/weborigin/KURL.h" | 51 #include "platform/weborigin/KURL.h" |
50 #include "public/platform/WebCString.h" | 52 #include "public/platform/WebCString.h" |
51 #include "public/platform/WebString.h" | 53 #include "public/platform/WebString.h" |
52 #include "public/platform/WebURL.h" | 54 #include "public/platform/WebURL.h" |
53 #include "public/platform/WebVector.h" | 55 #include "public/platform/WebVector.h" |
56 #include "public/web/WebDocument.h" | |
54 #include "public/web/WebFrame.h" | 57 #include "public/web/WebFrame.h" |
55 #include "public/web/WebPageSerializerClient.h" | 58 #include "public/web/WebPageSerializerClient.h" |
56 #include "public/web/WebView.h" | |
57 #include "web/WebLocalFrameImpl.h" | 59 #include "web/WebLocalFrameImpl.h" |
58 #include "web/WebPageSerializerImpl.h" | 60 #include "web/WebPageSerializerImpl.h" |
59 #include "web/WebViewImpl.h" | 61 #include "web/WebRemoteFrameImpl.h" |
60 #include "wtf/Assertions.h" | 62 #include "wtf/Assertions.h" |
61 #include "wtf/HashMap.h" | 63 #include "wtf/HashMap.h" |
64 #include "wtf/HashSet.h" | |
62 #include "wtf/Noncopyable.h" | 65 #include "wtf/Noncopyable.h" |
63 #include "wtf/Vector.h" | 66 #include "wtf/Vector.h" |
64 #include "wtf/text/StringConcatenate.h" | 67 #include "wtf/text/StringConcatenate.h" |
65 | 68 |
66 namespace blink { | 69 namespace blink { |
67 | 70 |
68 namespace { | 71 namespace { |
69 | 72 |
70 using ContentIDMap = WillBeHeapHashMap<RawPtrWillBeMember<Frame>, String>; | 73 using ContentIDMap = WillBeHeapHashMap<RawPtrWillBeMember<Frame>, String>; |
71 | 74 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
126 || doc->isXHTMLDocument() || doc->isImageDocument(); | 129 || doc->isXHTMLDocument() || doc->isImageDocument(); |
127 if (isHandledBySerializer) { | 130 if (isHandledBySerializer) { |
128 rewrittenLink = cidURI.string(); | 131 rewrittenLink = cidURI.string(); |
129 return true; | 132 return true; |
130 } | 133 } |
131 } | 134 } |
132 | 135 |
133 return false; | 136 return false; |
134 } | 137 } |
135 | 138 |
136 ContentIDMap generateFrameContentIDs(Page* page) | 139 ContentIDMap createFrameToContentIDMap( |
140 const WebVector<std::pair<WebFrame*, WebString>>& webFrameToContentID) | |
137 { | 141 { |
138 ContentIDMap frameToContentID; | 142 ContentIDMap result; |
139 int frameID = 0; | 143 for (const auto& it : webFrameToContentID) { |
140 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree().traverse Next()) { | 144 WebFrame* webFrame = it.first; |
141 // TODO(lukasza): Move cid generation to the browser + use base/guid.h | 145 const WebString& webContentID = it.second; |
142 // (see the draft at crrev.com/1386873003). | |
143 StringBuilder contentIDBuilder; | |
144 contentIDBuilder.appendLiteral("<frame"); | |
145 contentIDBuilder.appendNumber(frameID++); | |
146 contentIDBuilder.appendLiteral("@mhtml.blink>"); | |
147 | 146 |
148 frameToContentID.add(frame, contentIDBuilder.toString()); | 147 Frame* frame = webFrame->isWebLocalFrame() |
148 ? static_cast<Frame*>(toWebLocalFrameImpl(webFrame)->frame()) | |
149 : static_cast<Frame*>(toWebRemoteFrameImpl(webFrame)->frame()); | |
150 String contentID(webContentID); | |
151 | |
152 result.add(frame, contentID); | |
dcheng
2015/12/08 06:22:27
Replace the ternary statement with webFrame->toImp
Łukasz Anforowicz
2015/12/08 21:26:43
Nice. Done.
| |
149 } | 153 } |
150 return frameToContentID; | 154 return result; |
151 } | |
152 | |
153 PassRefPtr<SharedBuffer> serializePageToMHTML(Page* page, MHTMLArchive::Encoding Policy encodingPolicy) | |
154 { | |
155 Vector<SerializedResource> resources; | |
156 ContentIDMap frameToContentID = generateFrameContentIDs(page); | |
157 MHTMLPageSerializerDelegate delegate(frameToContentID); | |
158 PageSerializer serializer(resources, &delegate); | |
159 | |
160 RefPtr<SharedBuffer> output = SharedBuffer::create(); | |
161 String boundary = MHTMLArchive::generateMHTMLBoundary(); | |
162 | |
163 Document* document = page->deprecatedLocalMainFrame()->document(); | |
164 MHTMLArchive::generateMHTMLHeader( | |
165 boundary, document->title(), document->suggestedMIMEType(), *output); | |
166 | |
167 for (Frame* frame = page->deprecatedLocalMainFrame(); frame; frame = frame-> tree().traverseNext()) { | |
168 // TODO(lukasza): This causes incomplete MHTML for OOPIFs. | |
169 // (crbug.com/538766) | |
170 if (!frame->isLocalFrame()) | |
171 continue; | |
172 | |
173 resources.clear(); | |
174 serializer.serializeFrame(*toLocalFrame(frame)); | |
175 | |
176 bool isFirstResource = true; | |
177 for (const SerializedResource& resource : resources) { | |
178 // Frame is the 1st resource (see PageSerializer::serializeFrame doc | |
179 // comment). Frames need a Content-ID header. | |
180 String contentID = isFirstResource ? frameToContentID.get(frame) : S tring(); | |
181 | |
182 MHTMLArchive::generateMHTMLPart( | |
183 boundary, contentID, encodingPolicy, resource, *output); | |
184 | |
185 isFirstResource = false; | |
186 } | |
187 } | |
188 | |
189 MHTMLArchive::generateMHTMLFooter(boundary, *output); | |
190 return output.release(); | |
191 } | 155 } |
192 | 156 |
193 } // namespace | 157 } // namespace |
194 | 158 |
195 WebCString WebPageSerializer::serializeToMHTML(WebView* view) | 159 WebData WebPageSerializer::generateMHTMLHeader( |
160 const WebString& boundary, WebLocalFrame* topLevelFrame) | |
196 { | 161 { |
197 RefPtr<SharedBuffer> mhtml = serializePageToMHTML(toWebViewImpl(view)->page( ), MHTMLArchive::UseDefaultEncoding); | 162 Document* document = toWebLocalFrameImpl(topLevelFrame)->frame()->document() ; |
198 // FIXME: we are copying all the data here. Idealy we would have a WebShared Data(). | 163 |
199 return WebCString(mhtml->data(), mhtml->size()); | 164 RefPtr<SharedBuffer> buffer = SharedBuffer::create(); |
165 MHTMLArchive::generateMHTMLHeader( | |
166 boundary, document->title(), document->suggestedMIMEType(), | |
167 *buffer); | |
168 return PassRefPtr<SharedBuffer>(buffer); | |
dcheng
2015/12/08 06:22:27
Does buffer.release() work?
Łukasz Anforowicz
2015/12/08 21:26:43
Hmmm... I see that I've (inconsistently) used outp
| |
200 } | 169 } |
201 | 170 |
202 WebCString WebPageSerializer::serializeToMHTMLUsingBinaryEncoding(WebView* view) | 171 WebData WebPageSerializer::generateMHTMLParts( |
172 const WebString& boundary, WebLocalFrame* webFrame, bool useBinaryEncoding, | |
173 const WebVector<std::pair<WebFrame*, WebString>>& webFrameToContentID) | |
203 { | 174 { |
204 RefPtr<SharedBuffer> mhtml = serializePageToMHTML(toWebViewImpl(view)->page( ), MHTMLArchive::UseBinaryEncoding); | 175 // Translate arguments from public to internal blink APIs. |
205 // FIXME: we are copying all the data here. Idealy we would have a WebShared Data(). | 176 LocalFrame* frame = toWebLocalFrameImpl(webFrame)->frame(); |
206 return WebCString(mhtml->data(), mhtml->size()); | 177 MHTMLArchive::EncodingPolicy encodingPolicy = useBinaryEncoding |
178 ? MHTMLArchive::EncodingPolicy::UseBinaryEncoding | |
179 : MHTMLArchive::EncodingPolicy::UseDefaultEncoding; | |
180 ContentIDMap frameToContentID = | |
181 createFrameToContentIDMap(webFrameToContentID); | |
182 | |
183 // Serialize. | |
184 Vector<SerializedResource> resources; | |
185 MHTMLPageSerializerDelegate delegate(frameToContentID); | |
186 PageSerializer serializer(resources, &delegate); | |
187 serializer.serializeFrame(*frame); | |
188 | |
189 // Encode serializer's output as MHTML. | |
190 RefPtr<SharedBuffer> output = SharedBuffer::create(); | |
191 bool isFirstResource = true; | |
192 for (const SerializedResource& resource : resources) { | |
193 // Frame is the 1st resource (see PageSerializer::serializeFrame doc | |
194 // comment). Frames need a Content-ID header. | |
195 String contentID = isFirstResource ? frameToContentID.get(frame) : Strin g(); | |
196 | |
197 MHTMLArchive::generateMHTMLPart( | |
198 boundary, contentID, encodingPolicy, resource, *output); | |
199 | |
200 isFirstResource = false; | |
201 } | |
202 return output.release(); | |
203 } | |
204 | |
205 WebData WebPageSerializer::generateMHTMLFooter(const WebString& boundary) | |
206 { | |
207 RefPtr<SharedBuffer> buffer = SharedBuffer::create(); | |
208 MHTMLArchive::generateMHTMLFooter(boundary, *buffer); | |
209 return PassRefPtr<SharedBuffer>(buffer); | |
dcheng
2015/12/08 06:22:27
Ditto: does buffer.release() work?
Łukasz Anforowicz
2015/12/08 21:26:43
Done.
| |
207 } | 210 } |
208 | 211 |
209 bool WebPageSerializer::serialize(WebLocalFrame* frame, | 212 bool WebPageSerializer::serialize(WebLocalFrame* frame, |
210 WebPageSerializerClient* client, | 213 WebPageSerializerClient* client, |
211 const WebVector<WebURL>& links, | 214 const WebVector<WebURL>& links, |
212 const WebVector<WebString>& localPaths, | 215 const WebVector<WebString>& localPaths, |
213 const WebString& localDirectoryName) | 216 const WebString& localDirectoryName) |
214 { | 217 { |
215 WebPageSerializerImpl serializerImpl( | 218 WebPageSerializerImpl serializerImpl( |
216 frame, client, links, localPaths, localDirectoryName); | 219 frame, client, links, localPaths, localDirectoryName); |
(...skipping 19 matching lines...) Expand all Loading... | |
236 WebString WebPageSerializer::generateBaseTagDeclaration(const WebString& baseTar get) | 239 WebString WebPageSerializer::generateBaseTagDeclaration(const WebString& baseTar get) |
237 { | 240 { |
238 // TODO(yosin) We should call |PageSerializer::baseTagDeclarationOf()|. | 241 // TODO(yosin) We should call |PageSerializer::baseTagDeclarationOf()|. |
239 if (baseTarget.isEmpty()) | 242 if (baseTarget.isEmpty()) |
240 return String("<base href=\".\">"); | 243 return String("<base href=\".\">"); |
241 String baseString = "<base href=\".\" target=\"" + static_cast<const String& >(baseTarget) + "\">"; | 244 String baseString = "<base href=\".\" target=\"" + static_cast<const String& >(baseTarget) + "\">"; |
242 return baseString; | 245 return baseString; |
243 } | 246 } |
244 | 247 |
245 } // namespace blink | 248 } // namespace blink |
OLD | NEW |