|
1 /* |
|
2 * Copyright (C) 2008, 2009 Apple Inc. All Rights Reserved. |
|
3 * |
|
4 * Redistribution and use in source and binary forms, with or without |
|
5 * modification, are permitted provided that the following conditions |
|
6 * are met: |
|
7 * 1. Redistributions of source code must retain the above copyright |
|
8 * notice, this list of conditions and the following disclaimer. |
|
9 * 2. Redistributions in binary form must reproduce the above copyright |
|
10 * notice, this list of conditions and the following disclaimer in the |
|
11 * documentation and/or other materials provided with the distribution. |
|
12 * |
|
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
|
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
|
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
24 */ |
|
25 |
|
26 #ifndef ApplicationCacheGroup_h |
|
27 #define ApplicationCacheGroup_h |
|
28 |
|
29 #if ENABLE(OFFLINE_WEB_APPLICATIONS) |
|
30 |
|
31 #include <wtf/Noncopyable.h> |
|
32 #include <wtf/HashMap.h> |
|
33 #include <wtf/HashSet.h> |
|
34 |
|
35 #include "DOMApplicationCache.h" |
|
36 #include "KURL.h" |
|
37 #include "PlatformString.h" |
|
38 #include "ResourceHandle.h" |
|
39 #include "ResourceHandleClient.h" |
|
40 #include "SharedBuffer.h" |
|
41 |
|
42 namespace WebCore { |
|
43 |
|
44 class ApplicationCache; |
|
45 class ApplicationCacheResource; |
|
46 class Document; |
|
47 class DocumentLoader; |
|
48 class Frame; |
|
49 |
|
50 enum ApplicationCacheUpdateOption { |
|
51 ApplicationCacheUpdateWithBrowsingContext, |
|
52 ApplicationCacheUpdateWithoutBrowsingContext |
|
53 }; |
|
54 |
|
55 class ApplicationCacheGroup : public Noncopyable, ResourceHandleClient { |
|
56 public: |
|
57 ApplicationCacheGroup(const KURL& manifestURL, bool isCopy = false); |
|
58 ~ApplicationCacheGroup(); |
|
59 |
|
60 enum UpdateStatus { Idle, Checking, Downloading }; |
|
61 |
|
62 static ApplicationCache* cacheForMainRequest(const ResourceRequest&, DocumentLoader*); |
|
63 static ApplicationCache* fallbackCacheForMainRequest(const ResourceRequest&, DocumentLoader*); |
|
64 |
|
65 static void selectCache(Frame*, const KURL& manifestURL); |
|
66 static void selectCacheWithoutManifestURL(Frame*); |
|
67 |
|
68 const KURL& manifestURL() const { return m_manifestURL; } |
|
69 UpdateStatus updateStatus() const { return m_updateStatus; } |
|
70 void setUpdateStatus(UpdateStatus status); |
|
71 |
|
72 void setStorageID(unsigned storageID) { m_storageID = storageID; } |
|
73 unsigned storageID() const { return m_storageID; } |
|
74 void clearStorageID(); |
|
75 |
|
76 void update(Frame*, ApplicationCacheUpdateOption); // FIXME: Frame should not be needed when updating without browsing context. |
|
77 void cacheDestroyed(ApplicationCache*); |
|
78 |
|
79 bool cacheIsBeingUpdated(const ApplicationCache* cache) const { return cache == m_cacheBeingUpdated; } |
|
80 |
|
81 ApplicationCache* newestCache() const { return m_newestCache.get(); } |
|
82 void setNewestCache(PassRefPtr<ApplicationCache>); |
|
83 |
|
84 void makeObsolete(); |
|
85 bool isObsolete() const { return m_isObsolete; } |
|
86 |
|
87 void finishedLoadingMainResource(DocumentLoader*); |
|
88 void failedLoadingMainResource(DocumentLoader*); |
|
89 |
|
90 void disassociateDocumentLoader(DocumentLoader*); |
|
91 |
|
92 bool isCopy() const { return m_isCopy; } |
|
93 |
|
94 private: |
|
95 static void postListenerTask(ApplicationCacheHost::EventID id, const HashSet<DocumentLoader*>& set) { postListenerTask(id, 0, 0, set); } |
|
96 static void postListenerTask(ApplicationCacheHost::EventID id, DocumentLoader* loader) { postListenerTask(id, 0, 0, loader); } |
|
97 static void postListenerTask(ApplicationCacheHost::EventID, int progressTotal, int progressDone, const HashSet<DocumentLoader*>&); |
|
98 static void postListenerTask(ApplicationCacheHost::EventID, int progressTotal, int progressDone, DocumentLoader*); |
|
99 |
|
100 void scheduleReachedMaxAppCacheSizeCallback(); |
|
101 |
|
102 PassRefPtr<ResourceHandle> createResourceHandle(const KURL&, ApplicationCacheResource* newestCachedResource); |
|
103 |
|
104 // For normal resource loading, WebKit client is asked about each resource individually. Since application cache does not belong to any particular document, |
|
105 // the existing client callback cannot be used, so assume that any client that enables application cache also wants it to use credential storage. |
|
106 virtual bool shouldUseCredentialStorage(ResourceHandle*) { return true; } |
|
107 |
|
108 #if ENABLE(INSPECTOR) |
|
109 virtual void willSendRequest(ResourceHandle*, ResourceRequest&, const ResourceResponse&); |
|
110 #endif |
|
111 virtual void didReceiveResponse(ResourceHandle*, const ResourceResponse&); |
|
112 virtual void didReceiveData(ResourceHandle*, const char*, int length, int lengthReceived); |
|
113 virtual void didFinishLoading(ResourceHandle*); |
|
114 virtual void didFail(ResourceHandle*, const ResourceError&); |
|
115 |
|
116 void didReceiveManifestResponse(const ResourceResponse&); |
|
117 void didReceiveManifestData(const char*, int); |
|
118 void didFinishLoadingManifest(); |
|
119 void didReachMaxAppCacheSize(); |
|
120 |
|
121 void startLoadingEntry(); |
|
122 void deliverDelayedMainResources(); |
|
123 void checkIfLoadIsComplete(); |
|
124 void cacheUpdateFailed(); |
|
125 void manifestNotFound(); |
|
126 |
|
127 void addEntry(const String&, unsigned type); |
|
128 |
|
129 void associateDocumentLoaderWithCache(DocumentLoader*, ApplicationCache*); |
|
130 |
|
131 void stopLoading(); |
|
132 |
|
133 KURL m_manifestURL; |
|
134 UpdateStatus m_updateStatus; |
|
135 |
|
136 // This is the newest complete cache in the group. |
|
137 RefPtr<ApplicationCache> m_newestCache; |
|
138 |
|
139 // All complete caches in this cache group. |
|
140 HashSet<ApplicationCache*> m_caches; |
|
141 |
|
142 // The cache being updated (if any). Note that cache updating does not immediately create a new |
|
143 // ApplicationCache object, so this may be null even when update status is not Idle. |
|
144 RefPtr<ApplicationCache> m_cacheBeingUpdated; |
|
145 |
|
146 // List of pending master entries, used during the update process to ensure that new master entries are cached. |
|
147 HashSet<DocumentLoader*> m_pendingMasterResourceLoaders; |
|
148 // How many of the above pending master entries have not yet finished downloading. |
|
149 int m_downloadingPendingMasterResourceLoadersCount; |
|
150 |
|
151 // These are all the document loaders that are associated with a cache in this group. |
|
152 HashSet<DocumentLoader*> m_associatedDocumentLoaders; |
|
153 |
|
154 // The URLs and types of pending cache entries. |
|
155 typedef HashMap<String, unsigned> EntryMap; |
|
156 EntryMap m_pendingEntries; |
|
157 |
|
158 // The total number of items to be processed to update the cache group and the number that have been done. |
|
159 int m_progressTotal; |
|
160 int m_progressDone; |
|
161 |
|
162 // Frame used for fetching resources when updating. |
|
163 // FIXME: An update started by a particular frame should not stop if it is destroyed, but there are other frames associated with the same cache group. |
|
164 Frame* m_frame; |
|
165 |
|
166 // An obsolete cache group is never stored, but the opposite is not true - storing may fail for multiple reasons, such as exceeding disk quota. |
|
167 unsigned m_storageID; |
|
168 bool m_isObsolete; |
|
169 |
|
170 // During update, this is used to handle asynchronously arriving results. |
|
171 enum CompletionType { |
|
172 None, |
|
173 NoUpdate, |
|
174 Failure, |
|
175 Completed |
|
176 }; |
|
177 CompletionType m_completionType; |
|
178 |
|
179 // Whether this cache group is a copy that's only used for transferring the cache to another file. |
|
180 bool m_isCopy; |
|
181 |
|
182 // This flag is set immediately after the ChromeClient::reachedMaxAppCacheSize() callback is invoked as a result of the storage layer failing to save a cache |
|
183 // due to reaching the maximum size of the application cache database file. This flag is used by ApplicationCacheGroup::checkIfLoadIsComplete() to decide |
|
184 // the course of action in case of this failure (i.e. call the ChromeClient callback or run the failure steps). |
|
185 bool m_calledReachedMaxAppCacheSize; |
|
186 |
|
187 RefPtr<ResourceHandle> m_currentHandle; |
|
188 RefPtr<ApplicationCacheResource> m_currentResource; |
|
189 |
|
190 #if ENABLE(INSPECTOR) |
|
191 unsigned long m_currentResourceIdentifier; |
|
192 #endif |
|
193 |
|
194 RefPtr<ApplicationCacheResource> m_manifestResource; |
|
195 RefPtr<ResourceHandle> m_manifestHandle; |
|
196 |
|
197 friend class ChromeClientCallbackTimer; |
|
198 }; |
|
199 |
|
200 } // namespace WebCore |
|
201 |
|
202 #endif // ENABLE(OFFLINE_WEB_APPLICATIONS) |
|
203 |
|
204 #endif // ApplicationCacheGroup_h |