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, StoredCredentials storedCredentials)
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, storedCredentials, 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, StoredCredentials storedCredentials)
83 RefPtr<DocumentThreadableLoader> loader = adoptRef(new DocumentThreadableLoader(document, client, request, callbacksSetting, contentSniff, storedCredentials));
84 if (!loader->m_loader)
86 return loader.release();
89 DocumentThreadableLoader::DocumentThreadableLoader(Document* document, ThreadableLoaderClient* client, const ResourceRequest& request, LoadCallbacks callbacksSetting, ContentSniff contentSniff, StoredCredentials storedCredentials)
91 , m_document(document)
92 , m_allowStoredCredentials(storedCredentials == AllowStoredCredentials)
93 , m_sameOriginRequest(document->securityOrigin()->canRequest(request.url()))
97 m_loader = SubresourceLoader::create(document->frame(), this, request, false, callbacksSetting == SendLoadCallbacks, contentSniff == SniffContent);
100 DocumentThreadableLoader::~DocumentThreadableLoader()
103 m_loader->clearClient();
106 void DocumentThreadableLoader::cancel()
112 m_loader->clearClient();
117 void DocumentThreadableLoader::willSendRequest(SubresourceLoader* loader, ResourceRequest& request, const ResourceResponse&)
120 ASSERT_UNUSED(loader, loader == m_loader);
122 // FIXME: This needs to be fixed to follow the redirect correctly even for cross-domain requests.
123 if (!m_document->securityOrigin()->canRequest(request.url())) {
124 RefPtr<DocumentThreadableLoader> protect(this);
125 m_client->didFailRedirectCheck();
126 request = ResourceRequest();
130 void DocumentThreadableLoader::didSendData(SubresourceLoader* loader, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
133 ASSERT_UNUSED(loader, loader == m_loader);
135 m_client->didSendData(bytesSent, totalBytesToBeSent);
138 void DocumentThreadableLoader::didReceiveResponse(SubresourceLoader* loader, const ResourceResponse& response)
141 ASSERT_UNUSED(loader, loader == m_loader);
143 m_client->didReceiveResponse(response);
146 void DocumentThreadableLoader::didReceiveData(SubresourceLoader* loader, const char* data, int lengthReceived)
149 ASSERT_UNUSED(loader, loader == m_loader);
151 m_client->didReceiveData(data, lengthReceived);
154 void DocumentThreadableLoader::didFinishLoading(SubresourceLoader* loader)
156 ASSERT(loader == m_loader);
158 m_client->didFinishLoading(loader->identifier());
161 void DocumentThreadableLoader::didFail(SubresourceLoader* loader, const ResourceError& error)
164 ASSERT_UNUSED(loader, loader == m_loader);
166 m_client->didFail(error);
169 bool DocumentThreadableLoader::getShouldUseCredentialStorage(SubresourceLoader* loader, bool& shouldUseCredentialStorage)
171 ASSERT_UNUSED(loader, loader == m_loader);
173 if (!m_allowStoredCredentials) {
174 shouldUseCredentialStorage = false;
178 return false; // Only FrameLoaderClient can ultimately permit credential use.
181 void DocumentThreadableLoader::didReceiveAuthenticationChallenge(SubresourceLoader* loader, const AuthenticationChallenge&)
183 ASSERT(loader == m_loader);
184 // Users are not prompted for credentials for cross-origin requests.
185 if (!m_sameOriginRequest) {
186 RefPtr<DocumentThreadableLoader> protect(this);
187 m_client->didFail(loader->blockedError());
192 void DocumentThreadableLoader::receivedCancellation(SubresourceLoader* loader, const AuthenticationChallenge& challenge)
195 ASSERT_UNUSED(loader, loader == m_loader);
196 m_client->didReceiveAuthenticationCancellation(challenge.failureResponse());
199 } // namespace WebCore