1/*
2 * Copyright (C) 2010. Adam Barth. 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
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Adam Barth. ("Adam Barth") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#pragma once
30
31#include <wtf/text/WTFString.h>
32
33namespace WebCore {
34
35class Document;
36class DocumentParser;
37class Frame;
38class TextResourceDecoder;
39
40class DocumentWriter {
41 WTF_MAKE_NONCOPYABLE(DocumentWriter);
42public:
43 DocumentWriter() = default;
44
45 // This is only called by ScriptController::executeIfJavaScriptURL
46 // and always contains the result of evaluating a javascript: url.
47 void replaceDocument(const String&, Document* ownerDocument);
48
49 bool begin();
50 bool begin(const URL&, bool dispatchWindowObjectAvailable = true, Document* ownerDocument = nullptr);
51 void addData(const char* bytes, size_t length);
52 void insertDataSynchronously(const String&); // For an internal use only to prevent the parser from yielding.
53 WEBCORE_EXPORT void end();
54
55 void setFrame(Frame& frame) { m_frame = &frame; }
56
57 WEBCORE_EXPORT void setEncoding(const String& encoding, bool userChosen);
58
59 const String& mimeType() const { return m_mimeType; }
60 void setMIMEType(const String& type) { m_mimeType = type; }
61
62 // Exposed for DocumentParser::appendBytes.
63 TextResourceDecoder& decoder();
64 void reportDataReceived();
65
66 void setDocumentWasLoadedAsPartOfNavigation();
67
68private:
69 Ref<Document> createDocument(const URL&);
70 void clear();
71
72 Frame* m_frame { nullptr };
73
74 bool m_hasReceivedSomeData { false };
75 String m_mimeType;
76
77 bool m_encodingWasChosenByUser { false };
78 String m_encoding;
79 RefPtr<TextResourceDecoder> m_decoder;
80 RefPtr<DocumentParser> m_parser;
81
82 enum class State { NotStarted, Started, Finished };
83 State m_state { State::NotStarted };
84};
85
86} // namespace WebCore
87