NetworkConnectionToWebProcess.cpp [plain text]
#include "config.h"
#include "NetworkConnectionToWebProcess.h"
#include "BlobDataFileReferenceWithSandboxExtension.h"
#include "NetworkBlobRegistry.h"
#include "NetworkConnectionToWebProcessMessages.h"
#include "NetworkLoad.h"
#include "NetworkProcess.h"
#include "NetworkProcessConnectionMessages.h"
#include "NetworkResourceLoadParameters.h"
#include "NetworkResourceLoader.h"
#include "NetworkResourceLoaderMessages.h"
#include "RemoteNetworkingContext.h"
#include "SessionTracker.h"
#include "WebCoreArgumentCoders.h"
#include <WebCore/NotImplemented.h>
#include <WebCore/PingHandle.h>
#include <WebCore/PlatformCookieJar.h>
#include <WebCore/ResourceLoaderOptions.h>
#include <WebCore/ResourceRequest.h>
#include <WebCore/SessionID.h>
#include <wtf/RunLoop.h>
#if USE(NETWORK_SESSION)
#include "PingLoad.h"
#endif
using namespace WebCore;
namespace WebKit {
Ref<NetworkConnectionToWebProcess> NetworkConnectionToWebProcess::create(IPC::Connection::Identifier connectionIdentifier)
{
return adoptRef(*new NetworkConnectionToWebProcess(connectionIdentifier));
}
NetworkConnectionToWebProcess::NetworkConnectionToWebProcess(IPC::Connection::Identifier connectionIdentifier)
: m_connection(IPC::Connection::createServerConnection(connectionIdentifier, *this))
{
m_connection->open();
}
NetworkConnectionToWebProcess::~NetworkConnectionToWebProcess()
{
}
void NetworkConnectionToWebProcess::didCleanupResourceLoader(NetworkResourceLoader& loader)
{
ASSERT(m_networkResourceLoaders.get(loader.identifier()) == &loader);
m_networkResourceLoaders.remove(loader.identifier());
}
void NetworkConnectionToWebProcess::didReceiveMessage(IPC::Connection& connection, IPC::MessageDecoder& decoder)
{
if (decoder.messageReceiverName() == Messages::NetworkConnectionToWebProcess::messageReceiverName()) {
didReceiveNetworkConnectionToWebProcessMessage(connection, decoder);
return;
}
if (decoder.messageReceiverName() == Messages::NetworkResourceLoader::messageReceiverName()) {
HashMap<ResourceLoadIdentifier, RefPtr<NetworkResourceLoader>>::iterator loaderIterator = m_networkResourceLoaders.find(decoder.destinationID());
if (loaderIterator != m_networkResourceLoaders.end())
loaderIterator->value->didReceiveNetworkResourceLoaderMessage(connection, decoder);
return;
}
ASSERT_NOT_REACHED();
}
void NetworkConnectionToWebProcess::didReceiveSyncMessage(IPC::Connection& connection, IPC::MessageDecoder& decoder, std::unique_ptr<IPC::MessageEncoder>& reply)
{
if (decoder.messageReceiverName() == Messages::NetworkConnectionToWebProcess::messageReceiverName()) {
didReceiveSyncNetworkConnectionToWebProcessMessage(connection, decoder, reply);
return;
}
ASSERT_NOT_REACHED();
}
void NetworkConnectionToWebProcess::didClose(IPC::Connection&)
{
Ref<NetworkConnectionToWebProcess> protector(*this);
Vector<RefPtr<NetworkResourceLoader>> loaders;
copyValuesToVector(m_networkResourceLoaders, loaders);
for (auto& loader : loaders)
loader->abort();
ASSERT(m_networkResourceLoaders.isEmpty());
NetworkBlobRegistry::singleton().connectionToWebProcessDidClose(this);
NetworkProcess::singleton().removeNetworkConnectionToWebProcess(this);
}
void NetworkConnectionToWebProcess::didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference, IPC::StringReference)
{
}
void NetworkConnectionToWebProcess::scheduleResourceLoad(const NetworkResourceLoadParameters& loadParameters)
{
auto loader = NetworkResourceLoader::create(loadParameters, *this);
m_networkResourceLoaders.add(loadParameters.identifier, loader.ptr());
loader->start();
}
void NetworkConnectionToWebProcess::performSynchronousLoad(const NetworkResourceLoadParameters& loadParameters, RefPtr<Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply>&& reply)
{
auto loader = NetworkResourceLoader::create(loadParameters, *this, WTFMove(reply));
m_networkResourceLoaders.add(loadParameters.identifier, loader.ptr());
loader->start();
}
void NetworkConnectionToWebProcess::loadPing(const NetworkResourceLoadParameters& loadParameters)
{
#if USE(NETWORK_SESSION)
new PingLoad(loadParameters);
#else
RefPtr<NetworkingContext> context = RemoteNetworkingContext::create(loadParameters.sessionID, loadParameters.shouldClearReferrerOnHTTPSToHTTPRedirect);
new PingHandle(context.get(), loadParameters.request, loadParameters.allowStoredCredentials == AllowStoredCredentials, PingHandle::UsesAsyncCallbacks::Yes);
#endif
}
void NetworkConnectionToWebProcess::removeLoadIdentifier(ResourceLoadIdentifier identifier)
{
RefPtr<NetworkResourceLoader> loader = m_networkResourceLoaders.get(identifier);
if (!loader)
return;
loader->abort();
ASSERT(!m_networkResourceLoaders.contains(identifier));
}
void NetworkConnectionToWebProcess::setDefersLoading(ResourceLoadIdentifier identifier, bool defers)
{
RefPtr<NetworkResourceLoader> loader = m_networkResourceLoaders.get(identifier);
if (!loader)
return;
loader->setDefersLoading(defers);
}
void NetworkConnectionToWebProcess::prefetchDNS(const String& hostname)
{
NetworkProcess::singleton().prefetchDNS(hostname);
}
static NetworkStorageSession& storageSession(SessionID sessionID)
{
if (sessionID.isEphemeral()) {
NetworkStorageSession* privateStorageSession = SessionTracker::storageSession(sessionID);
if (privateStorageSession)
return *privateStorageSession;
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.");
}
return NetworkStorageSession::defaultStorageSession();
}
void NetworkConnectionToWebProcess::startDownload(SessionID sessionID, DownloadID downloadID, const ResourceRequest& request, const String& suggestedName)
{
NetworkProcess::singleton().downloadManager().startDownload(sessionID, downloadID, request, suggestedName);
}
void NetworkConnectionToWebProcess::convertMainResourceLoadToDownload(SessionID sessionID, uint64_t mainResourceLoadIdentifier, DownloadID downloadID, const ResourceRequest& request, const ResourceResponse& response)
{
auto& networkProcess = NetworkProcess::singleton();
if (!mainResourceLoadIdentifier) {
networkProcess.downloadManager().startDownload(sessionID, downloadID, request);
return;
}
NetworkResourceLoader* loader = m_networkResourceLoaders.get(mainResourceLoadIdentifier);
if (!loader) {
return;
}
#if USE(NETWORK_SESSION)
loader->networkLoad()->convertTaskToDownload(downloadID, request);
#else
networkProcess.downloadManager().convertHandleToDownload(downloadID, loader->networkLoad()->handle(), request, response);
loader->networkLoad()->handle()->continueDidReceiveResponse();
#endif
loader->didConvertToDownload();
}
void NetworkConnectionToWebProcess::cookiesForDOM(SessionID sessionID, const URL& firstParty, const URL& url, String& result)
{
result = WebCore::cookiesForDOM(storageSession(sessionID), firstParty, url);
}
void NetworkConnectionToWebProcess::setCookiesFromDOM(SessionID sessionID, const URL& firstParty, const URL& url, const String& cookieString)
{
WebCore::setCookiesFromDOM(storageSession(sessionID), firstParty, url, cookieString);
}
void NetworkConnectionToWebProcess::cookiesEnabled(SessionID sessionID, const URL& firstParty, const URL& url, bool& result)
{
result = WebCore::cookiesEnabled(storageSession(sessionID), firstParty, url);
}
void NetworkConnectionToWebProcess::cookieRequestHeaderFieldValue(SessionID sessionID, const URL& firstParty, const URL& url, String& result)
{
result = WebCore::cookieRequestHeaderFieldValue(storageSession(sessionID), firstParty, url);
}
void NetworkConnectionToWebProcess::getRawCookies(SessionID sessionID, const URL& firstParty, const URL& url, Vector<Cookie>& result)
{
WebCore::getRawCookies(storageSession(sessionID), firstParty, url, result);
}
void NetworkConnectionToWebProcess::deleteCookie(SessionID sessionID, const URL& url, const String& cookieName)
{
WebCore::deleteCookie(storageSession(sessionID), url, cookieName);
}
void NetworkConnectionToWebProcess::addCookie(SessionID sessionID, const URL& url, const Cookie& cookie)
{
WebCore::addCookie(storageSession(sessionID), url, cookie);
}
void NetworkConnectionToWebProcess::registerFileBlobURL(const URL& url, const String& path, const SandboxExtension::Handle& extensionHandle, const String& contentType)
{
RefPtr<SandboxExtension> extension = SandboxExtension::create(extensionHandle);
NetworkBlobRegistry::singleton().registerFileBlobURL(this, url, path, WTFMove(extension), contentType);
}
void NetworkConnectionToWebProcess::registerBlobURL(const URL& url, Vector<BlobPart> blobParts, const String& contentType)
{
NetworkBlobRegistry::singleton().registerBlobURL(this, url, WTFMove(blobParts), contentType);
}
void NetworkConnectionToWebProcess::registerBlobURLFromURL(const URL& url, const URL& srcURL)
{
NetworkBlobRegistry::singleton().registerBlobURL(this, url, srcURL);
}
void NetworkConnectionToWebProcess::preregisterSandboxExtensionsForOptionallyFileBackedBlob(const Vector<String>& filePaths, const SandboxExtension::HandleArray& handles)
{
ASSERT(filePaths.size() == handles.size());
for (size_t i = 0; i < filePaths.size(); ++i)
m_blobDataFileReferences.add(filePaths[i], BlobDataFileReferenceWithSandboxExtension::create(filePaths[i], SandboxExtension::create(handles[i])));
}
RefPtr<WebCore::BlobDataFileReference> NetworkConnectionToWebProcess::getBlobDataFileReferenceForPath(const String& path)
{
ASSERT(m_blobDataFileReferences.contains(path));
return m_blobDataFileReferences.get(path);
}
void NetworkConnectionToWebProcess::registerBlobURLOptionallyFileBacked(const URL& url, const URL& srcURL, const String& fileBackedPath, const String& contentType)
{
NetworkBlobRegistry::singleton().registerBlobURLOptionallyFileBacked(this, url, srcURL, fileBackedPath, contentType);
}
void NetworkConnectionToWebProcess::registerBlobURLForSlice(const URL& url, const URL& srcURL, int64_t start, int64_t end)
{
NetworkBlobRegistry::singleton().registerBlobURLForSlice(this, url, srcURL, start, end);
}
void NetworkConnectionToWebProcess::unregisterBlobURL(const URL& url)
{
NetworkBlobRegistry::singleton().unregisterBlobURL(this, url);
}
void NetworkConnectionToWebProcess::blobSize(const URL& url, uint64_t& resultSize)
{
resultSize = NetworkBlobRegistry::singleton().blobSize(this, url);
}
void NetworkConnectionToWebProcess::writeBlobsToTemporaryFiles(const Vector<String>& blobURLs, uint64_t requestIdentifier)
{
Vector<RefPtr<BlobDataFileReference>> fileReferences;
for (auto& url : blobURLs)
fileReferences.appendVector(NetworkBlobRegistry::singleton().filesInBlob(*this, { ParsedURLString, url }));
for (auto& file : fileReferences)
file->prepareForFileAccess();
NetworkBlobRegistry::singleton().writeBlobsToTemporaryFiles(blobURLs, [this, protectedThis = makeRef(*this), requestIdentifier, fileReferences = WTFMove(fileReferences)](auto& fileNames) mutable {
for (auto& file : fileReferences)
file->revokeFileAccess();
NetworkProcess::singleton().grantSandboxExtensionsToDatabaseProcessForBlobs(fileNames, [this, protectedThis = WTFMove(protectedThis), requestIdentifier, fileNames]() {
if (!m_connection->isValid())
return;
m_connection->send(Messages::NetworkProcessConnection::DidWriteBlobsToTemporaryFiles(requestIdentifier, fileNames), 0);
});
});
}
void NetworkConnectionToWebProcess::ensureLegacyPrivateBrowsingSession()
{
NetworkProcess::singleton().ensurePrivateBrowsingSession(SessionID::legacyPrivateSessionID());
}
}