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 #include "ApplicationCacheHostInternal.h"
35 #include "DocumentLoader.h"
36 #include "DOMApplicationCache.h"
38 #include "InspectorApplicationCacheAgent.h"
39 #include "InspectorInstrumentation.h"
41 #include "ProgressEvent.h"
42 #include "SecurityOrigin.h"
44 #include "WebFrameImpl.h"
46 #include "WebURLError.h"
47 #include "WebURLResponse.h"
48 #include "WebVector.h"
49 #include "WrappedResourceRequest.h"
50 #include "WrappedResourceResponse.h"
52 using namespace WebKit;
56 // We provide a custom implementation of this class that calls out to the
57 // embedding application instead of using WebCore's built in appcache system.
58 // This file replaces webcore/appcache/ApplicationCacheHost.cpp in our build.
60 ApplicationCacheHost::ApplicationCacheHost(DocumentLoader* documentLoader)
61 : m_domApplicationCache(0)
62 , m_documentLoader(documentLoader)
63 , m_defersEvents(true)
65 ASSERT(m_documentLoader);
68 ApplicationCacheHost::~ApplicationCacheHost()
72 void ApplicationCacheHost::maybeLoadMainResource(ResourceRequest& request, SubstituteData&)
74 // We defer creating the outer host object to avoid spurious creation/destruction
75 // around creating empty documents. At this point, we're initiating a main resource
76 // load for the document, so its for real.
78 if (!isApplicationCacheEnabled())
81 m_internal = adoptPtr(new ApplicationCacheHostInternal(this));
82 if (m_internal->m_outerHost) {
83 WrappedResourceRequest wrapped(request);
84 m_internal->m_outerHost->willStartMainResourceRequest(wrapped, WebFrameImpl::fromFrame(m_documentLoader->frame()));
88 // NOTE: The semantics of this method, and others in this interface, are subtly different
89 // than the method names would suggest. For example, in this method never returns an appcached
90 // response in the SubstituteData out argument, instead we return the appcached response thru
91 // the usual resource loading pipeline.
94 void ApplicationCacheHost::selectCacheWithoutManifest()
97 m_internal->m_outerHost->selectCacheWithoutManifest();
100 void ApplicationCacheHost::selectCacheWithManifest(const KURL& manifestURL)
103 if (!m_internal->m_outerHost->selectCacheWithManifest(manifestURL)) {
104 // It's a foreign entry, restart the current navigation from the top
105 // of the navigation algorithm. The navigation will not result in the
106 // same resource being loaded, because "foreign" entries are never picked
107 // during navigation.
108 // see WebCore::ApplicationCacheGroup::selectCache()
109 Frame* frame = m_documentLoader->frame();
110 frame->navigationScheduler()->scheduleLocationChange(frame->document()->securityOrigin(),
111 frame->document()->url(), frame->loader()->referrer());
116 void ApplicationCacheHost::maybeLoadMainResourceForRedirect(ResourceRequest&, SubstituteData&)
118 // N/A to the chromium port
121 bool ApplicationCacheHost::maybeLoadFallbackForMainResponse(const ResourceRequest&, const ResourceResponse& response)
124 WrappedResourceResponse wrapped(response);
125 m_internal->m_outerHost->didReceiveResponseForMainResource(wrapped);
130 bool ApplicationCacheHost::maybeLoadFallbackForMainError(const ResourceRequest&, const ResourceError& error)
132 // N/A to the chromium port
136 void ApplicationCacheHost::mainResourceDataReceived(const char* data, int length, long long, bool)
139 m_internal->m_outerHost->didReceiveDataForMainResource(data, length);
142 void ApplicationCacheHost::failedLoadingMainResource()
145 m_internal->m_outerHost->didFinishLoadingMainResource(false);
148 void ApplicationCacheHost::finishedLoadingMainResource()
151 m_internal->m_outerHost->didFinishLoadingMainResource(true);
154 bool ApplicationCacheHost::maybeLoadResource(ResourceLoader*, ResourceRequest& request, const KURL&)
156 // FIXME: look into the purpose of the unused KURL& originalURL parameter
158 WrappedResourceRequest wrapped(request);
159 m_internal->m_outerHost->willStartSubResourceRequest(wrapped);
164 bool ApplicationCacheHost::maybeLoadFallbackForRedirect(ResourceLoader*, ResourceRequest&, const ResourceResponse&)
166 // N/A to the chromium port
170 bool ApplicationCacheHost::maybeLoadFallbackForResponse(ResourceLoader*, const ResourceResponse&)
172 // N/A to the chromium port
176 bool ApplicationCacheHost::maybeLoadFallbackForError(ResourceLoader*, const ResourceError&)
178 // N/A to the chromium port
182 bool ApplicationCacheHost::maybeLoadSynchronously(ResourceRequest& request, ResourceError&, ResourceResponse&, Vector<char>&)
185 WrappedResourceRequest wrapped(request);
186 m_internal->m_outerHost->willStartSubResourceRequest(wrapped);
191 void ApplicationCacheHost::maybeLoadFallbackSynchronously(const ResourceRequest&, ResourceError&, ResourceResponse&, Vector<char>&)
193 // N/A to the chromium port
196 bool ApplicationCacheHost::canCacheInPageCache() const
198 // N/A to the chromium port which doesn't use the page cache.
202 void ApplicationCacheHost::setDOMApplicationCache(DOMApplicationCache* domApplicationCache)
204 ASSERT(!m_domApplicationCache || !domApplicationCache);
205 m_domApplicationCache = domApplicationCache;
208 void ApplicationCacheHost::notifyDOMApplicationCache(EventID id, int total, int done)
210 #if ENABLE(INSPECTOR)
211 if (id != PROGRESS_EVENT)
212 InspectorInstrumentation::updateApplicationCacheStatus(m_documentLoader->frame());
215 if (m_defersEvents) {
216 // Event dispatching is deferred until document.onload has fired.
217 m_deferredEvents.append(DeferredEvent(id, total, done));
220 dispatchDOMEvent(id, total, done);
223 #if ENABLE(INSPECTOR)
224 ApplicationCacheHost::CacheInfo ApplicationCacheHost::applicationCacheInfo()
227 return CacheInfo(KURL(), 0, 0, 0);
229 WebKit::WebApplicationCacheHost::CacheInfo webInfo;
230 m_internal->m_outerHost->getAssociatedCacheInfo(&webInfo);
231 return CacheInfo(webInfo.manifestURL, webInfo.creationTime, webInfo.updateTime, webInfo.totalSize);
234 void ApplicationCacheHost::fillResourceList(ResourceInfoList* resources)
239 WebKit::WebVector<WebKit::WebApplicationCacheHost::ResourceInfo> webResources;
240 m_internal->m_outerHost->getResourceList(&webResources);
241 for (size_t i = 0; i < webResources.size(); ++i) {
242 resources->append(ResourceInfo(
243 webResources[i].url, webResources[i].isMaster, webResources[i].isManifest, webResources[i].isFallback,
244 webResources[i].isForeign, webResources[i].isExplicit, webResources[i].size));
249 void ApplicationCacheHost::stopDeferringEvents()
251 RefPtr<DocumentLoader> protect(documentLoader());
252 for (unsigned i = 0; i < m_deferredEvents.size(); ++i) {
253 const DeferredEvent& deferred = m_deferredEvents[i];
254 dispatchDOMEvent(deferred.eventID, deferred.progressTotal, deferred.progressDone);
256 m_deferredEvents.clear();
257 m_defersEvents = false;
260 void ApplicationCacheHost::stopLoadingInFrame(Frame* frame)
262 // N/A to the chromium port
265 void ApplicationCacheHost::dispatchDOMEvent(EventID id, int total, int done)
267 if (m_domApplicationCache) {
268 const AtomicString& eventType = DOMApplicationCache::toEventType(id);
269 ExceptionCode ec = 0;
271 if (id == PROGRESS_EVENT)
272 event = ProgressEvent::create(eventType, true, done, total);
274 event = Event::create(eventType, false, false);
275 m_domApplicationCache->dispatchEvent(event, ec);
280 ApplicationCacheHost::Status ApplicationCacheHost::status() const
282 return m_internal ? static_cast<Status>(m_internal->m_outerHost->status()) : UNCACHED;
285 bool ApplicationCacheHost::update()
287 return m_internal ? m_internal->m_outerHost->startUpdate() : false;
290 bool ApplicationCacheHost::swapCache()
292 bool success = m_internal ? m_internal->m_outerHost->swapCache() : false;
294 InspectorInstrumentation::updateApplicationCacheStatus(m_documentLoader->frame());
298 bool ApplicationCacheHost::isApplicationCacheEnabled()
300 ASSERT(m_documentLoader->frame());
301 return m_documentLoader->frame()->settings()
302 && m_documentLoader->frame()->settings()->offlineWebApplicationCacheEnabled();
305 } // namespace WebCore