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