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