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
|
/*
* Copyright (C) 2011 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 "WebFullScreenManagerProxy.h"
#if ENABLE(FULLSCREEN_API)
#include "APIFullscreenClient.h"
#include "APIPageConfiguration.h"
#include "MessageSenderInlines.h"
#include "RemotePageFullscreenManagerProxy.h"
#include "WebAutomationSession.h"
#include "WebFrameProxy.h"
#include "WebFullScreenManagerMessages.h"
#include "WebFullScreenManagerProxyMessages.h"
#include "WebPageProxy.h"
#include "WebProcessPool.h"
#include "WebProcessProxy.h"
#include <WebCore/IntRect.h>
#include <WebCore/MIMETypeRegistry.h>
#include <WebCore/ScreenOrientationType.h>
#include <wtf/CallbackAggregator.h>
#include <wtf/LoggerHelper.h>
#include <wtf/TZoneMallocInlines.h>
#include <wtf/text/MakeString.h>
namespace WebKit {
using namespace WebCore;
#if ENABLE(QUICKLOOK_FULLSCREEN)
static WorkQueue& sharedQuickLookFileQueue()
{
static NeverDestroyed<Ref<WorkQueue>> queue(WorkQueue::create("com.apple.WebKit.QuickLookFileQueue"_s, WorkQueue::QOS::UserInteractive));
return queue.get();
}
#endif
WTF_MAKE_TZONE_ALLOCATED_IMPL(WebFullScreenManagerProxy);
Ref<WebFullScreenManagerProxy> WebFullScreenManagerProxy::create(WebPageProxy& page, WebFullScreenManagerProxyClient& client)
{
return adoptRef(*new WebFullScreenManagerProxy(page, client));
}
WebFullScreenManagerProxy::WebFullScreenManagerProxy(WebPageProxy& page, WebFullScreenManagerProxyClient& client)
: m_page(page)
, m_client(client)
#if !RELEASE_LOG_DISABLED
, m_logger(page.logger())
, m_logIdentifier(page.logIdentifier())
#endif
{
page.protectedLegacyMainFrameProcess()->addMessageReceiver(Messages::WebFullScreenManagerProxy::messageReceiverName(), page.webPageIDInMainFrameProcess(), *this);
}
WebFullScreenManagerProxy::~WebFullScreenManagerProxy()
{
if (RefPtr page = m_page.get())
page->protectedLegacyMainFrameProcess()->removeMessageReceiver(Messages::WebFullScreenManagerProxy::messageReceiverName(), page->webPageIDInMainFrameProcess());
if (CheckedPtr client = m_client)
client->closeFullScreenManager();
callCloseCompletionHandlers();
}
std::optional<SharedPreferencesForWebProcess> WebFullScreenManagerProxy::sharedPreferencesForWebProcess(const IPC::Connection& connection) const
{
return dynamicDowncast<WebProcessProxy>(AuxiliaryProcessProxy::fromConnection(connection))->sharedPreferencesForWebProcess();
}
template<typename M> void WebFullScreenManagerProxy::sendToWebProcess(M&& message)
{
RefPtr page = m_page.get();
if (!page)
return;
RefPtr fullScreenProcess = m_fullScreenProcess.get();
if (!fullScreenProcess)
return;
fullScreenProcess->send(std::forward<M>(message), page->webPageIDInProcess(*fullScreenProcess));
}
void WebFullScreenManagerProxy::didEnterFullScreen(CompletionHandler<void(bool)>&& completionHandler)
{
ALWAYS_LOG(LOGIDENTIFIER);
RefPtr page = m_page.get();
if (!page)
return completionHandler(false);
m_fullscreenState = FullscreenState::InFullscreen;
page->fullscreenClient().didEnterFullscreen(page.get());
completionHandler(true);
if (page->isControlledByAutomation()) {
if (RefPtr automationSession = page->configuration().processPool().automationSession())
automationSession->didEnterFullScreenForPage(*page);
}
}
void WebFullScreenManagerProxy::callCloseCompletionHandlers()
{
auto closeMediaCallbacks = WTFMove(m_closeCompletionHandlers);
for (auto& callback : closeMediaCallbacks)
callback();
}
void WebFullScreenManagerProxy::closeWithCallback(CompletionHandler<void()>&& completionHandler)
{
m_closeCompletionHandlers.append(WTFMove(completionHandler));
close();
}
void WebFullScreenManagerProxy::setAnimatingFullScreen(bool animating)
{
sendToWebProcess(Messages::WebFullScreenManager::SetAnimatingFullScreen(animating));
}
void WebFullScreenManagerProxy::requestRestoreFullScreen(CompletionHandler<void(bool)>&& completionHandler)
{
ALWAYS_LOG(LOGIDENTIFIER);
RefPtr page = m_page.get();
if (!page)
return completionHandler(false);
RefPtr fullScreenProcess = m_fullScreenProcess.get();
if (!fullScreenProcess)
return completionHandler(false);
fullScreenProcess->sendWithAsyncReply(Messages::WebFullScreenManager::RequestRestoreFullScreen(), WTFMove(completionHandler), page->webPageIDInProcess(*fullScreenProcess));
}
void WebFullScreenManagerProxy::requestExitFullScreen()
{
ALWAYS_LOG(LOGIDENTIFIER);
sendToWebProcess(Messages::WebFullScreenManager::RequestExitFullScreen());
}
void WebFullScreenManagerProxy::setFullscreenInsets(const WebCore::FloatBoxExtent& insets)
{
sendToWebProcess(Messages::WebFullScreenManager::SetFullscreenInsets(insets));
}
void WebFullScreenManagerProxy::setFullscreenAutoHideDuration(Seconds duration)
{
sendToWebProcess(Messages::WebFullScreenManager::SetFullscreenAutoHideDuration(duration));
}
void WebFullScreenManagerProxy::close()
{
if (CheckedPtr client = m_client)
client->closeFullScreenManager();
}
void WebFullScreenManagerProxy::detachFromClient()
{
close();
m_client = nullptr;
}
void WebFullScreenManagerProxy::attachToNewClient(WebFullScreenManagerProxyClient& client)
{
m_client = &client;
}
bool WebFullScreenManagerProxy::isFullScreen()
{
return m_client && m_client->isFullScreen();
}
bool WebFullScreenManagerProxy::blocksReturnToFullscreenFromPictureInPicture() const
{
return m_blocksReturnToFullscreenFromPictureInPicture;
}
void WebFullScreenManagerProxy::enterFullScreen(IPC::Connection& connection, FrameIdentifier frameID, bool blocksReturnToFullscreenFromPictureInPicture, FullScreenMediaDetails&& mediaDetails, CompletionHandler<void(bool)>&& completionHandler)
{
m_fullScreenProcess = dynamicDowncast<WebProcessProxy>(AuxiliaryProcessProxy::fromConnection(connection));
m_blocksReturnToFullscreenFromPictureInPicture = blocksReturnToFullscreenFromPictureInPicture;
#if PLATFORM(IOS_FAMILY)
#if ENABLE(VIDEO_USES_ELEMENT_FULLSCREEN)
m_isVideoElement = mediaDetails.type == FullScreenMediaDetails::Type::Video;
#endif
#if ENABLE(QUICKLOOK_FULLSCREEN)
if (mediaDetails.imageHandle) {
auto sharedMemoryBuffer = SharedMemory::map(WTFMove(*mediaDetails.imageHandle), WebCore::SharedMemory::Protection::ReadOnly);
if (sharedMemoryBuffer)
m_imageBuffer = sharedMemoryBuffer->createSharedBuffer(sharedMemoryBuffer->size());
}
m_imageMIMEType = mediaDetails.mimeType;
#endif // ENABLE(QUICKLOOK_FULLSCREEN)
#endif // PLATFORM(IOS_FAMILY)
CheckedPtr client = m_client;
if (!client)
return completionHandler(false);
client->enterFullScreen(mediaDetails.mediaDimensions, [this, protectedThis = Ref { *this }, completionHandler = WTFMove(completionHandler), frameID, logIdentifier = LOGIDENTIFIER] (bool success) mutable {
ALWAYS_LOG(logIdentifier);
if (!success)
return completionHandler(false);
m_fullscreenState = FullscreenState::EnteringFullscreen;
if (RefPtr page = m_page.get())
page->fullscreenClient().willEnterFullscreen(page.get());
enterFullScreenForOwnerElementsInOtherProcesses(frameID, [completionHandler = WTFMove(completionHandler)] mutable {
completionHandler(true);
});
});
}
void WebFullScreenManagerProxy::enterFullScreenForOwnerElementsInOtherProcesses(FrameIdentifier frameID, CompletionHandler<void()>&& completionHandler)
{
Ref aggregator = CallbackAggregator::create(WTFMove(completionHandler));
RefPtr webFrame = WebFrameProxy::webFrame(frameID);
if (!webFrame)
return;
RefPtr page = m_page.get();
if (!page)
return;
// Traverse from the frame's parent to the main frame and tell each unique process the lowest frame to start fullscreening.
HashSet<ProcessIdentifier> processes { webFrame->process().coreProcessIdentifier() };
for (RefPtr currentFrame = webFrame; currentFrame; currentFrame = currentFrame->parentFrame()) {
RefPtr ancestor = currentFrame->parentFrame();
if (!ancestor)
break;
auto addResult = processes.add(ancestor->process().coreProcessIdentifier());
if (addResult.isNewEntry) {
Ref process = ancestor->process();
process->sendWithAsyncReply(Messages::WebFullScreenManager::EnterFullScreenForOwnerElements(currentFrame->frameID()), [aggregator] { }, page->webPageIDInProcess(process));
}
}
}
#if ENABLE(QUICKLOOK_FULLSCREEN)
void WebFullScreenManagerProxy::updateImageSource(FullScreenMediaDetails&& mediaDetails)
{
if (mediaDetails.imageHandle) {
if (auto sharedMemoryBuffer = SharedMemory::map(WTFMove(*mediaDetails.imageHandle), WebCore::SharedMemory::Protection::ReadOnly))
m_imageBuffer = sharedMemoryBuffer->createSharedBuffer(sharedMemoryBuffer->size());
}
m_imageMIMEType = mediaDetails.mimeType;
if (CheckedPtr client = m_client)
client->updateImageSource();
}
#endif // ENABLE(QUICKLOOK_FULLSCREEN)
void WebFullScreenManagerProxy::exitFullScreen(CompletionHandler<void()>&& completionHandler)
{
#if ENABLE(QUICKLOOK_FULLSCREEN)
m_imageBuffer = nullptr;
#endif
if (CheckedPtr client = m_client)
client->exitFullScreen([this, protectedThis = Ref { *this }, completionHandler = WTFMove(completionHandler)] mutable {
m_fullscreenState = FullscreenState::ExitingFullscreen;
if (RefPtr page = m_page.get())
page->fullscreenClient().willExitFullscreen(page.get());
completionHandler();
});
else
completionHandler();
}
#if ENABLE(QUICKLOOK_FULLSCREEN)
void WebFullScreenManagerProxy::prepareQuickLookImageURL(CompletionHandler<void(URL&&)>&& completionHandler) const
{
if (!m_imageBuffer)
return completionHandler(URL());
sharedQuickLookFileQueue().dispatch([buffer = m_imageBuffer, mimeType = crossThreadCopy(m_imageMIMEType), completionHandler = WTFMove(completionHandler)]() mutable {
auto suffix = makeString('.', WebCore::MIMETypeRegistry::preferredExtensionForMIMEType(mimeType));
auto [filePath, fileHandle] = FileSystem::openTemporaryFile("QuickLook"_s, suffix);
ASSERT(FileSystem::isHandleValid(fileHandle));
size_t byteCount = FileSystem::writeToFile(fileHandle, buffer->span());
ASSERT_UNUSED(byteCount, byteCount == buffer->size());
FileSystem::closeFile(fileHandle);
RunLoop::protectedMain()->dispatch([filePath, completionHandler = WTFMove(completionHandler)]() mutable {
completionHandler(URL::fileURLWithFileSystemPath(filePath));
});
});
}
#endif
void WebFullScreenManagerProxy::beganEnterFullScreen(const IntRect& initialFrame, const IntRect& finalFrame, CompletionHandler<void(bool)>&& completionHandler)
{
RefPtr page = m_page.get();
if (!page)
return completionHandler(false);
page->callAfterNextPresentationUpdate([weakThis = WeakPtr { *this }, initialFrame, finalFrame, completionHandler = WTFMove(completionHandler)] mutable {
if (CheckedPtr client = weakThis ? weakThis->m_client : nullptr)
client->beganEnterFullScreen(initialFrame, finalFrame, [weakThis = WTFMove(weakThis), completionHandler = WTFMove(completionHandler)] (bool success) mutable {
if (weakThis && success)
weakThis->didEnterFullScreen(WTFMove(completionHandler));
else
completionHandler(false);
});
else
completionHandler(false);
});
}
void WebFullScreenManagerProxy::beganExitFullScreen(FrameIdentifier frameID, const IntRect& initialFrame, const IntRect& finalFrame, CompletionHandler<void()>&& completionHandler)
{
CheckedPtr client = m_client;
if (!client)
return completionHandler();
client->beganExitFullScreen(initialFrame, finalFrame, [this, protectedThis = Ref { *this }, completionHandler = WTFMove(completionHandler), frameID, logIdentifier = LOGIDENTIFIER] mutable {
m_fullscreenState = FullscreenState::NotInFullscreen;
RefPtr page = m_page.get();
if (!page)
return completionHandler();
ALWAYS_LOG(logIdentifier);
page->fullscreenClient().didExitFullscreen(page.get());
exitFullScreenInOtherProcesses(frameID, WTFMove(completionHandler));
if (page->isControlledByAutomation()) {
if (RefPtr automationSession = page->configuration().processPool().automationSession())
automationSession->didExitFullScreenForPage(*page);
}
callCloseCompletionHandlers();
});
}
void WebFullScreenManagerProxy::exitFullScreenInOtherProcesses(FrameIdentifier frameID, CompletionHandler<void()>&& completionHandler)
{
Ref aggregator = CallbackAggregator::create(WTFMove(completionHandler));
RefPtr webFrame = WebFrameProxy::webFrame(frameID);
if (!webFrame)
return;
RefPtr page = m_page.get();
if (!page)
return;
// Traverse from the frame's parent to the main frame and tell each unique process start unfullscreening.
HashSet<ProcessIdentifier> processes { webFrame->process().coreProcessIdentifier() };
for (RefPtr ancestor = webFrame->parentFrame(); ancestor; ancestor = ancestor->parentFrame()) {
auto addResult = processes.add(ancestor->process().coreProcessIdentifier());
if (addResult.isNewEntry) {
Ref process = ancestor->process();
process->sendWithAsyncReply(Messages::WebFullScreenManager::ExitFullScreenInMainFrame(), [aggregator] { }, page->webPageIDInProcess(process));
}
}
}
bool WebFullScreenManagerProxy::lockFullscreenOrientation(WebCore::ScreenOrientationType orientation)
{
if (CheckedPtr client = m_client)
return client->lockFullscreenOrientation(orientation);
return false;
}
void WebFullScreenManagerProxy::unlockFullscreenOrientation()
{
if (CheckedPtr client = m_client)
client->unlockFullscreenOrientation();
}
#if !RELEASE_LOG_DISABLED
WTFLogChannel& WebFullScreenManagerProxy::logChannel() const
{
return WebKit2LogFullscreen;
}
#endif
} // namespace WebKit
#endif // ENABLE(FULLSCREEN_API)
|