2 * Copyright (C) 2010. Adam Barth. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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 Apple Computer, Inc. ("Apple") 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.
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.
30 #include "core/loader/DocumentWriter.h"
32 #include "core/dom/Document.h"
33 #include "core/dom/ScriptableDocumentParser.h"
34 #include "core/html/parser/TextResourceDecoder.h"
35 #include "core/loader/FrameLoader.h"
36 #include "core/loader/FrameLoaderStateMachine.h"
37 #include "core/frame/LocalDOMWindow.h"
38 #include "core/frame/FrameView.h"
39 #include "core/frame/LocalFrame.h"
40 #include "core/frame/Settings.h"
41 #include "platform/weborigin/KURL.h"
42 #include "platform/weborigin/SecurityOrigin.h"
43 #include "wtf/PassOwnPtr.h"
47 PassRefPtrWillBeRawPtr<DocumentWriter> DocumentWriter::create(Document* document, const AtomicString& mimeType, const AtomicString& encoding)
49 return adoptRefWillBeNoop(new DocumentWriter(document, mimeType, encoding));
52 DocumentWriter::DocumentWriter(Document* document, const AtomicString& mimeType, const AtomicString& encoding)
53 : m_document(document)
54 , m_decoderBuilder(mimeType, encoding)
55 // We grab a reference to the parser so that we'll always send data to the
56 // original parser, even if the document acquires a new parser (e.g., via
58 , m_parser(m_document->implicitOpen())
60 if (m_document->frame()) {
61 if (FrameView* view = m_document->frame()->view())
62 view->setContentsSize(IntSize());
66 DocumentWriter::~DocumentWriter()
70 void DocumentWriter::trace(Visitor* visitor)
72 visitor->trace(m_document);
73 visitor->trace(m_parser);
76 void DocumentWriter::appendReplacingData(const String& source)
78 m_document->setCompatibilityMode(Document::NoQuirksMode);
80 // FIXME: This should call DocumentParser::appendBytes instead of append
81 // to support RawDataDocumentParsers.
82 if (DocumentParser* parser = m_document->parser()) {
83 parser->pinToMainThread();
84 // Because we're pinned to the main thread we don't need to worry about
85 // passing ownership of the source string.
86 parser->append(source.impl());
90 void DocumentWriter::addData(const char* bytes, size_t length)
93 if (m_parser->needsDecoder() && 0 < length) {
94 OwnPtr<TextResourceDecoder> decoder = m_decoderBuilder.buildFor(m_document);
95 m_parser->setDecoder(decoder.release());
97 // appendBytes() can result replacing DocumentLoader::m_writer.
98 RefPtrWillBeRawPtr<DocumentWriter> protectingThis(this);
99 m_parser->appendBytes(bytes, length);
102 void DocumentWriter::end()
106 // http://bugs.webkit.org/show_bug.cgi?id=10854
107 // The frame's last ref may be removed and it can be deleted by checkCompleted(),
108 // so we'll add a protective refcount
109 RefPtr<LocalFrame> protector(m_document->frame());
114 if (m_parser->needsDecoder()) {
115 OwnPtr<TextResourceDecoder> decoder = m_decoderBuilder.buildFor(m_document);
116 m_parser->setDecoder(decoder.release());
118 // flush() can result replacing DocumentLoader::m_writer.
119 RefPtrWillBeRawPtr<DocumentWriter> protectingThis(this);
127 m_document = nullptr;
130 void DocumentWriter::setUserChosenEncoding(const String& charset)
132 TextResourceDecoder* decoder = m_parser->decoder();
134 decoder->setEncoding(charset, TextResourceDecoder::UserChosenEncoding);
137 void DocumentWriter::setDocumentWasLoadedAsPartOfNavigation()
139 ASSERT(m_parser && !m_parser->isStopped());
140 m_parser->setDocumentWasLoadedAsPartOfNavigation();