tizen beta release
[framework/web/webkit-efl.git] / Source / WebKit / chromium / src / ApplicationCacheHost.cpp
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 "ApplicationCacheHost.h"
33
34 #include "ApplicationCacheHostInternal.h"
35 #include "DocumentLoader.h"
36 #include "DOMApplicationCache.h"
37 #include "Frame.h"
38 #include "InspectorApplicationCacheAgent.h"
39 #include "InspectorInstrumentation.h"
40 #include "Page.h"
41 #include "ProgressEvent.h"
42 #include "SecurityOrigin.h"
43 #include "Settings.h"
44 #include "WebFrameImpl.h"
45 #include "WebURL.h"
46 #include "WebURLError.h"
47 #include "WebURLResponse.h"
48 #include "WebVector.h"
49 #include "WrappedResourceRequest.h"
50 #include "WrappedResourceResponse.h"
51
52 using namespace WebKit;
53
54 namespace WebCore {
55
56 // We provide a custom implementation of this class that calls out to the
57 // embedding application instead of using WebCore's built in appcache system.
58 // This file replaces webcore/appcache/ApplicationCacheHost.cpp in our build.
59
60 ApplicationCacheHost::ApplicationCacheHost(DocumentLoader* documentLoader)
61     : m_domApplicationCache(0)
62     , m_documentLoader(documentLoader)
63     , m_defersEvents(true)
64 {
65     ASSERT(m_documentLoader);
66 }
67
68 ApplicationCacheHost::~ApplicationCacheHost()
69 {
70 }
71
72 void ApplicationCacheHost::maybeLoadMainResource(ResourceRequest& request, SubstituteData&)
73 {
74     // We defer creating the outer host object to avoid spurious creation/destruction
75     // around creating empty documents. At this point, we're initiating a main resource
76     // load for the document, so its for real.
77
78     if (!isApplicationCacheEnabled())
79         return;
80
81     m_internal = adoptPtr(new ApplicationCacheHostInternal(this));
82     if (m_internal->m_outerHost) {
83         WrappedResourceRequest wrapped(request);
84         m_internal->m_outerHost->willStartMainResourceRequest(wrapped, WebFrameImpl::fromFrame(m_documentLoader->frame()));
85     } else
86         m_internal.clear();
87
88     // NOTE: The semantics of this method, and others in this interface, are subtly different
89     // than the method names would suggest. For example, in this method never returns an appcached
90     // response in the SubstituteData out argument, instead we return the appcached response thru
91     // the usual resource loading pipeline.
92 }
93
94 void ApplicationCacheHost::selectCacheWithoutManifest()
95 {
96     if (m_internal)
97         m_internal->m_outerHost->selectCacheWithoutManifest();
98 }
99
100 void ApplicationCacheHost::selectCacheWithManifest(const KURL& manifestURL)
101 {
102     if (m_internal) {
103         if (!m_internal->m_outerHost->selectCacheWithManifest(manifestURL)) {
104             // It's a foreign entry, restart the current navigation from the top
105             // of the navigation algorithm. The navigation will not result in the
106             // same resource being loaded, because "foreign" entries are never picked
107             // during navigation.
108             // see WebCore::ApplicationCacheGroup::selectCache()
109             Frame* frame = m_documentLoader->frame();
110             frame->navigationScheduler()->scheduleLocationChange(frame->document()->securityOrigin(),
111                 frame->document()->url(), frame->loader()->referrer());
112         }
113     }
114 }
115
116 void ApplicationCacheHost::maybeLoadMainResourceForRedirect(ResourceRequest&, SubstituteData&)
117 {
118     // N/A to the chromium port
119 }
120
121 bool ApplicationCacheHost::maybeLoadFallbackForMainResponse(const ResourceRequest&, const ResourceResponse& response)
122 {
123     if (m_internal) {
124         WrappedResourceResponse wrapped(response);
125         m_internal->m_outerHost->didReceiveResponseForMainResource(wrapped);
126     }
127     return false;
128 }
129
130 bool ApplicationCacheHost::maybeLoadFallbackForMainError(const ResourceRequest&, const ResourceError& error)
131 {
132     // N/A to the chromium port
133     return false;
134 }
135
136 void ApplicationCacheHost::mainResourceDataReceived(const char* data, int length, long long, bool)
137 {
138     if (m_internal)
139         m_internal->m_outerHost->didReceiveDataForMainResource(data, length);
140 }
141
142 void ApplicationCacheHost::failedLoadingMainResource()
143 {
144     if (m_internal)
145         m_internal->m_outerHost->didFinishLoadingMainResource(false);
146 }
147
148 void ApplicationCacheHost::finishedLoadingMainResource()
149 {
150     if (m_internal)
151         m_internal->m_outerHost->didFinishLoadingMainResource(true);
152 }
153
154 bool ApplicationCacheHost::maybeLoadResource(ResourceLoader*, ResourceRequest& request, const KURL&)
155 {
156     // FIXME: look into the purpose of the unused KURL& originalURL parameter
157     if (m_internal) {
158         WrappedResourceRequest wrapped(request);
159         m_internal->m_outerHost->willStartSubResourceRequest(wrapped);
160     }
161     return false;
162 }
163
164 bool ApplicationCacheHost::maybeLoadFallbackForRedirect(ResourceLoader*, ResourceRequest&, const ResourceResponse&)
165 {
166     // N/A to the chromium port
167     return false;
168 }
169
170 bool ApplicationCacheHost::maybeLoadFallbackForResponse(ResourceLoader*, const ResourceResponse&)
171 {
172     // N/A to the chromium port
173     return false;
174 }
175
176 bool ApplicationCacheHost::maybeLoadFallbackForError(ResourceLoader*, const ResourceError&)
177 {
178     // N/A to the chromium port
179     return false;
180 }
181
182 bool ApplicationCacheHost::maybeLoadSynchronously(ResourceRequest& request, ResourceError&, ResourceResponse&, Vector<char>&)
183 {
184     if (m_internal) {
185         WrappedResourceRequest wrapped(request);
186         m_internal->m_outerHost->willStartSubResourceRequest(wrapped);
187     }
188     return false;
189 }
190
191 void ApplicationCacheHost::maybeLoadFallbackSynchronously(const ResourceRequest&, ResourceError&, ResourceResponse&, Vector<char>&)
192 {
193     // N/A to the chromium port
194 }
195
196 bool ApplicationCacheHost::canCacheInPageCache() const
197 {
198     // N/A to the chromium port which doesn't use the page cache.
199     return false;
200 }
201
202 void ApplicationCacheHost::setDOMApplicationCache(DOMApplicationCache* domApplicationCache)
203 {
204     ASSERT(!m_domApplicationCache || !domApplicationCache);
205     m_domApplicationCache = domApplicationCache;
206 }
207
208 void ApplicationCacheHost::notifyDOMApplicationCache(EventID id, int total, int done)
209 {
210 #if ENABLE(INSPECTOR)
211     if (id != PROGRESS_EVENT)
212         InspectorInstrumentation::updateApplicationCacheStatus(m_documentLoader->frame());
213 #endif
214
215     if (m_defersEvents) {
216         // Event dispatching is deferred until document.onload has fired.
217         m_deferredEvents.append(DeferredEvent(id, total, done));
218         return;
219     }
220     dispatchDOMEvent(id, total, done);
221 }
222
223 #if ENABLE(INSPECTOR)
224 ApplicationCacheHost::CacheInfo ApplicationCacheHost::applicationCacheInfo()
225 {
226     if (!m_internal)
227         return CacheInfo(KURL(), 0, 0, 0);
228
229     WebKit::WebApplicationCacheHost::CacheInfo webInfo;
230     m_internal->m_outerHost->getAssociatedCacheInfo(&webInfo);
231     return CacheInfo(webInfo.manifestURL, webInfo.creationTime, webInfo.updateTime, webInfo.totalSize);
232 }
233
234 void ApplicationCacheHost::fillResourceList(ResourceInfoList* resources)
235 {
236     if (!m_internal)
237         return;
238
239     WebKit::WebVector<WebKit::WebApplicationCacheHost::ResourceInfo> webResources;
240     m_internal->m_outerHost->getResourceList(&webResources);
241     for (size_t i = 0; i < webResources.size(); ++i) {
242         resources->append(ResourceInfo(
243             webResources[i].url, webResources[i].isMaster, webResources[i].isManifest, webResources[i].isFallback,
244             webResources[i].isForeign, webResources[i].isExplicit, webResources[i].size));
245     }
246 }
247 #endif
248
249 void ApplicationCacheHost::stopDeferringEvents()
250 {
251     RefPtr<DocumentLoader> protect(documentLoader());
252     for (unsigned i = 0; i < m_deferredEvents.size(); ++i) {
253         const DeferredEvent& deferred = m_deferredEvents[i];
254         dispatchDOMEvent(deferred.eventID, deferred.progressTotal, deferred.progressDone);
255     }
256     m_deferredEvents.clear();
257     m_defersEvents = false;
258 }
259
260 void ApplicationCacheHost::stopLoadingInFrame(Frame* frame)
261 {
262     // N/A to the chromium port
263 }
264
265 void ApplicationCacheHost::dispatchDOMEvent(EventID id, int total, int done)
266 {
267     if (m_domApplicationCache) {
268         const AtomicString& eventType = DOMApplicationCache::toEventType(id);
269         ExceptionCode ec = 0;
270         RefPtr<Event> event;
271         if (id == PROGRESS_EVENT)
272             event = ProgressEvent::create(eventType, true, done, total);
273         else
274             event = Event::create(eventType, false, false);
275         m_domApplicationCache->dispatchEvent(event, ec);
276         ASSERT(!ec);
277     }
278 }
279
280 ApplicationCacheHost::Status ApplicationCacheHost::status() const
281 {
282     return m_internal ? static_cast<Status>(m_internal->m_outerHost->status()) : UNCACHED;
283 }
284
285 bool ApplicationCacheHost::update()
286 {
287     return m_internal ? m_internal->m_outerHost->startUpdate() : false;
288 }
289
290 bool ApplicationCacheHost::swapCache()
291 {
292     bool success = m_internal ? m_internal->m_outerHost->swapCache() : false;
293     if (success)
294         InspectorInstrumentation::updateApplicationCacheStatus(m_documentLoader->frame());
295     return success;
296 }
297
298 bool ApplicationCacheHost::isApplicationCacheEnabled()
299 {
300     ASSERT(m_documentLoader->frame());
301     return m_documentLoader->frame()->settings()
302            && m_documentLoader->frame()->settings()->offlineWebApplicationCacheEnabled();
303 }
304
305 }  // namespace WebCore