Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1360)

Unified Diff: Source/core/loader/DocumentWriter.cpp

Issue 17640007: Refactoring: Simplify DocumentWriter by reorganizing its lifetime. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Fixed a build breakage Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/core/loader/DocumentWriter.cpp
diff --git a/Source/core/loader/DocumentWriter.cpp b/Source/core/loader/DocumentWriter.cpp
index fd3334276a428bdd3bae7545724a2c2c2dd2200f..4f41f9cfd1d20874d7595364529b5fe238ee4b8e 100644
--- a/Source/core/loader/DocumentWriter.cpp
+++ b/Source/core/loader/DocumentWriter.cpp
@@ -30,14 +30,12 @@
#include "core/loader/DocumentWriter.h"
#include "bindings/v8/ScriptController.h"
-#include "core/dom/DOMImplementation.h"
#include "core/dom/RawDataDocumentParser.h"
#include "core/dom/ScriptableDocumentParser.h"
#include "core/html/PluginDocument.h"
#include "core/loader/FrameLoader.h"
#include "core/loader/FrameLoaderClient.h"
#include "core/loader/FrameLoaderStateMachine.h"
-#include "core/loader/SinkDocument.h"
#include "core/loader/TextResourceDecoder.h"
#include "core/page/DOMWindow.h"
#include "core/page/Frame.h"
@@ -46,118 +44,46 @@
#include "core/platform/text/SegmentedString.h"
#include "weborigin/KURL.h"
#include "weborigin/SecurityOrigin.h"
+#include "wtf/PassOwnPtr.h"
namespace WebCore {
-DocumentWriter::DocumentWriter(Frame* frame)
- : m_frame(frame)
- , m_hasReceivedSomeData(false)
- , m_state(NotStartedWritingState)
-{
-}
-
-// This is only called by ScriptController::executeScriptIfJavaScriptURL
-// and always contains the result of evaluating a javascript: url.
-// This is the <iframe src="javascript:'html'"> case.
-void DocumentWriter::replaceDocument(const String& source, Document* ownerDocument)
+PassOwnPtr<DocumentWriter> DocumentWriter::create(Document* document, const String& mimeType, const String& encoding, bool encodingUserChoosen)
{
- m_frame->loader()->stopAllLoaders();
- begin(m_frame->document()->url(), true, ownerDocument);
-
- if (!source.isNull()) {
- if (!m_hasReceivedSomeData) {
- m_hasReceivedSomeData = true;
- m_frame->document()->setCompatibilityMode(Document::NoQuirksMode);
- }
-
- // FIXME: This should call DocumentParser::appendBytes instead of append
- // to support RawDataDocumentParsers.
- if (DocumentParser* parser = m_frame->document()->parser()) {
- parser->pinToMainThread();
- // Because we're pinned to the main thread we don't need to worry about
- // passing ownership of the source string.
- parser->append(source.impl());
- }
- }
-
- end();
+ return adoptPtr(new DocumentWriter(document, mimeType, encoding, encodingUserChoosen));
}
-void DocumentWriter::clear()
-{
- m_decoder = 0;
- m_decoderBuilder.clear();
- m_hasReceivedSomeData = false;
-}
-
-void DocumentWriter::begin()
+DocumentWriter::DocumentWriter(Document* document, const String& mimeType, const String& encoding, bool encodingUserChoosen)
+ : m_document(document)
+ , m_hasReceivedSomeData(false)
+ , m_decoderBuilder(mimeType, encoding, encodingUserChoosen)
+ // We grab a reference to the parser so that we'll always send data to the
+ // original parser, even if the document acquires a new parser (e.g., via
+ // document.open).
+ , m_parser(m_document->implicitOpen())
{
- begin(KURL());
+ if (FrameView* view = m_document->frame()->view())
+ view->setContentsSize(IntSize());
}
-PassRefPtr<Document> DocumentWriter::createDocument(const KURL& url)
+DocumentWriter::~DocumentWriter()
{
- return DOMImplementation::createDocument(mimeType(), m_frame, url, m_frame->inViewSourceMode());
}
-void DocumentWriter::begin(const KURL& urlReference, bool dispatch, Document* ownerDocument)
+void DocumentWriter::appendReplacingData(const String& source)
{
- // We grab a local copy of the URL because it's easy for callers to supply
- // a URL that will be deallocated during the execution of this function.
- // For example, see <https://bugs.webkit.org/show_bug.cgi?id=66360>.
- KURL url = urlReference;
-
- // Create a new document before clearing the frame, because it may need to
- // inherit an aliased security context.
- RefPtr<Document> document = createDocument(url);
-
- // If the new document is for a Plugin but we're supposed to be sandboxed from Plugins,
- // then replace the document with one whose parser will ignore the incoming data (bug 39323)
- if (document->isPluginDocument() && document->isSandboxed(SandboxPlugins))
- document = SinkDocument::create(m_frame, url);
-
- // FIXME: Do we need to consult the content security policy here about blocked plug-ins?
-
- bool shouldReuseDefaultView = m_frame->loader()->stateMachine()->isDisplayingInitialEmptyDocument() && m_frame->document()->isSecureTransitionTo(url);
-
- RefPtr<DOMWindow> originalDOMWindow;
- if (shouldReuseDefaultView)
- originalDOMWindow = m_frame->domWindow();
- m_frame->loader()->clear(!shouldReuseDefaultView, !shouldReuseDefaultView);
- clear();
-
- if (!shouldReuseDefaultView)
- m_frame->setDOMWindow(DOMWindow::create(m_frame));
- else {
- // Note that the old Document is still attached to the DOMWindow; the
- // setDocument() call below will detach the old Document.
- ASSERT(originalDOMWindow);
- m_frame->setDOMWindow(originalDOMWindow);
- }
-
- m_frame->loader()->setOutgoingReferrer(url);
- m_frame->domWindow()->setDocument(document);
-
- if (m_decoder)
- document->setDecoder(m_decoder.get());
- if (ownerDocument) {
- document->setCookieURL(ownerDocument->cookieURL());
- document->setSecurityOrigin(ownerDocument->securityOrigin());
+ ASSERT(!m_hasReceivedSomeData);
+ m_hasReceivedSomeData = true;
+ m_document->setCompatibilityMode(Document::NoQuirksMode);
+
+ // FIXME: This should call DocumentParser::appendBytes instead of append
+ // to support RawDataDocumentParsers.
+ if (DocumentParser* parser = m_document->parser()) {
+ parser->pinToMainThread();
+ // Because we're pinned to the main thread we don't need to worry about
+ // passing ownership of the source string.
+ parser->append(source.impl());
}
-
- m_frame->loader()->didBeginDocument(dispatch);
-
- document->implicitOpen();
-
- // We grab a reference to the parser so that we'll always send data to the
- // original parser, even if the document acquires a new parser (e.g., via
- // document.open).
- m_parser = document->parser();
-
- if (m_frame->view())
- m_frame->view()->setContentsSize(IntSize());
-
- m_state = StartedWritingState;
}
void DocumentWriter::reportDataReceived()
@@ -167,22 +93,14 @@ void DocumentWriter::reportDataReceived()
return;
m_hasReceivedSomeData = true;
if (m_decoder->encoding().usesVisualOrdering())
- m_frame->document()->setVisuallyOrdered();
+ m_document->setVisuallyOrdered();
}
void DocumentWriter::addData(const char* bytes, size_t length)
{
- // Check that we're inside begin()/end().
- // FIXME: Change these to ASSERT once https://bugs.webkit.org/show_bug.cgi?id=80427 has
- // been resolved.
- if (m_state == NotStartedWritingState)
- CRASH();
- if (m_state == FinishedWritingState)
- CRASH();
-
ASSERT(m_parser);
if (!m_decoder && m_parser->needsDecoder() && 0 < length)
- m_decoder = m_decoderBuilder.buildFor(m_frame->document());
+ m_decoder = m_decoderBuilder.buildFor(m_document);
size_t consumedChars = m_parser->appendBytes(bytes, length);
if (consumedChars)
reportDataReceived();
@@ -190,29 +108,27 @@ void DocumentWriter::addData(const char* bytes, size_t length)
void DocumentWriter::end()
{
- ASSERT(m_frame->page());
- ASSERT(m_frame->document());
-
- // The parser is guaranteed to be released after this point. begin() would
- // have to be called again before we can start writing more data.
- m_state = FinishedWritingState;
+ ASSERT(m_document);
// http://bugs.webkit.org/show_bug.cgi?id=10854
// The frame's last ref may be removed and it can be deleted by checkCompleted(),
// so we'll add a protective refcount
- RefPtr<Frame> protector(m_frame);
+ RefPtr<Frame> protector(m_document->frame());
if (!m_parser)
return;
+
if (!m_decoder && m_parser->needsDecoder())
- m_decoder = m_decoderBuilder.buildFor(m_frame->document());
+ m_decoder = m_decoderBuilder.buildFor(m_document);
size_t consumedChars = m_parser->flush();
if (consumedChars)
reportDataReceived();
if (!m_parser)
return;
+
m_parser->finish();
m_parser = 0;
+ m_document = 0;
}
void DocumentWriter::setDocumentWasLoadedAsPartOfNavigation()

Powered by Google App Engine
This is Rietveld 408576698