| 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 27 matching lines...) Expand all Loading... |
| 38 #include "core/html/HTMLAllCollection.h" | 38 #include "core/html/HTMLAllCollection.h" |
| 39 #include "core/html/HTMLFrameElementBase.h" | 39 #include "core/html/HTMLFrameElementBase.h" |
| 40 #include "core/html/HTMLFrameOwnerElement.h" | 40 #include "core/html/HTMLFrameOwnerElement.h" |
| 41 #include "core/html/HTMLInputElement.h" | 41 #include "core/html/HTMLInputElement.h" |
| 42 #include "core/html/HTMLTableElement.h" | 42 #include "core/html/HTMLTableElement.h" |
| 43 #include "core/loader/DocumentLoader.h" | 43 #include "core/loader/DocumentLoader.h" |
| 44 #include "core/page/Page.h" | 44 #include "core/page/Page.h" |
| 45 #include "core/page/PageSerializer.h" | 45 #include "core/page/PageSerializer.h" |
| 46 #include "platform/SerializedResource.h" | 46 #include "platform/SerializedResource.h" |
| 47 #include "platform/mhtml/MHTMLArchive.h" | 47 #include "platform/mhtml/MHTMLArchive.h" |
| 48 #include "platform/mhtml/MHTMLParser.h" |
| 48 #include "platform/weborigin/KURL.h" | 49 #include "platform/weborigin/KURL.h" |
| 49 #include "public/platform/WebCString.h" | 50 #include "public/platform/WebCString.h" |
| 50 #include "public/platform/WebString.h" | 51 #include "public/platform/WebString.h" |
| 51 #include "public/platform/WebURL.h" | 52 #include "public/platform/WebURL.h" |
| 52 #include "public/platform/WebVector.h" | 53 #include "public/platform/WebVector.h" |
| 53 #include "public/web/WebFrame.h" | 54 #include "public/web/WebFrame.h" |
| 54 #include "public/web/WebPageSerializerClient.h" | 55 #include "public/web/WebPageSerializerClient.h" |
| 55 #include "public/web/WebView.h" | 56 #include "public/web/WebView.h" |
| 56 #include "web/WebLocalFrameImpl.h" | 57 #include "web/WebLocalFrameImpl.h" |
| 57 #include "web/WebPageSerializerImpl.h" | 58 #include "web/WebPageSerializerImpl.h" |
| 58 #include "web/WebViewImpl.h" | 59 #include "web/WebViewImpl.h" |
| 60 #include "wtf/Assertions.h" |
| 61 #include "wtf/HashMap.h" |
| 59 #include "wtf/Vector.h" | 62 #include "wtf/Vector.h" |
| 60 #include "wtf/text/StringConcatenate.h" | 63 #include "wtf/text/StringConcatenate.h" |
| 61 | 64 |
| 62 namespace blink { | 65 namespace blink { |
| 63 | 66 |
| 64 namespace { | 67 namespace { |
| 65 | 68 |
| 66 class MHTMLPageSerializerDelegate final : public PageSerializer::Delegate { | 69 class MHTMLPageSerializerDelegate final : public PageSerializer::Delegate { |
| 67 public: | 70 public: |
| 68 ~MHTMLPageSerializerDelegate() override; | 71 MHTMLPageSerializerDelegate(HashMap<Frame*, String>* frameToContentID); |
| 69 bool shouldIgnoreAttribute(const Attribute&) override; | 72 bool shouldIgnoreAttribute(const Attribute&) override; |
| 73 String rewriteLink(const Element&) override; |
| 74 private: |
| 75 HashMap<Frame*, String>* m_frameToContentID; |
| 70 }; | 76 }; |
| 71 | 77 |
| 72 | 78 MHTMLPageSerializerDelegate::MHTMLPageSerializerDelegate( |
| 73 MHTMLPageSerializerDelegate::~MHTMLPageSerializerDelegate() | 79 HashMap<Frame*, String>* frameToContentID) |
| 80 : m_frameToContentID(frameToContentID) |
| 74 { | 81 { |
| 75 } | 82 } |
| 76 | 83 |
| 77 bool MHTMLPageSerializerDelegate::shouldIgnoreAttribute(const Attribute& attribu
te) | 84 bool MHTMLPageSerializerDelegate::shouldIgnoreAttribute(const Attribute& attribu
te) |
| 78 { | 85 { |
| 79 // TODO(fgorski): Presence of srcset attribute causes MHTML to not display i
mages, as only the value of src | 86 // TODO(fgorski): Presence of srcset attribute causes MHTML to not display i
mages, as only the value of src |
| 80 // is pulled into the archive. Discarding srcset prevents the problem. Long
term we should make sure to MHTML | 87 // is pulled into the archive. Discarding srcset prevents the problem. Long
term we should make sure to MHTML |
| 81 // plays nicely with srcset. | 88 // plays nicely with srcset. |
| 82 return attribute.localName() == HTMLNames::srcsetAttr; | 89 return attribute.localName() == HTMLNames::srcsetAttr; |
| 83 } | 90 } |
| 84 | 91 |
| 92 String MHTMLPageSerializerDelegate::rewriteLink(const Element& element) |
| 93 { |
| 94 if (!element.isFrameOwnerElement()) |
| 95 return String(); |
| 96 |
| 97 auto* frameOwnerElement = toHTMLFrameOwnerElement(&element); |
| 98 Frame* frame = frameOwnerElement->contentFrame(); |
| 99 if (!frame) |
| 100 return String(); |
| 101 |
| 102 KURL cidURI = MHTMLParser::convertContentIDToURI(m_frameToContentID->get(fra
me)); |
| 103 ASSERT(cidURI.isValid()); |
| 104 |
| 105 if (isHTMLFrameElementBase(&element)) |
| 106 return cidURI.string(); |
| 107 |
| 108 if (isHTMLObjectElement(&element)) { |
| 109 Document* doc = frameOwnerElement->contentDocument(); |
| 110 bool isHandledBySerializer = doc->isHTMLDocument() |
| 111 || doc->isXHTMLDocument() || doc->isImageDocument(); |
| 112 if (isHandledBySerializer) |
| 113 return cidURI.string(); |
| 114 } |
| 115 |
| 116 return String(); |
| 117 } |
| 118 |
| 85 } // namespace | 119 } // namespace |
| 86 | 120 |
| 121 static HashMap<Frame*, String> generateFrameContentIDs(Page* page) |
| 122 { |
| 123 HashMap<Frame*, String> frameToContentID; |
| 124 int frameID = 0; |
| 125 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree().traverse
Next()) { |
| 126 // TODO(lukasza): Move cid generation to the browser + use base/guid.h |
| 127 // (see the draft at crrev.com/1386873003). |
| 128 StringBuilder contentIDBuilder; |
| 129 contentIDBuilder.appendLiteral("<frame"); |
| 130 contentIDBuilder.appendNumber(frameID++); |
| 131 contentIDBuilder.appendLiteral("@mhtml.blink>"); |
| 132 |
| 133 frameToContentID.add(frame, contentIDBuilder.toString()); |
| 134 } |
| 135 return frameToContentID; |
| 136 } |
| 137 |
| 87 static PassRefPtr<SharedBuffer> serializePageToMHTML(Page* page, MHTMLArchive::E
ncodingPolicy encodingPolicy) | 138 static PassRefPtr<SharedBuffer> serializePageToMHTML(Page* page, MHTMLArchive::E
ncodingPolicy encodingPolicy) |
| 88 { | 139 { |
| 89 Vector<SerializedResource> resources; | 140 Vector<SerializedResource> resources; |
| 90 PageSerializer serializer(&resources, adoptPtr(new MHTMLPageSerializerDelega
te)); | 141 HashMap<Frame*, String> frameToContentID = generateFrameContentIDs(page); |
| 142 MHTMLPageSerializerDelegate delegate(&frameToContentID); |
| 143 PageSerializer serializer(&resources, &delegate); |
| 91 | 144 |
| 92 RefPtr<SharedBuffer> output = SharedBuffer::create(); | 145 RefPtr<SharedBuffer> output = SharedBuffer::create(); |
| 93 String boundary = MHTMLArchive::generateMHTMLBoundary(); | 146 String boundary = MHTMLArchive::generateMHTMLBoundary(); |
| 94 | 147 |
| 95 Document* document = page->deprecatedLocalMainFrame()->document(); | 148 Document* document = page->deprecatedLocalMainFrame()->document(); |
| 96 MHTMLArchive::generateMHTMLHeader( | 149 MHTMLArchive::generateMHTMLHeader( |
| 97 boundary, document->title(), document->suggestedMIMEType(), *output); | 150 boundary, document->title(), document->suggestedMIMEType(), *output); |
| 98 | 151 |
| 99 for (Frame* frame = page->deprecatedLocalMainFrame(); frame; frame = frame->
tree().traverseNext()) { | 152 for (Frame* frame = page->deprecatedLocalMainFrame(); frame; frame = frame->
tree().traverseNext()) { |
| 100 // TODO(lukasza): This causes incomplete MHTML for OOPIFs. | 153 // TODO(lukasza): This causes incomplete MHTML for OOPIFs. |
| 101 // (crbug.com/538766) | 154 // (crbug.com/538766) |
| 102 if (!frame->isLocalFrame()) | 155 if (!frame->isLocalFrame()) |
| 103 continue; | 156 continue; |
| 104 | 157 |
| 105 resources.clear(); | 158 resources.clear(); |
| 106 serializer.serializeFrame(*toLocalFrame(frame)); | 159 serializer.serializeFrame(*toLocalFrame(frame)); |
| 107 | 160 |
| 108 for (const auto& resource : resources) { | 161 for (size_t i = 0; i < resources.size(); i++) { |
| 162 const SerializedResource& resource = resources[i]; |
| 163 |
| 164 // Frame (the 1st serialized resource) needs a Content-ID header. |
| 165 String contentID = (i == 0) ? frameToContentID.get(frame) : String()
; |
| 166 |
| 109 MHTMLArchive::generateMHTMLPart( | 167 MHTMLArchive::generateMHTMLPart( |
| 110 boundary, encodingPolicy, resource, *output); | 168 boundary, contentID, encodingPolicy, resource, *output); |
| 111 } | 169 } |
| 112 } | 170 } |
| 113 | 171 |
| 114 MHTMLArchive::generateMHTMLFooter(boundary, *output); | 172 MHTMLArchive::generateMHTMLFooter(boundary, *output); |
| 115 return output.release(); | 173 return output.release(); |
| 116 } | 174 } |
| 117 | 175 |
| 118 WebCString WebPageSerializer::serializeToMHTML(WebView* view) | 176 WebCString WebPageSerializer::serializeToMHTML(WebView* view) |
| 119 { | 177 { |
| 120 RefPtr<SharedBuffer> mhtml = serializePageToMHTML(toWebViewImpl(view)->page(
), MHTMLArchive::UseDefaultEncoding); | 178 RefPtr<SharedBuffer> mhtml = serializePageToMHTML(toWebViewImpl(view)->page(
), MHTMLArchive::UseDefaultEncoding); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 WebString WebPageSerializer::generateBaseTagDeclaration(const WebString& baseTar
get) | 217 WebString WebPageSerializer::generateBaseTagDeclaration(const WebString& baseTar
get) |
| 160 { | 218 { |
| 161 // TODO(yosin) We should call |PageSerializer::baseTagDeclarationOf()|. | 219 // TODO(yosin) We should call |PageSerializer::baseTagDeclarationOf()|. |
| 162 if (baseTarget.isEmpty()) | 220 if (baseTarget.isEmpty()) |
| 163 return String("<base href=\".\">"); | 221 return String("<base href=\".\">"); |
| 164 String baseString = "<base href=\".\" target=\"" + static_cast<const String&
>(baseTarget) + "\">"; | 222 String baseString = "<base href=\".\" target=\"" + static_cast<const String&
>(baseTarget) + "\">"; |
| 165 return baseString; | 223 return baseString; |
| 166 } | 224 } |
| 167 | 225 |
| 168 } // namespace blink | 226 } // namespace blink |
| OLD | NEW |