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("application/x-webarchive"_s, archiveFactoryCreate<LegacyWebArchive>);
#endif
#if ENABLE(MHTML)
map.add("multipart/related"_s, archiveFactoryCreate<MHTMLArchive>);
map.add("application/x-mimearchive"_s, 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(HashSet<String, ASCIICaseInsensitiveHash>& mimeTypes)
{
for (auto& mimeType : archiveMIMETypes().keys())
mimeTypes.add(mimeType);
}
}