2 * Copyright (C) 2008, 2009 Apple 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
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "ApplicationCacheHost.h"
29 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
31 #include "ApplicationCache.h"
32 #include "ApplicationCacheGroup.h"
33 #include "ApplicationCacheResource.h"
34 #include "DocumentLoader.h"
35 #include "DOMApplicationCache.h"
37 #include "FrameLoader.h"
38 #include "FrameLoaderClient.h"
39 #include "MainResourceLoader.h"
40 #include "ProgressEvent.h"
41 #include "ResourceLoader.h"
42 #include "ResourceRequest.h"
47 ApplicationCacheHost::ApplicationCacheHost(DocumentLoader* documentLoader)
48 : m_domApplicationCache(0)
49 , m_documentLoader(documentLoader)
50 , m_defersEvents(true)
51 , m_candidateApplicationCacheGroup(0)
53 ASSERT(m_documentLoader);
56 ApplicationCacheHost::~ApplicationCacheHost()
58 ASSERT(!m_applicationCache || !m_candidateApplicationCacheGroup || m_applicationCache->group() == m_candidateApplicationCacheGroup);
60 if (m_applicationCache)
61 m_applicationCache->group()->disassociateDocumentLoader(m_documentLoader);
62 else if (m_candidateApplicationCacheGroup)
63 m_candidateApplicationCacheGroup->disassociateDocumentLoader(m_documentLoader);
66 void ApplicationCacheHost::selectCacheWithoutManifest()
68 ApplicationCacheGroup::selectCacheWithoutManifestURL(m_documentLoader->frame());
71 void ApplicationCacheHost::selectCacheWithManifest(const KURL& manifestURL)
73 ApplicationCacheGroup::selectCache(m_documentLoader->frame(), manifestURL);
76 void ApplicationCacheHost::maybeLoadMainResource(ResourceRequest& request, SubstituteData& substituteData)
78 // Check if this request should be loaded from the application cache
79 if (!substituteData.isValid() && isApplicationCacheEnabled()) {
80 ASSERT(!m_mainResourceApplicationCache);
82 m_mainResourceApplicationCache = ApplicationCacheGroup::cacheForMainRequest(request, m_documentLoader);
84 if (m_mainResourceApplicationCache) {
85 // Get the resource from the application cache. By definition, cacheForMainRequest() returns a cache that contains the resource.
86 ApplicationCacheResource* resource = m_mainResourceApplicationCache->resourceForRequest(request);
87 substituteData = SubstituteData(resource->data(),
88 resource->response().mimeType(),
89 resource->response().textEncodingName(), KURL());
94 void ApplicationCacheHost::maybeLoadMainResourceForRedirect(ResourceRequest& request, SubstituteData& substituteData)
96 ASSERT(status() == UNCACHED);
97 maybeLoadMainResource(request, substituteData);
100 bool ApplicationCacheHost::maybeLoadFallbackForMainResponse(const ResourceRequest& request, const ResourceResponse& r)
102 if (r.httpStatusCode() / 100 == 4 || r.httpStatusCode() / 100 == 5) {
103 ASSERT(!m_mainResourceApplicationCache);
104 if (isApplicationCacheEnabled()) {
105 m_mainResourceApplicationCache = ApplicationCacheGroup::fallbackCacheForMainRequest(request, documentLoader());
107 if (scheduleLoadFallbackResourceFromApplicationCache(documentLoader()->mainResourceLoader(), m_mainResourceApplicationCache.get()))
114 bool ApplicationCacheHost::maybeLoadFallbackForMainError(const ResourceRequest& request, const ResourceError& error)
116 if (!error.isCancellation()) {
117 ASSERT(!m_mainResourceApplicationCache);
118 if (isApplicationCacheEnabled()) {
119 m_mainResourceApplicationCache = ApplicationCacheGroup::fallbackCacheForMainRequest(request, m_documentLoader);
121 if (scheduleLoadFallbackResourceFromApplicationCache(documentLoader()->mainResourceLoader(), m_mainResourceApplicationCache.get()))
128 void ApplicationCacheHost::mainResourceDataReceived(const char*, int, long long, bool)
130 // This method is here to facilitate alternate implemetations of this interface by the host browser.
133 void ApplicationCacheHost::failedLoadingMainResource()
135 ApplicationCacheGroup* group = m_candidateApplicationCacheGroup;
136 if (!group && m_applicationCache) {
137 if (mainResourceApplicationCache()) {
138 // Even when the main resource is being loaded from an application cache, loading can fail if aborted.
141 group = m_applicationCache->group();
145 group->failedLoadingMainResource(m_documentLoader);
148 void ApplicationCacheHost::finishedLoadingMainResource()
150 ApplicationCacheGroup* group = candidateApplicationCacheGroup();
151 if (!group && applicationCache() && !mainResourceApplicationCache())
152 group = applicationCache()->group();
155 group->finishedLoadingMainResource(m_documentLoader);
158 bool ApplicationCacheHost::maybeLoadResource(ResourceLoader* loader, ResourceRequest& request, const KURL& originalURL)
160 if (!isApplicationCacheEnabled())
163 if (request.url() != originalURL)
166 ApplicationCacheResource* resource;
167 if (!shouldLoadResourceFromApplicationCache(request, resource))
170 m_documentLoader->m_pendingSubstituteResources.set(loader, resource);
171 m_documentLoader->deliverSubstituteResourcesAfterDelay();
176 bool ApplicationCacheHost::maybeLoadFallbackForRedirect(ResourceLoader* resourceLoader, ResourceRequest& request, const ResourceResponse& redirectResponse)
178 if (!redirectResponse.isNull() && !protocolHostAndPortAreEqual(request.url(), redirectResponse.url()))
179 if (scheduleLoadFallbackResourceFromApplicationCache(resourceLoader))
184 bool ApplicationCacheHost::maybeLoadFallbackForResponse(ResourceLoader* resourceLoader, const ResourceResponse& response)
186 if (response.httpStatusCode() / 100 == 4 || response.httpStatusCode() / 100 == 5)
187 if (scheduleLoadFallbackResourceFromApplicationCache(resourceLoader))
192 bool ApplicationCacheHost::maybeLoadFallbackForError(ResourceLoader* resourceLoader, const ResourceError& error)
194 if (!error.isCancellation())
195 if (scheduleLoadFallbackResourceFromApplicationCache(resourceLoader))
200 bool ApplicationCacheHost::maybeLoadSynchronously(ResourceRequest& request, ResourceError& error, ResourceResponse& response, Vector<char>& data)
202 ApplicationCacheResource* resource;
203 if (shouldLoadResourceFromApplicationCache(request, resource)) {
205 response = resource->response();
206 data.append(resource->data()->data(), resource->data()->size());
208 error = documentLoader()->frameLoader()->client()->cannotShowURLError(request);
215 void ApplicationCacheHost::maybeLoadFallbackSynchronously(const ResourceRequest& request, ResourceError& error, ResourceResponse& response, Vector<char>& data)
217 // If normal loading results in a redirect to a resource with another origin (indicative of a captive portal), or a 4xx or 5xx status code or equivalent,
218 // or if there were network errors (but not if the user canceled the download), then instead get, from the cache, the resource of the fallback entry
219 // corresponding to the matched namespace.
220 if ((!error.isNull() && !error.isCancellation())
221 || response.httpStatusCode() / 100 == 4 || response.httpStatusCode() / 100 == 5
222 || !protocolHostAndPortAreEqual(request.url(), response.url())) {
223 ApplicationCacheResource* resource;
224 if (getApplicationCacheFallbackResource(request, resource)) {
225 response = resource->response();
227 data.append(resource->data()->data(), resource->data()->size());
232 bool ApplicationCacheHost::canCacheInPageCache() const
234 return !applicationCache() && !candidateApplicationCacheGroup();
237 void ApplicationCacheHost::setDOMApplicationCache(DOMApplicationCache* domApplicationCache)
239 ASSERT(!m_domApplicationCache || !domApplicationCache);
240 m_domApplicationCache = domApplicationCache;
243 void ApplicationCacheHost::notifyDOMApplicationCache(EventID id, int total, int done)
245 if (m_defersEvents) {
246 // Event dispatching is deferred until document.onload has fired.
247 m_deferredEvents.append(DeferredEvent(id, total, done));
250 dispatchDOMEvent(id, total, done);
253 void ApplicationCacheHost::stopLoadingInFrame(Frame* frame)
255 ASSERT(!m_applicationCache || !m_candidateApplicationCacheGroup || m_applicationCache->group() == m_candidateApplicationCacheGroup);
257 if (m_candidateApplicationCacheGroup)
258 m_candidateApplicationCacheGroup->stopLoadingInFrame(frame);
259 else if (m_applicationCache)
260 m_applicationCache->group()->stopLoadingInFrame(frame);
263 void ApplicationCacheHost::stopDeferringEvents()
265 RefPtr<DocumentLoader> protect(documentLoader());
266 for (unsigned i = 0; i < m_deferredEvents.size(); ++i) {
267 const DeferredEvent& deferred = m_deferredEvents[i];
268 dispatchDOMEvent(deferred.eventID, deferred.progressTotal, deferred.progressDone);
270 m_deferredEvents.clear();
271 m_defersEvents = false;
274 #if ENABLE(INSPECTOR)
275 void ApplicationCacheHost::fillResourceList(ResourceInfoList* resources)
277 ApplicationCache* cache = applicationCache();
278 if (!cache || !cache->isComplete())
281 ApplicationCache::ResourceMap::const_iterator end = cache->end();
282 for (ApplicationCache::ResourceMap::const_iterator it = cache->begin(); it != end; ++it) {
283 RefPtr<ApplicationCacheResource> resource = it->second;
284 unsigned type = resource->type();
285 bool isMaster = type & ApplicationCacheResource::Master;
286 bool isManifest = type & ApplicationCacheResource::Manifest;
287 bool isExplicit = type & ApplicationCacheResource::Explicit;
288 bool isForeign = type & ApplicationCacheResource::Foreign;
289 bool isFallback = type & ApplicationCacheResource::Fallback;
290 resources->append(ResourceInfo(resource->url(), isMaster, isManifest, isFallback, isForeign, isExplicit, resource->estimatedSizeInStorage()));
294 ApplicationCacheHost::CacheInfo ApplicationCacheHost::applicationCacheInfo()
296 ApplicationCache* cache = applicationCache();
297 if (!cache || !cache->isComplete())
298 return CacheInfo(KURL(), 0, 0, 0);
300 // FIXME: Add "Creation Time" and "Update Time" to Application Caches.
301 return CacheInfo(cache->manifestResource()->url(), 0, 0, cache->estimatedSizeInStorage());
305 void ApplicationCacheHost::dispatchDOMEvent(EventID id, int total, int done)
307 if (m_domApplicationCache) {
308 const AtomicString& eventType = DOMApplicationCache::toEventType(id);
309 ExceptionCode ec = 0;
311 if (id == PROGRESS_EVENT)
312 event = ProgressEvent::create(eventType, true, done, total);
314 event = Event::create(eventType, false, false);
315 m_domApplicationCache->dispatchEvent(event, ec);
320 void ApplicationCacheHost::setCandidateApplicationCacheGroup(ApplicationCacheGroup* group)
322 ASSERT(!m_applicationCache);
323 m_candidateApplicationCacheGroup = group;
326 void ApplicationCacheHost::setApplicationCache(PassRefPtr<ApplicationCache> applicationCache)
328 if (m_candidateApplicationCacheGroup) {
329 ASSERT(!m_applicationCache);
330 m_candidateApplicationCacheGroup = 0;
333 m_applicationCache = applicationCache;
336 bool ApplicationCacheHost::shouldLoadResourceFromApplicationCache(const ResourceRequest& request, ApplicationCacheResource*& resource)
338 ApplicationCache* cache = applicationCache();
339 if (!cache || !cache->isComplete())
342 // If the resource is not to be fetched using the HTTP GET mechanism or equivalent, or if its URL has a different
343 // <scheme> component than the application cache's manifest, then fetch the resource normally.
344 if (!ApplicationCache::requestIsHTTPOrHTTPSGet(request) || !equalIgnoringCase(request.url().protocol(), cache->manifestResource()->url().protocol()))
347 // If the resource's URL is an master entry, the manifest, an explicit entry, or a fallback entry
348 // in the application cache, then get the resource from the cache (instead of fetching it).
349 resource = cache->resourceForURL(request.url());
351 // Resources that match fallback namespaces or online whitelist entries are fetched from the network,
352 // unless they are also cached.
353 if (!resource && (cache->urlMatchesFallbackNamespace(request.url()) || cache->isURLInOnlineWhitelist(request.url())))
356 // Resources that are not present in the manifest will always fail to load (at least, after the
357 // cache has been primed the first time), making the testing of offline applications simpler.
361 bool ApplicationCacheHost::getApplicationCacheFallbackResource(const ResourceRequest& request, ApplicationCacheResource*& resource, ApplicationCache* cache)
364 cache = applicationCache();
368 if (!cache->isComplete())
371 // If the resource is not a HTTP/HTTPS GET, then abort
372 if (!ApplicationCache::requestIsHTTPOrHTTPSGet(request))
376 if (!cache->urlMatchesFallbackNamespace(request.url(), &fallbackURL))
379 resource = cache->resourceForURL(fallbackURL);
385 bool ApplicationCacheHost::scheduleLoadFallbackResourceFromApplicationCache(ResourceLoader* loader, ApplicationCache* cache)
387 if (!isApplicationCacheEnabled())
390 ApplicationCacheResource* resource;
391 if (!getApplicationCacheFallbackResource(loader->request(), resource, cache))
394 m_documentLoader->m_pendingSubstituteResources.set(loader, resource);
395 m_documentLoader->deliverSubstituteResourcesAfterDelay();
397 loader->handle()->cancel();
402 ApplicationCacheHost::Status ApplicationCacheHost::status() const
404 ApplicationCache* cache = applicationCache();
408 switch (cache->group()->updateStatus()) {
409 case ApplicationCacheGroup::Checking:
411 case ApplicationCacheGroup::Downloading:
413 case ApplicationCacheGroup::Idle: {
414 if (cache->group()->isObsolete())
416 if (cache != cache->group()->newestCache())
422 ASSERT_NOT_REACHED();
426 bool ApplicationCacheHost::update()
428 ApplicationCache* cache = applicationCache();
431 cache->group()->update(m_documentLoader->frame(), ApplicationCacheUpdateWithoutBrowsingContext);
435 bool ApplicationCacheHost::swapCache()
437 ApplicationCache* cache = applicationCache();
441 // If the group of application caches to which cache belongs has the lifecycle status obsolete, unassociate document from cache.
442 if (cache->group()->isObsolete()) {
443 cache->group()->disassociateDocumentLoader(m_documentLoader);
447 // If there is no newer cache, raise an INVALID_STATE_ERR exception.
448 ApplicationCache* newestCache = cache->group()->newestCache();
449 if (cache == newestCache)
452 ASSERT(cache->group() == newestCache->group());
453 setApplicationCache(newestCache);
458 bool ApplicationCacheHost::isApplicationCacheEnabled()
460 return m_documentLoader->frame()->settings()
461 && m_documentLoader->frame()->settings()->offlineWebApplicationCacheEnabled();
464 } // namespace WebCore
466 #endif // ENABLE(OFFLINE_WEB_APPLICATIONS)