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 17 matching lines...) Expand all Loading... |
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 "WebPageSerializer.h" | 32 #include "WebPageSerializer.h" |
33 | 33 |
34 #include "HTMLNames.h" | 34 #include "HTMLNames.h" |
35 #include "WebFrame.h" | 35 #include "WebFrame.h" |
36 #include "WebFrameImpl.h" | 36 #include "WebFrameImpl.h" |
37 #include "WebPageSerializerClient.h" | 37 #include "WebPageSerializerClient.h" |
| 38 #include "WebPageSerializerImpl.h" |
38 #include "WebView.h" | 39 #include "WebView.h" |
39 #include "WebViewImpl.h" | 40 #include "WebViewImpl.h" |
40 #include "core/dom/Document.h" | 41 #include "core/dom/Document.h" |
41 #include "core/dom/Element.h" | 42 #include "core/dom/Element.h" |
42 #include "core/html/HTMLAllCollection.h" | 43 #include "core/html/HTMLAllCollection.h" |
43 #include "core/html/HTMLFrameOwnerElement.h" | 44 #include "core/html/HTMLFrameOwnerElement.h" |
44 #include "core/html/HTMLInputElement.h" | 45 #include "core/html/HTMLInputElement.h" |
45 #include "core/html/HTMLTableElement.h" | 46 #include "core/html/HTMLTableElement.h" |
46 #include "core/loader/DocumentLoader.h" | 47 #include "core/loader/DocumentLoader.h" |
47 #include "core/frame/Frame.h" | 48 #include "core/frame/Frame.h" |
48 #include "core/page/PageSerializer.h" | 49 #include "core/page/PageSerializer.h" |
49 #include "platform/SerializedResource.h" | 50 #include "platform/SerializedResource.h" |
50 #include "platform/mhtml/MHTMLArchive.h" | 51 #include "platform/mhtml/MHTMLArchive.h" |
51 #include "platform/weborigin/KURL.h" | 52 #include "platform/weborigin/KURL.h" |
52 #include "public/platform/WebCString.h" | 53 #include "public/platform/WebCString.h" |
53 #include "public/platform/WebString.h" | 54 #include "public/platform/WebString.h" |
54 #include "public/platform/WebURL.h" | 55 #include "public/platform/WebURL.h" |
55 #include "public/platform/WebVector.h" | 56 #include "public/platform/WebVector.h" |
56 #include "wtf/Vector.h" | 57 #include "wtf/Vector.h" |
57 #include "wtf/text/StringConcatenate.h" | 58 #include "wtf/text/StringConcatenate.h" |
58 | 59 |
59 using namespace WebCore; | 60 using namespace WebCore; |
60 | 61 |
| 62 namespace { |
| 63 |
| 64 KURL getSubResourceURLFromElement(Element* element) |
| 65 { |
| 66 ASSERT(element); |
| 67 const QualifiedName* attributeName = 0; |
| 68 if (element->hasTagName(HTMLNames::imgTag) || element->hasTagName(HTMLNames:
:scriptTag)) |
| 69 attributeName = &HTMLNames::srcAttr; |
| 70 else if (element->hasTagName(HTMLNames::inputTag)) { |
| 71 if (toHTMLInputElement(element)->isImageButton()) |
| 72 attributeName = &HTMLNames::srcAttr; |
| 73 } else if (element->hasTagName(HTMLNames::bodyTag) |
| 74 || isHTMLTableElement(element) |
| 75 || element->hasTagName(HTMLNames::trTag) |
| 76 || element->hasTagName(HTMLNames::tdTag)) |
| 77 attributeName = &HTMLNames::backgroundAttr; |
| 78 else if (element->hasTagName(HTMLNames::blockquoteTag) |
| 79 || element->hasTagName(HTMLNames::qTag) |
| 80 || element->hasTagName(HTMLNames::delTag) |
| 81 || element->hasTagName(HTMLNames::insTag)) |
| 82 attributeName = &HTMLNames::citeAttr; |
| 83 else if (element->hasTagName(HTMLNames::linkTag)) { |
| 84 // If the link element is not css, ignore it. |
| 85 if (equalIgnoringCase(element->getAttribute(HTMLNames::typeAttr), "text/
css")) { |
| 86 // FIXME: Add support for extracting links of sub-resources which |
| 87 // are inside style-sheet such as @import, @font-face, url(), etc. |
| 88 attributeName = &HTMLNames::hrefAttr; |
| 89 } |
| 90 } else if (element->hasTagName(HTMLNames::objectTag)) |
| 91 attributeName = &HTMLNames::dataAttr; |
| 92 else if (element->hasTagName(HTMLNames::embedTag)) |
| 93 attributeName = &HTMLNames::srcAttr; |
| 94 |
| 95 if (!attributeName) |
| 96 return KURL(); |
| 97 |
| 98 String value = element->getAttribute(*attributeName); |
| 99 // Ignore javascript content. |
| 100 if (value.isEmpty() || value.stripWhiteSpace().startsWith("javascript:", fal
se)) |
| 101 return KURL(); |
| 102 |
| 103 return element->document().completeURL(value); |
| 104 } |
| 105 |
| 106 void retrieveResourcesForElement(Element* element, |
| 107 Vector<Frame*>* visitedFrames, |
| 108 Vector<Frame*>* framesToVisit, |
| 109 Vector<KURL>* frameURLs, |
| 110 Vector<KURL>* resourceURLs) |
| 111 { |
| 112 // If the node is a frame, we'll process it later in retrieveResourcesForFra
me. |
| 113 if ((element->hasTagName(HTMLNames::iframeTag) || element->hasTagName(HTMLNa
mes::frameTag) |
| 114 || element->hasTagName(HTMLNames::objectTag) || element->hasTagName(HTML
Names::embedTag)) |
| 115 && element->isFrameOwnerElement()) { |
| 116 if (Frame* frame = toHTMLFrameOwnerElement(element)->contentFrame()) { |
| 117 if (!visitedFrames->contains(frame)) |
| 118 framesToVisit->append(frame); |
| 119 return; |
| 120 } |
| 121 } |
| 122 |
| 123 KURL url = getSubResourceURLFromElement(element); |
| 124 if (url.isEmpty() || !url.isValid()) |
| 125 return; // No subresource for this node. |
| 126 |
| 127 // Ignore URLs that have a non-standard protocols. Since the FTP protocol |
| 128 // does no have a cache mechanism, we skip it as well. |
| 129 if (!url.protocolIsInHTTPFamily() && !url.isLocalFile()) |
| 130 return; |
| 131 |
| 132 if (!resourceURLs->contains(url)) |
| 133 resourceURLs->append(url); |
| 134 } |
| 135 |
| 136 void retrieveResourcesForFrame(Frame* frame, |
| 137 const blink::WebVector<blink::WebCString>& suppor
tedSchemes, |
| 138 Vector<Frame*>* visitedFrames, |
| 139 Vector<Frame*>* framesToVisit, |
| 140 Vector<KURL>* frameURLs, |
| 141 Vector<KURL>* resourceURLs) |
| 142 { |
| 143 KURL frameURL = frame->loader().documentLoader()->request().url(); |
| 144 |
| 145 // If the frame's URL is invalid, ignore it, it is not retrievable. |
| 146 if (!frameURL.isValid()) |
| 147 return; |
| 148 |
| 149 // Ignore frames from unsupported schemes. |
| 150 bool isValidScheme = false; |
| 151 for (size_t i = 0; i < supportedSchemes.size(); ++i) { |
| 152 if (frameURL.protocolIs(static_cast<CString>(supportedSchemes[i]).data()
)) { |
| 153 isValidScheme = true; |
| 154 break; |
| 155 } |
| 156 } |
| 157 if (!isValidScheme) |
| 158 return; |
| 159 |
| 160 // If we have already seen that frame, ignore it. |
| 161 if (visitedFrames->contains(frame)) |
| 162 return; |
| 163 visitedFrames->append(frame); |
| 164 if (!frameURLs->contains(frameURL)) |
| 165 frameURLs->append(frameURL); |
| 166 |
| 167 // Now get the resources associated with each node of the document. |
| 168 RefPtr<HTMLCollection> allNodes = frame->document()->all(); |
| 169 for (unsigned i = 0; i < allNodes->length(); ++i) { |
| 170 Node* node = allNodes->item(i); |
| 171 // We are only interested in HTML resources. |
| 172 if (!node->isElementNode()) |
| 173 continue; |
| 174 retrieveResourcesForElement(toElement(node), |
| 175 visitedFrames, framesToVisit, |
| 176 frameURLs, resourceURLs); |
| 177 } |
| 178 } |
| 179 |
| 180 } // namespace |
| 181 |
61 namespace blink { | 182 namespace blink { |
62 | 183 |
63 void WebPageSerializer::serialize(WebView* view, WebVector<WebPageSerializer::Re
source>* resourcesParam) | 184 void WebPageSerializer::serialize(WebView* view, WebVector<WebPageSerializer::Re
source>* resourcesParam) |
64 { | 185 { |
65 Vector<SerializedResource> resources; | 186 Vector<SerializedResource> resources; |
66 PageSerializer serializer(&resources); | 187 PageSerializer serializer(&resources); |
67 serializer.serialize(toWebViewImpl(view)->page()); | 188 serializer.serialize(toWebViewImpl(view)->page()); |
68 | 189 |
69 Vector<Resource> result; | 190 Vector<Resource> result; |
70 for (Vector<SerializedResource>::const_iterator iter = resources.begin(); it
er != resources.end(); ++iter) { | 191 for (Vector<SerializedResource>::const_iterator iter = resources.begin(); it
er != resources.end(); ++iter) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 return WebCString(mhtml->data(), mhtml->size()); | 223 return WebCString(mhtml->data(), mhtml->size()); |
103 } | 224 } |
104 | 225 |
105 bool WebPageSerializer::serialize(WebFrame* frame, | 226 bool WebPageSerializer::serialize(WebFrame* frame, |
106 bool recursive, | 227 bool recursive, |
107 WebPageSerializerClient* client, | 228 WebPageSerializerClient* client, |
108 const WebVector<WebURL>& links, | 229 const WebVector<WebURL>& links, |
109 const WebVector<WebString>& localPaths, | 230 const WebVector<WebString>& localPaths, |
110 const WebString& localDirectoryName) | 231 const WebString& localDirectoryName) |
111 { | 232 { |
112 ASSERT(frame); | 233 WebPageSerializerImpl serializerImpl( |
113 ASSERT(client); | 234 frame, recursive, client, links, localPaths, localDirectoryName); |
114 ASSERT(links.size() == localPaths.size()); | 235 return serializerImpl.serialize(); |
| 236 } |
115 | 237 |
116 LinkLocalPathMap m_localLinks; | 238 bool WebPageSerializer::retrieveAllResources(WebView* view, |
| 239 const WebVector<WebCString>& suppor
tedSchemes, |
| 240 WebVector<WebURL>* resourceURLs, |
| 241 WebVector<WebURL>* frameURLs) { |
| 242 WebFrameImpl* mainFrame = toWebFrameImpl(view->mainFrame()); |
| 243 if (!mainFrame) |
| 244 return false; |
117 | 245 |
118 for (size_t i = 0; i < links.size(); i++) { | 246 Vector<Frame*> framesToVisit; |
119 KURL url = links[i]; | 247 Vector<Frame*> visitedFrames; |
120 ASSERT(!m_localLinks.contains(url.string())); | 248 Vector<KURL> frameKURLs; |
121 m_localLinks.set(url.string(), localPaths[i]); | 249 Vector<KURL> resourceKURLs; |
| 250 |
| 251 // Let's retrieve the resources from every frame in this page. |
| 252 framesToVisit.append(mainFrame->frame()); |
| 253 while (!framesToVisit.isEmpty()) { |
| 254 Frame* frame = framesToVisit[0]; |
| 255 framesToVisit.remove(0); |
| 256 retrieveResourcesForFrame(frame, supportedSchemes, |
| 257 &visitedFrames, &framesToVisit, |
| 258 &frameKURLs, &resourceKURLs); |
122 } | 259 } |
123 | 260 |
124 Vector<SerializedResource> resources; | 261 // Converts the results to WebURLs. |
125 PageSerializer serializer(&resources, &m_localLinks, localDirectoryName); | 262 WebVector<WebURL> resultResourceURLs(resourceKURLs.size()); |
126 serializer.serialize(toWebViewImpl(frame->view())->page()); | 263 for (size_t i = 0; i < resourceKURLs.size(); ++i) { |
| 264 resultResourceURLs[i] = resourceKURLs[i]; |
| 265 // A frame's src can point to the same URL as another resource, keep the |
| 266 // resource URL only in such cases. |
| 267 size_t index = frameKURLs.find(resourceKURLs[i]); |
| 268 if (index != kNotFound) |
| 269 frameKURLs.remove(index); |
| 270 } |
| 271 *resourceURLs = resultResourceURLs; |
| 272 WebVector<WebURL> resultFrameURLs(frameKURLs.size()); |
| 273 for (size_t i = 0; i < frameKURLs.size(); ++i) |
| 274 resultFrameURLs[i] = frameKURLs[i]; |
| 275 *frameURLs = resultFrameURLs; |
127 | 276 |
128 for (Vector<SerializedResource>::const_iterator iter = resources.begin(); it
er != resources.end(); ++iter) { | |
129 client->didSerializeDataForFrame(iter->url, WebCString(iter->data->data(
), iter->data->size()), WebPageSerializerClient::CurrentFrameIsFinished); | |
130 } | |
131 client->didSerializeDataForFrame(KURL(), WebCString("", 0), WebPageSerialize
rClient::AllFramesAreFinished); | |
132 return true; | 277 return true; |
133 } | 278 } |
134 | 279 |
| 280 WebString WebPageSerializer::generateMetaCharsetDeclaration(const WebString& cha
rset) |
| 281 { |
| 282 String charsetString = "<meta http-equiv=\"Content-Type\" content=\"text/htm
l; charset=" + static_cast<const String&>(charset) + "\">"; |
| 283 return charsetString; |
| 284 } |
| 285 |
| 286 WebString WebPageSerializer::generateMarkOfTheWebDeclaration(const WebURL& url) |
| 287 { |
| 288 return String::format("\n<!-- saved from url=(%04d)%s -->\n", |
| 289 static_cast<int>(url.spec().length()), |
| 290 url.spec().data()); |
| 291 } |
| 292 |
| 293 WebString WebPageSerializer::generateBaseTagDeclaration(const WebString& baseTar
get) |
| 294 { |
| 295 if (baseTarget.isEmpty()) |
| 296 return String("<base href=\".\">"); |
| 297 String baseString = "<base href=\".\" target=\"" + static_cast<const String&
>(baseTarget) + "\">"; |
| 298 return baseString; |
| 299 } |
| 300 |
135 } // namespace blink | 301 } // namespace blink |
OLD | NEW |