Skip to content

Commit c73e05c

Browse files
committed
Bug 901050 - Part 1: Make IPC::Channel virtual, r=ipc-reviewers,jld
This change to how IPC::Channel is declared will make it more feasible for the specific channel implementation to be selected at runtime. This patch will use this to allow selecting the IPC::Channel backend using a pref on macOS. Differential Revision: https://phabricator.services.mozilla.com/D251392
1 parent b387d30 commit c73e05c

14 files changed

+187
-248
lines changed

ipc/chromium/moz.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ UNIFIED_SOURCES += [
2424
"src/base/time.cc",
2525
"src/base/timer.cc",
2626
"src/chrome/common/chrome_switches.cc",
27+
"src/chrome/common/ipc_channel.cc",
2728
"src/chrome/common/ipc_channel_utils.cc",
2829
"src/chrome/common/ipc_message.cc",
2930
"src/chrome/common/ipc_message_utils.cc",
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2+
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3+
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style license that can be
5+
// found in the LICENSE file.
6+
7+
#include "ipc_channel.h"
8+
9+
#include "base/message_loop.h"
10+
#include "mozilla/ipc/ProtocolUtils.h"
11+
12+
#ifdef XP_WIN
13+
# include "chrome/common/ipc_channel_win.h"
14+
#else
15+
# include "chrome/common/ipc_channel_posix.h"
16+
#endif
17+
18+
namespace IPC {
19+
20+
Channel::Channel()
21+
: chan_cap_("ChannelImpl::SendMutex",
22+
MessageLoopForIO::current()->SerialEventTarget()) {}
23+
24+
Channel::~Channel() = default;
25+
26+
already_AddRefed<Channel> Channel::Create(ChannelHandle pipe, Mode mode,
27+
base::ProcessId other_pid) {
28+
#ifdef XP_WIN
29+
return mozilla::MakeAndAddRef<ChannelWin>(std::move(pipe), mode, other_pid);
30+
#else
31+
return mozilla::MakeAndAddRef<ChannelPosix>(std::move(pipe), mode, other_pid);
32+
#endif
33+
}
34+
35+
bool Channel::CreateRawPipe(ChannelHandle* server, ChannelHandle* client) {
36+
#ifdef XP_WIN
37+
return ChannelWin::CreateRawPipe(server, client);
38+
#else
39+
return ChannelPosix::CreateRawPipe(server, client);
40+
#endif
41+
}
42+
43+
} // namespace IPC

ipc/chromium/src/chrome/common/ipc_channel.h

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <queue>
1212
#include "base/basictypes.h"
1313
#include "base/process.h"
14+
#include "mozilla/EventTargetAndLockCapability.h"
1415
#include "mozilla/UniquePtr.h"
1516
#include "mozilla/UniquePtrExtensions.h"
1617
#include "mozilla/WeakPtr.h"
@@ -29,10 +30,10 @@ class MessageWriter;
2930
//------------------------------------------------------------------------------
3031

3132
class Channel {
32-
// Security tests need access to the pipe handle.
33-
friend class ChannelTest;
34-
3533
public:
34+
NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DELETE_ON_EVENT_TARGET(
35+
Channel, IOThread().GetEventTarget());
36+
3637
// For channels which are created after initialization, handles to the pipe
3738
// endpoints may be passed around directly using IPC messages.
3839
using ChannelHandle = mozilla::UniqueFileHandle;
@@ -89,9 +90,11 @@ class Channel {
8990
// The Channel must be created and destroyed on the IO thread, and all
9091
// methods, unless otherwise noted, are only safe to call on the I/O thread.
9192
//
92-
Channel(ChannelHandle pipe, Mode mode, base::ProcessId other_pid);
93+
static already_AddRefed<Channel> Create(ChannelHandle pipe, Mode mode,
94+
base::ProcessId other_pid);
9395

94-
~Channel();
96+
Channel(const Channel&) = delete;
97+
Channel& operator=(const Channel&) = delete;
9598

9699
// Connect the pipe. On the server side, this will initiate
97100
// waiting for connections. On the client, it attempts to
@@ -101,10 +104,10 @@ class Channel {
101104
//
102105
// |listener| will receive a callback on the current thread for each newly
103106
// received message.
104-
bool Connect(Listener* listener);
107+
virtual bool Connect(Listener* listener) MOZ_EXCLUDES(SendMutex()) = 0;
105108

106109
// Close this Channel explicitly. May be called multiple times.
107-
void Close();
110+
virtual void Close() MOZ_EXCLUDES(SendMutex()) = 0;
108111

109112
// Send a message over the Channel to the listener on the other end.
110113
//
@@ -113,43 +116,60 @@ class Channel {
113116
//
114117
// If you Send() a message on a Close()'d channel, we delete the message
115118
// immediately.
116-
bool Send(mozilla::UniquePtr<Message> message);
119+
virtual bool Send(mozilla::UniquePtr<Message> message)
120+
MOZ_EXCLUDES(SendMutex()) = 0;
117121

118122
// Explicitly set the pid expected for the other side of this channel. This
119123
// will be used for logging, and on Windows may be used for transferring
120124
// handles between processes.
121125
//
122126
// If it is set this way, the "hello" message will be checked to ensure that
123127
// the same pid is reported.
124-
void SetOtherPid(base::ProcessId other_pid);
125-
126-
// IsClosed() is safe to call from any thread, but the value returned may
127-
// be out of date.
128-
bool IsClosed() const;
128+
virtual void SetOtherPid(base::ProcessId other_pid)
129+
MOZ_EXCLUDES(SendMutex()) = 0;
129130

130131
#if defined(XP_DARWIN)
131132
// Configure the mach task_t for the peer task.
132-
void SetOtherMachTask(task_t task);
133+
virtual void SetOtherMachTask(task_t task) MOZ_EXCLUDES(SendMutex()) = 0;
133134

134135
// Tell this pipe to accept mach ports. Exactly one side of the IPC connection
135136
// must be set as `MODE_SERVER` and that side will be responsible for
136137
// transferring the rights between processes.
137-
void StartAcceptingMachPorts(Mode mode);
138+
virtual void StartAcceptingMachPorts(Mode mode) MOZ_EXCLUDES(SendMutex()) = 0;
138139
#elif defined(XP_WIN)
139140
// Tell this pipe to accept handles. Exactly one side of the IPC connection
140141
// must be set as `MODE_SERVER`, and that side will be responsible for calling
141142
// `DuplicateHandle` to transfer the handle between processes.
142-
void StartAcceptingHandles(Mode mode);
143+
virtual void StartAcceptingHandles(Mode mode) MOZ_EXCLUDES(SendMutex()) = 0;
143144
#endif
144145

145146
// Create a new pair of pipe endpoints which can be used to establish a
146147
// native IPC::Channel connection.
147148
static bool CreateRawPipe(ChannelHandle* server, ChannelHandle* client);
148149

149-
private:
150-
// PIMPL to which all channel calls are delegated.
151-
class ChannelImpl;
152-
RefPtr<ChannelImpl> channel_impl_;
150+
protected:
151+
Channel();
152+
virtual ~Channel();
153+
154+
// Capability for members which may only be used on the IO thread. Generally
155+
// used for state related to receiving IPC messages.
156+
const mozilla::EventTargetCapability<nsISerialEventTarget>& IOThread() const
157+
MOZ_RETURN_CAPABILITY(chan_cap_.Target()) {
158+
return chan_cap_.Target();
159+
}
160+
161+
// Capability for members which may only be used with the send mutex held.
162+
// Generally used for state related to sending IPC messages.
163+
mozilla::Mutex& SendMutex() MOZ_RETURN_CAPABILITY(chan_cap_.Lock()) {
164+
return chan_cap_.Lock();
165+
}
166+
167+
// Compound capability of the IO thread and a Mutex. This can be used for
168+
// members which may be used immutably either on the IO thread or with the
169+
// send mutex held, but may only be modified if both on the IO thread, and
170+
// holding the send mutex.
171+
mozilla::EventTargetAndLockCapability<nsISerialEventTarget, mozilla::Mutex>
172+
chan_cap_;
153173

154174
enum {
155175
#if defined(XP_DARWIN)

0 commit comments

Comments
 (0)