#ifndef DiskImageCache_h
#define DiskImageCache_h
#if ENABLE(DISK_IMAGE_CACHE)
#include "DiskImageCacheClient.h"
#include "PlatformString.h"
#include "SharedBuffer.h"
#include <wtf/HashMap.h>
#include <wtf/Noncopyable.h>
#include <wtf/RefCounted.h>
#include <wtf/RefPtr.h>
#include <wtf/Threading.h>
typedef unsigned disk_cache_id_t;
namespace WebCore {
class DiskImageCache {
WTF_MAKE_NONCOPYABLE(DiskImageCache);
private:
class Entry : public RefCounted<Entry> {
private:
Entry(SharedBuffer* buffer, disk_cache_id_t id);
public:
static PassRefPtr<DiskImageCache::Entry> create(SharedBuffer* buffer, disk_cache_id_t id)
{
return adoptRef(new DiskImageCache::Entry(buffer, id));
}
~Entry();
void map(const String& path);
void unmap();
void removeFile();
void clearDataWithoutMapping();
disk_cache_id_t id() const { return m_id; }
unsigned size() const { return m_size; }
void* data() const { return m_mapping; }
bool isMapped() const { return m_mapping != NULL; }
private:
bool mapInternal(const String& path);
SharedBuffer* m_buffer;
disk_cache_id_t m_id;
unsigned m_size;
void* m_mapping;
String m_path;
};
public:
friend DiskImageCache* diskImageCache();
static const disk_cache_id_t invalidDiskCacheId = 0;
disk_cache_id_t writeItem(PassRefPtr<SharedBuffer>);
void removeItem(disk_cache_id_t);
void* dataForItem(disk_cache_id_t);
void setClient(PassRefPtr<DiskImageCacheClient> client) { m_client = client; }
bool isEnabled() const { return m_enabled; }
void setEnabled(bool enabled) { m_enabled = enabled; }
unsigned maximumCacheSize() const { return m_maximumCacheSize; }
void setMaximumCacheSize(unsigned maximum) { m_maximumCacheSize = maximum; }
unsigned minimumImageSize() const { return m_minimumImageSize; }
void setMinimumImageSize(unsigned minimum) { m_minimumImageSize = minimum; }
const String& cacheDirectory() const { return m_cacheDirectory; }
unsigned size() const { return m_size; }
bool isFull() const { return m_size >= m_maximumCacheSize; }
private:
DiskImageCache();
~DiskImageCache() { }
bool createDirectoryIfNeeded();
disk_cache_id_t nextAvailableId();
String temporaryDirectory();
String temporaryFile();
void updateSize(unsigned delta);
bool m_enabled;
unsigned m_size;
unsigned m_maximumCacheSize;
unsigned m_minimumImageSize;
disk_cache_id_t m_nextAvailableId;
String m_cacheDirectory;
RefPtr<DiskImageCacheClient> m_client;
HashMap<disk_cache_id_t, RefPtr<DiskImageCache::Entry> > m_table;
Mutex m_mutex;
};
DiskImageCache* diskImageCache();
}
#endif // ENABLE(DISK_IMAGE_CACHE)
#endif // DiskImageCache_h