12b1178fd714acf42dc9121865153320844b04b4
[WebKit-https.git] / Source / WebKit2 / UIProcess / WebProcessPool.cpp
1 /*
2  * Copyright (C) 2010-2017 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 "WebProcessPool.h"
28
29 #include "APIArray.h"
30 #include "APIAutomationClient.h"
31 #include "APICustomProtocolManagerClient.h"
32 #include "APIDownloadClient.h"
33 #include "APIHTTPCookieStore.h"
34 #include "APILegacyContextHistoryClient.h"
35 #include "APIPageConfiguration.h"
36 #include "APIProcessPoolConfiguration.h"
37 #include "DownloadProxy.h"
38 #include "DownloadProxyMessages.h"
39 #include "GamepadData.h"
40 #include "HighPerformanceGraphicsUsageSampler.h"
41 #include "LegacyCustomProtocolManagerMessages.h"
42 #include "LogInitialization.h"
43 #include "NetworkProcessCreationParameters.h"
44 #include "NetworkProcessMessages.h"
45 #include "NetworkProcessProxy.h"
46 #include "PerActivityStateCPUUsageSampler.h"
47 #include "SandboxExtension.h"
48 #include "StatisticsData.h"
49 #include "TextChecker.h"
50 #include "UIGamepad.h"
51 #include "UIGamepadProvider.h"
52 #include "WKContextPrivate.h"
53 #include "WebAutomationSession.h"
54 #include "WebCertificateInfo.h"
55 #include "WebContextSupplement.h"
56 #include "WebCookieManagerProxy.h"
57 #include "WebCoreArgumentCoders.h"
58 #include "WebGeolocationManagerProxy.h"
59 #include "WebIconDatabase.h"
60 #include "WebKit2Initialize.h"
61 #include "WebMemorySampler.h"
62 #include "WebNotificationManagerProxy.h"
63 #include "WebPageGroup.h"
64 #include "WebPreferences.h"
65 #include "WebPreferencesKeys.h"
66 #include "WebProcessCreationParameters.h"
67 #include "WebProcessMessages.h"
68 #include "WebProcessPoolMessages.h"
69 #include "WebProcessProxy.h"
70 #include "WebsiteDataStore.h"
71 #include "WebsiteDataStoreParameters.h"
72 #include <WebCore/ApplicationCacheStorage.h>
73 #include <WebCore/Language.h>
74 #include <WebCore/LinkHash.h>
75 #include <WebCore/LogInitialization.h>
76 #include <WebCore/ResourceRequest.h>
77 #include <WebCore/SessionID.h>
78 #include <WebCore/URLParser.h>
79 #include <runtime/JSCInlines.h>
80 #include <wtf/CurrentTime.h>
81 #include <wtf/MainThread.h>
82 #include <wtf/NeverDestroyed.h>
83 #include <wtf/RunLoop.h>
84 #include <wtf/text/StringBuilder.h>
85
86 #if ENABLE(DATABASE_PROCESS)
87 #include "DatabaseProcessCreationParameters.h"
88 #include "DatabaseProcessMessages.h"
89 #endif
90
91 #if ENABLE(SERVICE_CONTROLS)
92 #include "ServicesController.h"
93 #endif
94
95 #if ENABLE(REMOTE_INSPECTOR)
96 #include <JavaScriptCore/RemoteInspector.h>
97 #endif
98
99 #if OS(LINUX)
100 #include "MemoryPressureMonitor.h"
101 #endif
102
103 #if PLATFORM(WAYLAND)
104 #include "WaylandCompositor.h"
105 #include <WebCore/PlatformDisplay.h>
106 #endif
107
108 #ifndef NDEBUG
109 #include <wtf/RefCountedLeakCounter.h>
110 #endif
111
112 using namespace WebCore;
113 using namespace WebKit;
114
115 namespace WebKit {
116
117 DEFINE_DEBUG_ONLY_GLOBAL(WTF::RefCountedLeakCounter, processPoolCounter, ("WebProcessPool"));
118
119 static uint64_t generateListenerIdentifier()
120 {
121     static uint64_t nextIdentifier = 1;
122     return nextIdentifier++;
123 }
124
125 static HashMap<uint64_t, Function<void(WebProcessPool&)>>& processPoolCreationListenerFunctionMap()
126 {
127     static NeverDestroyed<HashMap<uint64_t, Function<void(WebProcessPool&)>>> map;
128     return map;
129 }
130
131 uint64_t WebProcessPool::registerProcessPoolCreationListener(Function<void(WebProcessPool&)>&& function)
132 {
133     ASSERT(function);
134
135     auto identifier = generateListenerIdentifier();
136     processPoolCreationListenerFunctionMap().set(identifier, WTFMove(function));
137     return identifier;
138 }
139
140 void WebProcessPool::unregisterProcessPoolCreationListener(uint64_t identifier)
141 {
142     processPoolCreationListenerFunctionMap().remove(identifier);
143 }
144
145 Ref<WebProcessPool> WebProcessPool::create(API::ProcessPoolConfiguration& configuration)
146 {
147     InitializeWebKit2();
148     return adoptRef(*new WebProcessPool(configuration));
149 }
150
151 void WebProcessPool::notifyThisWebProcessPoolWasCreated()
152 {
153     auto& listenerMap = processPoolCreationListenerFunctionMap();
154
155     Vector<uint64_t> identifiers;
156     identifiers.reserveInitialCapacity(listenerMap.size());
157     for (auto identifier : listenerMap.keys())
158         identifiers.uncheckedAppend(identifier);
159
160     for (auto identifier : identifiers) {
161         auto iterator = listenerMap.find(identifier);
162         if (iterator == listenerMap.end())
163             continue;
164
165         // To make sure the Function object stays alive until after the function call has been made,
166         // we temporarily move it out of the map.
167         // This protects it from the Function calling unregisterProcessPoolCreationListener thereby
168         // removing itself from the map of listeners.
169         // If the identifier still exists in the map later, we move it back in.
170         Function<void(WebProcessPool&)> function = WTFMove(iterator->value);
171         function(*this);
172
173         iterator = listenerMap.find(identifier);
174         if (iterator != listenerMap.end()) {
175             ASSERT(!iterator->value);
176             iterator->value = WTFMove(function);
177         }
178     }
179 }
180
181 static Vector<WebProcessPool*>& processPools()
182 {
183     static NeverDestroyed<Vector<WebProcessPool*>> processPools;
184     return processPools;
185 }
186
187 const Vector<WebProcessPool*>& WebProcessPool::allProcessPools()
188 {
189     return processPools();
190 }
191
192 static WebsiteDataStore::Configuration legacyWebsiteDataStoreConfiguration(API::ProcessPoolConfiguration& processPoolConfiguration)
193 {
194     WebsiteDataStore::Configuration configuration;
195
196     configuration.localStorageDirectory = processPoolConfiguration.localStorageDirectory();
197     configuration.webSQLDatabaseDirectory = processPoolConfiguration.webSQLDatabaseDirectory();
198     configuration.applicationCacheDirectory = processPoolConfiguration.applicationCacheDirectory();
199     configuration.applicationCacheFlatFileSubdirectoryName = processPoolConfiguration.applicationCacheFlatFileSubdirectoryName();
200     configuration.mediaCacheDirectory = processPoolConfiguration.mediaCacheDirectory();
201     configuration.mediaKeysStorageDirectory = processPoolConfiguration.mediaKeysStorageDirectory();
202     configuration.networkCacheDirectory = processPoolConfiguration.diskCacheDirectory();
203     configuration.javaScriptConfigurationDirectory = processPoolConfiguration.javaScriptConfigurationDirectory();
204
205     // This is needed to support legacy WK2 clients, which did not have resource load statistics.
206     configuration.resourceLoadStatisticsDirectory = API::WebsiteDataStore::defaultResourceLoadStatisticsDirectory();
207
208     return configuration;
209 }
210
211 static HashSet<String, ASCIICaseInsensitiveHash>& globalURLSchemesWithCustomProtocolHandlers()
212 {
213     static NeverDestroyed<HashSet<String, ASCIICaseInsensitiveHash>> set;
214     return set;
215 }
216
217 WebProcessPool::WebProcessPool(API::ProcessPoolConfiguration& configuration)
218     : m_configuration(configuration.copy())
219     , m_haveInitialEmptyProcess(false)
220     , m_processWithPageCache(0)
221     , m_defaultPageGroup(WebPageGroup::createNonNull())
222     , m_automationClient(std::make_unique<API::AutomationClient>())
223     , m_downloadClient(std::make_unique<API::DownloadClient>())
224     , m_historyClient(std::make_unique<API::LegacyContextHistoryClient>())
225     , m_customProtocolManagerClient(std::make_unique<API::CustomProtocolManagerClient>())
226     , m_visitedLinkStore(VisitedLinkStore::create())
227     , m_visitedLinksPopulated(false)
228     , m_plugInAutoStartProvider(this)
229     , m_alwaysUsesComplexTextCodePath(false)
230     , m_shouldUseFontSmoothing(true)
231     , m_memorySamplerEnabled(false)
232     , m_memorySamplerInterval(1400.0)
233     , m_websiteDataStore(m_configuration->shouldHaveLegacyDataStore() ? API::WebsiteDataStore::createLegacy(legacyWebsiteDataStoreConfiguration(m_configuration)) : API::WebsiteDataStore::defaultDataStore())
234 #if PLATFORM(MAC)
235     , m_highPerformanceGraphicsUsageSampler(std::make_unique<HighPerformanceGraphicsUsageSampler>(*this))
236     , m_perActivityStateCPUUsageSampler(std::make_unique<PerActivityStateCPUUsageSampler>(*this))
237 #endif
238     , m_shouldUseTestingNetworkSession(false)
239     , m_processTerminationEnabled(true)
240     , m_canHandleHTTPSServerTrustEvaluation(true)
241     , m_didNetworkProcessCrash(false)
242     , m_memoryCacheDisabled(false)
243     , m_alwaysRunsAtBackgroundPriority(m_configuration->alwaysRunsAtBackgroundPriority())
244     , m_shouldTakeUIBackgroundAssertion(m_configuration->shouldTakeUIBackgroundAssertion())
245     , m_userObservablePageCounter([this](RefCounterEvent) { updateProcessSuppressionState(); })
246     , m_processSuppressionDisabledForPageCounter([this](RefCounterEvent) { updateProcessSuppressionState(); })
247     , m_hiddenPageThrottlingAutoIncreasesCounter([this](RefCounterEvent) { m_hiddenPageThrottlingTimer.startOneShot(0_s); })
248     , m_hiddenPageThrottlingTimer(RunLoop::main(), this, &WebProcessPool::updateHiddenPageThrottlingAutoIncreaseLimit)
249 {
250     for (auto& scheme : m_configuration->alwaysRevalidatedURLSchemes())
251         m_schemesToRegisterAsAlwaysRevalidated.add(scheme);
252
253     for (const auto& urlScheme : m_configuration->cachePartitionedURLSchemes())
254         m_schemesToRegisterAsCachePartitioned.add(urlScheme);
255
256     platformInitialize();
257
258     addMessageReceiver(Messages::WebProcessPool::messageReceiverName(), *this);
259
260     // NOTE: These sub-objects must be initialized after m_messageReceiverMap..
261     m_iconDatabase = WebIconDatabase::create(this);
262
263     addSupplement<WebCookieManagerProxy>();
264     addSupplement<WebGeolocationManagerProxy>();
265     addSupplement<WebNotificationManagerProxy>();
266 #if ENABLE(MEDIA_SESSION)
267     addSupplement<WebMediaSessionFocusManager>();
268 #endif
269
270     processPools().append(this);
271
272     addLanguageChangeObserver(this, languageChanged);
273
274     resolvePathsForSandboxExtensions();
275
276 #if !LOG_DISABLED || !RELEASE_LOG_DISABLED
277     WebCore::initializeLogChannelsIfNecessary();
278     WebKit::initializeLogChannelsIfNecessary();
279 #endif // !LOG_DISABLED || !RELEASE_LOG_DISABLED
280
281 #ifndef NDEBUG
282     processPoolCounter.increment();
283 #endif
284
285     notifyThisWebProcessPoolWasCreated();
286 }
287
288 #if !PLATFORM(COCOA) && !PLATFORM(GTK) && !PLATFORM(WPE)
289 void WebProcessPool::platformInitialize()
290 {
291 }
292 #endif
293
294 WebProcessPool::~WebProcessPool()
295 {
296     bool removed = processPools().removeFirst(this);
297     ASSERT_UNUSED(removed, removed);
298
299     removeLanguageChangeObserver(this);
300
301     m_messageReceiverMap.invalidate();
302
303     for (auto& supplement : m_supplements.values()) {
304         supplement->processPoolDestroyed();
305         supplement->clearProcessPool();
306     }
307
308     m_iconDatabase->invalidate();
309     m_iconDatabase->clearProcessPool();
310     WebIconDatabase* rawIconDatabase = m_iconDatabase.leakRef();
311     rawIconDatabase->derefWhenAppropriate();
312
313     invalidateCallbackMap(m_dictionaryCallbacks, CallbackBase::Error::OwnerWasInvalidated);
314
315     platformInvalidateContext();
316
317 #ifndef NDEBUG
318     processPoolCounter.decrement();
319 #endif
320
321     if (m_networkProcess)
322         m_networkProcess->shutDownProcess();
323
324 #if ENABLE(GAMEPAD)
325     if (!m_processesUsingGamepads.isEmpty())
326         UIGamepadProvider::singleton().processPoolStoppedUsingGamepads(*this);
327 #endif
328 }
329
330 void WebProcessPool::initializeClient(const WKContextClientBase* client)
331 {
332     m_client.initialize(client);
333 }
334
335 void WebProcessPool::initializeInjectedBundleClient(const WKContextInjectedBundleClientBase* client)
336 {
337     m_injectedBundleClient.initialize(client);
338 }
339
340 void WebProcessPool::initializeConnectionClient(const WKContextConnectionClientBase* client)
341 {
342     m_connectionClient.initialize(client);
343 }
344
345 void WebProcessPool::setHistoryClient(std::unique_ptr<API::LegacyContextHistoryClient> historyClient)
346 {
347     if (!historyClient)
348         m_historyClient = std::make_unique<API::LegacyContextHistoryClient>();
349     else
350         m_historyClient = WTFMove(historyClient);
351 }
352
353 void WebProcessPool::setDownloadClient(std::unique_ptr<API::DownloadClient> downloadClient)
354 {
355     if (!downloadClient)
356         m_downloadClient = std::make_unique<API::DownloadClient>();
357     else
358         m_downloadClient = WTFMove(downloadClient);
359 }
360
361 void WebProcessPool::setAutomationClient(std::unique_ptr<API::AutomationClient> automationClient)
362 {
363     if (!automationClient)
364         m_automationClient = std::make_unique<API::AutomationClient>();
365     else
366         m_automationClient = WTFMove(automationClient);
367 }
368
369 void WebProcessPool::setLegacyCustomProtocolManagerClient(std::unique_ptr<API::CustomProtocolManagerClient>&& customProtocolManagerClient)
370 {
371     if (!customProtocolManagerClient)
372         m_customProtocolManagerClient = std::make_unique<API::CustomProtocolManagerClient>();
373     else
374         m_customProtocolManagerClient = WTFMove(customProtocolManagerClient);
375 }
376
377 void WebProcessPool::setMaximumNumberOfProcesses(unsigned maximumNumberOfProcesses)
378 {
379     // Guard against API misuse.
380     if (!m_processes.isEmpty())
381         CRASH();
382
383     m_configuration->setMaximumProcessCount(maximumNumberOfProcesses);
384 }
385
386 IPC::Connection* WebProcessPool::networkingProcessConnection()
387 {
388     return m_networkProcess->connection();
389 }
390
391 void WebProcessPool::languageChanged(void* context)
392 {
393     static_cast<WebProcessPool*>(context)->languageChanged();
394 }
395
396 void WebProcessPool::languageChanged()
397 {
398     sendToAllProcesses(Messages::WebProcess::UserPreferredLanguagesChanged(userPreferredLanguages()));
399 #if USE(SOUP)
400     if (m_networkProcess)
401         m_networkProcess->send(Messages::NetworkProcess::UserPreferredLanguagesChanged(userPreferredLanguages()), 0);
402 #endif
403 }
404
405 void WebProcessPool::fullKeyboardAccessModeChanged(bool fullKeyboardAccessEnabled)
406 {
407     sendToAllProcesses(Messages::WebProcess::FullKeyboardAccessModeChanged(fullKeyboardAccessEnabled));
408 }
409
410 void WebProcessPool::textCheckerStateChanged()
411 {
412     sendToAllProcesses(Messages::WebProcess::SetTextCheckerState(TextChecker::state()));
413 }
414
415 NetworkProcessProxy& WebProcessPool::ensureNetworkProcess()
416 {
417     if (m_networkProcess)
418         return *m_networkProcess;
419
420     m_networkProcess = NetworkProcessProxy::create(*this);
421
422     NetworkProcessCreationParameters parameters;
423
424     parameters.privateBrowsingEnabled = WebPreferences::anyPagesAreUsingPrivateBrowsing();
425
426     parameters.cacheModel = cacheModel();
427     parameters.diskCacheSizeOverride = m_configuration->diskCacheSizeOverride();
428     parameters.canHandleHTTPSServerTrustEvaluation = m_canHandleHTTPSServerTrustEvaluation;
429
430     for (auto& scheme : globalURLSchemesWithCustomProtocolHandlers())
431         parameters.urlSchemesRegisteredForCustomProtocols.append(scheme);
432
433     for (auto& scheme : m_urlSchemesRegisteredForCustomProtocols)
434         parameters.urlSchemesRegisteredForCustomProtocols.append(scheme);
435
436     parameters.diskCacheDirectory = m_configuration->diskCacheDirectory();
437     if (!parameters.diskCacheDirectory.isEmpty())
438         SandboxExtension::createHandleForReadWriteDirectory(parameters.diskCacheDirectory, parameters.diskCacheDirectoryExtensionHandle);
439 #if ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION)
440     parameters.shouldEnableNetworkCacheSpeculativeRevalidation = m_configuration->diskCacheSpeculativeValidationEnabled();
441 #endif
442
443 #if PLATFORM(IOS)
444     String cookieStorageDirectory = this->cookieStorageDirectory();
445     if (!cookieStorageDirectory.isEmpty())
446         SandboxExtension::createHandleForReadWriteDirectory(cookieStorageDirectory, parameters.cookieStorageDirectoryExtensionHandle);
447
448     String containerCachesDirectory = this->networkingCachesDirectory();
449     if (!containerCachesDirectory.isEmpty())
450         SandboxExtension::createHandleForReadWriteDirectory(containerCachesDirectory, parameters.containerCachesDirectoryExtensionHandle);
451
452     String parentBundleDirectory = this->parentBundleDirectory();
453     if (!parentBundleDirectory.isEmpty())
454         SandboxExtension::createHandle(parentBundleDirectory, SandboxExtension::ReadOnly, parameters.parentBundleDirectoryExtensionHandle);
455 #endif
456
457 #if OS(LINUX)
458     if (MemoryPressureMonitor::isEnabled())
459         parameters.memoryPressureMonitorHandle = MemoryPressureMonitor::singleton().createHandle();
460 #endif
461
462     parameters.shouldUseTestingNetworkSession = m_shouldUseTestingNetworkSession;
463     parameters.presentingApplicationPID = getpid();
464
465     // Add any platform specific parameters
466     platformInitializeNetworkProcess(parameters);
467
468     // Initialize the network process.
469     m_networkProcess->send(Messages::NetworkProcess::InitializeNetworkProcess(parameters), 0);
470
471 #if PLATFORM(COCOA)
472     m_networkProcess->send(Messages::NetworkProcess::SetQOS(networkProcessLatencyQOS(), networkProcessThroughputQOS()), 0);
473 #endif
474
475     if (m_didNetworkProcessCrash) {
476         m_didNetworkProcessCrash = false;
477         for (auto& process : m_processes)
478             process->reinstateNetworkProcessAssertionState(*m_networkProcess);
479     }
480
481     return *m_networkProcess;
482 }
483
484 void WebProcessPool::networkProcessCrashed(NetworkProcessProxy* networkProcessProxy)
485 {
486     ASSERT(m_networkProcess);
487     ASSERT(networkProcessProxy == m_networkProcess.get());
488     m_didNetworkProcessCrash = true;
489
490     for (auto& supplement : m_supplements.values())
491         supplement->processDidClose(networkProcessProxy);
492
493     m_client.networkProcessDidCrash(this);
494
495     // Leave the process proxy around during client call, so that the client could query the process identifier.
496     m_networkProcess = nullptr;
497 }
498
499 void WebProcessPool::getNetworkProcessConnection(Ref<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply>&& reply)
500 {
501     ensureNetworkProcess();
502     ASSERT(m_networkProcess);
503
504     m_networkProcess->getNetworkProcessConnection(WTFMove(reply));
505 }
506
507 #if ENABLE(DATABASE_PROCESS)
508 void WebProcessPool::ensureDatabaseProcessAndWebsiteDataStore(WebsiteDataStore* relevantDataStore)
509 {
510     // *********
511     // IMPORTANT: Do not change the directory structure for indexed databases on disk without first consulting a reviewer from Apple (<rdar://problem/17454712>)
512     // *********
513
514     if (!m_databaseProcess) {
515         m_databaseProcess = DatabaseProcessProxy::create(this);
516
517         DatabaseProcessCreationParameters parameters;
518 #if ENABLE(INDEXED_DATABASE)
519         ASSERT(!m_configuration->indexedDBDatabaseDirectory().isEmpty());
520
521         parameters.sessionID = websiteDataStore().websiteDataStore().sessionID();
522         parameters.indexedDatabaseDirectory = m_configuration->indexedDBDatabaseDirectory();
523         SandboxExtension::createHandleForReadWriteDirectory(parameters.indexedDatabaseDirectory, parameters.indexedDatabaseDirectoryExtensionHandle);
524 #endif
525
526         ASSERT(!parameters.indexedDatabaseDirectory.isEmpty());
527         m_databaseProcess->send(Messages::DatabaseProcess::InitializeWebsiteDataStore(parameters), 0);
528     }
529
530     if (!relevantDataStore || relevantDataStore == &websiteDataStore().websiteDataStore())
531         return;
532
533     m_databaseProcess->send(Messages::DatabaseProcess::InitializeWebsiteDataStore(relevantDataStore->databaseProcessParameters()), 0);
534 }
535
536 void WebProcessPool::getDatabaseProcessConnection(Ref<Messages::WebProcessProxy::GetDatabaseProcessConnection::DelayedReply>&& reply)
537 {
538     ensureDatabaseProcessAndWebsiteDataStore(nullptr);
539
540     m_databaseProcess->getDatabaseProcessConnection(WTFMove(reply));
541 }
542
543 void WebProcessPool::databaseProcessCrashed(DatabaseProcessProxy* databaseProcessProxy)
544 {
545     ASSERT(m_databaseProcess);
546     ASSERT(databaseProcessProxy == m_databaseProcess.get());
547
548     for (auto& supplement : m_supplements.values())
549         supplement->processDidClose(databaseProcessProxy);
550
551     m_client.databaseProcessDidCrash(this);
552     m_databaseProcess = nullptr;
553 }
554 #endif
555
556 void WebProcessPool::willStartUsingPrivateBrowsing()
557 {
558     for (auto* processPool : allProcessPools())
559         processPool->setAnyPageGroupMightHavePrivateBrowsingEnabled(true);
560 }
561
562 void WebProcessPool::willStopUsingPrivateBrowsing()
563 {
564     for (auto* processPool : allProcessPools())
565         processPool->setAnyPageGroupMightHavePrivateBrowsingEnabled(false);
566 }
567
568 void WebProcessPool::windowServerConnectionStateChanged()
569 {
570     size_t processCount = m_processes.size();
571     for (size_t i = 0; i < processCount; ++i)
572         m_processes[i]->windowServerConnectionStateChanged();
573 }
574
575 void WebProcessPool::setAnyPageGroupMightHavePrivateBrowsingEnabled(bool privateBrowsingEnabled)
576 {
577     m_iconDatabase->setPrivateBrowsingEnabled(privateBrowsingEnabled);
578
579     if (networkProcess()) {
580         if (privateBrowsingEnabled)
581             networkProcess()->send(Messages::NetworkProcess::EnsurePrivateBrowsingSession({SessionID::legacyPrivateSessionID(), { }, { }, { }}), 0);
582         else
583             networkProcess()->send(Messages::NetworkProcess::DestroySession(SessionID::legacyPrivateSessionID()), 0);
584     }
585
586     if (privateBrowsingEnabled)
587         sendToAllProcesses(Messages::WebProcess::EnsurePrivateBrowsingSession(SessionID::legacyPrivateSessionID()));
588     else
589         sendToAllProcesses(Messages::WebProcess::DestroySession(SessionID::legacyPrivateSessionID()));
590 }
591
592 void (*s_invalidMessageCallback)(WKStringRef messageName);
593
594 void WebProcessPool::setInvalidMessageCallback(void (*invalidMessageCallback)(WKStringRef messageName))
595 {
596     s_invalidMessageCallback = invalidMessageCallback;
597 }
598
599 void WebProcessPool::didReceiveInvalidMessage(const IPC::StringReference& messageReceiverName, const IPC::StringReference& messageName)
600 {
601     if (!s_invalidMessageCallback)
602         return;
603
604     StringBuilder messageNameStringBuilder;
605     messageNameStringBuilder.append(messageReceiverName.data(), messageReceiverName.size());
606     messageNameStringBuilder.append('.');
607     messageNameStringBuilder.append(messageName.data(), messageName.size());
608
609     s_invalidMessageCallback(toAPI(API::String::create(messageNameStringBuilder.toString()).ptr()));
610 }
611
612 void WebProcessPool::processDidCachePage(WebProcessProxy* process)
613 {
614     if (m_processWithPageCache && m_processWithPageCache != process)
615         m_processWithPageCache->releasePageCache();
616     m_processWithPageCache = process;
617 }
618
619 void WebProcessPool::resolvePathsForSandboxExtensions()
620 {
621     m_resolvedPaths.injectedBundlePath = resolvePathForSandboxExtension(injectedBundlePath());
622     m_resolvedPaths.applicationCacheDirectory = resolveAndCreateReadWriteDirectoryForSandboxExtension(m_configuration->applicationCacheDirectory());
623     m_resolvedPaths.webSQLDatabaseDirectory = resolveAndCreateReadWriteDirectoryForSandboxExtension(m_configuration->webSQLDatabaseDirectory());
624     m_resolvedPaths.mediaCacheDirectory = resolveAndCreateReadWriteDirectoryForSandboxExtension(m_configuration->mediaCacheDirectory());
625     m_resolvedPaths.mediaKeyStorageDirectory = resolveAndCreateReadWriteDirectoryForSandboxExtension(m_configuration->mediaKeysStorageDirectory());
626     m_resolvedPaths.indexedDatabaseDirectory = resolveAndCreateReadWriteDirectoryForSandboxExtension(m_configuration->indexedDBDatabaseDirectory());
627
628     m_resolvedPaths.additionalWebProcessSandboxExtensionPaths.reserveCapacity(m_configuration->additionalReadAccessAllowedPaths().size());
629     for (const auto& path : m_configuration->additionalReadAccessAllowedPaths())
630         m_resolvedPaths.additionalWebProcessSandboxExtensionPaths.uncheckedAppend(resolvePathForSandboxExtension(path));
631
632     platformResolvePathsForSandboxExtensions();
633 }
634
635 WebProcessProxy& WebProcessPool::createNewWebProcess(WebsiteDataStore& websiteDataStore)
636 {
637     ensureNetworkProcess();
638
639     Ref<WebProcessProxy> process = WebProcessProxy::create(*this, websiteDataStore);
640
641     WebProcessCreationParameters parameters;
642
643     websiteDataStore.resolveDirectoriesIfNecessary();
644
645     parameters.injectedBundlePath = m_resolvedPaths.injectedBundlePath;
646     if (!parameters.injectedBundlePath.isEmpty())
647         SandboxExtension::createHandleWithoutResolvingPath(parameters.injectedBundlePath, SandboxExtension::ReadOnly, parameters.injectedBundlePathExtensionHandle);
648
649     parameters.additionalSandboxExtensionHandles.allocate(m_resolvedPaths.additionalWebProcessSandboxExtensionPaths.size());
650     for (size_t i = 0, size = m_resolvedPaths.additionalWebProcessSandboxExtensionPaths.size(); i < size; ++i)
651         SandboxExtension::createHandleWithoutResolvingPath(m_resolvedPaths.additionalWebProcessSandboxExtensionPaths[i], SandboxExtension::ReadOnly, parameters.additionalSandboxExtensionHandles[i]);
652
653     parameters.applicationCacheDirectory = websiteDataStore.resolvedApplicationCacheDirectory();
654     if (parameters.applicationCacheDirectory.isEmpty())
655         parameters.applicationCacheDirectory = m_resolvedPaths.applicationCacheDirectory;
656     if (!parameters.applicationCacheDirectory.isEmpty())
657         SandboxExtension::createHandleWithoutResolvingPath(parameters.applicationCacheDirectory, SandboxExtension::ReadWrite, parameters.applicationCacheDirectoryExtensionHandle);
658
659     parameters.applicationCacheFlatFileSubdirectoryName = m_configuration->applicationCacheFlatFileSubdirectoryName();
660
661     parameters.webSQLDatabaseDirectory = websiteDataStore.resolvedDatabaseDirectory();
662     if (parameters.webSQLDatabaseDirectory.isEmpty())
663         parameters.webSQLDatabaseDirectory = m_resolvedPaths.webSQLDatabaseDirectory;
664     if (!parameters.webSQLDatabaseDirectory.isEmpty())
665         SandboxExtension::createHandleWithoutResolvingPath(parameters.webSQLDatabaseDirectory, SandboxExtension::ReadWrite, parameters.webSQLDatabaseDirectoryExtensionHandle);
666
667     parameters.mediaCacheDirectory = websiteDataStore.resolvedMediaCacheDirectory();
668     if (parameters.mediaCacheDirectory.isEmpty())
669         parameters.mediaCacheDirectory = m_resolvedPaths.mediaCacheDirectory;
670     if (!parameters.mediaCacheDirectory.isEmpty())
671         SandboxExtension::createHandleWithoutResolvingPath(parameters.mediaCacheDirectory, SandboxExtension::ReadWrite, parameters.mediaCacheDirectoryExtensionHandle);
672
673     parameters.mediaKeyStorageDirectory = websiteDataStore.resolvedMediaKeysDirectory();
674     if (parameters.mediaKeyStorageDirectory.isEmpty())
675         parameters.mediaKeyStorageDirectory = m_resolvedPaths.mediaKeyStorageDirectory;
676     if (!parameters.mediaKeyStorageDirectory.isEmpty())
677         SandboxExtension::createHandleWithoutResolvingPath(parameters.mediaKeyStorageDirectory, SandboxExtension::ReadWrite, parameters.mediaKeyStorageDirectoryExtensionHandle);
678
679 #if PLATFORM(IOS)
680     setJavaScriptConfigurationFileEnabledFromDefaults();
681 #endif
682
683     if (javaScriptConfigurationFileEnabled()) {
684         parameters.javaScriptConfigurationDirectory = websiteDataStore.resolvedJavaScriptConfigurationDirectory();
685         if (!parameters.javaScriptConfigurationDirectory.isEmpty())
686             SandboxExtension::createHandleWithoutResolvingPath(parameters.javaScriptConfigurationDirectory, SandboxExtension::ReadWrite, parameters.javaScriptConfigurationDirectoryExtensionHandle);
687     }
688
689     parameters.shouldUseTestingNetworkSession = m_shouldUseTestingNetworkSession;
690
691     parameters.cacheModel = cacheModel();
692     parameters.languages = userPreferredLanguages();
693
694     copyToVector(m_schemesToRegisterAsEmptyDocument, parameters.urlSchemesRegisteredAsEmptyDocument);
695     copyToVector(m_schemesToRegisterAsSecure, parameters.urlSchemesRegisteredAsSecure);
696     copyToVector(m_schemesToRegisterAsBypassingContentSecurityPolicy, parameters.urlSchemesRegisteredAsBypassingContentSecurityPolicy);
697     copyToVector(m_schemesToSetDomainRelaxationForbiddenFor, parameters.urlSchemesForWhichDomainRelaxationIsForbidden);
698     copyToVector(m_schemesToRegisterAsLocal, parameters.urlSchemesRegisteredAsLocal);
699     copyToVector(m_schemesToRegisterAsNoAccess, parameters.urlSchemesRegisteredAsNoAccess);
700     copyToVector(m_schemesToRegisterAsDisplayIsolated, parameters.urlSchemesRegisteredAsDisplayIsolated);
701     copyToVector(m_schemesToRegisterAsCORSEnabled, parameters.urlSchemesRegisteredAsCORSEnabled);
702     copyToVector(m_schemesToRegisterAsAlwaysRevalidated, parameters.urlSchemesRegisteredAsAlwaysRevalidated);
703     copyToVector(m_schemesToRegisterAsCachePartitioned, parameters.urlSchemesRegisteredAsCachePartitioned);
704
705     parameters.shouldAlwaysUseComplexTextCodePath = m_alwaysUsesComplexTextCodePath;
706     parameters.shouldUseFontSmoothing = m_shouldUseFontSmoothing;
707
708     parameters.iconDatabaseEnabled = !iconDatabasePath().isEmpty();
709
710     parameters.terminationTimeout = 0_s;
711
712     parameters.textCheckerState = TextChecker::state();
713
714     parameters.fullKeyboardAccessEnabled = WebProcessProxy::fullKeyboardAccessEnabled();
715
716     parameters.defaultRequestTimeoutInterval = API::URLRequest::defaultTimeoutInterval();
717
718 #if ENABLE(NOTIFICATIONS)
719     // FIXME: There should be a generic way for supplements to add to the intialization parameters.
720     supplement<WebNotificationManagerProxy>()->populateCopyOfNotificationPermissions(parameters.notificationPermissions);
721 #endif
722
723     parameters.plugInAutoStartOriginHashes = m_plugInAutoStartProvider.autoStartOriginHashesCopy();
724     copyToVector(m_plugInAutoStartProvider.autoStartOrigins(), parameters.plugInAutoStartOrigins);
725
726     parameters.memoryCacheDisabled = m_memoryCacheDisabled;
727
728 #if ENABLE(SERVICE_CONTROLS)
729     auto& serviceController = ServicesController::singleton();
730     parameters.hasImageServices = serviceController.hasImageServices();
731     parameters.hasSelectionServices = serviceController.hasSelectionServices();
732     parameters.hasRichContentServices = serviceController.hasRichContentServices();
733     serviceController.refreshExistingServices();
734 #endif
735
736 #if ENABLE(NETSCAPE_PLUGIN_API)
737     parameters.pluginLoadClientPolicies = m_pluginLoadClientPolicies;
738 #endif
739
740 #if OS(LINUX)
741     parameters.shouldEnableMemoryPressureReliefLogging = true;
742     if (MemoryPressureMonitor::isEnabled())
743         parameters.memoryPressureMonitorHandle = MemoryPressureMonitor::singleton().createHandle();
744 #endif
745
746 #if PLATFORM(WAYLAND) && USE(EGL)
747     if (PlatformDisplay::sharedDisplay().type() == PlatformDisplay::Type::Wayland)
748         parameters.waylandCompositorDisplayName = WaylandCompositor::singleton().displayName();
749 #endif
750
751     parameters.resourceLoadStatisticsEnabled = resourceLoadStatisticsEnabled();
752 #if ENABLE(MEDIA_STREAM)
753     parameters.shouldCaptureAudioInUIProcess = m_configuration->shouldCaptureAudioInUIProcess();
754 #endif
755
756     parameters.presentingApplicationPID = getpid();
757
758     // Add any platform specific parameters
759     platformInitializeWebProcess(parameters);
760
761     RefPtr<API::Object> injectedBundleInitializationUserData = m_injectedBundleClient.getInjectedBundleInitializationUserData(this);
762     if (!injectedBundleInitializationUserData)
763         injectedBundleInitializationUserData = m_injectedBundleInitializationUserData;
764     parameters.initializationUserData = UserData(process->transformObjectsToHandles(injectedBundleInitializationUserData.get()));
765
766     process->send(Messages::WebProcess::InitializeWebProcess(parameters), 0);
767
768 #if PLATFORM(COCOA)
769     process->send(Messages::WebProcess::SetQOS(webProcessLatencyQOS(), webProcessThroughputQOS()), 0);
770 #endif
771
772     if (WebPreferences::anyPagesAreUsingPrivateBrowsing())
773         process->send(Messages::WebProcess::EnsurePrivateBrowsingSession(SessionID::legacyPrivateSessionID()), 0);
774
775     if (m_automationSession)
776         process->send(Messages::WebProcess::EnsureAutomationSessionProxy(m_automationSession->sessionIdentifier()), 0);
777
778     m_processes.append(process.ptr());
779
780     ASSERT(m_messagesToInjectedBundlePostedToEmptyContext.isEmpty());
781
782 #if ENABLE(REMOTE_INSPECTOR)
783     // Initialize remote inspector connection now that we have a sub-process that is hosting one of our web views.
784     Inspector::RemoteInspector::singleton(); 
785 #endif
786
787     return process;
788 }
789
790 void WebProcessPool::warmInitialProcess()  
791 {
792     if (m_haveInitialEmptyProcess) {
793         ASSERT(!m_processes.isEmpty());
794         return;
795     }
796
797     if (m_processes.size() >= maximumNumberOfProcesses())
798         return;
799
800     createNewWebProcess(m_websiteDataStore->websiteDataStore());
801     m_haveInitialEmptyProcess = true;
802 }
803
804 void WebProcessPool::enableProcessTermination()
805 {
806     m_processTerminationEnabled = true;
807     Vector<RefPtr<WebProcessProxy>> processes = m_processes;
808     for (size_t i = 0; i < processes.size(); ++i) {
809         if (shouldTerminate(processes[i].get()))
810             processes[i]->terminate();
811     }
812 }
813
814 bool WebProcessPool::shouldTerminate(WebProcessProxy* process)
815 {
816     ASSERT(m_processes.contains(process));
817
818     if (!m_processTerminationEnabled)
819         return false;
820
821     return true;
822 }
823
824 void WebProcessPool::processDidFinishLaunching(WebProcessProxy* process)
825 {
826     ASSERT(m_processes.contains(process));
827
828     if (!m_visitedLinksPopulated) {
829         populateVisitedLinks();
830         m_visitedLinksPopulated = true;
831     }
832
833     // Sometimes the memorySampler gets initialized after process initialization has happened but before the process has finished launching
834     // so check if it needs to be started here
835     if (m_memorySamplerEnabled) {
836         SandboxExtension::Handle sampleLogSandboxHandle;        
837         double now = WTF::currentTime();
838         String sampleLogFilePath = String::format("WebProcess%llupid%d", static_cast<unsigned long long>(now), process->processIdentifier());
839         sampleLogFilePath = SandboxExtension::createHandleForTemporaryFile(sampleLogFilePath, SandboxExtension::ReadWrite, sampleLogSandboxHandle);
840         
841         process->send(Messages::WebProcess::StartMemorySampler(sampleLogSandboxHandle, sampleLogFilePath, m_memorySamplerInterval), 0);
842     }
843
844     if (m_configuration->fullySynchronousModeIsAllowedForTesting())
845         process->connection()->allowFullySynchronousModeForTesting();
846
847     if (m_configuration->ignoreSynchronousMessagingTimeoutsForTesting())
848         process->connection()->ignoreTimeoutsForTesting();
849
850     m_connectionClient.didCreateConnection(this, process->webConnection());
851 }
852
853 void WebProcessPool::disconnectProcess(WebProcessProxy* process)
854 {
855     ASSERT(m_processes.contains(process));
856
857     if (m_haveInitialEmptyProcess && process == m_processes.last())
858         m_haveInitialEmptyProcess = false;
859
860     // FIXME (Multi-WebProcess): <rdar://problem/12239765> Some of the invalidation calls of the other supplements are still necessary in multi-process mode, but they should only affect data structures pertaining to the process being disconnected.
861     // Clearing everything causes assertion failures, so it's less trouble to skip that for now.
862     RefPtr<WebProcessProxy> protect(process);
863     if (m_processWithPageCache == process)
864         m_processWithPageCache = nullptr;
865
866     static_cast<WebContextSupplement*>(supplement<WebGeolocationManagerProxy>())->processDidClose(process);
867
868     m_processes.removeFirst(process);
869
870 #if ENABLE(GAMEPAD)
871     if (m_processesUsingGamepads.contains(process))
872         processStoppedUsingGamepads(*process);
873 #endif
874 }
875
876 WebProcessProxy& WebProcessPool::createNewWebProcessRespectingProcessCountLimit(WebsiteDataStore& websiteDataStore)
877 {
878     // FIXME: https://bugs.webkit.org/show_bug.cgi?id=168676
879     // Once WebsiteDataStores are truly per-view instead of per-process, remove this nonsense.
880
881 #if PLATFORM(COCOA)
882     bool mustMatchDataStore = &websiteDataStore != &API::WebsiteDataStore::defaultDataStore()->websiteDataStore();
883 #else
884     bool mustMatchDataStore = false;
885 #endif
886
887     if (m_processes.size() < maximumNumberOfProcesses())
888         return createNewWebProcess(websiteDataStore);
889
890     Vector<RefPtr<WebProcessProxy>> processesMatchingDataStore;
891     if (mustMatchDataStore) {
892         for (auto& process : m_processes) {
893             if (&process->websiteDataStore() == &websiteDataStore)
894                 processesMatchingDataStore.append(process);
895         }
896
897         if (processesMatchingDataStore.isEmpty())
898             return createNewWebProcess(websiteDataStore);
899     }
900
901     // Choose the process with fewest pages.
902     auto* processes = mustMatchDataStore ? &processesMatchingDataStore : &m_processes;
903     ASSERT(!processes->isEmpty());
904     auto& process = *std::min_element(processes->begin(), processes->end(), [](const RefPtr<WebProcessProxy>& a, const RefPtr<WebProcessProxy>& b) {
905         return a->pageCount() < b->pageCount();
906     });
907
908     return *process;
909 }
910
911 Ref<WebPageProxy> WebProcessPool::createWebPage(PageClient& pageClient, Ref<API::PageConfiguration>&& pageConfiguration)
912 {
913     if (!pageConfiguration->pageGroup())
914         pageConfiguration->setPageGroup(m_defaultPageGroup.ptr());
915     if (!pageConfiguration->preferences())
916         pageConfiguration->setPreferences(&pageConfiguration->pageGroup()->preferences());
917     if (!pageConfiguration->userContentController())
918         pageConfiguration->setUserContentController(&pageConfiguration->pageGroup()->userContentController());
919     if (!pageConfiguration->visitedLinkStore())
920         pageConfiguration->setVisitedLinkStore(m_visitedLinkStore.ptr());
921
922     if (!pageConfiguration->websiteDataStore()) {
923         ASSERT(!pageConfiguration->sessionID().isValid());
924         pageConfiguration->setWebsiteDataStore(m_websiteDataStore.ptr());
925         pageConfiguration->setSessionID(pageConfiguration->preferences()->privateBrowsingEnabled() ? SessionID::legacyPrivateSessionID() : m_websiteDataStore->websiteDataStore().sessionID());
926     }
927
928     RefPtr<WebProcessProxy> process;
929     if (m_haveInitialEmptyProcess) {
930         process = m_processes.last();
931         m_haveInitialEmptyProcess = false;
932     } else if (pageConfiguration->relatedPage()) {
933         // Sharing processes, e.g. when creating the page via window.open().
934         process = &pageConfiguration->relatedPage()->process();
935     } else
936         process = &createNewWebProcessRespectingProcessCountLimit(pageConfiguration->websiteDataStore()->websiteDataStore());
937
938     return process->createWebPage(pageClient, WTFMove(pageConfiguration));
939 }
940
941 void WebProcessPool::pageAddedToProcess(WebPageProxy& page)
942 {
943     auto result = m_sessionToPagesMap.add(page.sessionID(), HashSet<WebPageProxy*>()).iterator->value.add(&page);
944     ASSERT_UNUSED(result, result.isNewEntry);
945
946     auto sessionID = page.sessionID();
947     if (sessionID.isEphemeral()) {
948         // FIXME: Merge NetworkProcess::EnsurePrivateBrowsingSession and NetworkProcess::AddWebsiteDataStore into one message type.
949         // They do basically the same thing.
950         sendToNetworkingProcess(Messages::NetworkProcess::EnsurePrivateBrowsingSession(page.websiteDataStore().parameters()));
951         page.process().send(Messages::WebProcess::EnsurePrivateBrowsingSession(sessionID), 0);
952     } else if (sessionID != SessionID::defaultSessionID()) {
953         sendToNetworkingProcess(Messages::NetworkProcess::AddWebsiteDataStore(page.websiteDataStore().parameters()));
954         page.process().send(Messages::WebProcess::AddWebsiteDataStore(page.websiteDataStore().parameters()), 0);
955
956         auto databaseParameters = page.websiteDataStore().databaseProcessParameters();
957         if (!databaseParameters.indexedDatabaseDirectory.isEmpty())
958             sendToDatabaseProcessRelaunchingIfNecessary(Messages::DatabaseProcess::InitializeWebsiteDataStore(databaseParameters));
959     }
960 }
961
962 void WebProcessPool::pageRemovedFromProcess(WebPageProxy& page)
963 {
964     auto sessionID = page.sessionID();
965     auto iterator = m_sessionToPagesMap.find(sessionID);
966     ASSERT(iterator != m_sessionToPagesMap.end());
967
968     auto takenPage = iterator->value.take(&page);
969     ASSERT_UNUSED(takenPage, takenPage == &page);
970
971     if (iterator->value.isEmpty()) {
972         m_sessionToPagesMap.remove(iterator);
973
974         if (sessionID == SessionID::defaultSessionID())
975             return;
976
977         // The last user of this non-default SessionID is gone, so clean it up in the child processes.
978         networkProcess()->send(Messages::NetworkProcess::DestroySession(sessionID), 0);
979         page.process().send(Messages::WebProcess::DestroySession(sessionID), 0);
980     }
981 }
982
983 DownloadProxy* WebProcessPool::download(WebPageProxy* initiatingPage, const ResourceRequest& request, const String& suggestedFilename)
984 {
985     DownloadProxy* downloadProxy = createDownloadProxy(request);
986     SessionID sessionID = initiatingPage ? initiatingPage->sessionID() : SessionID::defaultSessionID();
987
988     if (initiatingPage)
989         initiatingPage->handleDownloadRequest(downloadProxy);
990
991     if (networkProcess()) {
992         ResourceRequest updatedRequest(request);
993         // Request's firstPartyForCookies will be used as Original URL of the download request.
994         // We set the value to top level document's URL.
995         if (initiatingPage)
996             updatedRequest.setFirstPartyForCookies(URL(URL(), initiatingPage->pageLoadState().url()));
997         else
998             updatedRequest.setFirstPartyForCookies(URL());
999         networkProcess()->send(Messages::NetworkProcess::DownloadRequest(sessionID, downloadProxy->downloadID(), updatedRequest, suggestedFilename), 0);
1000         return downloadProxy;
1001     }
1002
1003     return downloadProxy;
1004 }
1005
1006 DownloadProxy* WebProcessPool::resumeDownload(const API::Data* resumeData, const String& path)
1007 {
1008     DownloadProxy* downloadProxy = createDownloadProxy(ResourceRequest());
1009
1010     SandboxExtension::Handle sandboxExtensionHandle;
1011     if (!path.isEmpty())
1012         SandboxExtension::createHandle(path, SandboxExtension::ReadWrite, sandboxExtensionHandle);
1013
1014     if (networkProcess()) {
1015         // FIXME: If we started a download in an ephemeral session and that session still exists, we should find a way to use that same session.
1016         networkProcess()->send(Messages::NetworkProcess::ResumeDownload(SessionID::defaultSessionID(), downloadProxy->downloadID(), resumeData->dataReference(), path, sandboxExtensionHandle), 0);
1017         return downloadProxy;
1018     }
1019
1020     return downloadProxy;
1021 }
1022
1023 void WebProcessPool::postMessageToInjectedBundle(const String& messageName, API::Object* messageBody)
1024 {
1025     for (auto& process : m_processes) {
1026         // FIXME: Return early if the message body contains any references to WKPageRefs/WKFrameRefs etc. since they're local to a process.
1027         process->send(Messages::WebProcess::HandleInjectedBundleMessage(messageName, UserData(process->transformObjectsToHandles(messageBody).get())), 0);
1028     }
1029 }
1030
1031 void WebProcessPool::populateVisitedLinks()
1032 {
1033     m_historyClient->populateVisitedLinks(*this);
1034 }
1035
1036 WebProcessPool::Statistics& WebProcessPool::statistics()
1037 {
1038     static Statistics statistics = Statistics();
1039
1040     return statistics;
1041 }
1042
1043 #if ENABLE(NETSCAPE_PLUGIN_API)
1044 void WebProcessPool::setAdditionalPluginsDirectory(const String& directory)
1045 {
1046     Vector<String> directories;
1047     directories.append(directory);
1048
1049     m_pluginInfoStore.setAdditionalPluginsDirectories(directories);
1050 }
1051
1052 void WebProcessPool::refreshPlugins()
1053 {
1054     m_pluginInfoStore.refresh();
1055     sendToAllProcesses(Messages::WebProcess::RefreshPlugins());
1056 }
1057
1058 #endif // ENABLE(NETSCAPE_PLUGIN_API)
1059
1060 pid_t WebProcessPool::networkProcessIdentifier()
1061 {
1062     if (!m_networkProcess)
1063         return 0;
1064
1065     return m_networkProcess->processIdentifier();
1066 }
1067
1068 pid_t WebProcessPool::databaseProcessIdentifier()
1069 {
1070 #if ENABLE(DATABASE_PROCESS)
1071     if (!m_databaseProcess)
1072         return 0;
1073
1074     return m_databaseProcess->processIdentifier();
1075 #else
1076     return 0;
1077 #endif
1078 }
1079
1080 void WebProcessPool::setAlwaysUsesComplexTextCodePath(bool alwaysUseComplexText)
1081 {
1082     m_alwaysUsesComplexTextCodePath = alwaysUseComplexText;
1083     sendToAllProcesses(Messages::WebProcess::SetAlwaysUsesComplexTextCodePath(alwaysUseComplexText));
1084 }
1085
1086 void WebProcessPool::setShouldUseFontSmoothing(bool useFontSmoothing)
1087 {
1088     m_shouldUseFontSmoothing = useFontSmoothing;
1089     sendToAllProcesses(Messages::WebProcess::SetShouldUseFontSmoothing(useFontSmoothing));
1090 }
1091
1092 void WebProcessPool::registerURLSchemeAsEmptyDocument(const String& urlScheme)
1093 {
1094     m_schemesToRegisterAsEmptyDocument.add(urlScheme);
1095     sendToAllProcesses(Messages::WebProcess::RegisterURLSchemeAsEmptyDocument(urlScheme));
1096 }
1097
1098 void WebProcessPool::registerURLSchemeAsSecure(const String& urlScheme)
1099 {
1100     m_schemesToRegisterAsSecure.add(urlScheme);
1101     sendToAllProcesses(Messages::WebProcess::RegisterURLSchemeAsSecure(urlScheme));
1102 }
1103
1104 void WebProcessPool::registerURLSchemeAsBypassingContentSecurityPolicy(const String& urlScheme)
1105 {
1106     m_schemesToRegisterAsBypassingContentSecurityPolicy.add(urlScheme);
1107     sendToAllProcesses(Messages::WebProcess::RegisterURLSchemeAsBypassingContentSecurityPolicy(urlScheme));
1108 }
1109
1110 void WebProcessPool::setDomainRelaxationForbiddenForURLScheme(const String& urlScheme)
1111 {
1112     m_schemesToSetDomainRelaxationForbiddenFor.add(urlScheme);
1113     sendToAllProcesses(Messages::WebProcess::SetDomainRelaxationForbiddenForURLScheme(urlScheme));
1114 }
1115
1116 void WebProcessPool::setCanHandleHTTPSServerTrustEvaluation(bool value)
1117 {
1118     m_canHandleHTTPSServerTrustEvaluation = value;
1119     if (m_networkProcess) {
1120         m_networkProcess->send(Messages::NetworkProcess::SetCanHandleHTTPSServerTrustEvaluation(value), 0);
1121         return;
1122     }
1123 }
1124
1125 void WebProcessPool::registerURLSchemeAsLocal(const String& urlScheme)
1126 {
1127     m_schemesToRegisterAsLocal.add(urlScheme);
1128     sendToAllProcesses(Messages::WebProcess::RegisterURLSchemeAsLocal(urlScheme));
1129 }
1130
1131 void WebProcessPool::registerURLSchemeAsNoAccess(const String& urlScheme)
1132 {
1133     m_schemesToRegisterAsNoAccess.add(urlScheme);
1134     sendToAllProcesses(Messages::WebProcess::RegisterURLSchemeAsNoAccess(urlScheme));
1135 }
1136
1137 void WebProcessPool::registerURLSchemeAsDisplayIsolated(const String& urlScheme)
1138 {
1139     m_schemesToRegisterAsDisplayIsolated.add(urlScheme);
1140     sendToAllProcesses(Messages::WebProcess::RegisterURLSchemeAsDisplayIsolated(urlScheme));
1141 }
1142
1143 void WebProcessPool::registerURLSchemeAsCORSEnabled(const String& urlScheme)
1144 {
1145     m_schemesToRegisterAsCORSEnabled.add(urlScheme);
1146     sendToAllProcesses(Messages::WebProcess::RegisterURLSchemeAsCORSEnabled(urlScheme));
1147 }
1148
1149 void WebProcessPool::registerGlobalURLSchemeAsHavingCustomProtocolHandlers(const String& urlScheme)
1150 {
1151     if (!urlScheme)
1152         return;
1153
1154     globalURLSchemesWithCustomProtocolHandlers().add(urlScheme);
1155     for (auto* processPool : allProcessPools())
1156         processPool->registerSchemeForCustomProtocol(urlScheme);
1157 }
1158
1159 void WebProcessPool::unregisterGlobalURLSchemeAsHavingCustomProtocolHandlers(const String& urlScheme)
1160 {
1161     if (!urlScheme)
1162         return;
1163
1164     globalURLSchemesWithCustomProtocolHandlers().remove(urlScheme);
1165     for (auto* processPool : allProcessPools())
1166         processPool->unregisterSchemeForCustomProtocol(urlScheme);
1167 }
1168
1169 void WebProcessPool::registerURLSchemeAsCachePartitioned(const String& urlScheme)
1170 {
1171     m_schemesToRegisterAsCachePartitioned.add(urlScheme);
1172     sendToAllProcesses(Messages::WebProcess::RegisterURLSchemeAsCachePartitioned(urlScheme));
1173 }
1174
1175 void WebProcessPool::setCacheModel(CacheModel cacheModel)
1176 {
1177     m_configuration->setCacheModel(cacheModel);
1178     sendToAllProcesses(Messages::WebProcess::SetCacheModel(cacheModel));
1179
1180     if (m_networkProcess)
1181         m_networkProcess->send(Messages::NetworkProcess::SetCacheModel(cacheModel), 0);
1182 }
1183
1184 void WebProcessPool::setDefaultRequestTimeoutInterval(double timeoutInterval)
1185 {
1186     sendToAllProcesses(Messages::WebProcess::SetDefaultRequestTimeoutInterval(timeoutInterval));
1187 }
1188
1189 DownloadProxy* WebProcessPool::createDownloadProxy(const ResourceRequest& request)
1190 {
1191     return ensureNetworkProcess().createDownloadProxy(request);
1192 }
1193
1194 void WebProcessPool::addMessageReceiver(IPC::StringReference messageReceiverName, IPC::MessageReceiver& messageReceiver)
1195 {
1196     m_messageReceiverMap.addMessageReceiver(messageReceiverName, messageReceiver);
1197 }
1198
1199 void WebProcessPool::addMessageReceiver(IPC::StringReference messageReceiverName, uint64_t destinationID, IPC::MessageReceiver& messageReceiver)
1200 {
1201     m_messageReceiverMap.addMessageReceiver(messageReceiverName, destinationID, messageReceiver);
1202 }
1203
1204 void WebProcessPool::removeMessageReceiver(IPC::StringReference messageReceiverName)
1205 {
1206     m_messageReceiverMap.removeMessageReceiver(messageReceiverName);
1207 }
1208
1209 void WebProcessPool::removeMessageReceiver(IPC::StringReference messageReceiverName, uint64_t destinationID)
1210 {
1211     m_messageReceiverMap.removeMessageReceiver(messageReceiverName, destinationID);
1212 }
1213
1214 bool WebProcessPool::dispatchMessage(IPC::Connection& connection, IPC::Decoder& decoder)
1215 {
1216     return m_messageReceiverMap.dispatchMessage(connection, decoder);
1217 }
1218
1219 bool WebProcessPool::dispatchSyncMessage(IPC::Connection& connection, IPC::Decoder& decoder, std::unique_ptr<IPC::Encoder>& replyEncoder)
1220 {
1221     return m_messageReceiverMap.dispatchSyncMessage(connection, decoder, replyEncoder);
1222 }
1223
1224 void WebProcessPool::setEnhancedAccessibility(bool flag)
1225 {
1226     sendToAllProcesses(Messages::WebProcess::SetEnhancedAccessibility(flag));
1227 }
1228     
1229 void WebProcessPool::startMemorySampler(const double interval)
1230 {    
1231     // For new WebProcesses we will also want to start the Memory Sampler
1232     m_memorySamplerEnabled = true;
1233     m_memorySamplerInterval = interval;
1234     
1235     // For UIProcess
1236 #if ENABLE(MEMORY_SAMPLER)
1237     WebMemorySampler::singleton()->start(interval);
1238 #endif
1239     
1240     // For WebProcess
1241     SandboxExtension::Handle sampleLogSandboxHandle;    
1242     double now = WTF::currentTime();
1243     String sampleLogFilePath = String::format("WebProcess%llu", static_cast<unsigned long long>(now));
1244     sampleLogFilePath = SandboxExtension::createHandleForTemporaryFile(sampleLogFilePath, SandboxExtension::ReadWrite, sampleLogSandboxHandle);
1245     
1246     sendToAllProcesses(Messages::WebProcess::StartMemorySampler(sampleLogSandboxHandle, sampleLogFilePath, interval));
1247 }
1248
1249 void WebProcessPool::stopMemorySampler()
1250 {    
1251     // For WebProcess
1252     m_memorySamplerEnabled = false;
1253     
1254     // For UIProcess
1255 #if ENABLE(MEMORY_SAMPLER)
1256     WebMemorySampler::singleton()->stop();
1257 #endif
1258
1259     sendToAllProcesses(Messages::WebProcess::StopMemorySampler());
1260 }
1261
1262 void WebProcessPool::setIconDatabasePath(const String& path)
1263 {
1264     m_overrideIconDatabasePath = path;
1265     if (!m_overrideIconDatabasePath.isEmpty()) {
1266         // This implicitly enables the database on UI process side.
1267         m_iconDatabase->setDatabasePath(path);
1268     }
1269 }
1270
1271 String WebProcessPool::iconDatabasePath() const
1272 {
1273     if (!m_overrideIconDatabasePath.isNull())
1274         return m_overrideIconDatabasePath;
1275
1276     return platformDefaultIconDatabasePath();
1277 }
1278
1279 void WebProcessPool::useTestingNetworkSession()
1280 {
1281     ASSERT(m_processes.isEmpty());
1282     ASSERT(!m_networkProcess);
1283
1284     if (m_networkProcess)
1285         return;
1286
1287     if (!m_processes.isEmpty())
1288         return;
1289
1290     m_shouldUseTestingNetworkSession = true;
1291 }
1292
1293 template<typename T, typename U>
1294 void WebProcessPool::sendSyncToNetworkingProcess(T&& message, U&& reply)
1295 {
1296     if (m_networkProcess && m_networkProcess->canSendMessage())
1297         m_networkProcess->sendSync(std::forward<T>(message), std::forward<U>(reply), 0);
1298 }
1299
1300 void WebProcessPool::setAllowsAnySSLCertificateForWebSocket(bool allows)
1301 {
1302     sendSyncToNetworkingProcess(Messages::NetworkProcess::SetAllowsAnySSLCertificateForWebSocket(allows), Messages::NetworkProcess::SetAllowsAnySSLCertificateForWebSocket::Reply());
1303 }
1304
1305 void WebProcessPool::clearCachedCredentials()
1306 {
1307     sendToAllProcesses(Messages::WebProcess::ClearCachedCredentials());
1308     if (m_networkProcess)
1309         m_networkProcess->send(Messages::NetworkProcess::ClearCachedCredentials(), 0);
1310 }
1311
1312 void WebProcessPool::terminateDatabaseProcess()
1313 {
1314 #if ENABLE(DATABASE_PROCESS)
1315     if (!m_databaseProcess)
1316         return;
1317
1318     m_databaseProcess->terminate();
1319     m_databaseProcess = nullptr;
1320 #endif
1321 }
1322
1323 void WebProcessPool::terminateNetworkProcess()
1324 {
1325     if (!m_networkProcess)
1326         return;
1327     
1328     m_networkProcess->terminate();
1329     m_networkProcess = nullptr;
1330 }
1331
1332 void WebProcessPool::syncNetworkProcessCookies()
1333 {
1334     sendSyncToNetworkingProcess(Messages::NetworkProcess::SyncAllCookies(), Messages::NetworkProcess::SyncAllCookies::Reply());
1335 }
1336
1337 void WebProcessPool::allowSpecificHTTPSCertificateForHost(const WebCertificateInfo* certificate, const String& host)
1338 {
1339     ensureNetworkProcess();
1340     m_networkProcess->send(Messages::NetworkProcess::AllowSpecificHTTPSCertificateForHost(certificate->certificateInfo(), host), 0);
1341 }
1342
1343 void WebProcessPool::updateAutomationCapabilities() const
1344 {
1345 #if ENABLE(REMOTE_INSPECTOR)
1346     Inspector::RemoteInspector::singleton().clientCapabilitiesDidChange();
1347 #endif
1348 }
1349
1350 void WebProcessPool::setAutomationSession(RefPtr<WebAutomationSession>&& automationSession)
1351 {
1352     m_automationSession = WTFMove(automationSession);
1353
1354 #if ENABLE(REMOTE_INSPECTOR)
1355     if (m_automationSession) {
1356         m_automationSession->init();
1357         m_automationSession->setProcessPool(this);
1358
1359         sendToAllProcesses(Messages::WebProcess::EnsureAutomationSessionProxy(m_automationSession->sessionIdentifier()));
1360     } else
1361         sendToAllProcesses(Messages::WebProcess::DestroyAutomationSessionProxy());
1362 #endif
1363 }
1364
1365 void WebProcessPool::setHTTPPipeliningEnabled(bool enabled)
1366 {
1367 #if PLATFORM(COCOA)
1368     ResourceRequest::setHTTPPipeliningEnabled(enabled);
1369 #else
1370     UNUSED_PARAM(enabled);
1371 #endif
1372 }
1373
1374 bool WebProcessPool::httpPipeliningEnabled() const
1375 {
1376 #if PLATFORM(COCOA)
1377     return ResourceRequest::httpPipeliningEnabled();
1378 #else
1379     return false;
1380 #endif
1381 }
1382
1383 void WebProcessPool::getStatistics(uint32_t statisticsMask, Function<void (API::Dictionary*, CallbackBase::Error)>&& callbackFunction)
1384 {
1385     if (!statisticsMask) {
1386         callbackFunction(nullptr, CallbackBase::Error::Unknown);
1387         return;
1388     }
1389
1390     RefPtr<StatisticsRequest> request = StatisticsRequest::create(DictionaryCallback::create(WTFMove(callbackFunction)));
1391
1392     if (statisticsMask & StatisticsRequestTypeWebContent)
1393         requestWebContentStatistics(request.get());
1394     
1395     if (statisticsMask & StatisticsRequestTypeNetworking)
1396         requestNetworkingStatistics(request.get());
1397 }
1398
1399 void WebProcessPool::requestWebContentStatistics(StatisticsRequest* request)
1400 {
1401     // FIXME (Multi-WebProcess) <rdar://problem/13200059>: Make getting statistics from multiple WebProcesses work.
1402 }
1403
1404 void WebProcessPool::requestNetworkingStatistics(StatisticsRequest* request)
1405 {
1406     if (!m_networkProcess) {
1407         LOG_ERROR("Attempt to get NetworkProcess statistics but the NetworkProcess is unavailable");
1408         return;
1409     }
1410
1411     uint64_t requestID = request->addOutstandingRequest();
1412     m_statisticsRequests.set(requestID, request);
1413     m_networkProcess->send(Messages::NetworkProcess::GetNetworkProcessStatistics(requestID), 0);
1414 }
1415
1416 static WebProcessProxy* webProcessProxyFromConnection(IPC::Connection& connection, const Vector<RefPtr<WebProcessProxy>>& processes)
1417 {
1418     for (auto& process : processes) {
1419         if (process->hasConnection(connection))
1420             return process.get();
1421     }
1422
1423     ASSERT_NOT_REACHED();
1424     return nullptr;
1425 }
1426
1427 void WebProcessPool::handleMessage(IPC::Connection& connection, const String& messageName, const WebKit::UserData& messageBody)
1428 {
1429     auto* webProcessProxy = webProcessProxyFromConnection(connection, m_processes);
1430     if (!webProcessProxy)
1431         return;
1432     m_injectedBundleClient.didReceiveMessageFromInjectedBundle(this, messageName, webProcessProxy->transformHandlesToObjects(messageBody.object()).get());
1433 }
1434
1435 void WebProcessPool::handleSynchronousMessage(IPC::Connection& connection, const String& messageName, const UserData& messageBody, UserData& returnUserData)
1436 {
1437     auto* webProcessProxy = webProcessProxyFromConnection(connection, m_processes);
1438     if (!webProcessProxy)
1439         return;
1440
1441     RefPtr<API::Object> returnData;
1442     m_injectedBundleClient.didReceiveSynchronousMessageFromInjectedBundle(this, messageName, webProcessProxy->transformHandlesToObjects(messageBody.object()).get(), returnData);
1443     returnUserData = UserData(webProcessProxy->transformObjectsToHandles(returnData.get()));
1444 }
1445
1446 void WebProcessPool::didGetStatistics(const StatisticsData& statisticsData, uint64_t requestID)
1447 {
1448     RefPtr<StatisticsRequest> request = m_statisticsRequests.take(requestID);
1449     if (!request) {
1450         LOG_ERROR("Cannot report networking statistics.");
1451         return;
1452     }
1453
1454     request->completedRequest(requestID, statisticsData);
1455 }
1456
1457 #if ENABLE(GAMEPAD)
1458
1459 void WebProcessPool::startedUsingGamepads(IPC::Connection& connection)
1460 {
1461     auto* proxy = webProcessProxyFromConnection(connection, m_processes);
1462     if (!proxy)
1463         return;
1464
1465     bool wereAnyProcessesUsingGamepads = !m_processesUsingGamepads.isEmpty();
1466
1467     ASSERT(!m_processesUsingGamepads.contains(proxy));
1468     m_processesUsingGamepads.add(proxy);
1469
1470     if (!wereAnyProcessesUsingGamepads)
1471         UIGamepadProvider::singleton().processPoolStartedUsingGamepads(*this);
1472
1473     proxy->send(Messages::WebProcess::SetInitialGamepads(UIGamepadProvider::singleton().snapshotGamepads()), 0);
1474 }
1475
1476 void WebProcessPool::stoppedUsingGamepads(IPC::Connection& connection)
1477 {
1478     auto* proxy = webProcessProxyFromConnection(connection, m_processes);
1479     if (!proxy)
1480         return;
1481
1482     ASSERT(m_processesUsingGamepads.contains(proxy));
1483     processStoppedUsingGamepads(*proxy);
1484 }
1485
1486 void WebProcessPool::processStoppedUsingGamepads(WebProcessProxy& process)
1487 {
1488     bool wereAnyProcessesUsingGamepads = !m_processesUsingGamepads.isEmpty();
1489
1490     ASSERT(m_processesUsingGamepads.contains(&process));
1491     m_processesUsingGamepads.remove(&process);
1492
1493     if (wereAnyProcessesUsingGamepads && m_processesUsingGamepads.isEmpty())
1494         UIGamepadProvider::singleton().processPoolStoppedUsingGamepads(*this);
1495 }
1496
1497 void WebProcessPool::gamepadConnected(const UIGamepad& gamepad)
1498 {
1499     for (auto& process : m_processesUsingGamepads)
1500         process->send(Messages::WebProcess::GamepadConnected(gamepad.fullGamepadData()), 0);
1501 }
1502
1503 void WebProcessPool::gamepadDisconnected(const UIGamepad& gamepad)
1504 {
1505     for (auto& process : m_processesUsingGamepads)
1506         process->send(Messages::WebProcess::GamepadDisconnected(gamepad.index()), 0);
1507 }
1508
1509 void WebProcessPool::setInitialConnectedGamepads(const Vector<std::unique_ptr<UIGamepad>>& gamepads)
1510 {
1511     Vector<GamepadData> gamepadDatas;
1512     gamepadDatas.resize(gamepads.size());
1513     for (size_t i = 0; i < gamepads.size(); ++i) {
1514         if (!gamepads[i])
1515             continue;
1516         gamepadDatas[i] = gamepads[i]->fullGamepadData();
1517     }
1518
1519     for (auto& process : m_processesUsingGamepads)
1520         process->send(Messages::WebProcess::SetInitialGamepads(gamepadDatas), 0);
1521 }
1522
1523 #endif // ENABLE(GAMEPAD)
1524
1525 void WebProcessPool::setJavaScriptConfigurationFileEnabled(bool flag)
1526 {
1527     m_javaScriptConfigurationFileEnabled = flag;
1528 }
1529
1530 void WebProcessPool::garbageCollectJavaScriptObjects()
1531 {
1532     sendToAllProcesses(Messages::WebProcess::GarbageCollectJavaScriptObjects());
1533 }
1534
1535 void WebProcessPool::setJavaScriptGarbageCollectorTimerEnabled(bool flag)
1536 {
1537     sendToAllProcesses(Messages::WebProcess::SetJavaScriptGarbageCollectorTimerEnabled(flag));
1538 }
1539
1540 void WebProcessPool::addPlugInAutoStartOriginHash(const String& pageOrigin, unsigned plugInOriginHash, SessionID sessionID)
1541 {
1542     m_plugInAutoStartProvider.addAutoStartOriginHash(pageOrigin, plugInOriginHash, sessionID);
1543 }
1544
1545 void WebProcessPool::plugInDidReceiveUserInteraction(unsigned plugInOriginHash, SessionID sessionID)
1546 {
1547     m_plugInAutoStartProvider.didReceiveUserInteraction(plugInOriginHash, sessionID);
1548 }
1549
1550 Ref<API::Dictionary> WebProcessPool::plugInAutoStartOriginHashes() const
1551 {
1552     return m_plugInAutoStartProvider.autoStartOriginsTableCopy();
1553 }
1554
1555 void WebProcessPool::setPlugInAutoStartOriginHashes(API::Dictionary& dictionary)
1556 {
1557     m_plugInAutoStartProvider.setAutoStartOriginsTable(dictionary);
1558 }
1559
1560 void WebProcessPool::setPlugInAutoStartOrigins(API::Array& array)
1561 {
1562     m_plugInAutoStartProvider.setAutoStartOriginsArray(array);
1563 }
1564
1565 void WebProcessPool::setPlugInAutoStartOriginsFilteringOutEntriesAddedAfterTime(API::Dictionary& dictionary, double time)
1566 {
1567     m_plugInAutoStartProvider.setAutoStartOriginsFilteringOutEntriesAddedAfterTime(dictionary, time);
1568 }
1569
1570 void WebProcessPool::registerSchemeForCustomProtocol(const String& scheme)
1571 {
1572     if (!globalURLSchemesWithCustomProtocolHandlers().contains(scheme))
1573         m_urlSchemesRegisteredForCustomProtocols.add(scheme);
1574     sendToNetworkingProcess(Messages::LegacyCustomProtocolManager::RegisterScheme(scheme));
1575 }
1576
1577 void WebProcessPool::unregisterSchemeForCustomProtocol(const String& scheme)
1578 {
1579     m_urlSchemesRegisteredForCustomProtocols.remove(scheme);
1580     sendToNetworkingProcess(Messages::LegacyCustomProtocolManager::UnregisterScheme(scheme));
1581 }
1582
1583 #if ENABLE(NETSCAPE_PLUGIN_API)
1584 void WebProcessPool::setPluginLoadClientPolicy(WebCore::PluginLoadClientPolicy policy, const String& host, const String& bundleIdentifier, const String& versionString)
1585 {
1586     HashMap<String, HashMap<String, uint8_t>> policiesByIdentifier;
1587     if (m_pluginLoadClientPolicies.contains(host))
1588         policiesByIdentifier = m_pluginLoadClientPolicies.get(host);
1589
1590     HashMap<String, uint8_t> versionsToPolicies;
1591     if (policiesByIdentifier.contains(bundleIdentifier))
1592         versionsToPolicies = policiesByIdentifier.get(bundleIdentifier);
1593
1594     versionsToPolicies.set(versionString, policy);
1595     policiesByIdentifier.set(bundleIdentifier, versionsToPolicies);
1596     m_pluginLoadClientPolicies.set(host, policiesByIdentifier);
1597
1598     sendToAllProcesses(Messages::WebProcess::SetPluginLoadClientPolicy(policy, host, bundleIdentifier, versionString));
1599 }
1600
1601 void WebProcessPool::clearPluginClientPolicies()
1602 {
1603     m_pluginLoadClientPolicies.clear();
1604     sendToAllProcesses(Messages::WebProcess::ClearPluginClientPolicies());
1605 }
1606 #endif
1607
1608 void WebProcessPool::setMemoryCacheDisabled(bool disabled)
1609 {
1610     m_memoryCacheDisabled = disabled;
1611     sendToAllProcesses(Messages::WebProcess::SetMemoryCacheDisabled(disabled));
1612 }
1613
1614 void WebProcessPool::setFontWhitelist(API::Array* array)
1615 {
1616     m_fontWhitelist.clear();
1617     if (array) {
1618         for (size_t i = 0; i < array->size(); ++i) {
1619             if (API::String* font = array->at<API::String>(i))
1620                 m_fontWhitelist.append(font->string());
1621         }
1622     }
1623 }
1624
1625 void WebProcessPool::updateHiddenPageThrottlingAutoIncreaseLimit()
1626 {
1627     // We're estimating an upper bound for a set of background timer fires for a page to be 200ms
1628     // (including all timer fires, all paging-in, and any resulting GC). To ensure this does not
1629     // result in more than 1% CPU load allow for one timer fire per 100x this duration.
1630     static int maximumTimerThrottlePerPageInMS = 200 * 100;
1631
1632     int limitInMilliseconds = maximumTimerThrottlePerPageInMS * m_hiddenPageThrottlingAutoIncreasesCounter.value();
1633     sendToAllProcesses(Messages::WebProcess::SetHiddenPageDOMTimerThrottlingIncreaseLimit(limitInMilliseconds));
1634 }
1635
1636 void WebProcessPool::reportWebContentCPUTime(Seconds cpuTime, uint64_t activityState)
1637 {
1638 #if PLATFORM(MAC)
1639     if (m_perActivityStateCPUUsageSampler)
1640         m_perActivityStateCPUUsageSampler->reportWebContentCPUTime(cpuTime, static_cast<WebCore::ActivityStateForCPUSampling>(activityState));
1641 #else
1642     UNUSED_PARAM(cpuTime);
1643     UNUSED_PARAM(activityState);
1644 #endif
1645 }
1646
1647 } // namespace WebKit