Home | History | Annotate | Download | only in appcache
      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 "KURL.h"
     37 #include <wtf/Deque.h>
     38 #include <wtf/OwnPtr.h>
     39 #include <wtf/PassRefPtr.h>
     40 #include <wtf/RefPtr.h>
     41 #include <wtf/Vector.h>
     42 
     43 namespace WebCore {
     44     class DOMApplicationCache;
     45     class DocumentLoader;
     46     class Frame;
     47     class ResourceLoader;
     48     class ResourceError;
     49     class ResourceRequest;
     50     class ResourceResponse;
     51     class SubstituteData;
     52 #if PLATFORM(CHROMIUM)
     53     class ApplicationCacheHostInternal;
     54 #else
     55     class ApplicationCache;
     56     class ApplicationCacheGroup;
     57     class ApplicationCacheResource;
     58     class ApplicationCacheStorage;
     59 #endif
     60 
     61     class ApplicationCacheHost {
     62         WTF_MAKE_NONCOPYABLE(ApplicationCacheHost); WTF_MAKE_FAST_ALLOCATED;
     63     public:
     64         // The Status numeric values are specified in the HTML5 spec.
     65         enum Status {
     66             UNCACHED = 0,
     67             IDLE = 1,
     68             CHECKING = 2,
     69             DOWNLOADING = 3,
     70             UPDATEREADY = 4,
     71             OBSOLETE = 5
     72         };
     73 
     74         enum EventID {
     75             CHECKING_EVENT = 0,
     76             ERROR_EVENT,
     77             NOUPDATE_EVENT,
     78             DOWNLOADING_EVENT,
     79             PROGRESS_EVENT,
     80             UPDATEREADY_EVENT,
     81             CACHED_EVENT,
     82             OBSOLETE_EVENT  // Must remain the last value, this is used to size arrays.
     83         };
     84 
     85 #if ENABLE(INSPECTOR)
     86         struct CacheInfo {
     87             CacheInfo(const KURL& manifest, double creationTime, double updateTime, long long size)
     88                 : m_manifest(manifest)
     89                 , m_creationTime(creationTime)
     90                 , m_updateTime(updateTime)
     91                 , m_size(size) { }
     92             KURL m_manifest;
     93             double m_creationTime;
     94             double m_updateTime;
     95             long long m_size;
     96         };
     97 
     98         struct ResourceInfo {
     99             ResourceInfo(const KURL& resource, bool isMaster, bool isManifest, bool isFallback, bool isForeign, bool isExplicit, long long size)
    100                 : m_resource(resource)
    101                 , m_isMaster(isMaster)
    102                 , m_isManifest(isManifest)
    103                 , m_isFallback(isFallback)
    104                 , m_isForeign(isForeign)
    105                 , m_isExplicit(isExplicit)
    106                 , m_size(size) { }
    107             KURL m_resource;
    108             bool m_isMaster;
    109             bool m_isManifest;
    110             bool m_isFallback;
    111             bool m_isForeign;
    112             bool m_isExplicit;
    113             long long m_size;
    114         };
    115 
    116         typedef Vector<ResourceInfo> ResourceInfoList;
    117 #endif
    118 
    119         ApplicationCacheHost(DocumentLoader*);
    120         ~ApplicationCacheHost();
    121 
    122         void selectCacheWithoutManifest();
    123         void selectCacheWithManifest(const KURL& manifestURL);
    124 
    125         void maybeLoadMainResource(ResourceRequest&, SubstituteData&);
    126         void maybeLoadMainResourceForRedirect(ResourceRequest&, SubstituteData&);
    127         bool maybeLoadFallbackForMainResponse(const ResourceRequest&, const ResourceResponse&);
    128         bool maybeLoadFallbackForMainError(const ResourceRequest&, const ResourceError&);
    129         void mainResourceDataReceived(const char* data, int length, long long encodedDataLength, bool allAtOnce);
    130         void finishedLoadingMainResource();
    131         void failedLoadingMainResource();
    132 
    133         bool maybeLoadResource(ResourceLoader*, ResourceRequest&, const KURL& originalURL);
    134         bool maybeLoadFallbackForRedirect(ResourceLoader*, ResourceRequest&, const ResourceResponse&);
    135         bool maybeLoadFallbackForResponse(ResourceLoader*, const ResourceResponse&);
    136         bool maybeLoadFallbackForError(ResourceLoader*, const ResourceError&);
    137 
    138         bool maybeLoadSynchronously(ResourceRequest&, ResourceError&, ResourceResponse&, Vector<char>& data);
    139         void maybeLoadFallbackSynchronously(const ResourceRequest&, ResourceError&, ResourceResponse&, Vector<char>& data);
    140 
    141         bool canCacheInPageCache() const;
    142 
    143         Status status() const;
    144         bool update();
    145         bool swapCache();
    146 
    147         void setDOMApplicationCache(DOMApplicationCache*);
    148         void notifyDOMApplicationCache(EventID, int progressTotal, int progressDone);
    149 
    150         void stopLoadingInFrame(Frame*);
    151 
    152         void stopDeferringEvents(); // Also raises the events that have been queued up.
    153 
    154 #if ENABLE(INSPECTOR)
    155         void fillResourceList(ResourceInfoList*);
    156         CacheInfo applicationCacheInfo();
    157 #endif
    158 
    159 #if !PLATFORM(CHROMIUM)
    160         bool shouldLoadResourceFromApplicationCache(const ResourceRequest&, ApplicationCacheResource*&);
    161         bool getApplicationCacheFallbackResource(const ResourceRequest&, ApplicationCacheResource*&, ApplicationCache* = 0);
    162 #endif
    163 
    164     private:
    165         bool isApplicationCacheEnabled();
    166         DocumentLoader* documentLoader() const { return m_documentLoader; }
    167 
    168         struct DeferredEvent {
    169             EventID eventID;
    170             int progressTotal;
    171             int progressDone;
    172             DeferredEvent(EventID id, int total, int done) : eventID(id), progressTotal(total), progressDone(done) { }
    173         };
    174 
    175         DOMApplicationCache* m_domApplicationCache;
    176         DocumentLoader* m_documentLoader;
    177         bool m_defersEvents; // Events are deferred until after document onload.
    178         Vector<DeferredEvent> m_deferredEvents;
    179 
    180         void dispatchDOMEvent(EventID, int progressTotal, int progressDone);
    181 
    182 #if PLATFORM(CHROMIUM)
    183         friend class ApplicationCacheHostInternal;
    184         OwnPtr<ApplicationCacheHostInternal> m_internal;
    185 #else
    186         friend class ApplicationCacheGroup;
    187         friend class ApplicationCacheStorage;
    188 
    189         bool scheduleLoadFallbackResourceFromApplicationCache(ResourceLoader*, ApplicationCache* = 0);
    190         void setCandidateApplicationCacheGroup(ApplicationCacheGroup* group);
    191         ApplicationCacheGroup* candidateApplicationCacheGroup() const { return m_candidateApplicationCacheGroup; }
    192         void setApplicationCache(PassRefPtr<ApplicationCache> applicationCache);
    193         ApplicationCache* applicationCache() const { return m_applicationCache.get(); }
    194         ApplicationCache* mainResourceApplicationCache() const { return m_mainResourceApplicationCache.get(); }
    195 
    196 
    197         // The application cache that the document loader is associated with (if any).
    198         RefPtr<ApplicationCache> m_applicationCache;
    199 
    200         // Before an application cache has finished loading, this will be the candidate application
    201         // group that the document loader is associated with.
    202         ApplicationCacheGroup* m_candidateApplicationCacheGroup;
    203 
    204         // This is the application cache the main resource was loaded from (if any).
    205         RefPtr<ApplicationCache> m_mainResourceApplicationCache;
    206 #endif
    207     };
    208 
    209 }  // namespace WebCore
    210 
    211 #endif  // ENABLE(OFFLINE_WEB_APPLICATIONS)
    212 #endif  // ApplicationCacheHost_h
    213