1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
|
/*
* Copyright (C) 2008, 2009 Apple Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "ApplicationCacheHost.h"
#include "ApplicationCache.h"
#include "ApplicationCacheGroup.h"
#include "ApplicationCacheResource.h"
#include "DocumentLoader.h"
#include "DOMApplicationCache.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameLoaderClient.h"
#include "InspectorInstrumentation.h"
#include "ProgressEvent.h"
#include "ResourceHandle.h"
#include "ResourceLoader.h"
#include "ResourceRequest.h"
#include "Settings.h"
namespace WebCore {
ApplicationCacheHost::ApplicationCacheHost(DocumentLoader* documentLoader)
: m_domApplicationCache(0)
, m_documentLoader(documentLoader)
, m_defersEvents(true)
, m_candidateApplicationCacheGroup(0)
{
ASSERT(m_documentLoader);
}
ApplicationCacheHost::~ApplicationCacheHost()
{
ASSERT(!m_applicationCache || !m_candidateApplicationCacheGroup || m_applicationCache->group() == m_candidateApplicationCacheGroup);
if (m_applicationCache)
m_applicationCache->group()->disassociateDocumentLoader(m_documentLoader);
else if (m_candidateApplicationCacheGroup)
m_candidateApplicationCacheGroup->disassociateDocumentLoader(m_documentLoader);
}
void ApplicationCacheHost::selectCacheWithoutManifest()
{
ApplicationCacheGroup::selectCacheWithoutManifestURL(m_documentLoader->frame());
}
void ApplicationCacheHost::selectCacheWithManifest(const KURL& manifestURL)
{
ApplicationCacheGroup::selectCache(m_documentLoader->frame(), manifestURL);
}
void ApplicationCacheHost::maybeLoadMainResource(ResourceRequest& request, SubstituteData& substituteData)
{
// Check if this request should be loaded from the application cache
if (!substituteData.isValid() && isApplicationCacheEnabled()) {
ASSERT(!m_mainResourceApplicationCache);
m_mainResourceApplicationCache = ApplicationCacheGroup::cacheForMainRequest(request, m_documentLoader);
if (m_mainResourceApplicationCache) {
// Get the resource from the application cache. By definition, cacheForMainRequest() returns a cache that contains the resource.
ApplicationCacheResource* resource = m_mainResourceApplicationCache->resourceForRequest(request);
substituteData = SubstituteData(resource->data(),
resource->response().mimeType(),
resource->response().textEncodingName(), KURL());
}
}
}
void ApplicationCacheHost::maybeLoadMainResourceForRedirect(ResourceRequest& request, SubstituteData& substituteData)
{
ASSERT(status() == UNCACHED);
maybeLoadMainResource(request, substituteData);
}
bool ApplicationCacheHost::maybeLoadFallbackForMainResponse(const ResourceRequest& request, const ResourceResponse& r)
{
if (r.httpStatusCode() / 100 == 4 || r.httpStatusCode() / 100 == 5) {
ASSERT(!m_mainResourceApplicationCache);
if (isApplicationCacheEnabled()) {
m_mainResourceApplicationCache = ApplicationCacheGroup::fallbackCacheForMainRequest(request, documentLoader());
if (scheduleLoadFallbackResourceFromApplicationCache(documentLoader()->mainResourceLoader(), m_mainResourceApplicationCache.get()))
return true;
}
}
return false;
}
bool ApplicationCacheHost::maybeLoadFallbackForMainError(const ResourceRequest& request, const ResourceError& error)
{
if (!error.isCancellation()) {
ASSERT(!m_mainResourceApplicationCache);
if (isApplicationCacheEnabled()) {
m_mainResourceApplicationCache = ApplicationCacheGroup::fallbackCacheForMainRequest(request, m_documentLoader);
if (scheduleLoadFallbackResourceFromApplicationCache(documentLoader()->mainResourceLoader(), m_mainResourceApplicationCache.get()))
return true;
}
}
return false;
}
void ApplicationCacheHost::mainResourceDataReceived(const char*, int, long long, bool)
{
// This method is here to facilitate alternate implemetations of this interface by the host browser.
}
void ApplicationCacheHost::failedLoadingMainResource()
{
ApplicationCacheGroup* group = m_candidateApplicationCacheGroup;
if (!group && m_applicationCache) {
if (mainResourceApplicationCache()) {
// Even when the main resource is being loaded from an application cache, loading can fail if aborted.
return;
}
group = m_applicationCache->group();
}
if (group)
group->failedLoadingMainResource(m_documentLoader);
}
void ApplicationCacheHost::finishedLoadingMainResource()
{
ApplicationCacheGroup* group = candidateApplicationCacheGroup();
if (!group && applicationCache() && !mainResourceApplicationCache())
group = applicationCache()->group();
if (group)
group->finishedLoadingMainResource(m_documentLoader);
}
bool ApplicationCacheHost::maybeLoadResource(ResourceLoader* loader, ResourceRequest& request, const KURL& originalURL)
{
if (!isApplicationCacheEnabled())
return false;
if (request.url() != originalURL)
return false;
ApplicationCacheResource* resource;
if (!shouldLoadResourceFromApplicationCache(request, resource))
return false;
m_documentLoader->m_pendingSubstituteResources.set(loader, resource);
m_documentLoader->deliverSubstituteResourcesAfterDelay();
return true;
}
bool ApplicationCacheHost::maybeLoadFallbackForRedirect(ResourceLoader* resourceLoader, ResourceRequest& request, const ResourceResponse& redirectResponse)
{
if (!redirectResponse.isNull() && !protocolHostAndPortAreEqual(request.url(), redirectResponse.url()))
if (scheduleLoadFallbackResourceFromApplicationCache(resourceLoader))
return true;
return false;
}
bool ApplicationCacheHost::maybeLoadFallbackForResponse(ResourceLoader* resourceLoader, const ResourceResponse& response)
{
if (response.httpStatusCode() / 100 == 4 || response.httpStatusCode() / 100 == 5)
if (scheduleLoadFallbackResourceFromApplicationCache(resourceLoader))
return true;
return false;
}
bool ApplicationCacheHost::maybeLoadFallbackForError(ResourceLoader* resourceLoader, const ResourceError& error)
{
if (!error.isCancellation()) {
if (resourceLoader == m_documentLoader->mainResourceLoader())
return maybeLoadFallbackForMainError(resourceLoader->request(), error);
if (scheduleLoadFallbackResourceFromApplicationCache(resourceLoader))
return true;
}
return false;
}
bool ApplicationCacheHost::maybeLoadSynchronously(ResourceRequest& request, ResourceError& error, ResourceResponse& response, Vector<char>& data)
{
ApplicationCacheResource* resource;
if (shouldLoadResourceFromApplicationCache(request, resource)) {
if (resource) {
response = resource->response();
data.append(resource->data()->data(), resource->data()->size());
} else {
error = documentLoader()->frameLoader()->client()->cannotShowURLError(request);
}
return true;
}
return false;
}
void ApplicationCacheHost::maybeLoadFallbackSynchronously(const ResourceRequest& request, ResourceError& error, ResourceResponse& response, Vector<char>& data)
{
// 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,
// 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
// corresponding to the matched namespace.
if ((!error.isNull() && !error.isCancellation())
|| response.httpStatusCode() / 100 == 4 || response.httpStatusCode() / 100 == 5
|| !protocolHostAndPortAreEqual(request.url(), response.url())) {
ApplicationCacheResource* resource;
if (getApplicationCacheFallbackResource(request, resource)) {
response = resource->response();
data.clear();
data.append(resource->data()->data(), resource->data()->size());
}
}
}
bool ApplicationCacheHost::canCacheInPageCache()
{
return !applicationCache() && !candidateApplicationCacheGroup();
}
void ApplicationCacheHost::setDOMApplicationCache(DOMApplicationCache* domApplicationCache)
{
ASSERT(!m_domApplicationCache || !domApplicationCache);
m_domApplicationCache = domApplicationCache;
}
void ApplicationCacheHost::notifyDOMApplicationCache(EventID id, int total, int done)
{
if (id != PROGRESS_EVENT)
InspectorInstrumentation::updateApplicationCacheStatus(m_documentLoader->frame());
if (m_defersEvents) {
// Event dispatching is deferred until document.onload has fired.
m_deferredEvents.append(DeferredEvent(id, total, done));
return;
}
dispatchDOMEvent(id, total, done);
}
void ApplicationCacheHost::stopLoadingInFrame(Frame* frame)
{
ASSERT(!m_applicationCache || !m_candidateApplicationCacheGroup || m_applicationCache->group() == m_candidateApplicationCacheGroup);
if (m_candidateApplicationCacheGroup)
m_candidateApplicationCacheGroup->stopLoadingInFrame(frame);
else if (m_applicationCache)
m_applicationCache->group()->stopLoadingInFrame(frame);
}
void ApplicationCacheHost::stopDeferringEvents()
{
RefPtr<DocumentLoader> protect(documentLoader());
for (unsigned i = 0; i < m_deferredEvents.size(); ++i) {
const DeferredEvent& deferred = m_deferredEvents[i];
dispatchDOMEvent(deferred.eventID, deferred.progressTotal, deferred.progressDone);
}
m_deferredEvents.clear();
m_defersEvents = false;
}
#if ENABLE(INSPECTOR)
void ApplicationCacheHost::fillResourceList(ResourceInfoList* resources)
{
ApplicationCache* cache = applicationCache();
if (!cache || !cache->isComplete())
return;
ApplicationCache::ResourceMap::const_iterator end = cache->end();
for (ApplicationCache::ResourceMap::const_iterator it = cache->begin(); it != end; ++it) {
RefPtr<ApplicationCacheResource> resource = it->value;
unsigned type = resource->type();
bool isMaster = type & ApplicationCacheResource::Master;
bool isManifest = type & ApplicationCacheResource::Manifest;
bool isExplicit = type & ApplicationCacheResource::Explicit;
bool isForeign = type & ApplicationCacheResource::Foreign;
bool isFallback = type & ApplicationCacheResource::Fallback;
resources->append(ResourceInfo(resource->url(), isMaster, isManifest, isFallback, isForeign, isExplicit, resource->estimatedSizeInStorage()));
}
}
ApplicationCacheHost::CacheInfo ApplicationCacheHost::applicationCacheInfo()
{
ApplicationCache* cache = applicationCache();
if (!cache || !cache->isComplete())
return CacheInfo(KURL(), 0, 0, 0);
// FIXME: Add "Creation Time" and "Update Time" to Application Caches.
return CacheInfo(cache->manifestResource()->url(), 0, 0, cache->estimatedSizeInStorage());
}
#endif
void ApplicationCacheHost::dispatchDOMEvent(EventID id, int total, int done)
{
if (m_domApplicationCache) {
const AtomicString& eventType = DOMApplicationCache::toEventType(id);
RefPtr<Event> event;
if (id == PROGRESS_EVENT)
event = ProgressEvent::create(eventType, true, done, total);
else
event = Event::create(eventType, false, false);
m_domApplicationCache->dispatchEvent(event, ASSERT_NO_EXCEPTION);
}
}
void ApplicationCacheHost::setCandidateApplicationCacheGroup(ApplicationCacheGroup* group)
{
ASSERT(!m_applicationCache);
m_candidateApplicationCacheGroup = group;
}
void ApplicationCacheHost::setApplicationCache(PassRefPtr<ApplicationCache> applicationCache)
{
if (m_candidateApplicationCacheGroup) {
ASSERT(!m_applicationCache);
m_candidateApplicationCacheGroup = 0;
}
m_applicationCache = applicationCache;
}
bool ApplicationCacheHost::shouldLoadResourceFromApplicationCache(const ResourceRequest& request, ApplicationCacheResource*& resource)
{
ApplicationCache* cache = applicationCache();
if (!cache || !cache->isComplete())
return false;
// If the resource is not to be fetched using the HTTP GET mechanism or equivalent, or if its URL has a different
// <scheme> component than the application cache's manifest, then fetch the resource normally.
if (!ApplicationCache::requestIsHTTPOrHTTPSGet(request) || !equalIgnoringCase(request.url().protocol(), cache->manifestResource()->url().protocol()))
return false;
// If the resource's URL is an master entry, the manifest, an explicit entry, or a fallback entry
// in the application cache, then get the resource from the cache (instead of fetching it).
resource = cache->resourceForURL(request.url());
// Resources that match fallback namespaces or online whitelist entries are fetched from the network,
// unless they are also cached.
if (!resource && (cache->allowsAllNetworkRequests() || cache->urlMatchesFallbackNamespace(request.url()) || cache->isURLInOnlineWhitelist(request.url())))
return false;
// Resources that are not present in the manifest will always fail to load (at least, after the
// cache has been primed the first time), making the testing of offline applications simpler.
return true;
}
bool ApplicationCacheHost::getApplicationCacheFallbackResource(const ResourceRequest& request, ApplicationCacheResource*& resource, ApplicationCache* cache)
{
if (!cache) {
cache = applicationCache();
if (!cache)
return false;
}
if (!cache->isComplete())
return false;
// If the resource is not a HTTP/HTTPS GET, then abort
if (!ApplicationCache::requestIsHTTPOrHTTPSGet(request))
return false;
KURL fallbackURL;
if (cache->isURLInOnlineWhitelist(request.url()))
return false;
if (!cache->urlMatchesFallbackNamespace(request.url(), &fallbackURL))
return false;
resource = cache->resourceForURL(fallbackURL);
ASSERT(resource);
return true;
}
bool ApplicationCacheHost::scheduleLoadFallbackResourceFromApplicationCache(ResourceLoader* loader, ApplicationCache* cache)
{
if (!isApplicationCacheEnabled())
return false;
ApplicationCacheResource* resource;
if (!getApplicationCacheFallbackResource(loader->request(), resource, cache))
return false;
m_documentLoader->m_pendingSubstituteResources.set(loader, resource);
m_documentLoader->deliverSubstituteResourcesAfterDelay();
loader->handle()->cancel();
return true;
}
ApplicationCacheHost::Status ApplicationCacheHost::status() const
{
ApplicationCache* cache = applicationCache();
if (!cache)
return UNCACHED;
switch (cache->group()->updateStatus()) {
case ApplicationCacheGroup::Checking:
return CHECKING;
case ApplicationCacheGroup::Downloading:
return DOWNLOADING;
case ApplicationCacheGroup::Idle: {
if (cache->group()->isObsolete())
return OBSOLETE;
if (cache != cache->group()->newestCache())
return UPDATEREADY;
return IDLE;
}
}
ASSERT_NOT_REACHED();
return UNCACHED;
}
bool ApplicationCacheHost::update()
{
ApplicationCache* cache = applicationCache();
if (!cache)
return false;
cache->group()->update(m_documentLoader->frame(), ApplicationCacheUpdateWithoutBrowsingContext);
return true;
}
bool ApplicationCacheHost::swapCache()
{
ApplicationCache* cache = applicationCache();
if (!cache)
return false;
// If the group of application caches to which cache belongs has the lifecycle status obsolete, unassociate document from cache.
if (cache->group()->isObsolete()) {
cache->group()->disassociateDocumentLoader(m_documentLoader);
return true;
}
// If there is no newer cache, raise an INVALID_STATE_ERR exception.
ApplicationCache* newestCache = cache->group()->newestCache();
if (cache == newestCache)
return false;
ASSERT(cache->group() == newestCache->group());
setApplicationCache(newestCache);
InspectorInstrumentation::updateApplicationCacheStatus(m_documentLoader->frame());
return true;
}
void ApplicationCacheHost::abort()
{
ApplicationCacheGroup* cacheGroup = candidateApplicationCacheGroup();
if (cacheGroup)
cacheGroup->abort(m_documentLoader->frame());
else {
ApplicationCache* cache = applicationCache();
if (cache)
cache->group()->abort(m_documentLoader->frame());
}
}
bool ApplicationCacheHost::isApplicationCacheEnabled()
{
return m_documentLoader->frame() && m_documentLoader->frame()->settings()
&& m_documentLoader->frame()->settings()->offlineWebApplicationCacheEnabled();
}
} // namespace WebCore
|