• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006, 2007, 2009 Apple 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
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "config.h"
30 #include "SubresourceLoader.h"
31 
32 #include "DocumentLoader.h"
33 #include "Frame.h"
34 #include "FrameLoader.h"
35 #include "ResourceHandle.h"
36 #include "SubresourceLoaderClient.h"
37 #include <wtf/RefCountedLeakCounter.h>
38 
39 namespace WebCore {
40 
41 #ifndef NDEBUG
42 static WTF::RefCountedLeakCounter subresourceLoaderCounter("SubresourceLoader");
43 #endif
44 
SubresourceLoader(Frame * frame,SubresourceLoaderClient * client,bool sendResourceLoadCallbacks,bool shouldContentSniff)45 SubresourceLoader::SubresourceLoader(Frame* frame, SubresourceLoaderClient* client, bool sendResourceLoadCallbacks, bool shouldContentSniff)
46     : ResourceLoader(frame, sendResourceLoadCallbacks, shouldContentSniff)
47     , m_client(client)
48     , m_loadingMultipartContent(false)
49 {
50 #ifndef NDEBUG
51     subresourceLoaderCounter.increment();
52 #endif
53     m_documentLoader->addSubresourceLoader(this);
54 }
55 
~SubresourceLoader()56 SubresourceLoader::~SubresourceLoader()
57 {
58 #ifndef NDEBUG
59     subresourceLoaderCounter.decrement();
60 #endif
61 }
62 
create(Frame * frame,SubresourceLoaderClient * client,const ResourceRequest & request,bool skipCanLoadCheck,bool sendResourceLoadCallbacks,bool shouldContentSniff)63 PassRefPtr<SubresourceLoader> SubresourceLoader::create(Frame* frame, SubresourceLoaderClient* client, const ResourceRequest& request, bool skipCanLoadCheck, bool sendResourceLoadCallbacks, bool shouldContentSniff)
64 {
65     if (!frame)
66         return 0;
67 
68     FrameLoader* fl = frame->loader();
69     if (!skipCanLoadCheck && (fl->state() == FrameStateProvisional || fl->activeDocumentLoader()->isStopping()))
70         return 0;
71 
72     ResourceRequest newRequest = request;
73 
74     if (!skipCanLoadCheck
75             && FrameLoader::restrictAccessToLocal()
76             && !FrameLoader::canLoad(request.url(), String(), frame->document())) {
77         FrameLoader::reportLocalLoadFailed(frame, request.url().string());
78         return 0;
79     }
80 
81     if (FrameLoader::shouldHideReferrer(request.url(), fl->outgoingReferrer()))
82         newRequest.clearHTTPReferrer();
83     else if (!request.httpReferrer())
84         newRequest.setHTTPReferrer(fl->outgoingReferrer());
85     FrameLoader::addHTTPOriginIfNeeded(newRequest, fl->outgoingOrigin());
86 
87     // Use the original request's cache policy for two reasons:
88     // 1. For POST requests, we mutate the cache policy for the main resource,
89     //    but we do not want this to apply to subresources
90     // 2. Delegates that modify the cache policy using willSendRequest: should
91     //    not affect any other resources. Such changes need to be done
92     //    per request.
93     if (newRequest.isConditional())
94         newRequest.setCachePolicy(ReloadIgnoringCacheData);
95     else
96         newRequest.setCachePolicy(fl->originalRequest().cachePolicy());
97 
98     fl->addExtraFieldsToSubresourceRequest(newRequest);
99 
100     RefPtr<SubresourceLoader> subloader(adoptRef(new SubresourceLoader(frame, client, sendResourceLoadCallbacks, shouldContentSniff)));
101     if (!subloader->load(newRequest))
102         return 0;
103 
104     return subloader.release();
105 }
106 
willSendRequest(ResourceRequest & newRequest,const ResourceResponse & redirectResponse)107 void SubresourceLoader::willSendRequest(ResourceRequest& newRequest, const ResourceResponse& redirectResponse)
108 {
109     // Store the previous URL because the call to ResourceLoader::willSendRequest will modify it.
110     KURL previousURL = request().url();
111 
112     ResourceLoader::willSendRequest(newRequest, redirectResponse);
113     if (!previousURL.isNull() && !newRequest.isNull() && previousURL != newRequest.url() && m_client)
114         m_client->willSendRequest(this, newRequest, redirectResponse);
115 }
116 
didSendData(unsigned long long bytesSent,unsigned long long totalBytesToBeSent)117 void SubresourceLoader::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
118 {
119     RefPtr<SubresourceLoader> protect(this);
120 
121     if (m_client)
122         m_client->didSendData(this, bytesSent, totalBytesToBeSent);
123 }
124 
didReceiveResponse(const ResourceResponse & r)125 void SubresourceLoader::didReceiveResponse(const ResourceResponse& r)
126 {
127     ASSERT(!r.isNull());
128 
129     if (r.isMultipart())
130         m_loadingMultipartContent = true;
131 
132     // Reference the object in this method since the additional processing can do
133     // anything including removing the last reference to this object; one example of this is 3266216.
134     RefPtr<SubresourceLoader> protect(this);
135 
136     if (m_client)
137         m_client->didReceiveResponse(this, r);
138 
139     // The loader can cancel a load if it receives a multipart response for a non-image
140     if (reachedTerminalState())
141         return;
142     ResourceLoader::didReceiveResponse(r);
143 
144     RefPtr<SharedBuffer> buffer = resourceData();
145     if (m_loadingMultipartContent && buffer && buffer->size()) {
146         // Since a subresource loader does not load multipart sections progressively,
147         // deliver the previously received data to the loader all at once now.
148         // Then clear the data to make way for the next multipart section.
149         if (m_client)
150             m_client->didReceiveData(this, buffer->data(), buffer->size());
151         clearResourceData();
152 
153         // After the first multipart section is complete, signal to delegates that this load is "finished"
154         m_documentLoader->subresourceLoaderFinishedLoadingOnePart(this);
155         didFinishLoadingOnePart();
156     }
157 }
158 
didReceiveData(const char * data,int length,long long lengthReceived,bool allAtOnce)159 void SubresourceLoader::didReceiveData(const char* data, int length, long long lengthReceived, bool allAtOnce)
160 {
161     // Reference the object in this method since the additional processing can do
162     // anything including removing the last reference to this object; one example of this is 3266216.
163     RefPtr<SubresourceLoader> protect(this);
164 
165     ResourceLoader::didReceiveData(data, length, lengthReceived, allAtOnce);
166 
167     // A subresource loader does not load multipart sections progressively.
168     // So don't deliver any data to the loader yet.
169     if (!m_loadingMultipartContent && m_client)
170         m_client->didReceiveData(this, data, length);
171 }
172 
didFinishLoading()173 void SubresourceLoader::didFinishLoading()
174 {
175     if (cancelled())
176         return;
177     ASSERT(!reachedTerminalState());
178 
179     // Calling removeSubresourceLoader will likely result in a call to deref, so we must protect ourselves.
180     RefPtr<SubresourceLoader> protect(this);
181 
182     if (m_client)
183         m_client->didFinishLoading(this);
184 
185     m_handle = 0;
186 
187     if (cancelled())
188         return;
189     m_documentLoader->removeSubresourceLoader(this);
190     ResourceLoader::didFinishLoading();
191 }
192 
didFail(const ResourceError & error)193 void SubresourceLoader::didFail(const ResourceError& error)
194 {
195     if (cancelled())
196         return;
197     ASSERT(!reachedTerminalState());
198 
199     // Calling removeSubresourceLoader will likely result in a call to deref, so we must protect ourselves.
200     RefPtr<SubresourceLoader> protect(this);
201 
202     if (m_client)
203         m_client->didFail(this, error);
204 
205     m_handle = 0;
206 
207     if (cancelled())
208         return;
209     m_documentLoader->removeSubresourceLoader(this);
210     ResourceLoader::didFail(error);
211 }
212 
didCancel(const ResourceError & error)213 void SubresourceLoader::didCancel(const ResourceError& error)
214 {
215     ASSERT(!reachedTerminalState());
216 
217     // Calling removeSubresourceLoader will likely result in a call to deref, so we must protect ourselves.
218     RefPtr<SubresourceLoader> protect(this);
219 
220     if (m_client)
221         m_client->didFail(this, error);
222 
223     if (cancelled())
224         return;
225 
226     // The only way the subresource loader can reach the terminal state here is if the run loop spins when calling
227     // m_client->didFail. This should in theory not happen which is why the assert is here.
228     ASSERT(!reachedTerminalState());
229     if (reachedTerminalState())
230         return;
231 
232     m_documentLoader->removeSubresourceLoader(this);
233     ResourceLoader::didCancel(error);
234 }
235 
shouldUseCredentialStorage()236 bool SubresourceLoader::shouldUseCredentialStorage()
237 {
238     RefPtr<SubresourceLoader> protect(this);
239 
240     bool shouldUse;
241     if (m_client && m_client->getShouldUseCredentialStorage(this, shouldUse))
242         return shouldUse;
243 
244     return ResourceLoader::shouldUseCredentialStorage();
245 }
246 
didReceiveAuthenticationChallenge(const AuthenticationChallenge & challenge)247 void SubresourceLoader::didReceiveAuthenticationChallenge(const AuthenticationChallenge& challenge)
248 {
249     RefPtr<SubresourceLoader> protect(this);
250 
251     if (m_client)
252         m_client->didReceiveAuthenticationChallenge(this, challenge);
253 
254     // The SubResourceLoaderClient may have cancelled this ResourceLoader in response to the challenge.
255     // If that's the case, don't call didReceiveAuthenticationChallenge
256     if (reachedTerminalState())
257         return;
258 
259     ResourceLoader::didReceiveAuthenticationChallenge(challenge);
260 }
261 
receivedCancellation(const AuthenticationChallenge & challenge)262 void SubresourceLoader::receivedCancellation(const AuthenticationChallenge& challenge)
263 {
264     ASSERT(!reachedTerminalState());
265 
266     RefPtr<SubresourceLoader> protect(this);
267 
268     if (m_client)
269         m_client->receivedCancellation(this, challenge);
270 
271     ResourceLoader::receivedCancellation(challenge);
272 }
273 
274 
275 }
276