2 * Copyright (C) 2009 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 "DocumentThreadableLoader.h"
34 #include "AuthenticationChallenge.h"
36 #include "DocumentThreadableLoader.h"
38 #include "FrameLoader.h"
39 #include "ResourceRequest.h"
40 #include "SecurityOrigin.h"
41 #include "SubresourceLoader.h"
42 #include "ThreadableLoaderClient.h"
46 void DocumentThreadableLoader::loadResourceSynchronously(Document* document, const ResourceRequest& request, ThreadableLoaderClient& client)
48 bool sameOriginRequest = document->securityOrigin()->canRequest(request.url());
52 ResourceResponse response;
53 unsigned long identifier = std::numeric_limits<unsigned long>::max();
54 if (document->frame())
55 identifier = document->frame()->loader()->loadResourceSynchronously(request, error, response, data);
57 // No exception for file:/// resources, see <rdar://problem/4962298>.
58 // Also, if we have an HTTP response, then it wasn't a network error in fact.
59 if (!error.isNull() && !request.url().isLocalFile() && response.httpStatusCode() <= 0) {
60 client.didFail(error);
64 // FIXME: This check along with the one in willSendRequest is specific to xhr and
65 // should be made more generic.
66 if (sameOriginRequest && !document->securityOrigin()->canRequest(response.url())) {
67 client.didFailRedirectCheck();
71 client.didReceiveResponse(response);
73 const char* bytes = static_cast<const char*>(data.data());
74 int len = static_cast<int>(data.size());
75 client.didReceiveData(bytes, len);
77 client.didFinishLoading(identifier);
80 PassRefPtr<DocumentThreadableLoader> DocumentThreadableLoader::create(Document* document, ThreadableLoaderClient* client, const ResourceRequest& request, LoadCallbacks callbacksSetting, ContentSniff contentSniff)
83 RefPtr<DocumentThreadableLoader> loader = adoptRef(new DocumentThreadableLoader(document, client, request, callbacksSetting, contentSniff));
84 if (!loader->m_loader)
86 return loader.release();
89 DocumentThreadableLoader::DocumentThreadableLoader(Document* document, ThreadableLoaderClient* client, const ResourceRequest& request, LoadCallbacks callbacksSetting, ContentSniff contentSniff)
91 , m_document(document)
95 m_loader = SubresourceLoader::create(document->frame(), this, request, false, callbacksSetting == SendLoadCallbacks, contentSniff == SniffContent);
98 DocumentThreadableLoader::~DocumentThreadableLoader()
101 m_loader->clearClient();
104 void DocumentThreadableLoader::cancel()
110 m_loader->clearClient();
115 void DocumentThreadableLoader::willSendRequest(SubresourceLoader*, ResourceRequest& request, const ResourceResponse&)
119 // FIXME: This needs to be fixed to follow the redirect correctly even for cross-domain requests.
120 if (!m_document->securityOrigin()->canRequest(request.url())) {
121 RefPtr<DocumentThreadableLoader> protect(this);
122 m_client->didFailRedirectCheck();
127 void DocumentThreadableLoader::didSendData(SubresourceLoader*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
130 m_client->didSendData(bytesSent, totalBytesToBeSent);
133 void DocumentThreadableLoader::didReceiveResponse(SubresourceLoader*, const ResourceResponse& response)
136 m_client->didReceiveResponse(response);
139 void DocumentThreadableLoader::didReceiveData(SubresourceLoader*, const char* data, int lengthReceived)
142 m_client->didReceiveData(data, lengthReceived);
145 void DocumentThreadableLoader::didFinishLoading(SubresourceLoader* loader)
149 m_client->didFinishLoading(loader->identifier());
152 void DocumentThreadableLoader::didFail(SubresourceLoader*, const ResourceError& error)
155 m_client->didFail(error);
158 void DocumentThreadableLoader::receivedCancellation(SubresourceLoader*, const AuthenticationChallenge& challenge)
161 m_client->didReceiveAuthenticationCancellation(challenge.failureResponse());
164 } // namespace WebCore