1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
/*
* Copyright (C) 2009 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "ResourceHandle.h"
#include "ResourceHandleClient.h"
#include "ResourceHandleInternal.h"
#include "ResourceRequest.h"
#include "SharedBuffer.h"
#include "WrappedResourceRequest.h"
#include "WrappedResourceResponse.h"
#include <public/Platform.h>
#include <public/WebURLError.h>
#include <public/WebURLLoader.h>
#include <public/WebURLLoaderClient.h>
#include <public/WebURLRequest.h>
#include <public/WebURLResponse.h>
using namespace WebKit;
namespace WebCore {
// ResourceHandleInternal -----------------------------------------------------
ResourceHandleInternal::ResourceHandleInternal(const ResourceRequest& request, ResourceHandleClient* client)
: m_request(request)
, m_owner(0)
, m_client(client)
, m_state(ConnectionStateNew)
{
}
void ResourceHandleInternal::start()
{
if (m_state != ConnectionStateNew)
CRASH();
m_state = ConnectionStateStarted;
m_loader = adoptPtr(Platform::current()->createURLLoader());
ASSERT(m_loader);
WrappedResourceRequest wrappedRequest(m_request);
wrappedRequest.setAllowStoredCredentials(allowStoredCredentials());
m_loader->loadAsynchronously(wrappedRequest, this);
}
void ResourceHandleInternal::cancel()
{
m_state = ConnectionStateCanceled;
m_loader->cancel();
// Do not make any further calls to the client.
m_client = 0;
}
void ResourceHandleInternal::setDefersLoading(bool value)
{
m_loader->setDefersLoading(value);
}
bool ResourceHandleInternal::allowStoredCredentials() const
{
return m_client && m_client->shouldUseCredentialStorage(m_owner);
}
void ResourceHandleInternal::willSendRequest(
WebURLLoader*, WebURLRequest& request, const WebURLResponse& response)
{
ASSERT(m_client);
ASSERT(!request.isNull());
ASSERT(!response.isNull());
m_client->willSendRequest(m_owner, request.toMutableResourceRequest(), response.toResourceResponse());
}
void ResourceHandleInternal::didSendData(
WebURLLoader*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
{
ASSERT(m_client);
m_client->didSendData(m_owner, bytesSent, totalBytesToBeSent);
}
void ResourceHandleInternal::didReceiveResponse(WebURLLoader*, const WebURLResponse& response)
{
ASSERT(m_client);
ASSERT(!response.isNull());
bool isMultipart = response.isMultipartPayload();
bool isValidStateTransition = (m_state == ConnectionStateStarted || m_state == ConnectionStateReceivedResponse);
// In the case of multipart loads, calls to didReceiveData & didReceiveResponse can be interleaved.
if (!isMultipart && !isValidStateTransition)
CRASH();
m_state = ConnectionStateReceivedResponse;
m_client->didReceiveResponse(m_owner, response.toResourceResponse());
}
void ResourceHandleInternal::didDownloadData(WebURLLoader*, int dataLength)
{
ASSERT(m_client);
if (m_state != ConnectionStateReceivedResponse)
CRASH();
m_client->didDownloadData(m_owner, dataLength);
}
void ResourceHandleInternal::didReceiveData(WebURLLoader*, const char* data, int dataLength, int encodedDataLength)
{
ASSERT(m_client);
if (m_state != ConnectionStateReceivedResponse && m_state != ConnectionStateReceivingData)
CRASH();
m_state = ConnectionStateReceivingData;
m_client->didReceiveData(m_owner, data, dataLength, encodedDataLength);
}
void ResourceHandleInternal::didReceiveCachedMetadata(WebURLLoader*, const char* data, int dataLength)
{
ASSERT(m_client);
if (m_state != ConnectionStateReceivedResponse && m_state != ConnectionStateReceivingData)
CRASH();
m_client->didReceiveCachedMetadata(m_owner, data, dataLength);
}
void ResourceHandleInternal::didFinishLoading(WebURLLoader*, double finishTime)
{
ASSERT(m_client);
if (m_state != ConnectionStateReceivedResponse && m_state != ConnectionStateReceivingData)
CRASH();
m_state = ConnectionStateFinishedLoading;
m_client->didFinishLoading(m_owner, finishTime);
}
void ResourceHandleInternal::didFail(WebURLLoader*, const WebURLError& error)
{
ASSERT(m_client);
m_state = ConnectionStateFailed;
m_client->didFail(m_owner, error);
}
ResourceHandleInternal* ResourceHandleInternal::FromResourceHandle(ResourceHandle* handle)
{
return handle->d.get();
}
// ResourceHandle -------------------------------------------------------------
ResourceHandle::ResourceHandle(const ResourceRequest& request,
ResourceHandleClient* client,
bool defersLoading,
bool shouldContentSniff)
: d(adoptPtr(new ResourceHandleInternal(request, client)))
{
d->setOwner(this);
// FIXME: Figure out what to do with the bool params.
}
PassRefPtr<ResourceHandle> ResourceHandle::create(NetworkingContext* context,
const ResourceRequest& request,
ResourceHandleClient* client,
bool defersLoading,
bool shouldContentSniff)
{
RefPtr<ResourceHandle> newHandle = adoptRef(new ResourceHandle(
request, client, defersLoading, shouldContentSniff));
if (newHandle->start(context))
return newHandle.release();
return 0;
}
ResourceRequest& ResourceHandle::firstRequest()
{
return d->request();
}
ResourceHandleClient* ResourceHandle::client() const
{
return d->client();
}
void ResourceHandle::setClient(ResourceHandleClient* client)
{
d->setClient(client);
}
void ResourceHandle::setDefersLoading(bool value)
{
d->setDefersLoading(value);
}
bool ResourceHandle::start(NetworkingContext* context)
{
if (!context)
return false;
d->start();
return true;
}
bool ResourceHandle::hasAuthenticationChallenge() const
{
return false;
}
void ResourceHandle::clearAuthentication()
{
}
void ResourceHandle::cancel()
{
d->cancel();
}
ResourceHandle::~ResourceHandle()
{
d->setOwner(0);
}
bool ResourceHandle::loadsBlocked()
{
return false; // This seems to be related to sync XMLHttpRequest...
}
// static
void ResourceHandle::loadResourceSynchronously(NetworkingContext* context,
const ResourceRequest& request,
StoredCredentials storedCredentials,
ResourceError& error,
ResourceResponse& response,
Vector<char>& data)
{
OwnPtr<WebURLLoader> loader = adoptPtr(Platform::current()->createURLLoader());
ASSERT(loader);
WrappedResourceRequest requestIn(request);
requestIn.setAllowStoredCredentials(storedCredentials == AllowStoredCredentials);
WrappedResourceResponse responseOut(response);
WebURLError errorOut;
WebData dataOut;
loader->loadSynchronously(requestIn, responseOut, errorOut, dataOut);
error = errorOut;
data.clear();
data.append(dataOut.data(), dataOut.size());
}
// static
bool ResourceHandle::willLoadFromCache(ResourceRequest& request, Frame*)
{
// This method is used to determine if a POST request can be repeated from
// cache, but you cannot really know until you actually try to read from the
// cache. Even if we checked now, something else could come along and wipe
// out the cache entry by the time we fetch it.
//
// So, we always say yes here, to prevent the FrameLoader from initiating a
// reload. Then in FrameLoaderClientImpl::dispatchWillSendRequest, we
// fix-up the cache policy of the request to force a load from the cache.
//
ASSERT(request.httpMethod() == "POST");
return true;
}
// static
void ResourceHandle::cacheMetadata(const ResourceResponse& response, const Vector<char>& data)
{
WebKit::Platform::current()->cacheMetadata(response.url(), response.responseTime(), data.data(), data.size());
}
} // namespace WebCore
|