Move Archive processing to DocumentLoader, instead of FrameLoader.
authorjaphet@chromium.org <japhet@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 7 Apr 2012 00:43:17 +0000 (00:43 +0000)
committerjaphet@chromium.org <japhet@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 7 Apr 2012 00:43:17 +0000 (00:43 +0000)
https://bugs.webkit.org/show_bug.cgi?id=83055

Reviewed by Adam Barth.

No new tests, no functionality change intended.

* loader/DocumentLoader.cpp:
(WebCore::DocumentLoader::finishedLoading):
(WebCore::DocumentLoader::setupForReplaceByMIMEType):
(WebCore::DocumentLoader::maybeCreateArchive): Renamed from
    FrameLoader::finishedLoadingDocument(). Returns true if an archive
    was created.
(WebCore::DocumentLoader::setArchive):
(WebCore::DocumentLoader::scheduleArchiveLoad):
(WebCore::DocumentLoader::documentURL): Add a check for whether an archive url
    should be returned, so that we don't need special handling in Document and
    FrameLoader for overriding the document url later.
* loader/DocumentLoader.h:
* loader/FrameLoader.cpp:
(WebCore::FrameLoader::receivedFirstData): Remove archive special cases, since
    DocumentLoader::documentURL() will return the right thing for legacy archives
    and maybeCreateArchive() will override the base url for mhtml.
(WebCore::FrameLoader::loadArchive):
* loader/FrameLoader.h:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@113526 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/loader/DocumentLoader.cpp
Source/WebCore/loader/DocumentLoader.h
Source/WebCore/loader/FrameLoader.cpp
Source/WebCore/loader/FrameLoader.h

index f101d29..8d9aca7 100644 (file)
@@ -1,3 +1,31 @@
+2012-04-06  Nate Chapin  <japhet@chromium.org>
+
+        Move Archive processing to DocumentLoader, instead of FrameLoader.
+        https://bugs.webkit.org/show_bug.cgi?id=83055
+
+        Reviewed by Adam Barth.
+
+        No new tests, no functionality change intended.
+
+        * loader/DocumentLoader.cpp:
+        (WebCore::DocumentLoader::finishedLoading):
+        (WebCore::DocumentLoader::setupForReplaceByMIMEType):
+        (WebCore::DocumentLoader::maybeCreateArchive): Renamed from
+            FrameLoader::finishedLoadingDocument(). Returns true if an archive
+            was created.
+        (WebCore::DocumentLoader::setArchive):
+        (WebCore::DocumentLoader::scheduleArchiveLoad):
+        (WebCore::DocumentLoader::documentURL): Add a check for whether an archive url
+            should be returned, so that we don't need special handling in Document and
+            FrameLoader for overriding the document url later.
+        * loader/DocumentLoader.h:
+        * loader/FrameLoader.cpp:
+        (WebCore::FrameLoader::receivedFirstData): Remove archive special cases, since
+            DocumentLoader::documentURL() will return the right thing for legacy archives
+            and maybeCreateArchive() will override the base url for mhtml.
+        (WebCore::FrameLoader::loadArchive):
+        * loader/FrameLoader.h:
+
 2012-04-06  Charles Wei  <charles.wei@torchmobile.com.cn>
 
         [BlackBerry] Build fix to match the latest WebCore change
index d76861c..4980083 100644 (file)
@@ -225,6 +225,10 @@ void DocumentLoader::stopLoading()
 
     // Appcache uses ResourceHandle directly, DocumentLoader doesn't count these loads.
     m_applicationCacheHost->stopLoadingInFrame(m_frame);
+    
+#if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
+    clearArchiveResources();
+#endif
 
     if (!loading) {
         // If something above restarted loading we might run into mysterious crashes like 
@@ -282,11 +286,12 @@ void DocumentLoader::finishedLoading()
 {
     m_gotFirstByte = true;   
     commitIfReady();
-    if (!frameLoader())
+    if (!frameLoader() || frameLoader()->stateMachine()->creatingInitialEmptyDocument())
         return;
-    frameLoader()->finishedLoadingDocument(this);
+    if (!maybeCreateArchive())
+        frameLoader()->client()->finishedLoading(this);
     m_writer.end();
-    if (!m_mainDocumentError.isNull() || frameLoader()->stateMachine()->creatingInitialEmptyDocument())
+    if (!m_mainDocumentError.isNull())
         return;
     clearMainResourceLoader();
     frameLoader()->checkLoadComplete();
@@ -350,7 +355,7 @@ void DocumentLoader::setupForReplaceByMIMEType(const String& newMIMEType)
         commitLoad(resourceData->data(), resourceData->size());
     }
     
-    frameLoader()->finishedLoadingDocument(this);
+    maybeCreateArchive();
     m_writer.end();
     
     frameLoader()->setReplacing();
@@ -439,7 +444,46 @@ bool DocumentLoader::isLoadingInAPISense() const
     return frameLoader()->subframeIsLoading();
 }
 
