2 * Copyright (C) 2013-2016 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "NetworkBlobRegistry.h"
29 #include "BlobDataFileReferenceWithSandboxExtension.h"
30 #include "NetworkConnectionToWebProcess.h"
31 #include "SandboxExtension.h"
32 #include <WebCore/BlobPart.h>
33 #include <WebCore/BlobRegistryImpl.h>
34 #include <wtf/NeverDestroyed.h>
35 #include <wtf/RunLoop.h>
37 using namespace WebCore;
41 NetworkBlobRegistry& NetworkBlobRegistry::singleton()
43 ASSERT(RunLoop::isMain());
44 static NeverDestroyed<NetworkBlobRegistry> registry;
48 NetworkBlobRegistry::NetworkBlobRegistry()
52 void NetworkBlobRegistry::registerFileBlobURL(NetworkConnectionToWebProcess* connection, const URL& url, const String& path, RefPtr<SandboxExtension>&& sandboxExtension, const String& contentType)
54 blobRegistry().registerFileBlobURL(url, BlobDataFileReferenceWithSandboxExtension::create(path, WTFMove(sandboxExtension)), contentType);
56 ASSERT(!m_blobsForConnection.get(connection).contains(url));
57 BlobForConnectionMap::iterator mapIterator = m_blobsForConnection.find(connection);
58 if (mapIterator == m_blobsForConnection.end())
59 mapIterator = m_blobsForConnection.add(connection, HashSet<URL>()).iterator;
60 mapIterator->value.add(url);
63 void NetworkBlobRegistry::registerBlobURL(NetworkConnectionToWebProcess* connection, const URL& url, Vector<WebCore::BlobPart>&& blobParts, const String& contentType)
65 blobRegistry().registerBlobURL(url, WTFMove(blobParts), contentType);
67 ASSERT(!m_blobsForConnection.get(connection).contains(url));
68 BlobForConnectionMap::iterator mapIterator = m_blobsForConnection.find(connection);
69 if (mapIterator == m_blobsForConnection.end())
70 mapIterator = m_blobsForConnection.add(connection, HashSet<URL>()).iterator;
71 mapIterator->value.add(url);
74 void NetworkBlobRegistry::registerBlobURL(NetworkConnectionToWebProcess* connection, const WebCore::URL& url, const WebCore::URL& srcURL, bool shouldBypassConnectionCheck)
76 // The connection may not be registered if NetworkProcess prevously crashed for any reason.
77 BlobForConnectionMap::iterator mapIterator = m_blobsForConnection.find(connection);
78 if (mapIterator == m_blobsForConnection.end()) {
79 if (!shouldBypassConnectionCheck)
81 mapIterator = m_blobsForConnection.add(connection, HashSet<URL>()).iterator;
84 blobRegistry().registerBlobURL(url, srcURL);
86 ASSERT(shouldBypassConnectionCheck || mapIterator->value.contains(srcURL));
87 mapIterator->value.add(url);
90 void NetworkBlobRegistry::registerBlobURLOptionallyFileBacked(NetworkConnectionToWebProcess* connection, const URL& url, const URL& srcURL, const String& fileBackedPath, const String& contentType)
92 auto fileReference = connection->getBlobDataFileReferenceForPath(fileBackedPath);
93 ASSERT(fileReference);
95 blobRegistry().registerBlobURLOptionallyFileBacked(url, srcURL, WTFMove(fileReference), contentType);
97 ASSERT(!m_blobsForConnection.get(connection).contains(url));
98 BlobForConnectionMap::iterator mapIterator = m_blobsForConnection.find(connection);
99 if (mapIterator == m_blobsForConnection.end())
100 mapIterator = m_blobsForConnection.add(connection, HashSet<URL>()).iterator;
101 mapIterator->value.add(url);
104 void NetworkBlobRegistry::registerBlobURLForSlice(NetworkConnectionToWebProcess* connection, const WebCore::URL& url, const WebCore::URL& srcURL, int64_t start, int64_t end)
106 // The connection may not be registered if NetworkProcess prevously crashed for any reason.
107 BlobForConnectionMap::iterator mapIterator = m_blobsForConnection.find(connection);
108 if (mapIterator == m_blobsForConnection.end())
111 blobRegistry().registerBlobURLForSlice(url, srcURL, start, end);
113 ASSERT(mapIterator->value.contains(srcURL));
114 mapIterator->value.add(url);
117 void NetworkBlobRegistry::unregisterBlobURL(NetworkConnectionToWebProcess* connection, const WebCore::URL& url)
119 // The connection may not be registered if NetworkProcess prevously crashed for any reason.
120 BlobForConnectionMap::iterator mapIterator = m_blobsForConnection.find(connection);
121 if (mapIterator == m_blobsForConnection.end())
124 blobRegistry().unregisterBlobURL(url);
126 ASSERT(mapIterator->value.contains(url));
127 mapIterator->value.remove(url);
130 uint64_t NetworkBlobRegistry::blobSize(NetworkConnectionToWebProcess* connection, const WebCore::URL& url)
132 if (!m_blobsForConnection.contains(connection) || !m_blobsForConnection.find(connection)->value.contains(url))
135 return blobRegistry().blobSize(url);
138 void NetworkBlobRegistry::writeBlobsToTemporaryFiles(const Vector<String>& blobURLs, Function<void(const Vector<String>&)>&& completionHandler)
140 blobRegistry().writeBlobsToTemporaryFiles(blobURLs, WTFMove(completionHandler));
143 void NetworkBlobRegistry::writeBlobToFilePath(const URL& blobURL, const String& path, Function<void(bool success)>&& completionHandler)
145 if (!blobRegistry().isBlobRegistryImpl()) {
146 completionHandler(false);
147 ASSERT_NOT_REACHED();
151 auto blobFiles = filesInBlob({ ParsedURLString, blobURL });
152 for (auto& file : blobFiles)
153 file->prepareForFileAccess();
155 static_cast<BlobRegistryImpl&>(blobRegistry()).writeBlobToFilePath(blobURL, path, [blobFiles = WTFMove(blobFiles), completionHandler = WTFMove(completionHandler)] (bool success) {
156 for (auto& file : blobFiles)
157 file->revokeFileAccess();
158 completionHandler(success);
162 void NetworkBlobRegistry::connectionToWebProcessDidClose(NetworkConnectionToWebProcess* connection)
164 if (!m_blobsForConnection.contains(connection))
167 HashSet<URL>& blobsForConnection = m_blobsForConnection.find(connection)->value;
168 for (HashSet<URL>::iterator iter = blobsForConnection.begin(), end = blobsForConnection.end(); iter != end; ++iter)
169 blobRegistry().unregisterBlobURL(*iter);
171 m_blobsForConnection.remove(connection);
174 Vector<RefPtr<BlobDataFileReference>> NetworkBlobRegistry::filesInBlob(NetworkConnectionToWebProcess& connection, const WebCore::URL& url)
176 if (!m_blobsForConnection.contains(&connection) || !m_blobsForConnection.find(&connection)->value.contains(url))
179 return filesInBlob(url);
182 Vector<RefPtr<BlobDataFileReference>> NetworkBlobRegistry::filesInBlob(const URL& url)
184 ASSERT(blobRegistry().isBlobRegistryImpl());
185 BlobData* blobData = static_cast<BlobRegistryImpl&>(blobRegistry()).getBlobDataFromURL(url);
189 Vector<RefPtr<BlobDataFileReference>> result;
190 for (const BlobDataItem& item : blobData->items()) {
191 if (item.type() == BlobDataItem::Type::File)
192 result.append(item.file());