#include "config.h"
#include "GPUProcess.h"
#if ENABLE(GPU_PROCESS)
#include "ArgumentCoders.h"
#include "Attachment.h"
#include "AuxiliaryProcessMessages.h"
#include "DataReference.h"
#include "GPUConnectionToWebProcess.h"
#include "GPUProcessCreationParameters.h"
#include "GPUProcessSessionParameters.h"
#include "Logging.h"
#include "SandboxExtension.h"
#include "WebPageProxyMessages.h"
#include "WebProcessPoolMessages.h"
#include <WebCore/DeprecatedGlobalSettings.h>
#include <WebCore/LogInitialization.h>
#include <WebCore/NowPlayingManager.h>
#include <WebCore/RuntimeApplicationChecks.h>
#include <wtf/Algorithms.h>
#include <wtf/CallbackAggregator.h>
#include <wtf/MemoryPressureHandler.h>
#include <wtf/OptionSet.h>
#include <wtf/ProcessPrivilege.h>
#include <wtf/RunLoop.h>
#include <wtf/UniqueRef.h>
#include <wtf/text/AtomString.h>
#if USE(AUDIO_SESSION)
#include "RemoteAudioSessionProxyManager.h"
#endif
#if ENABLE(MEDIA_STREAM)
#include <WebCore/MockRealtimeMediaSourceCenter.h>
#endif
#if PLATFORM(COCOA)
#include <WebCore/VP9UtilitiesCocoa.h>
#endif
namespace WebKit {
using namespace WebCore;
GPUProcess::GPUProcess(AuxiliaryProcessInitializationParameters&& parameters)
{
initialize(WTFMove(parameters));
}
GPUProcess::~GPUProcess()
{
}
void GPUProcess::createGPUConnectionToWebProcess(ProcessIdentifier identifier, PAL::SessionID sessionID, CompletionHandler<void(Optional<IPC::Attachment>&&)>&& completionHandler)
{
auto ipcConnection = createIPCConnectionPair();
if (!ipcConnection) {
completionHandler({ });
return;
}
auto newConnection = GPUConnectionToWebProcess::create(*this, identifier, ipcConnection->first, sessionID);
#if ENABLE(MEDIA_STREAM)
auto access = m_mediaCaptureAccessMap.take(identifier);
newConnection->updateCaptureAccess(access.allowAudioCapture, access.allowVideoCapture, access.allowDisplayCapture);
#endif
ASSERT(!m_webProcessConnections.contains(identifier));
m_webProcessConnections.add(identifier, WTFMove(newConnection));
completionHandler(WTFMove(ipcConnection->second));
}
void GPUProcess::removeGPUConnectionToWebProcess(GPUConnectionToWebProcess& connection)
{
ASSERT(m_webProcessConnections.contains(connection.webProcessIdentifier()));
m_webProcessConnections.remove(connection.webProcessIdentifier());
}
void GPUProcess::connectionToWebProcessClosed(IPC::Connection& connection)
{
}
bool GPUProcess::shouldTerminate()
{
return m_webProcessConnections.isEmpty();
}
void GPUProcess::lowMemoryHandler(Critical critical)
{
WTF::releaseFastMallocFreeMemory();
}
void GPUProcess::initializeGPUProcess(GPUProcessCreationParameters&& parameters)
{
WTF::Thread::setCurrentThreadIsUserInitiated();
AtomString::init();
auto& memoryPressureHandler = MemoryPressureHandler::singleton();
memoryPressureHandler.setLowMemoryHandler([this] (Critical critical, Synchronous) {
lowMemoryHandler(critical);
});
memoryPressureHandler.install();
#if PLATFORM(IOS_FAMILY) || ENABLE(ROUTING_ARBITRATION)
DeprecatedGlobalSettings::setShouldManageAudioSessionCategory(true);
#endif
#if ENABLE(MEDIA_STREAM)
setMockCaptureDevicesEnabled(parameters.useMockCaptureDevices);
SandboxExtension::consumePermanently(parameters.cameraSandboxExtensionHandle);
SandboxExtension::consumePermanently(parameters.microphoneSandboxExtensionHandle);
#if PLATFORM(IOS)
SandboxExtension::consumePermanently(parameters.tccSandboxExtensionHandle);
#endif
#endif
#if HAVE(VISIBILITY_PROPAGATION_VIEW)
m_contextForVisibilityPropagation = LayerHostingContext::createForExternalHostingProcess({
m_canShowWhileLocked
});
send(Messages::GPUProcessProxy::DidCreateContextForVisibilityPropagation(m_contextForVisibilityPropagation->contextID()));
#endif
WTF::Thread::setCurrentThreadIsUserInteractive(0);
WebCore::setPresentingApplicationPID(parameters.parentPID);
}
void GPUProcess::prepareToSuspend(bool isSuspensionImminent, CompletionHandler<void()>&& completionHandler)
{
RELEASE_LOG(ProcessSuspension, "%p - GPUProcess::prepareToSuspend(), isSuspensionImminent: %d", this, isSuspensionImminent);
lowMemoryHandler(Critical::Yes);
completionHandler();
}
void GPUProcess::processDidResume()
{
RELEASE_LOG(ProcessSuspension, "%p - GPUProcess::processDidResume()", this);
resume();
}
void GPUProcess::resume()
{
}
void GPUProcess::processDidTransitionToForeground()
{
}
void GPUProcess::processDidTransitionToBackground()
{
}
GPUConnectionToWebProcess* GPUProcess::webProcessConnection(ProcessIdentifier identifier) const
{
return m_webProcessConnections.get(identifier);
}
#if ENABLE(MEDIA_STREAM)
void GPUProcess::setMockCaptureDevicesEnabled(bool isEnabled)
{
MockRealtimeMediaSourceCenter::setMockRealtimeMediaSourceCenterEnabled(isEnabled);
}
void GPUProcess::setOrientationForMediaCapture(uint64_t orientation)
{
for (auto& connection : m_webProcessConnections.values())
connection->setOrientationForMediaCapture(orientation);
}
void GPUProcess::updateCaptureAccess(bool allowAudioCapture, bool allowVideoCapture, bool allowDisplayCapture, ProcessIdentifier processID, CompletionHandler<void()>&& completionHandler)
{
if (auto* connection = webProcessConnection(processID)) {
connection->updateCaptureAccess(allowAudioCapture, allowVideoCapture, allowDisplayCapture);
return completionHandler();
}
auto& access = m_mediaCaptureAccessMap.add(processID, MediaCaptureAccess { allowAudioCapture, allowVideoCapture, allowDisplayCapture }).iterator->value;
access.allowAudioCapture |= allowAudioCapture;
access.allowVideoCapture |= allowVideoCapture;
access.allowDisplayCapture |= allowDisplayCapture;
completionHandler();
}
#endif
void GPUProcess::addSession(PAL::SessionID sessionID, GPUProcessSessionParameters&& parameters)
{
ASSERT(!m_sessions.contains(sessionID));
SandboxExtension::consumePermanently(parameters.mediaCacheDirectorySandboxExtensionHandle);
#if ENABLE(LEGACY_ENCRYPTED_MEDIA)
SandboxExtension::consumePermanently(parameters.mediaKeysStorageDirectorySandboxExtensionHandle);
#endif
m_sessions.add(sessionID, GPUSession {
WTFMove(parameters.mediaCacheDirectory)
#if ENABLE(LEGACY_ENCRYPTED_MEDIA)
, WTFMove(parameters.mediaKeysStorageDirectory)
#endif
});
}
void GPUProcess::removeSession(PAL::SessionID sessionID)
{
ASSERT(m_sessions.contains(sessionID));
m_sessions.remove(sessionID);
}
const String& GPUProcess::mediaCacheDirectory(PAL::SessionID sessionID) const
{
ASSERT(m_sessions.contains(sessionID));
return m_sessions.find(sessionID)->value.mediaCacheDirectory;
}
#if ENABLE(LEGACY_ENCRYPTED_MEDIA)
const String& GPUProcess::mediaKeysStorageDirectory(PAL::SessionID sessionID) const
{
ASSERT(m_sessions.contains(sessionID));
return m_sessions.find(sessionID)->value.mediaKeysStorageDirectory;
}
#endif
NowPlayingManager& GPUProcess::nowPlayingManager()
{
if (!m_nowPlayingManager)
m_nowPlayingManager = makeUnique<NowPlayingManager>();
return *m_nowPlayingManager;
}
#if ENABLE(GPU_PROCESS) && USE(AUDIO_SESSION)
RemoteAudioSessionProxyManager& GPUProcess::audioSessionManager() const
{
if (!m_audioSessionManager)
m_audioSessionManager = WTF::makeUnique<RemoteAudioSessionProxyManager>();
return *m_audioSessionManager;
}
#endif
#if ENABLE(MEDIA_STREAM) && PLATFORM(COCOA)
WorkQueue& GPUProcess::audioMediaStreamTrackRendererQueue()
{
if (!m_audioMediaStreamTrackRendererQueue)
m_audioMediaStreamTrackRendererQueue = WorkQueue::create("RemoteAudioMediaStreamTrackRenderer", WorkQueue::Type::Serial, WorkQueue::QOS::UserInteractive);
return *m_audioMediaStreamTrackRendererQueue;
}
WorkQueue& GPUProcess::videoMediaStreamTrackRendererQueue()
{
if (!m_videoMediaStreamTrackRendererQueue)
m_videoMediaStreamTrackRendererQueue = WorkQueue::create("RemoteVideoMediaStreamTrackRenderer", WorkQueue::Type::Serial, WorkQueue::QOS::UserInitiated);
return *m_videoMediaStreamTrackRendererQueue;
}
#endif
#if USE(LIBWEBRTC) && PLATFORM(COCOA)
WorkQueue& GPUProcess::libWebRTCCodecsQueue()
{
if (!m_libWebRTCCodecsQueue)
m_libWebRTCCodecsQueue = WorkQueue::create("LibWebRTCCodecsQueue", WorkQueue::Type::Serial, WorkQueue::QOS::UserInitiated);
return *m_libWebRTCCodecsQueue;
}
#endif
#if ENABLE(VP9)
void GPUProcess::enableVP9Decoders(bool shouldEnableVP8Decoder, bool shouldEnableVP9Decoder, bool shouldEnableVP9SWDecoder)
{
if (shouldEnableVP9Decoder && !m_enableVP9Decoder) {
m_enableVP9Decoder = true;
#if PLATFORM(COCOA)
WebCore::registerSupplementalVP9Decoder();
#endif
}
if (shouldEnableVP8Decoder && !m_enableVP8Decoder) {
m_enableVP8Decoder = true;
#if PLATFORM(COCOA)
WebCore::registerWebKitVP8Decoder();
#endif
}
if (shouldEnableVP9SWDecoder && !m_enableVP9SWDecoder) {
m_enableVP9SWDecoder = true;
#if PLATFORM(COCOA)
WebCore::registerWebKitVP9Decoder();
#endif
}
}
#endif
}
#endif // ENABLE(GPU_PROCESS)