WebResourceLoadScheduler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "WebResourceLoadScheduler.h"
  27. #include "Logging.h"
  28. #include "NetworkConnectionToWebProcessMessages.h"
  29. #include "NetworkProcessConnection.h"
  30. #include "NetworkResourceLoadParameters.h"
  31. #include "WebCoreArgumentCoders.h"
  32. #include "WebErrors.h"
  33. #include "WebFrame.h"
  34. #include "WebFrameLoaderClient.h"
  35. #include "WebPage.h"
  36. #include "WebProcess.h"
  37. #include "WebResourceLoader.h"
  38. #include <WebCore/CachedResource.h>
  39. #include <WebCore/Document.h>
  40. #include <WebCore/DocumentLoader.h>
  41. #include <WebCore/Frame.h>
  42. #include <WebCore/FrameLoader.h>
  43. #include <WebCore/NetscapePlugInStreamLoader.h>
  44. #include <WebCore/ReferrerPolicy.h>
  45. #include <WebCore/ResourceBuffer.h>
  46. #include <WebCore/ResourceLoader.h>
  47. #include <WebCore/Settings.h>
  48. #include <WebCore/SubresourceLoader.h>
  49. #include <wtf/text/CString.h>
  50. #if ENABLE(NETWORK_PROCESS)
  51. using namespace WebCore;
  52. namespace WebKit {
  53. WebResourceLoadScheduler::WebResourceLoadScheduler()
  54. : m_internallyFailedLoadTimer(RunLoop::main(), this, &WebResourceLoadScheduler::internallyFailedLoadTimerFired)
  55. , m_suspendPendingRequestsCount(0)
  56. {
  57. }
  58. WebResourceLoadScheduler::~WebResourceLoadScheduler()
  59. {
  60. }
  61. PassRefPtr<SubresourceLoader> WebResourceLoadScheduler::scheduleSubresourceLoad(Frame* frame, CachedResource* resource, const ResourceRequest& request, ResourceLoadPriority priority, const ResourceLoaderOptions& options)
  62. {
  63. RefPtr<SubresourceLoader> loader = SubresourceLoader::create(frame, resource, request, options);
  64. if (loader)
  65. scheduleLoad(loader.get(), resource, priority, frame->document()->referrerPolicy() == ReferrerPolicyDefault);
  66. return loader.release();
  67. }
  68. PassRefPtr<NetscapePlugInStreamLoader> WebResourceLoadScheduler::schedulePluginStreamLoad(Frame* frame, NetscapePlugInStreamLoaderClient* client, const ResourceRequest& request)
  69. {
  70. RefPtr<NetscapePlugInStreamLoader> loader = NetscapePlugInStreamLoader::create(frame, client, request);
  71. if (loader)
  72. scheduleLoad(loader.get(), 0, ResourceLoadPriorityLow, frame->document()->referrerPolicy() == ReferrerPolicyDefault);
  73. return loader.release();
  74. }
  75. void WebResourceLoadScheduler::scheduleLoad(ResourceLoader* resourceLoader, CachedResource* resource, ResourceLoadPriority priority, bool shouldClearReferrerOnHTTPSToHTTPRedirect)
  76. {
  77. ASSERT(resourceLoader);
  78. ASSERT(priority != ResourceLoadPriorityUnresolved);
  79. priority = ResourceLoadPriorityHighest;
  80. ResourceLoadIdentifier identifier = resourceLoader->identifier();
  81. ASSERT(identifier);
  82. // If the DocumentLoader schedules this as an archive resource load,
  83. // then we should remember the ResourceLoader in our records but not schedule it in the NetworkProcess.
  84. if (resourceLoader->documentLoader()->scheduleArchiveLoad(resourceLoader, resourceLoader->request())) {
  85. LOG(NetworkScheduling, "(WebProcess) WebResourceLoadScheduler::scheduleLoad, url '%s' will be handled as an archive resource.", resourceLoader->url().string().utf8().data());
  86. m_webResourceLoaders.set(identifier, WebResourceLoader::create(resourceLoader));
  87. return;
  88. }
  89. LOG(NetworkScheduling, "(WebProcess) WebResourceLoadScheduler::scheduleLoad, url '%s' will be scheduled with the NetworkProcess with priority %i", resourceLoader->url().string().utf8().data(), priority);
  90. ContentSniffingPolicy contentSniffingPolicy = resourceLoader->shouldSniffContent() ? SniffContent : DoNotSniffContent;
  91. StoredCredentials allowStoredCredentials = resourceLoader->shouldUseCredentialStorage() ? AllowStoredCredentials : DoNotAllowStoredCredentials;
  92. bool privateBrowsingEnabled = resourceLoader->frameLoader()->frame()->settings()->privateBrowsingEnabled();
  93. // FIXME: Some entities in WebCore use WebCore's "EmptyFrameLoaderClient" instead of having a proper WebFrameLoaderClient.
  94. // EmptyFrameLoaderClient shouldn't exist and everything should be using a WebFrameLoaderClient,
  95. // but in the meantime we have to make sure not to mis-cast.
  96. WebFrameLoaderClient* webFrameLoaderClient = toWebFrameLoaderClient(resourceLoader->frameLoader()->client());
  97. WebFrame* webFrame = webFrameLoaderClient ? webFrameLoaderClient->webFrame() : 0;
  98. WebPage* webPage = webFrame ? webFrame->page() : 0;
  99. NetworkResourceLoadParameters loadParameters;
  100. loadParameters.identifier = identifier;
  101. loadParameters.webPageID = webPage ? webPage->pageID() : 0;
  102. loadParameters.webFrameID = webFrame ? webFrame->frameID() : 0;
  103. loadParameters.request = resourceLoader->request();
  104. loadParameters.priority = priority;
  105. loadParameters.contentSniffingPolicy = contentSniffingPolicy;
  106. loadParameters.allowStoredCredentials = allowStoredCredentials;
  107. // If there is no WebFrame then this resource cannot be authenticated with the client.
  108. loadParameters.clientCredentialPolicy = (webFrame && webPage) ? resourceLoader->clientCredentialPolicy() : DoNotAskClientForAnyCredentials;
  109. loadParameters.inPrivateBrowsingMode = privateBrowsingEnabled;
  110. loadParameters.shouldClearReferrerOnHTTPSToHTTPRedirect = shouldClearReferrerOnHTTPSToHTTPRedirect;
  111. loadParameters.isMainResource = resource && resource->type() == CachedResource::MainResource;
  112. ASSERT((loadParameters.webPageID && loadParameters.webFrameID) || loadParameters.clientCredentialPolicy == DoNotAskClientForAnyCredentials);
  113. if (!WebProcess::shared().networkConnection()->connection()->send(Messages::NetworkConnectionToWebProcess::ScheduleResourceLoad(loadParameters), 0)) {
  114. // We probably failed to schedule this load with the NetworkProcess because it had crashed.
  115. // This load will never succeed so we will schedule it to fail asynchronously.
  116. scheduleInternallyFailedLoad(resourceLoader);
  117. return;
  118. }
  119. m_webResourceLoaders.set(identifier, WebResourceLoader::create(resourceLoader));
  120. notifyDidScheduleResourceRequest(resourceLoader);
  121. }
  122. void WebResourceLoadScheduler::scheduleInternallyFailedLoad(WebCore::ResourceLoader* resourceLoader)
  123. {
  124. m_internallyFailedResourceLoaders.add(resourceLoader);
  125. m_internallyFailedLoadTimer.startOneShot(0);
  126. }
  127. void WebResourceLoadScheduler::internallyFailedLoadTimerFired()
  128. {
  129. Vector<RefPtr<ResourceLoader>> internallyFailedResourceLoaders;
  130. copyToVector(m_internallyFailedResourceLoaders, internallyFailedResourceLoaders);
  131. for (size_t i = 0; i < internallyFailedResourceLoaders.size(); ++i)
  132. internallyFailedResourceLoaders[i]->didFail(internalError(internallyFailedResourceLoaders[i]->url()));
  133. }
  134. void WebResourceLoadScheduler::remove(ResourceLoader* resourceLoader)
  135. {
  136. ASSERT(resourceLoader);
  137. LOG(NetworkScheduling, "(WebProcess) WebResourceLoadScheduler::remove, url '%s'", resourceLoader->url().string().utf8().data());
  138. if (m_internallyFailedResourceLoaders.contains(resourceLoader)) {
  139. m_internallyFailedResourceLoaders.remove(resourceLoader);
  140. return;
  141. }
  142. ResourceLoadIdentifier identifier = resourceLoader->identifier();
  143. if (!identifier) {
  144. LOG_ERROR("WebResourceLoadScheduler removing a ResourceLoader that has no identifier.");
  145. return;
  146. }
  147. RefPtr<WebResourceLoader> loader = m_webResourceLoaders.take(identifier);
  148. // Loader may not be registered if we created it, but haven't scheduled yet (a bundle client can decide to cancel such request via willSendRequest).
  149. if (!loader)
  150. return;
  151. WebProcess::shared().networkConnection()->connection()->send(Messages::NetworkConnectionToWebProcess::RemoveLoadIdentifier(identifier), 0);
  152. // It's possible that this WebResourceLoader might be just about to message back to the NetworkProcess (e.g. ContinueWillSendRequest)
  153. // but there's no point in doing so anymore.
  154. loader->detachFromCoreLoader();
  155. }
  156. void WebResourceLoadScheduler::crossOriginRedirectReceived(ResourceLoader*, const KURL&)
  157. {
  158. // We handle cross origin redirects entirely within the NetworkProcess.
  159. // We override this call in the WebProcess to make it a no-op.
  160. }
  161. void WebResourceLoadScheduler::servePendingRequests(ResourceLoadPriority minimumPriority)
  162. {
  163. LOG(NetworkScheduling, "(WebProcess) WebResourceLoadScheduler::servePendingRequests");
  164. // The NetworkProcess scheduler is good at making sure loads are serviced until there are no more pending requests.
  165. // If this WebProcess isn't expecting requests to be served then we can ignore messaging the NetworkProcess right now.
  166. if (m_suspendPendingRequestsCount)
  167. return;
  168. WebProcess::shared().networkConnection()->connection()->send(Messages::NetworkConnectionToWebProcess::ServePendingRequests(minimumPriority), 0);
  169. }
  170. void WebResourceLoadScheduler::suspendPendingRequests()
  171. {
  172. ++m_suspendPendingRequestsCount;
  173. }
  174. void WebResourceLoadScheduler::resumePendingRequests()
  175. {
  176. ASSERT(m_suspendPendingRequestsCount);
  177. --m_suspendPendingRequestsCount;
  178. }
  179. void WebResourceLoadScheduler::setSerialLoadingEnabled(bool enabled)
  180. {
  181. WebProcess::shared().networkConnection()->connection()->sendSync(Messages::NetworkConnectionToWebProcess::SetSerialLoadingEnabled(enabled), Messages::NetworkConnectionToWebProcess::SetSerialLoadingEnabled::Reply(), 0);
  182. }
  183. void WebResourceLoadScheduler::networkProcessCrashed()
  184. {
  185. HashMap<unsigned long, RefPtr<WebResourceLoader>>::iterator end = m_webResourceLoaders.end();
  186. for (HashMap<unsigned long, RefPtr<WebResourceLoader>>::iterator i = m_webResourceLoaders.begin(); i != end; ++i)
  187. scheduleInternallyFailedLoad(i->value.get()->resourceLoader());
  188. m_webResourceLoaders.clear();
  189. }
  190. } // namespace WebKit
  191. #endif // ENABLE(NETWORK_PROCESS)