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/core/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 blink { 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(nullptr) 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 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::dispose() 168 { 169 // FIXME: Oilpan: remove the dispose step when the owning DocumentLoader 170 // becomes a garbage collected object. Until that time, have the 171 // DocumentLoader dispose and disable this ApplicationCacheHost when 172 // it is finalized. Releasing the WebApplicationCacheHost is needed 173 // to prevent further embedder notifications, which risk accessing an 174 // invalid DocumentLoader. 175 setApplicationCache(0); 176 m_host.clear(); 177 m_documentLoader = nullptr; 178 } 179 180 void ApplicationCacheHost::notifyApplicationCache(EventID id, int progressTotal, int progressDone, WebApplicationCacheHost::ErrorReason errorReason, const String& errorURL, int errorStatus, const String& errorMessage) 181 { 182 if (id != PROGRESS_EVENT) 183 InspectorInstrumentation::updateApplicationCacheStatus(m_documentLoader->frame()); 184 185 if (m_defersEvents) { 186 // Event dispatching is deferred until document.onload has fired. 187 m_deferredEvents.append(DeferredEvent(id, progressTotal, progressDone, errorReason, errorURL, errorStatus, errorMessage)); 188 return; 189 } 190 dispatchDOMEvent(id, progressTotal, progressDone, errorReason, errorURL, errorStatus, errorMessage); 191 } 192 193 ApplicationCacheHost::CacheInfo ApplicationCacheHost::applicationCacheInfo() 194 { 195 if (!m_host) 196 return CacheInfo(KURL(), 0, 0, 0); 197 198 WebApplicationCacheHost::CacheInfo webInfo; 199 m_host->getAssociatedCacheInfo(&webInfo); 200 return CacheInfo(webInfo.manifestURL, webInfo.creationTime, webInfo.updateTime, webInfo.totalSize); 201 } 202 203 void ApplicationCacheHost::fillResourceList(ResourceInfoList* resources) 204 { 205 if (!m_host) 206 return; 207 208 WebVector<WebApplicationCacheHost::ResourceInfo> webResources; 209 m_host->getResourceList(&webResources); 210 for (size_t i = 0; i < webResources.size(); ++i) { 211 resources->append(ResourceInfo( 212 webResources[i].url, webResources[i].isMaster, webResources[i].isManifest, webResources[i].isFallback, 213 webResources[i].isForeign, webResources[i].isExplicit, webResources[i].size)); 214 } 215 } 216 217 void ApplicationCacheHost::stopDeferringEvents() 218 { 219 RefPtr<DocumentLoader> protect(documentLoader()); 220 for (unsigned i = 0; i < m_deferredEvents.size(); ++i) { 221 const DeferredEvent& deferred = m_deferredEvents[i]; 222 dispatchDOMEvent(deferred.eventID, deferred.progressTotal, deferred.progressDone, deferred.errorReason, deferred.errorURL, deferred.errorStatus, deferred.errorMessage); 223 } 224 m_deferredEvents.clear(); 225 m_defersEvents = false; 226 } 227 228 void ApplicationCacheHost::dispatchDOMEvent(EventID id, int progressTotal, int progressDone, WebApplicationCacheHost::ErrorReason errorReason, const String& errorURL, int errorStatus, const String& errorMessage) 229 { 230 if (m_domApplicationCache) { 231 const AtomicString& eventType = ApplicationCache::toEventType(id); 232 RefPtrWillBeRawPtr<Event> event = nullptr; 233 if (id == PROGRESS_EVENT) 234 event = ProgressEvent::create(eventType, true, progressDone, progressTotal); 235 else if (id == ERROR_EVENT) 236 event = ApplicationCacheErrorEvent::create(errorReason, errorURL, errorStatus, errorMessage); 237 else 238 event = Event::create(eventType); 239 m_domApplicationCache->dispatchEvent(event, ASSERT_NO_EXCEPTION); 240 } 241 } 242 243 ApplicationCacheHost::Status ApplicationCacheHost::status() const 244 { 245 return m_host ? static_cast<Status>(m_host->status()) : UNCACHED; 246 } 247 248 bool ApplicationCacheHost::update() 249 { 250 return m_host ? m_host->startUpdate() : false; 251 } 252 253 bool ApplicationCacheHost::swapCache() 254 { 255 bool success = m_host ? m_host->swapCache() : false; 256 if (success) 257 InspectorInstrumentation::updateApplicationCacheStatus(m_documentLoader->frame()); 258 return success; 259 } 260 261 void ApplicationCacheHost::abort() 262 { 263 if (m_host) 264 m_host->abort(); 265 } 266 267 bool ApplicationCacheHost::isApplicationCacheEnabled() 268 { 269 ASSERT(m_documentLoader->frame()); 270 return m_documentLoader->frame()->settings() && m_documentLoader->frame()->settings()->offlineWebApplicationCacheEnabled(); 271 } 272 273 void ApplicationCacheHost::didChangeCacheAssociation() 274 { 275 // FIXME: Prod the inspector to update its notion of what cache the page is using. 276 } 277 278 void ApplicationCacheHost::notifyEventListener(WebApplicationCacheHost::EventID eventID) 279 { 280 notifyApplicationCache(static_cast<ApplicationCacheHost::EventID>(eventID), 0, 0, WebApplicationCacheHost::UnknownError, String(), 0, String()); 281 } 282 283 void ApplicationCacheHost::notifyProgressEventListener(const WebURL&, int progressTotal, int progressDone) 284 { 285 notifyApplicationCache(PROGRESS_EVENT, progressTotal, progressDone, WebApplicationCacheHost::UnknownError, String(), 0, String()); 286 } 287 288 void ApplicationCacheHost::notifyErrorEventListener(WebApplicationCacheHost::ErrorReason reason, const WebURL& url, int status, const WebString& message) 289 { 290 notifyApplicationCache(ERROR_EVENT, 0, 0, reason, url.string(), status, message); 291 } 292 293 void ApplicationCacheHost::trace(Visitor* visitor) 294 { 295 visitor->trace(m_domApplicationCache); 296 } 297 298 } // namespace blink 299