Home | History | Annotate | Download | only in web
      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 "public/platform/WebURL.h"
     35 #include "public/platform/WebURLError.h"
     36 #include "public/platform/WebURLResponse.h"
     37 #include "public/platform/WebVector.h"
     38 #include "ApplicationCacheHostInternal.h"
     39 #include "WebFrameImpl.h"
     40 #include "bindings/v8/ExceptionStatePlaceholder.h"
     41 #include "core/dom/ProgressEvent.h"
     42 #include "core/inspector/InspectorApplicationCacheAgent.h"
     43 #include "core/inspector/InspectorInstrumentation.h"
     44 #include "core/loader/DocumentLoader.h"
     45 #include "core/loader/FrameLoader.h"
     46 #include "core/loader/appcache/DOMApplicationCache.h"
     47 #include "core/page/Frame.h"
     48 #include "core/page/Page.h"
     49 #include "core/page/Settings.h"
     50 #include "core/platform/chromium/support/WrappedResourceRequest.h"
     51 #include "core/platform/chromium/support/WrappedResourceResponse.h"
     52 #include "weborigin/SecurityOrigin.h"
     53 
     54 using namespace WebKit;
     55 
     56 namespace WebCore {
     57 
     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.
     61 
     62 ApplicationCacheHost::ApplicationCacheHost(DocumentLoader* documentLoader)
     63     : m_domApplicationCache(0)
     64     , m_documentLoader(documentLoader)
     65     , m_defersEvents(true)
     66 {
     67     ASSERT(m_documentLoader);
     68 }
     69 
     70 ApplicationCacheHost::~ApplicationCacheHost()
     71 {
     72 }
     73 
     74 void ApplicationCacheHost::willStartLoadingMainResource(ResourceRequest& request)
     75 {
     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.
     79 
     80     if (!isApplicationCacheEnabled())
     81         return;
     82 
     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()));
     87     } else
     88         m_internal.clear();
     89 
     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.
     94 }
     95 
     96 void ApplicationCacheHost::selectCacheWithoutManifest()
     97 {
     98     if (m_internal)
     99         m_internal->m_outerHost->selectCacheWithoutManifest();
    100 }
    101 
    102 void ApplicationCacheHost::selectCacheWithManifest(const KURL& manifestURL)
    103 {
    104     if (m_internal) {
    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());
    114         }
    115     }
    116 }
    117 
    118 void ApplicationCacheHost::didReceiveResponseForMainResource(const ResourceResponse& response)
    119 {
    120     if (m_internal) {
    121         WrappedResourceResponse wrapped(response);
    122         m_internal->m_outerHost->didReceiveResponseForMainResource(wrapped);
    123     }
    124 }
    125 
    126 void ApplicationCacheHost::mainResourceDataReceived(const char* data, int length)
    127 {
    128     if (m_internal)
    129         m_internal->m_outerHost->didReceiveDataForMainResource(data, length);
    130 }
    131 
    132 void ApplicationCacheHost::failedLoadingMainResource()
    133 {
    134     if (m_internal)
    135         m_internal->m_outerHost->didFinishLoadingMainResource(false);
    136 }
    137 
    138 void ApplicationCacheHost::finishedLoadingMainResource()
    139 {
    140     if (m_internal)
    141         m_internal->m_outerHost->didFinishLoadingMainResource(true);
    142 }
    143 
    144 void ApplicationCacheHost::willStartLoadingResource(ResourceRequest& request)
    145 {
    146     // FIXME: look into the purpose of the unused KURL& originalURL parameter
    147     if (m_internal) {
    148         WrappedResourceRequest wrapped(request);
    149         m_internal->m_outerHost->willStartSubResourceRequest(wrapped);
    150     }
    151 }
    152 
    153 void ApplicationCacheHost::willStartLoadingSynchronously(ResourceRequest& request)
    154 {
    155     if (m_internal) {
    156         WrappedResourceRequest wrapped(request);
    157         m_internal->m_outerHost->willStartSubResourceRequest(wrapped);
    158     }
    159 }
    160 
    161 void ApplicationCacheHost::setDOMApplicationCache(DOMApplicationCache* domApplicationCache)
    162 {
    163     ASSERT(!m_domApplicationCache || !domApplicationCache);
    164     m_domApplicationCache = domApplicationCache;
    165 }
    166 
    167 void ApplicationCacheHost::notifyDOMApplicationCache(EventID id, int total, int done)
    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, total, done));
    175         return;
    176     }
    177     dispatchDOMEvent(id, total, done);
    178 }
    179 
    180 ApplicationCacheHost::CacheInfo ApplicationCacheHost::applicationCacheInfo()
    181 {
    182     if (!m_internal)
    183         return CacheInfo(KURL(), 0, 0, 0);
    184 
    185     WebKit::WebApplicationCacheHost::CacheInfo webInfo;
    186     m_internal->m_outerHost->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_internal)
    193         return;
    194 
    195     WebKit::WebVector<WebKit::WebApplicationCacheHost::ResourceInfo> webResources;
    196     m_internal->m_outerHost->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);
    210     }
    211     m_deferredEvents.clear();
    212     m_defersEvents = false;
    213 }
    214 
    215 void ApplicationCacheHost::dispatchDOMEvent(EventID id, int total, int done)
    216 {
    217     if (m_domApplicationCache) {
    218         const AtomicString& eventType = DOMApplicationCache::toEventType(id);
    219         RefPtr<Event> event;
    220         if (id == PROGRESS_EVENT)
    221             event = ProgressEvent::create(eventType, true, done, total);
    222         else
    223             event = Event::create(eventType, false, false);
    224         m_domApplicationCache->dispatchEvent(event, ASSERT_NO_EXCEPTION);
    225     }
    226 }
    227 
    228 ApplicationCacheHost::Status ApplicationCacheHost::status() const
    229 {
    230     return m_internal ? static_cast<Status>(m_internal->m_outerHost->status()) : UNCACHED;
    231 }
    232 
    233 bool ApplicationCacheHost::update()
    234 {
    235     return m_internal ? m_internal->m_outerHost->startUpdate() : false;
    236 }
    237 
    238 bool ApplicationCacheHost::swapCache()
    239 {
    240     bool success = m_internal ? m_internal->m_outerHost->swapCache() : false;
    241     if (success)
    242         InspectorInstrumentation::updateApplicationCacheStatus(m_documentLoader->frame());
    243     return success;
    244 }
    245 
    246 void ApplicationCacheHost::abort()
    247 {
    248     if (m_internal)
    249         m_internal->m_outerHost->abort();
    250 }
    251 
    252 bool ApplicationCacheHost::isApplicationCacheEnabled()
    253 {
    254     ASSERT(m_documentLoader->frame());
    255     return m_documentLoader->frame()->settings()
    256            && m_documentLoader->frame()->settings()->offlineWebApplicationCacheEnabled();
    257 }
    258 
    259 }  // namespace WebCore
    260