2011-03-07 Bill Budge <bbudge@chromium.org>
[WebKit-https.git] / 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 "SubresourceLoader.h"
36 #include "ThreadableLoaderClient.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
48 using namespace WebCore;
49 using namespace WebKit;
50 using namespace WTF;
51
52 namespace WebKit {
53
54 // This class bridges the interface differences between WebCore and WebKit loader clients.
55 // It forwards its ThreadableLoaderClient notifications to a WebURLLoaderClient.
56 class AssociatedURLLoader::ClientAdapter : public ThreadableLoaderClient {
57 public:
58     static PassOwnPtr<ClientAdapter> create(WebURLLoader*, 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 private:
70     ClientAdapter(WebURLLoader*, WebURLLoaderClient*, bool /*downloadingToFile*/);
71
72     WebURLLoader* m_loader;
73     WebURLLoaderClient* m_client;
74     unsigned long m_downloadLength;
75     bool m_downloadingToFile;
76 };
77
78 PassOwnPtr<AssociatedURLLoader::ClientAdapter> AssociatedURLLoader::ClientAdapter::create(WebURLLoader* loader, WebURLLoaderClient* client, bool downloadToFile)
79 {
80     return adoptPtr(new ClientAdapter(loader, client, downloadToFile));
81 }
82
83 AssociatedURLLoader::ClientAdapter::ClientAdapter(WebURLLoader* loader, WebURLLoaderClient* client, bool downloadingToFile)
84     : m_loader(loader)
85     , m_client(client)
86     , m_downloadLength(0)
87     , m_downloadingToFile(downloadingToFile)
88 {
89     ASSERT(m_loader);
90     ASSERT(m_client);
91 }
92
93 void AssociatedURLLoader::ClientAdapter::willSendRequest(ResourceRequest& newRequest, const ResourceResponse& redirectResponse)
94 {
95     WrappedResourceRequest wrappedNewRequest(newRequest);
96     WrappedResourceResponse wrappedRedirectResponse(redirectResponse);
97     m_client->willSendRequest(m_loader, wrappedNewRequest, wrappedRedirectResponse);
98 }
99
100 void AssociatedURLLoader::ClientAdapter::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
101 {
102     m_client->didSendData(m_loader, bytesSent, totalBytesToBeSent);
103 }
104
105 void AssociatedURLLoader::ClientAdapter::didReceiveResponse(const ResourceResponse& response)
106 {
107     WrappedResourceResponse wrappedResponse(response);
108     m_client->didReceiveResponse(m_loader, wrappedResponse);
109 }
110
111 void AssociatedURLLoader::ClientAdapter::didReceiveData(const char* data, int lengthReceived)
112 {
113     m_client->didReceiveData(m_loader, data, lengthReceived);
114     m_downloadLength += lengthReceived;
115 }
116
117 void AssociatedURLLoader::ClientAdapter::didReceiveCachedMetadata(const char* data, int lengthReceived)
118 {
119     m_client->didReceiveCachedMetadata(m_loader, data, lengthReceived);
120 }
121
122 void AssociatedURLLoader::ClientAdapter::didFinishLoading(unsigned long identifier, double finishTime)
123 {
124     if (m_downloadingToFile) {
125         int downloadLength = m_downloadLength <= INT_MAX ? m_downloadLength : INT_MAX;
126         m_client->didDownloadData(m_loader, downloadLength);
127     }
128
129     m_client->didFinishLoading(m_loader, finishTime);
130 }
131
132 void AssociatedURLLoader::ClientAdapter::didFail(const ResourceError& error)
133 {
134     WebURLError webError(error);
135     m_client->didFail(m_loader, webError);
136 }
137
138 AssociatedURLLoader::AssociatedURLLoader(PassRefPtr<WebFrameImpl> frameImpl)
139     : m_frameImpl(frameImpl)
140     , m_client(0)
141 {
142     ASSERT(m_frameImpl);
143 }
144
145 AssociatedURLLoader::AssociatedURLLoader(PassRefPtr<WebFrameImpl> frameImpl, const AssociatedURLLoaderOptions& options)
146     : m_frameImpl(frameImpl)
147     , m_options(options)
148     , m_client(0)
149 {
150     ASSERT(m_frameImpl);
151 }
152
153 AssociatedURLLoader::~AssociatedURLLoader()
154 {
155 }
156
157 #define COMPILE_ASSERT_MATCHING_ENUM(webkit_name, webcore_name) \
158     COMPILE_ASSERT(static_cast<int>(WebKit::webkit_name) == static_cast<int>(WebCore::webcore_name), mismatching_enums)
159
160 COMPILE_ASSERT_MATCHING_ENUM(DenyCrossOriginRequests, DenyCrossOriginRequests);
161 COMPILE_ASSERT_MATCHING_ENUM(UseAccessControl, UseAccessControl);
162 COMPILE_ASSERT_MATCHING_ENUM(AllowCrossOriginRequests, AllowCrossOriginRequests);
163
164 void AssociatedURLLoader::loadSynchronously(const WebURLRequest& request, WebURLResponse& response, WebURLError& error, WebData& data)
165 {
166     ASSERT(0); // Synchronous loading is not supported.
167 }
168
169 void AssociatedURLLoader::loadAsynchronously(const WebURLRequest& request, WebURLLoaderClient* client)
170 {
171     ASSERT(!m_client);
172
173     m_client = client;
174     ASSERT(m_client);
175
176     ThreadableLoaderOptions options;
177     options.sendLoadCallbacks = m_options.sendLoadCallbacks;
178     options.sniffContent = m_options.sniffContent;
179     options.allowCredentials = m_options.allowCredentials;
180     options.forcePreflight = m_options.forcePreflight;
181     options.crossOriginRequestPolicy = static_cast<WebCore::CrossOriginRequestPolicy>(options.crossOriginRequestPolicy);
182
183     WebURLRequest requestCopy(request);
184     const ResourceRequest& webcoreRequest = request.toResourceRequest();
185     Document* webcoreDocument = m_frameImpl->frame()->document();
186     m_clientAdapter = ClientAdapter::create(this, m_client, request.downloadToFile());
187
188     m_loader = DocumentThreadableLoader::create(webcoreDocument, m_clientAdapter.get(), webcoreRequest, options);
189 }
190
191 void AssociatedURLLoader::cancel()
192 {
193     if (m_loader) {
194         m_loader->cancel();
195         m_loader = 0;
196     }
197     m_client = 0;
198 }
199
200 void AssociatedURLLoader::setDefersLoading(bool defersLoading)
201 {
202     if (m_loader)
203         m_loader->setDefersLoading(defersLoading);
204 }
205
206 } // namespace WebKit