2 * Copyright (C) 2018 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "SuspendedPageProxy.h"
30 #include "WebPageMessages.h"
31 #include "WebPageProxy.h"
32 #include "WebPageProxyMessages.h"
33 #include "WebProcessMessages.h"
34 #include "WebProcessPool.h"
35 #include "WebProcessProxy.h"
36 #include <WebCore/URL.h>
37 #include <wtf/DebugUtilities.h>
40 using namespace WebCore;
43 static const HashSet<IPC::StringReference>& messageNamesToIgnoreWhileSuspended()
45 static NeverDestroyed<HashSet<IPC::StringReference>> messageNames;
46 static std::once_flag onceFlag;
47 std::call_once(onceFlag, [] {
48 messageNames.get().add("BackForwardAddItem");
49 messageNames.get().add("ClearAllEditCommands");
50 messageNames.get().add("DidChangeContentSize");
51 messageNames.get().add("DidChangeMainDocument");
52 messageNames.get().add("DidChangeProgress");
53 messageNames.get().add("DidCommitLoadForFrame");
54 messageNames.get().add("DidDestroyNavigation");
55 messageNames.get().add("DidFinishDocumentLoadForFrame");
56 messageNames.get().add("DidFinishProgress");
57 messageNames.get().add("DidCompletePageTransition");
58 messageNames.get().add("DidFirstLayoutForFrame");
59 messageNames.get().add("DidFirstVisuallyNonEmptyLayoutForFrame");
60 messageNames.get().add("DidNavigateWithNavigationData");
61 messageNames.get().add("DidReachLayoutMilestone");
62 messageNames.get().add("DidRestoreScrollPosition");
63 messageNames.get().add("DidSaveToPageCache");
64 messageNames.get().add("DidStartProgress");
65 messageNames.get().add("DidStartProvisionalLoadForFrame");
66 messageNames.get().add("EditorStateChanged");
67 messageNames.get().add("PageExtendedBackgroundColorDidChange");
68 messageNames.get().add("SetRenderTreeSize");
69 messageNames.get().add("SetStatusText");
70 messageNames.get().add("SetNetworkRequestsInProgress");
77 SuspendedPageProxy::SuspendedPageProxy(WebPageProxy& page, Ref<WebProcessProxy>&& process, WebBackForwardListItem& item, uint64_t mainFrameID)
79 , m_process(WTFMove(process))
80 , m_mainFrameID(mainFrameID)
81 , m_origin(SecurityOriginData::fromURL({ { }, item.url() }))
83 item.setSuspendedPage(*this);
84 m_process->addMessageReceiver(Messages::WebPageProxy::messageReceiverName(), m_page.pageID(), *this);
86 m_process->send(Messages::WebPage::SetIsSuspended(true), m_page.pageID());
89 SuspendedPageProxy::~SuspendedPageProxy()
94 // If the suspended page was not consumed before getting destroyed, then close the corresponding page
95 // on the WebProcess side.
96 m_process->send(Messages::WebPage::Close(), m_page.pageID());
97 m_process->removeMessageReceiver(Messages::WebPageProxy::messageReceiverName(), m_page.pageID());
99 // We call maybeShutDown() asynchronously since the SuspendedPage is currently being removed from the WebProcessPool
100 // and we want to avoid re-entering WebProcessPool methods.
101 RunLoop::main().dispatch([process = m_process.copyRef()] {
102 process->maybeShutDown();
106 void SuspendedPageProxy::unsuspend()
108 ASSERT(m_isSuspended);
110 m_isSuspended = false;
111 m_process->removeMessageReceiver(Messages::WebPageProxy::messageReceiverName(), m_page.pageID());
112 m_process->send(Messages::WebPage::SetIsSuspended(false), m_page.pageID());
115 void SuspendedPageProxy::didFinishLoad()
117 LOG(ProcessSwapping, "SuspendedPageProxy %s from process %i finished transition to suspended", loggingString(), m_process->processIdentifier());
120 m_finishedSuspending = true;
123 m_process->send(Messages::WebProcess::UpdateActivePages(), 0);
126 void SuspendedPageProxy::didReceiveMessage(IPC::Connection&, IPC::Decoder& decoder)
128 ASSERT(decoder.messageReceiverName() == Messages::WebPageProxy::messageReceiverName());
130 if (decoder.messageName() == Messages::WebPageProxy::DidFinishLoadForFrame::name()) {
135 if (!messageNamesToIgnoreWhileSuspended().contains(decoder.messageName()))
136 LOG(ProcessSwapping, "SuspendedPageProxy received unexpected WebPageProxy message '%s'", decoder.messageName().toString().data());
140 void SuspendedPageProxy::didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&)
145 const char* SuspendedPageProxy::loggingString() const
147 return debugString("(", String::format("%p", this), " page ID ", String::number(m_page.pageID()), ", m_finishedSuspending ", String::number(m_finishedSuspending), ")");
151 } // namespace WebKit