NetworkConnectionToWebProcess.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #include "config.h"
  26. #include "NetworkConnectionToWebProcess.h"
  27. #include "BlobRegistrationData.h"
  28. #include "ConnectionStack.h"
  29. #include "NetworkBlobRegistry.h"
  30. #include "NetworkConnectionToWebProcessMessages.h"
  31. #include "NetworkProcess.h"
  32. #include "NetworkResourceLoadParameters.h"
  33. #include "NetworkResourceLoader.h"
  34. #include "NetworkResourceLoaderMessages.h"
  35. #include "RemoteNetworkingContext.h"
  36. #include <WebCore/BlobData.h>
  37. #include <WebCore/PlatformCookieJar.h>
  38. #include <WebCore/ResourceLoaderOptions.h>
  39. #include <WebCore/ResourceRequest.h>
  40. #include <WebCore/RunLoop.h>
  41. #if ENABLE(NETWORK_PROCESS)
  42. using namespace WebCore;
  43. namespace WebKit {
  44. PassRefPtr<NetworkConnectionToWebProcess> NetworkConnectionToWebProcess::create(CoreIPC::Connection::Identifier connectionIdentifier)
  45. {
  46. return adoptRef(new NetworkConnectionToWebProcess(connectionIdentifier));
  47. }
  48. NetworkConnectionToWebProcess::NetworkConnectionToWebProcess(CoreIPC::Connection::Identifier connectionIdentifier)
  49. : m_serialLoadingEnabled(false)
  50. {
  51. m_connection = CoreIPC::Connection::createServerConnection(connectionIdentifier, this, RunLoop::main());
  52. m_connection->setOnlySendMessagesAsDispatchWhenWaitingForSyncReplyWhenProcessingSuchAMessage(true);
  53. m_connection->open();
  54. }
  55. NetworkConnectionToWebProcess::~NetworkConnectionToWebProcess()
  56. {
  57. }
  58. void NetworkConnectionToWebProcess::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageDecoder& decoder)
  59. {
  60. if (decoder.messageReceiverName() == Messages::NetworkConnectionToWebProcess::messageReceiverName()) {
  61. didReceiveNetworkConnectionToWebProcessMessage(connection, decoder);
  62. return;
  63. }
  64. if (decoder.messageReceiverName() == Messages::NetworkResourceLoader::messageReceiverName()) {
  65. HashMap<ResourceLoadIdentifier, RefPtr<NetworkResourceLoader>>::iterator loaderIterator = m_networkResourceLoaders.find(decoder.destinationID());
  66. if (loaderIterator != m_networkResourceLoaders.end())
  67. loaderIterator->value->didReceiveNetworkResourceLoaderMessage(connection, decoder);
  68. return;
  69. }
  70. ASSERT_NOT_REACHED();
  71. }
  72. void NetworkConnectionToWebProcess::didReceiveSyncMessage(CoreIPC::Connection* connection, CoreIPC::MessageDecoder& decoder, OwnPtr<CoreIPC::MessageEncoder>& reply)
  73. {
  74. if (decoder.messageReceiverName() == Messages::NetworkConnectionToWebProcess::messageReceiverName()) {
  75. didReceiveSyncNetworkConnectionToWebProcessMessage(connection, decoder, reply);
  76. return;
  77. }
  78. ASSERT_NOT_REACHED();
  79. }
  80. void NetworkConnectionToWebProcess::didClose(CoreIPC::Connection*)
  81. {
  82. // Protect ourself as we might be otherwise be deleted during this function.
  83. RefPtr<NetworkConnectionToWebProcess> protector(this);
  84. HashMap<ResourceLoadIdentifier, RefPtr<NetworkResourceLoader>>::iterator end = m_networkResourceLoaders.end();
  85. for (HashMap<ResourceLoadIdentifier, RefPtr<NetworkResourceLoader>>::iterator i = m_networkResourceLoaders.begin(); i != end; ++i)
  86. i->value->abort();
  87. NetworkBlobRegistry::shared().connectionToWebProcessDidClose(this);
  88. m_networkResourceLoaders.clear();
  89. NetworkProcess::shared().removeNetworkConnectionToWebProcess(this);
  90. }
  91. void NetworkConnectionToWebProcess::didReceiveInvalidMessage(CoreIPC::Connection*, CoreIPC::StringReference, CoreIPC::StringReference)
  92. {
  93. }
  94. void NetworkConnectionToWebProcess::scheduleResourceLoad(const NetworkResourceLoadParameters& loadParameters)
  95. {
  96. RefPtr<NetworkResourceLoader> loader = NetworkResourceLoader::create(loadParameters, this);
  97. m_networkResourceLoaders.add(loadParameters.identifier, loader);
  98. NetworkProcess::shared().networkResourceLoadScheduler().scheduleLoader(loader.get());
  99. }
  100. void NetworkConnectionToWebProcess::performSynchronousLoad(const NetworkResourceLoadParameters& loadParameters, PassRefPtr<Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply> reply)
  101. {
  102. RefPtr<NetworkResourceLoader> loader = NetworkResourceLoader::create(loadParameters, this, reply);
  103. m_networkResourceLoaders.add(loadParameters.identifier, loader);
  104. NetworkProcess::shared().networkResourceLoadScheduler().scheduleLoader(loader.get());
  105. }
  106. void NetworkConnectionToWebProcess::removeLoadIdentifier(ResourceLoadIdentifier identifier)
  107. {
  108. RefPtr<NetworkResourceLoader> loader = m_networkResourceLoaders.take(identifier);
  109. // It's possible we have no loader for this identifier if the NetworkProcess crashed and this was a respawned NetworkProcess.
  110. if (!loader)
  111. return;
  112. // Abort the load now, as the WebProcess won't be able to respond to messages any more which might lead
  113. // to leaked loader resources (connections, threads, etc).
  114. loader->abort();
  115. }
  116. void NetworkConnectionToWebProcess::servePendingRequests(uint32_t resourceLoadPriority)
  117. {
  118. NetworkProcess::shared().networkResourceLoadScheduler().servePendingRequests(static_cast<ResourceLoadPriority>(resourceLoadPriority));
  119. }
  120. void NetworkConnectionToWebProcess::setSerialLoadingEnabled(bool enabled)
  121. {
  122. m_serialLoadingEnabled = enabled;
  123. }
  124. static NetworkStorageSession& storageSession(bool privateBrowsingEnabled)
  125. {
  126. if (privateBrowsingEnabled) {
  127. NetworkStorageSession* privateSession = RemoteNetworkingContext::privateBrowsingSession();
  128. if (privateSession)
  129. return *privateSession;
  130. // Some requests with private browsing mode requested may still be coming shortly after NetworkProcess was told to destroy its session.
  131. // FIXME: Find a way to track private browsing sessions more rigorously.
  132. LOG_ERROR("Private browsing was requested, but there was no session for it. Please file a bug unless you just disabled private browsing, in which case it's an expected race.");
  133. }
  134. return NetworkStorageSession::defaultStorageSession();
  135. }
  136. void NetworkConnectionToWebProcess::startDownload(bool privateBrowsingEnabled, uint64_t downloadID, const ResourceRequest& request)
  137. {
  138. // FIXME: Do something with the private browsing flag.
  139. NetworkProcess::shared().downloadManager().startDownload(downloadID, request);
  140. }
  141. void NetworkConnectionToWebProcess::convertMainResourceLoadToDownload(uint64_t mainResourceLoadIdentifier, uint64_t downloadID, const ResourceRequest& request, const ResourceResponse& response)
  142. {
  143. if (!mainResourceLoadIdentifier) {
  144. NetworkProcess::shared().downloadManager().startDownload(downloadID, request);
  145. return;
  146. }
  147. NetworkResourceLoader* loader = m_networkResourceLoaders.get(mainResourceLoadIdentifier);
  148. NetworkProcess::shared().downloadManager().convertHandleToDownload(downloadID, loader->handle(), request, response);
  149. // Unblock the URL connection operation queue.
  150. loader->handle()->continueDidReceiveResponse();
  151. loader->didConvertHandleToDownload();
  152. }
  153. void NetworkConnectionToWebProcess::cookiesForDOM(bool privateBrowsingEnabled, const KURL& firstParty, const KURL& url, String& result)
  154. {
  155. result = WebCore::cookiesForDOM(storageSession(privateBrowsingEnabled), firstParty, url);
  156. }
  157. void NetworkConnectionToWebProcess::setCookiesFromDOM(bool privateBrowsingEnabled, const KURL& firstParty, const KURL& url, const String& cookieString)
  158. {
  159. WebCore::setCookiesFromDOM(storageSession(privateBrowsingEnabled), firstParty, url, cookieString);
  160. }
  161. void NetworkConnectionToWebProcess::cookiesEnabled(bool privateBrowsingEnabled, const KURL& firstParty, const KURL& url, bool& result)
  162. {
  163. result = WebCore::cookiesEnabled(storageSession(privateBrowsingEnabled), firstParty, url);
  164. }
  165. void NetworkConnectionToWebProcess::cookieRequestHeaderFieldValue(bool privateBrowsingEnabled, const KURL& firstParty, const KURL& url, String& result)
  166. {
  167. result = WebCore::cookieRequestHeaderFieldValue(storageSession(privateBrowsingEnabled), firstParty, url);
  168. }
  169. void NetworkConnectionToWebProcess::getRawCookies(bool privateBrowsingEnabled, const KURL& firstParty, const KURL& url, Vector<Cookie>& result)
  170. {
  171. WebCore::getRawCookies(storageSession(privateBrowsingEnabled), firstParty, url, result);
  172. }
  173. void NetworkConnectionToWebProcess::deleteCookie(bool privateBrowsingEnabled, const KURL& url, const String& cookieName)
  174. {
  175. WebCore::deleteCookie(storageSession(privateBrowsingEnabled), url, cookieName);
  176. }
  177. void NetworkConnectionToWebProcess::registerBlobURL(const KURL& url, const BlobRegistrationData& data)
  178. {
  179. Vector<RefPtr<SandboxExtension>> extensions;
  180. for (size_t i = 0, count = data.sandboxExtensions().size(); i < count; ++i) {
  181. if (RefPtr<SandboxExtension> extension = SandboxExtension::create(data.sandboxExtensions()[i]))
  182. extensions.append(extension);
  183. }
  184. NetworkBlobRegistry::shared().registerBlobURL(this, url, data.releaseData(), extensions);
  185. }
  186. void NetworkConnectionToWebProcess::registerBlobURLFromURL(const KURL& url, const KURL& srcURL)
  187. {
  188. NetworkBlobRegistry::shared().registerBlobURL(this, url, srcURL);
  189. }
  190. void NetworkConnectionToWebProcess::unregisterBlobURL(const KURL& url)
  191. {
  192. NetworkBlobRegistry::shared().unregisterBlobURL(this, url);
  193. }
  194. } // namespace WebKit
  195. #endif // ENABLE(NETWORK_PROCESS)