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 11 matching lines...) Expand all Loading... |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 | |
33 #include "public/web/WebPageSerializer.h" | 32 #include "public/web/WebPageSerializer.h" |
34 | 33 |
| 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/html/HTMLAllCollection.h" | 38 #include "core/html/HTMLAllCollection.h" |
| 39 #include "core/html/HTMLFrameElementBase.h" |
39 #include "core/html/HTMLFrameOwnerElement.h" | 40 #include "core/html/HTMLFrameOwnerElement.h" |
40 #include "core/html/HTMLInputElement.h" | 41 #include "core/html/HTMLInputElement.h" |
41 #include "core/html/HTMLTableElement.h" | 42 #include "core/html/HTMLTableElement.h" |
42 #include "core/loader/DocumentLoader.h" | 43 #include "core/loader/DocumentLoader.h" |
43 #include "core/page/Page.h" | 44 #include "core/page/Page.h" |
44 #include "core/page/PageSerializer.h" | 45 #include "core/page/PageSerializer.h" |
45 #include "platform/SerializedResource.h" | 46 #include "platform/SerializedResource.h" |
46 #include "platform/mhtml/MHTMLArchive.h" | 47 #include "platform/mhtml/MHTMLArchive.h" |
47 #include "platform/weborigin/KURL.h" | 48 #include "platform/weborigin/KURL.h" |
48 #include "public/platform/WebCString.h" | 49 #include "public/platform/WebCString.h" |
49 #include "public/platform/WebString.h" | 50 #include "public/platform/WebString.h" |
50 #include "public/platform/WebURL.h" | 51 #include "public/platform/WebURL.h" |
51 #include "public/platform/WebVector.h" | 52 #include "public/platform/WebVector.h" |
52 #include "public/web/WebLocalFrame.h" | 53 #include "public/web/WebFrame.h" |
53 #include "public/web/WebPageSerializerClient.h" | 54 #include "public/web/WebPageSerializerClient.h" |
| 55 #include "public/web/WebView.h" |
| 56 #include "web/WebLocalFrameImpl.h" |
| 57 #include "web/WebPageSerializerImpl.h" |
54 #include "web/WebViewImpl.h" | 58 #include "web/WebViewImpl.h" |
55 #include "wtf/Vector.h" | 59 #include "wtf/Vector.h" |
56 #include "wtf/text/StringConcatenate.h" | 60 #include "wtf/text/StringConcatenate.h" |
57 | 61 |
58 namespace blink { | 62 namespace blink { |
59 | 63 |
60 namespace { | 64 namespace { |
61 | 65 |
| 66 KURL getSubResourceURLFromElement(Element* element) |
| 67 { |
| 68 ASSERT(element); |
| 69 const QualifiedName& attributeName = element->subResourceAttributeName(); |
| 70 if (attributeName == QualifiedName::null()) |
| 71 return KURL(); |
| 72 |
| 73 String value = element->getAttribute(attributeName); |
| 74 // Ignore javascript content. |
| 75 if (value.isEmpty() || value.stripWhiteSpace().startsWith("javascript:", Tex
tCaseInsensitive)) |
| 76 return KURL(); |
| 77 |
| 78 return element->document().completeURL(value); |
| 79 } |
| 80 |
| 81 void retrieveResourcesForElement(Element* element, |
| 82 Vector<LocalFrame*>* visitedFrames, |
| 83 Vector<LocalFrame*>* framesToVisit, |
| 84 Vector<KURL>* frameURLs, |
| 85 Vector<KURL>* resourceURLs) |
| 86 { |
| 87 ASSERT(element); |
| 88 // If the node is a frame, we'll process it later in retrieveResourcesForFra
me. |
| 89 if (isHTMLFrameElementBase(*element) || isHTMLObjectElement(*element) || isH
TMLEmbedElement(*element)) { |
| 90 Frame* frame = toHTMLFrameOwnerElement(element)->contentFrame(); |
| 91 if (frame && frame->isLocalFrame()) { |
| 92 if (!visitedFrames->contains(toLocalFrame(frame))) |
| 93 framesToVisit->append(toLocalFrame(frame)); |
| 94 return; |
| 95 } |
| 96 } |
| 97 |
| 98 KURL url = getSubResourceURLFromElement(element); |
| 99 if (url.isEmpty() || !url.isValid()) |
| 100 return; // No subresource for this node. |
| 101 |
| 102 // Ignore URLs that have a non-standard protocols. Since the FTP protocol |
| 103 // does no have a cache mechanism, we skip it as well. |
| 104 if (!url.protocolIsInHTTPFamily() && !url.isLocalFile()) |
| 105 return; |
| 106 |
| 107 if (!resourceURLs->contains(url)) |
| 108 resourceURLs->append(url); |
| 109 } |
| 110 |
| 111 void retrieveResourcesForFrame(LocalFrame* frame, |
| 112 const WebVector<WebCString>& supportedSchemes, |
| 113 Vector<LocalFrame*>* visitedFrames, |
| 114 Vector<LocalFrame*>* framesToVisit, |
| 115 Vector<KURL>* frameURLs, |
| 116 Vector<KURL>* resourceURLs) |
| 117 { |
| 118 KURL frameURL = frame->loader().documentLoader()->request().url(); |
| 119 |
| 120 // If the frame's URL is invalid, ignore it, it is not retrievable. |
| 121 if (!frameURL.isValid()) |
| 122 return; |
| 123 |
| 124 // Ignore frames from unsupported schemes. |
| 125 bool isValidScheme = false; |
| 126 for (size_t i = 0; i < supportedSchemes.size(); ++i) { |
| 127 if (frameURL.protocolIs(static_cast<CString>(supportedSchemes[i]).data()
)) { |
| 128 isValidScheme = true; |
| 129 break; |
| 130 } |
| 131 } |
| 132 if (!isValidScheme) |
| 133 return; |
| 134 |
| 135 // If we have already seen that frame, ignore it. |
| 136 if (visitedFrames->contains(frame)) |
| 137 return; |
| 138 visitedFrames->append(frame); |
| 139 if (!frameURLs->contains(frameURL)) |
| 140 frameURLs->append(frameURL); |
| 141 |
| 142 // Now get the resources associated with each node of the document. |
| 143 RefPtrWillBeRawPtr<HTMLAllCollection> allElements = frame->document()->all()
; |
| 144 for (unsigned i = 0; i < allElements->length(); ++i) { |
| 145 Element* element = allElements->item(i); |
| 146 retrieveResourcesForElement(element, |
| 147 visitedFrames, framesToVisit, |
| 148 frameURLs, resourceURLs); |
| 149 } |
| 150 } |
| 151 |
62 class MHTMLPageSerializerDelegate final : public PageSerializer::Delegate { | 152 class MHTMLPageSerializerDelegate final : public PageSerializer::Delegate { |
63 public: | 153 public: |
64 ~MHTMLPageSerializerDelegate() override; | 154 ~MHTMLPageSerializerDelegate() override; |
65 bool shouldIgnoreAttribute(const Attribute&) override; | 155 bool shouldIgnoreAttribute(const Attribute&) override; |
66 }; | 156 }; |
67 | 157 |
| 158 |
68 MHTMLPageSerializerDelegate::~MHTMLPageSerializerDelegate() | 159 MHTMLPageSerializerDelegate::~MHTMLPageSerializerDelegate() |
69 { | 160 { |
70 } | 161 } |
71 | 162 |
72 bool MHTMLPageSerializerDelegate::shouldIgnoreAttribute(const Attribute& attribu
te) | 163 bool MHTMLPageSerializerDelegate::shouldIgnoreAttribute(const Attribute& attribu
te) |
73 { | 164 { |
74 // TODO(fgorski): Presence of srcset attribute causes MHTML to not display i
mages, as only the value of src | 165 // TODO(fgorski): Presence of srcset attribute causes MHTML to not display i
mages, as only the value of src |
75 // is pulled into the archive. Discarding srcset prevents the problem. Long
term we should make sure to MHTML | 166 // is pulled into the archive. Discarding srcset prevents the problem. Long
term we should make sure to MHTML |
76 // plays nicely with srcset. | 167 // plays nicely with srcset. |
77 return attribute.localName() == HTMLNames::srcsetAttr; | 168 return attribute.localName() == HTMLNames::srcsetAttr; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 return WebCString(mhtml->data(), mhtml->size()); | 205 return WebCString(mhtml->data(), mhtml->size()); |
115 } | 206 } |
116 | 207 |
117 WebCString WebPageSerializer::serializeToMHTMLUsingBinaryEncoding(WebView* view) | 208 WebCString WebPageSerializer::serializeToMHTMLUsingBinaryEncoding(WebView* view) |
118 { | 209 { |
119 RefPtr<SharedBuffer> mhtml = serializePageToMHTML(toWebViewImpl(view)->page(
), MHTMLArchive::UseBinaryEncoding); | 210 RefPtr<SharedBuffer> mhtml = serializePageToMHTML(toWebViewImpl(view)->page(
), MHTMLArchive::UseBinaryEncoding); |
120 // FIXME: we are copying all the data here. Idealy we would have a WebShared
Data(). | 211 // FIXME: we are copying all the data here. Idealy we would have a WebShared
Data(). |
121 return WebCString(mhtml->data(), mhtml->size()); | 212 return WebCString(mhtml->data(), mhtml->size()); |
122 } | 213 } |
123 | 214 |
124 bool WebPageSerializer::serialize(WebLocalFrame* frame, bool recursive, WebPageS
erializerClient* client, | 215 bool WebPageSerializer::serialize(WebLocalFrame* frame, |
125 const WebVector<WebURL>& links, const WebVector<WebString>& localPaths, cons
t WebString& localDirectoryName) | 216 bool recursive, |
| 217 WebPageSerializerClient* client, |
| 218 const WebVector<WebURL>& links, |
| 219 const WebVector<WebString>& localPaths, |
| 220 const WebString& localDirectoryName) |
126 { | 221 { |
127 ASSERT(frame); | 222 WebPageSerializerImpl serializerImpl( |
128 ASSERT(client); | 223 frame, recursive, client, links, localPaths, localDirectoryName); |
129 ASSERT(links.size() == localPaths.size()); | 224 return serializerImpl.serialize(); |
| 225 } |
130 | 226 |
131 Vector<SerializedResource> resources; | 227 bool WebPageSerializer::retrieveAllResources(WebView* view, |
132 PageSerializer serializer(&resources, nullptr); | 228 const WebVector<WebCString>& suppor
tedSchemes, |
| 229 WebVector<WebURL>* resourceURLs, |
| 230 WebVector<WebURL>* frameURLs) { |
| 231 WebLocalFrameImpl* mainFrame = toWebLocalFrameImpl(view->mainFrame()); |
| 232 if (!mainFrame) |
| 233 return false; |
133 | 234 |
134 serializer.setRewriteURLFolder(localDirectoryName); | 235 Vector<LocalFrame*> framesToVisit; |
135 for (size_t i = 0; i < links.size(); i++) { | 236 Vector<LocalFrame*> visitedFrames; |
136 KURL url = links[i]; | 237 Vector<KURL> frameKURLs; |
137 serializer.registerRewriteURL(url.string(), localPaths[i]); | 238 Vector<KURL> resourceKURLs; |
| 239 |
| 240 // Let's retrieve the resources from every frame in this page. |
| 241 framesToVisit.append(mainFrame->frame()); |
| 242 while (!framesToVisit.isEmpty()) { |
| 243 LocalFrame* frame = framesToVisit[0]; |
| 244 framesToVisit.remove(0); |
| 245 retrieveResourcesForFrame(frame, supportedSchemes, |
| 246 &visitedFrames, &framesToVisit, |
| 247 &frameKURLs, &resourceKURLs); |
138 } | 248 } |
139 | 249 |
140 serializer.serialize(toWebViewImpl(frame->view())->page()); | 250 // Converts the results to WebURLs. |
| 251 WebVector<WebURL> resultResourceURLs(resourceKURLs.size()); |
| 252 for (size_t i = 0; i < resourceKURLs.size(); ++i) { |
| 253 resultResourceURLs[i] = resourceKURLs[i]; |
| 254 // A frame's src can point to the same URL as another resource, keep the |
| 255 // resource URL only in such cases. |
| 256 size_t index = frameKURLs.find(resourceKURLs[i]); |
| 257 if (index != kNotFound) |
| 258 frameKURLs.remove(index); |
| 259 } |
| 260 *resourceURLs = resultResourceURLs; |
| 261 WebVector<WebURL> resultFrameURLs(frameKURLs.size()); |
| 262 for (size_t i = 0; i < frameKURLs.size(); ++i) |
| 263 resultFrameURLs[i] = frameKURLs[i]; |
| 264 *frameURLs = resultFrameURLs; |
141 | 265 |
142 for (SerializedResource& resource : resources) { | |
143 client->didSerializeDataForFrame(resource.url, WebCString(resource.data-
>data(), resource.data->size()), WebPageSerializerClient::CurrentFrameIsFinished
); | |
144 } | |
145 client->didSerializeDataForFrame(KURL(), WebCString("", 0), WebPageSerialize
rClient::AllFramesAreFinished); | |
146 return true; | 266 return true; |
147 } | 267 } |
148 | 268 |
149 WebString WebPageSerializer::generateMetaCharsetDeclaration(const WebString& cha
rset) | 269 WebString WebPageSerializer::generateMetaCharsetDeclaration(const WebString& cha
rset) |
150 { | 270 { |
151 String charsetString = "<meta http-equiv=\"Content-Type\" content=\"text/htm
l; charset=" + static_cast<const String&>(charset) + "\">"; | 271 String charsetString = "<meta http-equiv=\"Content-Type\" content=\"text/htm
l; charset=" + static_cast<const String&>(charset) + "\">"; |
152 return charsetString; | 272 return charsetString; |
153 } | 273 } |
154 | 274 |
155 WebString WebPageSerializer::generateMarkOfTheWebDeclaration(const WebURL& url) | 275 WebString WebPageSerializer::generateMarkOfTheWebDeclaration(const WebURL& url) |
156 { | 276 { |
157 return String::format("\n<!-- saved from url=(%04d)%s -->\n", | 277 return String::format("\n<!-- saved from url=(%04d)%s -->\n", |
158 static_cast<int>(url.spec().length()), url.spec().data()); | 278 static_cast<int>(url.spec().length()), |
| 279 url.spec().data()); |
159 } | 280 } |
160 | 281 |
161 WebString WebPageSerializer::generateBaseTagDeclaration(const WebString& baseTar
get) | 282 WebString WebPageSerializer::generateBaseTagDeclaration(const WebString& baseTar
get) |
162 { | 283 { |
163 if (baseTarget.isEmpty()) | 284 if (baseTarget.isEmpty()) |
164 return String("<base href=\".\">"); | 285 return String("<base href=\".\">"); |
165 String baseString = "<base href=\".\" target=\"" + static_cast<const String&
>(baseTarget) + "\">"; | 286 String baseString = "<base href=\".\" target=\"" + static_cast<const String&
>(baseTarget) + "\">"; |
166 return baseString; | 287 return baseString; |
167 } | 288 } |
168 | 289 |
169 } // namespace blink | 290 } // namespace blink |
OLD | NEW |