| 1 | /* |
| 2 | * Copyright (C) 2008-2017 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 | #pragma once |
| 27 | |
| 28 | #include "ApplicationCacheResourceLoader.h" |
| 29 | #include "DOMApplicationCache.h" |
| 30 | #include <wtf/Noncopyable.h> |
| 31 | #include <wtf/HashMap.h> |
| 32 | #include <wtf/HashSet.h> |
| 33 | #include <wtf/URL.h> |
| 34 | #include <wtf/text/WTFString.h> |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | class ApplicationCache; |
| 39 | class ApplicationCacheResource; |
| 40 | class ApplicationCacheStorage; |
| 41 | class Document; |
| 42 | class DocumentLoader; |
| 43 | class Frame; |
| 44 | class SecurityOrigin; |
| 45 | |
| 46 | enum ApplicationCacheUpdateOption { |
| 47 | ApplicationCacheUpdateWithBrowsingContext, |
| 48 | ApplicationCacheUpdateWithoutBrowsingContext |
| 49 | }; |
| 50 | |
| 51 | class ApplicationCacheGroup { |
| 52 | WTF_MAKE_NONCOPYABLE(ApplicationCacheGroup); |
| 53 | WTF_MAKE_FAST_ALLOCATED; |
| 54 | public: |
| 55 | explicit ApplicationCacheGroup(Ref<ApplicationCacheStorage>&&, const URL& manifestURL); |
| 56 | virtual ~ApplicationCacheGroup(); |
| 57 | |
| 58 | enum UpdateStatus { Idle, Checking, Downloading }; |
| 59 | |
| 60 | static ApplicationCache* cacheForMainRequest(const ResourceRequest&, DocumentLoader*); |
| 61 | static ApplicationCache* fallbackCacheForMainRequest(const ResourceRequest&, DocumentLoader*); |
| 62 | |
| 63 | static void selectCache(Frame&, const URL& manifestURL); |
| 64 | static void selectCacheWithoutManifestURL(Frame&); |
| 65 | |
| 66 | ApplicationCacheStorage& storage() { return m_storage; } |
| 67 | const URL& manifestURL() const { return m_manifestURL; } |
| 68 | const SecurityOrigin& origin() const { return m_origin.get(); } |
| 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 | void abort(Frame&); |
| 80 | |
| 81 | bool cacheIsComplete(ApplicationCache& cache) { return m_caches.contains(&cache); } |
| 82 | |
| 83 | void stopLoadingInFrame(Frame&); |
| 84 | |
| 85 | ApplicationCache* newestCache() const { return m_newestCache.get(); } |
| 86 | void setNewestCache(Ref<ApplicationCache>&&); |
| 87 | |
| 88 | void makeObsolete(); |
| 89 | bool isObsolete() const { return m_isObsolete; } |
| 90 | |
| 91 | void finishedLoadingMainResource(DocumentLoader&); |
| 92 | void failedLoadingMainResource(DocumentLoader&); |
| 93 | |
| 94 | void disassociateDocumentLoader(DocumentLoader&); |
| 95 | |
| 96 | private: |
| 97 | static void postListenerTask(const AtomicString& eventType, const HashSet<DocumentLoader*>& set) { postListenerTask(eventType, 0, 0, set); } |
| 98 | static void postListenerTask(const AtomicString& eventType, DocumentLoader& loader) { postListenerTask(eventType, 0, 0, loader); } |
| 99 | static void postListenerTask(const AtomicString& eventType, int progressTotal, int progressDone, const HashSet<DocumentLoader*>&); |
| 100 | static void postListenerTask(const AtomicString& eventType, int progressTotal, int progressDone, DocumentLoader&); |
| 101 | |
| 102 | void scheduleReachedMaxAppCacheSizeCallback(); |
| 103 | |
| 104 | void didFinishLoadingManifest(); |
| 105 | void didFailLoadingManifest(ApplicationCacheResourceLoader::Error); |
| 106 | |
| 107 | void didFailLoadingEntry(ApplicationCacheResourceLoader::Error, const URL&, unsigned type); |
| 108 | void didFinishLoadingEntry(const URL&); |
| 109 | |
| 110 | void didReachMaxAppCacheSize(); |
| 111 | void didReachOriginQuota(int64_t totalSpaceNeeded); |
| 112 | |
| 113 | void startLoadingEntry(); |
| 114 | void deliverDelayedMainResources(); |
| 115 | void checkIfLoadIsComplete(); |
| 116 | void cacheUpdateFailed(); |
| 117 | void recalculateAvailableSpaceInQuota(); |
| 118 | void manifestNotFound(); |
| 119 | |
| 120 | void addEntry(const String&, unsigned type); |
| 121 | |
| 122 | void associateDocumentLoaderWithCache(DocumentLoader*, ApplicationCache*); |
| 123 | |
| 124 | void stopLoading(); |
| 125 | |
| 126 | ResourceRequest createRequest(URL&&, ApplicationCacheResource*); |
| 127 | |
| 128 | Ref<ApplicationCacheStorage> m_storage; |
| 129 | |
| 130 | URL m_manifestURL; |
| 131 | Ref<SecurityOrigin> m_origin; |
| 132 | UpdateStatus m_updateStatus { Idle }; |
| 133 | |
| 134 | // This is the newest complete cache in the group. |
| 135 | RefPtr<ApplicationCache> m_newestCache; |
| 136 | |
| 137 | // All complete caches in this cache group. |
| 138 | HashSet<ApplicationCache*> m_caches; |
| 139 | |
| 140 | // The cache being updated (if any). Note that cache updating does not immediately create a new |
| 141 | // ApplicationCache object, so this may be null even when update status is not Idle. |
| 142 | RefPtr<ApplicationCache> m_cacheBeingUpdated; |
| 143 | |
| 144 | // List of pending master entries, used during the update process to ensure that new master entries are cached. |
| 145 | HashSet<DocumentLoader*> m_pendingMasterResourceLoaders; |
| 146 | // How many of the above pending master entries have not yet finished downloading. |
| 147 | int m_downloadingPendingMasterResourceLoadersCount { 0 }; |
| 148 | |
| 149 | // These are all the document loaders that are associated with a cache in this group. |
| 150 | HashSet<DocumentLoader*> m_associatedDocumentLoaders; |
| 151 | |
| 152 | // The URLs and types of pending cache entries. |
| 153 | HashMap<String, unsigned> m_pendingEntries; |
| 154 | |
| 155 | // The total number of items to be processed to update the cache group and the number that have been done. |
| 156 | int m_progressTotal { 0 }; |
| 157 | int m_progressDone { 0 }; |
| 158 | |
| 159 | // Frame used for fetching resources when updating. |
| 160 | // 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. |
| 161 | Frame* m_frame { nullptr }; |
| 162 | |
| 163 | // An obsolete cache group is never stored, but the opposite is not true - storing may fail for multiple reasons, such as exceeding disk quota. |
| 164 | unsigned m_storageID { 0 }; |
| 165 | bool m_isObsolete { false }; |
| 166 | |
| 167 | // During update, this is used to handle asynchronously arriving results. |
| 168 | enum CompletionType { |
| 169 | None, |
| 170 | NoUpdate, |
| 171 | Failure, |
| 172 | Completed |
| 173 | }; |
| 174 | CompletionType m_completionType { None }; |
| 175 | |
| 176 | // This flag is set immediately after the ChromeClient::reachedMaxAppCacheSize() callback is invoked as a result of the storage layer failing to save a cache |
| 177 | // due to reaching the maximum size of the application cache database file. This flag is used by ApplicationCacheGroup::checkIfLoadIsComplete() to decide |
| 178 | // the course of action in case of this failure (i.e. call the ChromeClient callback or run the failure steps). |
| 179 | bool m_calledReachedMaxAppCacheSize { false }; |
| 180 | |
| 181 | RefPtr<ApplicationCacheResource> m_currentResource; |
| 182 | RefPtr<ApplicationCacheResourceLoader> m_entryLoader; |
| 183 | unsigned long m_currentResourceIdentifier; |
| 184 | |
| 185 | RefPtr<ApplicationCacheResource> m_manifestResource; |
| 186 | RefPtr<ApplicationCacheResourceLoader> m_manifestLoader; |
| 187 | |
| 188 | int64_t m_availableSpaceInQuota; |
| 189 | bool m_originQuotaExceededPreviously { false }; |
| 190 | |
| 191 | friend class ChromeClientCallbackTimer; |
| 192 | }; |
| 193 | |
| 194 | } // namespace WebCore |
| 195 | |