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/fetch/TextResourceDecoder.h" 35 #include "core/loader/FrameLoader.h" 36 #include "core/loader/FrameLoaderStateMachine.h" 37 #include "core/frame/DOMWindow.h" 38 #include "core/frame/Frame.h" 39 #include "core/frame/FrameView.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 47 PassRefPtr<DocumentWriter> DocumentWriter::create(Document* document, const AtomicString& mimeType, const AtomicString& encoding, bool encodingUserChoosen) 48 { 49 return adoptRef(new DocumentWriter(document, mimeType, encoding, encodingUserChoosen)); 50 } 51 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 66 DocumentWriter::~DocumentWriter() 67 { 68 } 69 70 void DocumentWriter::appendReplacingData(const String& source) 71 { 72 m_document->setCompatibilityMode(Document::NoQuirksMode); 73 74 // FIXME: This should call DocumentParser::appendBytes instead of append 75 // to support RawDataDocumentParsers. 76 if (DocumentParser* parser = m_document->parser()) { 77 parser->pinToMainThread(); 78 // Because we're pinned to the main thread we don't need to worry about 79 // passing ownership of the source string. 80 parser->append(source.impl()); 81 parser->setHasAppendedData(); 82 } 83 } 84 85 void DocumentWriter::addData(const char* bytes, size_t length) 86 { 87 ASSERT(m_parser); 88 if (m_parser->needsDecoder() && 0 < length) { 89 OwnPtr<TextResourceDecoder> decoder = m_decoderBuilder.buildFor(m_document); 90 m_parser->setDecoder(decoder.release()); 91 } 92 // appendBytes() can result replacing DocumentLoader::m_writer. 93 RefPtr<DocumentWriter> protectingThis(this); 94 m_parser->appendBytes(bytes, length); 95 } 96 97 void DocumentWriter::end() 98 { 99 ASSERT(m_document); 100 101 // http://bugs.webkit.org/show_bug.cgi?id=10854 102 // The frame's last ref may be removed and it can be deleted by checkCompleted(), 103 // so we'll add a protective refcount 104 RefPtr<Frame> protector(m_document->frame()); 105 106 if (!m_parser) 107 return; 108 109 if (m_parser->needsDecoder()) { 110 OwnPtr<TextResourceDecoder> decoder = m_decoderBuilder.buildFor(m_document); 111 m_parser->setDecoder(decoder.release()); 112 } 113 // flush() can result replacing DocumentLoader::m_writer. 114 RefPtr<DocumentWriter> protectingThis(this); 115 m_parser->flush(); 116 117 if (!m_parser) 118 return; 119 120 m_parser->finish(); 121 m_parser = 0; 122 m_document = 0; 123 } 124 125 void DocumentWriter::setUserChosenEncoding(const String& charset) 126 { 127 TextResourceDecoder* decoder = m_parser->decoder(); 128 if (decoder) 129 decoder->setEncoding(charset, TextResourceDecoder::UserChosenEncoding); 130 } 131 132 void DocumentWriter::setDocumentWasLoadedAsPartOfNavigation() 133 { 134 ASSERT(m_parser && !m_parser->isStopped()); 135 m_parser->setDocumentWasLoadedAsPartOfNavigation(); 136 } 137 138 } // namespace WebCore 139