1/*
2 * Copyright (C) 2010-2018 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
26#include "config.h"
27#include "WebPlatformStrategies.h"
28
29#include "BlobRegistryProxy.h"
30#include "BlockingResponseMap.h"
31#include "DataReference.h"
32#include "HangDetectionDisabler.h"
33#include "NetworkConnectionToWebProcessMessages.h"
34#include "NetworkProcessConnection.h"
35#include "NetworkResourceLoadParameters.h"
36#include "PluginInfoStore.h"
37#include "WebCoreArgumentCoders.h"
38#include "WebErrors.h"
39#include "WebFrame.h"
40#include "WebFrameLoaderClient.h"
41#include "WebLoaderStrategy.h"
42#include "WebPage.h"
43#include "WebPasteboardOverrides.h"
44#include "WebPasteboardProxyMessages.h"
45#include "WebProcess.h"
46#include "WebProcessProxyMessages.h"
47#include <WebCore/Color.h>
48#include <WebCore/Document.h>
49#include <WebCore/DocumentLoader.h>
50#include <WebCore/Frame.h>
51#include <WebCore/LoaderStrategy.h>
52#include <WebCore/NetworkStorageSession.h>
53#include <WebCore/Page.h>
54#include <WebCore/PageGroup.h>
55#include <WebCore/PasteboardItemInfo.h>
56#include <WebCore/PlatformPasteboard.h>
57#include <WebCore/ProgressTracker.h>
58#include <WebCore/ResourceError.h>
59#include <WebCore/SameSiteInfo.h>
60#include <WebCore/StorageNamespace.h>
61#include <WebCore/SubframeLoader.h>
62#include <pal/SessionID.h>
63#include <wtf/Atomics.h>
64#include <wtf/URL.h>
65
66#if PLATFORM(MAC)
67#include "StringUtilities.h"
68#endif
69
70#if PLATFORM(GTK)
71#include "WebSelectionData.h"
72#endif
73
74namespace WebKit {
75using namespace WebCore;
76
77void WebPlatformStrategies::initialize()
78{
79 static NeverDestroyed<WebPlatformStrategies> platformStrategies;
80 setPlatformStrategies(&platformStrategies.get());
81}
82
83WebPlatformStrategies::WebPlatformStrategies()
84{
85}
86
87LoaderStrategy* WebPlatformStrategies::createLoaderStrategy()
88{
89 return &WebProcess::singleton().webLoaderStrategy();
90}
91
92PasteboardStrategy* WebPlatformStrategies::createPasteboardStrategy()
93{
94 return this;
95}
96
97BlobRegistry* WebPlatformStrategies::createBlobRegistry()
98{
99 return new BlobRegistryProxy;
100}
101
102#if PLATFORM(COCOA)
103// PasteboardStrategy
104
105void WebPlatformStrategies::getTypes(Vector<String>& types, const String& pasteboardName)
106{
107 // First check the overrides.
108 // The purpose of the overrides is to avoid messaging back to the UI process.
109 // Therefore, if there are any overridden types, we return just those.
110 types = WebPasteboardOverrides::sharedPasteboardOverrides().overriddenTypes(pasteboardName);
111 if (!types.isEmpty())
112 return;
113
114 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::GetPasteboardTypes(pasteboardName), Messages::WebPasteboardProxy::GetPasteboardTypes::Reply(types), 0);
115}
116
117RefPtr<WebCore::SharedBuffer> WebPlatformStrategies::bufferForType(const String& pasteboardType, const String& pasteboardName)
118{
119 // First check the overrides.
120 Vector<char> overrideBuffer;
121 if (WebPasteboardOverrides::sharedPasteboardOverrides().getDataForOverride(pasteboardName, pasteboardType, overrideBuffer))
122 return SharedBuffer::create(WTFMove(overrideBuffer));
123
124 // Fallback to messaging the UI process for native pasteboard content.
125 SharedMemory::Handle handle;
126 uint64_t size { 0 };
127 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::GetPasteboardBufferForType(pasteboardName, pasteboardType), Messages::WebPasteboardProxy::GetPasteboardBufferForType::Reply(handle, size), 0);
128 if (handle.isNull())
129 return nullptr;
130 RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::map(handle, SharedMemory::Protection::ReadOnly);
131 return SharedBuffer::create(static_cast<unsigned char *>(sharedMemoryBuffer->data()), size);
132}
133
134void WebPlatformStrategies::getPathnamesForType(Vector<String>& pathnames, const String& pasteboardType, const String& pasteboardName)
135{
136 SandboxExtension::HandleArray sandboxExtensionsHandleArray;
137 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::GetPasteboardPathnamesForType(pasteboardName, pasteboardType),
138 Messages::WebPasteboardProxy::GetPasteboardPathnamesForType::Reply(pathnames, sandboxExtensionsHandleArray), 0);
139 ASSERT(pathnames.size() == sandboxExtensionsHandleArray.size());
140 for (size_t i = 0; i < sandboxExtensionsHandleArray.size(); i++) {
141 if (auto extension = SandboxExtension::create(WTFMove(sandboxExtensionsHandleArray[i])))
142 extension->consumePermanently();
143 }
144}
145
146String WebPlatformStrategies::stringForType(const String& pasteboardType, const String& pasteboardName)
147{
148 String value;
149 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::GetPasteboardStringForType(pasteboardName, pasteboardType), Messages::WebPasteboardProxy::GetPasteboardStringForType::Reply(value), 0);
150 return value;
151}
152
153Vector<String> WebPlatformStrategies::allStringsForType(const String& pasteboardType, const String& pasteboardName)
154{
155 Vector<String> values;
156 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::GetPasteboardStringsForType(pasteboardName, pasteboardType), Messages::WebPasteboardProxy::GetPasteboardStringsForType::Reply(values), 0);
157 return values;
158}
159
160long WebPlatformStrategies::changeCount(const WTF::String &pasteboardName)
161{
162 uint64_t changeCount { 0 };
163 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::GetPasteboardChangeCount(pasteboardName), Messages::WebPasteboardProxy::GetPasteboardChangeCount::Reply(changeCount), 0);
164 return changeCount;
165}
166
167String WebPlatformStrategies::uniqueName()
168{
169 String pasteboardName;
170 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::GetPasteboardUniqueName(), Messages::WebPasteboardProxy::GetPasteboardUniqueName::Reply(pasteboardName), 0);
171 return pasteboardName;
172}
173
174Color WebPlatformStrategies::color(const String& pasteboardName)
175{
176 Color color;
177 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::GetPasteboardColor(pasteboardName), Messages::WebPasteboardProxy::GetPasteboardColor::Reply(color), 0);
178 return color;
179}
180
181URL WebPlatformStrategies::url(const String& pasteboardName)
182{
183 String urlString;
184 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::GetPasteboardURL(pasteboardName), Messages::WebPasteboardProxy::GetPasteboardURL::Reply(urlString), 0);
185 return URL({ }, urlString);
186}
187
188long WebPlatformStrategies::addTypes(const Vector<String>& pasteboardTypes, const String& pasteboardName)
189{
190 uint64_t newChangeCount { 0 };
191 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::AddPasteboardTypes(pasteboardName, pasteboardTypes), Messages::WebPasteboardProxy::AddPasteboardTypes::Reply(newChangeCount), 0);
192 return newChangeCount;
193}
194
195long WebPlatformStrategies::setTypes(const Vector<String>& pasteboardTypes, const String& pasteboardName)
196{
197 uint64_t newChangeCount { 0 };
198 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::SetPasteboardTypes(pasteboardName, pasteboardTypes), Messages::WebPasteboardProxy::SetPasteboardTypes::Reply(newChangeCount), 0);
199 return newChangeCount;
200}
201
202long WebPlatformStrategies::setBufferForType(SharedBuffer* buffer, const String& pasteboardType, const String& pasteboardName)
203{
204 SharedMemory::Handle handle;
205 if (buffer && buffer->size()) {
206 RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::allocate(buffer->size());
207 // FIXME: Null check prevents crashing, but it is not great that we will have empty pasteboard content for this type,
208 // because we've already set the types.
209 if (sharedMemoryBuffer) {
210 memcpy(sharedMemoryBuffer->data(), buffer->data(), buffer->size());
211 sharedMemoryBuffer->createHandle(handle, SharedMemory::Protection::ReadOnly);
212 }
213 }
214 uint64_t newChangeCount { 0 };
215 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::SetPasteboardBufferForType(pasteboardName, pasteboardType, handle, buffer ? buffer->size() : 0), Messages::WebPasteboardProxy::SetPasteboardBufferForType::Reply(newChangeCount), 0);
216 return newChangeCount;
217}
218
219long WebPlatformStrategies::setURL(const PasteboardURL& pasteboardURL, const String& pasteboardName)
220{
221 uint64_t newChangeCount;
222 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::SetPasteboardURL(pasteboardURL, pasteboardName), Messages::WebPasteboardProxy::SetPasteboardURL::Reply(newChangeCount), 0);
223 return newChangeCount;
224}
225
226long WebPlatformStrategies::setColor(const Color& color, const String& pasteboardName)
227{
228 uint64_t newChangeCount { 0 };
229 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::SetPasteboardColor(pasteboardName, color), Messages::WebPasteboardProxy::SetPasteboardColor::Reply(newChangeCount), 0);
230 return newChangeCount;
231}
232
233long WebPlatformStrategies::setStringForType(const String& string, const String& pasteboardType, const String& pasteboardName)
234{
235 uint64_t newChangeCount { 0 };
236 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::SetPasteboardStringForType(pasteboardName, pasteboardType, string), Messages::WebPasteboardProxy::SetPasteboardStringForType::Reply(newChangeCount), 0);
237 return newChangeCount;
238}
239
240int WebPlatformStrategies::getNumberOfFiles(const String& pasteboardName)
241{
242 uint64_t numberOfFiles { 0 };
243 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::GetNumberOfFiles(pasteboardName), Messages::WebPasteboardProxy::GetNumberOfFiles::Reply(numberOfFiles), 0);
244 return numberOfFiles;
245}
246
247#if PLATFORM(IOS_FAMILY)
248
249void WebPlatformStrategies::writeToPasteboard(const PasteboardURL& url, const String& pasteboardName)
250{
251 WebProcess::singleton().parentProcessConnection()->send(Messages::WebPasteboardProxy::WriteURLToPasteboard(url, pasteboardName), 0);
252}
253
254void WebPlatformStrategies::writeToPasteboard(const WebCore::PasteboardWebContent& content, const String& pasteboardName)
255{
256 WebProcess::singleton().parentProcessConnection()->send(Messages::WebPasteboardProxy::WriteWebContentToPasteboard(content, pasteboardName), 0);
257}
258
259void WebPlatformStrategies::writeToPasteboard(const WebCore::PasteboardImage& image, const String& pasteboardName)
260{
261 WebProcess::singleton().parentProcessConnection()->send(Messages::WebPasteboardProxy::WriteImageToPasteboard(image, pasteboardName), 0);
262}
263
264void WebPlatformStrategies::writeToPasteboard(const String& pasteboardType, const String& text, const String& pasteboardName)
265{
266 WebProcess::singleton().parentProcessConnection()->send(Messages::WebPasteboardProxy::WriteStringToPasteboard(pasteboardType, text, pasteboardName), 0);
267}
268
269int WebPlatformStrategies::getPasteboardItemsCount(const String& pasteboardName)
270{
271 uint64_t itemsCount { 0 };
272 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::GetPasteboardItemsCount(pasteboardName), Messages::WebPasteboardProxy::GetPasteboardItemsCount::Reply(itemsCount), 0);
273 return itemsCount;
274}
275
276Vector<PasteboardItemInfo> WebPlatformStrategies::allPasteboardItemInfo(const String& pasteboardName)
277{
278 Vector<PasteboardItemInfo> allInfo;
279 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::AllPasteboardItemInfo(pasteboardName), Messages::WebPasteboardProxy::AllPasteboardItemInfo::Reply(allInfo), 0);
280 return allInfo;
281}
282
283PasteboardItemInfo WebPlatformStrategies::informationForItemAtIndex(int index, const String& pasteboardName)
284{
285 PasteboardItemInfo info;
286 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::InformationForItemAtIndex(index, pasteboardName), Messages::WebPasteboardProxy::InformationForItemAtIndex::Reply(info), 0);
287 return info;
288}
289
290void WebPlatformStrategies::updateSupportedTypeIdentifiers(const Vector<String>& identifiers, const String& pasteboardName)
291{
292 WebProcess::singleton().parentProcessConnection()->send(Messages::WebPasteboardProxy::UpdateSupportedTypeIdentifiers(identifiers, pasteboardName), 0);
293}
294
295RefPtr<WebCore::SharedBuffer> WebPlatformStrategies::readBufferFromPasteboard(int index, const String& pasteboardType, const String& pasteboardName)
296{
297 SharedMemory::Handle handle;
298 uint64_t size { 0 };
299 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::ReadBufferFromPasteboard(index, pasteboardType, pasteboardName), Messages::WebPasteboardProxy::ReadBufferFromPasteboard::Reply(handle, size), 0);
300 if (handle.isNull())
301 return nullptr;
302 RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::map(handle, SharedMemory::Protection::ReadOnly);
303 return SharedBuffer::create(static_cast<unsigned char *>(sharedMemoryBuffer->data()), size);
304}
305
306URL WebPlatformStrategies::readURLFromPasteboard(int index, const String& pasteboardName, String& title)
307{
308 String urlString;
309 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::ReadURLFromPasteboard(index, pasteboardName), Messages::WebPasteboardProxy::ReadURLFromPasteboard::Reply(urlString, title), 0);
310 return URL({ }, urlString);
311}
312
313String WebPlatformStrategies::readStringFromPasteboard(int index, const String& pasteboardType, const String& pasteboardName)
314{
315 String value;
316 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::ReadStringFromPasteboard(index, pasteboardType, pasteboardName), Messages::WebPasteboardProxy::ReadStringFromPasteboard::Reply(value), 0);
317 return value;
318}
319#endif // PLATFORM(IOS_FAMILY)
320
321#endif // PLATFORM(COCOA)
322
323#if PLATFORM(GTK)
324// PasteboardStrategy
325
326void WebPlatformStrategies::writeToClipboard(const String& pasteboardName, const SelectionData& selection)
327{
328 WebSelectionData selectionData(selection);
329 WebProcess::singleton().parentProcessConnection()->send(Messages::WebPasteboardProxy::WriteToClipboard(pasteboardName, selectionData), 0);
330}
331
332Ref<SelectionData> WebPlatformStrategies::readFromClipboard(const String& pasteboardName)
333{
334 WebSelectionData selection;
335 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::ReadFromClipboard(pasteboardName), Messages::WebPasteboardProxy::ReadFromClipboard::Reply(selection), 0);
336 return WTFMove(selection.selectionData);
337}
338
339#endif // PLATFORM(GTK)
340
341#if USE(LIBWPE)
342// PasteboardStrategy
343
344void WebPlatformStrategies::getTypes(Vector<String>& types)
345{
346 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::GetPasteboardTypes(), Messages::WebPasteboardProxy::GetPasteboardTypes::Reply(types), 0);
347}
348
349String WebPlatformStrategies::readStringFromPasteboard(int index, const String& pasteboardType)
350{
351 String value;
352 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::ReadStringFromPasteboard(index, pasteboardType), Messages::WebPasteboardProxy::ReadStringFromPasteboard::Reply(value), 0);
353 return value;
354}
355
356void WebPlatformStrategies::writeToPasteboard(const WebCore::PasteboardWebContent& content)
357{
358 WebProcess::singleton().parentProcessConnection()->send(Messages::WebPasteboardProxy::WriteWebContentToPasteboard(content), 0);
359}
360
361void WebPlatformStrategies::writeToPasteboard(const String& pasteboardType, const String& text)
362{
363 WebProcess::singleton().parentProcessConnection()->send(Messages::WebPasteboardProxy::WriteStringToPasteboard(pasteboardType, text), 0);
364}
365
366#endif // USE(LIBWPE)
367
368Vector<String> WebPlatformStrategies::typesSafeForDOMToReadAndWrite(const String& pasteboardName, const String& origin)
369{
370 Vector<String> types;
371 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::TypesSafeForDOMToReadAndWrite(pasteboardName, origin), Messages::WebPasteboardProxy::TypesSafeForDOMToReadAndWrite::Reply(types), 0);
372 return types;
373}
374
375long WebPlatformStrategies::writeCustomData(const WebCore::PasteboardCustomData& data, const String& pasteboardName)
376{
377 uint64_t newChangeCount { 0 };
378 WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPasteboardProxy::WriteCustomData(data, pasteboardName), Messages::WebPasteboardProxy::WriteCustomData::Reply(newChangeCount), 0);
379 return newChangeCount;
380}
381
382} // namespace WebKit
383