LinkPreloadResourceClients.h [plain text]
#pragma once
#include "CachedCSSStyleSheet.h"
#include "CachedFont.h"
#include "CachedFontClient.h"
#include "CachedImage.h"
#include "CachedImageClient.h"
#include "CachedRawResource.h"
#include "CachedRawResourceClient.h"
#include "CachedResourceHandle.h"
#include "CachedScript.h"
#include "CachedStyleSheetClient.h"
#include <wtf/WeakPtr.h>
namespace WebCore {
class LinkLoader;
class LinkPreloadResourceClient {
public:
virtual ~LinkPreloadResourceClient() { }
void triggerEvents(const CachedResource&);
virtual void clear() = 0;
protected:
LinkPreloadResourceClient(LinkLoader&, CachedResource&);
void addResource(CachedResourceClient& client)
{
m_resource->addClient(client);
}
void clearResource(CachedResourceClient& client)
{
if (m_resource) {
m_resource->cancelLoad();
m_resource->removeClient(client);
}
m_resource = nullptr;
}
CachedResource* ownedResource() { return m_resource.get(); }
private:
WeakPtr<LinkLoader> m_loader;
CachedResourceHandle<CachedResource> m_resource;
};
class LinkPreloadScriptResourceClient: public LinkPreloadResourceClient, CachedResourceClient {
public:
static std::unique_ptr<LinkPreloadScriptResourceClient> create(LinkLoader& loader, CachedScript& resource)
{
return std::unique_ptr<LinkPreloadScriptResourceClient>(new LinkPreloadScriptResourceClient(loader, resource));
}
virtual ~LinkPreloadScriptResourceClient() { }
void notifyFinished(CachedResource& resource) override { triggerEvents(resource); }
void clear() override { clearResource(*this); }
bool shouldMarkAsReferenced() const override { return false; }
private:
LinkPreloadScriptResourceClient(LinkLoader& loader, CachedScript& resource)
: LinkPreloadResourceClient(loader, resource)
{
addResource(*this);
}
};
class LinkPreloadStyleResourceClient: public LinkPreloadResourceClient, public CachedStyleSheetClient {
public:
static std::unique_ptr<LinkPreloadStyleResourceClient> create(LinkLoader& loader, CachedCSSStyleSheet& resource)
{
return std::unique_ptr<LinkPreloadStyleResourceClient>(new LinkPreloadStyleResourceClient(loader, resource));
}
virtual ~LinkPreloadStyleResourceClient() { }
void setCSSStyleSheet(const String&, const URL&, const String&, const CachedCSSStyleSheet* resource) override
{
ASSERT(resource);
ASSERT(ownedResource() == resource);
triggerEvents(*resource);
}
void clear() override { clearResource(*this); }
bool shouldMarkAsReferenced() const override { return false; }
private:
LinkPreloadStyleResourceClient(LinkLoader& loader, CachedCSSStyleSheet& resource)
: LinkPreloadResourceClient(loader, resource)
{
addResource(*this);
}
};
class LinkPreloadImageResourceClient: public LinkPreloadResourceClient, public CachedImageClient {
public:
static std::unique_ptr<LinkPreloadImageResourceClient> create(LinkLoader& loader, CachedImage& resource)
{
return std::unique_ptr<LinkPreloadImageResourceClient>(new LinkPreloadImageResourceClient(loader, resource));
}
virtual ~LinkPreloadImageResourceClient() { }
void notifyFinished(CachedResource& resource) override { triggerEvents(resource); }
void clear() override { clearResource(*this); }
bool shouldMarkAsReferenced() const override { return false; }
private:
LinkPreloadImageResourceClient(LinkLoader& loader, CachedImage& resource)
: LinkPreloadResourceClient(loader, dynamic_cast<CachedResource&>(resource))
{
addResource(*this);
}
};
class LinkPreloadFontResourceClient: public LinkPreloadResourceClient, public CachedFontClient {
public:
static std::unique_ptr<LinkPreloadFontResourceClient> create(LinkLoader& loader, CachedFont& resource)
{
return std::unique_ptr<LinkPreloadFontResourceClient>(new LinkPreloadFontResourceClient(loader, resource));
}
virtual ~LinkPreloadFontResourceClient() { }
void fontLoaded(CachedFont& resource) override
{
ASSERT(ownedResource() == &resource);
triggerEvents(resource);
}
void clear() override { clearResource(*this); }
bool shouldMarkAsReferenced() const override { return false; }
private:
LinkPreloadFontResourceClient(LinkLoader& loader, CachedFont& resource)
: LinkPreloadResourceClient(loader, resource)
{
addResource(*this);
}
};
class LinkPreloadRawResourceClient: public LinkPreloadResourceClient, public CachedRawResourceClient {
public:
static std::unique_ptr<LinkPreloadRawResourceClient> create(LinkLoader& loader, CachedRawResource& resource)
{
return std::unique_ptr<LinkPreloadRawResourceClient>(new LinkPreloadRawResourceClient(loader, resource));
}
virtual ~LinkPreloadRawResourceClient() { }
void notifyFinished(CachedResource& resource) override { triggerEvents(resource); }
void clear() override { clearResource(*this); }
bool shouldMarkAsReferenced() const override { return false; }
private:
LinkPreloadRawResourceClient(LinkLoader& loader, CachedRawResource& resource)
: LinkPreloadResourceClient(loader, resource)
{
addResource(*this);
}
};
}