+bool DocumentLoader::maybeCreateArchive()
+{
+#if !ENABLE(WEB_ARCHIVE) && !ENABLE(MHTML)
+    return false;
+#else
+    
+    // Give the archive machinery a crack at this document. If the MIME type is not an archive type, it will return 0.
+    m_archive = ArchiveFactory::create(m_response.url(), mainResourceData().get(), m_response.mimeType());
+    if (!m_archive)
+        return false;
+    
+    addAllArchiveResources(m_archive.get());
+    ArchiveResource* mainResource = m_archive->mainResource();
+    m_parsedArchiveData = mainResource->data();
+    m_writer.setMIMEType(mainResource->mimeType());
+    
+    ASSERT(m_frame->document());
+    String userChosenEncoding = overrideEncoding();
+    bool encodingIsUserChosen = !userChosenEncoding.isNull();
+    m_writer.setEncoding(encodingIsUserChosen ? userChosenEncoding : mainResource->textEncoding(), encodingIsUserChosen);
+
+#if ENABLE(MHTML)
+    // The origin is the MHTML file, we need to set the base URL to the document encoded in the MHTML so
+    // relative URLs are resolved properly.
+    if (m_archive->type() == Archive::MHTML)
+        m_frame->document()->setBaseURLOverride(mainResource->url());
+#endif
+
+    m_writer.addData(mainResource->data()->data(), mainResource->data()->size());
+    return true;
+#endif // !ENABLE(WEB_ARCHIVE) && !ENABLE(MHTML)
+}
+
 #if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
+void DocumentLoader::setArchive(PassRefPtr<Archive> archive)
+{
+    m_archive = archive;
+    addAllArchiveResources(m_archive.get());
+}
+
 void DocumentLoader::addAllArchiveResources(Archive* archive)
 {
     if (!m_archiveResourceCollection)
@@ -477,11 +521,6 @@ void DocumentLoader::clearArchiveResources()
     m_substituteResourceDeliveryTimer.stop();
 }
 
-void DocumentLoader::setParsedArchiveData(PassRefPtr<SharedBuffer> data)
-{
-    m_parsedArchiveData = data;
-}
-
 SharedBuffer* DocumentLoader::parsedArchiveData() const
 {
     return m_parsedArchiveData.get();
@@ -627,11 +666,10 @@ bool DocumentLoader::scheduleArchiveLoad(ResourceLoader* loader, const ResourceR
         }
     }
 
-    Archive* archive = frameLoader()->archive();
-    if (!archive)
+    if (!m_archive)
         return false;
 
