|
1 /* |
|
2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. |
|
3 * (C) 2007 Graham Dennis (graham.dennis@gmail.com) |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions |
|
7 * are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * 2. Redistributions in binary form must reproduce the above copyright |
|
12 * notice, this list of conditions and the following disclaimer in the |
|
13 * documentation and/or other materials provided with the distribution. |
|
14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
|
15 * its contributors may be used to endorse or promote products derived |
|
16 * from this software without specific prior written permission. |
|
17 * |
|
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
|
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
|
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
|
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
28 */ |
|
29 |
|
30 #include "config.h" |
|
31 #include "ResourceLoader.h" |
|
32 |
|
33 #include "DocumentLoader.h" |
|
34 #include "Frame.h" |
|
35 #include "FrameLoader.h" |
|
36 #include "Page.h" |
|
37 #include "ProgressTracker.h" |
|
38 #include "ResourceHandle.h" |
|
39 #include "ResourceError.h" |
|
40 #include "SharedBuffer.h" |
|
41 |
|
42 #if PLATFORM(SYMBIAN) |
|
43 #include "BrCtl.h" |
|
44 #include "WebFrame.h" |
|
45 #include "WebFrameBridge.h" |
|
46 #include "WebFrameView.h" |
|
47 #include "WebView.h" |
|
48 #endif |
|
49 |
|
50 namespace WebCore { |
|
51 |
|
52 PassRefPtr<SharedBuffer> ResourceLoader::resourceData() |
|
53 { |
|
54 if (m_resourceData) |
|
55 return m_resourceData; |
|
56 |
|
57 if (ResourceHandle::supportsBufferedData() && m_handle) |
|
58 return m_handle->bufferedData(); |
|
59 |
|
60 return 0; |
|
61 } |
|
62 |
|
63 ResourceLoader::ResourceLoader(Frame* frame, bool sendResourceLoadCallbacks, bool shouldContentSniff) |
|
64 : m_reachedTerminalState(false) |
|
65 , m_cancelled(false) |
|
66 , m_calledDidFinishLoad(false) |
|
67 , m_sendResourceLoadCallbacks(sendResourceLoadCallbacks) |
|
68 , m_shouldContentSniff(shouldContentSniff) |
|
69 , m_shouldBufferData(true) |
|
70 , m_frame(frame) |
|
71 , m_documentLoader(frame->loader()->activeDocumentLoader()) |
|
72 , m_identifier(0) |
|
73 , m_defersLoading(frame->page()->defersLoading()) |
|
74 { |
|
75 } |
|
76 |
|
77 ResourceLoader::~ResourceLoader() |
|
78 { |
|
79 ASSERT(m_reachedTerminalState); |
|
80 } |
|
81 |
|
82 void ResourceLoader::releaseResources() |
|
83 { |
|
84 ASSERT(!m_reachedTerminalState); |
|
85 |
|
86 // It's possible that when we release the handle, it will be |
|
87 // deallocated and release the last reference to this object. |
|
88 // We need to retain to avoid accessing the object after it |
|
89 // has been deallocated and also to avoid reentering this method. |
|
90 RefPtr<ResourceLoader> protector(this); |
|
91 |
|
92 if (m_frame) { |
|
93 #if PLATFORM(SYMBIAN) |
|
94 if (control(m_frame.get())->webView()->isSynchRequestPending()) { |
|
95 // Reset synchronous request status for new frame |
|
96 control(m_frame.get())->webView()->synchRequestPending(false); |
|
97 if (!cancelled()) { |
|
98 // Cancel pending synchronous request |
|
99 cancel(); |
|
100 } |
|
101 } |
|
102 #endif |
|
103 m_frame = 0; |
|
104 } |
|
105 m_documentLoader = 0; |
|
106 |
|
107 // We need to set reachedTerminalState to true before we release |
|
108 // the resources to prevent a double dealloc of WebView <rdar://problem/4372628> |
|
109 m_reachedTerminalState = true; |
|
110 |
|
111 m_identifier = 0; |
|
112 |
|
113 if (m_handle) { |
|
114 // Clear out the ResourceHandle's client so that it doesn't try to call |
|
115 // us back after we release it. |
|
116 m_handle->setClient(0); |
|
117 m_handle = 0; |
|
118 } |
|
119 |
|
120 m_resourceData = 0; |
|
121 m_deferredRequest = ResourceRequest(); |
|
122 } |
|
123 |
|
124 bool ResourceLoader::load(const ResourceRequest& r) |
|
125 { |
|
126 ASSERT(!m_handle); |
|
127 ASSERT(m_deferredRequest.isNull()); |
|
128 ASSERT(!frameLoader()->isArchiveLoadPending(this)); |
|
129 |
|
130 m_originalURL = r.url(); |
|
131 |
|
132 ResourceRequest clientRequest(r); |
|
133 willSendRequest(clientRequest, ResourceResponse()); |
|
134 if (clientRequest.isNull()) { |
|
135 didFail(frameLoader()->cancelledError(r)); |
|
136 return false; |
|
137 } |
|
138 |
|
139 if (frameLoader()->willUseArchive(this, clientRequest, m_originalURL)) |
|
140 return true; |
|
141 |
|
142 if (m_defersLoading) { |
|
143 m_deferredRequest = clientRequest; |
|
144 return true; |
|
145 } |
|
146 |
|
147 m_handle = ResourceHandle::create(clientRequest, this, m_frame.get(), m_defersLoading, m_shouldContentSniff, true); |
|
148 |
|
149 return true; |
|
150 } |
|
151 |
|
152 void ResourceLoader::setDefersLoading(bool defers) |
|
153 { |
|
154 m_defersLoading = defers; |
|
155 if (m_handle) |
|
156 m_handle->setDefersLoading(defers); |
|
157 if (!defers && !m_deferredRequest.isNull()) { |
|
158 ResourceRequest request(m_deferredRequest); |
|
159 m_deferredRequest = ResourceRequest(); |
|
160 load(request); |
|
161 } |
|
162 } |
|
163 |
|
164 FrameLoader* ResourceLoader::frameLoader() const |
|
165 { |
|
166 if (!m_frame) |
|
167 return 0; |
|
168 return m_frame->loader(); |
|
169 } |
|
170 |
|
171 void ResourceLoader::addData(const char* data, int length, bool allAtOnce) |
|
172 { |
|
173 if (!m_shouldBufferData) |
|
174 return; |
|
175 |
|
176 if (allAtOnce) { |
|
177 m_resourceData = new SharedBuffer(data, length); |
|
178 return; |
|
179 } |
|
180 |
|
181 if (ResourceHandle::supportsBufferedData()) { |
|
182 // Buffer data only if the connection has handed us the data because is has stopped buffering it. |
|
183 if (m_resourceData) |
|
184 m_resourceData->append(data, length); |
|
185 } else { |
|
186 if (!m_resourceData) |
|
187 m_resourceData = new SharedBuffer(data, length); |
|
188 else |
|
189 m_resourceData->append(data, length); |
|
190 } |
|
191 } |
|
192 |
|
193 void ResourceLoader::clearResourceData() |
|
194 { |
|
195 if (m_resourceData) |
|
196 m_resourceData->clear(); |
|
197 } |
|
198 |
|
199 void ResourceLoader::willSendRequest(ResourceRequest& request, const ResourceResponse& redirectResponse) |
|
200 { |
|
201 // Protect this in this delegate method since the additional processing can do |
|
202 // anything including possibly derefing this; one example of this is Radar 3266216. |
|
203 RefPtr<ResourceLoader> protector(this); |
|
204 |
|
205 ASSERT(!m_reachedTerminalState); |
|
206 |
|
207 if (m_sendResourceLoadCallbacks) { |
|
208 if (!m_identifier) { |
|
209 m_identifier = m_frame->page()->progress()->createUniqueIdentifier(); |
|
210 frameLoader()->assignIdentifierToInitialRequest(m_identifier, request); |
|
211 } |
|
212 |
|
213 frameLoader()->willSendRequest(this, request, redirectResponse); |
|
214 } |
|
215 |
|
216 m_request = request; |
|
217 } |
|
218 |
|
219 void ResourceLoader::didReceiveResponse(const ResourceResponse& r) |
|
220 { |
|
221 ASSERT(!m_reachedTerminalState); |
|
222 |
|
223 // Protect this in this delegate method since the additional processing can do |
|
224 // anything including possibly derefing this; one example of this is Radar 3266216. |
|
225 RefPtr<ResourceLoader> protector(this); |
|
226 |
|
227 m_response = r; |
|
228 |
|
229 if (m_sendResourceLoadCallbacks) |
|
230 frameLoader()->didReceiveResponse(this, m_response); |
|
231 } |
|
232 |
|
233 void ResourceLoader::didReceiveData(const char* data, int length, long long lengthReceived, bool allAtOnce) |
|
234 { |
|
235 // The following assertions are not quite valid here, since a subclass |
|
236 // might override didReceiveData in a way that invalidates them. This |
|
237 // happens with the steps listed in 3266216 |
|
238 // ASSERT(con == connection); |
|
239 // ASSERT(!m_reachedTerminalState); |
|
240 |
|
241 // Protect this in this delegate method since the additional processing can do |
|
242 // anything including possibly derefing this; one example of this is Radar 3266216. |
|
243 RefPtr<ResourceLoader> protector(this); |
|
244 |
|
245 addData(data, length, allAtOnce); |
|
246 // FIXME: If we get a resource with more than 2B bytes, this code won't do the right thing. |
|
247 // However, with today's computers and networking speeds, this won't happen in practice. |
|
248 // Could be an issue with a giant local file. |
|
249 if (m_sendResourceLoadCallbacks && m_frame) |
|
250 frameLoader()->didReceiveData(this, data, length, static_cast<int>(lengthReceived)); |
|
251 } |
|
252 |
|
253 void ResourceLoader::willStopBufferingData(const char* data, int length) |
|
254 { |
|
255 if (!m_shouldBufferData) |
|
256 return; |
|
257 |
|
258 ASSERT(!m_resourceData); |
|
259 m_resourceData = new SharedBuffer(data, length); |
|
260 } |
|
261 |
|
262 void ResourceLoader::didFinishLoading() |
|
263 { |
|
264 // If load has been cancelled after finishing (which could happen with a |
|
265 // JavaScript that changes the window location), do nothing. |
|
266 if (m_cancelled) |
|
267 return; |
|
268 ASSERT(!m_reachedTerminalState); |
|
269 |
|
270 didFinishLoadingOnePart(); |
|
271 releaseResources(); |
|
272 } |
|
273 |
|
274 void ResourceLoader::didFinishLoadingOnePart() |
|
275 { |
|
276 if (m_cancelled) |
|
277 return; |
|
278 ASSERT(!m_reachedTerminalState); |
|
279 |
|
280 if (m_calledDidFinishLoad) |
|
281 return; |
|
282 m_calledDidFinishLoad = true; |
|
283 if (m_sendResourceLoadCallbacks) |
|
284 frameLoader()->didFinishLoad(this); |
|
285 } |
|
286 |
|
287 void ResourceLoader::didFail(const ResourceError& error) |
|
288 { |
|
289 if (m_cancelled) |
|
290 return; |
|
291 ASSERT(!m_reachedTerminalState); |
|
292 |
|
293 // Protect this in this delegate method since the additional processing can do |
|
294 // anything including possibly derefing this; one example of this is Radar 3266216. |
|
295 RefPtr<ResourceLoader> protector(this); |
|
296 |
|
297 if (m_sendResourceLoadCallbacks && !m_calledDidFinishLoad) |
|
298 frameLoader()->didFailToLoad(this, error); |
|
299 |
|
300 releaseResources(); |
|
301 } |
|
302 |
|
303 void ResourceLoader::wasBlocked() |
|
304 { |
|
305 didFail(blockedError()); |
|
306 } |
|
307 |
|
308 void ResourceLoader::didCancel(const ResourceError& error) |
|
309 { |
|
310 ASSERT(!m_cancelled); |
|
311 ASSERT(!m_reachedTerminalState); |
|
312 |
|
313 // This flag prevents bad behavior when loads that finish cause the |
|
314 // load itself to be cancelled (which could happen with a javascript that |
|
315 // changes the window location). This is used to prevent both the body |
|
316 // of this method and the body of connectionDidFinishLoading: running |
|
317 // for a single delegate. Cancelling wins. |
|
318 m_cancelled = true; |
|
319 |
|
320 if (m_handle) |
|
321 m_handle->clearAuthentication(); |
|
322 |
|
323 frameLoader()->cancelPendingArchiveLoad(this); |
|
324 if (m_handle) { |
|
325 m_handle->cancel(); |
|
326 m_handle = 0; |
|
327 } |
|
328 if (m_sendResourceLoadCallbacks && !m_calledDidFinishLoad) |
|
329 frameLoader()->didFailToLoad(this, error); |
|
330 |
|
331 releaseResources(); |
|
332 } |
|
333 |
|
334 void ResourceLoader::cancel() |
|
335 { |
|
336 cancel(ResourceError()); |
|
337 } |
|
338 |
|
339 void ResourceLoader::cancel(const ResourceError& error) |
|
340 { |
|
341 if (m_reachedTerminalState) |
|
342 return; |
|
343 if (!error.isNull()) |
|
344 didCancel(error); |
|
345 else |
|
346 didCancel(cancelledError()); |
|
347 } |
|
348 |
|
349 const ResourceResponse& ResourceLoader::response() const |
|
350 { |
|
351 return m_response; |
|
352 } |
|
353 |
|
354 ResourceError ResourceLoader::cancelledError() |
|
355 { |
|
356 return frameLoader()->cancelledError(m_request); |
|
357 } |
|
358 |
|
359 ResourceError ResourceLoader::blockedError() |
|
360 { |
|
361 return frameLoader()->blockedError(m_request); |
|
362 } |
|
363 |
|
364 void ResourceLoader::willSendRequest(ResourceHandle*, ResourceRequest& request, const ResourceResponse& redirectResponse) |
|
365 { |
|
366 willSendRequest(request, redirectResponse); |
|
367 } |
|
368 |
|
369 void ResourceLoader::didReceiveResponse(ResourceHandle*, const ResourceResponse& response) |
|
370 { |
|
371 didReceiveResponse(response); |
|
372 } |
|
373 |
|
374 void ResourceLoader::didReceiveData(ResourceHandle*, const char* data, int length, int lengthReceived) |
|
375 { |
|
376 didReceiveData(data, length, lengthReceived, false); |
|
377 } |
|
378 |
|
379 void ResourceLoader::didFinishLoading(ResourceHandle*) |
|
380 { |
|
381 didFinishLoading(); |
|
382 } |
|
383 |
|
384 void ResourceLoader::didFail(ResourceHandle*, const ResourceError& error) |
|
385 { |
|
386 didFail(error); |
|
387 } |
|
388 |
|
389 void ResourceLoader::wasBlocked(ResourceHandle*) |
|
390 { |
|
391 wasBlocked(); |
|
392 } |
|
393 |
|
394 void ResourceLoader::didReceiveAuthenticationChallenge(const AuthenticationChallenge& challenge) |
|
395 { |
|
396 // Protect this in this delegate method since the additional processing can do |
|
397 // anything including possibly derefing this; one example of this is Radar 3266216. |
|
398 RefPtr<ResourceLoader> protector(this); |
|
399 frameLoader()->didReceiveAuthenticationChallenge(this, challenge); |
|
400 } |
|
401 |
|
402 void ResourceLoader::didCancelAuthenticationChallenge(const AuthenticationChallenge& challenge) |
|
403 { |
|
404 // Protect this in this delegate method since the additional processing can do |
|
405 // anything including possibly derefing this; one example of this is Radar 3266216. |
|
406 RefPtr<ResourceLoader> protector(this); |
|
407 frameLoader()->didCancelAuthenticationChallenge(this, challenge); |
|
408 } |
|
409 |
|
410 void ResourceLoader::receivedCancellation(const AuthenticationChallenge&) |
|
411 { |
|
412 cancel(); |
|
413 } |
|
414 |
|
415 void ResourceLoader::willCacheResponse(ResourceHandle*, CacheStoragePolicy& policy) |
|
416 { |
|
417 // When in private browsing mode, prevent caching to disk |
|
418 if (policy == StorageAllowed && frameLoader()->privateBrowsingEnabled()) |
|
419 policy = StorageAllowedInMemoryOnly; |
|
420 } |
|
421 |
|
422 #if PLATFORM(SYMBIAN) |
|
423 int ResourceLoader::receivedDataBufferSize() const |
|
424 { |
|
425 if (m_resourceData) |
|
426 return m_resourceData->size(); |
|
427 return 0; |
|
428 } |
|
429 #endif |
|
430 |
|
431 } |