Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / loader / appcache / ApplicationCacheHost.h
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 "public/platform/WebApplicationCacheHostClient.h"
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 blink {
43     class ApplicationCache;
44     class DocumentLoader;
45     class LocalFrame;
46     class ResourceLoader;
47     class ResourceError;
48     class ResourceRequest;
49     class ResourceResponse;
50     class SubstituteData;
51
52     class ApplicationCacheHost : public blink::WebApplicationCacheHostClient {
53         WTF_MAKE_NONCOPYABLE(ApplicationCacheHost);
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         explicit ApplicationCacheHost(DocumentLoader*);
109         virtual ~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, blink::WebApplicationCacheHost::ErrorReason, const String& errorURL, int errorStatus, const String& errorMessage);
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         // WebApplicationCacheHostClient implementation
137         virtual void didChangeCacheAssociation() OVERRIDE FINAL;
138         virtual void notifyEventListener(blink::WebApplicationCacheHost::EventID) OVERRIDE FINAL;
139         virtual void notifyProgressEventListener(const blink::WebURL&, int progressTotal, int progressDone) OVERRIDE FINAL;
140         virtual void notifyErrorEventListener(blink::WebApplicationCacheHost::ErrorReason, const blink::WebURL&, int status, const blink::WebString& message) OVERRIDE FINAL;
141
142         bool isApplicationCacheEnabled();
143         DocumentLoader* documentLoader() const { return m_documentLoader; }
144
145         struct DeferredEvent {
146             EventID eventID;
147             int progressTotal;
148             int progressDone;
149             blink::WebApplicationCacheHost::ErrorReason errorReason;
150             String errorURL;
151             int errorStatus;
152             String errorMessage;
153             DeferredEvent(EventID id, int progressTotal, int progressDone, blink::WebApplicationCacheHost::ErrorReason errorReason, const String& errorURL, int errorStatus, const String& errorMessage)
154                 : eventID(id)
155                 , progressTotal(progressTotal)
156                 , progressDone(progressDone)
157                 , errorReason(errorReason)
158                 , errorURL(errorURL)
159                 , errorStatus(errorStatus)
160                 , errorMessage(errorMessage)
161             {
162             }
163         };
164
165         ApplicationCache* m_domApplicationCache;
166         DocumentLoader* m_documentLoader;
167         bool m_defersEvents; // Events are deferred until after document onload.
168         Vector<DeferredEvent> m_deferredEvents;
169
170         void dispatchDOMEvent(EventID, int progressTotal, int progressDone, blink::WebApplicationCacheHost::ErrorReason, const String& errorURL, int errorStatus, const String& errorMessage);
171
172         OwnPtr<blink::WebApplicationCacheHost> m_host;
173     };
174
175 }  // namespace blink
176
177 #endif  // ApplicationCacheHost_h