Delay WebProcess launch until a load is triggered in a Web view
[WebKit-https.git] / Source / WebKit / Shared / API / Cocoa / RemoteObjectRegistry.mm
1 /*
2  * Copyright (C) 2013 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #import "config.h"
27 #import "RemoteObjectRegistry.h"
28
29 #import "MessageSender.h"
30 #import "ProcessThrottler.h"
31 #import "RemoteObjectInvocation.h"
32 #import "RemoteObjectRegistryMessages.h"
33 #import "UserData.h"
34 #import "WebPage.h"
35 #import "WebPageProxy.h"
36 #import "WebProcessProxy.h"
37 #import "_WKRemoteObjectRegistryInternal.h"
38
39 namespace WebKit {
40
41 RemoteObjectRegistry::RemoteObjectRegistry(_WKRemoteObjectRegistry *remoteObjectRegistry, WebPage& page)
42     : m_remoteObjectRegistry(remoteObjectRegistry)
43     , m_messageSender(page)
44     , m_takeBackgroundActivityToken([] { return ProcessThrottler::BackgroundActivityToken(); })
45 {
46 }
47
48 RemoteObjectRegistry::RemoteObjectRegistry(_WKRemoteObjectRegistry *remoteObjectRegistry, WebPageProxy& page)
49     : m_remoteObjectRegistry(remoteObjectRegistry)
50     , m_messageSender(page)
51     , m_takeBackgroundActivityToken([&page] { return page.process().throttler().backgroundActivityToken(); })
52     , m_launchInitialProcessIfNecessary([&page] { page.launchInitialProcessIfNecessary(); })
53 {
54 }
55
56
57 RemoteObjectRegistry::~RemoteObjectRegistry()
58 {
59 }
60
61 void RemoteObjectRegistry::sendInvocation(const RemoteObjectInvocation& invocation)
62 {
63     // For backward-compatibility, support invoking injected bundle methods before having done any load in the WebView.
64     if (m_launchInitialProcessIfNecessary)
65         m_launchInitialProcessIfNecessary();
66
67     if (auto* replyInfo = invocation.replyInfo()) {
68         ASSERT(!m_pendingReplies.contains(replyInfo->replyID));
69         m_pendingReplies.add(replyInfo->replyID, m_takeBackgroundActivityToken());
70     }
71
72     m_messageSender.send(Messages::RemoteObjectRegistry::InvokeMethod(invocation));
73 }
74
75 void RemoteObjectRegistry::sendReplyBlock(uint64_t replyID, const UserData& blockInvocation)
76 {
77     m_messageSender.send(Messages::RemoteObjectRegistry::CallReplyBlock(replyID, blockInvocation));
78 }
79
80 void RemoteObjectRegistry::sendUnusedReply(uint64_t replyID)
81 {
82     m_messageSender.send(Messages::RemoteObjectRegistry::ReleaseUnusedReplyBlock(replyID));
83 }
84
85 void RemoteObjectRegistry::invokeMethod(const RemoteObjectInvocation& invocation)
86 {
87     [m_remoteObjectRegistry _invokeMethod:invocation];
88 }
89
90 void RemoteObjectRegistry::callReplyBlock(uint64_t replyID, const UserData& blockInvocation)
91 {
92     bool wasRemoved = m_pendingReplies.remove(replyID);
93     ASSERT_UNUSED(wasRemoved, wasRemoved);
94
95     [m_remoteObjectRegistry _callReplyWithID:replyID blockInvocation:blockInvocation];
96 }
97
98 void RemoteObjectRegistry::releaseUnusedReplyBlock(uint64_t replyID)
99 {
100     bool wasRemoved = m_pendingReplies.remove(replyID);
101     ASSERT_UNUSED(wasRemoved, wasRemoved);
102
103     [m_remoteObjectRegistry _releaseReplyWithID:replyID];
104 }
105 } // namespace WebKit