2 * Copyright (C) 2004, 2006, 2007, 2008, 2009, 2010 Apple 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
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "ResourceHandle.h"
28 #include "ResourceHandleInternal.h"
30 #include "BlobRegistry.h"
33 #include "ResourceHandleClient.h"
36 #include <wtf/text/CString.h>
40 static bool shouldForceContentSniffing;
42 ResourceHandle::ResourceHandle(const ResourceRequest& request, ResourceHandleClient* client, bool defersLoading, bool shouldContentSniff)
43 : d(adoptPtr(new ResourceHandleInternal(this, request, client, defersLoading, shouldContentSniff && shouldContentSniffURL(request.url()))))
45 if (!request.url().isValid()) {
46 scheduleFailure(InvalidURLFailure);
50 if (!portAllowed(request.url())) {
51 scheduleFailure(BlockedFailure);
56 PassRefPtr<ResourceHandle> ResourceHandle::create(NetworkingContext* context, const ResourceRequest& request, ResourceHandleClient* client, bool defersLoading, bool shouldContentSniff)
59 if (request.url().protocolIs("blob")) {
60 PassRefPtr<ResourceHandle> handle = blobRegistry().createResourceHandle(request, client);
66 RefPtr<ResourceHandle> newHandle(adoptRef(new ResourceHandle(request, client, defersLoading, shouldContentSniff)));
68 if (newHandle->d->m_scheduledFailureType != NoFailure)
69 return newHandle.release();
71 if (newHandle->start(context))
72 return newHandle.release();
77 void ResourceHandle::scheduleFailure(FailureType type)
79 d->m_scheduledFailureType = type;
80 d->m_failureTimer.startOneShot(0);
83 void ResourceHandle::fireFailure(Timer<ResourceHandle>*)
88 switch (d->m_scheduledFailureType) {
93 d->m_scheduledFailureType = NoFailure;
94 client()->wasBlocked(this);
96 case InvalidURLFailure:
97 d->m_scheduledFailureType = NoFailure;
98 client()->cannotShowURL(this);
102 ASSERT_NOT_REACHED();
105 ResourceHandleClient* ResourceHandle::client() const
110 void ResourceHandle::setClient(ResourceHandleClient* client)
112 d->m_client = client;
115 ResourceRequest& ResourceHandle::firstRequest()
117 return d->m_firstRequest;
120 const String& ResourceHandle::lastHTTPMethod() const
122 return d->m_lastHTTPMethod;
125 bool ResourceHandle::hasAuthenticationChallenge() const
127 return !d->m_currentWebChallenge.isNull();
130 void ResourceHandle::clearAuthentication()
133 d->m_currentMacChallenge = nil;
135 d->m_currentWebChallenge.nullify();
138 bool ResourceHandle::shouldContentSniff() const
140 return d->m_shouldContentSniff;
143 bool ResourceHandle::shouldContentSniffURL(const KURL& url)
146 if (shouldForceContentSniffing)
149 // We shouldn't content sniff file URLs as their MIME type should be established via their extension.
150 return !url.protocolIs("file");
153 void ResourceHandle::forceContentSniffing()
155 shouldForceContentSniffing = true;
158 void ResourceHandle::setDefersLoading(bool defers)
160 LOG(Network, "Handle %p setDefersLoading(%s)", this, defers ? "true" : "false");
162 ASSERT(d->m_defersLoading != defers); // Deferring is not counted, so calling setDefersLoading() repeatedly is likely to be in error.
163 d->m_defersLoading = defers;
166 ASSERT(d->m_failureTimer.isActive() == (d->m_scheduledFailureType != NoFailure));
167 if (d->m_failureTimer.isActive())
168 d->m_failureTimer.stop();
169 } else if (d->m_scheduledFailureType != NoFailure) {
170 ASSERT(!d->m_failureTimer.isActive());
171 d->m_failureTimer.startOneShot(0);
174 platformSetDefersLoading(defers);
178 void ResourceHandle::prepareForURL(const KURL& url)
180 return prefetchDNS(url.host());
184 void ResourceHandle::cacheMetadata(const ResourceResponse&, const Vector<char>&)
186 // Optionally implemented by platform.
189 } // namespace WebCore