OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2009 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
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. | |
29 */ | |
30 | |
31 #ifndef WebPageSerializerImpl_h | |
32 #define WebPageSerializerImpl_h | |
33 | |
34 #include "wtf/Forward.h" | |
35 #include "wtf/HashMap.h" | |
36 #include "wtf/Vector.h" | |
37 #include "wtf/text/StringBuilder.h" | |
38 #include "wtf/text/StringHash.h" | |
39 #include "wtf/text/WTFString.h" | |
40 | |
41 #include "public/platform/WebString.h" | |
42 #include "public/platform/WebURL.h" | |
43 #include "public/web/WebPageSerializer.h" | |
44 #include "public/web/WebPageSerializerClient.h" | |
45 #include "web/WebEntities.h" | |
46 | |
47 namespace WTF { | |
48 class TextEncoding; | |
49 } | |
50 | |
51 namespace blink { | |
52 | |
53 class Document; | |
54 class Element; | |
55 class Node; | |
56 class WebLocalFrame; | |
57 class WebLocalFrameImpl; | |
58 | |
59 // Responsible for serializing the specified frame into html | |
60 // (replacing links with paths to local files). | |
61 class WebPageSerializerImpl { | |
62 STACK_ALLOCATED(); | |
63 public: | |
64 // Do serialization action. | |
65 // | |
66 // Returns false to indicate that no data has been serialized (i.e. because | |
67 // the target frame didn't have a valid url). | |
68 // | |
69 // Synchronously calls WebPageSerializerClient methods to report | |
70 // serialization results. See WebPageSerializerClient comments for more | |
71 // details. | |
72 bool serialize(); | |
73 | |
74 // The parameter specifies which frame need to be serialized. | |
75 // The parameter delegate specifies the pointer of interface | |
76 // DomSerializerDelegate provide sink interface which can receive the | |
77 // individual chunks of data to be saved. | |
78 // The parameter urlsToLocalPaths contains a mapping between original URLs | |
79 // of saved resources and corresponding local file paths. | |
80 WebPageSerializerImpl( | |
81 WebLocalFrame*, | |
82 WebPageSerializerClient*, | |
83 const WebVector<std::pair<WebURL, WebString>>& urlsToLocalPaths); | |
84 | |
85 private: | |
86 // Specified frame which need to be serialized; | |
87 RawPtrWillBeMember<WebLocalFrameImpl> m_specifiedWebLocalFrameImpl; | |
88 // Pointer of WebPageSerializerClient | |
89 WebPageSerializerClient* m_client; | |
90 // This hash map is used to map resource URL of original link to its local | |
91 // file path. | |
92 typedef HashMap<WTF::String, WTF::String> LinkLocalPathMap; | |
93 // local_links_ include all pair of local resource path and corresponding | |
94 // original link. | |
95 LinkLocalPathMap m_localLinks; | |
96 // Data buffer for saving result of serialized DOM data. | |
97 StringBuilder m_dataBuffer; | |
98 | |
99 // Web entities conversion maps. | |
100 WebEntities m_htmlEntities; | |
101 WebEntities m_xmlEntities; | |
102 | |
103 class SerializeDomParam { | |
104 STACK_ALLOCATED(); | |
105 public: | |
106 SerializeDomParam(const KURL&, const WTF::TextEncoding&, Document*); | |
107 | |
108 const KURL& url; | |
109 const WTF::TextEncoding& textEncoding; | |
110 RawPtrWillBeMember<Document> document; | |
111 bool isHTMLDocument; // document.isHTMLDocument() | |
112 bool haveSeenDocType; | |
113 bool haveAddedCharsetDeclaration; | |
114 // This meta element need to be skipped when serializing DOM. | |
115 RawPtrWillBeMember<const Element> skipMetaElement; | |
116 // Flag indicates we are in script or style tag. | |
117 bool isInScriptOrStyleTag; | |
118 bool haveAddedXMLProcessingDirective; | |
119 // Flag indicates whether we have added additional contents before end t
ag. | |
120 // This flag will be re-assigned in each call of function | |
121 // PostActionAfterSerializeOpenTag and it could be changed in function | |
122 // PreActionBeforeSerializeEndTag if the function adds new contents into | |
123 // serialization stream. | |
124 bool haveAddedContentsBeforeEnd; | |
125 }; | |
126 | |
127 // Before we begin serializing open tag of a element, we give the target | |
128 // element a chance to do some work prior to add some additional data. | |
129 WTF::String preActionBeforeSerializeOpenTag(const Element*, | |
130 SerializeDomParam* param, | |
131 bool* needSkip); | |
132 // After we finish serializing open tag of a element, we give the target | |
133 // element a chance to do some post work to add some additional data. | |
134 WTF::String postActionAfterSerializeOpenTag(const Element*, | |
135 SerializeDomParam* param); | |
136 // Before we begin serializing end tag of a element, we give the target | |
137 // element a chance to do some work prior to add some additional data. | |
138 WTF::String preActionBeforeSerializeEndTag(const Element*, | |
139 SerializeDomParam* param, | |
140 bool* needSkip); | |
141 // After we finish serializing end tag of a element, we give the target | |
142 // element a chance to do some post work to add some additional data. | |
143 WTF::String postActionAfterSerializeEndTag(const Element*, | |
144 SerializeDomParam* param); | |
145 // Save generated html content to data buffer. | |
146 void saveHTMLContentToBuffer(const WTF::String& content, | |
147 SerializeDomParam* param); | |
148 | |
149 enum FlushOption { | |
150 ForceFlush, | |
151 DoNotForceFlush, | |
152 }; | |
153 | |
154 // Flushes the content buffer by encoding and sending the content to the | |
155 // WebPageSerializerClient. Content is not flushed if the buffer is not full | |
156 // unless force is 1. | |
157 void encodeAndFlushBuffer(WebPageSerializerClient::PageSerializationStatus s
tatus, | |
158 SerializeDomParam* param, | |
159 FlushOption); | |
160 // Serialize open tag of an specified element. | |
161 void openTagToString(Element*, | |
162 SerializeDomParam* param); | |
163 // Serialize end tag of an specified element. | |
164 void endTagToString(Element*, | |
165 SerializeDomParam* param); | |
166 // Build content for a specified node | |
167 void buildContentForNode(Node*, | |
168 SerializeDomParam* param); | |
169 }; | |
170 | |
171 } // namespace blink | |
172 | |
173 #endif | |
OLD | NEW |