1/*
2 * Copyright (c) 2009, Google Inc. All rights reserved.
3 * Copyright (c) 2017 Apple 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 are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#pragma once
33
34#include <wtf/Deque.h>
35#include <wtf/URL.h>
36#include <wtf/Vector.h>
37#include <wtf/WeakPtr.h>
38
39namespace WebCore {
40
41class ApplicationCache;
42class ApplicationCacheGroup;
43class ApplicationCacheResource;
44class ApplicationCacheStorage;
45class DOMApplicationCache;
46class DocumentLoader;
47class Frame;
48class ResourceError;
49class ResourceLoader;
50class ResourceRequest;
51class ResourceResponse;
52class SharedBuffer;
53class SubstituteData;
54
55class ApplicationCacheHost {
56 WTF_MAKE_NONCOPYABLE(ApplicationCacheHost); WTF_MAKE_FAST_ALLOCATED;
57public:
58 // The Status numeric values are specified in the HTML5 spec.
59 enum Status {
60 UNCACHED = 0,
61 IDLE = 1,
62 CHECKING = 2,
63 DOWNLOADING = 3,
64 UPDATEREADY = 4,
65 OBSOLETE = 5
66 };
67
68 struct CacheInfo {
69 URL manifest;
70 double creationTime;
71 double updateTime;
72 long long size;
73 };
74
75 struct ResourceInfo {
76 URL resource;
77 bool isMaster;
78 bool isManifest;
79 bool isFallback;
80 bool isForeign;
81 bool isExplicit;
82 long long size;
83 };
84
85 explicit ApplicationCacheHost(DocumentLoader&);
86 ~ApplicationCacheHost();
87
88 static URL createFileURL(const String&);
89
90 void selectCacheWithoutManifest();
91 void selectCacheWithManifest(const URL& manifestURL);
92
93 void maybeLoadMainResource(const ResourceRequest&, SubstituteData&);
94 void maybeLoadMainResourceForRedirect(const ResourceRequest&, SubstituteData&);
95 bool maybeLoadFallbackForMainResponse(const ResourceRequest&, const ResourceResponse&);
96 void mainResourceDataReceived(const char* data, int length, long long encodedDataLength, bool allAtOnce);
97 void finishedLoadingMainResource();
98 void failedLoadingMainResource();
99
100 WEBCORE_EXPORT bool maybeLoadResource(ResourceLoader&, const ResourceRequest&, const URL& originalURL);
101 WEBCORE_EXPORT bool maybeLoadFallbackForRedirect(ResourceLoader*, ResourceRequest&, const ResourceResponse&);
102 WEBCORE_EXPORT bool maybeLoadFallbackForResponse(ResourceLoader*, const ResourceResponse&);
103 WEBCORE_EXPORT bool maybeLoadFallbackForError(ResourceLoader*, const ResourceError&);
104
105 bool maybeLoadSynchronously(ResourceRequest&, ResourceError&, ResourceResponse&, RefPtr<SharedBuffer>&);
106 void maybeLoadFallbackSynchronously(const ResourceRequest&, ResourceError&, ResourceResponse&, RefPtr<SharedBuffer>&);
107
108 bool canCacheInPageCache();
109
110 Status status() const;
111 bool update();
112 bool swapCache();
113 void abort();
114
115 void setDOMApplicationCache(DOMApplicationCache*);
116 void notifyDOMApplicationCache(const AtomString& eventType, int progressTotal, int progressDone);
117
118 void stopLoadingInFrame(Frame&);
119
120 void stopDeferringEvents(); // Also raises the events that have been queued up.
121
122 Vector<ResourceInfo> resourceList();
123 CacheInfo applicationCacheInfo();
124
125 bool shouldLoadResourceFromApplicationCache(const ResourceRequest&, ApplicationCacheResource*&);
126 bool getApplicationCacheFallbackResource(const ResourceRequest&, ApplicationCacheResource*&, ApplicationCache* = nullptr);
127
128private:
129 friend class ApplicationCacheGroup;
130
131 struct DeferredEvent {
132 AtomString eventType;
133 int progressTotal;
134 int progressDone;
135 };
136
137 bool isApplicationCacheEnabled();
138 bool isApplicationCacheBlockedForRequest(const ResourceRequest&);
139
140 void dispatchDOMEvent(const AtomString& eventType, int progressTotal, int progressDone);
141
142 bool scheduleLoadFallbackResourceFromApplicationCache(ResourceLoader*, ApplicationCache* = nullptr);
143 void setCandidateApplicationCacheGroup(ApplicationCacheGroup*);
144 ApplicationCacheGroup* candidateApplicationCacheGroup() const { return m_candidateApplicationCacheGroup; }
145 void setApplicationCache(RefPtr<ApplicationCache>&&);
146 ApplicationCache* applicationCache() const { return m_applicationCache.get(); }
147 ApplicationCache* mainResourceApplicationCache() const { return m_mainResourceApplicationCache.get(); }
148 bool maybeLoadFallbackForMainError(const ResourceRequest&, const ResourceError&);
149
150 WeakPtr<DOMApplicationCache> m_domApplicationCache;
151 DocumentLoader& m_documentLoader;
152
153 bool m_defersEvents { true }; // Events are deferred until after document onload.
154 Vector<DeferredEvent> m_deferredEvents;
155
156 // The application cache that the document loader is associated with (if any).
157 RefPtr<ApplicationCache> m_applicationCache;
158
159 // Before an application cache has finished loading, this will be the candidate application
160 // group that the document loader is associated with.
161 ApplicationCacheGroup* m_candidateApplicationCacheGroup { nullptr };
162
163 // This is the application cache the main resource was loaded from (if any).
164 RefPtr<ApplicationCache> m_mainResourceApplicationCache;
165};
166
167} // namespace WebCore
168