• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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"
44 
45 namespace WebCore {
46 
create(Document * document,const AtomicString & mimeType,const AtomicString & encoding,bool encodingUserChoosen)47 PassRefPtrWillBeRawPtr<DocumentWriter> DocumentWriter::create(Document* document, const AtomicString& mimeType, const AtomicString& encoding, bool encodingUserChoosen)
48 {
49     return adoptRefWillBeNoop(new DocumentWriter(document, mimeType, encoding, encodingUserChoosen));
50 }
51 
DocumentWriter(Document * document,const AtomicString & mimeType,const AtomicString & encoding,bool encodingUserChoosen)52 DocumentWriter::DocumentWriter(Document* document, const AtomicString& mimeType, const AtomicString& encoding, bool encodingUserChoosen)
53     : m_document(document)
54     , m_decoderBuilder(mimeType, encoding, encodingUserChoosen)
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 
~DocumentWriter()66 DocumentWriter::~DocumentWriter()
67 {
68 }
69 
trace(Visitor * visitor)70 void DocumentWriter::trace(Visitor* visitor)
71 {
72     visitor->trace(m_document);
73     visitor->trace(m_parser);
74 }
75 
appendReplacingData(const String & source)76 void 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 
addData(const char * bytes,size_t length)90 void 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 
end()102 void 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     RefPtr<LocalFrame> protector(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     // flush() can result replacing DocumentLoader::m_writer.
119     RefPtrWillBeRawPtr<DocumentWriter> protectingThis(this);
120     m_parser->flush();
121 
122     if (!m_parser)
123         return;
124 
125     m_parser->finish();
126     m_parser = nullptr;
127     m_document = nullptr;
128 }
129 
setUserChosenEncoding(const String & charset)130 void DocumentWriter::setUserChosenEncoding(const String& charset)
131 {
132     TextResourceDecoder* decoder = m_parser->decoder();
133     if (decoder)
134         decoder->setEncoding(charset, TextResourceDecoder::UserChosenEncoding);
135 }
136 
setDocumentWasLoadedAsPartOfNavigation()137 void DocumentWriter::setDocumentWasLoadedAsPartOfNavigation()
138 {
139     ASSERT(m_parser && !m_parser->isStopped());
140     m_parser->setDocumentWasLoadedAsPartOfNavigation();
141 }
142 
143 } // namespace WebCore
144