diff options
Diffstat (limited to 'Source/WebCore/loader/archive')
-rw-r--r-- | Source/WebCore/loader/archive/Archive.cpp | 20 | ||||
-rw-r--r-- | Source/WebCore/loader/archive/Archive.h | 44 | ||||
-rw-r--r-- | Source/WebCore/loader/archive/ArchiveFactory.cpp | 54 | ||||
-rw-r--r-- | Source/WebCore/loader/archive/ArchiveFactory.h | 12 | ||||
-rw-r--r-- | Source/WebCore/loader/archive/ArchiveResource.cpp | 20 | ||||
-rw-r--r-- | Source/WebCore/loader/archive/ArchiveResource.h | 15 | ||||
-rw-r--r-- | Source/WebCore/loader/archive/ArchiveResourceCollection.cpp | 63 | ||||
-rw-r--r-- | Source/WebCore/loader/archive/ArchiveResourceCollection.h | 38 | ||||
-rw-r--r-- | Source/WebCore/loader/archive/mhtml/MHTMLArchive.cpp | 94 | ||||
-rw-r--r-- | Source/WebCore/loader/archive/mhtml/MHTMLArchive.h | 28 | ||||
-rw-r--r-- | Source/WebCore/loader/archive/mhtml/MHTMLParser.cpp | 78 | ||||
-rw-r--r-- | Source/WebCore/loader/archive/mhtml/MHTMLParser.h | 16 |
12 files changed, 212 insertions, 270 deletions
diff --git a/Source/WebCore/loader/archive/Archive.cpp b/Source/WebCore/loader/archive/Archive.cpp index 86d7558e1..5e05be22c 100644 --- a/Source/WebCore/loader/archive/Archive.cpp +++ b/Source/WebCore/loader/archive/Archive.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008 Apple Inc. All rights reserved. + * Copyright (C) 2008-2017 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,7 +10,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * 3. Neither the name of Apple Inc. ("Apple") nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * @@ -37,17 +37,17 @@ Archive::~Archive() void Archive::clearAllSubframeArchives() { - Vector<RefPtr<Archive>> clearedArchives; - clearAllSubframeArchivesImpl(&clearedArchives); + HashSet<Archive*> clearedArchives; + clearedArchives.add(this); + clearAllSubframeArchives(clearedArchives); } -void Archive::clearAllSubframeArchivesImpl(Vector<RefPtr<Archive>>* clearedArchives) +void Archive::clearAllSubframeArchives(HashSet<Archive*>& clearedArchives) { - for (Vector<RefPtr<Archive>>::iterator it = m_subframeArchives.begin(); it != m_subframeArchives.end(); ++it) { - if (!clearedArchives->contains(*it)) { - clearedArchives->append(*it); - (*it)->clearAllSubframeArchivesImpl(clearedArchives); - } + ASSERT(clearedArchives.contains(this)); + for (auto& archive : m_subframeArchives) { + if (clearedArchives.add(archive.ptr())) + archive->clearAllSubframeArchives(clearedArchives); } m_subframeArchives.clear(); } diff --git a/Source/WebCore/loader/archive/Archive.h b/Source/WebCore/loader/archive/Archive.h index 15f6b7563..ff74ce283 100644 --- a/Source/WebCore/loader/archive/Archive.h +++ b/Source/WebCore/loader/archive/Archive.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008 Apple Inc. All rights reserved. + * Copyright (C) 2008-2017 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,7 +10,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * 3. Neither the name of Apple Inc. ("Apple") nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * @@ -26,47 +26,41 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef Archive_h -#define Archive_h +#pragma once #include "ArchiveResource.h" - -#include <wtf/PassRefPtr.h> -#include <wtf/RefCounted.h> -#include <wtf/RefPtr.h> -#include <wtf/Vector.h> +#include <wtf/HashSet.h> namespace WebCore { class Archive : public RefCounted<Archive> { public: - enum Type { - WebArchive, - MHTML - }; virtual ~Archive(); - virtual Type type() const = 0; + + virtual bool shouldLoadFromArchiveOnly() const = 0; + virtual bool shouldOverrideBaseURL() const = 0; + virtual bool shouldUseMainResourceEncoding() const = 0; + virtual bool shouldUseMainResourceURL() const = 0; + ArchiveResource* mainResource() { return m_mainResource.get(); } - const Vector<RefPtr<ArchiveResource>>& subresources() const { return m_subresources; } - const Vector<RefPtr<Archive>>& subframeArchives() const { return m_subframeArchives; } + const Vector<Ref<ArchiveResource>>& subresources() const { return m_subresources; } + const Vector<Ref<Archive>>& subframeArchives() const { return m_subframeArchives; } protected: // These methods are meant for subclasses for different archive types to add resources in to the archive, // and should not be exposed as archives should be immutable to clients - void setMainResource(PassRefPtr<ArchiveResource> mainResource) { m_mainResource = mainResource; } - void addSubresource(PassRefPtr<ArchiveResource> subResource) { m_subresources.append(subResource); } - void addSubframeArchive(PassRefPtr<Archive> subframeArchive) { m_subframeArchives.append(subframeArchive); } + void setMainResource(Ref<ArchiveResource>&& mainResource) { m_mainResource = WTFMove(mainResource); } + void addSubresource(Ref<ArchiveResource>&& resource) { m_subresources.append(WTFMove(resource)); } + void addSubframeArchive(Ref<Archive>&& subframeArchive) { m_subframeArchives.append(WTFMove(subframeArchive)); } void clearAllSubframeArchives(); private: - void clearAllSubframeArchivesImpl(Vector<RefPtr<Archive>>* clearedArchives); + void clearAllSubframeArchives(HashSet<Archive*>&); RefPtr<ArchiveResource> m_mainResource; - Vector<RefPtr<ArchiveResource>> m_subresources; - Vector<RefPtr<Archive>> m_subframeArchives; + Vector<Ref<ArchiveResource>> m_subresources; + Vector<Ref<Archive>> m_subframeArchives; }; -} - -#endif // Archive +} // namespace WebCore diff --git a/Source/WebCore/loader/archive/ArchiveFactory.cpp b/Source/WebCore/loader/archive/ArchiveFactory.cpp index adbd5da44..417ac47a7 100644 --- a/Source/WebCore/loader/archive/ArchiveFactory.cpp +++ b/Source/WebCore/loader/archive/ArchiveFactory.cpp @@ -10,7 +10,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * 3. Neither the name of Apple Inc. ("Apple") nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * @@ -40,41 +40,42 @@ #include <wtf/HashMap.h> #include <wtf/HashSet.h> +#include <wtf/NeverDestroyed.h> #include <wtf/StdLibExtras.h> #include <wtf/text/WTFString.h> namespace WebCore { -typedef PassRefPtr<Archive> RawDataCreationFunction(const URL&, SharedBuffer*); -typedef HashMap<String, RawDataCreationFunction*, CaseFoldingHash> ArchiveMIMETypesMap; +typedef RefPtr<Archive> RawDataCreationFunction(const URL&, SharedBuffer&); +typedef HashMap<String, RawDataCreationFunction*, ASCIICaseInsensitiveHash> ArchiveMIMETypesMap; -// The create functions in the archive classes return PassRefPtr to concrete subclasses +// The create functions in the archive classes return RefPtr to concrete subclasses // of Archive. This adaptor makes the functions have a uniform return type. -template <typename ArchiveClass> static PassRefPtr<Archive> archiveFactoryCreate(const URL& url, SharedBuffer* buffer) +template<typename ArchiveClass> static RefPtr<Archive> archiveFactoryCreate(const URL& url, SharedBuffer& buffer) { return ArchiveClass::create(url, buffer); } -static ArchiveMIMETypesMap& archiveMIMETypes() +static ArchiveMIMETypesMap createArchiveMIMETypesMap() { - DEFINE_STATIC_LOCAL(ArchiveMIMETypesMap, mimeTypes, ()); - static bool initialized = false; - - if (initialized) - return mimeTypes; + ArchiveMIMETypesMap map; - // FIXME: Remove unnecessary 'static_cast<RawDataCreationFunction*>' from the following 'mimeTypes.set' operations - // once we switch to a non-broken Visual Studio compiler. https://bugs.webkit.org/show_bug.cgi?id=121235 #if ENABLE(WEB_ARCHIVE) && USE(CF) - mimeTypes.set("application/x-webarchive", static_cast<RawDataCreationFunction*>(&archiveFactoryCreate<LegacyWebArchive>)); + map.add(ASCIILiteral { "application/x-webarchive" }, archiveFactoryCreate<LegacyWebArchive>); #endif + #if ENABLE(MHTML) - mimeTypes.set("multipart/related", static_cast<RawDataCreationFunction*>(&archiveFactoryCreate<MHTMLArchive>)); - mimeTypes.set("application/x-mimearchive", static_cast<RawDataCreationFunction*>(&archiveFactoryCreate<MHTMLArchive>)); + map.add(ASCIILiteral { "multipart/related" }, archiveFactoryCreate<MHTMLArchive>); + map.add(ASCIILiteral { "application/x-mimearchive" }, archiveFactoryCreate<MHTMLArchive>); #endif - initialized = true; - return mimeTypes; + return map; +} + +static ArchiveMIMETypesMap& archiveMIMETypes() +{ + static NeverDestroyed<ArchiveMIMETypesMap> map = createArchiveMIMETypesMap(); + return map; } bool ArchiveFactory::isArchiveMimeType(const String& mimeType) @@ -82,17 +83,22 @@ bool ArchiveFactory::isArchiveMimeType(const String& mimeType) return !mimeType.isEmpty() && archiveMIMETypes().contains(mimeType); } -PassRefPtr<Archive> ArchiveFactory::create(const URL& url, SharedBuffer* data, const String& mimeType) +RefPtr<Archive> ArchiveFactory::create(const URL& url, SharedBuffer* data, const String& mimeType) { - RawDataCreationFunction* function = mimeType.isEmpty() ? 0 : archiveMIMETypes().get(mimeType); - return function ? function(url, data) : PassRefPtr<Archive>(0); + if (!data) + return nullptr; + if (mimeType.isEmpty()) + return nullptr; + auto* function = archiveMIMETypes().get(mimeType); + if (!function) + return nullptr; + return function(url, *data); } void ArchiveFactory::registerKnownArchiveMIMETypes() { - HashSet<String>& mimeTypes = MIMETypeRegistry::getSupportedNonImageMIMETypes(); - - for (const auto& mimeType : archiveMIMETypes().keys()) + auto& mimeTypes = MIMETypeRegistry::getSupportedNonImageMIMETypes(); + for (auto& mimeType : archiveMIMETypes().keys()) mimeTypes.add(mimeType); } diff --git a/Source/WebCore/loader/archive/ArchiveFactory.h b/Source/WebCore/loader/archive/ArchiveFactory.h index 54b64dd77..f37b3c51d 100644 --- a/Source/WebCore/loader/archive/ArchiveFactory.h +++ b/Source/WebCore/loader/archive/ArchiveFactory.h @@ -10,7 +10,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * 3. Neither the name of Apple Inc. ("Apple") nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * @@ -26,13 +26,11 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef ArchiveFactory_h -#define ArchiveFactory_h +#pragma once #include "Archive.h" #include <wtf/Forward.h> -#include <wtf/PassRefPtr.h> namespace WebCore { @@ -41,10 +39,8 @@ class SharedBuffer; class ArchiveFactory { public: static bool isArchiveMimeType(const String&); - static PassRefPtr<Archive> create(const URL&, SharedBuffer* data, const String& mimeType); + static RefPtr<Archive> create(const URL&, SharedBuffer* data, const String& mimeType); static void registerKnownArchiveMIMETypes(); }; -} - -#endif // ArchiveFactory_h +} // namespace WebCore diff --git a/Source/WebCore/loader/archive/ArchiveResource.cpp b/Source/WebCore/loader/archive/ArchiveResource.cpp index 21000c828..bf1e8461b 100644 --- a/Source/WebCore/loader/archive/ArchiveResource.cpp +++ b/Source/WebCore/loader/archive/ArchiveResource.cpp @@ -10,7 +10,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * 3. Neither the name of Apple Inc. ("Apple") nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * @@ -33,8 +33,8 @@ namespace WebCore { -inline ArchiveResource::ArchiveResource(PassRefPtr<SharedBuffer> data, const URL& url, const String& mimeType, const String& textEncoding, const String& frameName, const ResourceResponse& response) - : SubstituteResource(url, response, data) +inline ArchiveResource::ArchiveResource(Ref<SharedBuffer>&& data, const URL& url, const String& mimeType, const String& textEncoding, const String& frameName, const ResourceResponse& response) + : SubstituteResource(url, response, WTFMove(data)) , m_mimeType(mimeType) , m_textEncoding(textEncoding) , m_frameName(frameName) @@ -42,21 +42,21 @@ inline ArchiveResource::ArchiveResource(PassRefPtr<SharedBuffer> data, const URL { } -PassRefPtr<ArchiveResource> ArchiveResource::create(PassRefPtr<SharedBuffer> data, const URL& url, const String& mimeType, const String& textEncoding, const String& frameName, const ResourceResponse& response) +RefPtr<ArchiveResource> ArchiveResource::create(RefPtr<SharedBuffer>&& data, const URL& url, const String& mimeType, const String& textEncoding, const String& frameName, const ResourceResponse& response) { if (!data) - return 0; + return nullptr; if (response.isNull()) { unsigned dataSize = data->size(); - return adoptRef(new ArchiveResource(data, url, mimeType, textEncoding, frameName, - ResourceResponse(url, mimeType, dataSize, textEncoding, String()))); + return adoptRef(*new ArchiveResource(data.releaseNonNull(), url, mimeType, textEncoding, frameName, + ResourceResponse(url, mimeType, dataSize, textEncoding))); } - return adoptRef(new ArchiveResource(data, url, mimeType, textEncoding, frameName, response)); + return adoptRef(*new ArchiveResource(data.releaseNonNull(), url, mimeType, textEncoding, frameName, response)); } -PassRefPtr<ArchiveResource> ArchiveResource::create(PassRefPtr<SharedBuffer> data, const URL& url, const ResourceResponse& response) +RefPtr<ArchiveResource> ArchiveResource::create(RefPtr<SharedBuffer>&& data, const URL& url, const ResourceResponse& response) { - return create(data, url, response.mimeType(), response.textEncodingName(), String(), response); + return create(WTFMove(data), url, response.mimeType(), response.textEncodingName(), String(), response); } } diff --git a/Source/WebCore/loader/archive/ArchiveResource.h b/Source/WebCore/loader/archive/ArchiveResource.h index 9b4b01b9c..209e9717a 100644 --- a/Source/WebCore/loader/archive/ArchiveResource.h +++ b/Source/WebCore/loader/archive/ArchiveResource.h @@ -10,7 +10,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * 3. Neither the name of Apple Inc. ("Apple") nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * @@ -26,8 +26,7 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef ArchiveResource_h -#define ArchiveResource_h +#pragma once #include "SubstituteResource.h" @@ -35,8 +34,8 @@ namespace WebCore { class ArchiveResource : public SubstituteResource { public: - static PassRefPtr<ArchiveResource> create(PassRefPtr<SharedBuffer>, const URL&, const ResourceResponse&); - static PassRefPtr<ArchiveResource> create(PassRefPtr<SharedBuffer>, const URL&, + static RefPtr<ArchiveResource> create(RefPtr<SharedBuffer>&&, const URL&, const ResourceResponse&); + WEBCORE_EXPORT static RefPtr<ArchiveResource> create(RefPtr<SharedBuffer>&&, const URL&, const String& mimeType, const String& textEncoding, const String& frameName, const ResourceResponse& = ResourceResponse()); @@ -48,7 +47,7 @@ public: bool shouldIgnoreWhenUnarchiving() const { return m_shouldIgnoreWhenUnarchiving; } private: - ArchiveResource(PassRefPtr<SharedBuffer>, const URL&, const String& mimeType, const String& textEncoding, const String& frameName, const ResourceResponse&); + ArchiveResource(Ref<SharedBuffer>&&, const URL&, const String& mimeType, const String& textEncoding, const String& frameName, const ResourceResponse&); String m_mimeType; String m_textEncoding; @@ -57,6 +56,4 @@ private: bool m_shouldIgnoreWhenUnarchiving; }; -} - -#endif // ArchiveResource_h +} // namespace WebCore diff --git a/Source/WebCore/loader/archive/ArchiveResourceCollection.cpp b/Source/WebCore/loader/archive/ArchiveResourceCollection.cpp index ac9e377b5..a3705ec77 100644 --- a/Source/WebCore/loader/archive/ArchiveResourceCollection.cpp +++ b/Source/WebCore/loader/archive/ArchiveResourceCollection.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008 Apple Inc. All rights reserved. + * Copyright (C) 2008-2017 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,7 +10,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * 3. Neither the name of Apple Inc. ("Apple") nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * @@ -29,64 +29,43 @@ #include "config.h" #include "ArchiveResourceCollection.h" -namespace WebCore { +#include "Archive.h" -ArchiveResourceCollection::ArchiveResourceCollection() -{ -} +namespace WebCore { -void ArchiveResourceCollection::addAllResources(Archive* archive) +void ArchiveResourceCollection::addAllResources(Archive& archive) { - ASSERT(archive); - if (!archive) - return; + for (auto& subresource : archive.subresources()) + m_subresources.set(subresource->url(), subresource.ptr()); - const Vector<RefPtr<ArchiveResource>>& subresources = archive->subresources(); - for (Vector<RefPtr<ArchiveResource>>::const_iterator iterator = subresources.begin(); iterator != subresources.end(); ++iterator) - m_subresources.set((*iterator)->url(), iterator->get()); - - const Vector<RefPtr<Archive>>& subframes = archive->subframeArchives(); - for (Vector<RefPtr<Archive>>::const_iterator iterator = subframes.begin(); iterator != subframes.end(); ++iterator) { - RefPtr<Archive> archive = *iterator; - ASSERT(archive->mainResource()); - - const String& frameName = archive->mainResource()->frameName(); - if (!frameName.isNull()) - m_subframes.set(frameName, archive.get()); - else { - // In the MHTML case, frames don't have a name so we use the URL instead. - m_subframes.set(archive->mainResource()->url().string(), archive.get()); + for (auto& subframeArchive : archive.subframeArchives()) { + ASSERT(subframeArchive->mainResource()); + auto frameName = subframeArchive->mainResource()->frameName(); + if (frameName.isNull()) { + // In the MHTML case, frames don't have a name, so we use the URL instead. + frameName = subframeArchive->mainResource()->url().string(); } + m_subframes.set(frameName, subframeArchive.ptr()); } } // FIXME: Adding a resource directly to a DocumentLoader/ArchiveResourceCollection seems like bad design, but is API some apps rely on. // Can we change the design in a manner that will let us deprecate that API without reducing functionality of those apps? -void ArchiveResourceCollection::addResource(PassRefPtr<ArchiveResource> resource) +void ArchiveResourceCollection::addResource(Ref<ArchiveResource>&& resource) { - ASSERT(resource); - if (!resource) - return; - - const URL& url = resource->url(); // get before passing PassRefPtr (which sets it to 0) - m_subresources.set(url, resource); + auto& url = resource->url(); + m_subresources.set(url, WTFMove(resource)); } ArchiveResource* ArchiveResourceCollection::archiveResourceForURL(const URL& url) { - ArchiveResource* resource = m_subresources.get(url); - if (!resource) - return 0; - - return resource; + return m_subresources.get(url); } -PassRefPtr<Archive> ArchiveResourceCollection::popSubframeArchive(const String& frameName, const URL& url) +RefPtr<Archive> ArchiveResourceCollection::popSubframeArchive(const String& frameName, const URL& url) { - RefPtr<Archive> archive = m_subframes.take(frameName); - if (archive) - return archive.release(); - + if (auto archive = m_subframes.take(frameName)) + return archive; return m_subframes.take(url.string()); } diff --git a/Source/WebCore/loader/archive/ArchiveResourceCollection.h b/Source/WebCore/loader/archive/ArchiveResourceCollection.h index 1927a8952..37df68b36 100644 --- a/Source/WebCore/loader/archive/ArchiveResourceCollection.h +++ b/Source/WebCore/loader/archive/ArchiveResourceCollection.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008 Apple Inc. All rights reserved. + * Copyright (C) 2008-2017 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,7 +10,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * 3. Neither the name of Apple Inc. ("Apple") nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * @@ -26,35 +26,35 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef ArchiveResourceCollection_h -#define ArchiveResourceCollection_h - -#include "Archive.h" -#include "ArchiveResource.h" -#include "URL.h" -#include <wtf/text/WTFString.h> +#pragma once +#include <wtf/Forward.h> #include <wtf/HashMap.h> -#include <wtf/RefCounted.h> +#include <wtf/Noncopyable.h> +#include <wtf/text/StringHash.h> namespace WebCore { +class Archive; +class ArchiveResource; +class URL; + class ArchiveResourceCollection { - WTF_MAKE_NONCOPYABLE(ArchiveResourceCollection); WTF_MAKE_FAST_ALLOCATED; + WTF_MAKE_NONCOPYABLE(ArchiveResourceCollection); + WTF_MAKE_FAST_ALLOCATED; + public: - ArchiveResourceCollection(); + ArchiveResourceCollection() = default; - void addResource(PassRefPtr<ArchiveResource>); - void addAllResources(Archive*); + void addResource(Ref<ArchiveResource>&&); + void addAllResources(Archive&); - ArchiveResource* archiveResourceForURL(const URL&); - PassRefPtr<Archive> popSubframeArchive(const String& frameName, const URL&); + WEBCORE_EXPORT ArchiveResource* archiveResourceForURL(const URL&); + RefPtr<Archive> popSubframeArchive(const String& frameName, const URL&); private: HashMap<String, RefPtr<ArchiveResource>> m_subresources; HashMap<String, RefPtr<Archive>> m_subframes; }; -} - -#endif +} // namespace WebCore diff --git a/Source/WebCore/loader/archive/mhtml/MHTMLArchive.cpp b/Source/WebCore/loader/archive/mhtml/MHTMLArchive.cpp index 4708bf11e..2ca67e27b 100644 --- a/Source/WebCore/loader/archive/mhtml/MHTMLArchive.cpp +++ b/Source/WebCore/loader/archive/mhtml/MHTMLArchive.cpp @@ -59,7 +59,6 @@ namespace WebCore { const char* const quotedPrintable = "quoted-printable"; const char* const base64 = "base64"; -const char* const binary = "binary"; static String generateRandomBoundary() { @@ -102,50 +101,40 @@ MHTMLArchive::~MHTMLArchive() clearAllSubframeArchives(); } -PassRefPtr<MHTMLArchive> MHTMLArchive::create() +Ref<MHTMLArchive> MHTMLArchive::create() { - return adoptRef(new MHTMLArchive); + return adoptRef(*new MHTMLArchive); } -PassRefPtr<MHTMLArchive> MHTMLArchive::create(const URL& url, SharedBuffer* data) +RefPtr<MHTMLArchive> MHTMLArchive::create(const URL& url, SharedBuffer& data) { // For security reasons we only load MHTML pages from local URLs. - if (!SchemeRegistry::shouldTreatURLSchemeAsLocal(url.protocol())) - return 0; + if (!SchemeRegistry::shouldTreatURLSchemeAsLocal(url.protocol().toString())) + return nullptr; - MHTMLParser parser(data); + MHTMLParser parser(&data); RefPtr<MHTMLArchive> mainArchive = parser.parseArchive(); if (!mainArchive) - return 0; // Invalid MHTML file. + return nullptr; // Invalid MHTML file. // Since MHTML is a flat format, we need to make all frames aware of all resources. for (size_t i = 0; i < parser.frameCount(); ++i) { RefPtr<MHTMLArchive> archive = parser.frameAt(i); for (size_t j = 1; j < parser.frameCount(); ++j) { if (i != j) - archive->addSubframeArchive(parser.frameAt(j)); + archive->addSubframeArchive(*parser.frameAt(j)); } for (size_t j = 0; j < parser.subResourceCount(); ++j) - archive->addSubresource(parser.subResourceAt(j)); + archive->addSubresource(*parser.subResourceAt(j)); } - return mainArchive.release(); + return mainArchive; } -PassRefPtr<SharedBuffer> MHTMLArchive::generateMHTMLData(Page* page) -{ - return generateMHTMLData(page, false); -} - -PassRefPtr<SharedBuffer> MHTMLArchive::generateMHTMLDataUsingBinaryEncoding(Page* page) -{ - return generateMHTMLData(page, true); -} - -PassRefPtr<SharedBuffer> MHTMLArchive::generateMHTMLData(Page* page, bool useBinaryEncoding) +RefPtr<SharedBuffer> MHTMLArchive::generateMHTMLData(Page* page) { Vector<PageSerializer::Resource> resources; - PageSerializer pageSerializer(&resources); - pageSerializer.serialize(page); + PageSerializer pageSerializer(resources); + pageSerializer.serialize(*page); String boundary = generateRandomBoundary(); String endOfResourceBoundary = makeString("--", boundary, "\r\n"); @@ -176,18 +165,14 @@ PassRefPtr<SharedBuffer> MHTMLArchive::generateMHTMLData(Page* page, bool useBin RefPtr<SharedBuffer> mhtmlData = SharedBuffer::create(); mhtmlData->append(asciiString.data(), asciiString.length()); - for (size_t i = 0; i < resources.size(); ++i) { - const PageSerializer::Resource& resource = resources[i]; - + for (auto& resource : resources) { stringBuilder.clear(); stringBuilder.append(endOfResourceBoundary); stringBuilder.append("Content-Type: "); stringBuilder.append(resource.mimeType); - const char* contentEncoding = 0; - if (useBinaryEncoding) - contentEncoding = binary; - else if (MIMETypeRegistry::isSupportedJavaScriptMIMEType(resource.mimeType) || MIMETypeRegistry::isSupportedNonImageMIMEType(resource.mimeType)) + const char* contentEncoding = nullptr; + if (MIMETypeRegistry::isSupportedJavaScriptMIMEType(resource.mimeType) || MIMETypeRegistry::isSupportedNonImageMIMEType(resource.mimeType)) contentEncoding = quotedPrintable; else contentEncoding = base64; @@ -201,36 +186,27 @@ PassRefPtr<SharedBuffer> MHTMLArchive::generateMHTMLData(Page* page, bool useBin asciiString = stringBuilder.toString().utf8(); mhtmlData->append(asciiString.data(), asciiString.length()); - if (!strcmp(contentEncoding, binary)) { - const char* data; - size_t position = 0; - while (size_t length = resource.data->getSomeData(data, position)) { - mhtmlData->append(data, length); - position += length; - } + // FIXME: ideally we would encode the content as a stream without having to fetch it all. + const char* data = resource.data->data(); + size_t dataLength = resource.data->size(); + Vector<char> encodedData; + if (!strcmp(contentEncoding, quotedPrintable)) { + quotedPrintableEncode(data, dataLength, encodedData); + mhtmlData->append(encodedData.data(), encodedData.size()); + mhtmlData->append("\r\n", 2); } else { - // FIXME: ideally we would encode the content as a stream without having to fetch it all. - const char* data = resource.data->data(); - size_t dataLength = resource.data->size(); - Vector<char> encodedData; - if (!strcmp(contentEncoding, quotedPrintable)) { - quotedPrintableEncode(data, dataLength, encodedData); - mhtmlData->append(encodedData.data(), encodedData.size()); + ASSERT(!strcmp(contentEncoding, base64)); + // We are not specifying insertLFs = true below as it would cut the lines with LFs and MHTML requires CRLFs. + base64Encode(data, dataLength, encodedData); + const size_t maximumLineLength = 76; + size_t index = 0; + size_t encodedDataLength = encodedData.size(); + do { + size_t lineLength = std::min(encodedDataLength - index, maximumLineLength); + mhtmlData->append(encodedData.data() + index, lineLength); mhtmlData->append("\r\n", 2); - } else { - ASSERT(!strcmp(contentEncoding, base64)); - // We are not specifying insertLFs = true below as it would cut the lines with LFs and MHTML requires CRLFs. - base64Encode(data, dataLength, encodedData); - const size_t maximumLineLength = 76; - size_t index = 0; - size_t encodedDataLength = encodedData.size(); - do { - size_t lineLength = std::min(encodedDataLength - index, maximumLineLength); - mhtmlData->append(encodedData.data() + index, lineLength); - mhtmlData->append("\r\n", 2); - index += maximumLineLength; - } while (index < encodedDataLength); - } + index += maximumLineLength; + } while (index < encodedDataLength); } } diff --git a/Source/WebCore/loader/archive/mhtml/MHTMLArchive.h b/Source/WebCore/loader/archive/mhtml/MHTMLArchive.h index 14cd87572..8524659aa 100644 --- a/Source/WebCore/loader/archive/mhtml/MHTMLArchive.h +++ b/Source/WebCore/loader/archive/mhtml/MHTMLArchive.h @@ -28,8 +28,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef MHTMLArchive_h -#define MHTMLArchive_h +#pragma once #if ENABLE(MHTML) @@ -41,27 +40,26 @@ class MHTMLParser; class Page; class SharedBuffer; -class MHTMLArchive : public Archive { +class MHTMLArchive final : public Archive { public: - virtual Type type() const { return MHTML; } + static Ref<MHTMLArchive> create(); + static RefPtr<MHTMLArchive> create(const URL&, SharedBuffer&); - static PassRefPtr<MHTMLArchive> create(); - static PassRefPtr<MHTMLArchive> create(const URL&, SharedBuffer*); - - static PassRefPtr<SharedBuffer> generateMHTMLData(Page*); - // Binary encoding results in smaller MHTML files but they might not work in other browsers. - static PassRefPtr<SharedBuffer> generateMHTMLDataUsingBinaryEncoding(Page*); + static RefPtr<SharedBuffer> generateMHTMLData(Page*); virtual ~MHTMLArchive(); private: - static PassRefPtr<SharedBuffer> generateMHTMLData(Page*, bool useBinaryEncoding); - friend class MHTMLParser; + MHTMLArchive(); + + bool shouldLoadFromArchiveOnly() const final { return true; } + bool shouldOverrideBaseURL() const final { return true; } + bool shouldUseMainResourceEncoding() const final { return false; } + bool shouldUseMainResourceURL() const final { return false; } }; -} +} // namespace WebCore -#endif -#endif +#endif // ENABLE(MHTML) diff --git a/Source/WebCore/loader/archive/mhtml/MHTMLParser.cpp b/Source/WebCore/loader/archive/mhtml/MHTMLParser.cpp index d8760f9ed..b0414ebc9 100644 --- a/Source/WebCore/loader/archive/mhtml/MHTMLParser.cpp +++ b/Source/WebCore/loader/archive/mhtml/MHTMLParser.cpp @@ -37,7 +37,6 @@ #include "MIMEHeader.h" #include "MIMETypeRegistry.h" #include "QuotedPrintable.h" -#include <wtf/HashMap.h> #include <wtf/text/Base64.h> namespace WebCore { @@ -57,17 +56,16 @@ MHTMLParser::MHTMLParser(SharedBuffer* data) { } -PassRefPtr<MHTMLArchive> MHTMLParser::parseArchive() +RefPtr<MHTMLArchive> MHTMLParser::parseArchive() { - RefPtr<MIMEHeader> header = MIMEHeader::parseHeader(&m_lineReader); - return parseArchiveWithHeader(header.get()); + return parseArchiveWithHeader(MIMEHeader::parseHeader(m_lineReader).get()); } -PassRefPtr<MHTMLArchive> MHTMLParser::parseArchiveWithHeader(MIMEHeader* header) +RefPtr<MHTMLArchive> MHTMLParser::parseArchiveWithHeader(MIMEHeader* header) { if (!header) { LOG_ERROR("Failed to parse MHTML part: no header."); - return 0; + return nullptr; } RefPtr<MHTMLArchive> archive = MHTMLArchive::create(); @@ -76,8 +74,8 @@ PassRefPtr<MHTMLArchive> MHTMLParser::parseArchiveWithHeader(MIMEHeader* header) bool endOfArchiveReached = false; RefPtr<ArchiveResource> resource = parseNextPart(*header, String(), String(), endOfArchiveReached); if (!resource) - return 0; - archive->setMainResource(resource); + return nullptr; + archive->setMainResource(resource.releaseNonNull()); return archive; } @@ -86,31 +84,31 @@ PassRefPtr<MHTMLArchive> MHTMLParser::parseArchiveWithHeader(MIMEHeader* header) bool endOfArchive = false; while (!endOfArchive) { - RefPtr<MIMEHeader> resourceHeader = MIMEHeader::parseHeader(&m_lineReader); + RefPtr<MIMEHeader> resourceHeader = MIMEHeader::parseHeader(m_lineReader); if (!resourceHeader) { LOG_ERROR("Failed to parse MHTML, invalid MIME header."); - return 0; + return nullptr; } if (resourceHeader->contentType() == "multipart/alternative") { // Ignore IE nesting which makes little sense (IE seems to nest only some of the frames). RefPtr<MHTMLArchive> subframeArchive = parseArchiveWithHeader(resourceHeader.get()); if (!subframeArchive) { LOG_ERROR("Failed to parse MHTML subframe."); - return 0; + return nullptr; } bool endOfPartReached = skipLinesUntilBoundaryFound(m_lineReader, header->endOfPartBoundary()); ASSERT_UNUSED(endOfPartReached, endOfPartReached); // The top-frame is the first frame found, regardless of the nesting level. if (subframeArchive->mainResource()) addResourceToArchive(subframeArchive->mainResource(), archive.get()); - archive->addSubframeArchive(subframeArchive); + archive->addSubframeArchive(subframeArchive.releaseNonNull()); continue; } RefPtr<ArchiveResource> resource = parseNextPart(*resourceHeader, header->endOfPartBoundary(), header->endOfDocumentBoundary(), endOfArchive); if (!resource) { LOG_ERROR("Failed to parse MHTML part."); - return 0; + return nullptr; } addResourceToArchive(resource.get(), archive.get()); } @@ -128,17 +126,17 @@ void MHTMLParser::addResourceToArchive(ArchiveResource* resource, MHTMLArchive* // The first document suitable resource is the main frame. if (!archive->mainResource()) { - archive->setMainResource(resource); + archive->setMainResource(*resource); m_frames.append(archive); return; } RefPtr<MHTMLArchive> subframe = MHTMLArchive::create(); - subframe->setMainResource(resource); + subframe->setMainResource(*resource); m_frames.append(subframe); } -PassRefPtr<ArchiveResource> MHTMLParser::parseNextPart(const MIMEHeader& mimeHeader, const String& endOfPartBoundary, const String& endOfDocumentBoundary, bool& endOfArchiveReached) +RefPtr<ArchiveResource> MHTMLParser::parseNextPart(const MIMEHeader& mimeHeader, const String& endOfPartBoundary, const String& endOfDocumentBoundary, bool& endOfArchiveReached) { ASSERT(endOfPartBoundary.isEmpty() == endOfDocumentBoundary.isEmpty()); @@ -148,31 +146,31 @@ PassRefPtr<ArchiveResource> MHTMLParser::parseNextPart(const MIMEHeader& mimeHea if (mimeHeader.contentTransferEncoding() == MIMEHeader::Binary) { if (!checkBoundary) { LOG_ERROR("Binary contents requires end of part"); - return 0; + return nullptr; } m_lineReader.setSeparator(endOfPartBoundary.utf8().data()); Vector<char> part; if (!m_lineReader.nextChunk(part)) { LOG_ERROR("Binary contents requires end of part"); - return 0; - } - content->append(part); - m_lineReader.setSeparator("\r\n"); - Vector<char> nextChars; - if (m_lineReader.peek(nextChars, 2) != 2) { - LOG_ERROR("Invalid seperator."); - return 0; - } - endOfPartReached = true; - ASSERT(nextChars.size() == 2); - endOfArchiveReached = (nextChars[0] == '-' && nextChars[1] == '-'); - if (!endOfArchiveReached) { - String line = m_lineReader.nextChunkAsUTF8StringWithLatin1Fallback(); - if (!line.isEmpty()) { - LOG_ERROR("No CRLF at end of binary section."); - return 0; - } - } + return nullptr; + } + content->append(part); + m_lineReader.setSeparator("\r\n"); + Vector<char> nextChars; + if (m_lineReader.peek(nextChars, 2) != 2) { + LOG_ERROR("Invalid seperator."); + return nullptr; + } + endOfPartReached = true; + ASSERT(nextChars.size() == 2); + endOfArchiveReached = (nextChars[0] == '-' && nextChars[1] == '-'); + if (!endOfArchiveReached) { + String line = m_lineReader.nextChunkAsUTF8StringWithLatin1Fallback(); + if (!line.isEmpty()) { + LOG_ERROR("No CRLF at end of binary section."); + return nullptr; + } + } } else { String line; while (!(line = m_lineReader.nextChunkAsUTF8StringWithLatin1Fallback()).isNull()) { @@ -191,7 +189,7 @@ PassRefPtr<ArchiveResource> MHTMLParser::parseNextPart(const MIMEHeader& mimeHea } if (!endOfPartReached && checkBoundary) { LOG_ERROR("No bounday found for MHTML part."); - return 0; + return nullptr; } Vector<char> data; @@ -199,7 +197,7 @@ PassRefPtr<ArchiveResource> MHTMLParser::parseNextPart(const MIMEHeader& mimeHea case MIMEHeader::Base64: if (!base64Decode(content->data(), content->size(), data)) { LOG_ERROR("Invalid base64 content for MHTML part."); - return 0; + return nullptr; } break; case MIMEHeader::QuotedPrintable: @@ -211,14 +209,14 @@ PassRefPtr<ArchiveResource> MHTMLParser::parseNextPart(const MIMEHeader& mimeHea break; default: LOG_ERROR("Invalid encoding for MHTML part."); - return 0; + return nullptr; } RefPtr<SharedBuffer> contentBuffer = SharedBuffer::adoptVector(data); // FIXME: the URL in the MIME header could be relative, we should resolve it if it is. // The specs mentions 5 ways to resolve a URL: http://tools.ietf.org/html/rfc2557#section-5 // IE and Firefox (UNMht) seem to generate only absolute URLs. URL location = URL(URL(), mimeHeader.contentLocation()); - return ArchiveResource::create(contentBuffer, location, mimeHeader.contentType(), mimeHeader.charset(), String()); + return ArchiveResource::create(WTFMove(contentBuffer), location, mimeHeader.contentType(), mimeHeader.charset(), String()); } size_t MHTMLParser::frameCount() const diff --git a/Source/WebCore/loader/archive/mhtml/MHTMLParser.h b/Source/WebCore/loader/archive/mhtml/MHTMLParser.h index 4f1b126bc..1e9bd4d77 100644 --- a/Source/WebCore/loader/archive/mhtml/MHTMLParser.h +++ b/Source/WebCore/loader/archive/mhtml/MHTMLParser.h @@ -28,10 +28,10 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef MHTMLParser_h -#define MHTMLParser_h +#pragma once #if ENABLE(MHTML) + #include "SharedBufferChunkReader.h" #include <wtf/RefPtr.h> #include <wtf/text/WTFString.h> @@ -48,7 +48,7 @@ class MHTMLParser { public: explicit MHTMLParser(SharedBuffer*); - PassRefPtr<MHTMLArchive> parseArchive(); + RefPtr<MHTMLArchive> parseArchive(); size_t frameCount() const; MHTMLArchive* frameAt(size_t) const; @@ -57,8 +57,8 @@ public: ArchiveResource* subResourceAt(size_t) const; private: - PassRefPtr<MHTMLArchive> parseArchiveWithHeader(MIMEHeader*); - PassRefPtr<ArchiveResource> parseNextPart(const MIMEHeader&, const String& endOfPartBoundary, const String& endOfDocumentBoundary, bool& endOfArchiveReached); + RefPtr<MHTMLArchive> parseArchiveWithHeader(MIMEHeader*); + RefPtr<ArchiveResource> parseNextPart(const MIMEHeader&, const String& endOfPartBoundary, const String& endOfDocumentBoundary, bool& endOfArchiveReached); void addResourceToArchive(ArchiveResource*, MHTMLArchive*); @@ -67,8 +67,6 @@ private: Vector<RefPtr<MHTMLArchive>> m_frames; }; -} - -#endif -#endif +} // namespace WebCore +#endif // ENABLE(MHTML) |