2 * Copyright (C) 2010, 2011 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 "AssociatedURLLoader.h"
34 #include "DocumentThreadableLoader.h"
35 #include "DocumentThreadableLoaderClient.h"
36 #include "SubresourceLoader.h"
38 #include "WebApplicationCacheHost.h"
39 #include "WebDataSource.h"
40 #include "WebFrameImpl.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"
49 using namespace WebCore;
50 using namespace WebKit;
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);
60 static PassOwnPtr<ClientAdapter> create(AssociatedURLLoader*, WebURLLoaderClient*, bool /*downloadToFile*/);
62 virtual void didSendData(unsigned long long /*bytesSent*/, unsigned long long /*totalBytesToBeSent*/);
63 virtual void willSendRequest(ResourceRequest& /*newRequest*/, const ResourceResponse& /*redirectResponse*/);
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&);
72 virtual bool isDocumentThreadableLoaderClient() { return true; }
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();
78 // Stops loading and releases the DocumentThreadableLoader as early as possible.
79 void clearClient() { m_client = 0; }
82 ClientAdapter(AssociatedURLLoader*, WebURLLoaderClient*, bool /*downloadToFile*/);
84 void notifyError(Timer<ClientAdapter>*);
86 AssociatedURLLoader* m_loader;
87 WebURLLoaderClient* m_client;
90 Timer<ClientAdapter> m_errorTimer;
91 bool m_downloadToFile;
92 bool m_enableErrorNotifications;
96 PassOwnPtr<AssociatedURLLoader::ClientAdapter> AssociatedURLLoader::ClientAdapter::create(AssociatedURLLoader* loader, WebURLLoaderClient* client, bool downloadToFile)
98 return adoptPtr(new ClientAdapter(loader, client, downloadToFile));
101 AssociatedURLLoader::ClientAdapter::ClientAdapter(AssociatedURLLoader* loader, WebURLLoaderClient* client, bool downloadToFile)
104 , m_errorTimer(this, &ClientAdapter::notifyError)
105 , m_downloadToFile(downloadToFile)
106 , m_enableErrorNotifications(false)
113 void AssociatedURLLoader::ClientAdapter::willSendRequest(ResourceRequest& newRequest, const ResourceResponse& redirectResponse)
118 WrappedResourceRequest wrappedNewRequest(newRequest);
119 WrappedResourceResponse wrappedRedirectResponse(redirectResponse);
120 m_client->willSendRequest(m_loader, wrappedNewRequest, wrappedRedirectResponse);
123 void AssociatedURLLoader::ClientAdapter::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
128 m_client->didSendData(m_loader, bytesSent, totalBytesToBeSent);
131 void AssociatedURLLoader::ClientAdapter::didReceiveResponse(unsigned long, const ResourceResponse& response)
133 WrappedResourceResponse wrappedResponse(response);
134 m_client->didReceiveResponse(m_loader, wrappedResponse);
137 void AssociatedURLLoader::ClientAdapter::didDownloadData(int dataLength)
142 m_client->didDownloadData(m_loader, dataLength);
145 void AssociatedURLLoader::ClientAdapter::didReceiveData(const char* data, int dataLength)
150 m_client->didReceiveData(m_loader, data, dataLength, -1);
153 void AssociatedURLLoader::ClientAdapter::didReceiveCachedMetadata(const char* data, int dataLength)
158 m_client->didReceiveCachedMetadata(m_loader, data, dataLength);
161 void AssociatedURLLoader::ClientAdapter::didFinishLoading(unsigned long identifier, double finishTime)
166 m_client->didFinishLoading(m_loader, finishTime);
169 void AssociatedURLLoader::ClientAdapter::didFail(const ResourceError& error)
175 m_error = WebURLError(error);
176 if (m_enableErrorNotifications)
177 notifyError(&m_errorTimer);
180 void AssociatedURLLoader::ClientAdapter::enableErrorNotifications()
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.
186 m_errorTimer.startOneShot(0);
189 void AssociatedURLLoader::ClientAdapter::notifyError(Timer<ClientAdapter>* timer)
191 ASSERT_UNUSED(timer, timer == &m_errorTimer);
193 m_client->didFail(m_loader, m_error);
196 AssociatedURLLoader::AssociatedURLLoader(PassRefPtr<WebFrameImpl> frameImpl, const WebURLLoaderOptions& options)
197 : m_frameImpl(frameImpl)
204 AssociatedURLLoader::~AssociatedURLLoader()
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)
212 COMPILE_ASSERT_MATCHING_ENUM(WebURLLoaderOptions::CrossOriginRequestPolicyDeny, DenyCrossOriginRequests);
213 COMPILE_ASSERT_MATCHING_ENUM(WebURLLoaderOptions::CrossOriginRequestPolicyUseAccessControl, UseAccessControl);
214 COMPILE_ASSERT_MATCHING_ENUM(WebURLLoaderOptions::CrossOriginRequestPolicyAllow, AllowCrossOriginRequests);
216 void AssociatedURLLoader::loadSynchronously(const WebURLRequest& request, WebURLResponse& response, WebURLError& error, WebData& data)
218 ASSERT(0); // Synchronous loading is not supported.
221 void AssociatedURLLoader::loadAsynchronously(const WebURLRequest& request, WebURLLoaderClient* client)
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;
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();
243 void AssociatedURLLoader::cancel()
246 m_clientAdapter->clearClient();
251 void AssociatedURLLoader::setDefersLoading(bool defersLoading)
254 m_loader->setDefersLoading(defersLoading);
257 } // namespace WebKit