| 1 | /* |
| 2 | * Copyright (C) 2006-2018 Apple 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 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 "ApplicationCacheHost.h" |
| 34 | #include "AuthenticationChallenge.h" |
| 35 | #include "ContentRuleListResults.h" |
| 36 | #include "DataURLDecoder.h" |
| 37 | #include "DiagnosticLoggingClient.h" |
| 38 | #include "DiagnosticLoggingKeys.h" |
| 39 | #include "DocumentLoader.h" |
| 40 | #include "Frame.h" |
| 41 | #include "FrameLoader.h" |
| 42 | #include "FrameLoaderClient.h" |
| 43 | #include "InspectorInstrumentation.h" |
| 44 | #include "LoaderStrategy.h" |
| 45 | #include "Logging.h" |
| 46 | #include "Page.h" |
| 47 | #include "PlatformStrategies.h" |
| 48 | #include "ProgressTracker.h" |
| 49 | #include "ResourceError.h" |
| 50 | #include "ResourceHandle.h" |
| 51 | #include "SecurityOrigin.h" |
| 52 | #include "SharedBuffer.h" |
| 53 | #include <wtf/CompletionHandler.h> |
| 54 | #include <wtf/Ref.h> |
| 55 | |
| 56 | #if ENABLE(CONTENT_EXTENSIONS) |
| 57 | #include "UserContentController.h" |
| 58 | #endif |
| 59 | |
| 60 | #if USE(QUICK_LOOK) |
| 61 | #include "PreviewConverter.h" |
| 62 | #include "PreviewLoader.h" |
| 63 | #endif |
| 64 | |
| 65 | #undef RELEASE_LOG_IF_ALLOWED |
| 66 | #define RELEASE_LOG_IF_ALLOWED(fmt, ...) RELEASE_LOG_IF(isAlwaysOnLoggingAllowed(), Network, "%p - ResourceLoader::" fmt, this, ##__VA_ARGS__) |
| 67 | |
| 68 | namespace WebCore { |
| 69 | |
| 70 | ResourceLoader::ResourceLoader(Frame& frame, ResourceLoaderOptions options) |
| 71 | : m_frame { &frame } |
| 72 | , m_documentLoader { frame.loader().activeDocumentLoader() } |
| 73 | , m_defersLoading { options.defersLoadingPolicy == DefersLoadingPolicy::AllowDefersLoading && frame.page()->defersLoading() } |
| 74 | , m_options { options } |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | ResourceLoader::~ResourceLoader() |
| 79 | { |
| 80 | ASSERT(m_reachedTerminalState); |
| 81 | } |
| 82 | |
| 83 | void ResourceLoader::finishNetworkLoad() |
| 84 | { |
| 85 | platformStrategies()->loaderStrategy()->remove(this); |
| 86 | |
| 87 | if (m_handle) { |
| 88 | ASSERT(m_handle->client() == this); |
| 89 | m_handle->clearClient(); |
| 90 | m_handle = nullptr; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void ResourceLoader::releaseResources() |
| 95 | { |
| 96 | ASSERT(!m_reachedTerminalState); |
| 97 | |
| 98 | // It's possible that when we release the handle, it will be |
| 99 | // deallocated and release the last reference to this object. |
| 100 | // We need to retain to avoid accessing the object after it |
| 101 | // has been deallocated and also to avoid reentering this method. |
| 102 | Ref<ResourceLoader> protectedThis(*this); |
| 103 | |
| 104 | m_frame = nullptr; |
| 105 | m_documentLoader = nullptr; |
| 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 | finishNetworkLoad(); |
| 112 | |
| 113 | m_identifier = 0; |
| 114 | |
| 115 | m_resourceData = nullptr; |
| 116 | m_deferredRequest = ResourceRequest(); |
| 117 | } |
| 118 | |
| 119 | void ResourceLoader::init(ResourceRequest&& clientRequest, CompletionHandler<void(bool)>&& completionHandler) |
| 120 | { |
| 121 | ASSERT(!m_handle); |
| 122 | ASSERT(m_request.isNull()); |
| 123 | ASSERT(m_deferredRequest.isNull()); |
| 124 | ASSERT(!m_documentLoader->isSubstituteLoadPending(this)); |
| 125 | |
| 126 | m_loadTiming.markStartTimeAndFetchStart(); |
| 127 | |
| 128 | #if PLATFORM(IOS_FAMILY) |
| 129 | // If the documentLoader was detached while this ResourceLoader was waiting its turn |
| 130 | // in ResourceLoadScheduler queue, don't continue. |
| 131 | if (!m_documentLoader->frame()) { |
| 132 | cancel(); |
| 133 | return completionHandler(false); |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | m_defersLoading = m_options.defersLoadingPolicy == DefersLoadingPolicy::AllowDefersLoading && m_frame->page()->defersLoading(); |
| 138 | |
| 139 | if (m_options.securityCheck == SecurityCheckPolicy::DoSecurityCheck && !m_frame->document()->securityOrigin().canDisplay(clientRequest.url())) { |
| 140 | FrameLoader::reportLocalLoadFailed(m_frame.get(), clientRequest.url().string()); |
| 141 | releaseResources(); |
| 142 | return completionHandler(false); |
| 143 | } |
| 144 | |
| 145 | // The various plug-in implementations call directly to ResourceLoader::load() instead of piping requests |
| 146 | // through FrameLoader. As a result, they miss the FrameLoader::addExtraFieldsToRequest() step which sets |
| 147 | // up the 1st party for cookies URL and Same-Site info. Until plug-in implementations can be reigned in |
| 148 | // to pipe through that method, we need to make sure there is always both a 1st party for cookies set and |
| 149 | // Same-Site info. See <https://bugs.webkit.org/show_bug.cgi?id=26391>. |
| 150 | if (clientRequest.firstPartyForCookies().isNull()) { |
| 151 | if (Document* document = m_frame->document()) |
| 152 | clientRequest.setFirstPartyForCookies(document->firstPartyForCookies()); |
| 153 | } |
| 154 | FrameLoader::addSameSiteInfoToRequestIfNeeded(clientRequest, m_frame->document()); |
| 155 | |
| 156 | willSendRequestInternal(WTFMove(clientRequest), ResourceResponse(), [this, protectedThis = makeRef(*this), completionHandler = WTFMove(completionHandler)](ResourceRequest&& request) mutable { |
| 157 | |
| 158 | #if PLATFORM(IOS_FAMILY) |
| 159 | // If this ResourceLoader was stopped as a result of willSendRequest, bail out. |
| 160 | if (m_reachedTerminalState) |
| 161 | return completionHandler(false); |
| 162 | #endif |
| 163 | |
| 164 | if (request.isNull()) { |
| 165 | cancel(); |
| 166 | return completionHandler(false); |
| 167 | } |
| 168 | |
| 169 | m_request = WTFMove(request); |
| 170 | m_originalRequest = m_request; |
| 171 | completionHandler(true); |
| 172 | }); |
| 173 | } |
| 174 | |
| 175 | void ResourceLoader::deliverResponseAndData(const ResourceResponse& response, RefPtr<SharedBuffer>&& buffer) |
| 176 | { |
| 177 | didReceiveResponse(response, [this, protectedThis = makeRef(*this), buffer = WTFMove(buffer)]() mutable { |
| 178 | if (reachedTerminalState()) |
| 179 | return; |
| 180 | |
| 181 | if (buffer) { |
| 182 | unsigned size = buffer->size(); |
| 183 | didReceiveBuffer(buffer.releaseNonNull(), size, DataPayloadWholeResource); |
| 184 | if (reachedTerminalState()) |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | NetworkLoadMetrics emptyMetrics; |
| 189 | didFinishLoading(emptyMetrics); |
| 190 | }); |
| 191 | } |
| 192 | |
| 193 | void ResourceLoader::start() |
| 194 | { |
| 195 | ASSERT(!m_handle); |
| 196 | ASSERT(!m_request.isNull()); |
| 197 | ASSERT(m_deferredRequest.isNull()); |
| 198 | ASSERT(frameLoader()); |
| 199 | |
| 200 | #if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML) |
| 201 | if (m_documentLoader->scheduleArchiveLoad(*this, m_request)) |
| 202 | return; |
| 203 | #endif |
| 204 | |
| 205 | if (m_documentLoader->applicationCacheHost().maybeLoadResource(*this, m_request, m_request.url())) |
| 206 | return; |
| 207 | |
| 208 | if (m_defersLoading) { |
| 209 | m_deferredRequest = m_request; |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | if (m_reachedTerminalState) |
| 214 | return; |
| 215 | |
| 216 | if (m_request.url().protocolIsData()) { |
| 217 | loadDataURL(); |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | #if USE(SOUP) |
| 222 | if (m_request.url().protocolIs("resource" )) { |
| 223 | loadGResource(); |
| 224 | return; |
| 225 | } |
| 226 | #endif |
| 227 | |
| 228 | m_handle = ResourceHandle::create(frameLoader()->networkingContext(), m_request, this, m_defersLoading, m_options.sniffContent == ContentSniffingPolicy::SniffContent, m_options.sniffContentEncoding == ContentEncodingSniffingPolicy::Sniff); |
| 229 | } |
| 230 | |
| 231 | void ResourceLoader::setDefersLoading(bool defers) |
| 232 | { |
| 233 | if (m_options.defersLoadingPolicy == DefersLoadingPolicy::DisallowDefersLoading) |
| 234 | return; |
| 235 | |
| 236 | m_defersLoading = defers; |
| 237 | if (m_handle) |
| 238 | m_handle->setDefersLoading(defers); |
| 239 | |
| 240 | platformStrategies()->loaderStrategy()->setDefersLoading(*this, defers); |
| 241 | } |
| 242 | |
| 243 | FrameLoader* ResourceLoader::frameLoader() const |
| 244 | { |
| 245 | if (!m_frame) |
| 246 | return nullptr; |
| 247 | return &m_frame->loader(); |
| 248 | } |
| 249 | |
| 250 | void ResourceLoader::loadDataURL() |
| 251 | { |
| 252 | auto url = m_request.url(); |
| 253 | ASSERT(url.protocolIsData()); |
| 254 | |
| 255 | DataURLDecoder::ScheduleContext scheduleContext; |
| 256 | #if HAVE(RUNLOOP_TIMER) |
| 257 | if (auto* scheduledPairs = m_frame->page()->scheduledRunLoopPairs()) |
| 258 | scheduleContext.scheduledPairs = *scheduledPairs; |
| 259 | #endif |
| 260 | DataURLDecoder::decode(url, scheduleContext, [this, protectedThis = makeRef(*this), url](auto decodeResult) mutable { |
| 261 | if (this->reachedTerminalState()) |
| 262 | return; |
| 263 | if (!decodeResult) { |
| 264 | RELEASE_LOG_IF_ALLOWED("loadDataURL: decoding of data failed (frame = %p, frameLoader = %p, resourceID = %lu)" , frame(), frameLoader(), identifier()); |
| 265 | protectedThis->didFail(ResourceError(errorDomainWebKitInternal, 0, url, "Data URL decoding failed" )); |
| 266 | return; |
| 267 | } |
| 268 | if (this->wasCancelled()) |
| 269 | return; |
| 270 | auto& result = decodeResult.value(); |
| 271 | auto dataSize = result.data ? result.data->size() : 0; |
| 272 | |
| 273 | ResourceResponse dataResponse { url, result.mimeType, static_cast<long long>(dataSize), result.charset }; |
| 274 | dataResponse.setHTTPStatusCode(200); |
| 275 | dataResponse.setHTTPStatusText("OK"_s ); |
| 276 | dataResponse.setHTTPHeaderField(HTTPHeaderName::ContentType, result.contentType); |
| 277 | dataResponse.setSource(ResourceResponse::Source::Network); |
| 278 | this->didReceiveResponse(dataResponse, [this, protectedThis = WTFMove(protectedThis), dataSize, data = result.data.releaseNonNull()]() mutable { |
| 279 | if (!this->reachedTerminalState() && dataSize) |
| 280 | this->didReceiveBuffer(WTFMove(data), dataSize, DataPayloadWholeResource); |
| 281 | |
| 282 | if (!this->reachedTerminalState()) { |
| 283 | NetworkLoadMetrics emptyMetrics; |
| 284 | this->didFinishLoading(emptyMetrics); |
| 285 | } |
| 286 | }); |
| 287 | }); |
| 288 | } |
| 289 | |
| 290 | void ResourceLoader::setDataBufferingPolicy(DataBufferingPolicy dataBufferingPolicy) |
| 291 | { |
| 292 | m_options.dataBufferingPolicy = dataBufferingPolicy; |
| 293 | |
| 294 | // Reset any already buffered data |
| 295 | if (dataBufferingPolicy == DataBufferingPolicy::DoNotBufferData) |
| 296 | m_resourceData = nullptr; |
| 297 | } |
| 298 | |
| 299 | void ResourceLoader::willSwitchToSubstituteResource() |
| 300 | { |
| 301 | ASSERT(!m_documentLoader->isSubstituteLoadPending(this)); |
| 302 | platformStrategies()->loaderStrategy()->remove(this); |
| 303 | if (m_handle) |
| 304 | m_handle->cancel(); |
| 305 | } |
| 306 | |
| 307 | void ResourceLoader::addDataOrBuffer(const char* data, unsigned length, SharedBuffer* buffer, DataPayloadType dataPayloadType) |
| 308 | { |
| 309 | if (m_options.dataBufferingPolicy == DataBufferingPolicy::DoNotBufferData) |
| 310 | return; |
| 311 | |
| 312 | if (!m_resourceData || dataPayloadType == DataPayloadWholeResource) { |
| 313 | if (buffer) |
| 314 | m_resourceData = buffer; |
| 315 | else |
| 316 | m_resourceData = SharedBuffer::create(data, length); |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | if (buffer) |
| 321 | m_resourceData->append(*buffer); |
| 322 | else |
| 323 | m_resourceData->append(data, length); |
| 324 | } |
| 325 | |
| 326 | void ResourceLoader::clearResourceData() |
| 327 | { |
| 328 | if (m_resourceData) |
| 329 | m_resourceData->clear(); |
| 330 | } |
| 331 | |
| 332 | bool ResourceLoader::isSubresourceLoader() const |
| 333 | { |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | void ResourceLoader::willSendRequestInternal(ResourceRequest&& request, const ResourceResponse& redirectResponse, CompletionHandler<void(ResourceRequest&&)>&& completionHandler) |
| 338 | { |
| 339 | // Protect this in this delegate method since the additional processing can do |
| 340 | // anything including possibly derefing this; one example of this is Radar 3266216. |
| 341 | Ref<ResourceLoader> protectedThis(*this); |
| 342 | |
| 343 | ASSERT(!m_reachedTerminalState); |
| 344 | #if ENABLE(CONTENT_EXTENSIONS) |
| 345 | ASSERT(m_resourceType != ContentExtensions::ResourceType::Invalid); |
| 346 | #endif |
| 347 | |
| 348 | // We need a resource identifier for all requests, even if FrameLoader is never going to see it (such as with CORS preflight requests). |
| 349 | bool createdResourceIdentifier = false; |
| 350 | if (!m_identifier) { |
| 351 | m_identifier = m_frame->page()->progress().createUniqueIdentifier(); |
| 352 | createdResourceIdentifier = true; |
| 353 | } |
| 354 | |
| 355 | #if ENABLE(CONTENT_EXTENSIONS) |
| 356 | if (!redirectResponse.isNull() && frameLoader()) { |
| 357 | Page* page = frameLoader()->frame().page(); |
| 358 | if (page && m_documentLoader) { |
| 359 | auto results = page->userContentProvider().processContentRuleListsForLoad(request.url(), m_resourceType, *m_documentLoader); |
| 360 | bool blockedLoad = results.summary.blockedLoad; |
| 361 | ContentExtensions::applyResultsToRequest(WTFMove(results), page, request); |
| 362 | if (blockedLoad) { |
| 363 | RELEASE_LOG_IF_ALLOWED("willSendRequestinternal: resource load canceled because of content blocker (frame = %p, frameLoader = %p, resourceID = %lu)" , frame(), frameLoader(), identifier()); |
| 364 | didFail(blockedByContentBlockerError()); |
| 365 | completionHandler({ }); |
| 366 | return; |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | #endif |
| 371 | |
| 372 | if (request.isNull()) { |
| 373 | RELEASE_LOG_IF_ALLOWED("willSendRequestinternal: resource load canceled because of empty request (frame = %p, frameLoader = %p, resourceID = %lu)" , frame(), frameLoader(), identifier()); |
| 374 | didFail(cannotShowURLError()); |
| 375 | completionHandler({ }); |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | if (m_options.sendLoadCallbacks == SendCallbackPolicy::SendCallbacks) { |
| 380 | if (createdResourceIdentifier) |
| 381 | frameLoader()->notifier().assignIdentifierToInitialRequest(m_identifier, documentLoader(), request); |
| 382 | |
| 383 | #if PLATFORM(IOS_FAMILY) |
| 384 | // If this ResourceLoader was stopped as a result of assignIdentifierToInitialRequest, bail out |
| 385 | if (m_reachedTerminalState) { |
| 386 | completionHandler(WTFMove(request)); |
| 387 | return; |
| 388 | } |
| 389 | #endif |
| 390 | |
| 391 | frameLoader()->notifier().willSendRequest(this, request, redirectResponse); |
| 392 | } |
| 393 | else |
| 394 | InspectorInstrumentation::willSendRequest(m_frame.get(), m_identifier, m_frame->loader().documentLoader(), request, redirectResponse); |
| 395 | |
| 396 | #if USE(QUICK_LOOK) |
| 397 | if (auto previewConverter = m_documentLoader->previewConverter()) |
| 398 | request = previewConverter->safeRequest(request); |
| 399 | #endif |
| 400 | |
| 401 | bool isRedirect = !redirectResponse.isNull(); |
| 402 | if (isRedirect) |
| 403 | platformStrategies()->loaderStrategy()->crossOriginRedirectReceived(this, request.url()); |
| 404 | |
| 405 | m_request = request; |
| 406 | |
| 407 | if (isRedirect) { |
| 408 | auto& redirectURL = request.url(); |
| 409 | if (!m_documentLoader->isCommitted()) |
| 410 | frameLoader()->client().dispatchDidReceiveServerRedirectForProvisionalLoad(); |
| 411 | |
| 412 | if (redirectURL.protocolIsData()) { |
| 413 | // Handle data URL decoding locally. |
| 414 | finishNetworkLoad(); |
| 415 | loadDataURL(); |
| 416 | } |
| 417 | } |
| 418 | completionHandler(WTFMove(request)); |
| 419 | } |
| 420 | |
| 421 | void ResourceLoader::willSendRequest(ResourceRequest&& request, const ResourceResponse& redirectResponse, CompletionHandler<void(ResourceRequest&&)>&& completionHandler) |
| 422 | { |
| 423 | willSendRequestInternal(WTFMove(request), redirectResponse, WTFMove(completionHandler)); |
| 424 | } |
| 425 | |
| 426 | void ResourceLoader::didSendData(unsigned long long, unsigned long long) |
| 427 | { |
| 428 | } |
| 429 | |
| 430 | static void logResourceResponseSource(Frame* frame, ResourceResponse::Source source) |
| 431 | { |
| 432 | if (!frame || !frame->page()) |
| 433 | return; |
| 434 | |
| 435 | String sourceKey; |
| 436 | switch (source) { |
| 437 | case ResourceResponse::Source::Network: |
| 438 | sourceKey = DiagnosticLoggingKeys::networkKey(); |
| 439 | break; |
| 440 | case ResourceResponse::Source::DiskCache: |
| 441 | sourceKey = DiagnosticLoggingKeys::diskCacheKey(); |
| 442 | break; |
| 443 | case ResourceResponse::Source::DiskCacheAfterValidation: |
| 444 | sourceKey = DiagnosticLoggingKeys::diskCacheAfterValidationKey(); |
| 445 | break; |
| 446 | case ResourceResponse::Source::ServiceWorker: |
| 447 | sourceKey = DiagnosticLoggingKeys::serviceWorkerKey(); |
| 448 | break; |
| 449 | case ResourceResponse::Source::MemoryCache: |
| 450 | case ResourceResponse::Source::MemoryCacheAfterValidation: |
| 451 | case ResourceResponse::Source::ApplicationCache: |
| 452 | case ResourceResponse::Source::Unknown: |
| 453 | return; |
| 454 | } |
| 455 | |
| 456 | frame->page()->diagnosticLoggingClient().logDiagnosticMessage(DiagnosticLoggingKeys::resourceResponseSourceKey(), sourceKey, ShouldSample::Yes); |
| 457 | } |
| 458 | |
| 459 | bool ResourceLoader::shouldAllowResourceToAskForCredentials() const |
| 460 | { |
| 461 | return m_canCrossOriginRequestsAskUserForCredentials || m_frame->tree().top().document()->securityOrigin().canRequest(m_request.url()); |
| 462 | } |
| 463 | |
| 464 | void ResourceLoader::didBlockAuthenticationChallenge() |
| 465 | { |
| 466 | m_wasAuthenticationChallengeBlocked = true; |
| 467 | if (m_options.clientCredentialPolicy == ClientCredentialPolicy::CannotAskClientForCredentials) |
| 468 | return; |
| 469 | ASSERT(!shouldAllowResourceToAskForCredentials()); |
| 470 | FrameLoader::reportAuthenticationChallengeBlocked(m_frame.get(), m_request.url(), "it is a cross-origin request"_s ); |
| 471 | } |
| 472 | |
| 473 | void ResourceLoader::didReceiveResponse(const ResourceResponse& r, CompletionHandler<void()>&& policyCompletionHandler) |
| 474 | { |
| 475 | ASSERT(!m_reachedTerminalState); |
| 476 | CompletionHandlerCallingScope completionHandlerCaller(WTFMove(policyCompletionHandler)); |
| 477 | |
| 478 | // Protect this in this delegate method since the additional processing can do |
| 479 | // anything including possibly derefing this; one example of this is Radar 3266216. |
| 480 | Ref<ResourceLoader> protectedThis(*this); |
| 481 | |
| 482 | logResourceResponseSource(m_frame.get(), r.source()); |
| 483 | |
| 484 | m_response = r; |
| 485 | |
| 486 | if (FormData* data = m_request.httpBody()) |
| 487 | data->removeGeneratedFilesIfNeeded(); |
| 488 | |
| 489 | if (m_options.sendLoadCallbacks == SendCallbackPolicy::SendCallbacks) |
| 490 | frameLoader()->notifier().didReceiveResponse(this, m_response); |
| 491 | } |
| 492 | |
| 493 | void ResourceLoader::didReceiveData(const char* data, unsigned length, long long encodedDataLength, DataPayloadType dataPayloadType) |
| 494 | { |
| 495 | // The following assertions are not quite valid here, since a subclass |
| 496 | // might override didReceiveData in a way that invalidates them. This |
| 497 | // happens with the steps listed in 3266216 |
| 498 | // ASSERT(con == connection); |
| 499 | // ASSERT(!m_reachedTerminalState); |
| 500 | |
| 501 | didReceiveDataOrBuffer(data, length, nullptr, encodedDataLength, dataPayloadType); |
| 502 | } |
| 503 | |
| 504 | void ResourceLoader::didReceiveBuffer(Ref<SharedBuffer>&& buffer, long long encodedDataLength, DataPayloadType dataPayloadType) |
| 505 | { |
| 506 | didReceiveDataOrBuffer(nullptr, 0, WTFMove(buffer), encodedDataLength, dataPayloadType); |
| 507 | } |
| 508 | |
| 509 | void ResourceLoader::didReceiveDataOrBuffer(const char* data, unsigned length, RefPtr<SharedBuffer>&& buffer, long long encodedDataLength, DataPayloadType dataPayloadType) |
| 510 | { |
| 511 | // This method should only get data+length *OR* a SharedBuffer. |
| 512 | ASSERT(!buffer || (!data && !length)); |
| 513 | |
| 514 | // Protect this in this delegate method since the additional processing can do |
| 515 | // anything including possibly derefing this; one example of this is Radar 3266216. |
| 516 | Ref<ResourceLoader> protectedThis(*this); |
| 517 | |
| 518 | addDataOrBuffer(data, length, buffer.get(), dataPayloadType); |
| 519 | |
| 520 | // FIXME: If we get a resource with more than 2B bytes, this code won't do the right thing. |
| 521 | // However, with today's computers and networking speeds, this won't happen in practice. |
| 522 | // Could be an issue with a giant local file. |
| 523 | if (m_options.sendLoadCallbacks == SendCallbackPolicy::SendCallbacks && m_frame) |
| 524 | frameLoader()->notifier().didReceiveData(this, buffer ? buffer->data() : data, buffer ? buffer->size() : length, static_cast<int>(encodedDataLength)); |
| 525 | } |
| 526 | |
| 527 | void ResourceLoader::didFinishLoading(const NetworkLoadMetrics& networkLoadMetrics) |
| 528 | { |
| 529 | RELEASE_LOG_IF_ALLOWED("didFinishLoading: (frame = %p, frameLoader = %p, resourceID = %lu)" , frame(), frameLoader(), identifier()); |
| 530 | |
| 531 | didFinishLoadingOnePart(networkLoadMetrics); |
| 532 | |
| 533 | // If the load has been cancelled by a delegate in response to didFinishLoad(), do not release |
| 534 | // the resources a second time, they have been released by cancel. |
| 535 | if (wasCancelled()) |
| 536 | return; |
| 537 | releaseResources(); |
| 538 | } |
| 539 | |
| 540 | void ResourceLoader::didFinishLoadingOnePart(const NetworkLoadMetrics& networkLoadMetrics) |
| 541 | { |
| 542 | // If load has been cancelled after finishing (which could happen with a |
| 543 | // JavaScript that changes the window location), do nothing. |
| 544 | if (wasCancelled()) |
| 545 | return; |
| 546 | ASSERT(!m_reachedTerminalState); |
| 547 | |
| 548 | if (m_notifiedLoadComplete) |
| 549 | return; |
| 550 | m_notifiedLoadComplete = true; |
| 551 | if (m_options.sendLoadCallbacks == SendCallbackPolicy::SendCallbacks) |
| 552 | frameLoader()->notifier().didFinishLoad(this, networkLoadMetrics); |
| 553 | } |
| 554 | |
| 555 | void ResourceLoader::didFail(const ResourceError& error) |
| 556 | { |
| 557 | RELEASE_LOG_IF_ALLOWED("didFail: (frame = %p, frameLoader = %p, resourceID = %lu)" , frame(), frameLoader(), identifier()); |
| 558 | |
| 559 | if (wasCancelled()) |
| 560 | return; |
| 561 | ASSERT(!m_reachedTerminalState); |
| 562 | |
| 563 | // Protect this in this delegate method since the additional processing can do |
| 564 | // anything including possibly derefing this; one example of this is Radar 3266216. |
| 565 | Ref<ResourceLoader> protectedThis(*this); |
| 566 | |
| 567 | cleanupForError(error); |
| 568 | releaseResources(); |
| 569 | } |
| 570 | |
| 571 | void ResourceLoader::cleanupForError(const ResourceError& error) |
| 572 | { |
| 573 | if (FormData* data = m_request.httpBody()) |
| 574 | data->removeGeneratedFilesIfNeeded(); |
| 575 | |
| 576 | if (m_notifiedLoadComplete) |
| 577 | return; |
| 578 | m_notifiedLoadComplete = true; |
| 579 | if (m_options.sendLoadCallbacks == SendCallbackPolicy::SendCallbacks && m_identifier) |
| 580 | frameLoader()->notifier().didFailToLoad(this, error); |
| 581 | } |
| 582 | |
| 583 | void ResourceLoader::cancel() |
| 584 | { |
| 585 | cancel(ResourceError()); |
| 586 | } |
| 587 | |
| 588 | void ResourceLoader::cancel(const ResourceError& error) |
| 589 | { |
| 590 | // If the load has already completed - succeeded, failed, or previously cancelled - do nothing. |
| 591 | if (m_reachedTerminalState) |
| 592 | return; |
| 593 | |
| 594 | ResourceError nonNullError = error.isNull() ? cancelledError() : error; |
| 595 | |
| 596 | // willCancel() and didFailToLoad() both call out to clients that might do |
| 597 | // something causing the last reference to this object to go away. |
| 598 | Ref<ResourceLoader> protectedThis(*this); |
| 599 | |
| 600 | // If we re-enter cancel() from inside willCancel(), we want to pick up from where we left |
| 601 | // off without re-running willCancel() |
| 602 | if (m_cancellationStatus == NotCancelled) { |
| 603 | m_cancellationStatus = CalledWillCancel; |
| 604 | |
| 605 | willCancel(nonNullError); |
| 606 | } |
| 607 | |
| 608 | // If we re-enter cancel() from inside didFailToLoad(), we want to pick up from where we |
| 609 | // left off without redoing any of this work. |
| 610 | if (m_cancellationStatus == CalledWillCancel) { |
| 611 | m_cancellationStatus = Cancelled; |
| 612 | |
| 613 | if (m_handle) |
| 614 | m_handle->clearAuthentication(); |
| 615 | |
| 616 | m_documentLoader->cancelPendingSubstituteLoad(this); |
| 617 | if (m_handle) { |
| 618 | m_handle->cancel(); |
| 619 | m_handle = nullptr; |
| 620 | } |
| 621 | cleanupForError(nonNullError); |
| 622 | } |
| 623 | |
| 624 | // If cancel() completed from within the call to willCancel() or didFailToLoad(), |
| 625 | // we don't want to redo didCancel() or releasesResources(). |
| 626 | if (m_reachedTerminalState) |
| 627 | return; |
| 628 | |
| 629 | didCancel(nonNullError); |
| 630 | |
| 631 | if (m_cancellationStatus == FinishedCancel) |
| 632 | return; |
| 633 | m_cancellationStatus = FinishedCancel; |
| 634 | |
| 635 | releaseResources(); |
| 636 | } |
| 637 | |
| 638 | ResourceError ResourceLoader::cancelledError() |
| 639 | { |
| 640 | return frameLoader()->cancelledError(m_request); |
| 641 | } |
| 642 | |
| 643 | ResourceError ResourceLoader::blockedError() |
| 644 | { |
| 645 | return frameLoader()->client().blockedError(m_request); |
| 646 | } |
| 647 | |
| 648 | ResourceError ResourceLoader::blockedByContentBlockerError() |
| 649 | { |
| 650 | return frameLoader()->client().blockedByContentBlockerError(m_request); |
| 651 | } |
| 652 | |
| 653 | ResourceError ResourceLoader::cannotShowURLError() |
| 654 | { |
| 655 | return frameLoader()->client().cannotShowURLError(m_request); |
| 656 | } |
| 657 | |
| 658 | void ResourceLoader::willSendRequestAsync(ResourceHandle* handle, ResourceRequest&& request, ResourceResponse&& redirectResponse, CompletionHandler<void(ResourceRequest&&)>&& completionHandler) |
| 659 | { |
| 660 | RefPtr<ResourceHandle> protectedHandle(handle); |
| 661 | if (documentLoader()->applicationCacheHost().maybeLoadFallbackForRedirect(this, request, redirectResponse)) { |
| 662 | RELEASE_LOG_IF_ALLOWED("willSendRequestAsync: exiting early because maybeLoadFallbackForRedirect returned false (frame = %p, frameLoader = %p, resourceID = %lu)" , frame(), frameLoader(), identifier()); |
| 663 | completionHandler(WTFMove(request)); |
| 664 | return; |
| 665 | } |
| 666 | willSendRequestInternal(WTFMove(request), redirectResponse, WTFMove(completionHandler)); |
| 667 | } |
| 668 | |
| 669 | void ResourceLoader::didSendData(ResourceHandle*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent) |
| 670 | { |
| 671 | didSendData(bytesSent, totalBytesToBeSent); |
| 672 | } |
| 673 | |
| 674 | void ResourceLoader::didReceiveResponseAsync(ResourceHandle*, ResourceResponse&& response, CompletionHandler<void()>&& completionHandler) |
| 675 | { |
| 676 | if (documentLoader()->applicationCacheHost().maybeLoadFallbackForResponse(this, response)) { |
| 677 | completionHandler(); |
| 678 | return; |
| 679 | } |
| 680 | didReceiveResponse(response, WTFMove(completionHandler)); |
| 681 | } |
| 682 | |
| 683 | void ResourceLoader::didReceiveData(ResourceHandle*, const char* data, unsigned length, int encodedDataLength) |
| 684 | { |
| 685 | didReceiveData(data, length, encodedDataLength, DataPayloadBytes); |
| 686 | } |
| 687 | |
| 688 | void ResourceLoader::didReceiveBuffer(ResourceHandle*, Ref<SharedBuffer>&& buffer, int encodedDataLength) |
| 689 | { |
| 690 | didReceiveBuffer(WTFMove(buffer), encodedDataLength, DataPayloadBytes); |
| 691 | } |
| 692 | |
| 693 | void ResourceLoader::didFinishLoading(ResourceHandle*) |
| 694 | { |
| 695 | NetworkLoadMetrics emptyMetrics; |
| 696 | didFinishLoading(emptyMetrics); |
| 697 | } |
| 698 | |
| 699 | void ResourceLoader::didFail(ResourceHandle*, const ResourceError& error) |
| 700 | { |
| 701 | if (documentLoader()->applicationCacheHost().maybeLoadFallbackForError(this, error)) |
| 702 | return; |
| 703 | didFail(error); |
| 704 | } |
| 705 | |
| 706 | void ResourceLoader::wasBlocked(ResourceHandle*) |
| 707 | { |
| 708 | RELEASE_LOG_IF_ALLOWED("wasBlocked: resource load canceled because of content blocker (frame = %p, frameLoader = %p, resourceID = %lu)" , frame(), frameLoader(), identifier()); |
| 709 | didFail(blockedError()); |
| 710 | } |
| 711 | |
| 712 | void ResourceLoader::cannotShowURL(ResourceHandle*) |
| 713 | { |
| 714 | RELEASE_LOG_IF_ALLOWED("wasBlocked: resource load canceled because of invalid URL (frame = %p, frameLoader = %p, resourceID = %lu)" , frame(), frameLoader(), identifier()); |
| 715 | didFail(cannotShowURLError()); |
| 716 | } |
| 717 | |
| 718 | bool ResourceLoader::shouldUseCredentialStorage() |
| 719 | { |
| 720 | if (m_options.storedCredentialsPolicy != StoredCredentialsPolicy::Use) |
| 721 | return false; |
| 722 | |
| 723 | Ref<ResourceLoader> protectedThis(*this); |
| 724 | return frameLoader()->client().shouldUseCredentialStorage(documentLoader(), identifier()); |
| 725 | } |
| 726 | |
| 727 | bool ResourceLoader::isAllowedToAskUserForCredentials() const |
| 728 | { |
| 729 | if (m_options.clientCredentialPolicy == ClientCredentialPolicy::CannotAskClientForCredentials) |
| 730 | return false; |
| 731 | if (!shouldAllowResourceToAskForCredentials()) |
| 732 | return false; |
| 733 | return m_options.credentials == FetchOptions::Credentials::Include || (m_options.credentials == FetchOptions::Credentials::SameOrigin && m_frame->document()->securityOrigin().canRequest(originalRequest().url())); |
| 734 | } |
| 735 | |
| 736 | bool ResourceLoader::shouldIncludeCertificateInfo() const |
| 737 | { |
| 738 | if (m_options.certificateInfoPolicy == CertificateInfoPolicy::IncludeCertificateInfo) |
| 739 | return true; |
| 740 | if (UNLIKELY(InspectorInstrumentation::hasFrontends())) |
| 741 | return true; |
| 742 | return false; |
| 743 | } |
| 744 | |
| 745 | void ResourceLoader::didReceiveAuthenticationChallenge(ResourceHandle* handle, const AuthenticationChallenge& challenge) |
| 746 | { |
| 747 | ASSERT_UNUSED(handle, handle == m_handle); |
| 748 | ASSERT(m_handle->hasAuthenticationChallenge()); |
| 749 | |
| 750 | // Protect this in this delegate method since the additional processing can do |
| 751 | // anything including possibly derefing this; one example of this is Radar 3266216. |
| 752 | Ref<ResourceLoader> protectedThis(*this); |
| 753 | |
| 754 | if (m_options.storedCredentialsPolicy == StoredCredentialsPolicy::Use) { |
| 755 | if (isAllowedToAskUserForCredentials()) { |
| 756 | frameLoader()->notifier().didReceiveAuthenticationChallenge(this, challenge); |
| 757 | return; |
| 758 | } |
| 759 | didBlockAuthenticationChallenge(); |
| 760 | } |
| 761 | challenge.authenticationClient()->receivedRequestToContinueWithoutCredential(challenge); |
| 762 | ASSERT(!m_handle || !m_handle->hasAuthenticationChallenge()); |
| 763 | } |
| 764 | |
| 765 | #if USE(PROTECTION_SPACE_AUTH_CALLBACK) |
| 766 | void ResourceLoader::canAuthenticateAgainstProtectionSpaceAsync(ResourceHandle*, const ProtectionSpace& protectionSpace, CompletionHandler<void(bool)>&& completionHandler) |
| 767 | { |
| 768 | completionHandler(canAuthenticateAgainstProtectionSpace(protectionSpace)); |
| 769 | } |
| 770 | |
| 771 | bool ResourceLoader::canAuthenticateAgainstProtectionSpace(const ProtectionSpace& protectionSpace) |
| 772 | { |
| 773 | Ref<ResourceLoader> protectedThis(*this); |
| 774 | return frameLoader()->client().canAuthenticateAgainstProtectionSpace(documentLoader(), identifier(), protectionSpace); |
| 775 | } |
| 776 | |
| 777 | #endif |
| 778 | |
| 779 | #if PLATFORM(IOS_FAMILY) |
| 780 | |
| 781 | RetainPtr<CFDictionaryRef> ResourceLoader::connectionProperties(ResourceHandle*) |
| 782 | { |
| 783 | return frameLoader()->connectionProperties(this); |
| 784 | } |
| 785 | |
| 786 | #endif |
| 787 | |
| 788 | void ResourceLoader::receivedCancellation(const AuthenticationChallenge&) |
| 789 | { |
| 790 | cancel(); |
| 791 | } |
| 792 | |
| 793 | #if PLATFORM(COCOA) |
| 794 | |
| 795 | void ResourceLoader::schedule(SchedulePair& pair) |
| 796 | { |
| 797 | if (m_handle) |
| 798 | m_handle->schedule(pair); |
| 799 | } |
| 800 | |
| 801 | void ResourceLoader::unschedule(SchedulePair& pair) |
| 802 | { |
| 803 | if (m_handle) |
| 804 | m_handle->unschedule(pair); |
| 805 | } |
| 806 | |
| 807 | #endif |
| 808 | |
| 809 | #if USE(QUICK_LOOK) |
| 810 | bool ResourceLoader::isQuickLookResource() const |
| 811 | { |
| 812 | return !!m_previewLoader; |
| 813 | } |
| 814 | #endif |
| 815 | |
| 816 | bool ResourceLoader::isAlwaysOnLoggingAllowed() const |
| 817 | { |
| 818 | return frameLoader() && frameLoader()->isAlwaysOnLoggingAllowed(); |
| 819 | } |
| 820 | |
| 821 | } |
| 822 | |