2021-06-22 21:17:22 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_ipc_NodeChannel_h
|
|
|
|
#define mozilla_ipc_NodeChannel_h
|
|
|
|
|
|
|
|
#include "mojo/core/ports/node.h"
|
|
|
|
#include "mojo/core/ports/node_delegate.h"
|
Bug 1766377 - Fix some sign-compare warnings by using ProcessId more consistently. r=nika,necko-reviewers,kershaw
dom/media/ipc/RDDProcessManager.cpp(320,21): error: comparison of integers of different signs: 'base::ProcessId' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
gpuProcessPid != -1 ? gpuProcessPid : base::GetCurrentProcId();
~~~~~~~~~~~~~ ^ ~~
dom/media/ipc/RDDProcessManager.cpp(332,21): error: comparison of integers of different signs: 'base::ProcessId' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
if (gpuProcessPid != -1) {
~~~~~~~~~~~~~ ^ ~~
gfx/layers/ipc/SharedSurfacesParent.cpp(360,38): error: comparison of integers of different signs: 'base::ProcessId' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
if (!gpm || gpm->GPUProcessPid() != -1) {
~~~~~~~~~~~~~~~~~~~~ ^ ~~
ipc/glue/MessageChannel.cpp(2145,13): error: comparison of integers of different signs: 'int32_t' (aka 'int') and 'const base::ProcessId' (aka 'const unsigned long') [-Werror,-Wsign-compare]
if (pid != base::kInvalidProcessId &&
~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~
Differential Revision: https://phabricator.services.mozilla.com/D144688
2022-04-27 10:45:19 +03:00
|
|
|
#include "base/process.h"
|
2021-06-22 21:17:22 +03:00
|
|
|
#include "chrome/common/ipc_message.h"
|
|
|
|
#include "chrome/common/ipc_channel.h"
|
|
|
|
#include "mozilla/ipc/ProtocolUtils.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsTHashMap.h"
|
|
|
|
#include "mozilla/Queue.h"
|
|
|
|
#include "mozilla/DataMutex.h"
|
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
|
2022-05-05 18:39:24 +03:00
|
|
|
#ifdef FUZZING_SNAPSHOT
|
|
|
|
# include "mozilla/fuzzing/IPCFuzzController.h"
|
|
|
|
#endif
|
|
|
|
|
2021-06-22 21:17:22 +03:00
|
|
|
namespace mozilla::ipc {
|
|
|
|
|
|
|
|
class NodeController;
|
|
|
|
|
|
|
|
// Represents a live connection between our Node and a remote process. This
|
|
|
|
// object acts as an IPC::Channel listener and performs basic processing on
|
|
|
|
// messages as they're passed between processes.
|
|
|
|
|
|
|
|
class NodeChannel final : public IPC::Channel::Listener {
|
|
|
|
using NodeName = mojo::core::ports::NodeName;
|
|
|
|
using PortName = mojo::core::ports::PortName;
|
|
|
|
|
2022-05-05 18:39:24 +03:00
|
|
|
#ifdef FUZZING_SNAPSHOT
|
|
|
|
// Required because IPCFuzzController calls OnMessageReceived.
|
|
|
|
friend class mozilla::fuzzing::IPCFuzzController;
|
|
|
|
#endif
|
|
|
|
|
2021-06-22 21:17:22 +03:00
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DESTROY(NodeChannel, Destroy())
|
|
|
|
|
|
|
|
struct Introduction {
|
|
|
|
NodeName mName;
|
2022-02-09 02:53:45 +03:00
|
|
|
IPC::Channel::ChannelHandle mHandle;
|
|
|
|
IPC::Channel::Mode mMode;
|
Bug 1766377 - Fix some sign-compare warnings by using ProcessId more consistently. r=nika,necko-reviewers,kershaw
dom/media/ipc/RDDProcessManager.cpp(320,21): error: comparison of integers of different signs: 'base::ProcessId' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
gpuProcessPid != -1 ? gpuProcessPid : base::GetCurrentProcId();
~~~~~~~~~~~~~ ^ ~~
dom/media/ipc/RDDProcessManager.cpp(332,21): error: comparison of integers of different signs: 'base::ProcessId' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
if (gpuProcessPid != -1) {
~~~~~~~~~~~~~ ^ ~~
gfx/layers/ipc/SharedSurfacesParent.cpp(360,38): error: comparison of integers of different signs: 'base::ProcessId' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
if (!gpm || gpm->GPUProcessPid() != -1) {
~~~~~~~~~~~~~~~~~~~~ ^ ~~
ipc/glue/MessageChannel.cpp(2145,13): error: comparison of integers of different signs: 'int32_t' (aka 'int') and 'const base::ProcessId' (aka 'const unsigned long') [-Werror,-Wsign-compare]
if (pid != base::kInvalidProcessId &&
~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~
Differential Revision: https://phabricator.services.mozilla.com/D144688
2022-04-27 10:45:19 +03:00
|
|
|
base::ProcessId mMyPid = base::kInvalidProcessId;
|
|
|
|
base::ProcessId mOtherPid = base::kInvalidProcessId;
|
2021-06-22 21:17:22 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class Listener {
|
|
|
|
public:
|
|
|
|
virtual ~Listener() = default;
|
|
|
|
|
|
|
|
NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
|
|
|
|
|
|
|
|
virtual void OnEventMessage(const NodeName& aFromNode,
|
|
|
|
UniquePtr<IPC::Message> aMessage) = 0;
|
|
|
|
virtual void OnBroadcast(const NodeName& aFromNode,
|
|
|
|
UniquePtr<IPC::Message> aMessage) = 0;
|
|
|
|
virtual void OnIntroduce(const NodeName& aFromNode,
|
|
|
|
Introduction aIntroduction) = 0;
|
|
|
|
virtual void OnRequestIntroduction(const NodeName& aFromNode,
|
|
|
|
const NodeName& aName) = 0;
|
|
|
|
virtual void OnAcceptInvite(const NodeName& aFromNode,
|
|
|
|
const NodeName& aRealName,
|
|
|
|
const PortName& aInitialPort) = 0;
|
|
|
|
virtual void OnChannelError(const NodeName& aFromNode) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
NodeChannel(const NodeName& aName, UniquePtr<IPC::Channel> aChannel,
|
2022-05-10 20:45:42 +03:00
|
|
|
Listener* aListener,
|
|
|
|
base::ProcessId aPid = base::kInvalidProcessId);
|
2021-06-22 21:17:22 +03:00
|
|
|
|
|
|
|
// Send the given message over this peer channel link. May be called from any
|
|
|
|
// thread.
|
|
|
|
void SendEventMessage(UniquePtr<IPC::Message> aMessage);
|
|
|
|
|
|
|
|
// Ask the broker process to broadcast this message to every node. May be
|
|
|
|
// called from any thread.
|
|
|
|
void Broadcast(UniquePtr<IPC::Message> aMessage);
|
|
|
|
|
|
|
|
// Ask the broker process to introduce this node to another node with the
|
|
|
|
// given name. May be called from any thread.
|
|
|
|
void RequestIntroduction(const NodeName& aPeerName);
|
|
|
|
|
|
|
|
// Send an introduction to the target node. May be called from any thread.
|
|
|
|
void Introduce(Introduction aIntroduction);
|
|
|
|
|
|
|
|
void AcceptInvite(const NodeName& aRealName, const PortName& aInitialPort);
|
|
|
|
|
|
|
|
// The PID of the remote process, once known. May be called from any thread.
|
Bug 1766377 - Fix some sign-compare warnings by using ProcessId more consistently. r=nika,necko-reviewers,kershaw
dom/media/ipc/RDDProcessManager.cpp(320,21): error: comparison of integers of different signs: 'base::ProcessId' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
gpuProcessPid != -1 ? gpuProcessPid : base::GetCurrentProcId();
~~~~~~~~~~~~~ ^ ~~
dom/media/ipc/RDDProcessManager.cpp(332,21): error: comparison of integers of different signs: 'base::ProcessId' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
if (gpuProcessPid != -1) {
~~~~~~~~~~~~~ ^ ~~
gfx/layers/ipc/SharedSurfacesParent.cpp(360,38): error: comparison of integers of different signs: 'base::ProcessId' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
if (!gpm || gpm->GPUProcessPid() != -1) {
~~~~~~~~~~~~~~~~~~~~ ^ ~~
ipc/glue/MessageChannel.cpp(2145,13): error: comparison of integers of different signs: 'int32_t' (aka 'int') and 'const base::ProcessId' (aka 'const unsigned long') [-Werror,-Wsign-compare]
if (pid != base::kInvalidProcessId &&
~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~
Differential Revision: https://phabricator.services.mozilla.com/D144688
2022-04-27 10:45:19 +03:00
|
|
|
base::ProcessId OtherPid() const { return mOtherPid; }
|
2021-06-22 21:17:22 +03:00
|
|
|
|
|
|
|
// Start communicating with the remote process using this NodeChannel. MUST BE
|
|
|
|
// CALLED FROM THE IO THREAD.
|
|
|
|
void Start(bool aCallConnect = true);
|
|
|
|
|
|
|
|
// Stop communicating with the remote process using this NodeChannel, MUST BE
|
|
|
|
// CALLED FROM THE IO THREAD.
|
|
|
|
void Close();
|
|
|
|
|
|
|
|
// Only ever called by NodeController to update the name after an invite has
|
|
|
|
// completed. MUST BE CALLED FROM THE IO THREAD.
|
|
|
|
void SetName(const NodeName& aNewName) { mName = aNewName; }
|
|
|
|
|
2022-05-05 18:39:24 +03:00
|
|
|
#ifdef FUZZING_SNAPSHOT
|
|
|
|
const NodeName& GetName() { return mName; }
|
|
|
|
#endif
|
|
|
|
|
2021-11-23 19:15:21 +03:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
// Called by the GeckoChildProcessHost to provide the task_t for the peer
|
|
|
|
// process. MUST BE CALLED FROM THE IO THREAD.
|
|
|
|
void SetMachTaskPort(task_t aTask);
|
|
|
|
#endif
|
|
|
|
|
2021-06-22 21:17:22 +03:00
|
|
|
private:
|
|
|
|
~NodeChannel();
|
|
|
|
|
|
|
|
void Destroy();
|
|
|
|
void FinalDestroy();
|
|
|
|
|
|
|
|
// Update the known PID for the remote process. IO THREAD ONLY
|
Bug 1766377 - Fix some sign-compare warnings by using ProcessId more consistently. r=nika,necko-reviewers,kershaw
dom/media/ipc/RDDProcessManager.cpp(320,21): error: comparison of integers of different signs: 'base::ProcessId' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
gpuProcessPid != -1 ? gpuProcessPid : base::GetCurrentProcId();
~~~~~~~~~~~~~ ^ ~~
dom/media/ipc/RDDProcessManager.cpp(332,21): error: comparison of integers of different signs: 'base::ProcessId' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
if (gpuProcessPid != -1) {
~~~~~~~~~~~~~ ^ ~~
gfx/layers/ipc/SharedSurfacesParent.cpp(360,38): error: comparison of integers of different signs: 'base::ProcessId' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
if (!gpm || gpm->GPUProcessPid() != -1) {
~~~~~~~~~~~~~~~~~~~~ ^ ~~
ipc/glue/MessageChannel.cpp(2145,13): error: comparison of integers of different signs: 'int32_t' (aka 'int') and 'const base::ProcessId' (aka 'const unsigned long') [-Werror,-Wsign-compare]
if (pid != base::kInvalidProcessId &&
~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~
Differential Revision: https://phabricator.services.mozilla.com/D144688
2022-04-27 10:45:19 +03:00
|
|
|
void SetOtherPid(base::ProcessId aNewPid);
|
2021-06-22 21:17:22 +03:00
|
|
|
|
|
|
|
void SendMessage(UniquePtr<IPC::Message> aMessage);
|
|
|
|
void DoSendMessage(UniquePtr<IPC::Message> aMessage);
|
|
|
|
|
|
|
|
// IPC::Channel::Listener implementation
|
2022-05-11 00:37:25 +03:00
|
|
|
void OnMessageReceived(UniquePtr<IPC::Message> aMessage) override;
|
Bug 1766377 - Fix some sign-compare warnings by using ProcessId more consistently. r=nika,necko-reviewers,kershaw
dom/media/ipc/RDDProcessManager.cpp(320,21): error: comparison of integers of different signs: 'base::ProcessId' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
gpuProcessPid != -1 ? gpuProcessPid : base::GetCurrentProcId();
~~~~~~~~~~~~~ ^ ~~
dom/media/ipc/RDDProcessManager.cpp(332,21): error: comparison of integers of different signs: 'base::ProcessId' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
if (gpuProcessPid != -1) {
~~~~~~~~~~~~~ ^ ~~
gfx/layers/ipc/SharedSurfacesParent.cpp(360,38): error: comparison of integers of different signs: 'base::ProcessId' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
if (!gpm || gpm->GPUProcessPid() != -1) {
~~~~~~~~~~~~~~~~~~~~ ^ ~~
ipc/glue/MessageChannel.cpp(2145,13): error: comparison of integers of different signs: 'int32_t' (aka 'int') and 'const base::ProcessId' (aka 'const unsigned long') [-Werror,-Wsign-compare]
if (pid != base::kInvalidProcessId &&
~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~
Differential Revision: https://phabricator.services.mozilla.com/D144688
2022-04-27 10:45:19 +03:00
|
|
|
void OnChannelConnected(base::ProcessId aPeerPid) override;
|
2021-06-22 21:17:22 +03:00
|
|
|
void OnChannelError() override;
|
|
|
|
|
|
|
|
// NOTE: This strong reference will create a reference cycle between the
|
|
|
|
// listener and the NodeChannel while it is in use. The Listener must clear
|
|
|
|
// its reference to the NodeChannel to avoid leaks before shutdown.
|
|
|
|
const RefPtr<Listener> mListener;
|
|
|
|
|
|
|
|
// The apparent name of this Node. This may change during the invite process
|
|
|
|
// while waiting for the remote node name to be communicated to us.
|
|
|
|
|
|
|
|
// WARNING: This must only be accessed on the IO thread.
|
|
|
|
NodeName mName;
|
|
|
|
|
|
|
|
// NOTE: This won't change once the connection has been established, but may
|
|
|
|
// be `-1` until then. This will only be written to on the IO thread, but may
|
|
|
|
// be read from other threads.
|
Bug 1766377 - Fix some sign-compare warnings by using ProcessId more consistently. r=nika,necko-reviewers,kershaw
dom/media/ipc/RDDProcessManager.cpp(320,21): error: comparison of integers of different signs: 'base::ProcessId' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
gpuProcessPid != -1 ? gpuProcessPid : base::GetCurrentProcId();
~~~~~~~~~~~~~ ^ ~~
dom/media/ipc/RDDProcessManager.cpp(332,21): error: comparison of integers of different signs: 'base::ProcessId' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
if (gpuProcessPid != -1) {
~~~~~~~~~~~~~ ^ ~~
gfx/layers/ipc/SharedSurfacesParent.cpp(360,38): error: comparison of integers of different signs: 'base::ProcessId' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
if (!gpm || gpm->GPUProcessPid() != -1) {
~~~~~~~~~~~~~~~~~~~~ ^ ~~
ipc/glue/MessageChannel.cpp(2145,13): error: comparison of integers of different signs: 'int32_t' (aka 'int') and 'const base::ProcessId' (aka 'const unsigned long') [-Werror,-Wsign-compare]
if (pid != base::kInvalidProcessId &&
~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~
Differential Revision: https://phabricator.services.mozilla.com/D144688
2022-04-27 10:45:19 +03:00
|
|
|
std::atomic<base::ProcessId> mOtherPid;
|
2021-06-22 21:17:22 +03:00
|
|
|
|
|
|
|
// WARNING: This must only be accessed on the IO thread.
|
|
|
|
mozilla::UniquePtr<IPC::Channel> mChannel;
|
|
|
|
|
|
|
|
// WARNING: This must only be accessed on the IO thread.
|
|
|
|
bool mClosed = false;
|
|
|
|
|
2022-05-05 18:39:24 +03:00
|
|
|
#ifdef FUZZING_SNAPSHOT
|
|
|
|
// WARNING: This must only be accessed on the IO thread.
|
|
|
|
bool mBlockSendRecv = false;
|
|
|
|
#endif
|
|
|
|
|
2021-06-22 21:17:22 +03:00
|
|
|
// WARNING: Must only be accessed on the IO thread.
|
2021-06-26 00:30:49 +03:00
|
|
|
WeakPtr<IPC::Channel::Listener> mExistingListener;
|
2021-06-22 21:17:22 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla::ipc
|
|
|
|
|
|
|
|
#endif
|