NetworkResourceLoader.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (C) 2012 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. #ifndef NetworkResourceLoader_h
  26. #define NetworkResourceLoader_h
  27. #if ENABLE(NETWORK_PROCESS)
  28. #include "HostRecord.h"
  29. #include "MessageSender.h"
  30. #include "NetworkConnectionToWebProcessMessages.h"
  31. #include "ShareableResource.h"
  32. #include <WebCore/ResourceHandleClient.h>
  33. #include <WebCore/ResourceLoaderOptions.h>
  34. #include <WebCore/ResourceRequest.h>
  35. #include <WebCore/RunLoop.h>
  36. #include <wtf/MainThread.h>
  37. typedef const struct _CFCachedURLResponse* CFCachedURLResponseRef;
  38. namespace WebCore {
  39. class ResourceBuffer;
  40. class ResourceHandle;
  41. class ResourceRequest;
  42. }
  43. namespace WebKit {
  44. class NetworkConnectionToWebProcess;
  45. class NetworkLoaderClient;
  46. class NetworkResourceLoadParameters;
  47. class RemoteNetworkingContext;
  48. class SandboxExtension;
  49. class NetworkResourceLoader : public RefCounted<NetworkResourceLoader>, public WebCore::ResourceHandleClient, public CoreIPC::MessageSender {
  50. public:
  51. static RefPtr<NetworkResourceLoader> create(const NetworkResourceLoadParameters& parameters, NetworkConnectionToWebProcess* connection)
  52. {
  53. return adoptRef(new NetworkResourceLoader(parameters, connection, nullptr));
  54. }
  55. static RefPtr<NetworkResourceLoader> create(const NetworkResourceLoadParameters& parameters, NetworkConnectionToWebProcess* connection, PassRefPtr<Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply> reply)
  56. {
  57. return adoptRef(new NetworkResourceLoader(parameters, connection, reply));
  58. }
  59. ~NetworkResourceLoader();
  60. NetworkConnectionToWebProcess* connectionToWebProcess() const { return m_connection.get(); }
  61. WebCore::ResourceLoadPriority priority() { return m_priority; }
  62. WebCore::ResourceRequest& request() { return m_request; }
  63. WebCore::ResourceHandle* handle() const { return m_handle.get(); }
  64. void didConvertHandleToDownload();
  65. void start();
  66. void abort();
  67. // ResourceHandleClient methods
  68. virtual void willSendRequestAsync(WebCore::ResourceHandle*, const WebCore::ResourceRequest&, const WebCore::ResourceResponse& redirectResponse) OVERRIDE;
  69. virtual void didSendData(WebCore::ResourceHandle*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent) OVERRIDE;
  70. virtual void didReceiveResponseAsync(WebCore::ResourceHandle*, const WebCore::ResourceResponse&) OVERRIDE;
  71. virtual void didReceiveData(WebCore::ResourceHandle*, const char*, int, int encodedDataLength) OVERRIDE;
  72. virtual void didReceiveBuffer(WebCore::ResourceHandle*, PassRefPtr<WebCore::SharedBuffer>, int encodedDataLength) OVERRIDE;
  73. virtual void didFinishLoading(WebCore::ResourceHandle*, double finishTime) OVERRIDE;
  74. virtual void didFail(WebCore::ResourceHandle*, const WebCore::ResourceError&) OVERRIDE;
  75. virtual void wasBlocked(WebCore::ResourceHandle*) OVERRIDE;
  76. virtual void cannotShowURL(WebCore::ResourceHandle*) OVERRIDE;
  77. virtual bool shouldUseCredentialStorage(WebCore::ResourceHandle*) OVERRIDE;
  78. virtual void shouldUseCredentialStorageAsync(WebCore::ResourceHandle*) OVERRIDE;
  79. virtual void didReceiveAuthenticationChallenge(WebCore::ResourceHandle*, const WebCore::AuthenticationChallenge&) OVERRIDE;
  80. virtual void didCancelAuthenticationChallenge(WebCore::ResourceHandle*, const WebCore::AuthenticationChallenge&) OVERRIDE;
  81. virtual bool usesAsyncCallbacks() OVERRIDE { return true; }
  82. #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
  83. virtual void canAuthenticateAgainstProtectionSpaceAsync(WebCore::ResourceHandle*, const WebCore::ProtectionSpace&) OVERRIDE;
  84. #endif
  85. #if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
  86. virtual bool supportsDataArray() OVERRIDE;
  87. virtual void didReceiveDataArray(WebCore::ResourceHandle*, CFArrayRef) OVERRIDE;
  88. #endif
  89. #if PLATFORM(MAC)
  90. static size_t fileBackedResourceMinimumSize();
  91. virtual void willCacheResponseAsync(WebCore::ResourceHandle*, NSCachedURLResponse *) OVERRIDE;
  92. virtual void willStopBufferingData(WebCore::ResourceHandle*, const char*, int) OVERRIDE;
  93. #endif // PLATFORM(MAC)
  94. // Message handlers.
  95. void didReceiveNetworkResourceLoaderMessage(CoreIPC::Connection*, CoreIPC::MessageDecoder&);
  96. #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
  97. static void tryGetShareableHandleFromCFURLCachedResponse(ShareableResource::Handle&, CFCachedURLResponseRef);
  98. #endif
  99. bool isSynchronous() const;
  100. bool isLoadingMainResource() const { return m_isLoadingMainResource; }
  101. void setHostRecord(HostRecord* hostRecord) { ASSERT(isMainThread()); m_hostRecord = hostRecord; }
  102. HostRecord* hostRecord() const { ASSERT(isMainThread()); return m_hostRecord.get(); }
  103. template<typename U> bool sendAbortingOnFailure(const U& message, unsigned messageSendFlags = 0)
  104. {
  105. bool result = messageSenderConnection()->send(message, messageSenderDestinationID(), messageSendFlags);
  106. if (!result)
  107. abort();
  108. return result;
  109. }
  110. #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
  111. void continueCanAuthenticateAgainstProtectionSpace(bool);
  112. #endif
  113. void continueWillSendRequest(const WebCore::ResourceRequest& newRequest);
  114. #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
  115. static void tryGetShareableHandleFromSharedBuffer(ShareableResource::Handle&, WebCore::SharedBuffer*);
  116. #endif
  117. private:
  118. NetworkResourceLoader(const NetworkResourceLoadParameters&, NetworkConnectionToWebProcess*, PassRefPtr<Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply>);
  119. // CoreIPC::MessageSender
  120. virtual CoreIPC::Connection* messageSenderConnection() OVERRIDE;
  121. virtual uint64_t messageSenderDestinationID() OVERRIDE { return m_identifier; }
  122. void continueDidReceiveResponse();
  123. void cleanup();
  124. void platformDidReceiveResponse(const WebCore::ResourceResponse&);
  125. void consumeSandboxExtensions();
  126. void invalidateSandboxExtensions();
  127. RefPtr<RemoteNetworkingContext> m_networkingContext;
  128. RefPtr<WebCore::ResourceHandle> m_handle;
  129. // Keep the suggested request around while asynchronously asking to update it, because some parts of the request don't survive IPC.
  130. WebCore::ResourceRequest m_suggestedRequestForWillSendRequest;
  131. uint64_t m_bytesReceived;
  132. bool m_handleConvertedToDownload;
  133. OwnPtr<NetworkLoaderClient> m_networkLoaderClient;
  134. ResourceLoadIdentifier m_identifier;
  135. uint64_t m_webPageID;
  136. uint64_t m_webFrameID;
  137. WebCore::ResourceRequest m_request;
  138. WebCore::ResourceLoadPriority m_priority;
  139. WebCore::ContentSniffingPolicy m_contentSniffingPolicy;
  140. WebCore::StoredCredentials m_allowStoredCredentials;
  141. WebCore::ClientCredentialPolicy m_clientCredentialPolicy;
  142. bool m_inPrivateBrowsingMode;
  143. bool m_shouldClearReferrerOnHTTPSToHTTPRedirect;
  144. bool m_isLoadingMainResource;
  145. Vector<RefPtr<SandboxExtension>> m_requestBodySandboxExtensions;
  146. Vector<RefPtr<SandboxExtension>> m_resourceSandboxExtensions;
  147. bool m_sandboxExtensionsAreConsumed;
  148. RefPtr<NetworkConnectionToWebProcess> m_connection;
  149. RefPtr<HostRecord> m_hostRecord;
  150. };
  151. } // namespace WebKit
  152. #endif // ENABLE(NETWORK_PROCESS)
  153. #endif // NetworkResourceLoader_h