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 #include "platform/weborigin/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 
     41 namespace WebCore {
     42     class ApplicationCache;
     43     class DocumentLoader;
     44     class Frame;
     45     class ResourceLoader;
     46     class ResourceError;
     47     class ResourceRequest;
     48     class ResourceResponse;
     49     class SubstituteData;
     50     class ApplicationCacheHostInternal;
     51 
     52     class ApplicationCacheHost {
     53         WTF_MAKE_NONCOPYABLE(ApplicationCacheHost); WTF_MAKE_FAST_ALLOCATED;
     54     public:
     55         // The Status numeric values are specified in the HTML5 spec.
     56         enum Status {
     57             UNCACHED = 0,
     58             IDLE = 1,
     59             CHECKING = 2,
     60             DOWNLOADING = 3,
     61             UPDATEREADY = 4,
     62             OBSOLETE = 5
     63         };
     64 
     65         enum EventID {
     66             CHECKING_EVENT = 0,
     67             ERROR_EVENT,
     68             NOUPDATE_EVENT,
     69             DOWNLOADING_EVENT,
     70             PROGRESS_EVENT,
     71             UPDATEREADY_EVENT,
     72             CACHED_EVENT,
     73             OBSOLETE_EVENT  // Must remain the last value, this is used to size arrays.
     74         };
     75 
     76         struct CacheInfo {
     77             CacheInfo(const KURL& manifest, double creationTime, double updateTime, long long size)
     78                 : m_manifest(manifest)
     79                 , m_creationTime(creationTime)
     80                 , m_updateTime(updateTime)
     81                 , m_size(size) { }
     82             KURL m_manifest;
     83             double m_creationTime;
     84             double m_updateTime;
     85             long long m_size;
     86         };
     87 
     88         struct ResourceInfo {
     89             ResourceInfo(const KURL& resource, bool isMaster, bool isManifest, bool isFallback, bool isForeign, bool isExplicit, long long size)
     90                 : m_resource(resource)
     91                 , m_isMaster(isMaster)
     92                 , m_isManifest(isManifest)
     93                 , m_isFallback(isFallback)
     94                 , m_isForeign(isForeign)
     95                 , m_isExplicit(isExplicit)
     96                 , m_size(size) { }
     97             KURL m_resource;
     98             bool m_isMaster;
     99             bool m_isManifest;
    100             bool m_isFallback;
    101             bool m_isForeign;
    102             bool m_isExplicit;
    103             long long m_size;
    104         };
    105 
    106         typedef Vector<ResourceInfo> ResourceInfoList;
    107 
    108         ApplicationCacheHost(DocumentLoader*);
    109         ~ApplicationCacheHost();
    110 
    111         void selectCacheWithoutManifest();
    112         void selectCacheWithManifest(const KURL& manifestURL);
    113 
    114         void willStartLoadingMainResource(ResourceRequest&);
    115         void didReceiveResponseForMainResource(const ResourceResponse&);
    116         void mainResourceDataReceived(const char* data, int length);
    117         void finishedLoadingMainResource();
    118         void failedLoadingMainResource();
    119 
    120         void willStartLoadingResource(ResourceRequest&);
    121 
    122         Status status() const;
    123         bool update();
    124         bool swapCache();
    125         void abort();
    126 
    127         void setApplicationCache(ApplicationCache*);
    128         void notifyApplicationCache(EventID, int progressTotal, int progressDone);
    129 
    130         void stopDeferringEvents(); // Also raises the events that have been queued up.
    131 
    132         void fillResourceList(ResourceInfoList*);
    133         CacheInfo applicationCacheInfo();
    134 
    135     private:
    136         bool isApplicationCacheEnabled();
    137         DocumentLoader* documentLoader() const { return m_documentLoader; }
    138 
    139         struct DeferredEvent {
    140             EventID eventID;
    141             int progressTotal;
    142             int progressDone;
    143             DeferredEvent(EventID id, int total, int done) : eventID(id), progressTotal(total), progressDone(done) { }
    144         };
    145 
    146         ApplicationCache* m_domApplicationCache;
    147         DocumentLoader* m_documentLoader;
    148         bool m_defersEvents; // Events are deferred until after document onload.
    149         Vector<DeferredEvent> m_deferredEvents;
    150 
    151         void dispatchDOMEvent(EventID, int progressTotal, int progressDone);
    152 
    153         friend class ApplicationCacheHostInternal;
    154         OwnPtr<ApplicationCacheHostInternal> m_internal;
    155     };
    156 
    157 }  // namespace WebCore
    158 
    159 #endif  // ApplicationCacheHost_h
    160