ArchiveFactory.cpp [plain text]
#include "config.h"
#include "ArchiveFactory.h"
#include "MIMETypeRegistry.h"
#if ENABLE(WEB_ARCHIVE) && USE(CF)
#include "LegacyWebArchive.h"
#endif
#if ENABLE(MHTML)
#include "MHTMLArchive.h"
#endif
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/StdLibExtras.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
typedef RefPtr<Archive> RawDataCreationFunction(const URL&, SharedBuffer&);
typedef HashMap<String, RawDataCreationFunction*, ASCIICaseInsensitiveHash> ArchiveMIMETypesMap;
template<typename ArchiveClass> static RefPtr<Archive> archiveFactoryCreate(const URL& url, SharedBuffer& buffer)
{
return ArchiveClass::create(url, buffer);
}
static ArchiveMIMETypesMap createArchiveMIMETypesMap()
{
ArchiveMIMETypesMap map;
#if ENABLE(WEB_ARCHIVE) && USE(CF)
map.add(ASCIILiteral { "application/x-webarchive" }, archiveFactoryCreate<LegacyWebArchive>);
#endif
#if ENABLE(MHTML)
map.add(ASCIILiteral { "multipart/related" }, archiveFactoryCreate<MHTMLArchive>);
map.add(ASCIILiteral { "application/x-mimearchive" }, archiveFactoryCreate<MHTMLArchive>);
#endif
return map;
}
static ArchiveMIMETypesMap& archiveMIMETypes()
{
static NeverDestroyed<ArchiveMIMETypesMap> map = createArchiveMIMETypesMap();
return map;
}
bool ArchiveFactory::isArchiveMimeType(const String& mimeType)
{
return !mimeType.isEmpty() && archiveMIMETypes().contains(mimeType);
}
RefPtr<Archive> ArchiveFactory::create(const URL& url, SharedBuffer* data, const String& mimeType)
{
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()
{
auto& mimeTypes = MIMETypeRegistry::getSupportedNonImageMIMETypes();
for (auto& mimeType : archiveMIMETypes().keys())
mimeTypes.add(mimeType);
}
}