-    switch (archive->type()) {
+    switch (m_archive->type()) {
 #if ENABLE(WEB_ARCHIVE)
     case Archive::WebArchive:
         // WebArchiveDebugMode means we fail loads instead of trying to fetch them from the network if they're not in the archive.
@@ -704,6 +742,10 @@ const KURL& DocumentLoader::responseURL() const
 KURL DocumentLoader::documentURL() const
 {
     KURL url = substituteData().responseURL();
+#if ENABLE(WEB_ARCHIVE)
+    if (url.isEmpty() && m_archive && m_archive->type() == Archive::WebArchive)
+        url = m_archive->mainResource()->url();
+#endif
     if (url.isEmpty())
         url = requestURL();
     if (url.isEmpty())
index 3b1fb12..b0b50ea 100644 (file)
@@ -132,14 +132,12 @@ namespace WebCore {
 #endif
 
 #if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
+        void setArchive(PassRefPtr<Archive>);
         void addAllArchiveResources(Archive*);
         void addArchiveResource(PassRefPtr<ArchiveResource>);
-        
         PassRefPtr<Archive> popArchiveForSubframe(const String& frameName, const KURL&);
-        void clearArchiveResources();
-        void setParsedArchiveData(PassRefPtr<SharedBuffer>);
         SharedBuffer* parsedArchiveData() const;
-        
+
         bool scheduleArchiveLoad(ResourceLoader*, const ResourceRequest&, const KURL&);
 #endif // ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
 
@@ -257,6 +255,11 @@ namespace WebCore {
         bool doesProgressiveLoad(const String& MIMEType) const;
         void checkLoadComplete();
         void clearMainResourceLoader();
+        
+        bool maybeCreateArchive();
+#if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
+        void clearArchiveResources();
+#endif
 
         void deliverSubstituteResourcesAfterDelay();
         void substituteResourceDeliveryTimerFired(Timer<DocumentLoader>*);
@@ -326,6 +329,7 @@ namespace WebCore {
 
         OwnPtr<ArchiveResourceCollection> m_archiveResourceCollection;
 #if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
+        RefPtr<Archive> m_archive;
         RefPtr<SharedBuffer> m_parsedArchiveData;
 #endif
 
index 3cc9b78..2d64671 100644 (file)
 
 #if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
 #include "Archive.h"
-#include "ArchiveFactory.h"
 #endif
 
 namespace WebCore {
@@ -565,25 +564,9 @@ void FrameLoader::clear(bool clearWindowProperties, bool clearScriptObjects, boo
 void FrameLoader::receivedFirstData()
 {
     KURL workingURL = activeDocumentLoader()->documentURL();
-#if ENABLE(WEB_ARCHIVE)
-    // FIXME: The document loader, not the frame loader, should be in charge of loading web archives.
-    // Once this is done, we can just make DocumentLoader::documentURL() return the right URL
-    // based on whether it has a non-null archive or not.
-    if (m_archive && activeDocumentLoader()->parsedArchiveData())
-        workingURL = m_archive->mainResource()->url();
-#endif
-
     activeDocumentLoader()->writer()->begin(workingURL, false);
     activeDocumentLoader()->writer()->setDocumentWasLoadedAsPartOfNavigation();
 
-#if ENABLE(MHTML)
-    if (m_archive) {
-        // The origin is the MHTML file, we need to set the base URL to the document encoded in the MHTML so
-        // relative URLs are resolved properly.
-        m_frame->document()->setBaseURLOverride(m_archive->mainResource()->url());
-    }
-#endif
-
     dispatchDidCommitLoad();
     dispatchDidClearWindowObjectsInAllWorlds();
 
@@ -829,9 +812,7 @@ void FrameLoader::loadURLIntoChildFrame(const KURL& url, const String& referer,
 #if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
 void FrameLoader::loadArchive(PassRefPtr<Archive> archive)
 {
-    m_archive = archive;
-    
-    ArchiveResource* mainResource = m_archive->mainResource();
+    ArchiveResource* mainResource = archive->mainResource();
     ASSERT(mainResource);
     if (!mainResource)
         return;
@@ -844,7 +825,7 @@ void FrameLoader::loadArchive(PassRefPtr<Archive> archive)
 #endif
 
     RefPtr<DocumentLoader> documentLoader = m_client->createDocumentLoader(request, substituteData);
-    documentLoader->addAllArchiveResources(m_archive.get());
+    documentLoader->setArchive(archive.get());
     load(documentLoader.get());
 }
 #endif // ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
@@ -1545,11 +1526,6 @@ void FrameLoader::stopAllLoaders(ClearProvisionalItemPolicy clearProvisionalItem
         m_documentLoader->stopLoading();
 
     setProvisionalDocumentLoader(0);
-    
-#if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
-    if (m_documentLoader)
-        m_documentLoader->clearArchiveResources();
-#endif
 
     m_checkTimer.stop();
 
@@ -2001,41 +1977,6 @@ bool FrameLoader::isLoadingMainFrame() const
     return page && m_frame == page->mainFrame();
 }
 
-void FrameLoader::finishedLoadingDocument(DocumentLoader* loader)
-{
-    if (m_stateMachine.creatingInitialEmptyDocument())
-        return;
-
-#if !ENABLE(WEB_ARCHIVE) && !ENABLE(MHTML)
-    m_client->finishedLoading(loader);
-#else
-    // Give archive machinery a crack at this document. If the MIME type is not an archive type, it will return 0.
-    m_archive = ArchiveFactory::create(loader->response().url(), loader->mainResourceData().get(), loader->responseMIMEType());
-    if (!m_archive) {
-        m_client->finishedLoading(loader);
-        return;
-    }
-
-    // FIXME: The remainder of this function should be moved to DocumentLoader.
-
-    loader->addAllArchiveResources(m_archive.get());
-
-    ArchiveResource* mainResource = m_archive->mainResource();
-    loader->setParsedArchiveData(mainResource->data());
-
-    loader->writer()->setMIMEType(mainResource->mimeType());
-
-    closeURL();
-    didOpenURL();
-
-    ASSERT(m_frame->document());
-    String userChosenEncoding = documentLoader()->overrideEncoding();
-    bool encodingIsUserChosen = !userChosenEncoding.isNull();
-    loader->writer()->setEncoding(encodingIsUserChosen ? userChosenEncoding : mainResource->textEncoding(), encodingIsUserChosen);
-    loader->writer()->addData(mainResource->data()->data(), mainResource->data()->size());
-#endif // ENABLE(WEB_ARCHIVE)
-}
-
 bool FrameLoader::isReplacing() const
 {
     return m_loadType == FrameLoadTypeReplace;
index 6fbe41b..ed378ae 100644 (file)
@@ -160,7 +160,6 @@ public:
     bool isHostedByObjectElement() const;
     bool isLoadingMainFrame() const;
 
-    void finishedLoadingDocument(DocumentLoader*);
     bool isReplacing() const;
     void setReplacing();
     bool subframeIsLoading() const;
@@ -284,10 +283,6 @@ public:
 
     NetworkingContext* networkingContext() const;
 
-#if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
-    Archive* archive() const { return m_archive.get(); }
-#endif
-
 private:
     bool allChildrenAreComplete() const; // immediate children, not all descendants
 
@@ -432,10 +427,6 @@ private:
 
     RefPtr<FrameNetworkingContext> m_networkingContext;
 
-#if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
-    RefPtr<Archive> m_archive;
-#endif
-
     KURL m_previousUrl;
     RefPtr<HistoryItem> m_requestedHistoryItem;
 };