Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(558)

Side by Side Diff: Source/core/page/PageSerializer.cpp

Issue 1177733002: Merge page serializers [11/12] (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fix issues Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 HTMLAttributeList attributeList; 80 HTMLAttributeList attributeList;
81 AttributeCollection attributes = element.attributes(); 81 AttributeCollection attributes = element.attributes();
82 for (const Attribute& attr: attributes) { 82 for (const Attribute& attr: attributes) {
83 // FIXME: We should deal appropriately with the attribute if they have a namespace. 83 // FIXME: We should deal appropriately with the attribute if they have a namespace.
84 attributeList.append(std::make_pair(attr.name().localName(), attr.value( ).string())); 84 attributeList.append(std::make_pair(attr.name().localName(), attr.value( ).string()));
85 } 85 }
86 WTF::TextEncoding textEncoding = encodingFromMetaAttributes(attributeList); 86 WTF::TextEncoding textEncoding = encodingFromMetaAttributes(attributeList);
87 return textEncoding.isValid(); 87 return textEncoding.isValid();
88 } 88 }
89 89
90 static bool shouldIgnoreElement(const Element& element)
91 {
92 return isHTMLScriptElement(element) || isHTMLNoScriptElement(element) || isC harsetSpecifyingNode(element);
93 }
94
95 static const QualifiedName& frameOwnerURLAttributeName(const HTMLFrameOwnerEleme nt& frameOwner) 90 static const QualifiedName& frameOwnerURLAttributeName(const HTMLFrameOwnerEleme nt& frameOwner)
96 { 91 {
97 // FIXME: We should support all frame owners including applets. 92 // FIXME: We should support all frame owners including applets.
98 return isHTMLObjectElement(frameOwner) ? HTMLNames::dataAttr : HTMLNames::sr cAttr; 93 return isHTMLObjectElement(frameOwner) ? HTMLNames::dataAttr : HTMLNames::sr cAttr;
99 } 94 }
100 95
101 class SerializerMarkupAccumulator final : public MarkupAccumulator { 96 class SerializerMarkupAccumulator : public MarkupAccumulator {
102 STACK_ALLOCATED(); 97 STACK_ALLOCATED();
103 public: 98 public:
104 SerializerMarkupAccumulator(PageSerializer*, const Document&, WillBeHeapVect or<RawPtrWillBeMember<Node>>&); 99 SerializerMarkupAccumulator(PageSerializer*, const Document&, WillBeHeapVect or<RawPtrWillBeMember<Node>>&);
105 virtual ~SerializerMarkupAccumulator(); 100 virtual ~SerializerMarkupAccumulator();
106 101
107 protected: 102 protected:
108 virtual void appendText(StringBuilder& out, Text&) override; 103 virtual void appendText(StringBuilder& out, Text&) override;
109 virtual bool shouldIgnoreAttribute(const Attribute&) override; 104 virtual bool shouldIgnoreAttribute(const Attribute&) override;
110 virtual void appendElement(StringBuilder& out, Element&, Namespaces*) overri de; 105 virtual void appendElement(StringBuilder& out, Element&, Namespaces*) overri de;
111 virtual void appendCustomAttributes(StringBuilder& out, const Element&, Name spaces*) override; 106 virtual void appendCustomAttributes(StringBuilder& out, const Element&, Name spaces*) override;
112 virtual void appendStartTag(Node&, Namespaces* = nullptr) override; 107 virtual void appendStartTag(Node&, Namespaces* = nullptr) override;
113 virtual void appendEndTag(const Element&) override; 108 virtual void appendEndTag(const Element&) override;
114 109
110 virtual bool shouldIgnoreElement(const Element&);
yosin_UTC9 2015/06/15 09:34:37 Can we mark this function |const|?
Tiger (Sony Mobile) 2015/06/15 11:26:45 Done.
111
112 PageSerializer* getPageSerializer();
113 const Document& getDocument();
114
115 private: 115 private:
116 PageSerializer* m_serializer; 116 PageSerializer* m_serializer;
117 RawPtrWillBeMember<const Document> m_document; 117 RawPtrWillBeMember<const Document> m_document;
118 118
119 // FIXME: |PageSerializer| uses |m_nodes| for collecting nodes in document 119 // FIXME: |PageSerializer| uses |m_nodes| for collecting nodes in document
120 // included into serialized text then extracts image, object, etc. The size 120 // included into serialized text then extracts image, object, etc. The size
121 // of this vector isn't small for large document. It is better to use 121 // of this vector isn't small for large document. It is better to use
122 // callback like functionality. 122 // callback like functionality.
123 WillBeHeapVector<RawPtrWillBeMember<Node>>& m_nodes; 123 WillBeHeapVector<RawPtrWillBeMember<Node>>& m_nodes;
124 }; 124 };
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 MarkupAccumulator::appendStartTag(node, namespaces); 196 MarkupAccumulator::appendStartTag(node, namespaces);
197 m_nodes.append(&node); 197 m_nodes.append(&node);
198 } 198 }
199 199
200 void SerializerMarkupAccumulator::appendEndTag(const Element& element) 200 void SerializerMarkupAccumulator::appendEndTag(const Element& element)
201 { 201 {
202 if (!shouldIgnoreElement(element)) 202 if (!shouldIgnoreElement(element))
203 MarkupAccumulator::appendEndTag(element); 203 MarkupAccumulator::appendEndTag(element);
204 } 204 }
205 205
206 PageSerializer::PageSerializer(Vector<SerializedResource>* resources, PassOwnPtr <Delegate> delegate) 206 bool SerializerMarkupAccumulator::shouldIgnoreElement(const Element& element)
207 {
208 return isHTMLScriptElement(element) || isHTMLNoScriptElement(element) || isC harsetSpecifyingNode(element);
209 }
210
211 PageSerializer* SerializerMarkupAccumulator::getPageSerializer()
yosin_UTC9 2015/06/15 09:34:37 In Blink, we don't use "get" prefix.
Tiger (Sony Mobile) 2015/06/15 11:26:45 Done.
212 {
213 return m_serializer;
214 }
215
216 const Document& SerializerMarkupAccumulator::getDocument()
yosin_UTC9 2015/06/15 09:34:37 In Blink, we don't use "get" prefix.
Tiger (Sony Mobile) 2015/06/15 11:26:45 Done.
217 {
218 return *m_document;
219 }
220
221 class LinkChangeSerializerMarkupAccumulator : public SerializerMarkupAccumulator {
yosin_UTC9 2015/06/15 09:34:36 |final|?
Tiger (Sony Mobile) 2015/06/15 11:26:45 Done.
222 public:
223 LinkChangeSerializerMarkupAccumulator(PageSerializer*, const Document&, Will BeHeapVector<RawPtrWillBeMember<Node>>&, LinkLocalPathMap*, String);
224
225 protected:
226 virtual void appendElement(StringBuilder&, Element&, Namespaces*) override;
yosin_UTC9 2015/06/15 09:34:36 no need to have |virtual|.
Tiger (Sony Mobile) 2015/06/15 11:26:45 Done.
227 virtual void appendAttribute(StringBuilder&, const Element&, const Attribute &, Namespaces*) override;
yosin_UTC9 2015/06/15 09:34:37 no need to have |virtual|.
Tiger (Sony Mobile) 2015/06/15 11:26:45 Done.
228
229 virtual bool shouldIgnoreElement(const Element&);
yosin_UTC9 2015/06/15 09:34:37 Please use |override| instead of |virtual|.
Tiger (Sony Mobile) 2015/06/15 11:26:45 Done.
230
231 private:
232 // m_replaceLinks include all pair of local resource path and corresponding original link.
233 LinkLocalPathMap* m_replaceLinks;
234 String m_directoryName;
235 };
236
237 LinkChangeSerializerMarkupAccumulator::LinkChangeSerializerMarkupAccumulator(Pag eSerializer* serializer, const Document& document, WillBeHeapVector<RawPtrWillB eMember<Node>>& nodes, LinkLocalPathMap* links, String directoryName)
238 : SerializerMarkupAccumulator(serializer, document, nodes)
239 , m_replaceLinks(links)
240 , m_directoryName(directoryName)
241 {
242 }
243
244 void LinkChangeSerializerMarkupAccumulator::appendElement(StringBuilder& result, Element& element, Namespaces* namespaces)
245 {
246 if (element.hasTagName(HTMLNames::htmlTag)) {
247 // Add MOTW (Mark of the Web) declaration before html tag.
248 // See http://msdn2.microsoft.com/en-us/library/ms537628(VS.85).aspx.
249 result.append('\n');
250 MarkupFormatter::appendComment(result, String::format(" saved from url=( %04d)%s ",
251 static_cast<int>(getDocument().url().string().utf8().length()),
252 getDocument().url().string().utf8().data()));
253 result.append('\n');
254 }
255
256 SerializerMarkupAccumulator::appendElement(result, element, namespaces);
257
258 if (element.hasTagName(HTMLNames::baseTag)) {
259 // TODO(tiger): Refactor MarkupAccumulator so it is easier to append an element like this, without special cases for XHTML
260 // Append a new base tag declaration.
261 result.appendLiteral("<base href=\".\"");
262 if (!getDocument().baseTarget().isEmpty()) {
263 result.appendLiteral(" target=\"");
264 result.append(getDocument().baseTarget());
265 result.append('"');
266 }
267 if (getDocument().isXHTMLDocument())
268 result.appendLiteral(" />");
269 else
270 result.appendLiteral(">");
271 }
272 }
273
274 void LinkChangeSerializerMarkupAccumulator::appendAttribute(StringBuilder& resul t, const Element& element, const Attribute& attribute, Namespaces* namespaces)
275 {
276 if (m_replaceLinks && element.isURLAttribute(attribute) && !element.isJavaSc riptURLAttribute(attribute)) {
277
278 String completeURL = getDocument().completeURL(attribute.value());
279
280 if (m_replaceLinks->contains(completeURL)) {
281 // TODO(tiger): Refactor MarkupAccumulator so it is easier to append an attribute like this.
282 result.append(' ');
283 result.append(attribute.name().toString());
284 result.appendLiteral("=\"");
285 if (!m_directoryName.isEmpty()) {
286 result.appendLiteral("./");
287 MarkupFormatter::appendAttributeValue(result, m_directoryName, g etDocument().isHTMLDocument());
288 result.append('/');
289 }
290 MarkupFormatter::appendAttributeValue(result, m_replaceLinks->get(co mpleteURL), getDocument().isHTMLDocument());
291 result.appendLiteral("\"");
292 return;
293 }
294 }
295 MarkupAccumulator::appendAttribute(result, element, attribute, namespaces);
296 }
297
298 bool LinkChangeSerializerMarkupAccumulator::shouldIgnoreElement(const Element& e lement)
299 {
300 return SerializerMarkupAccumulator::shouldIgnoreElement(element) || isHTMLBa seElement(element);
301 }
302
303
304 PageSerializer::PageSerializer(Vector<SerializedResource>* resources, PassOwnPtr <Delegate> delegate, LinkLocalPathMap* urls, String directory)
207 : m_resources(resources) 305 : m_resources(resources)
306 , m_URLs(urls)
307 , m_directory(directory)
208 , m_blankFrameCounter(0) 308 , m_blankFrameCounter(0)
209 , m_delegate(delegate) 309 , m_delegate(delegate)
210 { 310 {
211 } 311 }
212 312
213 void PageSerializer::serialize(Page* page) 313 void PageSerializer::serialize(Page* page)
214 { 314 {
215 serializeFrame(page->deprecatedLocalMainFrame()); 315 serializeFrame(page->deprecatedLocalMainFrame());
216 } 316 }
217 317
(...skipping 16 matching lines...) Expand all
234 } 334 }
235 335
236 // If frame is an image document, add the image and don't continue 336 // If frame is an image document, add the image and don't continue
237 if (document.isImageDocument()) { 337 if (document.isImageDocument()) {
238 ImageDocument& imageDocument = toImageDocument(document); 338 ImageDocument& imageDocument = toImageDocument(document);
239 addImageToResources(imageDocument.cachedImage(), imageDocument.imageElem ent()->layoutObject(), url); 339 addImageToResources(imageDocument.cachedImage(), imageDocument.imageElem ent()->layoutObject(), url);
240 return; 340 return;
241 } 341 }
242 342
243 WillBeHeapVector<RawPtrWillBeMember<Node>> serializedNodes; 343 WillBeHeapVector<RawPtrWillBeMember<Node>> serializedNodes;
244 SerializerMarkupAccumulator accumulator(this, document, serializedNodes); 344 String text;
245 String text = serializeNodes<EditingStrategy>(accumulator, document, Include Node); 345 if (m_URLs) {
346 LinkChangeSerializerMarkupAccumulator accumulator(this, document, serial izedNodes, m_URLs, m_directory);
347 text = serializeNodes<EditingStrategy>(accumulator, document, IncludeNod e);
348 } else {
349 SerializerMarkupAccumulator accumulator(this, document, serializedNodes) ;
350 text = serializeNodes<EditingStrategy>(accumulator, document, IncludeNod e);
351 }
352
246 WTF::TextEncoding textEncoding(document.charset()); 353 WTF::TextEncoding textEncoding(document.charset());
247 CString frameHTML = textEncoding.normalizeAndEncode(text, WTF::EntitiesForUn encodables); 354 CString frameHTML = textEncoding.normalizeAndEncode(text, WTF::EntitiesForUn encodables);
248 m_resources->append(SerializedResource(url, document.suggestedMIMEType(), Sh aredBuffer::create(frameHTML.data(), frameHTML.length()))); 355 m_resources->append(SerializedResource(url, document.suggestedMIMEType(), Sh aredBuffer::create(frameHTML.data(), frameHTML.length())));
249 m_resourceURLs.add(url); 356 m_resourceURLs.add(url);
250 357
251 for (Node* node: serializedNodes) { 358 for (Node* node: serializedNodes) {
252 ASSERT(node); 359 ASSERT(node);
253 if (!node->isElementNode()) 360 if (!node->isElementNode())
254 continue; 361 continue;
255 362
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 531
425 return fakeURL; 532 return fakeURL;
426 } 533 }
427 534
428 PageSerializer::Delegate* PageSerializer::delegate() 535 PageSerializer::Delegate* PageSerializer::delegate()
429 { 536 {
430 return m_delegate.get(); 537 return m_delegate.get();
431 } 538 }
432 539
433 } // namespace blink 540 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698