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); |
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 WebString WebPageSerializer::generateMHTMLBoundary() |
196 { | 160 { |
197 RefPtr<SharedBuffer> mhtml = serializePageToMHTML(toWebViewImpl(view)->page(
), MHTMLArchive::UseDefaultEncoding); | 161 return MHTMLArchive::generateMHTMLBoundary(); |
198 // FIXME: we are copying all the data here. Idealy we would have a WebShared
Data(). | |
199 return WebCString(mhtml->data(), mhtml->size()); | |
200 } | 162 } |
201 | 163 |
202 WebCString WebPageSerializer::serializeToMHTMLUsingBinaryEncoding(WebView* view) | 164 WebData WebPageSerializer::generateMHTMLHeader( |
| 165 const WebString& boundary, WebLocalFrame* topLevelFrame) |
203 { | 166 { |
204 RefPtr<SharedBuffer> mhtml = serializePageToMHTML(toWebViewImpl(view)->page(
), MHTMLArchive::UseBinaryEncoding); | 167 Document* document = toWebLocalFrameImpl(topLevelFrame)->frame()->document()
; |
205 // FIXME: we are copying all the data here. Idealy we would have a WebShared
Data(). | 168 |
206 return WebCString(mhtml->data(), mhtml->size()); | 169 RefPtr<SharedBuffer> buffer = SharedBuffer::create(); |
| 170 MHTMLArchive::generateMHTMLHeader( |
| 171 boundary, document->title(), document->suggestedMIMEType(), |
| 172 *buffer); |
| 173 return PassRefPtr<SharedBuffer>(buffer); |
| 174 } |
| 175 |
| 176 WebData WebPageSerializer::generateMHTMLParts( |
| 177 const WebString& boundary, WebLocalFrame* webFrame, bool useBinaryEncoding, |
| 178 const WebVector<std::pair<WebFrame*, WebString>>& webFrameToContentID) |
| 179 { |
| 180 // Translate arguments from public to internal blink APIs. |
| 181 LocalFrame* frame = toWebLocalFrameImpl(webFrame)->frame(); |
| 182 MHTMLArchive::EncodingPolicy encodingPolicy = useBinaryEncoding |
| 183 ? MHTMLArchive::EncodingPolicy::UseBinaryEncoding |
| 184 : MHTMLArchive::EncodingPolicy::UseDefaultEncoding; |
| 185 ContentIDMap frameToContentID = |
| 186 createFrameToContentIDMap(webFrameToContentID); |
| 187 |
| 188 // Serialize. |
| 189 Vector<SerializedResource> resources; |
| 190 MHTMLPageSerializerDelegate delegate(frameToContentID); |
| 191 PageSerializer serializer(resources, &delegate); |
| 192 serializer.serializeFrame(*frame); |
| 193 |
| 194 // Encode serializer's output as MHTML. |
| 195 RefPtr<SharedBuffer> output = SharedBuffer::create(); |
| 196 bool isFirstResource = true; |
| 197 for (const SerializedResource& resource : resources) { |
| 198 // Frame is the 1st resource (see PageSerializer::serializeFrame doc |
| 199 // comment). Frames need a Content-ID header. |
| 200 String contentID = isFirstResource ? frameToContentID.get(frame) : Strin
g(); |
| 201 |
| 202 MHTMLArchive::generateMHTMLPart( |
| 203 boundary, contentID, encodingPolicy, resource, *output); |
| 204 |
| 205 isFirstResource = false; |
| 206 } |
| 207 return output.release(); |
| 208 } |
| 209 |
| 210 WebData WebPageSerializer::generateMHTMLFooter(const WebString& boundary) |
| 211 { |
| 212 RefPtr<SharedBuffer> buffer = SharedBuffer::create(); |
| 213 MHTMLArchive::generateMHTMLFooter(boundary, *buffer); |
| 214 return PassRefPtr<SharedBuffer>(buffer); |
207 } | 215 } |
208 | 216 |
209 bool WebPageSerializer::serialize(WebLocalFrame* frame, | 217 bool WebPageSerializer::serialize(WebLocalFrame* frame, |
210 WebPageSerializerClient* client, | 218 WebPageSerializerClient* client, |
211 const WebVector<WebURL>& links, | 219 const WebVector<WebURL>& links, |
212 const WebVector<WebString>& localPaths, | 220 const WebVector<WebString>& localPaths, |
213 const WebString& localDirectoryName) | 221 const WebString& localDirectoryName) |
214 { | 222 { |
215 WebPageSerializerImpl serializerImpl( | 223 WebPageSerializerImpl serializerImpl( |
216 frame, client, links, localPaths, localDirectoryName); | 224 frame, client, links, localPaths, localDirectoryName); |
(...skipping 19 matching lines...) Expand all Loading... |
236 WebString WebPageSerializer::generateBaseTagDeclaration(const WebString& baseTar
get) | 244 WebString WebPageSerializer::generateBaseTagDeclaration(const WebString& baseTar
get) |
237 { | 245 { |
238 // TODO(yosin) We should call |PageSerializer::baseTagDeclarationOf()|. | 246 // TODO(yosin) We should call |PageSerializer::baseTagDeclarationOf()|. |
239 if (baseTarget.isEmpty()) | 247 if (baseTarget.isEmpty()) |
240 return String("<base href=\".\">"); | 248 return String("<base href=\".\">"); |
241 String baseString = "<base href=\".\" target=\"" + static_cast<const String&
>(baseTarget) + "\">"; | 249 String baseString = "<base href=\".\" target=\"" + static_cast<const String&
>(baseTarget) + "\">"; |
242 return baseString; | 250 return baseString; |
243 } | 251 } |
244 | 252 |
245 } // namespace blink | 253 } // namespace blink |
OLD | NEW |