initial import
[vuplus_webkit] / Source / WebKit / chromium / src / ApplicationCacheHost.cpp
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 #include "config.h"
32 #include "ApplicationCacheHost.h"
33
34 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
35
36 #include "ApplicationCacheHostInternal.h"
37 #include "DocumentLoader.h"
38 #include "DOMApplicationCache.h"
39 #include "Frame.h"
40 #include "InspectorApplicationCacheAgent.h"
41 #include "InspectorInstrumentation.h"
42 #include "Page.h"
43 #include "ProgressEvent.h"
44 #include "SecurityOrigin.h"
45 #include "Settings.h"
46 #include "WebFrameImpl.h"
47 #include "WebURL.h"
48 #include "WebURLError.h"
49 #include "WebURLResponse.h"
50 #include "WebVector.h"
51 #include "WrappedResourceRequest.h"
52 #include "WrappedResourceResponse.h"
53
54 using namespace WebKit;
55
56 namespace WebCore {
57
58 // We provide a custom implementation of this class that calls out to the
59 // embedding application instead of using WebCore's built in appcache system.
60 // This file replaces webcore/appcache/ApplicationCacheHost.cpp in our build.
61
62 ApplicationCacheHost::ApplicationCacheHost(DocumentLoader* documentLoader)
63     : m_domApplicationCache(0)
64     , m_documentLoader(documentLoader)
65     , m_defersEvents(true)
66 {
67     ASSERT(m_documentLoader);
68 }
69
70 ApplicationCacheHost::~ApplicationCacheHost()
71 {
72 }
73
74 void ApplicationCacheHost::maybeLoadMainResource(ResourceRequest& request, SubstituteData&)
75 {
76     // We defer creating the outer host object to avoid spurious creation/destruction
77     // around creating empty documents. At this point, we're initiating a main resource
78     // load for the document, so its for real.
79
80     if (!isApplicationCacheEnabled())
81         return;
82
83     m_internal = adoptPtr(new ApplicationCacheHostInternal(this));
84     if (m_internal->m_outerHost) {
85         WrappedResourceRequest wrapped(request);
86         m_internal->m_outerHost->willStartMainResourceRequest(wrapped, WebFrameImpl::fromFrame(m_documentLoader->frame()));
87     } else
88         m_internal.clear();
89
90     // NOTE: The semantics of this method, and others in this interface, are subtly different
91     // than the method names would suggest. For example, in this method never returns an appcached
92     // response in the SubstituteData out argument, instead we return the appcached response thru
93     // the usual resource loading pipeline.
94 }
95
96 void ApplicationCacheHost::selectCacheWithoutManifest()
97 {
98     if (m_internal)
99         m_internal->m_outerHost->selectCacheWithoutManifest();
100 }
101
102 void ApplicationCacheHost::selectCacheWithManifest(const KURL& manifestURL)
103 {
104     if (m_internal) {
105         if (!m_internal->m_outerHost->selectCacheWithManifest(manifestURL)) {
106             // It's a foreign entry, restart the current navigation from the top
107             // of the navigation algorithm. The navigation will not result in the
108             // same resource being loaded, because "foreign" entries are never picked
109             // during navigation.
110             // see WebCore::ApplicationCacheGroup::selectCache()
111             Frame* frame = m_documentLoader->frame();
112             frame->navigationScheduler()->scheduleLocationChange(frame->document()->securityOrigin(),
113                 frame->document()->url(), frame->loader()->referrer());
114         }
115     }
116 }
117
118 void ApplicationCacheHost::maybeLoadMainResourceForRedirect(ResourceRequest&, SubstituteData&)
119 {
120     // N/A to the chromium port
121 }
122
123 bool ApplicationCacheHost::maybeLoadFallbackForMainResponse(const ResourceRequest&, const ResourceResponse& response)
124 {
125     if (m_internal) {
126         WrappedResourceResponse wrapped(response);
127         m_internal->m_outerHost->didReceiveResponseForMainResource(wrapped);
128     }
129     return false;
130 }
131
132 bool ApplicationCacheHost::maybeLoadFallbackForMainError(const ResourceRequest&, const ResourceError& error)
133 {
134     // N/A to the chromium port
135     return false;
136 }
137
138 void ApplicationCacheHost::mainResourceDataReceived(const char* data, int length, long long, bool)
139 {
140     if (m_internal)
141         m_internal->m_outerHost->didReceiveDataForMainResource(data, length);
142 }
143
144 void ApplicationCacheHost::failedLoadingMainResource()
145 {
146     if (m_internal)
147         m_internal->m_outerHost->didFinishLoadingMainResource(false);
148 }
149
150 void ApplicationCacheHost::finishedLoadingMainResource()
151 {
152     if (m_internal)
153         m_internal->m_outerHost->didFinishLoadingMainResource(true);
154 }
155
156 bool ApplicationCacheHost::maybeLoadResource(ResourceLoader*, ResourceRequest& request, const KURL&)
157 {
158     // FIXME: look into the purpose of the unused KURL& originalURL parameter
159     if (m_internal) {
160         WrappedResourceRequest wrapped(request);
161         m_internal->m_outerHost->willStartSubResourceRequest(wrapped);
162     }
163     return false;
164 }
165
166 bool ApplicationCacheHost::maybeLoadFallbackForRedirect(ResourceLoader*, ResourceRequest&, const ResourceResponse&)
167 {
168     // N/A to the chromium port
169     return false;
170 }
171
172 bool ApplicationCacheHost::maybeLoadFallbackForResponse(ResourceLoader*, const ResourceResponse&)
173 {
174     // N/A to the chromium port
175     return false;
176 }
177
178 bool ApplicationCacheHost::maybeLoadFallbackForError(ResourceLoader*, const ResourceError&)
179 {
180     // N/A to the chromium port
181     return false;
182 }
183
184 bool ApplicationCacheHost::maybeLoadSynchronously(ResourceRequest& request, ResourceError&, ResourceResponse&, Vector<char>&)
185 {
186     if (m_internal) {
187         WrappedResourceRequest wrapped(request);
188         m_internal->m_outerHost->willStartSubResourceRequest(wrapped);
189     }
190     return false;
191 }
192
193 void ApplicationCacheHost::maybeLoadFallbackSynchronously(const ResourceRequest&, ResourceError&, ResourceResponse&, Vector<char>&)
194 {
195     // N/A to the chromium port
196 }
197
198 bool ApplicationCacheHost::canCacheInPageCache() const
199 {
200     // N/A to the chromium port which doesn't use the page cache.
201     return false;
202 }
203
204 void ApplicationCacheHost::setDOMApplicationCache(DOMApplicationCache* domApplicationCache)
205 {
206     ASSERT(!m_domApplicationCache || !domApplicationCache);
207     m_domApplicationCache = domApplicationCache;
208 }
209
210 void ApplicationCacheHost::notifyDOMApplicationCache(EventID id, int total, int done)
211 {
212 #if ENABLE(INSPECTOR)
213     if (id != PROGRESS_EVENT)
214         InspectorInstrumentation::updateApplicationCacheStatus(m_documentLoader->frame());
215 #endif
216
217     if (m_defersEvents) {
218         // Event dispatching is deferred until document.onload has fired.
219         m_deferredEvents.append(DeferredEvent(id, total, done));
220         return;
221     }
222     dispatchDOMEvent(id, total, done);
223 }
224
225 #if ENABLE(INSPECTOR)
226 ApplicationCacheHost::CacheInfo ApplicationCacheHost::applicationCacheInfo()
227 {
228     if (!m_internal)
229         return CacheInfo(KURL(), 0, 0, 0);
230
231     WebKit::WebApplicationCacheHost::CacheInfo webInfo;
232     m_internal->m_outerHost->getAssociatedCacheInfo(&webInfo);
233     return CacheInfo(webInfo.manifestURL, webInfo.creationTime, webInfo.updateTime, webInfo.totalSize);
234 }
235
236 void ApplicationCacheHost::fillResourceList(ResourceInfoList* resources)
237 {
238     if (!m_internal)
239         return;
240
241     WebKit::WebVector<WebKit::WebApplicationCacheHost::ResourceInfo> webResources;
242     m_internal->m_outerHost->getResourceList(&webResources);
243     for (size_t i = 0; i < webResources.size(); ++i) {
244         resources->append(ResourceInfo(
245             webResources[i].url, webResources[i].isMaster, webResources[i].isManifest, webResources[i].isFallback,
246             webResources[i].isForeign, webResources[i].isExplicit, webResources[i].size));
247     }
248 }
249 #endif
250
251 void ApplicationCacheHost::stopDeferringEvents()
252 {
253     RefPtr<DocumentLoader> protect(documentLoader());
254     for (unsigned i = 0; i < m_deferredEvents.size(); ++i) {
255         const DeferredEvent& deferred = m_deferredEvents[i];
256         dispatchDOMEvent(deferred.eventID, deferred.progressTotal, deferred.progressDone);
257     }
258     m_deferredEvents.clear();
259     m_defersEvents = false;
260 }
261
262 void ApplicationCacheHost::stopLoadingInFrame(Frame* frame)
263 {
264     // N/A to the chromium port
265 }
266
267 void ApplicationCacheHost::dispatchDOMEvent(EventID id, int total, int done)
268 {
269     if (m_domApplicationCache) {
270         const AtomicString& eventType = DOMApplicationCache::toEventType(id);
271         ExceptionCode ec = 0;
272         RefPtr<Event> event;
273         if (id == PROGRESS_EVENT)
274             event = ProgressEvent::create(eventType, true, done, total);
275         else
276             event = Event::create(eventType, false, false);
277         m_domApplicationCache->dispatchEvent(event, ec);
278         ASSERT(!ec);
279     }
280 }
281
282 ApplicationCacheHost::Status ApplicationCacheHost::status() const
283 {
284     return m_internal ? static_cast<Status>(m_internal->m_outerHost->status()) : UNCACHED;
285 }
286
287 bool ApplicationCacheHost::update()
288 {
289     return m_internal ? m_internal->m_outerHost->startUpdate() : false;
290 }
291
292 bool ApplicationCacheHost::swapCache()
293 {
294     return m_internal ? m_internal->m_outerHost->swapCache() : false;
295 }
296
297 bool ApplicationCacheHost::isApplicationCacheEnabled()
298 {
299     ASSERT(m_documentLoader->frame());
300     return m_documentLoader->frame()->settings()
301            && m_documentLoader->frame()->settings()->offlineWebApplicationCacheEnabled();
302 }
303
304 }  // namespace WebCore
305
306 #endif  // ENABLE(OFFLINE_WEB_APPLICATIONS)