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, CrossOriginRedirectPolicy crossOriginRedirectPolicy)
83 RefPtr<DocumentThreadableLoader> loader = adoptRef(new DocumentThreadableLoader(document, client, request, callbacksSetting, contentSniff, storedCredentials, crossOriginRedirectPolicy));
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, CrossOriginRedirectPolicy crossOriginRedirectPolicy)
91 , m_document(document)
92 , m_allowStoredCredentials(storedCredentials == AllowStoredCredentials)
93 , m_sameOriginRequest(document->securityOrigin()->canRequest(request.url()))
94 , m_denyCrossOriginRedirect(crossOriginRedirectPolicy == DenyCrossOriginRedirect)
98 ASSERT(storedCredentials == AllowStoredCredentials || storedCredentials == DoNotAllowStoredCredentials);
99 ASSERT(crossOriginRedirectPolicy == DenyCrossOriginRedirect || crossOriginRedirectPolicy == AllowCrossOriginRedirect);
100 m_loader = SubresourceLoader::create(document->frame(), this, request, false, callbacksSetting == SendLoadCallbacks, contentSniff == SniffContent);
103 DocumentThreadableLoader::~DocumentThreadableLoader()
106 m_loader->clearClient();
109 void DocumentThreadableLoader::cancel()
115 m_loader->clearClient();
120 void DocumentThreadableLoader::willSendRequest(SubresourceLoader* loader, ResourceRequest& request, const ResourceResponse&)
123 ASSERT_UNUSED(loader, loader == m_loader);
125 // FIXME: This needs to be fixed to follow the redirect correctly even for cross-domain requests.
126 if (m_denyCrossOriginRedirect && !m_document->securityOrigin()->canRequest(request.url())) {
127 RefPtr<DocumentThreadableLoader> protect(this);
128 m_client->didFailRedirectCheck();
129 request = ResourceRequest();
133 void DocumentThreadableLoader::didSendData(SubresourceLoader* loader, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
136 ASSERT_UNUSED(loader, loader == m_loader);
138 m_client->didSendData(bytesSent, totalBytesToBeSent);
141 void DocumentThreadableLoader::didReceiveResponse(SubresourceLoader* loader, const ResourceResponse& response)
144 ASSERT_UNUSED(loader, loader == m_loader);
146 m_client->didReceiveResponse(response);
149 void DocumentThreadableLoader::didReceiveData(SubresourceLoader* loader, const char* data, int lengthReceived)
152 ASSERT_UNUSED(loader, loader == m_loader);
154 m_client->didReceiveData(data, lengthReceived);
157 void DocumentThreadableLoader::didFinishLoading(SubresourceLoader* loader)
159 ASSERT(loader == m_loader);
161 m_client->didFinishLoading(loader->identifier());
164 void DocumentThreadableLoader::didFail(SubresourceLoader* loader, const ResourceError& error)
167 ASSERT_UNUSED(loader, loader == m_loader);
169 m_client->didFail(error);
172 bool DocumentThreadableLoader::getShouldUseCredentialStorage(SubresourceLoader* loader, bool& shouldUseCredentialStorage)
174 ASSERT_UNUSED(loader, loader == m_loader);
176 if (!m_allowStoredCredentials) {
177 shouldUseCredentialStorage = false;
181 return false; // Only FrameLoaderClient can ultimately permit credential use.
184 void DocumentThreadableLoader::didReceiveAuthenticationChallenge(SubresourceLoader* loader, const AuthenticationChallenge&)
186 ASSERT(loader == m_loader);
187 // Users are not prompted for credentials for cross-origin requests.
188 if (!m_sameOriginRequest) {
189 RefPtr<DocumentThreadableLoader> protect(this);
190 m_client->didFail(loader->blockedError());
195 void DocumentThreadableLoader::receivedCancellation(SubresourceLoader* loader, const AuthenticationChallenge& challenge)
198 ASSERT_UNUSED(loader, loader == m_loader);
199 m_client->didReceiveAuthenticationCancellation(challenge.failureResponse());
202 } // namespace WebCore