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
37namespace WebKit {
38
39class AuxiliaryProcessProxy : ProcessLauncher::Client, public IPC::Connection::Client {
40 WTF_MAKE_NONCOPYABLE(AuxiliaryProcessProxy);
41
42protected:
43 explicit AuxiliaryProcessProxy(bool alwaysRunsAtBackgroundPriority = false);
44
45public:
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 IPC::Connection* connection() const
56 {
57 ASSERT(m_connection);
58 return m_connection.get();
59 }
60
61 bool hasConnection(const IPC::Connection& connection) const
62 {
63 return m_connection == &connection;
64 }
65
66 void addMessageReceiver(IPC::StringReference messageReceiverName, IPC::MessageReceiver&);
67 void addMessageReceiver(IPC::StringReference messageReceiverName, uint64_t destinationID, IPC::MessageReceiver&);
68 void removeMessageReceiver(IPC::StringReference messageReceiverName, uint64_t destinationID);
69 void removeMessageReceiver(IPC::StringReference messageReceiverName);
70
71 enum class State {
72 Launching,
73 Running,
74 Terminated,
75 };
76 State state() const;
77
78 ProcessID processIdentifier() const { return m_processLauncher ? m_processLauncher->processIdentifier() : 0; }
79
80 bool canSendMessage() const { return state() != State::Terminated;}
81 bool sendMessage(std::unique_ptr<IPC::Encoder>, OptionSet<IPC::SendOption>);
82
83 void shutDownProcess();
84
85 WebCore::ProcessIdentifier coreProcessIdentifier() const { return m_processIdentifier; }
86
87 void setProcessSuppressionEnabled(bool);
88
89protected:
90 // ProcessLauncher::Client
91 void didFinishLaunching(ProcessLauncher*, IPC::Connection::Identifier) override;
92
93 bool dispatchMessage(IPC::Connection&, IPC::Decoder&);
94 bool dispatchSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
95
96 virtual void getLaunchOptions(ProcessLauncher::LaunchOptions&);
97 virtual void platformGetLaunchOptions(ProcessLauncher::LaunchOptions&) { };
98
99private:
100 virtual void connectionWillOpen(IPC::Connection&);
101 virtual void processWillShutDown(IPC::Connection&) = 0;
102
103 Vector<std::pair<std::unique_ptr<IPC::Encoder>, OptionSet<IPC::SendOption>>> m_pendingMessages;
104 RefPtr<ProcessLauncher> m_processLauncher;
105 RefPtr<IPC::Connection> m_connection;
106 IPC::MessageReceiverMap m_messageReceiverMap;
107 bool m_alwaysRunsAtBackgroundPriority { false };
108 WebCore::ProcessIdentifier m_processIdentifier { WebCore::ProcessIdentifier::generate() };
109};
110
111template<typename T>
112bool AuxiliaryProcessProxy::send(T&& message, uint64_t destinationID, OptionSet<IPC::SendOption> sendOptions)
113{
114 COMPILE_ASSERT(!T::isSync, AsyncMessageExpected);
115
116 auto encoder = std::make_unique<IPC::Encoder>(T::receiverName(), T::name(), destinationID);
117 encoder->encode(message.arguments());
118
119 return sendMessage(WTFMove(encoder), sendOptions);
120}
121
122template<typename U>
123bool AuxiliaryProcessProxy::sendSync(U&& message, typename U::Reply&& reply, uint64_t destinationID, Seconds timeout, OptionSet<IPC::SendSyncOption> sendSyncOptions)
124{
125 COMPILE_ASSERT(U::isSync, SyncMessageExpected);
126
127 if (!m_connection)
128 return false;
129
130 TraceScope scope(SyncMessageStart, SyncMessageEnd);
131
132 return connection()->sendSync(std::forward<U>(message), WTFMove(reply), destinationID, timeout, sendSyncOptions);
133}
134
135template<typename T, typename... Args>
136void AuxiliaryProcessProxy::sendWithAsyncReply(T&& message, CompletionHandler<void(Args...)>&& completionHandler)
137{
138 if (!m_connection) {
139 T::cancelReply(WTFMove(completionHandler));
140 return;
141 }
142
143 connection()->sendWithAsyncReply(std::forward<T>(message), WTFMove(completionHandler));
144}
145
146} // namespace WebKit
147