WebResourceLoader.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (C) 2012, 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 "WebResourceLoader.h"
  27. #if ENABLE(NETWORK_PROCESS)
  28. #include "DataReference.h"
  29. #include "Logging.h"
  30. #include "NetworkProcessConnection.h"
  31. #include "NetworkResourceLoaderMessages.h"
  32. #include "PlatformCertificateInfo.h"
  33. #include "WebCoreArgumentCoders.h"
  34. #include "WebErrors.h"
  35. #include "WebProcess.h"
  36. #include <WebCore/DocumentLoader.h>
  37. #include <WebCore/ResourceBuffer.h>
  38. #include <WebCore/ResourceError.h>
  39. #include <WebCore/ResourceLoader.h>
  40. using namespace WebCore;
  41. namespace WebKit {
  42. PassRefPtr<WebResourceLoader> WebResourceLoader::create(PassRefPtr<ResourceLoader> coreLoader)
  43. {
  44. return adoptRef(new WebResourceLoader(coreLoader));
  45. }
  46. WebResourceLoader::WebResourceLoader(PassRefPtr<WebCore::ResourceLoader> coreLoader)
  47. : m_coreLoader(coreLoader)
  48. {
  49. }
  50. WebResourceLoader::~WebResourceLoader()
  51. {
  52. }
  53. CoreIPC::Connection* WebResourceLoader::messageSenderConnection()
  54. {
  55. return WebProcess::shared().networkConnection()->connection();
  56. }
  57. uint64_t WebResourceLoader::messageSenderDestinationID()
  58. {
  59. return m_coreLoader->identifier();
  60. }
  61. void WebResourceLoader::cancelResourceLoader()
  62. {
  63. m_coreLoader->cancel();
  64. }
  65. void WebResourceLoader::detachFromCoreLoader()
  66. {
  67. m_coreLoader = 0;
  68. }
  69. void WebResourceLoader::willSendRequest(const ResourceRequest& proposedRequest, const ResourceResponse& redirectResponse)
  70. {
  71. LOG(Network, "(WebProcess) WebResourceLoader::willSendRequest to '%s'", proposedRequest.url().string().utf8().data());
  72. RefPtr<WebResourceLoader> protector(this);
  73. ResourceRequest newRequest = proposedRequest;
  74. m_coreLoader->willSendRequest(newRequest, redirectResponse);
  75. if (!m_coreLoader)
  76. return;
  77. send(Messages::NetworkResourceLoader::ContinueWillSendRequest(newRequest));
  78. }
  79. void WebResourceLoader::didSendData(uint64_t bytesSent, uint64_t totalBytesToBeSent)
  80. {
  81. m_coreLoader->didSendData(bytesSent, totalBytesToBeSent);
  82. }
  83. void WebResourceLoader::didReceiveResponseWithCertificateInfo(const ResourceResponse& response, const PlatformCertificateInfo& certificateInfo, bool needsContinueDidReceiveResponseMessage)
  84. {
  85. LOG(Network, "(WebProcess) WebResourceLoader::didReceiveResponseWithCertificateInfo for '%s'. Status %d.", m_coreLoader->url().string().utf8().data(), response.httpStatusCode());
  86. RefPtr<WebResourceLoader> protector(this);
  87. ResourceResponse responseCopy(response);
  88. responseCopy.setCertificateChain(certificateInfo.certificateChain());
  89. m_coreLoader->didReceiveResponse(responseCopy);
  90. // If m_coreLoader becomes null as a result of the didReceiveResponse callback, we can't use the send function().
  91. if (!m_coreLoader)
  92. return;
  93. if (needsContinueDidReceiveResponseMessage)
  94. send(Messages::NetworkResourceLoader::ContinueDidReceiveResponse());
  95. }
  96. void WebResourceLoader::didReceiveData(const CoreIPC::DataReference& data, int64_t encodedDataLength)
  97. {
  98. LOG(Network, "(WebProcess) WebResourceLoader::didReceiveData of size %i for '%s'", (int)data.size(), m_coreLoader->url().string().utf8().data());
  99. m_coreLoader->didReceiveData(reinterpret_cast<const char*>(data.data()), data.size(), encodedDataLength, DataPayloadBytes);
  100. }
  101. void WebResourceLoader::didFinishResourceLoad(double finishTime)
  102. {
  103. LOG(Network, "(WebProcess) WebResourceLoader::didFinishResourceLoad for '%s'", m_coreLoader->url().string().utf8().data());
  104. m_coreLoader->didFinishLoading(finishTime);
  105. }
  106. void WebResourceLoader::didFailResourceLoad(const ResourceError& error)
  107. {
  108. LOG(Network, "(WebProcess) WebResourceLoader::didFailResourceLoad for '%s'", m_coreLoader->url().string().utf8().data());
  109. m_coreLoader->didFail(error);
  110. }
  111. void WebResourceLoader::didReceiveResource(const ShareableResource::Handle& handle, double finishTime)
  112. {
  113. LOG(Network, "(WebProcess) WebResourceLoader::didReceiveResource for '%s'", m_coreLoader->url().string().utf8().data());
  114. RefPtr<SharedBuffer> buffer = handle.tryWrapInSharedBuffer();
  115. if (!buffer) {
  116. LOG_ERROR("Unable to create buffer from ShareableResource sent from the network process.");
  117. m_coreLoader->didFail(internalError(m_coreLoader->request().url()));
  118. return;
  119. }
  120. RefPtr<WebResourceLoader> protector(this);
  121. // Only send data to the didReceiveData callback if it exists.
  122. if (buffer->size())
  123. m_coreLoader->didReceiveBuffer(buffer.get(), buffer->size(), DataPayloadWholeResource);
  124. if (!m_coreLoader)
  125. return;
  126. m_coreLoader->didFinishLoading(finishTime);
  127. }
  128. void WebResourceLoader::canAuthenticateAgainstProtectionSpace(const ProtectionSpace& protectionSpace)
  129. {
  130. RefPtr<WebResourceLoader> protector(this);
  131. bool result = m_coreLoader->canAuthenticateAgainstProtectionSpace(protectionSpace);
  132. if (!m_coreLoader)
  133. return;
  134. send(Messages::NetworkResourceLoader::ContinueCanAuthenticateAgainstProtectionSpace(result));
  135. }
  136. } // namespace WebKit
  137. #endif // ENABLE(NETWORK_PROCESS)