abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010. Adam Barth. All rights reserved. |
bfulgham@apple.com | 01c956f | 2016-08-16 20:32:57 +0000 | [diff] [blame] | 3 | * Copyright (C) 2016 Apple Inc. All rights reserved. |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
mjs@apple.com | 9204733 | 2014-03-15 04:08:27 +0000 | [diff] [blame] | 14 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 15 | * its contributors may be used to endorse or promote products derived |
| 16 | * from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 19 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include "config.h" |
| 31 | #include "DocumentWriter.h" |
| 32 | |
bfulgham@apple.com | b30d7f8 | 2016-06-07 15:47:30 +0000 | [diff] [blame] | 33 | #include "ContentSecurityPolicy.h" |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 34 | #include "DOMImplementation.h" |
abarth@webkit.org | 37bf553 | 2010-04-20 22:26:44 +0000 | [diff] [blame] | 35 | #include "DOMWindow.h" |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 36 | #include "Frame.h" |
| 37 | #include "FrameLoader.h" |
| 38 | #include "FrameLoaderClient.h" |
japhet@chromium.org | 61d4b9c | 2010-06-21 20:06:02 +0000 | [diff] [blame] | 39 | #include "FrameLoaderStateMachine.h" |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 40 | #include "FrameView.h" |
timothy_horton@apple.com | 1210554 | 2014-06-18 01:35:32 +0000 | [diff] [blame] | 41 | #include "MIMETypeRegistry.h" |
| 42 | #include "MainFrame.h" |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 43 | #include "PluginDocument.h" |
eric@webkit.org | e37f595 | 2010-06-28 07:56:10 +0000 | [diff] [blame] | 44 | #include "RawDataDocumentParser.h" |
weinig@apple.com | 123e4aa | 2013-04-14 00:54:27 +0000 | [diff] [blame] | 45 | #include "ScriptController.h" |
eric@webkit.org | e37f595 | 2010-06-28 07:56:10 +0000 | [diff] [blame] | 46 | #include "ScriptableDocumentParser.h" |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 47 | #include "SecurityOrigin.h" |
andersca@apple.com | a57d555 | 2014-12-22 23:42:30 +0000 | [diff] [blame] | 48 | #include "SecurityOriginPolicy.h" |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 49 | #include "SegmentedString.h" |
| 50 | #include "Settings.h" |
beidson@apple.com | 34f9451 | 2010-05-18 23:53:31 +0000 | [diff] [blame] | 51 | #include "SinkDocument.h" |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 52 | #include "TextResourceDecoder.h" |
akling@apple.com | f851598 | 2013-09-02 18:50:01 +0000 | [diff] [blame] | 53 | #include <wtf/Ref.h> |
eric@webkit.org | e37f595 | 2010-06-28 07:56:10 +0000 | [diff] [blame] | 54 | |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 55 | namespace WebCore { |
| 56 | |
| 57 | static inline bool canReferToParentFrameEncoding(const Frame* frame, const Frame* parentFrame) |
| 58 | { |
akling@apple.com | 6be0e97 | 2017-01-18 19:35:49 +0000 | [diff] [blame] | 59 | return parentFrame && parentFrame->document()->securityOrigin().canAccess(frame->document()->securityOrigin()); |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | DocumentWriter::DocumentWriter(Frame* frame) |
| 63 | : m_frame(frame) |
abarth@webkit.org | 99edc53 | 2011-06-12 00:51:11 +0000 | [diff] [blame] | 64 | , m_hasReceivedSomeData(false) |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 65 | , m_encodingWasChosenByUser(false) |
commit-queue@webkit.org | fe59ee0 | 2012-03-06 22:45:12 +0000 | [diff] [blame] | 66 | , m_state(NotStartedWritingState) |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 67 | { |
| 68 | } |
| 69 | |
eric@webkit.org | 5bfa04d | 2010-06-24 23:28:05 +0000 | [diff] [blame] | 70 | // This is only called by ScriptController::executeIfJavaScriptURL |
| 71 | // and always contains the result of evaluating a javascript: url. |
| 72 | // This is the <iframe src="javascript:'html'"> case. |
abarth@webkit.org | 1a142b7 | 2011-10-12 17:05:41 +0000 | [diff] [blame] | 73 | void DocumentWriter::replaceDocument(const String& source, Document* ownerDocument) |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 74 | { |
andersca@apple.com | df550b9 | 2013-08-15 22:17:17 +0000 | [diff] [blame] | 75 | m_frame->loader().stopAllLoaders(); |
bfulgham@apple.com | e815123 | 2016-12-23 00:20:27 +0000 | [diff] [blame] | 76 | |
| 77 | // If we are in the midst of changing the frame's document, don't execute script |
mcatanzaro@igalia.com | 458380d | 2017-02-09 13:57:45 +0000 | [diff] [blame] | 78 | // that modifies the document further: |
bfulgham@apple.com | e815123 | 2016-12-23 00:20:27 +0000 | [diff] [blame] | 79 | if (m_frame->documentIsBeingReplaced()) |
| 80 | return; |
| 81 | |
abarth@webkit.org | 1a142b7 | 2011-10-12 17:05:41 +0000 | [diff] [blame] | 82 | begin(m_frame->document()->url(), true, ownerDocument); |
eric@webkit.org | 5bfa04d | 2010-06-24 23:28:05 +0000 | [diff] [blame] | 83 | |
jiewen_tan@apple.com | 5533622 | 2015-12-02 19:04:07 +0000 | [diff] [blame] | 84 | // begin() might fire an unload event, which will result in a situation where no new document has been attached, |
| 85 | // and the old document has been detached. Therefore, bail out if no document is attached. |
| 86 | if (!m_frame->document()) |
| 87 | return; |
| 88 | |
eric@webkit.org | d80f85e | 2010-06-24 23:41:25 +0000 | [diff] [blame] | 89 | if (!source.isNull()) { |
abarth@webkit.org | 99edc53 | 2011-06-12 00:51:11 +0000 | [diff] [blame] | 90 | if (!m_hasReceivedSomeData) { |
| 91 | m_hasReceivedSomeData = true; |
benjamin@webkit.org | 6d7ab06 | 2014-05-30 00:15:18 +0000 | [diff] [blame] | 92 | m_frame->document()->setCompatibilityMode(DocumentCompatibilityMode::NoQuirksMode); |
eric@webkit.org | 5bfa04d | 2010-06-24 23:28:05 +0000 | [diff] [blame] | 93 | } |
eric@webkit.org | 71d0f46 | 2010-06-25 07:09:36 +0000 | [diff] [blame] | 94 | |
eric@webkit.org | 44a233c | 2010-06-25 23:14:00 +0000 | [diff] [blame] | 95 | // FIXME: This should call DocumentParser::appendBytes instead of append |
| 96 | // to support RawDataDocumentParsers. |
andersca@apple.com | 7256f67 | 2014-01-18 17:11:46 +0000 | [diff] [blame] | 97 | if (DocumentParser* parser = m_frame->document()->parser()) |
eric@webkit.org | 068ee69 | 2013-03-01 21:53:26 +0000 | [diff] [blame] | 98 | parser->append(source.impl()); |
eric@webkit.org | 5bfa04d | 2010-06-24 23:28:05 +0000 | [diff] [blame] | 99 | } |
| 100 | |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 101 | end(); |
| 102 | } |
| 103 | |
| 104 | void DocumentWriter::clear() |
| 105 | { |
cdumez@apple.com | d839ea1 | 2015-07-04 19:42:18 +0000 | [diff] [blame] | 106 | m_decoder = nullptr; |
abarth@webkit.org | 99edc53 | 2011-06-12 00:51:11 +0000 | [diff] [blame] | 107 | m_hasReceivedSomeData = false; |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 108 | if (!m_encodingWasChosenByUser) |
| 109 | m_encoding = String(); |
| 110 | } |
| 111 | |
| 112 | void DocumentWriter::begin() |
| 113 | { |
darin@apple.com | 5ffbb5c | 2013-09-27 16:39:41 +0000 | [diff] [blame] | 114 | begin(URL()); |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 115 | } |
| 116 | |
akling@apple.com | f3266b9 | 2015-08-09 08:11:32 +0000 | [diff] [blame] | 117 | Ref<Document> DocumentWriter::createDocument(const URL& url) |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 118 | { |
akling@apple.com | 63e53cb | 2014-02-05 20:42:37 +0000 | [diff] [blame] | 119 | if (!m_frame->loader().stateMachine().isDisplayingInitialEmptyDocument() && m_frame->loader().client().shouldAlwaysUsePluginDocument(m_mimeType)) |
japhet@chromium.org | 6a088ce | 2010-06-25 17:17:45 +0000 | [diff] [blame] | 120 | return PluginDocument::create(m_frame, url); |
dbates@webkit.org | 46a1c6f | 2013-12-19 18:10:07 +0000 | [diff] [blame] | 121 | #if PLATFORM(IOS) |
timothy_horton@apple.com | 1210554 | 2014-06-18 01:35:32 +0000 | [diff] [blame] | 122 | if (MIMETypeRegistry::isPDFMIMEType(m_mimeType) && (m_frame->isMainFrame() || !m_frame->settings().useImageDocumentForSubframePDF())) |
timothy_horton@apple.com | 8310992 | 2014-08-31 22:01:34 +0000 | [diff] [blame] | 123 | return SinkDocument::create(m_frame, url); |
dbates@webkit.org | 46a1c6f | 2013-12-19 18:10:07 +0000 | [diff] [blame] | 124 | #endif |
akling@apple.com | 7f50aae | 2013-08-22 16:01:31 +0000 | [diff] [blame] | 125 | if (!m_frame->loader().client().hasHTMLView()) |
antti@apple.com | e9bd326 | 2014-01-02 15:50:28 +0000 | [diff] [blame] | 126 | return Document::createNonRenderedPlaceholder(m_frame, url); |
andersca@apple.com | 003f466 | 2014-02-17 23:53:29 +0000 | [diff] [blame] | 127 | return DOMImplementation::createDocument(m_mimeType, m_frame, url); |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 128 | } |
| 129 | |
darin@apple.com | 5ffbb5c | 2013-09-27 16:39:41 +0000 | [diff] [blame] | 130 | void DocumentWriter::begin(const URL& urlReference, bool dispatch, Document* ownerDocument) |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 131 | { |
abarth@webkit.org | 0cb1dfc | 2011-08-30 21:31:57 +0000 | [diff] [blame] | 132 | // We grab a local copy of the URL because it's easy for callers to supply |
| 133 | // a URL that will be deallocated during the execution of this function. |
| 134 | // For example, see <https://bugs.webkit.org/show_bug.cgi?id=66360>. |
darin@apple.com | 5ffbb5c | 2013-09-27 16:39:41 +0000 | [diff] [blame] | 135 | URL url = urlReference; |
abarth@webkit.org | 0cb1dfc | 2011-08-30 21:31:57 +0000 | [diff] [blame] | 136 | |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 137 | // Create a new document before clearing the frame, because it may need to |
| 138 | // inherit an aliased security context. |
akling@apple.com | f3266b9 | 2015-08-09 08:11:32 +0000 | [diff] [blame] | 139 | Ref<Document> document = createDocument(url); |
beidson@apple.com | 34f9451 | 2010-05-18 23:53:31 +0000 | [diff] [blame] | 140 | |
| 141 | // If the new document is for a Plugin but we're supposed to be sandboxed from Plugins, |
abarth@webkit.org | 5d72ad2 | 2010-06-14 04:40:20 +0000 | [diff] [blame] | 142 | // then replace the document with one whose parser will ignore the incoming data (bug 39323) |
abarth@webkit.org | 29f0c1a | 2011-11-05 01:32:07 +0000 | [diff] [blame] | 143 | if (document->isPluginDocument() && document->isSandboxed(SandboxPlugins)) |
japhet@chromium.org | 6a088ce | 2010-06-25 17:17:45 +0000 | [diff] [blame] | 144 | document = SinkDocument::create(m_frame, url); |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 145 | |
abarth@webkit.org | 15f9e21 | 2011-04-07 04:43:50 +0000 | [diff] [blame] | 146 | // FIXME: Do we need to consult the content security policy here about blocked plug-ins? |
| 147 | |
akling@apple.com | 63e53cb | 2014-02-05 20:42:37 +0000 | [diff] [blame] | 148 | bool shouldReuseDefaultView = m_frame->loader().stateMachine().isDisplayingInitialEmptyDocument() && m_frame->document()->isSecureTransitionTo(url); |
abarth@webkit.org | 23ea90a | 2012-08-14 19:47:59 +0000 | [diff] [blame] | 149 | if (shouldReuseDefaultView) |
| 150 | document->takeDOMWindowFrom(m_frame->document()); |
| 151 | else |
| 152 | document->createDOMWindow(); |
| 153 | |
bfulgham@apple.com | b30d7f8 | 2016-06-07 15:47:30 +0000 | [diff] [blame] | 154 | // Per <http://www.w3.org/TR/upgrade-insecure-requests/>, we need to retain an ongoing set of upgraded |
| 155 | // requests in new navigation contexts. Although this information is present when we construct the |
| 156 | // Document object, it is discard in the subsequent 'clear' statements below. So, we must capture it |
| 157 | // so we can restore it. |
| 158 | HashSet<RefPtr<SecurityOrigin>> insecureNavigationRequestsToUpgrade; |
bfulgham@apple.com | 01c956f | 2016-08-16 20:32:57 +0000 | [diff] [blame] | 159 | if (auto* existingDocument = m_frame->document()) |
bfulgham@apple.com | b30d7f8 | 2016-06-07 15:47:30 +0000 | [diff] [blame] | 160 | insecureNavigationRequestsToUpgrade = existingDocument->contentSecurityPolicy()->takeNavigationRequestsToUpgrade(); |
bfulgham@apple.com | b30d7f8 | 2016-06-07 15:47:30 +0000 | [diff] [blame] | 161 | |
akling@apple.com | f3266b9 | 2015-08-09 08:11:32 +0000 | [diff] [blame] | 162 | m_frame->loader().clear(document.ptr(), !shouldReuseDefaultView, !shouldReuseDefaultView); |
japhet@chromium.org | af889b9 | 2011-02-11 17:28:06 +0000 | [diff] [blame] | 163 | clear(); |
abarth@webkit.org | 23ea90a | 2012-08-14 19:47:59 +0000 | [diff] [blame] | 164 | |
jiewen_tan@apple.com | 5533622 | 2015-12-02 19:04:07 +0000 | [diff] [blame] | 165 | // m_frame->loader().clear() might fire unload event which could remove the view of the document. |
| 166 | // Bail out if document has no view. |
| 167 | if (!document->view()) |
| 168 | return; |
| 169 | |
abarth@webkit.org | 23ea90a | 2012-08-14 19:47:59 +0000 | [diff] [blame] | 170 | if (!shouldReuseDefaultView) |
psolanki@apple.com | c5b5dad | 2013-08-16 17:55:32 +0000 | [diff] [blame] | 171 | m_frame->script().updatePlatformScriptObjects(); |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 172 | |
andersca@apple.com | df550b9 | 2013-08-15 22:17:17 +0000 | [diff] [blame] | 173 | m_frame->loader().setOutgoingReferrer(url); |
akling@apple.com | 6fbca0f | 2014-12-17 19:39:16 +0000 | [diff] [blame] | 174 | m_frame->setDocument(document.copyRef()); |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 175 | |
bfulgham@apple.com | b30d7f8 | 2016-06-07 15:47:30 +0000 | [diff] [blame] | 176 | document->contentSecurityPolicy()->setInsecureNavigationRequestsToUpgrade(WTFMove(insecureNavigationRequestsToUpgrade)); |
| 177 | |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 178 | if (m_decoder) |
| 179 | document->setDecoder(m_decoder.get()); |
abarth@webkit.org | 1a142b7 | 2011-10-12 17:05:41 +0000 | [diff] [blame] | 180 | if (ownerDocument) { |
| 181 | document->setCookieURL(ownerDocument->cookieURL()); |
andersca@apple.com | a57d555 | 2014-12-22 23:42:30 +0000 | [diff] [blame] | 182 | document->setSecurityOriginPolicy(ownerDocument->securityOriginPolicy()); |
dbates@webkit.org | 2abec1b | 2016-12-08 23:54:13 +0000 | [diff] [blame] | 183 | document->setStrictMixedContentMode(ownerDocument->isStrictMixedContentMode()); |
commit-queue@webkit.org | 9416cdd | 2011-09-28 21:01:37 +0000 | [diff] [blame] | 184 | } |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 185 | |
andersca@apple.com | df550b9 | 2013-08-15 22:17:17 +0000 | [diff] [blame] | 186 | m_frame->loader().didBeginDocument(dispatch); |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 187 | |
| 188 | document->implicitOpen(); |
| 189 | |
abarth@webkit.org | 99edc53 | 2011-06-12 00:51:11 +0000 | [diff] [blame] | 190 | // We grab a reference to the parser so that we'll always send data to the |
| 191 | // original parser, even if the document acquires a new parser (e.g., via |
| 192 | // document.open). |
abarth@webkit.org | 14a3bdf | 2011-06-11 00:56:13 +0000 | [diff] [blame] | 193 | m_parser = document->parser(); |
| 194 | |
akling@apple.com | 7f50aae | 2013-08-22 16:01:31 +0000 | [diff] [blame] | 195 | if (m_frame->view() && m_frame->loader().client().hasHTMLView()) |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 196 | m_frame->view()->setContentsSize(IntSize()); |
commit-queue@webkit.org | fe59ee0 | 2012-03-06 22:45:12 +0000 | [diff] [blame] | 197 | |
| 198 | m_state = StartedWritingState; |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 199 | } |
| 200 | |
eric@webkit.org | 44a233c | 2010-06-25 23:14:00 +0000 | [diff] [blame] | 201 | TextResourceDecoder* DocumentWriter::createDecoderIfNeeded() |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 202 | { |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 203 | if (!m_decoder) { |
akling@apple.com | 1752350 | 2013-08-17 10:58:40 +0000 | [diff] [blame] | 204 | m_decoder = TextResourceDecoder::create(m_mimeType, |
| 205 | m_frame->settings().defaultTextEncodingName(), |
| 206 | m_frame->settings().usesEncodingDetector()); |
darin@apple.com | fed4d16 | 2013-08-25 02:28:06 +0000 | [diff] [blame] | 207 | Frame* parentFrame = m_frame->tree().parent(); |
akling@apple.com | 1752350 | 2013-08-17 10:58:40 +0000 | [diff] [blame] | 208 | // Set the hint encoding to the parent frame encoding only if |
| 209 | // the parent and the current frames share the security origin. |
| 210 | // We impose this condition because somebody can make a child frame |
| 211 | // containing a carefully crafted html/javascript in one encoding |
| 212 | // that can be mistaken for hintEncoding (or related encoding) by |
| 213 | // an auto detector. When interpreted in the latter, it could be |
| 214 | // an attack vector. |
| 215 | // FIXME: This might be too cautious for non-7bit-encodings and |
| 216 | // we may consider relaxing this later after testing. |
| 217 | if (canReferToParentFrameEncoding(m_frame, parentFrame)) |
| 218 | m_decoder->setHintEncoding(parentFrame->document()->decoder()); |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 219 | if (m_encoding.isEmpty()) { |
| 220 | if (canReferToParentFrameEncoding(m_frame, parentFrame)) |
dewei_zhu@apple.com | 5e4916c | 2015-09-15 02:13:03 +0000 | [diff] [blame] | 221 | m_decoder->setEncoding(parentFrame->document()->textEncoding(), TextResourceDecoder::EncodingFromParentFrame); |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 222 | } else { |
| 223 | m_decoder->setEncoding(m_encoding, |
| 224 | m_encodingWasChosenByUser ? TextResourceDecoder::UserChosenEncoding : TextResourceDecoder::EncodingFromHTTPHeader); |
| 225 | } |
| 226 | m_frame->document()->setDecoder(m_decoder.get()); |
| 227 | } |
eric@webkit.org | 44a233c | 2010-06-25 23:14:00 +0000 | [diff] [blame] | 228 | return m_decoder.get(); |
| 229 | } |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 230 | |
hyatt@apple.com | ce8ee2a | 2010-08-27 20:29:34 +0000 | [diff] [blame] | 231 | void DocumentWriter::reportDataReceived() |
eric@webkit.org | 44a233c | 2010-06-25 23:14:00 +0000 | [diff] [blame] | 232 | { |
| 233 | ASSERT(m_decoder); |
abarth@webkit.org | 99edc53 | 2011-06-12 00:51:11 +0000 | [diff] [blame] | 234 | if (m_hasReceivedSomeData) |
| 235 | return; |
| 236 | m_hasReceivedSomeData = true; |
| 237 | if (m_decoder->encoding().usesVisualOrdering()) |
| 238 | m_frame->document()->setVisuallyOrdered(); |
antti@apple.com | 0eb8724 | 2017-03-02 06:19:04 +0000 | [diff] [blame] | 239 | m_frame->document()->resolveStyle(Document::ResolveStyleType::Rebuild); |
eric@webkit.org | 44a233c | 2010-06-25 23:14:00 +0000 | [diff] [blame] | 240 | } |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 241 | |
abarth@webkit.org | 031254e | 2011-06-12 04:19:39 +0000 | [diff] [blame] | 242 | void DocumentWriter::addData(const char* bytes, size_t length) |
eric@webkit.org | 44a233c | 2010-06-25 23:14:00 +0000 | [diff] [blame] | 243 | { |
commit-queue@webkit.org | fe59ee0 | 2012-03-06 22:45:12 +0000 | [diff] [blame] | 244 | // Check that we're inside begin()/end(). |
| 245 | // FIXME: Change these to ASSERT once https://bugs.webkit.org/show_bug.cgi?id=80427 has |
| 246 | // been resolved. |
| 247 | if (m_state == NotStartedWritingState) |
| 248 | CRASH(); |
| 249 | if (m_state == FinishedWritingState) |
| 250 | CRASH(); |
| 251 | |
| 252 | ASSERT(m_parser); |
weinig@apple.com | ee87b2f | 2013-10-05 23:59:58 +0000 | [diff] [blame] | 253 | m_parser->appendBytes(*this, bytes, length); |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 254 | } |
| 255 | |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 256 | void DocumentWriter::end() |
| 257 | { |
japhet@chromium.org | 440e4fd | 2012-04-13 01:07:27 +0000 | [diff] [blame] | 258 | ASSERT(m_frame->page()); |
| 259 | ASSERT(m_frame->document()); |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 260 | |
commit-queue@webkit.org | fe59ee0 | 2012-03-06 22:45:12 +0000 | [diff] [blame] | 261 | // The parser is guaranteed to be released after this point. begin() would |
| 262 | // have to be called again before we can start writing more data. |
| 263 | m_state = FinishedWritingState; |
| 264 | |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 265 | // http://bugs.webkit.org/show_bug.cgi?id=10854 |
| 266 | // The frame's last ref may be removed and it can be deleted by checkCompleted(), |
| 267 | // so we'll add a protective refcount |
akling@apple.com | f851598 | 2013-09-02 18:50:01 +0000 | [diff] [blame] | 268 | Ref<Frame> protect(*m_frame); |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 269 | |
abarth@webkit.org | 5b4ba32 | 2011-08-03 18:56:37 +0000 | [diff] [blame] | 270 | if (!m_parser) |
| 271 | return; |
abarth@webkit.org | 99edc53 | 2011-06-12 00:51:11 +0000 | [diff] [blame] | 272 | // FIXME: m_parser->finish() should imply m_parser->flush(). |
weinig@apple.com | ee87b2f | 2013-10-05 23:59:58 +0000 | [diff] [blame] | 273 | m_parser->flush(*this); |
abarth@webkit.org | 14a3bdf | 2011-06-11 00:56:13 +0000 | [diff] [blame] | 274 | if (!m_parser) |
| 275 | return; |
| 276 | m_parser->finish(); |
cdumez@apple.com | d839ea1 | 2015-07-04 19:42:18 +0000 | [diff] [blame] | 277 | m_parser = nullptr; |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 278 | } |
| 279 | |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 280 | void DocumentWriter::setEncoding(const String& name, bool userChosen) |
| 281 | { |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 282 | m_encoding = name; |
| 283 | m_encodingWasChosenByUser = userChosen; |
| 284 | } |
| 285 | |
ap@apple.com | 048c67f | 2010-10-04 19:48:49 +0000 | [diff] [blame] | 286 | void DocumentWriter::setDocumentWasLoadedAsPartOfNavigation() |
| 287 | { |
commit-queue@webkit.org | fe59ee0 | 2012-03-06 22:45:12 +0000 | [diff] [blame] | 288 | ASSERT(m_parser && !m_parser->isStopped()); |
abarth@webkit.org | 14a3bdf | 2011-06-11 00:56:13 +0000 | [diff] [blame] | 289 | m_parser->setDocumentWasLoadedAsPartOfNavigation(); |
ap@apple.com | 048c67f | 2010-10-04 19:48:49 +0000 | [diff] [blame] | 290 | } |
| 291 | |
abarth@webkit.org | 412c54e | 2010-04-20 22:12:16 +0000 | [diff] [blame] | 292 | } // namespace WebCore |