[IOS] We should prevent WebProcess suspension while the UIProcess is waiting for...
[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 {
53 }
54
55
56 RemoteObjectRegistry::~RemoteObjectRegistry()
57 {
58 }
59
60 void RemoteObjectRegistry::sendInvocation(const RemoteObjectInvocation& invocation)
61 {
62     if (auto* replyInfo = invocation.replyInfo()) {
63         ASSERT(!m_pendingReplies.contains(replyInfo->replyID));
64         m_pendingReplies.add(replyInfo->replyID, m_takeBackgroundActivityToken());
65     }
66
67     m_messageSender.send(Messages::RemoteObjectRegistry::InvokeMethod(invocation));
68 }
69
70 void RemoteObjectRegistry::sendReplyBlock(uint64_t replyID, const UserData& blockInvocation)
71 {
72     m_messageSender.send(Messages::RemoteObjectRegistry::CallReplyBlock(replyID, blockInvocation));
73 }
74
75 void RemoteObjectRegistry::sendUnusedReply(uint64_t replyID)
76 {
77     m_messageSender.send(Messages::RemoteObjectRegistry::ReleaseUnusedReplyBlock(replyID));
78 }
79
80 void RemoteObjectRegistry::invokeMethod(const RemoteObjectInvocation& invocation)
81 {
82 #if WK_API_ENABLED
83     [m_remoteObjectRegistry _invokeMethod:invocation];
84 #endif
85 }
86
87 void RemoteObjectRegistry::callReplyBlock(uint64_t replyID, const UserData& blockInvocation)
88 {
89     bool wasRemoved = m_pendingReplies.remove(replyID);
90     ASSERT_UNUSED(wasRemoved, wasRemoved);
91
92 #if WK_API_ENABLED
93     [m_remoteObjectRegistry _callReplyWithID:replyID blockInvocation:blockInvocation];
94 #endif
95 }
96
97 void RemoteObjectRegistry::releaseUnusedReplyBlock(uint64_t replyID)
98 {
99     bool wasRemoved = m_pendingReplies.remove(replyID);
100     ASSERT_UNUSED(wasRemoved, wasRemoved);
101
102 #if WK_API_ENABLED
103     [m_remoteObjectRegistry _releaseReplyWithID:replyID];
104 #endif
105 }
106 } // namespace WebKit