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
|
/*
* Copyright (C) 2018 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 "SuspendedPageProxy.h"
#include "APIPageConfiguration.h"
#include "BrowsingContextGroup.h"
#include "DrawingAreaProxy.h"
#include "HandleMessage.h"
#include "Logging.h"
#include "MessageSenderInlines.h"
#include "WebBackForwardCache.h"
#include "WebFrameProxy.h"
#include "WebPageMessages.h"
#include "WebPageProxy.h"
#include "WebPageProxyMessages.h"
#include "WebProcessMessages.h"
#include "WebProcessPool.h"
#include <wtf/DebugUtilities.h>
#include <wtf/HexNumber.h>
#include <wtf/TZoneMallocInlines.h>
#include <wtf/URL.h>
#include <wtf/text/MakeString.h>
namespace WebKit {
using namespace WebCore;
static const Seconds suspensionTimeout { 10_s };
static WeakHashSet<SuspendedPageProxy>& allSuspendedPages()
{
static NeverDestroyed<WeakHashSet<SuspendedPageProxy>> map;
return map;
}
WTF_MAKE_TZONE_ALLOCATED_IMPL(SuspendedPageProxy);
RefPtr<WebProcessProxy> SuspendedPageProxy::findReusableSuspendedPageProcess(WebProcessPool& processPool, const RegistrableDomain& registrableDomain, WebsiteDataStore& dataStore, WebProcessProxy::LockdownMode lockdownMode, const API::PageConfiguration& pageConfiguration)
{
for (Ref suspendedPage : allSuspendedPages()) {
Ref process = suspendedPage->process();
if (&process->processPool() == &processPool
&& process->site() && process->site()->domain() == registrableDomain
&& process->websiteDataStore() == &dataStore
&& process->crossOriginMode() != CrossOriginMode::Isolated
&& process->lockdownMode() == lockdownMode
&& !process->wasTerminated()
&& process->hasSameGPUAndNetworkProcessPreferencesAs(pageConfiguration)) {
return process;
}
}
return nullptr;
}
#if !LOG_DISABLED
using MessageNameSet = HashSet<IPC::MessageName, WTF::IntHash<IPC::MessageName>, WTF::StrongEnumHashTraits<IPC::MessageName>>;
static const MessageNameSet& messageNamesToIgnoreWhileSuspended()
{
static NeverDestroyed<MessageNameSet> messageNames;
static std::once_flag onceFlag;
std::call_once(onceFlag, [] {
messageNames.get().add(IPC::MessageName::WebPageProxy_BackForwardAddItem);
messageNames.get().add(IPC::MessageName::WebPageProxy_ClearAllEditCommands);
messageNames.get().add(IPC::MessageName::WebPageProxy_DidChangeContentSize);
messageNames.get().add(IPC::MessageName::WebPageProxy_DidChangeMainDocument);
messageNames.get().add(IPC::MessageName::WebPageProxy_DidChangeProgress);
messageNames.get().add(IPC::MessageName::WebPageProxy_DidCommitLoadForFrame);
messageNames.get().add(IPC::MessageName::WebPageProxy_DidFinishDocumentLoadForFrame);
messageNames.get().add(IPC::MessageName::WebPageProxy_DidFinishProgress);
messageNames.get().add(IPC::MessageName::WebPageProxy_DidFirstLayoutForFrame);
messageNames.get().add(IPC::MessageName::WebPageProxy_DidFirstVisuallyNonEmptyLayoutForFrame);
messageNames.get().add(IPC::MessageName::WebPageProxy_DidNavigateWithNavigationData);
messageNames.get().add(IPC::MessageName::WebPageProxy_DidReachLayoutMilestone);
messageNames.get().add(IPC::MessageName::WebPageProxy_DidRestoreScrollPosition);
messageNames.get().add(IPC::MessageName::WebPageProxy_DidStartProgress);
messageNames.get().add(IPC::MessageName::WebPageProxy_DidStartProvisionalLoadForFrame);
messageNames.get().add(IPC::MessageName::WebPageProxy_EditorStateChanged);
messageNames.get().add(IPC::MessageName::WebPageProxy_PageExtendedBackgroundColorDidChange);
messageNames.get().add(IPC::MessageName::WebPageProxy_SetRenderTreeSize);
messageNames.get().add(IPC::MessageName::WebPageProxy_SetNetworkRequestsInProgress);
});
return messageNames;
}
#endif
Ref<SuspendedPageProxy> SuspendedPageProxy::create(WebPageProxy& page, Ref<WebProcessProxy>&& process, Ref<WebFrameProxy>&& mainFrame, Ref<BrowsingContextGroup>&& browsingContextGroup, ShouldDelayClosingUntilFirstLayerFlush shouldDelayClosingUntilFirstLayerFlush)
{
return adoptRef(*new SuspendedPageProxy(page, WTFMove(process), WTFMove(mainFrame), WTFMove(browsingContextGroup), shouldDelayClosingUntilFirstLayerFlush));
}
SuspendedPageProxy::SuspendedPageProxy(WebPageProxy& page, Ref<WebProcessProxy>&& process, Ref<WebFrameProxy>&& mainFrame, Ref<BrowsingContextGroup>&& browsingContextGroup, ShouldDelayClosingUntilFirstLayerFlush shouldDelayClosingUntilFirstLayerFlush)
: m_page(page)
, m_webPageID(page.webPageIDInMainFrameProcess())
, m_process(WTFMove(process))
, m_mainFrame(WTFMove(mainFrame))
, m_browsingContextGroup(WTFMove(browsingContextGroup))
, m_shouldDelayClosingUntilFirstLayerFlush(shouldDelayClosingUntilFirstLayerFlush)
, m_suspensionTimeoutTimer(RunLoop::main(), this, &SuspendedPageProxy::suspensionTimedOut)
#if USE(RUNNINGBOARD)
, m_suspensionActivity(m_process->throttler().backgroundActivity("Page suspension for back/forward cache"_s))
#endif
#if HAVE(VISIBILITY_PROPAGATION_VIEW)
, m_contextIDForVisibilityPropagationInWebProcess(page.contextIDForVisibilityPropagationInWebProcess())
#if ENABLE(GPU_PROCESS)
, m_contextIDForVisibilityPropagationInGPUProcess(page.contextIDForVisibilityPropagationInGPUProcess())
#endif
#endif
{
allSuspendedPages().add(*this);
m_process->addSuspendedPageProxy(*this);
m_messageReceiverRegistration.startReceivingMessages(m_process, m_webPageID, *this);
m_suspensionTimeoutTimer.startOneShot(suspensionTimeout);
sendWithAsyncReply(Messages::WebPage::SetIsSuspended(true), [weakThis = WeakPtr { *this }](std::optional<bool> didSuspend) {
RefPtr protectedThis = weakThis.get();
if (!protectedThis || !didSuspend)
return;
protectedThis->didProcessRequestToSuspend(*didSuspend ? SuspensionState::Suspended : SuspensionState::FailedToSuspend);
});
}
template<typename M>
void SuspendedPageProxy::send(M&& message)
{
m_process->send(std::forward<M>(message), m_webPageID);
}
template<typename M, typename C>
void SuspendedPageProxy::sendWithAsyncReply(M&& message, C&& completionHandler)
{
m_process->sendWithAsyncReply(std::forward<M>(message), std::forward<C>(completionHandler), m_webPageID);
}
SuspendedPageProxy::~SuspendedPageProxy()
{
allSuspendedPages().remove(*this);
if (m_readyToUnsuspendHandler) {
RunLoop::protectedMain()->dispatch([readyToUnsuspendHandler = WTFMove(m_readyToUnsuspendHandler)]() mutable {
readyToUnsuspendHandler(nullptr);
});
}
if (m_suspensionState != SuspensionState::Resumed) {
// If the suspended page was not consumed before getting destroyed, then close the corresponding page
// on the WebProcess side.
close();
}
m_process->removeSuspendedPageProxy(*this);
}
void SuspendedPageProxy::didDestroyNavigation(WebCore::NavigationIdentifier navigationID)
{
if (RefPtr page = m_page.get())
page->didDestroyNavigationShared(m_process.copyRef(), navigationID);
}
Ref<WebBackForwardCache> SuspendedPageProxy::protectedBackForwardCache() const
{
return backForwardCache();
}
WebBackForwardCache& SuspendedPageProxy::backForwardCache() const
{
return process().processPool().backForwardCache();
}
void SuspendedPageProxy::waitUntilReadyToUnsuspend(CompletionHandler<void(SuspendedPageProxy*)>&& completionHandler)
{
if (m_readyToUnsuspendHandler)
m_readyToUnsuspendHandler(nullptr);
switch (m_suspensionState) {
case SuspensionState::Suspending:
m_readyToUnsuspendHandler = WTFMove(completionHandler);
break;
case SuspensionState::FailedToSuspend:
case SuspensionState::Suspended:
completionHandler(this);
break;
case SuspensionState::Resumed:
ASSERT_NOT_REACHED();
completionHandler(nullptr);
break;
}
}
void SuspendedPageProxy::unsuspend()
{
ASSERT(m_suspensionState == SuspensionState::Suspended);
m_suspensionState = SuspensionState::Resumed;
sendWithAsyncReply(Messages::WebPage::SetIsSuspended(false), [](std::optional<bool> didSuspend) {
ASSERT(!didSuspend.has_value());
});
}
void SuspendedPageProxy::close()
{
ASSERT(m_suspensionState != SuspensionState::Resumed);
if (m_isClosed)
return;
RELEASE_LOG(ProcessSwapping, "%p - SuspendedPageProxy::close()", this);
m_isClosed = true;
send(Messages::WebPage::Close());
}
void SuspendedPageProxy::pageDidFirstLayerFlush()
{
m_shouldDelayClosingUntilFirstLayerFlush = ShouldDelayClosingUntilFirstLayerFlush::No;
if (m_shouldCloseWhenEnteringAcceleratedCompositingMode) {
// We needed the suspended page to stay alive to avoid flashing. Now we can get rid of it.
close();
}
}
bool SuspendedPageProxy::pageIsClosedOrClosing() const
{
return m_isClosed || m_shouldCloseWhenEnteringAcceleratedCompositingMode;
}
void SuspendedPageProxy::closeWithoutFlashing()
{
RELEASE_LOG(ProcessSwapping, "%p - SuspendedPageProxy::closeWithoutFlashing() shouldDelayClosingUntilFirstLayerFlush? %d", this, m_shouldDelayClosingUntilFirstLayerFlush == ShouldDelayClosingUntilFirstLayerFlush::Yes);
if (m_shouldDelayClosingUntilFirstLayerFlush == ShouldDelayClosingUntilFirstLayerFlush::Yes) {
m_shouldCloseWhenEnteringAcceleratedCompositingMode = true;
return;
}
close();
}
void SuspendedPageProxy::didProcessRequestToSuspend(SuspensionState newSuspensionState)
{
LOG(ProcessSwapping, "SuspendedPageProxy %s from process %i finished transition to suspended", loggingString().utf8().data(), m_process->processID());
RELEASE_LOG(ProcessSwapping, "%p - SuspendedPageProxy::didProcessRequestToSuspend() success? %d", this, newSuspensionState == SuspensionState::Suspended);
ASSERT(m_suspensionState == SuspensionState::Suspending);
ASSERT(newSuspensionState == SuspensionState::Suspended || newSuspensionState == SuspensionState::FailedToSuspend);
m_suspensionState = newSuspensionState;
m_suspensionTimeoutTimer.stop();
#if USE(RUNNINGBOARD)
m_suspensionActivity = nullptr;
#endif
m_messageReceiverRegistration.stopReceivingMessages();
if (m_suspensionState == SuspensionState::FailedToSuspend)
closeWithoutFlashing();
if (m_readyToUnsuspendHandler)
m_readyToUnsuspendHandler(this);
}
void SuspendedPageProxy::suspensionTimedOut()
{
RELEASE_LOG_ERROR(ProcessSwapping, "%p - SuspendedPageProxy::suspensionTimedOut() destroying the suspended page because it failed to suspend in time", this);
protectedBackForwardCache()->removeEntry(*this); // Will destroy |this|.
}
WebPageProxy* SuspendedPageProxy::page() const
{
return m_page.get();
}
void SuspendedPageProxy::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder)
{
ASSERT(decoder.messageReceiverName() == Messages::WebPageProxy::messageReceiverName());
if (decoder.messageName() == Messages::WebPageProxy::DidDestroyNavigation::name()) {
IPC::handleMessage<Messages::WebPageProxy::DidDestroyNavigation>(connection, decoder, this, &SuspendedPageProxy::didDestroyNavigation);
return;
}
#if !LOG_DISABLED
if (!messageNamesToIgnoreWhileSuspended().contains(decoder.messageName()))
LOG(ProcessSwapping, "SuspendedPageProxy received unexpected WebPageProxy message '%s'", description(decoder.messageName()).characters());
#endif
}
bool SuspendedPageProxy::didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, UniqueRef<IPC::Encoder>&)
{
return false;
}
#if !LOG_DISABLED
String SuspendedPageProxy::loggingString() const
{
return makeString("(0x"_s, hex(reinterpret_cast<uintptr_t>(this)), " WebPage ID "_s, m_webPageID.toUInt64(), ", m_suspensionState "_s, static_cast<unsigned>(m_suspensionState), ')');
}
#endif
} // namespace WebKit
|