2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "ApplicationCacheHost.h"
34 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
36 #include "ApplicationCacheHostInternal.h"
37 #include "DocumentLoader.h"
38 #include "DOMApplicationCache.h"
40 #include "InspectorApplicationCacheAgent.h"
41 #include "InspectorInstrumentation.h"
43 #include "ProgressEvent.h"
44 #include "SecurityOrigin.h"
46 #include "WebFrameImpl.h"
48 #include "WebURLError.h"
49 #include "WebURLResponse.h"
50 #include "WebVector.h"
51 #include "WrappedResourceRequest.h"
52 #include "WrappedResourceResponse.h"
54 using namespace WebKit;
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.
62 ApplicationCacheHost::ApplicationCacheHost(DocumentLoader* documentLoader)
63 : m_domApplicationCache(0)
64 , m_documentLoader(documentLoader)
65 , m_defersEvents(true)
67 ASSERT(m_documentLoader);
70 ApplicationCacheHost::~ApplicationCacheHost()
74 void ApplicationCacheHost::maybeLoadMainResource(ResourceRequest& request, SubstituteData&)
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.
80 if (!isApplicationCacheEnabled())
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()));
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.
96 void ApplicationCacheHost::selectCacheWithoutManifest()
99 m_internal->m_outerHost->selectCacheWithoutManifest();
102 void ApplicationCacheHost::selectCacheWithManifest(const KURL& manifestURL)
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());
118 void ApplicationCacheHost::maybeLoadMainResourceForRedirect(ResourceRequest&, SubstituteData&)
120 // N/A to the chromium port
123 bool ApplicationCacheHost::maybeLoadFallbackForMainResponse(const ResourceRequest&, const ResourceResponse& response)
126 WrappedResourceResponse wrapped(response);
127 m_internal->m_outerHost->didReceiveResponseForMainResource(wrapped);
132 bool ApplicationCacheHost::maybeLoadFallbackForMainError(const ResourceRequest&, const ResourceError& error)
134 // N/A to the chromium port
138 void ApplicationCacheHost::mainResourceDataReceived(const char* data, int length, long long, bool)
141 m_internal->m_outerHost->didReceiveDataForMainResource(data, length);
144 void ApplicationCacheHost::failedLoadingMainResource()
147 m_internal->m_outerHost->didFinishLoadingMainResource(false);
150 void ApplicationCacheHost::finishedLoadingMainResource()
153 m_internal->m_outerHost->didFinishLoadingMainResource(true);
156 bool ApplicationCacheHost::maybeLoadResource(ResourceLoader*, ResourceRequest& request, const KURL&)
158 // FIXME: look into the purpose of the unused KURL& originalURL parameter
160 WrappedResourceRequest wrapped(request);
161 m_internal->m_outerHost->willStartSubResourceRequest(wrapped);
166 bool ApplicationCacheHost::maybeLoadFallbackForRedirect(ResourceLoader*, ResourceRequest&, const ResourceResponse&)
168 // N/A to the chromium port
172 bool ApplicationCacheHost::maybeLoadFallbackForResponse(ResourceLoader*, const ResourceResponse&)
174 // N/A to the chromium port
178 bool ApplicationCacheHost::maybeLoadFallbackForError(ResourceLoader*, const ResourceError&)
180 // N/A to the chromium port
184 bool ApplicationCacheHost::maybeLoadSynchronously(ResourceRequest& request, ResourceError&, ResourceResponse&, Vector<char>&)
187 WrappedResourceRequest wrapped(request);
188 m_internal->m_outerHost->willStartSubResourceRequest(wrapped);
193 void ApplicationCacheHost::maybeLoadFallbackSynchronously(const ResourceRequest&, ResourceError&, ResourceResponse&, Vector<char>&)
195 // N/A to the chromium port
198 bool ApplicationCacheHost::canCacheInPageCache() const
200 // N/A to the chromium port which doesn't use the page cache.
204 void ApplicationCacheHost::setDOMApplicationCache(DOMApplicationCache* domApplicationCache)
206 ASSERT(!m_domApplicationCache || !domApplicationCache);
207 m_domApplicationCache = domApplicationCache;
210 void ApplicationCacheHost::notifyDOMApplicationCache(EventID id, int total, int done)
212 #if ENABLE(INSPECTOR)
213 if (id != PROGRESS_EVENT)
214 InspectorInstrumentation::updateApplicationCacheStatus(m_documentLoader->frame());
217 if (m_defersEvents) {
218 // Event dispatching is deferred until document.onload has fired.
219 m_deferredEvents.append(DeferredEvent(id, total, done));
222 dispatchDOMEvent(id, total, done);
225 #if ENABLE(INSPECTOR)
226 ApplicationCacheHost::CacheInfo ApplicationCacheHost::applicationCacheInfo()
229 return CacheInfo(KURL(), 0, 0, 0);
231 WebKit::WebApplicationCacheHost::CacheInfo webInfo;
232 m_internal->m_outerHost->getAssociatedCacheInfo(&webInfo);
233 return CacheInfo(webInfo.manifestURL, webInfo.creationTime, webInfo.updateTime, webInfo.totalSize);
236 void ApplicationCacheHost::fillResourceList(ResourceInfoList* resources)
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));
251 void ApplicationCacheHost::stopDeferringEvents()
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);
258 m_deferredEvents.clear();
259 m_defersEvents = false;
262 void ApplicationCacheHost::stopLoadingInFrame(Frame* frame)
264 // N/A to the chromium port
267 void ApplicationCacheHost::dispatchDOMEvent(EventID id, int total, int done)
269 if (m_domApplicationCache) {
270 const AtomicString& eventType = DOMApplicationCache::toEventType(id);
271 ExceptionCode ec = 0;
273 if (id == PROGRESS_EVENT)
274 event = ProgressEvent::create(eventType, true, done, total);
276 event = Event::create(eventType, false, false);
277 m_domApplicationCache->dispatchEvent(event, ec);
282 ApplicationCacheHost::Status ApplicationCacheHost::status() const
284 return m_internal ? static_cast<Status>(m_internal->m_outerHost->status()) : UNCACHED;
287 bool ApplicationCacheHost::update()
289 return m_internal ? m_internal->m_outerHost->startUpdate() : false;
292 bool ApplicationCacheHost::swapCache()
294 return m_internal ? m_internal->m_outerHost->swapCache() : false;
297 bool ApplicationCacheHost::isApplicationCacheEnabled()
299 ASSERT(m_documentLoader->frame());
300 return m_documentLoader->frame()->settings()
301 && m_documentLoader->frame()->settings()->offlineWebApplicationCacheEnabled();
304 } // namespace WebCore
306 #endif // ENABLE(OFFLINE_WEB_APPLICATIONS)