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/heap/Handle.h"
35#include "platform/weborigin/KURL.h"
36#include "public/platform/WebApplicationCacheHostClient.h"
37#include "wtf/OwnPtr.h"
38#include "wtf/Vector.h"
39
40namespace blink {
41    class ApplicationCache;
42    class DocumentLoader;
43    class ResourceRequest;
44    class ResourceResponse;
45
46    class ApplicationCacheHost FINAL : public NoBaseWillBeGarbageCollectedFinalized<ApplicationCacheHost>, public WebApplicationCacheHostClient {
47        WTF_MAKE_NONCOPYABLE(ApplicationCacheHost);
48    public:
49        static PassOwnPtrWillBeRawPtr<ApplicationCacheHost> create(DocumentLoader* loader)
50        {
51            return adoptPtrWillBeNoop(new ApplicationCacheHost(loader));
52        }
53        virtual ~ApplicationCacheHost();
54
55        void dispose();
56
57        // The Status numeric values are specified in the HTML5 spec.
58        enum Status {
59            UNCACHED = 0,
60            IDLE = 1,
61            CHECKING = 2,
62            DOWNLOADING = 3,
63            UPDATEREADY = 4,
64            OBSOLETE = 5
65        };
66
67        enum EventID {
68            CHECKING_EVENT = 0,
69            ERROR_EVENT,
70            NOUPDATE_EVENT,
71            DOWNLOADING_EVENT,
72            PROGRESS_EVENT,
73            UPDATEREADY_EVENT,
74            CACHED_EVENT,
75            OBSOLETE_EVENT  // Must remain the last value, this is used to size arrays.
76        };
77
78        struct CacheInfo {
79            CacheInfo(const KURL& manifest, double creationTime, double updateTime, long long size)
80                : m_manifest(manifest)
81                , m_creationTime(creationTime)
82                , m_updateTime(updateTime)
83                , m_size(size) { }
84            KURL m_manifest;
85            double m_creationTime;
86            double m_updateTime;
87            long long m_size;
88        };
89
90        struct ResourceInfo {
91            ResourceInfo(const KURL& resource, bool isMaster, bool isManifest, bool isFallback, bool isForeign, bool isExplicit, long long size)
92                : m_resource(resource)
93                , m_isMaster(isMaster)
94                , m_isManifest(isManifest)
95                , m_isFallback(isFallback)
96                , m_isForeign(isForeign)
97                , m_isExplicit(isExplicit)
98                , m_size(size) { }
99            KURL m_resource;
100            bool m_isMaster;
101            bool m_isManifest;
102            bool m_isFallback;
103            bool m_isForeign;
104            bool m_isExplicit;
105            long long m_size;
106        };
107
108        typedef Vector<ResourceInfo> ResourceInfoList;
109
110        void selectCacheWithoutManifest();
111        void selectCacheWithManifest(const KURL& manifestURL);
112
113        void willStartLoadingMainResource(ResourceRequest&);
114        void didReceiveResponseForMainResource(const ResourceResponse&);
115        void mainResourceDataReceived(const char* data, int length);
116        void finishedLoadingMainResource();
117        void failedLoadingMainResource();
118
119        void willStartLoadingResource(ResourceRequest&);
120
121        Status status() const;
122        bool update();
123        bool swapCache();
124        void abort();
125
126        void setApplicationCache(ApplicationCache*);
127        void notifyApplicationCache(EventID, int progressTotal, int progressDone, WebApplicationCacheHost::ErrorReason, const String& errorURL, int errorStatus, const String& errorMessage);
128
129        void stopDeferringEvents(); // Also raises the events that have been queued up.
130
131        void fillResourceList(ResourceInfoList*);
132        CacheInfo applicationCacheInfo();
133
134        void trace(Visitor*);
135
136    private:
137        explicit ApplicationCacheHost(DocumentLoader*);
138
139        // WebApplicationCacheHostClient implementation
140        virtual void didChangeCacheAssociation() OVERRIDE FINAL;
141        virtual void notifyEventListener(WebApplicationCacheHost::EventID) OVERRIDE FINAL;
142        virtual void notifyProgressEventListener(const WebURL&, int progressTotal, int progressDone) OVERRIDE FINAL;
143        virtual void notifyErrorEventListener(WebApplicationCacheHost::ErrorReason, const WebURL&, int status, const WebString& message) OVERRIDE FINAL;
144
145        bool isApplicationCacheEnabled();
146        DocumentLoader* documentLoader() const { return m_documentLoader; }
147
148        struct DeferredEvent {
149            EventID eventID;
150            int progressTotal;
151            int progressDone;
152            WebApplicationCacheHost::ErrorReason errorReason;
153            String errorURL;
154            int errorStatus;
155            String errorMessage;
156            DeferredEvent(EventID id, int progressTotal, int progressDone, WebApplicationCacheHost::ErrorReason errorReason, const String& errorURL, int errorStatus, const String& errorMessage)
157                : eventID(id)
158                , progressTotal(progressTotal)
159                , progressDone(progressDone)
160                , errorReason(errorReason)
161                , errorURL(errorURL)
162                , errorStatus(errorStatus)
163                , errorMessage(errorMessage)
164            {
165            }
166        };
167
168        RawPtrWillBeWeakMember<ApplicationCache> m_domApplicationCache;
169        DocumentLoader* m_documentLoader;
170        bool m_defersEvents; // Events are deferred until after document onload.
171        Vector<DeferredEvent> m_deferredEvents;
172
173        void dispatchDOMEvent(EventID, int progressTotal, int progressDone, WebApplicationCacheHost::ErrorReason, const String& errorURL, int errorStatus, const String& errorMessage);
174
175        OwnPtr<WebApplicationCacheHost> m_host;
176    };
177
178}  // namespace blink
179
180#endif  // ApplicationCacheHost_h
181