Home | History | Annotate | Download | only in appcache
      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 "core/loader/appcache/ApplicationCacheHost.h"
     33 
     34 #include "bindings/v8/ExceptionStatePlaceholder.h"
     35 #include "core/events/ApplicationCacheErrorEvent.h"
     36 #include "core/events/ProgressEvent.h"
     37 #include "core/frame/LocalFrame.h"
     38 #include "core/frame/Settings.h"
     39 #include "core/inspector/InspectorApplicationCacheAgent.h"
     40 #include "core/inspector/InspectorInstrumentation.h"
     41 #include "core/loader/DocumentLoader.h"
     42 #include "core/loader/FrameLoader.h"
     43 #include "core/loader/FrameLoaderClient.h"
     44 #include "core/loader/appcache/ApplicationCache.h"
     45 #include "core/page/FrameTree.h"
     46 #include "core/page/Page.h"
     47 #include "platform/exported/WrappedResourceRequest.h"
     48 #include "platform/exported/WrappedResourceResponse.h"
     49 #include "platform/weborigin/SecurityOrigin.h"
     50 #include "public/platform/WebURL.h"
     51 #include "public/platform/WebURLError.h"
     52 #include "public/platform/WebURLResponse.h"
     53 #include "public/platform/WebVector.h"
     54 
     55 using namespace blink;
     56 
     57 namespace WebCore {
     58 
     59 // We provide a custom implementation of this class that calls out to the
     60 // embedding application instead of using WebCore's built in appcache system.
     61 // This file replaces webcore/appcache/ApplicationCacheHost.cpp in our build.
     62 
     63 ApplicationCacheHost::ApplicationCacheHost(DocumentLoader* documentLoader)
     64     : m_domApplicationCache(0)
     65     , m_documentLoader(documentLoader)
     66     , m_defersEvents(true)
     67 {
     68     ASSERT(m_documentLoader);
     69 }
     70 
     71 ApplicationCacheHost::~ApplicationCacheHost()
     72 {
     73 }
     74 
     75 void ApplicationCacheHost::willStartLoadingMainResource(ResourceRequest& request)
     76 {
     77     // We defer creating the outer host object to avoid spurious creation/destruction
     78     // around creating empty documents. At this point, we're initiating a main resource
     79     // load for the document, so its for real.
     80 
     81     if (!isApplicationCacheEnabled())
     82         return;
     83 
     84     ASSERT(m_documentLoader->frame());
     85     LocalFrame& frame = *m_documentLoader->frame();
     86     m_host = frame.loader().client()->createApplicationCacheHost(this);
     87     if (m_host) {
     88         WrappedResourceRequest wrapped(request);
     89 
     90         const WebApplicationCacheHost* spawningHost = 0;
     91         Frame* spawningFrame = frame.tree().parent();
     92         if (!spawningFrame || !spawningFrame->isLocalFrame())
     93             spawningFrame = frame.loader().opener();
     94         if (!spawningFrame || !spawningFrame->isLocalFrame())
     95             spawningFrame = &frame;
     96         if (DocumentLoader* spawningDocLoader = toLocalFrame(spawningFrame)->loader().documentLoader())
     97             spawningHost = spawningDocLoader->applicationCacheHost() ? spawningDocLoader->applicationCacheHost()->m_host.get() : 0;
     98 
     99         m_host->willStartMainResourceRequest(wrapped, spawningHost);
    100     }
    101 
    102     // NOTE: The semantics of this method, and others in this interface, are subtly different
    103     // than the method names would suggest. For example, in this method never returns an appcached
    104     // response in the SubstituteData out argument, instead we return the appcached response thru
    105     // the usual resource loading pipeline.
    106 }
    107 
    108 void ApplicationCacheHost::selectCacheWithoutManifest()
    109 {
    110     if (m_host)
    111         m_host->selectCacheWithoutManifest();
    112 }
    113 
    114 void ApplicationCacheHost::selectCacheWithManifest(const KURL& manifestURL)
    115 {
    116     if (m_host && !m_host->selectCacheWithManifest(manifestURL)) {
    117         // It's a foreign entry, restart the current navigation from the top
    118         // of the navigation algorithm. The navigation will not result in the
    119         // same resource being loaded, because "foreign" entries are never picked
    120         // during navigation.
    121         // see WebCore::ApplicationCacheGroup::selectCache()
    122         LocalFrame* frame = m_documentLoader->frame();
    123         frame->navigationScheduler().scheduleLocationChange(frame->document(), frame->document()->url(), Referrer(frame->document()->referrer(), frame->document()->referrerPolicy()));
    124     }
    125 }
    126 
    127 void ApplicationCacheHost::didReceiveResponseForMainResource(const ResourceResponse& response)
    128 {
    129     if (m_host) {
    130         WrappedResourceResponse wrapped(response);
    131         m_host->didReceiveResponseForMainResource(wrapped);
    132     }
    133 }
    134 
    135 void ApplicationCacheHost::mainResourceDataReceived(const char* data, int length)
    136 {
    137     if (m_host)
    138         m_host->didReceiveDataForMainResource(data, length);
    139 }
    140 
    141 void ApplicationCacheHost::failedLoadingMainResource()
    142 {
    143     if (m_host)
    144         m_host->didFinishLoadingMainResource(false);
    145 }
    146 
    147 void ApplicationCacheHost::finishedLoadingMainResource()
    148 {
    149     if (m_host)
    150         m_host->didFinishLoadingMainResource(true);
    151 }
    152 
    153 void ApplicationCacheHost::willStartLoadingResource(ResourceRequest& request)
    154 {
    155     if (m_host) {
    156         WrappedResourceRequest wrapped(request);
    157         m_host->willStartSubResourceRequest(wrapped);
    158     }
    159 }
    160 
    161 void ApplicationCacheHost::setApplicationCache(ApplicationCache* domApplicationCache)
    162 {
    163     ASSERT(!m_domApplicationCache || !domApplicationCache);
    164     m_domApplicationCache = domApplicationCache;
    165 }
    166 
    167 void ApplicationCacheHost::notifyApplicationCache(EventID id, int progressTotal, int progressDone, blink::WebApplicationCacheHost::ErrorReason errorReason, const String& errorURL, int errorStatus, const String& errorMessage)
    168 {
    169     if (id != PROGRESS_EVENT)
    170         InspectorInstrumentation::updateApplicationCacheStatus(m_documentLoader->frame());
    171 
    172     if (m_defersEvents) {
    173         // Event dispatching is deferred until document.onload has fired.
    174         m_deferredEvents.append(DeferredEvent(id, progressTotal, progressDone, errorReason, errorURL, errorStatus, errorMessage));
    175         return;
    176     }
    177     dispatchDOMEvent(id, progressTotal, progressDone, errorReason, errorURL, errorStatus, errorMessage);
    178 }
    179 
    180 ApplicationCacheHost::CacheInfo ApplicationCacheHost::applicationCacheInfo()
    181 {
    182     if (!m_host)
    183         return CacheInfo(KURL(), 0, 0, 0);
    184 
    185     blink::WebApplicationCacheHost::CacheInfo webInfo;
    186     m_host->getAssociatedCacheInfo(&webInfo);
    187     return CacheInfo(webInfo.manifestURL, webInfo.creationTime, webInfo.updateTime, webInfo.totalSize);
    188 }
    189 
    190 void ApplicationCacheHost::fillResourceList(ResourceInfoList* resources)
    191 {
    192     if (!m_host)
    193         return;
    194 
    195     blink::WebVector<blink::WebApplicationCacheHost::ResourceInfo> webResources;
    196     m_host->getResourceList(&webResources);
    197     for (size_t i = 0; i < webResources.size(); ++i) {
    198         resources->append(ResourceInfo(
    199             webResources[i].url, webResources[i].isMaster, webResources[i].isManifest, webResources[i].isFallback,
    200             webResources[i].isForeign, webResources[i].isExplicit, webResources[i].size));
    201     }
    202 }
    203 
    204 void ApplicationCacheHost::stopDeferringEvents()
    205 {
    206     RefPtr<DocumentLoader> protect(documentLoader());
    207     for (unsigned i = 0; i < m_deferredEvents.size(); ++i) {
    208         const DeferredEvent& deferred = m_deferredEvents[i];
    209         dispatchDOMEvent(deferred.eventID, deferred.progressTotal, deferred.progressDone, deferred.errorReason, deferred.errorURL, deferred.errorStatus, deferred.errorMessage);
    210     }
    211     m_deferredEvents.clear();
    212     m_defersEvents = false;
    213 }
    214 
    215 void ApplicationCacheHost::dispatchDOMEvent(EventID id, int progressTotal, int progressDone, blink::WebApplicationCacheHost::ErrorReason errorReason, const String& errorURL, int errorStatus, const String& errorMessage)
    216 {
    217     if (m_domApplicationCache) {
    218         const AtomicString& eventType = ApplicationCache::toEventType(id);
    219         RefPtrWillBeRawPtr<Event> event = nullptr;
    220         if (id == PROGRESS_EVENT)
    221             event = ProgressEvent::create(eventType, true, progressDone, progressTotal);
    222         else if (id == ERROR_EVENT)
    223             event = ApplicationCacheErrorEvent::create(errorReason, errorURL, errorStatus, errorMessage);
    224         else
    225             event = Event::create(eventType);
    226         m_domApplicationCache->dispatchEvent(event, ASSERT_NO_EXCEPTION);
    227     }
    228 }
    229 
    230 ApplicationCacheHost::Status ApplicationCacheHost::status() const
    231 {
    232     return m_host ? static_cast<Status>(m_host->status()) : UNCACHED;
    233 }
    234 
    235 bool ApplicationCacheHost::update()
    236 {
    237     return m_host ? m_host->startUpdate() : false;
    238 }
    239 
    240 bool ApplicationCacheHost::swapCache()
    241 {
    242     bool success = m_host ? m_host->swapCache() : false;
    243     if (success)
    244         InspectorInstrumentation::updateApplicationCacheStatus(m_documentLoader->frame());
    245     return success;
    246 }
    247 
    248 void ApplicationCacheHost::abort()
    249 {
    250     if (m_host)
    251         m_host->abort();
    252 }
    253 
    254 bool ApplicationCacheHost::isApplicationCacheEnabled()
    255 {
    256     ASSERT(m_documentLoader->frame());
    257     return m_documentLoader->frame()->settings() && m_documentLoader->frame()->settings()->offlineWebApplicationCacheEnabled();
    258 }
    259 
    260 void ApplicationCacheHost::didChangeCacheAssociation()
    261 {
    262     // FIXME: Prod the inspector to update its notion of what cache the page is using.
    263 }
    264 
    265 void ApplicationCacheHost::notifyEventListener(blink::WebApplicationCacheHost::EventID eventID)
    266 {
    267     notifyApplicationCache(static_cast<ApplicationCacheHost::EventID>(eventID), 0, 0, blink::WebApplicationCacheHost::UnknownError, String(), 0, String());
    268 }
    269 
    270 void ApplicationCacheHost::notifyProgressEventListener(const blink::WebURL&, int progressTotal, int progressDone)
    271 {
    272     notifyApplicationCache(PROGRESS_EVENT, progressTotal, progressDone, blink::WebApplicationCacheHost::UnknownError, String(), 0, String());
    273 }
    274 
    275 void ApplicationCacheHost::notifyErrorEventListener(blink::WebApplicationCacheHost::ErrorReason reason, const blink::WebURL& url, int status, const blink::WebString& message)
    276 {
    277     notifyApplicationCache(ERROR_EVENT, 0, 0, reason, url.string(), status, message);
    278 }
    279 
    280 } // namespace WebCore
    281