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
|
/*
* Copyright (C) 2024 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 "ModelConnectionToWebProcess.h"
#if ENABLE(MODEL_PROCESS)
#include "LayerHostingContext.h"
#include "Logging.h"
#include "MessageSenderInlines.h"
#include "ModelConnectionToWebProcessMessages.h"
#include "ModelProcess.h"
#include "ModelProcessConnectionInfo.h"
#include "ModelProcessConnectionMessages.h"
#include "ModelProcessConnectionParameters.h"
#include "ModelProcessMessages.h"
#include "ModelProcessModelPlayerManagerProxy.h"
#include "ModelProcessModelPlayerManagerProxyMessages.h"
#include "ModelProcessModelPlayerProxy.h"
#include "ModelProcessModelPlayerProxyMessages.h"
#include "ModelProcessProxyMessages.h"
#include "WebErrors.h"
#include "WebProcessMessages.h"
#include <WebCore/LogInitialization.h>
#if ENABLE(IPC_TESTING_API)
#include "IPCTesterMessages.h"
#endif
#define MESSAGE_CHECK(assertion) MESSAGE_CHECK_BASE(assertion, connection())
namespace WebKit {
using namespace WebCore;
Ref<ModelConnectionToWebProcess> ModelConnectionToWebProcess::create(ModelProcess& modelProcess, WebCore::ProcessIdentifier webProcessIdentifier, PAL::SessionID sessionID, IPC::Connection::Handle&& connectionHandle, ModelProcessConnectionParameters&& parameters)
{
return adoptRef(*new ModelConnectionToWebProcess(modelProcess, webProcessIdentifier, sessionID, WTFMove(connectionHandle), WTFMove(parameters)));
}
ModelConnectionToWebProcess::ModelConnectionToWebProcess(ModelProcess& modelProcess, WebCore::ProcessIdentifier webProcessIdentifier, PAL::SessionID sessionID, IPC::Connection::Handle&& connectionHandle, ModelProcessConnectionParameters&& parameters)
: m_modelProcessModelPlayerManagerProxy(ModelProcessModelPlayerManagerProxy::create(*this))
, m_connection(IPC::Connection::createClientConnection(IPC::Connection::Identifier { WTFMove(connectionHandle) }))
, m_modelProcess(modelProcess)
, m_webProcessIdentifier(webProcessIdentifier)
, m_webProcessIdentity(WTFMove(parameters.webProcessIdentity))
, m_sessionID(sessionID)
#if HAVE(AUDIT_TOKEN)
, m_presentingApplicationAuditToken(parameters.presentingApplicationAuditToken ? std::optional(parameters.presentingApplicationAuditToken->auditToken()) : std::nullopt)
#endif
#if ENABLE(IPC_TESTING_API)
, m_ipcTester(IPCTester::create())
#endif
, m_sharedPreferencesForWebProcess(WTFMove(parameters.sharedPreferencesForWebProcess))
{
RELEASE_ASSERT(RunLoop::isMain());
// Use this flag to force synchronous messages to be treated as asynchronous messages in the WebProcess.
// Otherwise, the WebProcess would process incoming synchronous IPC while waiting for a synchronous IPC
// reply from the model process, which would be unsafe.
m_connection->setOnlySendMessagesAsDispatchWhenWaitingForSyncReplyWhenProcessingSuchAMessage(true);
m_connection->open(*this);
WebKit::ModelProcessConnectionInfo info {
#if HAVE(AUDIT_TOKEN)
modelProcess.parentProcessConnection()->getAuditToken(),
#endif
};
m_connection->send(Messages::ModelProcessConnection::DidInitialize(info), 0);
++gObjectCountForTesting;
}
ModelConnectionToWebProcess::~ModelConnectionToWebProcess()
{
RELEASE_ASSERT(RunLoop::isMain());
m_connection->invalidate();
--gObjectCountForTesting;
}
uint64_t ModelConnectionToWebProcess::gObjectCountForTesting = 0;
void ModelConnectionToWebProcess::didClose(IPC::Connection& connection)
{
modelProcess().connectionToWebProcessClosed(connection);
modelProcess().removeModelConnectionToWebProcess(*this); // May destroy |this|.
}
#if HAVE(VISIBILITY_PROPAGATION_VIEW)
void ModelConnectionToWebProcess::createVisibilityPropagationContextForPage(WebPageProxyIdentifier pageProxyID, WebCore::PageIdentifier pageID, bool canShowWhileLocked)
{
auto contextForVisibilityPropagation = LayerHostingContext::createForExternalHostingProcess({ canShowWhileLocked });
RELEASE_LOG(Process, "ModelConnectionToWebProcess::createVisibilityPropagationContextForPage: pageProxyID=%" PRIu64 ", webPageID=%" PRIu64 ", contextID=%u", pageProxyID.toUInt64(), pageID.toUInt64(), contextForVisibilityPropagation->contextID());
modelProcess().send(Messages::ModelProcessProxy::DidCreateContextForVisibilityPropagation(pageProxyID, pageID, contextForVisibilityPropagation->contextID()));
m_visibilityPropagationContexts.add(std::make_pair(pageProxyID, pageID), WTFMove(contextForVisibilityPropagation));
}
void ModelConnectionToWebProcess::destroyVisibilityPropagationContextForPage(WebPageProxyIdentifier pageProxyID, WebCore::PageIdentifier pageID)
{
RELEASE_LOG(Process, "ModelConnectionToWebProcess::destroyVisibilityPropagationContextForPage: pageProxyID=%" PRIu64 ", webPageID=%" PRIu64, pageProxyID.toUInt64(), pageID.toUInt64());
auto key = std::make_pair(pageProxyID, pageID);
ASSERT(m_visibilityPropagationContexts.contains(key));
m_visibilityPropagationContexts.remove(key);
}
#endif
void ModelConnectionToWebProcess::configureLoggingChannel(const String& channelName, WTFLogChannelState state, WTFLogLevel level)
{
#if !RELEASE_LOG_DISABLED
if (auto* channel = WebCore::getLogChannel(channelName)) {
channel->state = state;
channel->level = level;
}
auto* channel = getLogChannel(channelName);
if (!channel)
return;
channel->state = state;
channel->level = level;
#else
UNUSED_PARAM(channelName);
UNUSED_PARAM(state);
UNUSED_PARAM(level);
#endif
}
bool ModelConnectionToWebProcess::allowsExitUnderMemoryPressure() const
{
// FIXME: Should allow exit if we have no models.
return false;
}
Logger& ModelConnectionToWebProcess::logger()
{
if (!m_logger) {
m_logger = Logger::create(this);
m_logger->setEnabled(this, isAlwaysOnLoggingAllowed());
}
return *m_logger;
}
void ModelConnectionToWebProcess::didReceiveInvalidMessage(IPC::Connection& connection, IPC::MessageName messageName, int32_t)
{
#if ENABLE(IPC_TESTING_API)
if (connection.ignoreInvalidMessageForTesting())
return;
#endif
RELEASE_LOG_FAULT(IPC, "Received an invalid message '%" PUBLIC_LOG_STRING "' from WebContent process %" PRIu64 ".", description(messageName).characters(), m_webProcessIdentifier.toUInt64());
m_modelProcess->parentProcessConnection()->send(Messages::ModelProcessProxy::TerminateWebProcess(m_webProcessIdentifier), 0);
}
void ModelConnectionToWebProcess::lowMemoryHandler(Critical critical, Synchronous synchronous)
{
}
bool ModelConnectionToWebProcess::dispatchMessage(IPC::Connection& connection, IPC::Decoder& decoder)
{
if (decoder.messageReceiverName() == Messages::ModelProcessModelPlayerManagerProxy::messageReceiverName()) {
modelProcessModelPlayerManagerProxy().didReceiveMessageFromWebProcess(connection, decoder);
return true;
}
if (decoder.messageReceiverName() == Messages::ModelProcessModelPlayerProxy::messageReceiverName()) {
modelProcessModelPlayerManagerProxy().didReceivePlayerMessage(connection, decoder);
return true;
}
#if ENABLE(IPC_TESTING_API)
if (decoder.messageReceiverName() == Messages::IPCTester::messageReceiverName()) {
m_ipcTester->didReceiveMessage(connection, decoder);
return true;
}
#endif
return messageReceiverMap().dispatchMessage(connection, decoder);
}
bool ModelConnectionToWebProcess::dispatchSyncMessage(IPC::Connection& connection, IPC::Decoder& decoder, UniqueRef<IPC::Encoder>& replyEncoder)
{
#if ENABLE(IPC_TESTING_API)
if (decoder.messageReceiverName() == Messages::IPCTester::messageReceiverName()) {
m_ipcTester->didReceiveSyncMessage(connection, decoder, replyEncoder);
return true;
}
#endif
return messageReceiverMap().dispatchSyncMessage(connection, decoder, replyEncoder);
}
bool ModelConnectionToWebProcess::isAlwaysOnLoggingAllowed() const
{
return m_sessionID.isAlwaysOnLoggingAllowed() || m_sharedPreferencesForWebProcess.allowPrivacySensitiveOperationsInNonPersistentDataStores;
}
} // namespace WebKit
#undef MESSAGE_CHECK
#endif // ENABLE(MODEL_PROCESS)
|