1 | /* |
2 | * Copyright (C) 2012-2019 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 | #pragma once |
27 | |
28 | #include "Connection.h" |
29 | #include "MessageReceiverMap.h" |
30 | #include "ProcessLauncher.h" |
31 | |
32 | #include <WebCore/ProcessIdentifier.h> |
33 | #include <wtf/ProcessID.h> |
34 | #include <wtf/SystemTracing.h> |
35 | #include <wtf/ThreadSafeRefCounted.h> |
36 | |
37 | namespace WebKit { |
38 | |
39 | class AuxiliaryProcessProxy : ProcessLauncher::Client, public IPC::Connection::Client { |
40 | WTF_MAKE_NONCOPYABLE(AuxiliaryProcessProxy); |
41 | |
42 | protected: |
43 | explicit AuxiliaryProcessProxy(bool alwaysRunsAtBackgroundPriority = false); |
44 | |
45 | public: |
46 | virtual ~AuxiliaryProcessProxy(); |
47 | |
48 | void connect(); |
49 | void terminate(); |
50 | |
51 | template<typename T> bool send(T&& message, uint64_t destinationID, OptionSet<IPC::SendOption> sendOptions = { }); |
52 | template<typename T> bool sendSync(T&& message, typename T::Reply&&, uint64_t destinationID, Seconds timeout = 1_s, OptionSet<IPC::SendSyncOption> sendSyncOptions = { }); |
53 | template<typename T, typename... Args> void sendWithAsyncReply(T&&, CompletionHandler<void(Args...)>&&); |
54 | |
55 | template<typename T, typename U> |
56 | bool send(T&& message, ObjectIdentifier<U> destinationID, OptionSet<IPC::SendOption> sendOptions = { }) |
57 | { |
58 | return send<T>(WTFMove(message), destinationID.toUInt64(), sendOptions); |
59 | } |
60 | |
61 | template<typename T, typename U> |
62 | bool sendSync(T&& message, typename T::Reply&& reply, ObjectIdentifier<U> destinationID, Seconds timeout = 1_s, OptionSet<IPC::SendSyncOption> sendSyncOptions = { }) |
63 | { |
64 | return sendSync<T>(WTFMove(message), WTFMove(reply), destinationID.toUInt64(), timeout, sendSyncOptions); |
65 | } |
66 | |
67 | IPC::Connection* connection() const |
68 | { |
69 | ASSERT(m_connection); |
70 | return m_connection.get(); |
71 | } |
72 | |
73 | bool hasConnection(const IPC::Connection& connection) const |
74 | { |
75 | return m_connection == &connection; |
76 | } |
77 | |
78 | void addMessageReceiver(IPC::StringReference messageReceiverName, IPC::MessageReceiver&); |
79 | void addMessageReceiver(IPC::StringReference messageReceiverName, uint64_t destinationID, IPC::MessageReceiver&); |
80 | void removeMessageReceiver(IPC::StringReference messageReceiverName, uint64_t destinationID); |
81 | void removeMessageReceiver(IPC::StringReference messageReceiverName); |
82 | |
83 | template <typename T> |
84 | void addMessageReceiver(IPC::StringReference messageReceiverName, ObjectIdentifier<T> destinationID, IPC::MessageReceiver& receiver) |
85 | { |
86 | addMessageReceiver(messageReceiverName, destinationID.toUInt64(), receiver); |
87 | } |
88 | |
89 | template <typename T> |
90 | void removeMessageReceiver(IPC::StringReference messageReceiverName, ObjectIdentifier<T> destinationID) |
91 | { |
92 | removeMessageReceiver(messageReceiverName, destinationID.toUInt64()); |
93 | } |
94 | |
95 | enum class State { |
96 | Launching, |
97 | Running, |
98 | Terminated, |
99 | }; |
100 | State state() const; |
101 | |
102 | ProcessID processIdentifier() const { return m_processLauncher ? m_processLauncher->processIdentifier() : 0; } |
103 | |
104 | bool canSendMessage() const { return state() != State::Terminated;} |
105 | bool sendMessage(std::unique_ptr<IPC::Encoder>, OptionSet<IPC::SendOption>); |
106 | |
107 | void shutDownProcess(); |
108 | |
109 | WebCore::ProcessIdentifier coreProcessIdentifier() const { return m_processIdentifier; } |
110 | |
111 | void setProcessSuppressionEnabled(bool); |
112 | |
113 | protected: |
114 | // ProcessLauncher::Client |
115 | void didFinishLaunching(ProcessLauncher*, IPC::Connection::Identifier) override; |
116 | |
117 | bool dispatchMessage(IPC::Connection&, IPC::Decoder&); |
118 | bool dispatchSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&); |
119 | |
120 | virtual void getLaunchOptions(ProcessLauncher::LaunchOptions&); |
121 | virtual void platformGetLaunchOptions(ProcessLauncher::LaunchOptions&) { }; |
122 | |
123 | private: |
124 | virtual void connectionWillOpen(IPC::Connection&); |
125 | virtual void processWillShutDown(IPC::Connection&) = 0; |
126 | |
127 | Vector<std::pair<std::unique_ptr<IPC::Encoder>, OptionSet<IPC::SendOption>>> m_pendingMessages; |
128 | RefPtr<ProcessLauncher> m_processLauncher; |
129 | RefPtr<IPC::Connection> m_connection; |
130 | IPC::MessageReceiverMap m_messageReceiverMap; |
131 | bool m_alwaysRunsAtBackgroundPriority { false }; |
132 | WebCore::ProcessIdentifier m_processIdentifier { WebCore::ProcessIdentifier::generate() }; |
133 | }; |
134 | |
135 | template<typename T> |
136 | bool AuxiliaryProcessProxy::send(T&& message, uint64_t destinationID, OptionSet<IPC::SendOption> sendOptions) |
137 | { |
138 | COMPILE_ASSERT(!T::isSync, AsyncMessageExpected); |
139 | |
140 | auto encoder = std::make_unique<IPC::Encoder>(T::receiverName(), T::name(), destinationID); |
141 | encoder->encode(message.arguments()); |
142 | |
143 | return sendMessage(WTFMove(encoder), sendOptions); |
144 | } |
145 | |
146 | template<typename U> |
147 | bool AuxiliaryProcessProxy::sendSync(U&& message, typename U::Reply&& reply, uint64_t destinationID, Seconds timeout, OptionSet<IPC::SendSyncOption> sendSyncOptions) |
148 | { |
149 | COMPILE_ASSERT(U::isSync, SyncMessageExpected); |
150 | |
151 | if (!m_connection) |
152 | return false; |
153 | |
154 | TraceScope scope(SyncMessageStart, SyncMessageEnd); |
155 | |
156 | return connection()->sendSync(std::forward<U>(message), WTFMove(reply), destinationID, timeout, sendSyncOptions); |
157 | } |
158 | |
159 | template<typename T, typename... Args> |
160 | void AuxiliaryProcessProxy::sendWithAsyncReply(T&& message, CompletionHandler<void(Args...)>&& completionHandler) |
161 | { |
162 | if (!m_connection) { |
163 | T::cancelReply(WTFMove(completionHandler)); |
164 | return; |
165 | } |
166 | |
167 | connection()->sendWithAsyncReply(std::forward<T>(message), WTFMove(completionHandler)); |
168 | } |
169 | |
170 | } // namespace WebKit |
171 | |