initial import
[vuplus_webkit] / Source / WebKit / chromium / src / AssociatedURLLoader.cpp
1 /*
2  * Copyright (C) 2010, 2011 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 "AssociatedURLLoader.h"
33
34 #include "DocumentThreadableLoader.h"
35 #include "DocumentThreadableLoaderClient.h"
36 #include "SubresourceLoader.h"
37 #include "Timer.h"
38 #include "WebApplicationCacheHost.h"
39 #include "WebDataSource.h"
40 #include "WebFrameImpl.h"
41 #include "WebKit.h"
42 #include "WebKitPlatformSupport.h"
43 #include "WebURLError.h"
44 #include "WebURLLoaderClient.h"
45 #include "WebURLRequest.h"
46 #include "WrappedResourceRequest.h"
47 #include "WrappedResourceResponse.h"
48
49 using namespace WebCore;
50 using namespace WebKit;
51 using namespace WTF;
52
53 namespace WebKit {
54
55 // This class bridges the interface differences between WebCore and WebKit loader clients.
56 // It forwards its ThreadableLoaderClient notifications to a WebURLLoaderClient.
57 class AssociatedURLLoader::ClientAdapter : public DocumentThreadableLoaderClient {
58     WTF_MAKE_NONCOPYABLE(ClientAdapter);
59 public:
60     static PassOwnPtr<ClientAdapter> create(AssociatedURLLoader*, WebURLLoaderClient*, bool /*downloadToFile*/);
61
62     virtual void didSendData(unsigned long long /*bytesSent*/, unsigned long long /*totalBytesToBeSent*/);
63     virtual void willSendRequest(ResourceRequest& /*newRequest*/, const ResourceResponse& /*redirectResponse*/);
64
65     virtual void didReceiveResponse(unsigned long, const ResourceResponse&);
66     virtual void didDownloadData(int /*dataLength*/);
67     virtual void didReceiveData(const char*, int /*dataLength*/);
68     virtual void didReceiveCachedMetadata(const char*, int /*dataLength*/);
69     virtual void didFinishLoading(unsigned long /*identifier*/, double /*finishTime*/);
70     virtual void didFail(const ResourceError&);
71
72     virtual bool isDocumentThreadableLoaderClient() { return true; }
73
74     // Enables forwarding of error notifications to the WebURLLoaderClient. These must be
75     // deferred until after the call to AssociatedURLLoader::loadAsynchronously() completes.
76     void enableErrorNotifications();
77
78     // Stops loading and releases the DocumentThreadableLoader as early as possible.
79     void clearClient() { m_client = 0; } 
80
81 private:
82     ClientAdapter(AssociatedURLLoader*, WebURLLoaderClient*, bool /*downloadToFile*/);
83
84     void notifyError(Timer<ClientAdapter>*);
85
86     AssociatedURLLoader* m_loader;
87     WebURLLoaderClient* m_client;
88     WebURLError m_error;
89
90     Timer<ClientAdapter> m_errorTimer;
91     bool m_downloadToFile;
92     bool m_enableErrorNotifications;
93     bool m_didFail;
94 };
95
96 PassOwnPtr<AssociatedURLLoader::ClientAdapter> AssociatedURLLoader::ClientAdapter::create(AssociatedURLLoader* loader, WebURLLoaderClient* client, bool downloadToFile)
97 {
98     return adoptPtr(new ClientAdapter(loader, client, downloadToFile));
99 }
100
101 AssociatedURLLoader::ClientAdapter::ClientAdapter(AssociatedURLLoader* loader, WebURLLoaderClient* client, bool downloadToFile)
102     : m_loader(loader)
103     , m_client(client)
104     , m_errorTimer(this, &ClientAdapter::notifyError)
105     , m_downloadToFile(downloadToFile)
106     , m_enableErrorNotifications(false)
107     , m_didFail(false)
108 {
109     ASSERT(m_loader);
110     ASSERT(m_client);
111 }
112
113 void AssociatedURLLoader::ClientAdapter::willSendRequest(ResourceRequest& newRequest, const ResourceResponse& redirectResponse)
114 {
115     if (!m_client)
116         return;
117
118     WrappedResourceRequest wrappedNewRequest(newRequest);
119     WrappedResourceResponse wrappedRedirectResponse(redirectResponse);
120     m_client->willSendRequest(m_loader, wrappedNewRequest, wrappedRedirectResponse);
121 }
122
123 void AssociatedURLLoader::ClientAdapter::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
124 {
125     if (!m_client)
126         return;
127
128     m_client->didSendData(m_loader, bytesSent, totalBytesToBeSent);
129 }
130
131 void AssociatedURLLoader::ClientAdapter::didReceiveResponse(unsigned long, const ResourceResponse& response)
132 {
133     WrappedResourceResponse wrappedResponse(response);
134     m_client->didReceiveResponse(m_loader, wrappedResponse);
135 }
136
137 void AssociatedURLLoader::ClientAdapter::didDownloadData(int dataLength)
138 {
139     if (!m_client)
140         return;
141
142     m_client->didDownloadData(m_loader, dataLength);
143 }
144
145 void AssociatedURLLoader::ClientAdapter::didReceiveData(const char* data, int dataLength)
146 {
147     if (!m_client)
148         return;
149
150     m_client->didReceiveData(m_loader, data, dataLength, -1);
151 }
152
153 void AssociatedURLLoader::ClientAdapter::didReceiveCachedMetadata(const char* data, int dataLength)
154 {
155     if (!m_client)
156         return;
157
158     m_client->didReceiveCachedMetadata(m_loader, data, dataLength);
159 }
160
161 void AssociatedURLLoader::ClientAdapter::didFinishLoading(unsigned long identifier, double finishTime)
162 {
163     if (!m_client)
164         return;
165
166     m_client->didFinishLoading(m_loader, finishTime);
167 }
168
169 void AssociatedURLLoader::ClientAdapter::didFail(const ResourceError& error)
170 {
171     if (!m_client)
172         return;
173
174     m_didFail = true;
175     m_error = WebURLError(error);
176     if (m_enableErrorNotifications)
177         notifyError(&m_errorTimer);
178 }
179
180 void AssociatedURLLoader::ClientAdapter::enableErrorNotifications()
181 {
182     m_enableErrorNotifications = true;
183     // If an error has already been received, start a timer to report it to the client
184     // after AssociatedURLLoader::loadAsynchronously has returned to the caller.
185     if (m_didFail)
186         m_errorTimer.startOneShot(0);
187 }
188
189 void AssociatedURLLoader::ClientAdapter::notifyError(Timer<ClientAdapter>* timer)
190 {
191     ASSERT_UNUSED(timer, timer == &m_errorTimer);
192
193     m_client->didFail(m_loader, m_error);
194 }
195
196 AssociatedURLLoader::AssociatedURLLoader(PassRefPtr<WebFrameImpl> frameImpl, const WebURLLoaderOptions& options)
197     : m_frameImpl(frameImpl)
198     , m_options(options)
199     , m_client(0)
200 {
201     ASSERT(m_frameImpl);
202 }
203
204 AssociatedURLLoader::~AssociatedURLLoader()
205 {
206     cancel();
207 }
208
209 #define COMPILE_ASSERT_MATCHING_ENUM(webkit_name, webcore_name) \
210     COMPILE_ASSERT(static_cast<int>(WebKit::webkit_name) == static_cast<int>(WebCore::webcore_name), mismatching_enums)
211
212 COMPILE_ASSERT_MATCHING_ENUM(WebURLLoaderOptions::CrossOriginRequestPolicyDeny, DenyCrossOriginRequests);
213 COMPILE_ASSERT_MATCHING_ENUM(WebURLLoaderOptions::CrossOriginRequestPolicyUseAccessControl, UseAccessControl);
214 COMPILE_ASSERT_MATCHING_ENUM(WebURLLoaderOptions::CrossOriginRequestPolicyAllow, AllowCrossOriginRequests);
215
216 void AssociatedURLLoader::loadSynchronously(const WebURLRequest& request, WebURLResponse& response, WebURLError& error, WebData& data)
217 {
218     ASSERT(0); // Synchronous loading is not supported.
219 }
220
221 void AssociatedURLLoader::loadAsynchronously(const WebURLRequest& request, WebURLLoaderClient* client)
222 {
223     ASSERT(!m_client);
224
225     m_client = client;
226     ASSERT(m_client);
227
228     ThreadableLoaderOptions options;
229     options.sendLoadCallbacks = SendCallbacks; // Always send callbacks.
230     options.sniffContent = m_options.sniffContent ? SniffContent : DoNotSniffContent;
231     options.allowCredentials = m_options.allowCredentials ? AllowStoredCredentials : DoNotAllowStoredCredentials;
232     options.preflightPolicy = m_options.forcePreflight ? ForcePreflight : ConsiderPreflight;
233     options.crossOriginRequestPolicy = static_cast<WebCore::CrossOriginRequestPolicy>(m_options.crossOriginRequestPolicy);
234     options.shouldBufferData = DoNotBufferData;
235
236     const ResourceRequest& webcoreRequest = request.toResourceRequest();
237     Document* webcoreDocument = m_frameImpl->frame()->document();
238     m_clientAdapter = ClientAdapter::create(this, m_client, request.downloadToFile());
239     m_loader = DocumentThreadableLoader::create(webcoreDocument, m_clientAdapter.get(), webcoreRequest, options);
240     m_clientAdapter->enableErrorNotifications();
241 }
242
243 void AssociatedURLLoader::cancel()
244 {
245     if (m_clientAdapter)
246         m_clientAdapter->clearClient();
247     if (m_loader)
248         m_loader->cancel();
249 }
250
251 void AssociatedURLLoader::setDefersLoading(bool defersLoading)
252 {
253     if (m_loader)
254         m_loader->setDefersLoading(defersLoading);
255 }
256
257 } // namespace WebKit