AsynchronousNetworkLoaderClient.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C) 2013 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. * 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.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "AsynchronousNetworkLoaderClient.h"
  27. #include "DataReference.h"
  28. #include "NetworkResourceLoader.h"
  29. #include "PlatformCertificateInfo.h"
  30. #include "WebCoreArgumentCoders.h"
  31. #include "WebResourceLoaderMessages.h"
  32. #include <WebCore/ResourceError.h>
  33. #include <WebCore/SharedBuffer.h>
  34. #include <wtf/CurrentTime.h>
  35. #if ENABLE(NETWORK_PROCESS)
  36. using namespace WebCore;
  37. namespace WebKit {
  38. AsynchronousNetworkLoaderClient::AsynchronousNetworkLoaderClient()
  39. {
  40. }
  41. void AsynchronousNetworkLoaderClient::willSendRequest(NetworkResourceLoader* loader, ResourceRequest& request, const ResourceResponse& redirectResponse)
  42. {
  43. // This message is DispatchMessageEvenWhenWaitingForSyncReply to avoid a situation where the NetworkProcess is deadlocked
  44. // waiting for 6 connections to complete while the WebProcess is waiting for a 7th (Synchronous XHR) to complete.
  45. loader->sendAbortingOnFailure(Messages::WebResourceLoader::WillSendRequest(request, redirectResponse), CoreIPC::DispatchMessageEvenWhenWaitingForSyncReply);
  46. }
  47. void AsynchronousNetworkLoaderClient::canAuthenticateAgainstProtectionSpace(NetworkResourceLoader* loader, const ProtectionSpace& protectionSpace)
  48. {
  49. // This message is DispatchMessageEvenWhenWaitingForSyncReply to avoid a situation where the NetworkProcess is deadlocked
  50. // waiting for 6 connections to complete while the WebProcess is waiting for a 7th (Synchronous XHR) to complete.
  51. loader->sendAbortingOnFailure(Messages::WebResourceLoader::CanAuthenticateAgainstProtectionSpace(protectionSpace), CoreIPC::DispatchMessageEvenWhenWaitingForSyncReply);
  52. }
  53. void AsynchronousNetworkLoaderClient::didReceiveResponse(NetworkResourceLoader* loader, const ResourceResponse& response)
  54. {
  55. loader->sendAbortingOnFailure(Messages::WebResourceLoader::DidReceiveResponseWithCertificateInfo(response, PlatformCertificateInfo(response), loader->isLoadingMainResource()));
  56. }
  57. void AsynchronousNetworkLoaderClient::didReceiveBuffer(NetworkResourceLoader* loader, SharedBuffer* buffer, int encodedDataLength)
  58. {
  59. #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
  60. ShareableResource::Handle shareableResourceHandle;
  61. NetworkResourceLoader::tryGetShareableHandleFromSharedBuffer(shareableResourceHandle, buffer);
  62. if (!shareableResourceHandle.isNull()) {
  63. // Since we're delivering this resource by ourselves all at once and don't need anymore data or callbacks from the network layer, abort the loader.
  64. loader->abort();
  65. loader->send(Messages::WebResourceLoader::DidReceiveResource(shareableResourceHandle, currentTime()));
  66. return;
  67. }
  68. #endif // __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
  69. CoreIPC::DataReference dataReference(reinterpret_cast<const uint8_t*>(buffer->data()), buffer->size());
  70. loader->sendAbortingOnFailure(Messages::WebResourceLoader::DidReceiveData(dataReference, encodedDataLength));
  71. }
  72. void AsynchronousNetworkLoaderClient::didSendData(NetworkResourceLoader* loader, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
  73. {
  74. loader->send(Messages::WebResourceLoader::DidSendData(bytesSent, totalBytesToBeSent));
  75. }
  76. void AsynchronousNetworkLoaderClient::didFinishLoading(NetworkResourceLoader* loader, double finishTime)
  77. {
  78. loader->send(Messages::WebResourceLoader::DidFinishResourceLoad(finishTime));
  79. }
  80. void AsynchronousNetworkLoaderClient::didFail(NetworkResourceLoader* loader, const ResourceError& error)
  81. {
  82. loader->send(Messages::WebResourceLoader::DidFailResourceLoad(error));
  83. }
  84. } // namespace WebKit
  85. #endif // ENABLE(NETWORK_PROCESS)