1/*
2 * Copyright 2016 The Chromium Authors. All rights reserved.
3 * Copyright (C) 2016 Akamai Technologies Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include "CachedCSSStyleSheet.h"
30#include "CachedFont.h"
31#include "CachedFontClient.h"
32#include "CachedImage.h"
33#include "CachedImageClient.h"
34#include "CachedRawResource.h"
35#include "CachedRawResourceClient.h"
36#include "CachedResourceHandle.h"
37#include "CachedScript.h"
38#include "CachedStyleSheetClient.h"
39#include "CachedTextTrack.h"
40
41#include <wtf/WeakPtr.h>
42
43namespace WebCore {
44
45class LinkLoader;
46
47class LinkPreloadResourceClient {
48public:
49 virtual ~LinkPreloadResourceClient() = default;
50
51 void triggerEvents(const CachedResource&);
52
53 virtual void clear() = 0;
54
55protected:
56 LinkPreloadResourceClient(LinkLoader&, CachedResource&);
57
58 void addResource(CachedResourceClient& client)
59 {
60 m_resource->addClient(client);
61 }
62
63 void clearResource(CachedResourceClient& client)
64 {
65 if (!m_resource)
66 return;
67
68 m_resource->removeClient(client);
69 m_resource = nullptr;
70 }
71
72 CachedResource* ownedResource() { return m_resource.get(); }
73
74private:
75 WeakPtr<LinkLoader> m_loader;
76 CachedResourceHandle<CachedResource> m_resource;
77};
78
79class LinkPreloadDefaultResourceClient : public LinkPreloadResourceClient, CachedResourceClient {
80 WTF_MAKE_FAST_ALLOCATED;
81public:
82 LinkPreloadDefaultResourceClient(LinkLoader& loader, CachedResource& resource)
83 : LinkPreloadResourceClient(loader, resource)
84 {
85 addResource(*this);
86 }
87
88private:
89 void notifyFinished(CachedResource& resource) final { triggerEvents(resource); }
90 void clear() final { clearResource(*this); }
91 bool shouldMarkAsReferenced() const final { return false; }
92};
93
94class LinkPreloadStyleResourceClient : public LinkPreloadResourceClient, public CachedStyleSheetClient {
95 WTF_MAKE_FAST_ALLOCATED;
96public:
97 LinkPreloadStyleResourceClient(LinkLoader& loader, CachedCSSStyleSheet& resource)
98 : LinkPreloadResourceClient(loader, resource)
99 {
100 addResource(*this);
101 }
102
103private:
104 void setCSSStyleSheet(const String&, const URL&, const String&, const CachedCSSStyleSheet* resource) final
105 {
106 ASSERT(resource);
107 ASSERT(ownedResource() == resource);
108 triggerEvents(*resource);
109 }
110
111 void clear() final { clearResource(*this); }
112 bool shouldMarkAsReferenced() const final { return false; }
113};
114
115class LinkPreloadImageResourceClient : public LinkPreloadResourceClient, public CachedImageClient {
116 WTF_MAKE_FAST_ALLOCATED;
117public:
118 LinkPreloadImageResourceClient(LinkLoader& loader, CachedImage& resource)
119 : LinkPreloadResourceClient(loader, static_cast<CachedResource&>(resource))
120 {
121 addResource(*this);
122 }
123
124private:
125 void notifyFinished(CachedResource& resource) final { triggerEvents(resource); }
126 void clear() final { clearResource(*this); }
127 bool shouldMarkAsReferenced() const final { return false; }
128};
129
130class LinkPreloadFontResourceClient : public LinkPreloadResourceClient, public CachedFontClient {
131 WTF_MAKE_FAST_ALLOCATED;
132public:
133 LinkPreloadFontResourceClient(LinkLoader& loader, CachedFont& resource)
134 : LinkPreloadResourceClient(loader, resource)
135 {
136 addResource(*this);
137 }
138
139private:
140 void fontLoaded(CachedFont& resource) final
141 {
142 ASSERT(ownedResource() == &resource);
143 triggerEvents(resource);
144 }
145
146 void clear() final { clearResource(*this); }
147 bool shouldMarkAsReferenced() const final { return false; }
148};
149
150class LinkPreloadRawResourceClient : public LinkPreloadResourceClient, public CachedRawResourceClient {
151 WTF_MAKE_FAST_ALLOCATED;
152public:
153 LinkPreloadRawResourceClient(LinkLoader& loader, CachedRawResource& resource)
154 : LinkPreloadResourceClient(loader, resource)
155 {
156 addResource(*this);
157 }
158
159private:
160 void notifyFinished(CachedResource& resource) final { triggerEvents(resource); }
161 void clear() final { clearResource(*this); }
162 bool shouldMarkAsReferenced() const final { return false; }
163};
164
165}
166