2 * Copyright (C) 2009 Google 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 are
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
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.
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.
32 #include "core/loader/appcache/ApplicationCacheHost.h"
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"
55 using namespace blink;
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.
63 ApplicationCacheHost::ApplicationCacheHost(DocumentLoader* documentLoader)
64 : m_domApplicationCache(nullptr)
65 , m_documentLoader(documentLoader)
66 , m_defersEvents(true)
68 ASSERT(m_documentLoader);
71 ApplicationCacheHost::~ApplicationCacheHost()
75 void ApplicationCacheHost::willStartLoadingMainResource(ResourceRequest& request)
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.
81 if (!isApplicationCacheEnabled())
84 ASSERT(m_documentLoader->frame());
85 LocalFrame& frame = *m_documentLoader->frame();
86 m_host = frame.loader().client()->createApplicationCacheHost(this);
88 WrappedResourceRequest wrapped(request);
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;
99 m_host->willStartMainResourceRequest(wrapped, spawningHost);
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.
108 void ApplicationCacheHost::selectCacheWithoutManifest()
111 m_host->selectCacheWithoutManifest();
114 void ApplicationCacheHost::selectCacheWithManifest(const KURL& manifestURL)
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()));
127 void ApplicationCacheHost::didReceiveResponseForMainResource(const ResourceResponse& response)
130 WrappedResourceResponse wrapped(response);
131 m_host->didReceiveResponseForMainResource(wrapped);
135 void ApplicationCacheHost::mainResourceDataReceived(const char* data, int length)
138 m_host->didReceiveDataForMainResource(data, length);
141 void ApplicationCacheHost::failedLoadingMainResource()
144 m_host->didFinishLoadingMainResource(false);
147 void ApplicationCacheHost::finishedLoadingMainResource()
150 m_host->didFinishLoadingMainResource(true);
153 void ApplicationCacheHost::willStartLoadingResource(ResourceRequest& request)
156 WrappedResourceRequest wrapped(request);
157 m_host->willStartSubResourceRequest(wrapped);
161 void ApplicationCacheHost::setApplicationCache(ApplicationCache* domApplicationCache)
163 ASSERT(!m_domApplicationCache || !domApplicationCache);
164 m_domApplicationCache = domApplicationCache;
167 void ApplicationCacheHost::dispose()
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);
177 m_documentLoader = nullptr;
180 void ApplicationCacheHost::notifyApplicationCache(EventID id, int progressTotal, int progressDone, WebApplicationCacheHost::ErrorReason errorReason, const String& errorURL, int errorStatus, const String& errorMessage)
182 if (id != PROGRESS_EVENT)
183 InspectorInstrumentation::updateApplicationCacheStatus(m_documentLoader->frame());
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));
190 dispatchDOMEvent(id, progressTotal, progressDone, errorReason, errorURL, errorStatus, errorMessage);
193 ApplicationCacheHost::CacheInfo ApplicationCacheHost::applicationCacheInfo()
196 return CacheInfo(KURL(), 0, 0, 0);
198 WebApplicationCacheHost::CacheInfo webInfo;
199 m_host->getAssociatedCacheInfo(&webInfo);
200 return CacheInfo(webInfo.manifestURL, webInfo.creationTime, webInfo.updateTime, webInfo.totalSize);
203 void ApplicationCacheHost::fillResourceList(ResourceInfoList* resources)
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));
217 void ApplicationCacheHost::stopDeferringEvents()
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);
224 m_deferredEvents.clear();
225 m_defersEvents = false;
228 void ApplicationCacheHost::dispatchDOMEvent(EventID id, int progressTotal, int progressDone, WebApplicationCacheHost::ErrorReason errorReason, const String& errorURL, int errorStatus, const String& errorMessage)
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);
238 event = Event::create(eventType);
239 m_domApplicationCache->dispatchEvent(event, ASSERT_NO_EXCEPTION);
243 ApplicationCacheHost::Status ApplicationCacheHost::status() const
245 return m_host ? static_cast<Status>(m_host->status()) : UNCACHED;
248 bool ApplicationCacheHost::update()
250 return m_host ? m_host->startUpdate() : false;
253 bool ApplicationCacheHost::swapCache()
255 bool success = m_host ? m_host->swapCache() : false;
257 InspectorInstrumentation::updateApplicationCacheStatus(m_documentLoader->frame());
261 void ApplicationCacheHost::abort()
267 bool ApplicationCacheHost::isApplicationCacheEnabled()
269 ASSERT(m_documentLoader->frame());
270 return m_documentLoader->frame()->settings() && m_documentLoader->frame()->settings()->offlineWebApplicationCacheEnabled();
273 void ApplicationCacheHost::didChangeCacheAssociation()
275 // FIXME: Prod the inspector to update its notion of what cache the page is using.
278 void ApplicationCacheHost::notifyEventListener(WebApplicationCacheHost::EventID eventID)
280 notifyApplicationCache(static_cast<ApplicationCacheHost::EventID>(eventID), 0, 0, WebApplicationCacheHost::UnknownError, String(), 0, String());
283 void ApplicationCacheHost::notifyProgressEventListener(const WebURL&, int progressTotal, int progressDone)
285 notifyApplicationCache(PROGRESS_EVENT, progressTotal, progressDone, WebApplicationCacheHost::UnknownError, String(), 0, String());
288 void ApplicationCacheHost::notifyErrorEventListener(WebApplicationCacheHost::ErrorReason reason, const WebURL& url, int status, const WebString& message)
290 notifyApplicationCache(ERROR_EVENT, 0, 0, reason, url.string(), status, message);
293 void ApplicationCacheHost::trace(Visitor* visitor)
295 visitor->trace(m_domApplicationCache);