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 #if ENABLE(OFFLINE_WEB_APPLICATIONS) 35 36 #include <wtf/OwnPtr.h> 37 #include <wtf/PassRefPtr.h> 38 #include <wtf/RefPtr.h> 39 #include <wtf/Vector.h> 40 41 namespace WebCore { 42 43 class DOMApplicationCache; 44 class DocumentLoader; 45 class KURL; 46 class ResourceLoader; 47 class ResourceError; 48 struct ResourceRequest; 49 class ResourceResponse; 50 class SubstituteData; 51 #if PLATFORM(CHROMIUM) 52 class ApplicationCacheHostInternal; 53 #else 54 class ApplicationCache; 55 class ApplicationCacheGroup; 56 class ApplicationCacheResource; 57 class ApplicationCacheStorage; 58 #endif 59 60 class ApplicationCacheHost { 61 public: 62 // The Status numeric values are specified in the HTML5 spec. 63 enum Status { 64 UNCACHED = 0, 65 IDLE = 1, 66 CHECKING = 2, 67 DOWNLOADING = 3, 68 UPDATEREADY = 4, 69 OBSOLETE = 5 70 }; 71 72 enum EventID { 73 CHECKING_EVENT = 0, 74 ERROR_EVENT, 75 NOUPDATE_EVENT, 76 DOWNLOADING_EVENT, 77 PROGRESS_EVENT, 78 UPDATEREADY_EVENT, 79 CACHED_EVENT, 80 OBSOLETE_EVENT // Must remain the last value, this is used to size arrays. 81 }; 82 83 ApplicationCacheHost(DocumentLoader*); 84 ~ApplicationCacheHost(); 85 86 void selectCacheWithoutManifest(); 87 void selectCacheWithManifest(const KURL& manifestURL); 88 89 void maybeLoadMainResource(ResourceRequest&, SubstituteData&); 90 bool maybeLoadFallbackForMainResponse(const ResourceRequest&, const ResourceResponse&); 91 bool maybeLoadFallbackForMainError(const ResourceRequest&, const ResourceError&); 92 void mainResourceDataReceived(const char* data, int length, long long lengthReceived, bool allAtOnce); 93 void finishedLoadingMainResource(); 94 void failedLoadingMainResource(); 95 96 bool maybeLoadResource(ResourceLoader*, ResourceRequest&, const KURL& originalURL); 97 bool maybeLoadFallbackForRedirect(ResourceLoader*, ResourceRequest&, const ResourceResponse&); 98 bool maybeLoadFallbackForResponse(ResourceLoader*, const ResourceResponse&); 99 bool maybeLoadFallbackForError(ResourceLoader*, const ResourceError&); 100 101 bool maybeLoadSynchronously(ResourceRequest&, ResourceError&, ResourceResponse&, Vector<char>& data); 102 void maybeLoadFallbackSynchronously(const ResourceRequest&, ResourceError&, ResourceResponse&, Vector<char>& data); 103 104 bool canCacheInPageCache() const; 105 106 Status status() const; 107 bool update(); 108 bool swapCache(); 109 110 void setDOMApplicationCache(DOMApplicationCache* domApplicationCache); 111 void notifyEventListener(EventID id); 112 113 private: 114 bool isApplicationCacheEnabled(); documentLoader()115 DocumentLoader* documentLoader() { return m_documentLoader; } 116 117 DOMApplicationCache* m_domApplicationCache; 118 DocumentLoader* m_documentLoader; 119 120 #if PLATFORM(CHROMIUM) 121 friend class ApplicationCacheHostInternal; 122 OwnPtr<ApplicationCacheHostInternal> m_internal; 123 #else 124 friend class ApplicationCacheGroup; 125 friend class ApplicationCacheStorage; 126 127 bool scheduleLoadFallbackResourceFromApplicationCache(ResourceLoader*, ApplicationCache* = 0); 128 bool shouldLoadResourceFromApplicationCache(const ResourceRequest&, ApplicationCacheResource*&); 129 bool getApplicationCacheFallbackResource(const ResourceRequest&, ApplicationCacheResource*&, ApplicationCache* = 0); 130 void setCandidateApplicationCacheGroup(ApplicationCacheGroup* group); candidateApplicationCacheGroup()131 ApplicationCacheGroup* candidateApplicationCacheGroup() const { return m_candidateApplicationCacheGroup; } 132 void setApplicationCache(PassRefPtr<ApplicationCache> applicationCache); applicationCache()133 ApplicationCache* applicationCache() const { return m_applicationCache.get(); } mainResourceApplicationCache()134 ApplicationCache* mainResourceApplicationCache() const { return m_mainResourceApplicationCache.get(); } 135 136 137 // The application cache that the document loader is associated with (if any). 138 RefPtr<ApplicationCache> m_applicationCache; 139 140 // Before an application cache has finished loading, this will be the candidate application 141 // group that the document loader is associated with. 142 ApplicationCacheGroup* m_candidateApplicationCacheGroup; 143 144 // This is the application cache the main resource was loaded from (if any). 145 RefPtr<ApplicationCache> m_mainResourceApplicationCache; 146 #endif 147 }; 148 149 } // namespace WebCore 150 151 #endif // ENABLE(OFFLINE_WEB_APPLICATIONS) 152 #endif // ApplicationCacheHost_h 153