2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef ApplicationCacheGroup_h
27 #define ApplicationCacheGroup_h
29 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
31 #include <wtf/Noncopyable.h>
32 #include <wtf/HashMap.h>
33 #include <wtf/HashSet.h>
36 #include "PlatformString.h"
37 #include "ResourceHandle.h"
38 #include "ResourceHandleClient.h"
39 #include "SharedBuffer.h"
43 class ApplicationCache;
44 class ApplicationCacheResource;
45 class DOMApplicationCache;
50 class ApplicationCacheGroup : Noncopyable, ResourceHandleClient {
52 ApplicationCacheGroup(const KURL& manifestURL);
53 ~ApplicationCacheGroup();
55 enum Status { Idle, Checking, Downloading };
57 static ApplicationCache* cacheForMainRequest(const ResourceRequest&, DocumentLoader*);
59 static void selectCache(Frame*, const KURL& manifestURL);
60 static void selectCacheWithoutManifestURL(Frame*);
62 const KURL& manifestURL() const { return m_manifestURL; }
63 Status status() const { return m_status; }
65 void setStorageID(unsigned storageID) { m_storageID = storageID; }
66 unsigned storageID() const { return m_storageID; }
67 void clearStorageID();
70 void cacheDestroyed(ApplicationCache*);
72 ApplicationCache* newestCache() const { return m_newestCache.get(); }
74 void finishedLoadingMainResource(DocumentLoader* loader);
75 void documentLoaderDestroyed(DocumentLoader* loader);
77 void setNewestCache(PassRefPtr<ApplicationCache> newestCache);
80 typedef void (DOMApplicationCache::*ListenerFunction)();
81 void callListenersOnAssociatedDocuments(ListenerFunction);
82 void callListeners(ListenerFunction, const Vector<RefPtr<DocumentLoader> >& loaders);
84 virtual void didReceiveResponse(ResourceHandle*, const ResourceResponse&);
85 virtual void didReceiveData(ResourceHandle*, const char*, int, int lengthReceived);
86 virtual void didFinishLoading(ResourceHandle*);
87 virtual void didFail(ResourceHandle*, const ResourceError&);
89 void didReceiveManifestResponse(const ResourceResponse&);
90 void didReceiveManifestData(const char*, int);
91 void didFinishLoadingManifest();
92 void didFailToLoadManifest();
94 void startLoadingEntry();
95 void checkIfLoadIsComplete();
96 void cacheUpdateFailed();
98 void addEntry(const String&, unsigned type);
100 void associateDocumentLoaderWithCache(DocumentLoader*, ApplicationCache*);
107 // This is the newest cache in the group.
108 RefPtr<ApplicationCache> m_newestCache;
110 // During tear-down we save the pointer to the newest cache to prevent reference cycles.
111 ApplicationCache* m_savedNewestCachePointer;
113 // The caches in this cache group.
114 HashSet<ApplicationCache*> m_caches;
116 // The cache being updated (if any).
117 RefPtr<ApplicationCache> m_cacheBeingUpdated;
119 // When a cache group does not yet have a complete cache, this contains the document loaders
120 // that should be associated with the cache once it has been downloaded.
121 HashSet<DocumentLoader*> m_cacheCandidates;
123 // These are all the document loaders that are associated with a cache in this group.
124 HashSet<DocumentLoader*> m_associatedDocumentLoaders;
126 // The URLs and types of pending cache entries.
127 typedef HashMap<String, unsigned> EntryMap;
128 EntryMap m_pendingEntries;
130 // Frame used for fetching resources when updating
133 unsigned m_storageID;
135 RefPtr<ResourceHandle> m_currentHandle;
136 RefPtr<ApplicationCacheResource> m_currentResource;
138 RefPtr<ApplicationCacheResource> m_manifestResource;
139 RefPtr<ResourceHandle> m_manifestHandle;
142 } // namespace WebCore
144 #endif // ENABLE(OFFLINE_WEB_APPLICATIONS)
146 #endif // ApplicationCacheGroup_h