ArchiveFactory.cpp [plain text]
#include "config.h"
#include "ArchiveFactory.h"
#include "MIMETypeRegistry.h"
#include "PlatformString.h"
#if USE(CF) && !PLATFORM(QT) && ENABLE(WEB_ARCHIVE)
#include "LegacyWebArchive.h"
#endif
#if ENABLE(MHTML)
#include "MHTMLArchive.h"
#endif
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
#include <wtf/StdLibExtras.h>
namespace WebCore {
typedef PassRefPtr<Archive> RawDataCreationFunction(const KURL&, SharedBuffer*);
typedef HashMap<String, RawDataCreationFunction*, CaseFoldingHash> ArchiveMIMETypesMap;
template <typename ArchiveClass> static PassRefPtr<Archive> archiveFactoryCreate(const KURL& url, SharedBuffer* buffer)
{
return ArchiveClass::create(url, buffer);
}
static ArchiveMIMETypesMap& archiveMIMETypes()
{
DEFINE_STATIC_LOCAL(ArchiveMIMETypesMap, mimeTypes, ());
static bool initialized = false;
if (initialized)
return mimeTypes;
#if ENABLE(WEB_ARCHIVE) && USE(CF)
mimeTypes.set("application/x-webarchive", archiveFactoryCreate<LegacyWebArchive>);
#endif
#if ENABLE(MHTML)
mimeTypes.set("multipart/related", archiveFactoryCreate<MHTMLArchive>);
#endif
initialized = true;
return mimeTypes;
}
bool ArchiveFactory::isArchiveMimeType(const String& mimeType)
{
return !mimeType.isEmpty() && archiveMIMETypes().contains(mimeType);
}
PassRefPtr<Archive> ArchiveFactory::create(const KURL& url, SharedBuffer* data, const String& mimeType)
{
RawDataCreationFunction* function = mimeType.isEmpty() ? 0 : archiveMIMETypes().get(mimeType);
return function ? function(url, data) : PassRefPtr<Archive>(0);
}
void ArchiveFactory::registerKnownArchiveMIMETypes()
{
HashSet<String>& mimeTypes = MIMETypeRegistry::getSupportedNonImageMIMETypes();
ArchiveMIMETypesMap::iterator i = archiveMIMETypes().begin();
ArchiveMIMETypesMap::iterator end = archiveMIMETypes().end();
for (; i != end; ++i)
mimeTypes.add(i->first);
}
}