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 "WebApplicationCacheHost.h"
38#include "WebDataSource.h"
39#include "WebFrameImpl.h"
40#include "WebKit.h"
41#include "WebKitClient.h"
42#include "WebURLError.h"
43#include "WebURLLoaderClient.h"
44#include "WebURLRequest.h"
45#include "WrappedResourceRequest.h"
46#include "WrappedResourceResponse.h"
47
48using namespace WebCore;
49using namespace WebKit;
50using namespace WTF;
51
52namespace WebKit {
53
54// This class bridges the interface differences between WebCore and WebKit loader clients.
55// It forwards its ThreadableLoaderClient notifications to a WebURLLoaderClient.
56class AssociatedURLLoader::ClientAdapter : public DocumentThreadableLoaderClient {
57public:
58    static PassOwnPtr<ClientAdapter> create(AssociatedURLLoader*, WebURLLoaderClient*, bool /*downloadToFile*/);
59
60    virtual void didSendData(unsigned long long /*bytesSent*/, unsigned long long /*totalBytesToBeSent*/);
61    virtual void willSendRequest(ResourceRequest& /*newRequest*/, const ResourceResponse& /*redirectResponse*/);
62
63    virtual void didReceiveResponse(const ResourceResponse&);
64    virtual void didReceiveData(const char*, int /*dataLength*/);
65    virtual void didReceiveCachedMetadata(const char*, int /*dataLength*/);
66    virtual void didFinishLoading(unsigned long /*identifier*/, double /*finishTime*/);
67    virtual void didFail(const ResourceError&);
68
69    virtual bool isDocumentThreadableLoaderClient() { return true; }
70
71    // This method stops loading and releases the DocumentThreadableLoader as early as possible.
72    void clearClient() { m_client = 0; }
73
74private:
75    ClientAdapter(AssociatedURLLoader*, WebURLLoaderClient*, bool /*downloadToFile*/);
76
77    AssociatedURLLoader* m_loader;
78    WebURLLoaderClient* m_client;
79    unsigned long m_downloadLength;
80    bool m_downloadToFile;
81};
82
83PassOwnPtr<AssociatedURLLoader::ClientAdapter> AssociatedURLLoader::ClientAdapter::create(AssociatedURLLoader* loader, WebURLLoaderClient* client, bool downloadToFile)
84{
85    return adoptPtr(new ClientAdapter(loader, client, downloadToFile));
86}
87
88AssociatedURLLoader::ClientAdapter::ClientAdapter(AssociatedURLLoader* loader, WebURLLoaderClient* client, bool downloadToFile)
89    : m_loader(loader)
90    , m_client(client)
91    , m_downloadLength(0)
92    , m_downloadToFile(downloadToFile)
93{
94    ASSERT(m_loader);
95    ASSERT(m_client);
96}
97
98void AssociatedURLLoader::ClientAdapter::willSendRequest(ResourceRequest& newRequest, const ResourceResponse& redirectResponse)
99{
100    if (!m_client)
101        return;
102
103    WrappedResourceRequest wrappedNewRequest(newRequest);
104    WrappedResourceResponse wrappedRedirectResponse(redirectResponse);
105    m_client->willSendRequest(m_loader, wrappedNewRequest, wrappedRedirectResponse);
106}
107
108void AssociatedURLLoader::ClientAdapter::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
109{
110    if (!m_client)
111        return;
112
113    m_client->didSendData(m_loader, bytesSent, totalBytesToBeSent);
114}
115
116void AssociatedURLLoader::ClientAdapter::didReceiveResponse(const ResourceResponse& response)
117{
118    WrappedResourceResponse wrappedResponse(response);
119    m_client->didReceiveResponse(m_loader, wrappedResponse);
120}
121
122void AssociatedURLLoader::ClientAdapter::didReceiveData(const char* data, int dataLength)
123{
124    if (!m_client)
125        return;
126
127    m_client->didReceiveData(m_loader, data, dataLength, -1);
128    m_downloadLength += dataLength;
129}
130
131void AssociatedURLLoader::ClientAdapter::didReceiveCachedMetadata(const char* data, int dataLength)
132{
133    if (!m_client)
134        return;
135
136    m_client->didReceiveCachedMetadata(m_loader, data, dataLength);
137}
138
139void AssociatedURLLoader::ClientAdapter::didFinishLoading(unsigned long identifier, double finishTime)
140{
141    if (!m_client)
142        return;
143
144    if (m_downloadToFile) {
145        int downloadLength = m_downloadLength <= INT_MAX ? m_downloadLength : INT_MAX;
146        m_client->didDownloadData(m_loader, downloadLength);
147        // While the client could have cancelled, continue, since the load finished.
148    }
149
150    m_client->didFinishLoading(m_loader, finishTime);
151}
152
153void AssociatedURLLoader::ClientAdapter::didFail(const ResourceError& error)
154{
155    if (!m_client)
156        return;
157
158    WebURLError webError(error);
159    m_client->didFail(m_loader, webError);
160}
161
162AssociatedURLLoader::AssociatedURLLoader(PassRefPtr<WebFrameImpl> frameImpl)
163    : m_frameImpl(frameImpl)
164    , m_client(0)
165{
166    ASSERT(m_frameImpl);
167
168    m_options.sniffContent = false;
169    m_options.allowCredentials = true;
170    m_options.forcePreflight = false;
171    m_options.crossOriginRequestPolicy = WebURLLoaderOptions::CrossOriginRequestPolicyAllow; // FIXME We should deny by default, but this would break some tests.
172}
173
174AssociatedURLLoader::AssociatedURLLoader(PassRefPtr<WebFrameImpl> frameImpl, const WebURLLoaderOptions& options)
175    : m_frameImpl(frameImpl)
176    , m_options(options)
177    , m_client(0)
178{
179    ASSERT(m_frameImpl);
180}
181
182AssociatedURLLoader::~AssociatedURLLoader()
183{
184    if (m_clientAdapter)
185        m_clientAdapter->clearClient();
186}
187
188#define COMPILE_ASSERT_MATCHING_ENUM(webkit_name, webcore_name) \
189    COMPILE_ASSERT(static_cast<int>(WebKit::webkit_name) == static_cast<int>(WebCore::webcore_name), mismatching_enums)
190
191COMPILE_ASSERT_MATCHING_ENUM(WebURLLoaderOptions::CrossOriginRequestPolicyDeny, DenyCrossOriginRequests);
192COMPILE_ASSERT_MATCHING_ENUM(WebURLLoaderOptions::CrossOriginRequestPolicyUseAccessControl, UseAccessControl);
193COMPILE_ASSERT_MATCHING_ENUM(WebURLLoaderOptions::CrossOriginRequestPolicyAllow, AllowCrossOriginRequests);
194
195void AssociatedURLLoader::loadSynchronously(const WebURLRequest& request, WebURLResponse& response, WebURLError& error, WebData& data)
196{
197    ASSERT(0); // Synchronous loading is not supported.
198}
199
200void AssociatedURLLoader::loadAsynchronously(const WebURLRequest& request, WebURLLoaderClient* client)
201{
202    ASSERT(!m_client);
203
204    m_client = client;
205    ASSERT(m_client);
206
207    ThreadableLoaderOptions options;
208    options.sendLoadCallbacks = true; // Always send callbacks.
209    options.sniffContent = m_options.sniffContent;
210    options.allowCredentials = m_options.allowCredentials;
211    options.forcePreflight = m_options.forcePreflight;
212    options.crossOriginRequestPolicy = static_cast<WebCore::CrossOriginRequestPolicy>(m_options.crossOriginRequestPolicy);
213    options.shouldBufferData = false;
214
215    const ResourceRequest& webcoreRequest = request.toResourceRequest();
216    Document* webcoreDocument = m_frameImpl->frame()->document();
217    m_clientAdapter = ClientAdapter::create(this, m_client, request.downloadToFile());
218
219    m_loader = DocumentThreadableLoader::create(webcoreDocument, m_clientAdapter.get(), webcoreRequest, options);
220}
221
222void AssociatedURLLoader::cancel()
223{
224    if (m_loader) {
225        m_clientAdapter->clearClient();
226        m_loader->cancel();
227    }
228}
229
230void AssociatedURLLoader::setDefersLoading(bool defersLoading)
231{
232    if (m_loader)
233        m_loader->setDefersLoading(defersLoading);
234}
235
236} // namespace WebKit
237