1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
|
/*
* Copyright (C) 2019-2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "GPUProcessProxy.h"
#if ENABLE(GPU_PROCESS)
#include "DrawingAreaProxy.h"
#include "GPUProcessConnectionParameters.h"
#include "GPUProcessCreationParameters.h"
#include "GPUProcessMessages.h"
#include "GPUProcessPreferences.h"
#include "GPUProcessProxyMessages.h"
#include "GPUProcessSessionParameters.h"
#include "Logging.h"
#include "OverrideLanguages.h"
#include "ProvisionalPageProxy.h"
#include "WebPageGroup.h"
#include "WebPageMessages.h"
#include "WebPageProxy.h"
#include "WebPreferences.h"
#include "WebProcessMessages.h"
#include "WebProcessPool.h"
#include "WebProcessProxy.h"
#include "WebProcessProxyMessages.h"
#include <WebCore/DisplayCapturePromptType.h>
#include <WebCore/LogInitialization.h>
#include <WebCore/MockRealtimeMediaSourceCenter.h>
#include <WebCore/RuntimeApplicationChecks.h>
#include <WebCore/ScreenProperties.h>
#include <wtf/CompletionHandler.h>
#include <wtf/LogInitialization.h>
#include <wtf/MachSendRight.h>
#include <wtf/TranslatedProcess.h>
#if PLATFORM(IOS_FAMILY)
#include <WebCore/AGXCompilerService.h>
#include <wtf/spi/darwin/XPCSPI.h>
#endif
#if USE(SANDBOX_EXTENSIONS_FOR_CACHE_AND_TEMP_DIRECTORY_ACCESS)
#include "SandboxUtilities.h"
#include <wtf/FileSystem.h>
#endif
#if PLATFORM(COCOA)
#include <wtf/BlockPtr.h>
#endif
#if USE(GBM)
#include <WebCore/PlatformDisplay.h>
#endif
#define MESSAGE_CHECK(assertion) MESSAGE_CHECK_BASE(assertion, this->connection())
namespace WebKit {
using namespace WebCore;
#if ENABLE(MEDIA_STREAM) && HAVE(AUDIT_TOKEN)
static bool shouldCreateAppleCameraServiceSandboxExtension()
{
#if !PLATFORM(MAC) && !PLATFORM(MACCATALYST)
return false;
#elif CPU(ARM64)
return true;
#else
return WTF::isX86BinaryRunningOnARM();
#endif
}
#endif
static WeakPtr<GPUProcessProxy>& singleton()
{
static NeverDestroyed<WeakPtr<GPUProcessProxy>> singleton;
return singleton;
}
Ref<GPUProcessProxy> GPUProcessProxy::getOrCreate()
{
ASSERT(RunLoop::isMain());
if (auto& existingGPUProcess = singleton()) {
ASSERT(existingGPUProcess->state() != State::Terminated);
return *existingGPUProcess;
}
auto gpuProcess = adoptRef(*new GPUProcessProxy);
singleton() = gpuProcess;
return gpuProcess;
}
GPUProcessProxy* GPUProcessProxy::singletonIfCreated()
{
return singleton().get();
}
#if USE(SANDBOX_EXTENSIONS_FOR_CACHE_AND_TEMP_DIRECTORY_ACCESS)
static String gpuProcessCachesDirectory()
{
String path = WebsiteDataStore::cacheDirectoryInContainerOrHomeDirectory("/Library/Caches/com.apple.WebKit.GPU/"_s);
FileSystem::makeAllDirectories(path);
return path;
}
#endif
GPUProcessProxy::GPUProcessProxy()
: AuxiliaryProcessProxy()
, m_throttler(*this, WebProcessPool::anyProcessPoolNeedsUIBackgroundAssertion())
#if ENABLE(MEDIA_STREAM)
, m_useMockCaptureDevices(MockRealtimeMediaSourceCenter::mockRealtimeMediaSourceCenterEnabled())
#endif
{
connect();
GPUProcessCreationParameters parameters;
parameters.auxiliaryProcessParameters = auxiliaryProcessParameters();
parameters.overrideLanguages = overrideLanguages();
#if ENABLE(MEDIA_STREAM)
parameters.useMockCaptureDevices = m_useMockCaptureDevices;
#if PLATFORM(MAC)
// FIXME: Remove this and related parameter when <rdar://problem/29448368> is fixed.
if (MacApplication::isSafari()) {
if (auto handle = SandboxExtension::createHandleForGenericExtension("com.apple.webkit.microphone"_s))
parameters.microphoneSandboxExtensionHandle = WTFMove(*handle);
m_hasSentMicrophoneSandboxExtension = true;
}
#endif
#endif // ENABLE(MEDIA_STREAM)
parameters.parentPID = getCurrentProcessID();
#if USE(SANDBOX_EXTENSIONS_FOR_CACHE_AND_TEMP_DIRECTORY_ACCESS)
auto containerCachesDirectory = resolveAndCreateReadWriteDirectoryForSandboxExtension(gpuProcessCachesDirectory());
auto containerTemporaryDirectory = WebsiteDataStore::defaultResolvedContainerTemporaryDirectory();
if (!containerCachesDirectory.isEmpty()) {
if (auto handle = SandboxExtension::createHandleWithoutResolvingPath(containerCachesDirectory, SandboxExtension::Type::ReadWrite))
parameters.containerCachesDirectoryExtensionHandle = WTFMove(*handle);
}
if (!containerTemporaryDirectory.isEmpty()) {
if (auto handle = SandboxExtension::createHandleWithoutResolvingPath(containerTemporaryDirectory, SandboxExtension::Type::ReadWrite))
parameters.containerTemporaryDirectoryExtensionHandle = WTFMove(*handle);
}
#endif
#if PLATFORM(IOS_FAMILY) && HAVE(AGX_COMPILER_SERVICE)
if (WebCore::deviceHasAGXCompilerService()) {
parameters.compilerServiceExtensionHandles = SandboxExtension::createHandlesForMachLookup(WebCore::agxCompilerServices(), std::nullopt);
parameters.dynamicIOKitExtensionHandles = SandboxExtension::createHandlesForIOKitClassExtensions(WebCore::agxCompilerClasses(), std::nullopt);
}
#endif
#if USE(GBM)
parameters.renderDeviceFile = WebCore::PlatformDisplay::sharedDisplay().drmRenderNodeFile();
#endif
platformInitializeGPUProcessParameters(parameters);
// Initialize the GPU process.
send(Messages::GPUProcess::InitializeGPUProcess(parameters), 0);
#if PLATFORM(COCOA)
m_hasSentGPUToolsSandboxExtensions = !parameters.gpuToolsExtensionHandles.isEmpty();
#endif
#if HAVE(AUDIO_COMPONENT_SERVER_REGISTRATIONS) && ENABLE(AUDIO_COMPONENT_SERVER_REGISTRATIONS_IN_GPU_PROCESS)
auto registrations = fetchAudioComponentServerRegistrations();
RELEASE_ASSERT(registrations);
send(Messages::GPUProcess::ConsumeAudioComponentRegistrations(IPC::SharedBufferReference(WTFMove(registrations))), 0);
#endif
updateProcessAssertion();
}
GPUProcessProxy::~GPUProcessProxy() = default;
#if ENABLE(MEDIA_STREAM)
void GPUProcessProxy::setUseMockCaptureDevices(bool value)
{
if (value == m_useMockCaptureDevices)
return;
m_useMockCaptureDevices = value;
send(Messages::GPUProcess::SetMockCaptureDevicesEnabled { m_useMockCaptureDevices }, 0);
}
void GPUProcessProxy::setUseSCContentSharingPicker(bool use)
{
#if HAVE(SC_CONTENT_SHARING_PICKER)
if (use == m_useSCContentSharingPicker)
return;
m_useSCContentSharingPicker = use;
send(Messages::GPUProcess::SetUseSCContentSharingPicker { m_useSCContentSharingPicker }, 0);
#else
UNUSED_PARAM(use);
#endif
}
void GPUProcessProxy::setOrientationForMediaCapture(WebCore::IntDegrees orientation)
{
if (m_orientation == orientation)
return;
m_orientation = orientation;
send(Messages::GPUProcess::SetOrientationForMediaCapture { orientation }, 0);
}
#if HAVE(APPLE_CAMERA_USER_CLIENT)
static const ASCIILiteral appleCameraUserClientPath { "com.apple.aneuserd"_s };
static const ASCIILiteral appleCameraUserClientIOKitClientClass { "H11ANEInDirectPathClient"_s };
static const ASCIILiteral appleCameraUserClientIOKitServiceClass { "H11ANEIn"_s };
#endif
static inline bool addCameraSandboxExtensions(Vector<SandboxExtension::Handle>& extensions)
{
auto sandboxExtensionHandle = SandboxExtension::createHandleForGenericExtension("com.apple.webkit.camera"_s);
if (!sandboxExtensionHandle) {
RELEASE_LOG_ERROR(WebRTC, "Unable to create com.apple.webkit.camera sandbox extension");
return false;
}
#if HAVE(AUDIT_TOKEN)
if (shouldCreateAppleCameraServiceSandboxExtension()) {
auto appleCameraServicePathSandboxExtensionHandle = SandboxExtension::createHandleForMachLookup("com.apple.applecamerad"_s, std::nullopt);
if (!appleCameraServicePathSandboxExtensionHandle) {
RELEASE_LOG_ERROR(WebRTC, "Unable to create com.apple.applecamerad sandbox extension");
return false;
}
extensions.append(WTFMove(*appleCameraServicePathSandboxExtensionHandle));
#if HAVE(ADDITIONAL_APPLE_CAMERA_SERVICE)
auto additionalAppleCameraServicePathSandboxExtensionHandle = SandboxExtension::createHandleForMachLookup("com.apple.appleh13camerad"_s, std::nullopt);
if (!additionalAppleCameraServicePathSandboxExtensionHandle) {
RELEASE_LOG_ERROR(WebRTC, "Unable to create com.apple.appleh13camerad sandbox extension");
return false;
}
extensions.append(WTFMove(*additionalAppleCameraServicePathSandboxExtensionHandle));
#endif
#if HAVE(APPLE_CAMERA_USER_CLIENT)
// Needed for rdar://108282689:
auto appleCameraUserClientExtensionHandle = SandboxExtension::createHandleForMachLookup(appleCameraUserClientPath, std::nullopt);
if (!appleCameraUserClientExtensionHandle) {
RELEASE_LOG_ERROR(WebRTC, "Unable to create %s sandbox extension", appleCameraUserClientPath.characters8());
return false;
}
extensions.append(WTFMove(*appleCameraUserClientExtensionHandle));
auto appleCameraUserClientIOKitClientClassExtensionHandle = SandboxExtension::createHandleForIOKitClassExtension(appleCameraUserClientIOKitClientClass, std::nullopt);
if (!appleCameraUserClientIOKitClientClassExtensionHandle) {
RELEASE_LOG_ERROR(WebRTC, "Unable to create %s sandbox extension", appleCameraUserClientIOKitClientClass.characters8());
return false;
}
extensions.append(WTFMove(*appleCameraUserClientIOKitClientClassExtensionHandle));
auto appleCameraUserClientIOKitServiceClassExtensionHandle = SandboxExtension::createHandleForIOKitClassExtension(appleCameraUserClientIOKitServiceClass, std::nullopt);
if (!appleCameraUserClientIOKitServiceClassExtensionHandle) {
RELEASE_LOG_ERROR(WebRTC, "Unable to create %s sandbox extension", appleCameraUserClientIOKitServiceClass.characters8());
return false;
}
extensions.append(WTFMove(*appleCameraUserClientIOKitServiceClassExtensionHandle));
#endif
}
#endif // HAVE(AUDIT_TOKEN)
extensions.append(WTFMove(*sandboxExtensionHandle));
return true;
}
static inline bool addMicrophoneSandboxExtension(Vector<SandboxExtension::Handle>& extensions)
{
auto sandboxExtensionHandle = SandboxExtension::createHandleForGenericExtension("com.apple.webkit.microphone"_s);
if (!sandboxExtensionHandle) {
RELEASE_LOG_ERROR(WebRTC, "Unable to create com.apple.webkit.microphone sandbox extension");
return false;
}
extensions.append(WTFMove(*sandboxExtensionHandle));
return true;
}
#if HAVE(SCREEN_CAPTURE_KIT)
static inline bool addDisplayCaptureSandboxExtension(std::optional<audit_token_t> auditToken, Vector<SandboxExtension::Handle>& extensions)
{
if (!auditToken) {
RELEASE_LOG_ERROR(WebRTC, "NULL audit token");
return false;
}
auto handle = SandboxExtension::createHandleForMachLookup("com.apple.replaykit.sharingsession.notification"_s, *auditToken);
if (!handle)
return false;
extensions.append(WTFMove(*handle));
handle = SandboxExtension::createHandleForMachLookup("com.apple.replaykit.sharingsession"_s, *auditToken);
if (!handle)
return false;
extensions.append(WTFMove(*handle));
handle = SandboxExtension::createHandleForMachLookup("com.apple.tccd.system"_s, *auditToken);
if (!handle)
return false;
extensions.append(WTFMove(*handle));
handle = SandboxExtension::createHandleForMachLookup("com.apple.replayd"_s, *auditToken);
if (!handle)
return false;
extensions.append(WTFMove(*handle));
return true;
}
#endif
#if PLATFORM(IOS) || PLATFORM(VISION)
static inline bool addTCCDSandboxExtension(Vector<SandboxExtension::Handle>& extensions)
{
auto handle = SandboxExtension::createHandleForMachLookup("com.apple.tccd"_s, std::nullopt);
if (!handle) {
RELEASE_LOG_ERROR(WebRTC, "Unable to create com.apple.tccd sandbox extension");
return false;
}
extensions.append(WTFMove(*handle));
return true;
}
#endif
void GPUProcessProxy::updateSandboxAccess(bool allowAudioCapture, bool allowVideoCapture, bool allowDisplayCapture)
{
if (m_useMockCaptureDevices)
return;
#if PLATFORM(COCOA)
Vector<SandboxExtension::Handle> extensions;
if (allowVideoCapture && !m_hasSentCameraSandboxExtension && addCameraSandboxExtensions(extensions))
m_hasSentCameraSandboxExtension = true;
if (allowAudioCapture && !m_hasSentMicrophoneSandboxExtension && addMicrophoneSandboxExtension(extensions))
m_hasSentMicrophoneSandboxExtension = true;
#if HAVE(SCREEN_CAPTURE_KIT)
if (allowDisplayCapture && !m_hasSentDisplayCaptureSandboxExtension && addDisplayCaptureSandboxExtension(connection()->getAuditToken(), extensions))
m_hasSentDisplayCaptureSandboxExtension = true;
#endif
#if PLATFORM(IOS) || PLATFORM(VISION)
if ((allowAudioCapture || allowVideoCapture) && !m_hasSentTCCDSandboxExtension && addTCCDSandboxExtension(extensions))
m_hasSentTCCDSandboxExtension = true;
#endif // PLATFORM(IOS) || PLATFORM(VISION)
if (!extensions.isEmpty())
send(Messages::GPUProcess::UpdateSandboxAccess { extensions }, 0);
#endif // PLATFORM(COCOA)
}
void GPUProcessProxy::updateCaptureAccess(bool allowAudioCapture, bool allowVideoCapture, bool allowDisplayCapture, WebCore::ProcessIdentifier processID, CompletionHandler<void()>&& completionHandler)
{
updateSandboxAccess(allowAudioCapture, allowVideoCapture, allowDisplayCapture);
sendWithAsyncReply(Messages::GPUProcess::UpdateCaptureAccess { allowAudioCapture, allowVideoCapture, allowDisplayCapture, processID }, WTFMove(completionHandler));
}
void GPUProcessProxy::updateCaptureOrigin(const WebCore::SecurityOriginData& originData, WebCore::ProcessIdentifier processID)
{
send(Messages::GPUProcess::UpdateCaptureOrigin { originData, processID }, 0);
}
void GPUProcessProxy::addMockMediaDevice(const WebCore::MockMediaDevice& device)
{
send(Messages::GPUProcess::AddMockMediaDevice { device }, 0);
}
void GPUProcessProxy::clearMockMediaDevices()
{
send(Messages::GPUProcess::ClearMockMediaDevices { }, 0);
}
void GPUProcessProxy::removeMockMediaDevice(const String& persistentId)
{
send(Messages::GPUProcess::RemoveMockMediaDevice { persistentId }, 0);
}
void GPUProcessProxy::setMockMediaDeviceIsEphemeral(const String& persistentId, bool isEphemeral)
{
send(Messages::GPUProcess::SetMockMediaDeviceIsEphemeral { persistentId, isEphemeral }, 0);
}
void GPUProcessProxy::resetMockMediaDevices()
{
send(Messages::GPUProcess::ResetMockMediaDevices { }, 0);
}
void GPUProcessProxy::setMockCaptureDevicesInterrupted(bool isCameraInterrupted, bool isMicrophoneInterrupted)
{
send(Messages::GPUProcess::SetMockCaptureDevicesInterrupted { isCameraInterrupted, isMicrophoneInterrupted }, 0);
}
void GPUProcessProxy::triggerMockMicrophoneConfigurationChange()
{
send(Messages::GPUProcess::TriggerMockMicrophoneConfigurationChange { }, 0);
}
#endif // ENABLE(MEDIA_STREAM)
#if HAVE(SCREEN_CAPTURE_KIT)
void GPUProcessProxy::promptForGetDisplayMedia(WebCore::DisplayCapturePromptType type, CompletionHandler<void(std::optional<WebCore::CaptureDevice>)>&& completionHandler)
{
sendWithAsyncReply(Messages::GPUProcess::PromptForGetDisplayMedia { type }, WTFMove(completionHandler));
}
#endif
void GPUProcessProxy::getLaunchOptions(ProcessLauncher::LaunchOptions& launchOptions)
{
launchOptions.processType = ProcessLauncher::ProcessType::GPU;
AuxiliaryProcessProxy::getLaunchOptions(launchOptions);
}
void GPUProcessProxy::connectionWillOpen(IPC::Connection&)
{
}
void GPUProcessProxy::processWillShutDown(IPC::Connection& connection)
{
ASSERT_UNUSED(connection, this->connection() == &connection);
if (singleton() == this)
singleton() = nullptr;
}
#if ENABLE(VP9)
std::optional<bool> GPUProcessProxy::s_hasVP9HardwareDecoder;
std::optional<bool> GPUProcessProxy::s_hasVP9ExtensionSupport;
#endif
void GPUProcessProxy::updateWebGPUEnabled(WebProcessProxy& webProcessProxy, bool webGPUEnabled)
{
send(Messages::GPUProcess::UpdateWebGPUEnabled(webProcessProxy.coreProcessIdentifier(), webGPUEnabled), 0);
}
void GPUProcessProxy::updateDOMRenderingEnabled(WebProcessProxy& webProcessProxy, bool isDOMRenderingEnabled)
{
send(Messages::GPUProcess::UpdateDOMRenderingEnabled(webProcessProxy.coreProcessIdentifier(), isDOMRenderingEnabled), 0);
}
void GPUProcessProxy::createGPUProcessConnection(WebProcessProxy& webProcessProxy, IPC::Connection::Handle&& connectionIdentifier, GPUProcessConnectionParameters&& parameters)
{
#if ENABLE(VP9)
parameters.hasVP9HardwareDecoder = s_hasVP9HardwareDecoder;
parameters.hasVP9ExtensionSupport = s_hasVP9ExtensionSupport;
#endif
if (auto* store = webProcessProxy.websiteDataStore())
addSession(*store);
RELEASE_LOG(ProcessSuspension, "%p - GPUProcessProxy is taking a background assertion because a web process is requesting a connection", this);
startResponsivenessTimer(UseLazyStop::No);
sendWithAsyncReply(Messages::GPUProcess::CreateGPUConnectionToWebProcess { webProcessProxy.coreProcessIdentifier(), webProcessProxy.sessionID(), WTFMove(connectionIdentifier), WTFMove(parameters) }, [this, weakThis = WeakPtr { *this }]() mutable {
if (!weakThis)
return;
stopResponsivenessTimer();
}, 0, IPC::SendOption::DispatchMessageEvenWhenWaitingForSyncReply);
}
void GPUProcessProxy::gpuProcessExited(ProcessTerminationReason reason)
{
Ref protectedThis { *this };
switch (reason) {
case ProcessTerminationReason::ExceededMemoryLimit:
case ProcessTerminationReason::ExceededCPULimit:
case ProcessTerminationReason::RequestedByClient:
case ProcessTerminationReason::IdleExit:
case ProcessTerminationReason::Unresponsive:
case ProcessTerminationReason::Crash:
RELEASE_LOG_ERROR(Process, "%p - GPUProcessProxy::gpuProcessExited: reason=%" PUBLIC_LOG_STRING, this, processTerminationReasonToString(reason));
break;
case ProcessTerminationReason::ExceededProcessCountLimit:
case ProcessTerminationReason::NavigationSwap:
case ProcessTerminationReason::RequestedByNetworkProcess:
case ProcessTerminationReason::RequestedByGPUProcess:
ASSERT_NOT_REACHED();
break;
}
if (singleton() == this)
singleton() = nullptr;
for (auto& processPool : WebProcessPool::allProcessPools())
processPool->gpuProcessExited(processID(), reason);
}
void GPUProcessProxy::processIsReadyToExit()
{
RELEASE_LOG(Process, "%p - GPUProcessProxy::processIsReadyToExit:", this);
terminate();
gpuProcessExited(ProcessTerminationReason::IdleExit); // May cause |this| to get deleted.
}
void GPUProcessProxy::terminateForTesting()
{
processIsReadyToExit();
}
void GPUProcessProxy::webProcessConnectionCountForTesting(CompletionHandler<void(uint64_t)>&& completionHandler)
{
sendWithAsyncReply(Messages::GPUProcess::WebProcessConnectionCountForTesting(), WTFMove(completionHandler));
}
void GPUProcessProxy::didClose(IPC::Connection&)
{
RELEASE_LOG_ERROR(Process, "%p - GPUProcessProxy::didClose:", this);
gpuProcessExited(ProcessTerminationReason::Crash); // May cause |this| to get deleted.
}
void GPUProcessProxy::didReceiveInvalidMessage(IPC::Connection& connection, IPC::MessageName messageName)
{
logInvalidMessage(connection, messageName);
WebProcessPool::didReceiveInvalidMessage(messageName);
// Terminate the GPU process.
terminate();
// Since we've invalidated the connection we'll never get a IPC::Connection::Client::didClose
// callback so we'll explicitly call it here instead.
didClose(connection);
}
void GPUProcessProxy::didFinishLaunching(ProcessLauncher* launcher, IPC::Connection::Identifier connectionIdentifier)
{
AuxiliaryProcessProxy::didFinishLaunching(launcher, connectionIdentifier);
if (!connectionIdentifier) {
gpuProcessExited(ProcessTerminationReason::Crash);
return;
}
#if USE(RUNNINGBOARD)
if (xpc_connection_t connection = this->connection()->xpcConnection())
m_throttler.didConnectToProcess(xpc_connection_get_pid(connection));
#endif
#if PLATFORM(COCOA)
if (auto networkProcess = NetworkProcessProxy::defaultNetworkProcess())
networkProcess->sendXPCEndpointToProcess(*this);
#endif
beginResponsivenessChecks();
for (auto& processPool : WebProcessPool::allProcessPools())
processPool->gpuProcessDidFinishLaunching(processID());
#if HAVE(POWERLOG_TASK_MODE_QUERY)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), makeBlockPtr([weakThis = WeakPtr { *this }] () mutable {
if (!isPowerLoggingInTaskMode())
return;
RunLoop::main().dispatch([weakThis = WTFMove(weakThis)] () {
if (!weakThis)
return;
weakThis->enablePowerLogging();
});
}).get());
#endif
}
void GPUProcessProxy::updateProcessAssertion()
{
bool hasAnyForegroundWebProcesses = false;
bool hasAnyBackgroundWebProcesses = false;
for (auto& processPool : WebProcessPool::allProcessPools()) {
hasAnyForegroundWebProcesses |= processPool->hasForegroundWebProcesses();
hasAnyBackgroundWebProcesses |= processPool->hasBackgroundWebProcesses();
}
if (hasAnyForegroundWebProcesses) {
if (!ProcessThrottler::isValidForegroundActivity(m_activityFromWebProcesses)) {
m_activityFromWebProcesses = throttler().foregroundActivity("GPU for foreground view(s)"_s);
}
return;
}
if (hasAnyBackgroundWebProcesses) {
if (!ProcessThrottler::isValidBackgroundActivity(m_activityFromWebProcesses)) {
m_activityFromWebProcesses = throttler().backgroundActivity("GPU for background view(s)"_s);
}
return;
}
// Use std::exchange() instead of a simple nullptr assignment to avoid re-entering this
// function during the destructor of the ProcessThrottler activity, before setting
// m_activityFromWebProcesses.
std::exchange(m_activityFromWebProcesses, nullptr);
}
static inline GPUProcessSessionParameters gpuProcessSessionParameters(const WebsiteDataStore& store)
{
GPUProcessSessionParameters parameters;
parameters.mediaCacheDirectory = store.resolvedMediaCacheDirectory();
SandboxExtension::Handle mediaCacheDirectoryExtensionHandle;
if (!parameters.mediaCacheDirectory.isEmpty()) {
if (auto handle = SandboxExtension::createHandleWithoutResolvingPath(parameters.mediaCacheDirectory, SandboxExtension::Type::ReadWrite))
parameters.mediaCacheDirectorySandboxExtensionHandle = WTFMove(*handle);
}
#if ENABLE(LEGACY_ENCRYPTED_MEDIA)
parameters.mediaKeysStorageDirectory = store.resolvedMediaKeysDirectory();
SandboxExtension::Handle mediaKeysStorageDirectorySandboxExtensionHandle;
if (!parameters.mediaKeysStorageDirectory.isEmpty()) {
if (auto handle = SandboxExtension::createHandleWithoutResolvingPath(parameters.mediaKeysStorageDirectory, SandboxExtension::Type::ReadWrite))
parameters.mediaKeysStorageDirectorySandboxExtensionHandle = WTFMove(*handle);
}
#endif
return parameters;
}
void GPUProcessProxy::addSession(const WebsiteDataStore& store)
{
if (!canSendMessage())
return;
if (m_sessionIDs.contains(store.sessionID()))
return;
send(Messages::GPUProcess::AddSession { store.sessionID(), gpuProcessSessionParameters(store) }, 0);
m_sessionIDs.add(store.sessionID());
}
void GPUProcessProxy::removeSession(PAL::SessionID sessionID)
{
if (!canSendMessage())
return;
if (m_sessionIDs.remove(sessionID))
send(Messages::GPUProcess::RemoveSession { sessionID }, 0);
}
void GPUProcessProxy::sendPrepareToSuspend(IsSuspensionImminent isSuspensionImminent, double remainingRunTime, CompletionHandler<void()>&& completionHandler)
{
sendWithAsyncReply(Messages::GPUProcess::PrepareToSuspend(isSuspensionImminent == IsSuspensionImminent::Yes, MonotonicTime::now() + Seconds(remainingRunTime)), WTFMove(completionHandler), 0, { }, ShouldStartProcessThrottlerActivity::No);
}
void GPUProcessProxy::sendProcessDidResume(ResumeReason)
{
if (canSendMessage())
send(Messages::GPUProcess::ProcessDidResume(), 0);
}
void GPUProcessProxy::terminateWebProcess(WebCore::ProcessIdentifier webProcessIdentifier)
{
if (auto process = WebProcessProxy::processForIdentifier(webProcessIdentifier))
process->requestTermination(ProcessTerminationReason::RequestedByGPUProcess);
}
#if HAVE(VISIBILITY_PROPAGATION_VIEW)
void GPUProcessProxy::didCreateContextForVisibilityPropagation(WebPageProxyIdentifier webPageProxyID, WebCore::PageIdentifier pageID, LayerHostingContextID contextID)
{
RELEASE_LOG(Process, "GPUProcessProxy::didCreateContextForVisibilityPropagation: webPageProxyID: %" PRIu64 ", pagePID: %" PRIu64 ", contextID: %u", webPageProxyID.toUInt64(), pageID.toUInt64(), contextID);
auto page = WebProcessProxy::webPage(webPageProxyID);
if (!page) {
RELEASE_LOG(Process, "GPUProcessProxy::didCreateContextForVisibilityPropagation() No WebPageProxy with this identifier");
return;
}
if (page->webPageID() == pageID) {
page->didCreateContextInGPUProcessForVisibilityPropagation(contextID);
return;
}
auto* provisionalPage = page->provisionalPageProxy();
if (provisionalPage && provisionalPage->webPageID() == pageID) {
provisionalPage->didCreateContextInGPUProcessForVisibilityPropagation(contextID);
return;
}
RELEASE_LOG(Process, "GPUProcessProxy::didCreateContextForVisibilityPropagation() There was a WebPageProxy for this identifier, but it had the wrong WebPage identifier.");
}
#endif
#if PLATFORM(MAC)
void GPUProcessProxy::displayConfigurationChanged(CGDirectDisplayID displayID, CGDisplayChangeSummaryFlags flags)
{
send(Messages::GPUProcess::DisplayConfigurationChanged { displayID, flags }, 0);
}
void GPUProcessProxy::setScreenProperties(const WebCore::ScreenProperties& properties)
{
send(Messages::GPUProcess::SetScreenProperties { properties }, 0);
}
#endif
void GPUProcessProxy::updatePreferences(WebProcessProxy& webProcess)
{
if (!canSendMessage())
return;
// FIXME (https://bugs.webkit.org/show_bug.cgi?id=241821): It's not ideal that these features are controlled by preferences-level feature flags (i.e. per-web view), but there is only
// one GPU process and the runtime-enabled features backing these preferences are process-wide. We should refactor each of these features
// so that they aren't process-global, and then reimplement this feature flag propagation to the GPU Process in a way that respects the
// settings of the page that is hosting each media element.
// For the time being, each of the below features are enabled in the GPU Process if it is enabled by at least one web page's preferences.
// In practice, all web pages' preferences should agree on these feature flag values.
GPUProcessPreferences gpuPreferences;
for (auto page : webProcess.pages()) {
auto& webPreferences = page->preferences();
if (!webPreferences.useGPUProcessForMediaEnabled())
continue;
gpuPreferences.copyEnabledWebPreferences(webPreferences);
}
send(Messages::GPUProcess::UpdateGPUProcessPreferences(gpuPreferences), 0);
}
void GPUProcessProxy::updateScreenPropertiesIfNeeded()
{
#if PLATFORM(MAC)
if (!canSendMessage())
return;
setScreenProperties(collectScreenProperties());
#endif
}
void GPUProcessProxy::didBecomeUnresponsive()
{
RELEASE_LOG_ERROR(Process, "GPUProcessProxy::didBecomeUnresponsive: GPUProcess with PID %d became unresponsive, terminating it", processID());
terminate();
gpuProcessExited(ProcessTerminationReason::Unresponsive);
}
#if !PLATFORM(COCOA)
void GPUProcessProxy::platformInitializeGPUProcessParameters(GPUProcessCreationParameters& parameters)
{
}
#endif
#if ENABLE(VIDEO)
void GPUProcessProxy::requestBitmapImageForCurrentTime(ProcessIdentifier processIdentifier, MediaPlayerIdentifier playerIdentifier, CompletionHandler<void(ShareableBitmap::Handle&&)>&& completion)
{
sendWithAsyncReply(Messages::GPUProcess::RequestBitmapImageForCurrentTime(processIdentifier, playerIdentifier), WTFMove(completion));
}
#endif
#if ENABLE(MEDIA_STREAM) && PLATFORM(IOS_FAMILY)
void GPUProcessProxy::statusBarWasTapped(CompletionHandler<void()>&& completionHandler)
{
if (auto page = WebProcessProxy::audioCapturingWebPage())
page->statusBarWasTapped();
// Find the web page capturing audio and put focus on it.
completionHandler();
}
#endif
} // namespace WebKit
#undef MESSAGE_CHECK
#endif // ENABLE(GPU_PROCESS)
|