1/* 2 * Copyright (c) 2009, Google 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 are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31#ifndef ApplicationCacheHost_h 32#define ApplicationCacheHost_h 33 34#include "KURL.h" 35#include <wtf/Deque.h> 36#include <wtf/OwnPtr.h> 37#include <wtf/PassRefPtr.h> 38#include <wtf/RefPtr.h> 39#include <wtf/Vector.h> 40 41namespace WebCore { 42 class DOMApplicationCache; 43 class DocumentLoader; 44 class Frame; 45 class ResourceLoader; 46 class ResourceError; 47 class ResourceRequest; 48 class ResourceResponse; 49 class SubstituteData; 50 class ApplicationCache; 51 class ApplicationCacheGroup; 52 class ApplicationCacheResource; 53 class ApplicationCacheStorage; 54 55 class ApplicationCacheHost { 56 WTF_MAKE_NONCOPYABLE(ApplicationCacheHost); WTF_MAKE_FAST_ALLOCATED; 57 public: 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 enum EventID { 69 CHECKING_EVENT = 0, 70 ERROR_EVENT, 71 NOUPDATE_EVENT, 72 DOWNLOADING_EVENT, 73 PROGRESS_EVENT, 74 UPDATEREADY_EVENT, 75 CACHED_EVENT, 76 OBSOLETE_EVENT // Must remain the last value, this is used to size arrays. 77 }; 78 79#if ENABLE(INSPECTOR) 80 struct CacheInfo { 81 CacheInfo(const KURL& manifest, double creationTime, double updateTime, long long size) 82 : m_manifest(manifest) 83 , m_creationTime(creationTime) 84 , m_updateTime(updateTime) 85 , m_size(size) { } 86 KURL m_manifest; 87 double m_creationTime; 88 double m_updateTime; 89 long long m_size; 90 }; 91 92 struct ResourceInfo { 93 ResourceInfo(const KURL& resource, bool isMaster, bool isManifest, bool isFallback, bool isForeign, bool isExplicit, long long size) 94 : m_resource(resource) 95 , m_isMaster(isMaster) 96 , m_isManifest(isManifest) 97 , m_isFallback(isFallback) 98 , m_isForeign(isForeign) 99 , m_isExplicit(isExplicit) 100 , m_size(size) { } 101 KURL m_resource; 102 bool m_isMaster; 103 bool m_isManifest; 104 bool m_isFallback; 105 bool m_isForeign; 106 bool m_isExplicit; 107 long long m_size; 108 }; 109 110 typedef Vector<ResourceInfo> ResourceInfoList; 111#endif 112 113 ApplicationCacheHost(DocumentLoader*); 114 ~ApplicationCacheHost(); 115 116 void selectCacheWithoutManifest(); 117 void selectCacheWithManifest(const KURL& manifestURL); 118 119 void maybeLoadMainResource(ResourceRequest&, SubstituteData&); 120 void maybeLoadMainResourceForRedirect(ResourceRequest&, SubstituteData&); 121 bool maybeLoadFallbackForMainResponse(const ResourceRequest&, const ResourceResponse&); 122 void mainResourceDataReceived(const char* data, int length, long long encodedDataLength, bool allAtOnce); 123 void finishedLoadingMainResource(); 124 void failedLoadingMainResource(); 125 126 bool maybeLoadResource(ResourceLoader*, ResourceRequest&, const KURL& originalURL); 127 bool maybeLoadFallbackForRedirect(ResourceLoader*, ResourceRequest&, const ResourceResponse&); 128 bool maybeLoadFallbackForResponse(ResourceLoader*, const ResourceResponse&); 129 bool maybeLoadFallbackForError(ResourceLoader*, const ResourceError&); 130 131 bool maybeLoadSynchronously(ResourceRequest&, ResourceError&, ResourceResponse&, Vector<char>& data); 132 void maybeLoadFallbackSynchronously(const ResourceRequest&, ResourceError&, ResourceResponse&, Vector<char>& data); 133 134 bool canCacheInPageCache(); 135 136 Status status() const; 137 bool update(); 138 bool swapCache(); 139 void abort(); 140 141 void setDOMApplicationCache(DOMApplicationCache*); 142 void notifyDOMApplicationCache(EventID, int progressTotal, int progressDone); 143 144 void stopLoadingInFrame(Frame*); 145 146 void stopDeferringEvents(); // Also raises the events that have been queued up. 147 148#if ENABLE(INSPECTOR) 149 void fillResourceList(ResourceInfoList*); 150 CacheInfo applicationCacheInfo(); 151#endif 152 153 bool shouldLoadResourceFromApplicationCache(const ResourceRequest&, ApplicationCacheResource*&); 154 bool getApplicationCacheFallbackResource(const ResourceRequest&, ApplicationCacheResource*&, ApplicationCache* = 0); 155 156 private: 157 bool isApplicationCacheEnabled(); 158 DocumentLoader* documentLoader() const { return m_documentLoader; } 159 160 struct DeferredEvent { 161 EventID eventID; 162 int progressTotal; 163 int progressDone; 164 DeferredEvent(EventID id, int total, int done) : eventID(id), progressTotal(total), progressDone(done) { } 165 }; 166 167 DOMApplicationCache* m_domApplicationCache; 168 DocumentLoader* m_documentLoader; 169 bool m_defersEvents; // Events are deferred until after document onload. 170 Vector<DeferredEvent> m_deferredEvents; 171 172 void dispatchDOMEvent(EventID, int progressTotal, int progressDone); 173 174 friend class ApplicationCacheGroup; 175 friend class ApplicationCacheStorage; 176 177 bool scheduleLoadFallbackResourceFromApplicationCache(ResourceLoader*, ApplicationCache* = 0); 178 void setCandidateApplicationCacheGroup(ApplicationCacheGroup* group); 179 ApplicationCacheGroup* candidateApplicationCacheGroup() const { return m_candidateApplicationCacheGroup; } 180 void setApplicationCache(PassRefPtr<ApplicationCache> applicationCache); 181 ApplicationCache* applicationCache() const { return m_applicationCache.get(); } 182 ApplicationCache* mainResourceApplicationCache() const { return m_mainResourceApplicationCache.get(); } 183 bool maybeLoadFallbackForMainError(const ResourceRequest&, const ResourceError&); 184 185 186 // The application cache that the document loader is associated with (if any). 187 RefPtr<ApplicationCache> m_applicationCache; 188 189 // Before an application cache has finished loading, this will be the candidate application 190 // group that the document loader is associated with. 191 ApplicationCacheGroup* m_candidateApplicationCacheGroup; 192 193 // This is the application cache the main resource was loaded from (if any). 194 RefPtr<ApplicationCache> m_mainResourceApplicationCache; 195 }; 196 197} // namespace WebCore 198 199#endif // ApplicationCacheHost_h 200