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/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(0)
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 LocalFrame* spawningFrame = frame.tree().parent();
93 spawningFrame = frame.loader().opener();
95 spawningFrame = &frame;
96 if (DocumentLoader* spawningDocLoader = 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 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()));
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::notifyApplicationCache(EventID id, int progressTotal, int progressDone, blink::WebApplicationCacheHost::ErrorReason errorReason, const String& errorURL, int errorStatus, const String& errorMessage)
169 if (id != PROGRESS_EVENT)
170 InspectorInstrumentation::updateApplicationCacheStatus(m_documentLoader->frame());
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));
177 dispatchDOMEvent(id, progressTotal, progressDone, errorReason, errorURL, errorStatus, errorMessage);
180 ApplicationCacheHost::CacheInfo ApplicationCacheHost::applicationCacheInfo()
183 return CacheInfo(KURL(), 0, 0, 0);
185 blink::WebApplicationCacheHost::CacheInfo webInfo;
186 m_host->getAssociatedCacheInfo(&webInfo);
187 return CacheInfo(webInfo.manifestURL, webInfo.creationTime, webInfo.updateTime, webInfo.totalSize);
190 void ApplicationCacheHost::fillResourceList(ResourceInfoList* resources)
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));
204 void ApplicationCacheHost::stopDeferringEvents()
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);
211 m_deferredEvents.clear();
212 m_defersEvents = false;
215 void ApplicationCacheHost::dispatchDOMEvent(EventID id, int progressTotal, int progressDone, blink::WebApplicationCacheHost::ErrorReason errorReason, const String& errorURL, int errorStatus, const String& errorMessage)
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);
225 event = Event::create(eventType);
226 m_domApplicationCache->dispatchEvent(event, ASSERT_NO_EXCEPTION);
230 ApplicationCacheHost::Status ApplicationCacheHost::status() const
232 return m_host ? static_cast<Status>(m_host->status()) : UNCACHED;
235 bool ApplicationCacheHost::update()
237 return m_host ? m_host->startUpdate() : false;
240 bool ApplicationCacheHost::swapCache()
242 bool success = m_host ? m_host->swapCache() : false;
244 InspectorInstrumentation::updateApplicationCacheStatus(m_documentLoader->frame());
248 void ApplicationCacheHost::abort()
254 bool ApplicationCacheHost::isApplicationCacheEnabled()
256 ASSERT(m_documentLoader->frame());
257 return m_documentLoader->frame()->settings() && m_documentLoader->frame()->settings()->offlineWebApplicationCacheEnabled();
260 void ApplicationCacheHost::didChangeCacheAssociation()
262 // FIXME: Prod the inspector to update its notion of what cache the page is using.
265 void ApplicationCacheHost::notifyEventListener(blink::WebApplicationCacheHost::EventID eventID)
267 notifyApplicationCache(static_cast<ApplicationCacheHost::EventID>(eventID), 0, 0, blink::WebApplicationCacheHost::UnknownError, String(), 0, String());
270 void ApplicationCacheHost::notifyProgressEventListener(const blink::WebURL&, int progressTotal, int progressDone)
272 notifyApplicationCache(PROGRESS_EVENT, progressTotal, progressDone, blink::WebApplicationCacheHost::UnknownError, String(), 0, String());
275 void ApplicationCacheHost::notifyErrorEventListener(blink::WebApplicationCacheHost::ErrorReason reason, const blink::WebURL& url, int status, const blink::WebString& message)
277 notifyApplicationCache(ERROR_EVENT, 0, 0, reason, url.string(), status, message);
280 } // namespace WebCore