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 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.
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#include "config.h"
30#include "core/loader/DocumentWriter.h"
31
32#include "core/dom/Document.h"
33#include "core/dom/ScriptableDocumentParser.h"
34#include "core/frame/FrameView.h"
35#include "core/frame/LocalDOMWindow.h"
36#include "core/frame/LocalFrame.h"
37#include "core/frame/Settings.h"
38#include "core/html/parser/TextResourceDecoder.h"
39#include "core/loader/FrameLoader.h"
40#include "core/loader/FrameLoaderStateMachine.h"
41#include "platform/weborigin/KURL.h"
42#include "platform/weborigin/SecurityOrigin.h"
43#include "wtf/PassOwnPtr.h"
44
45namespace blink {
46
47PassRefPtrWillBeRawPtr<DocumentWriter> DocumentWriter::create(Document* document, const AtomicString& mimeType, const AtomicString& encoding)
48{
49    return adoptRefWillBeNoop(new DocumentWriter(document, mimeType, encoding));
50}
51
52DocumentWriter::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
57    // document.open).
58    , m_parser(m_document->implicitOpen())
59{
60    if (m_document->frame()) {
61        if (FrameView* view = m_document->frame()->view())
62            view->setContentsSize(IntSize());
63    }
64}
65
66DocumentWriter::~DocumentWriter()
67{
68}
69
70void DocumentWriter::trace(Visitor* visitor)
71{
72    visitor->trace(m_document);
73    visitor->trace(m_parser);
74}
75
76void DocumentWriter::appendReplacingData(const String& source)
77{
78    m_document->setCompatibilityMode(Document::NoQuirksMode);
79
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());
87    }
88}
89
90void DocumentWriter::addData(const char* bytes, size_t length)
91{
92    ASSERT(m_parser);
93    if (m_parser->needsDecoder() && 0 < length) {
94        OwnPtr<TextResourceDecoder> decoder = m_decoderBuilder.buildFor(m_document);
95        m_parser->setDecoder(decoder.release());
96    }
97    // appendBytes() can result replacing DocumentLoader::m_writer.
98    RefPtrWillBeRawPtr<DocumentWriter> protectingThis(this);
99    m_parser->appendBytes(bytes, length);
100}
101
102void DocumentWriter::end()
103{
104    ASSERT(m_document);
105
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    RefPtrWillBeRawPtr<LocalFrame> protect(m_document->frame());
110
111    if (!m_parser)
112        return;
113
114    if (m_parser->needsDecoder()) {
115        OwnPtr<TextResourceDecoder> decoder = m_decoderBuilder.buildFor(m_document);
116        m_parser->setDecoder(decoder.release());
117    }
118
119    // finish() can result replacing DocumentLoader::m_writer.
120    RefPtrWillBeRawPtr<DocumentWriter> protectingThis(this);
121    m_parser->finish();
122    m_parser = nullptr;
123    m_document = nullptr;
124}
125
126void DocumentWriter::setUserChosenEncoding(const String& charset)
127{
128    TextResourceDecoder* decoder = m_parser->decoder();
129    if (decoder)
130        decoder->setEncoding(charset, TextResourceDecoder::UserChosenEncoding);
131}
132
133void DocumentWriter::setDocumentWasLoadedAsPartOfNavigation()
134{
135    ASSERT(m_parser && !m_parser->isStopped());
136    m_parser->setDocumentWasLoadedAsPartOfNavigation();
137}
138
139} // namespace blink
140