2013-09-28 01:42:08 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: sw=4 ts=4 et :
|
|
|
|
*/
|
|
|
|
/* 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 ipc_glue_MessageChannel_h
|
|
|
|
#define ipc_glue_MessageChannel_h 1
|
|
|
|
|
|
|
|
#include "base/basictypes.h"
|
|
|
|
#include "base/message_loop.h"
|
|
|
|
|
2014-08-21 18:13:23 +00:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2013-09-28 01:42:08 +00:00
|
|
|
#include "mozilla/Monitor.h"
|
2014-01-10 20:58:20 +00:00
|
|
|
#include "mozilla/Vector.h"
|
2015-07-21 07:21:51 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include "mozilla/ipc/Neutering.h"
|
|
|
|
#endif // defined(OS_WIN)
|
2013-09-28 01:42:08 +00:00
|
|
|
#include "mozilla/ipc/Transport.h"
|
2016-07-04 05:36:34 +00:00
|
|
|
#if defined(MOZ_CRASHREPORTER) && defined(OS_WIN)
|
|
|
|
#include "mozilla/mozalloc_oom.h"
|
|
|
|
#include "nsExceptionHandler.h"
|
|
|
|
#endif
|
2013-09-28 01:42:08 +00:00
|
|
|
#include "MessageLink.h"
|
|
|
|
|
|
|
|
#include <deque>
|
2016-11-28 16:03:53 +00:00
|
|
|
#include <functional>
|
2013-09-28 01:42:08 +00:00
|
|
|
#include <stack>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
|
|
|
class MessageChannel;
|
2016-10-30 18:26:40 +00:00
|
|
|
class IToplevelProtocol;
|
2013-09-28 01:42:08 +00:00
|
|
|
|
|
|
|
class RefCountedMonitor : public Monitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RefCountedMonitor()
|
|
|
|
: Monitor("mozilla.ipc.MessageChannel.mMonitor")
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RefCountedMonitor)
|
2014-06-19 02:29:00 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
~RefCountedMonitor() {}
|
2013-09-28 01:42:08 +00:00
|
|
|
};
|
|
|
|
|
2016-01-23 01:45:43 +00:00
|
|
|
enum class SyncSendError {
|
|
|
|
SendSuccess,
|
|
|
|
PreviousTimeout,
|
|
|
|
SendingCPOWWhileDispatchingSync,
|
|
|
|
SendingCPOWWhileDispatchingUrgent,
|
|
|
|
NotConnectedBeforeSend,
|
|
|
|
DisconnectedDuringSend,
|
|
|
|
CancelledBeforeSend,
|
|
|
|
CancelledAfterSend,
|
|
|
|
TimedOut,
|
|
|
|
ReplyError,
|
|
|
|
};
|
|
|
|
|
2016-10-30 18:26:40 +00:00
|
|
|
enum ChannelState {
|
|
|
|
ChannelClosed,
|
|
|
|
ChannelOpening,
|
|
|
|
ChannelConnected,
|
|
|
|
ChannelTimeout,
|
|
|
|
ChannelClosing,
|
|
|
|
ChannelError
|
|
|
|
};
|
|
|
|
|
2016-02-23 23:36:06 +00:00
|
|
|
class AutoEnterTransaction;
|
|
|
|
|
2013-09-28 01:42:08 +00:00
|
|
|
class MessageChannel : HasResultCodes
|
|
|
|
{
|
|
|
|
friend class ProcessLink;
|
|
|
|
friend class ThreadLink;
|
|
|
|
|
2014-01-10 20:58:31 +00:00
|
|
|
class CxxStackFrame;
|
|
|
|
class InterruptFrame;
|
|
|
|
|
2013-09-28 01:42:08 +00:00
|
|
|
typedef mozilla::Monitor Monitor;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static const int32_t kNoTimeout;
|
|
|
|
|
|
|
|
typedef IPC::Message Message;
|
2016-04-27 23:21:49 +00:00
|
|
|
typedef IPC::MessageInfo MessageInfo;
|
2013-09-28 01:42:08 +00:00
|
|
|
typedef mozilla::ipc::Transport Transport;
|
|
|
|
|
2016-10-30 18:26:40 +00:00
|
|
|
explicit MessageChannel(IToplevelProtocol *aListener);
|
2013-09-28 01:42:08 +00:00
|
|
|
~MessageChannel();
|
|
|
|
|
2016-11-15 03:26:00 +00:00
|
|
|
IToplevelProtocol *Listener() const {
|
|
|
|
return mListener;
|
|
|
|
}
|
|
|
|
|
2013-09-28 01:42:08 +00:00
|
|
|
// "Open" from the perspective of the transport layer; the underlying
|
|
|
|
// socketpair/pipe should already be created.
|
|
|
|
//
|
2014-08-15 17:12:37 +00:00
|
|
|
// Returns true if the transport layer was successfully connected,
|
2013-09-28 01:42:08 +00:00
|
|
|
// i.e., mChannelState == ChannelConnected.
|
|
|
|
bool Open(Transport* aTransport, MessageLoop* aIOLoop=0, Side aSide=UnknownSide);
|
|
|
|
|
|
|
|
// "Open" a connection to another thread in the same process.
|
|
|
|
//
|
2014-08-15 17:12:37 +00:00
|
|
|
// Returns true if the transport layer was successfully connected,
|
2013-09-28 01:42:08 +00:00
|
|
|
// i.e., mChannelState == ChannelConnected.
|
|
|
|
//
|
|
|
|
// For more details on the process of opening a channel between
|
|
|
|
// threads, see the extended comment on this function
|
|
|
|
// in MessageChannel.cpp.
|
|
|
|
bool Open(MessageChannel *aTargetChan, MessageLoop *aTargetLoop, Side aSide);
|
|
|
|
|
|
|
|
// Close the underlying transport channel.
|
|
|
|
void Close();
|
|
|
|
|
|
|
|
// Force the channel to behave as if a channel error occurred. Valid
|
|
|
|
// for process links only, not thread links.
|
|
|
|
void CloseWithError();
|
|
|
|
|
2014-12-19 01:35:44 +00:00
|
|
|
void CloseWithTimeout();
|
|
|
|
|
2014-03-04 17:17:01 +00:00
|
|
|
void SetAbortOnError(bool abort)
|
|
|
|
{
|
2015-02-13 19:10:56 +00:00
|
|
|
mAbortOnError = abort;
|
2014-03-04 17:17:01 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 01:51:34 +00:00
|
|
|
// Call aInvoke for each pending message until it returns false.
|
2016-03-14 15:22:32 +00:00
|
|
|
// XXX: You must get permission from an IPC peer to use this function
|
|
|
|
// since it requires custom deserialization and re-orders events.
|
2016-11-28 16:03:53 +00:00
|
|
|
void PeekMessages(std::function<bool(const Message& aMsg)> aInvoke);
|
2016-03-14 15:22:32 +00:00
|
|
|
|
2014-08-15 17:12:37 +00:00
|
|
|
// Misc. behavioral traits consumers can request for this channel
|
|
|
|
enum ChannelFlags {
|
|
|
|
REQUIRE_DEFAULT = 0,
|
|
|
|
// Windows: if this channel operates on the UI thread, indicates
|
|
|
|
// WindowsMessageLoop code should enable deferred native message
|
|
|
|
// handling to prevent deadlocks. Should only be used for protocols
|
|
|
|
// that manage child processes which might create native UI, like
|
|
|
|
// plugins.
|
2016-08-03 17:54:35 +00:00
|
|
|
REQUIRE_DEFERRED_MESSAGE_PROTECTION = 1 << 0,
|
|
|
|
// Windows: When this flag is specified, any wait that occurs during
|
|
|
|
// synchronous IPC will be alertable, thus allowing a11y code in the
|
|
|
|
// chrome process to reenter content while content is waiting on a
|
|
|
|
// synchronous call.
|
|
|
|
REQUIRE_A11Y_REENTRY = 1 << 1,
|
2014-08-15 17:12:37 +00:00
|
|
|
};
|
|
|
|
void SetChannelFlags(ChannelFlags aFlags) { mFlags = aFlags; }
|
|
|
|
ChannelFlags GetChannelFlags() { return mFlags; }
|
|
|
|
|
2013-09-28 01:42:08 +00:00
|
|
|
// Asynchronously send a message to the other side of the channel
|
|
|
|
bool Send(Message* aMsg);
|
|
|
|
|
|
|
|
// Asynchronously deliver a message back to this side of the
|
|
|
|
// channel
|
|
|
|
bool Echo(Message* aMsg);
|
|
|
|
|
|
|
|
// Synchronously send |msg| (i.e., wait for |reply|)
|
|
|
|
bool Send(Message* aMsg, Message* aReply);
|
|
|
|
|
2013-10-01 00:27:45 +00:00
|
|
|
// Make an Interrupt call to the other side of the channel
|
2013-09-28 01:42:08 +00:00
|
|
|
bool Call(Message* aMsg, Message* aReply);
|
|
|
|
|
2014-12-29 23:12:47 +00:00
|
|
|
// Wait until a message is received
|
|
|
|
bool WaitForIncomingMessage();
|
|
|
|
|
2014-01-31 18:43:44 +00:00
|
|
|
bool CanSend() const;
|
|
|
|
|
2016-01-23 01:45:43 +00:00
|
|
|
// If sending a sync message returns an error, this function gives a more
|
|
|
|
// descriptive error message.
|
|
|
|
SyncSendError LastSendError() const {
|
|
|
|
AssertWorkerThread();
|
|
|
|
return mLastSendError;
|
|
|
|
}
|
|
|
|
|
2015-07-15 19:47:14 +00:00
|
|
|
// Currently only for debugging purposes, doesn't aquire mMonitor.
|
|
|
|
ChannelState GetChannelState__TotallyRacy() const {
|
|
|
|
return mChannelState;
|
|
|
|
}
|
|
|
|
|
2013-09-28 01:42:08 +00:00
|
|
|
void SetReplyTimeoutMs(int32_t aTimeoutMs);
|
|
|
|
|
|
|
|
bool IsOnCxxStack() const {
|
|
|
|
return !mCxxStackFrames.empty();
|
|
|
|
}
|
|
|
|
|
2016-02-23 23:36:06 +00:00
|
|
|
bool IsInTransaction() const;
|
2015-10-07 18:13:48 +00:00
|
|
|
void CancelCurrentTransaction();
|
2015-06-19 18:32:35 +00:00
|
|
|
|
2015-02-19 06:05:54 +00:00
|
|
|
/**
|
|
|
|
* This function is used by hang annotation code to determine which IPDL
|
|
|
|
* actor is highest in the call stack at the time of the hang. It should
|
|
|
|
* be called from the main thread when a sync or intr message is about to
|
|
|
|
* be sent.
|
|
|
|
*/
|
|
|
|
int32_t GetTopmostMessageRoutingId() const;
|
|
|
|
|
2013-09-28 01:42:08 +00:00
|
|
|
// Unsound_IsClosed and Unsound_NumQueuedMessages are safe to call from any
|
|
|
|
// thread, but they make no guarantees about whether you'll get an
|
|
|
|
// up-to-date value; the values are written on one thread and read without
|
|
|
|
// locking, on potentially different threads. Thus you should only use
|
|
|
|
// them when you don't particularly care about getting a recent value (e.g.
|
|
|
|
// in a memory report).
|
|
|
|
bool Unsound_IsClosed() const {
|
|
|
|
return mLink ? mLink->Unsound_IsClosed() : true;
|
|
|
|
}
|
|
|
|
uint32_t Unsound_NumQueuedMessages() const {
|
|
|
|
return mLink ? mLink->Unsound_NumQueuedMessages() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsPumpingMessages() {
|
|
|
|
return sIsPumpingMessages;
|
|
|
|
}
|
|
|
|
static void SetIsPumpingMessages(bool aIsPumping) {
|
|
|
|
sIsPumpingMessages = aIsPumping;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef OS_WIN
|
|
|
|
struct MOZ_STACK_CLASS SyncStackFrame
|
|
|
|
{
|
2013-10-01 00:27:45 +00:00
|
|
|
SyncStackFrame(MessageChannel* channel, bool interrupt);
|
2013-09-28 01:42:08 +00:00
|
|
|
~SyncStackFrame();
|
|
|
|
|
2013-10-01 00:27:45 +00:00
|
|
|
bool mInterrupt;
|
2013-09-28 01:42:08 +00:00
|
|
|
bool mSpinNestedEvents;
|
|
|
|
bool mListenerNotified;
|
|
|
|
MessageChannel* mChannel;
|
|
|
|
|
|
|
|
// The previous stack frame for this channel.
|
|
|
|
SyncStackFrame* mPrev;
|
|
|
|
|
|
|
|
// The previous stack frame on any channel.
|
|
|
|
SyncStackFrame* mStaticPrev;
|
|
|
|
};
|
|
|
|
friend struct MessageChannel::SyncStackFrame;
|
|
|
|
|
|
|
|
static bool IsSpinLoopActive() {
|
|
|
|
for (SyncStackFrame* frame = sStaticTopFrame; frame; frame = frame->mPrev) {
|
|
|
|
if (frame->mSpinNestedEvents)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// The deepest sync stack frame for this channel.
|
|
|
|
SyncStackFrame* mTopFrame;
|
|
|
|
|
2014-05-18 03:16:51 +00:00
|
|
|
bool mIsSyncWaitingOnNonMainThread;
|
|
|
|
|
2013-09-28 01:42:08 +00:00
|
|
|
// The deepest sync stack frame on any channel.
|
|
|
|
static SyncStackFrame* sStaticTopFrame;
|
|
|
|
|
|
|
|
public:
|
2013-10-01 00:27:45 +00:00
|
|
|
void ProcessNativeEventsInInterruptCall();
|
2013-09-28 01:42:08 +00:00
|
|
|
static void NotifyGeckoEventDispatch();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void SpinInternalEventLoop();
|
2016-08-03 17:54:35 +00:00
|
|
|
#if defined(ACCESSIBILITY)
|
|
|
|
bool WaitForSyncNotifyWithA11yReentry();
|
|
|
|
#endif // defined(ACCESSIBILITY)
|
|
|
|
#endif // defined(OS_WIN)
|
2013-09-28 01:42:08 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void CommonThreadOpenInit(MessageChannel *aTargetChan, Side aSide);
|
|
|
|
void OnOpenAsSlave(MessageChannel *aTargetChan, Side aSide);
|
|
|
|
|
|
|
|
void PostErrorNotifyTask();
|
|
|
|
void OnNotifyMaybeChannelError();
|
2015-04-15 23:43:21 +00:00
|
|
|
void ReportConnectionError(const char* aChannelName, Message* aMsg = nullptr) const;
|
2013-11-14 20:24:18 +00:00
|
|
|
void ReportMessageRouteError(const char* channelName) const;
|
2014-08-22 16:23:01 +00:00
|
|
|
bool MaybeHandleError(Result code, const Message& aMsg, const char* channelName);
|
2013-09-28 01:42:08 +00:00
|
|
|
|
|
|
|
void Clear();
|
|
|
|
|
|
|
|
// Send OnChannelConnected notification to listeners.
|
2014-08-21 18:13:23 +00:00
|
|
|
void DispatchOnChannelConnected();
|
2013-09-28 01:42:08 +00:00
|
|
|
|
2013-10-01 00:27:45 +00:00
|
|
|
bool InterruptEventOccurred();
|
2014-12-29 23:12:47 +00:00
|
|
|
bool HasPendingEvents();
|
2013-09-28 01:42:08 +00:00
|
|
|
|
2016-02-23 23:36:06 +00:00
|
|
|
void ProcessPendingRequests(AutoEnterTransaction& aTransaction);
|
2016-05-16 21:02:43 +00:00
|
|
|
bool ProcessPendingRequest(Message &&aUrgent);
|
2013-10-01 16:15:03 +00:00
|
|
|
|
2013-09-28 01:42:08 +00:00
|
|
|
void MaybeUndeferIncall();
|
|
|
|
void EnqueuePendingMessages();
|
|
|
|
|
|
|
|
// Dispatches an incoming message to its appropriate handler.
|
2016-05-16 21:02:43 +00:00
|
|
|
void DispatchMessage(Message &&aMsg);
|
2013-09-28 01:42:08 +00:00
|
|
|
|
|
|
|
// DispatchMessage will route to one of these functions depending on the
|
|
|
|
// protocol type of the message.
|
2015-06-30 21:48:29 +00:00
|
|
|
void DispatchSyncMessage(const Message &aMsg, Message*& aReply);
|
2013-09-28 01:42:08 +00:00
|
|
|
void DispatchUrgentMessage(const Message &aMsg);
|
|
|
|
void DispatchAsyncMessage(const Message &aMsg);
|
2013-10-01 16:15:03 +00:00
|
|
|
void DispatchRPCMessage(const Message &aMsg);
|
2016-05-16 21:02:43 +00:00
|
|
|
void DispatchInterruptMessage(Message &&aMsg, size_t aStackDepth);
|
2013-09-28 01:42:08 +00:00
|
|
|
|
|
|
|
// Return true if the wait ended because a notification was received.
|
|
|
|
//
|
|
|
|
// Return false if the time elapsed from when we started the process of
|
|
|
|
// waiting until afterwards exceeded the currently allotted timeout.
|
|
|
|
// That *DOES NOT* mean false => "no event" (== timeout); there are many
|
|
|
|
// circumstances that could cause the measured elapsed time to exceed the
|
|
|
|
// timeout EVEN WHEN we were notified.
|
|
|
|
//
|
|
|
|
// So in sum: true is a meaningful return value; false isn't,
|
|
|
|
// necessarily.
|
2015-10-07 21:38:08 +00:00
|
|
|
bool WaitForSyncNotify(bool aHandleWindowsMessages);
|
2013-10-01 00:27:45 +00:00
|
|
|
bool WaitForInterruptNotify();
|
2013-09-28 01:42:08 +00:00
|
|
|
|
|
|
|
bool WaitResponse(bool aWaitTimedOut);
|
|
|
|
|
|
|
|
bool ShouldContinueFromTimeout();
|
|
|
|
|
2016-01-23 02:02:27 +00:00
|
|
|
void EndTimeout();
|
2016-01-23 01:52:23 +00:00
|
|
|
void CancelTransaction(int transaction);
|
2015-06-19 18:32:35 +00:00
|
|
|
|
2016-10-15 18:47:14 +00:00
|
|
|
void RepostAllMessages();
|
|
|
|
|
2013-09-28 01:42:08 +00:00
|
|
|
// The "remote view of stack depth" can be different than the
|
|
|
|
// actual stack depth when there are out-of-turn replies. When we
|
2013-10-01 00:27:45 +00:00
|
|
|
// receive one, our actual Interrupt stack depth doesn't decrease, but
|
2013-09-28 01:42:08 +00:00
|
|
|
// the other side (that sent the reply) thinks it has. So, the
|
|
|
|
// "view" returned here is |stackDepth| minus the number of
|
|
|
|
// out-of-turn replies.
|
|
|
|
//
|
|
|
|
// Only called from the worker thread.
|
|
|
|
size_t RemoteViewOfStackDepth(size_t stackDepth) const {
|
|
|
|
AssertWorkerThread();
|
|
|
|
return stackDepth - mOutOfTurnReplies.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t NextSeqno() {
|
|
|
|
AssertWorkerThread();
|
|
|
|
return (mSide == ChildSide) ? --mNextSeqno : ++mNextSeqno;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This helper class manages mCxxStackDepth on behalf of MessageChannel.
|
|
|
|
// When the stack depth is incremented from zero to non-zero, it invokes
|
2013-10-01 00:27:45 +00:00
|
|
|
// a callback, and similarly for when the depth goes from non-zero to zero.
|
2016-10-30 18:26:40 +00:00
|
|
|
void EnteredCxxStack();
|
2013-09-28 01:42:08 +00:00
|
|
|
void ExitedCxxStack();
|
|
|
|
|
2016-10-30 18:26:40 +00:00
|
|
|
void EnteredCall();
|
|
|
|
void ExitedCall();
|
2013-09-28 01:42:08 +00:00
|
|
|
|
2016-10-30 18:26:40 +00:00
|
|
|
void EnteredSyncSend();
|
|
|
|
void ExitedSyncSend();
|
2015-01-17 02:34:47 +00:00
|
|
|
|
2013-09-28 01:42:08 +00:00
|
|
|
void DebugAbort(const char* file, int line, const char* cond,
|
|
|
|
const char* why,
|
2016-05-16 21:02:43 +00:00
|
|
|
bool reply=false);
|
2013-09-28 01:42:08 +00:00
|
|
|
|
|
|
|
// This method is only safe to call on the worker thread, or in a
|
|
|
|
// debugger with all threads paused.
|
2013-10-01 00:27:45 +00:00
|
|
|
void DumpInterruptStack(const char* const pfx="") const;
|
2013-09-28 01:42:08 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Called from both threads
|
2013-10-01 00:27:45 +00:00
|
|
|
size_t InterruptStackDepth() const {
|
2013-09-28 01:42:08 +00:00
|
|
|
mMonitor->AssertCurrentThreadOwns();
|
2013-10-01 00:27:45 +00:00
|
|
|
return mInterruptStack.size();
|
2013-09-28 01:42:08 +00:00
|
|
|
}
|
|
|
|
|
2013-10-01 00:27:45 +00:00
|
|
|
bool AwaitingInterruptReply() const {
|
2013-09-28 01:42:08 +00:00
|
|
|
mMonitor->AssertCurrentThreadOwns();
|
2013-10-01 00:27:45 +00:00
|
|
|
return !mInterruptStack.empty();
|
2013-09-28 01:42:08 +00:00
|
|
|
}
|
2014-12-29 23:12:47 +00:00
|
|
|
bool AwaitingIncomingMessage() const {
|
|
|
|
mMonitor->AssertCurrentThreadOwns();
|
|
|
|
return mIsWaitingForIncoming;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MOZ_STACK_CLASS AutoEnterWaitForIncoming
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit AutoEnterWaitForIncoming(MessageChannel& aChannel)
|
|
|
|
: mChannel(aChannel)
|
|
|
|
{
|
|
|
|
aChannel.mMonitor->AssertCurrentThreadOwns();
|
|
|
|
aChannel.mIsWaitingForIncoming = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoEnterWaitForIncoming()
|
|
|
|
{
|
|
|
|
mChannel.mIsWaitingForIncoming = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
MessageChannel& mChannel;
|
|
|
|
};
|
|
|
|
friend class AutoEnterWaitForIncoming;
|
2013-09-28 01:42:08 +00:00
|
|
|
|
2016-02-23 23:36:06 +00:00
|
|
|
// Returns true if we're dispatching an async message's callback.
|
2014-12-19 01:35:48 +00:00
|
|
|
bool DispatchingAsyncMessage() const {
|
|
|
|
AssertWorkerThread();
|
|
|
|
return mDispatchingAsyncMessage;
|
|
|
|
}
|
|
|
|
|
2016-09-30 23:20:50 +00:00
|
|
|
int DispatchingAsyncMessageNestedLevel() const {
|
2014-12-19 01:35:48 +00:00
|
|
|
AssertWorkerThread();
|
2016-09-30 23:20:50 +00:00
|
|
|
return mDispatchingAsyncMessageNestedLevel;
|
2014-12-19 01:35:48 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 11:32:47 +00:00
|
|
|
bool Connected() const;
|
|
|
|
|
2013-09-28 01:42:08 +00:00
|
|
|
private:
|
|
|
|
// Executed on the IO thread.
|
|
|
|
void NotifyWorkerThread();
|
|
|
|
|
|
|
|
// Return true if |aMsg| is a special message targeted at the IO
|
|
|
|
// thread, in which case it shouldn't be delivered to the worker.
|
|
|
|
bool MaybeInterceptSpecialIOMessage(const Message& aMsg);
|
|
|
|
|
|
|
|
void OnChannelConnected(int32_t peer_id);
|
|
|
|
|
|
|
|
// Tell the IO thread to close the channel and wait for it to ACK.
|
|
|
|
void SynchronouslyClose();
|
|
|
|
|
2016-01-23 04:32:14 +00:00
|
|
|
bool WasTransactionCanceled(int transaction);
|
2014-10-08 04:32:45 +00:00
|
|
|
bool ShouldDeferMessage(const Message& aMsg);
|
2016-02-29 23:43:35 +00:00
|
|
|
void OnMessageReceivedFromLink(Message&& aMsg);
|
2013-09-28 01:42:08 +00:00
|
|
|
void OnChannelErrorFromLink();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Run on the not current thread.
|
|
|
|
void NotifyChannelClosed();
|
|
|
|
void NotifyMaybeChannelError();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Can be run on either thread
|
|
|
|
void AssertWorkerThread() const
|
|
|
|
{
|
2017-01-26 19:48:54 +00:00
|
|
|
MOZ_ASSERT(mWorkerLoopID != -1, "Channel hasn't been opened yet");
|
2015-04-06 22:34:20 +00:00
|
|
|
MOZ_RELEASE_ASSERT(mWorkerLoopID == MessageLoop::current()->id(),
|
|
|
|
"not on worker thread!");
|
2013-09-28 01:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The "link" thread is either the I/O thread (ProcessLink) or the
|
|
|
|
// other actor's work thread (ThreadLink). In either case, it is
|
|
|
|
// NOT our worker thread.
|
|
|
|
void AssertLinkThread() const
|
|
|
|
{
|
2017-01-26 19:48:54 +00:00
|
|
|
MOZ_ASSERT(mWorkerLoopID != -1, "Channel hasn't been opened yet");
|
2015-04-06 22:34:20 +00:00
|
|
|
MOZ_RELEASE_ASSERT(mWorkerLoopID != MessageLoop::current()->id(),
|
|
|
|
"on worker thread but should not be!");
|
2013-09-28 01:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-10-15 18:47:14 +00:00
|
|
|
class MessageTask :
|
|
|
|
public CancelableRunnable,
|
2016-11-08 12:05:45 +00:00
|
|
|
public LinkedListElement<RefPtr<MessageTask>>,
|
|
|
|
public nsIRunnablePriority
|
2016-07-04 05:36:34 +00:00
|
|
|
{
|
2016-10-15 18:47:14 +00:00
|
|
|
public:
|
2017-01-18 00:50:34 +00:00
|
|
|
explicit MessageTask(MessageChannel* aChannel, Message&& aMessage);
|
2016-07-04 05:36:34 +00:00
|
|
|
|
2016-11-08 12:05:45 +00:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
2016-10-15 18:47:14 +00:00
|
|
|
NS_IMETHOD Run() override;
|
|
|
|
nsresult Cancel() override;
|
2016-11-08 12:05:45 +00:00
|
|
|
NS_IMETHOD GetPriority(uint32_t* aPriority) override;
|
2016-10-15 18:47:14 +00:00
|
|
|
void Post();
|
|
|
|
void Clear();
|
2016-07-04 05:36:34 +00:00
|
|
|
|
2016-10-15 18:47:14 +00:00
|
|
|
bool IsScheduled() const { return mScheduled; }
|
2013-09-28 01:42:08 +00:00
|
|
|
|
2016-10-15 18:47:14 +00:00
|
|
|
Message& Msg() { return mMessage; }
|
|
|
|
const Message& Msg() const { return mMessage; }
|
2013-09-28 01:42:08 +00:00
|
|
|
|
2016-10-15 18:47:14 +00:00
|
|
|
private:
|
|
|
|
MessageTask() = delete;
|
|
|
|
MessageTask(const MessageTask&) = delete;
|
2016-11-08 12:05:45 +00:00
|
|
|
~MessageTask() {}
|
2013-09-28 01:42:08 +00:00
|
|
|
|
2016-10-15 18:47:14 +00:00
|
|
|
MessageChannel* mChannel;
|
|
|
|
Message mMessage;
|
|
|
|
bool mScheduled : 1;
|
2013-09-28 01:42:08 +00:00
|
|
|
};
|
|
|
|
|
2016-10-15 18:47:14 +00:00
|
|
|
bool ShouldRunMessage(const Message& aMsg);
|
|
|
|
void RunMessage(MessageTask& aTask);
|
2013-09-28 01:42:08 +00:00
|
|
|
|
2016-10-15 18:47:14 +00:00
|
|
|
typedef LinkedList<RefPtr<MessageTask>> MessageQueue;
|
|
|
|
typedef std::map<size_t, Message> MessageMap;
|
|
|
|
typedef IPC::Message::msgid_t msgid_t;
|
2013-09-28 01:42:08 +00:00
|
|
|
|
|
|
|
private:
|
2016-08-10 08:53:00 +00:00
|
|
|
// Based on presumption the listener owns and overlives the channel,
|
|
|
|
// this is never nullified.
|
2016-10-30 18:26:40 +00:00
|
|
|
IToplevelProtocol* mListener;
|
2013-09-28 01:42:08 +00:00
|
|
|
ChannelState mChannelState;
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<RefCountedMonitor> mMonitor;
|
2013-09-28 01:42:08 +00:00
|
|
|
Side mSide;
|
|
|
|
MessageLink* mLink;
|
|
|
|
MessageLoop* mWorkerLoop; // thread where work is done
|
2016-04-28 00:06:05 +00:00
|
|
|
RefPtr<CancelableRunnable> mChannelErrorTask; // NotifyMaybeChannelError runnable
|
2013-09-28 01:42:08 +00:00
|
|
|
|
|
|
|
// id() of mWorkerLoop. This persists even after mWorkerLoop is cleared
|
|
|
|
// during channel shutdown.
|
|
|
|
int mWorkerLoopID;
|
|
|
|
|
|
|
|
// Timeout periods are broken up in two to prevent system suspension from
|
|
|
|
// triggering an abort. This method (called by WaitForEvent with a 'did
|
|
|
|
// timeout' flag) decides if we should wait again for half of mTimeoutMs
|
|
|
|
// or give up.
|
|
|
|
int32_t mTimeoutMs;
|
|
|
|
bool mInTimeoutSecondHalf;
|
|
|
|
|
|
|
|
// Worker-thread only; sequence numbers for messages that require
|
|
|
|
// synchronous replies.
|
|
|
|
int32_t mNextSeqno;
|
|
|
|
|
|
|
|
static bool sIsPumpingMessages;
|
|
|
|
|
2016-01-23 01:45:43 +00:00
|
|
|
// If ::Send returns false, this gives a more descriptive error.
|
|
|
|
SyncSendError mLastSendError;
|
|
|
|
|
2014-10-08 04:32:45 +00:00
|
|
|
template<class T>
|
|
|
|
class AutoSetValue {
|
2013-09-28 01:42:08 +00:00
|
|
|
public:
|
2014-10-08 04:32:45 +00:00
|
|
|
explicit AutoSetValue(T &var, const T &newValue)
|
2016-01-20 01:32:24 +00:00
|
|
|
: mVar(var), mPrev(var), mNew(newValue)
|
2013-09-28 01:42:08 +00:00
|
|
|
{
|
2014-10-08 04:32:45 +00:00
|
|
|
mVar = newValue;
|
2013-09-28 01:42:08 +00:00
|
|
|
}
|
2014-10-08 04:32:45 +00:00
|
|
|
~AutoSetValue() {
|
2016-01-20 01:32:24 +00:00
|
|
|
// The value may have been zeroed if the transaction was
|
|
|
|
// canceled. In that case we shouldn't return it to its previous
|
|
|
|
// value.
|
|
|
|
if (mVar == mNew) {
|
|
|
|
mVar = mPrev;
|
|
|
|
}
|
2013-09-28 01:42:08 +00:00
|
|
|
}
|
|
|
|
private:
|
2014-10-08 04:32:45 +00:00
|
|
|
T& mVar;
|
|
|
|
T mPrev;
|
2016-01-20 01:32:24 +00:00
|
|
|
T mNew;
|
2013-09-28 01:42:08 +00:00
|
|
|
};
|
|
|
|
|
2014-12-19 01:35:48 +00:00
|
|
|
bool mDispatchingAsyncMessage;
|
2016-09-30 23:20:50 +00:00
|
|
|
int mDispatchingAsyncMessageNestedLevel;
|
2014-12-19 01:35:48 +00:00
|
|
|
|
2013-10-01 16:15:03 +00:00
|
|
|
// When we send an urgent request from the parent process, we could race
|
|
|
|
// with an RPC message that was issued by the child beforehand. In this
|
|
|
|
// case, if the parent were to wake up while waiting for the urgent reply,
|
|
|
|
// and process the RPC, it could send an additional urgent message. The
|
|
|
|
// child would wake up to process the urgent message (as it always will),
|
|
|
|
// then send a reply, which could be received by the parent out-of-order
|
|
|
|
// with respect to the first urgent reply.
|
|
|
|
//
|
|
|
|
// To address this problem, urgent or RPC requests are associated with a
|
|
|
|
// "transaction". Whenever one side of the channel wishes to start a
|
|
|
|
// chain of RPC/urgent messages, it allocates a new transaction ID. Any
|
|
|
|
// messages the parent receives, not apart of this transaction, are
|
|
|
|
// deferred. When issuing RPC/urgent requests on top of a started
|
|
|
|
// transaction, the initiating transaction ID is used.
|
2014-10-08 04:32:45 +00:00
|
|
|
//
|
2013-10-01 16:15:03 +00:00
|
|
|
// To ensure IDs are unique, we use sequence numbers for transaction IDs,
|
|
|
|
// which grow in opposite directions from child to parent.
|
|
|
|
|
2016-02-23 23:36:06 +00:00
|
|
|
friend class AutoEnterTransaction;
|
|
|
|
AutoEnterTransaction *mTransactionStack;
|
2013-10-01 16:15:03 +00:00
|
|
|
|
2016-09-30 23:20:50 +00:00
|
|
|
int32_t CurrentNestedInsideSyncTransaction() const;
|
2016-01-23 01:58:54 +00:00
|
|
|
|
2016-02-23 23:36:06 +00:00
|
|
|
bool AwaitingSyncReply() const;
|
2016-09-30 23:20:50 +00:00
|
|
|
int AwaitingSyncReplyNestedLevel() const;
|
2013-10-01 16:15:03 +00:00
|
|
|
|
2016-02-23 23:36:06 +00:00
|
|
|
bool DispatchingSyncMessage() const;
|
2016-09-30 23:20:50 +00:00
|
|
|
int DispatchingSyncMessageNestedLevel() const;
|
2013-09-28 01:42:08 +00:00
|
|
|
|
2014-12-19 01:35:44 +00:00
|
|
|
// If a sync message times out, we store its sequence number here. Any
|
|
|
|
// future sync messages will fail immediately. Once the reply for original
|
|
|
|
// sync message is received, we allow sync messages again.
|
|
|
|
//
|
|
|
|
// When a message times out, nothing is done to inform the other side. The
|
|
|
|
// other side will eventually dispatch the message and send a reply. Our
|
|
|
|
// side is responsible for replying to all sync messages sent by the other
|
|
|
|
// side when it dispatches the timed out message. The response is always an
|
|
|
|
// error.
|
|
|
|
//
|
|
|
|
// A message is only timed out if it initiated a transaction. This avoids
|
|
|
|
// hitting a lot of corner cases with message nesting that we don't really
|
|
|
|
// care about.
|
|
|
|
int32_t mTimedOutMessageSeqno;
|
2016-09-30 23:20:50 +00:00
|
|
|
int mTimedOutMessageNestedLevel;
|
2014-12-19 01:35:44 +00:00
|
|
|
|
2016-10-15 18:47:14 +00:00
|
|
|
// Queue of all incoming messages.
|
2013-09-28 01:42:08 +00:00
|
|
|
//
|
|
|
|
// If both this side and the other side are functioning correctly, the queue
|
|
|
|
// can only be in certain configurations. Let
|
|
|
|
//
|
|
|
|
// |A<| be an async in-message,
|
|
|
|
// |S<| be a sync in-message,
|
2013-10-01 00:27:45 +00:00
|
|
|
// |C<| be an Interrupt in-call,
|
|
|
|
// |R<| be an Interrupt reply.
|
2013-09-28 01:42:08 +00:00
|
|
|
//
|
|
|
|
// The queue can only match this configuration
|
|
|
|
//
|
2016-10-15 18:47:14 +00:00
|
|
|
// A<* (S< | C< | R< (?{mInterruptStack.size() == 1} A<* (S< | C<)))
|
2013-09-28 01:42:08 +00:00
|
|
|
//
|
|
|
|
// The other side can send as many async messages |A<*| as it wants before
|
|
|
|
// sending us a blocking message.
|
|
|
|
//
|
|
|
|
// The first case is |S<|, a sync in-msg. The other side must be blocked,
|
|
|
|
// and thus can't send us any more messages until we process the sync
|
|
|
|
// in-msg.
|
|
|
|
//
|
2013-10-01 00:27:45 +00:00
|
|
|
// The second case is |C<|, an Interrupt in-call; the other side must be blocked.
|
2013-09-28 01:42:08 +00:00
|
|
|
// (There's a subtlety here: this in-call might have raced with an
|
|
|
|
// out-call, but we detect that with the mechanism below,
|
|
|
|
// |mRemoteStackDepth|, and races don't matter to the queue.)
|
|
|
|
//
|
|
|
|
// Final case, the other side replied to our most recent out-call |R<|.
|
2016-10-15 18:47:14 +00:00
|
|
|
// If that was the *only* out-call on our stack, |?{mInterruptStack.size() == 1}|,
|
2013-09-28 01:42:08 +00:00
|
|
|
// then other side "finished with us," and went back to its own business.
|
|
|
|
// That business might have included sending any number of async message
|
|
|
|
// |A<*| until sending a blocking message |(S< | C<)|. If we had more than
|
2013-10-01 00:27:45 +00:00
|
|
|
// one Interrupt call on our stack, the other side *better* not have sent us
|
2013-09-28 01:42:08 +00:00
|
|
|
// another blocking message, because it's blocked on a reply from us.
|
|
|
|
//
|
|
|
|
MessageQueue mPending;
|
2013-10-01 16:15:03 +00:00
|
|
|
|
2013-09-28 01:42:08 +00:00
|
|
|
// Stack of all the out-calls on which this channel is awaiting responses.
|
|
|
|
// Each stack refers to a different protocol and the stacks are mutually
|
|
|
|
// exclusive: multiple outcalls of the same kind cannot be initiated while
|
|
|
|
// another is active.
|
2016-04-27 23:21:49 +00:00
|
|
|
std::stack<MessageInfo> mInterruptStack;
|
2013-09-28 01:42:08 +00:00
|
|
|
|
2013-10-01 00:27:45 +00:00
|
|
|
// This is what we think the Interrupt stack depth is on the "other side" of this
|
|
|
|
// Interrupt channel. We maintain this variable so that we can detect racy Interrupt
|
|
|
|
// calls. With each Interrupt out-call sent, we send along what *we* think the
|
|
|
|
// stack depth of the remote side is *before* it will receive the Interrupt call.
|
2013-09-28 01:42:08 +00:00
|
|
|
//
|
|
|
|
// After sending the out-call, our stack depth is "incremented" by pushing
|
|
|
|
// that pending message onto mPending.
|
|
|
|
//
|
|
|
|
// Then when processing an in-call |c|, it must be true that
|
|
|
|
//
|
2016-10-15 18:47:14 +00:00
|
|
|
// mInterruptStack.size() == c.remoteDepth
|
2013-09-28 01:42:08 +00:00
|
|
|
//
|
|
|
|
// I.e., my depth is actually the same as what the other side thought it
|
|
|
|
// was when it sent in-call |c|. If this fails to hold, we have detected
|
2013-10-01 00:27:45 +00:00
|
|
|
// racy Interrupt calls.
|
2013-09-28 01:42:08 +00:00
|
|
|
//
|
|
|
|
// We then increment mRemoteStackDepth *just before* processing the
|
|
|
|
// in-call, since we know the other side is waiting on it, and decrement
|
|
|
|
// it *just after* finishing processing that in-call, since our response
|
|
|
|
// will pop the top of the other side's |mPending|.
|
|
|
|
//
|
|
|
|
// One nice aspect of this race detection is that it is symmetric; if one
|
|
|
|
// side detects a race, then the other side must also detect the same race.
|
|
|
|
size_t mRemoteStackDepthGuess;
|
|
|
|
|
|
|
|
// Approximation of code frames on the C++ stack. It can only be
|
|
|
|
// interpreted as the implication:
|
|
|
|
//
|
|
|
|
// !mCxxStackFrames.empty() => MessageChannel code on C++ stack
|
|
|
|
//
|
|
|
|
// This member is only accessed on the worker thread, and so is not
|
|
|
|
// protected by mMonitor. It is managed exclusively by the helper
|
|
|
|
// |class CxxStackFrame|.
|
2014-01-10 20:58:20 +00:00
|
|
|
mozilla::Vector<InterruptFrame> mCxxStackFrames;
|
2013-09-28 01:42:08 +00:00
|
|
|
|
2013-10-01 00:27:45 +00:00
|
|
|
// Did we process an Interrupt out-call during this stack? Only meaningful in
|
2013-09-28 01:42:08 +00:00
|
|
|
// ExitedCxxStack(), from which this variable is reset.
|
2013-10-01 00:27:45 +00:00
|
|
|
bool mSawInterruptOutMsg;
|
2013-09-28 01:42:08 +00:00
|
|
|
|
2014-12-29 23:12:47 +00:00
|
|
|
// Are we waiting on this channel for an incoming message? This is used
|
|
|
|
// to implement WaitForIncomingMessage(). Must only be accessed while owning
|
|
|
|
// mMonitor.
|
|
|
|
bool mIsWaitingForIncoming;
|
|
|
|
|
2013-10-01 00:27:45 +00:00
|
|
|
// Map of replies received "out of turn", because of Interrupt
|
2013-09-28 01:42:08 +00:00
|
|
|
// in-calls racing with replies to outstanding in-calls. See
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=521929.
|
|
|
|
MessageMap mOutOfTurnReplies;
|
|
|
|
|
2013-10-01 00:27:45 +00:00
|
|
|
// Stack of Interrupt in-calls that were deferred because of race
|
2013-09-28 01:42:08 +00:00
|
|
|
// conditions.
|
|
|
|
std::stack<Message> mDeferred;
|
|
|
|
|
|
|
|
#ifdef OS_WIN
|
|
|
|
HANDLE mEvent;
|
|
|
|
#endif
|
2014-03-04 17:17:01 +00:00
|
|
|
|
|
|
|
// Should the channel abort the process from the I/O thread when
|
|
|
|
// a channel error occurs?
|
|
|
|
bool mAbortOnError;
|
2014-08-06 04:44:42 +00:00
|
|
|
|
2016-09-20 02:17:09 +00:00
|
|
|
// True if the listener has already been notified of a channel close or
|
|
|
|
// error.
|
|
|
|
bool mNotifiedChannelDone;
|
|
|
|
|
2014-08-15 17:12:37 +00:00
|
|
|
// See SetChannelFlags
|
|
|
|
ChannelFlags mFlags;
|
2014-08-21 18:13:23 +00:00
|
|
|
|
|
|
|
// Task and state used to asynchronously notify channel has been connected
|
|
|
|
// safely. This is necessary to be able to cancel notification if we are
|
|
|
|
// closed at the same time.
|
2016-10-15 18:54:56 +00:00
|
|
|
RefPtr<CancelableRunnable> mOnChannelConnectedTask;
|
2016-02-05 00:09:11 +00:00
|
|
|
bool mPeerPidSet;
|
2014-08-21 18:13:23 +00:00
|
|
|
int32_t mPeerPid;
|
2013-09-28 01:42:08 +00:00
|
|
|
};
|
|
|
|
|
2015-06-24 00:52:01 +00:00
|
|
|
void
|
|
|
|
CancelCPOWs();
|
2014-08-06 04:43:36 +00:00
|
|
|
|
2013-09-28 01:42:08 +00:00
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // ifndef ipc_glue_MessageChannel_h
|