Skip to content

Commit ffe2709

Browse files
committed
Bug 1874739 - Part 13: Introduce the IRefCountedProtocol type, r=ipc-reviewers,mccr8
This acts as a new class between IProtocol and IToplevelProtocol which just introduces the AddRef and Release virtual methods. It's used to allow generic code to hold a strong reference to an actor which is structurally known to be reference counted, and generally will have limited use elsewhere. Differential Revision: https://phabricator.services.mozilla.com/D198623
1 parent ad33420 commit ffe2709

File tree

3 files changed

+23
-25
lines changed

3 files changed

+23
-25
lines changed

ipc/glue/ProtocolUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ void IProtocol::DestroySubtree(ActorDestroyReason aWhy) {
629629

630630
IToplevelProtocol::IToplevelProtocol(const char* aName, ProtocolId aProtoId,
631631
Side aSide)
632-
: IProtocol(aProtoId, aSide),
632+
: IRefCountedProtocol(aProtoId, aSide),
633633
mOtherPid(base::kInvalidProcessId),
634634
mLastLocalId(0),
635635
mChannel(aName, this) {

ipc/glue/ProtocolUtils.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ typedef IPCMessageStart ProtocolId;
156156
// Generated by IPDL compiler
157157
const char* ProtocolIdToName(IPCMessageStart aId);
158158

159+
class IRefCountedProtocol;
159160
class IToplevelProtocol;
160161
class ActorLifecycleProxy;
161162
class WeakActorLifecycleProxy;
@@ -409,13 +410,23 @@ class Endpoint;
409410
template <class PFooSide>
410411
class ManagedEndpoint;
411412

413+
/**
414+
* All refcounted protocols should inherit this class.
415+
*/
416+
class IRefCountedProtocol : public IProtocol {
417+
public:
418+
NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
419+
420+
using IProtocol::IProtocol;
421+
};
422+
412423
/**
413424
* All top-level protocols should inherit this class.
414425
*
415426
* IToplevelProtocol tracks all top-level protocol actors created from
416427
* this protocol actor.
417428
*/
418-
class IToplevelProtocol : public IProtocol {
429+
class IToplevelProtocol : public IRefCountedProtocol {
419430
template <class PFooSide>
420431
friend class Endpoint;
421432

@@ -425,9 +436,6 @@ class IToplevelProtocol : public IProtocol {
425436
~IToplevelProtocol() = default;
426437

427438
public:
428-
// All top-level protocols are refcounted.
429-
NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
430-
431439
// Shadow methods on IProtocol which are implemented directly on toplevel
432440
// actors.
433441
int32_t Register(IProtocol* aRouted);

ipc/ipdl/ipdl/lower.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,12 +1305,6 @@ def _subtreeUsesShmem(p):
13051305

13061306

13071307
class Protocol(ipdl.ast.Protocol):
1308-
def managerInterfaceType(self, ptr=False):
1309-
return Type("mozilla::ipc::IProtocol", ptr=ptr)
1310-
1311-
def openedProtocolInterfaceType(self, ptr=False):
1312-
return Type("mozilla::ipc::IToplevelProtocol", ptr=ptr)
1313-
13141308
def _ipdlmgrtype(self):
13151309
assert 1 == len(self.decl.type.managers)
13161310
for mgr in self.decl.type.managers:
@@ -3625,11 +3619,12 @@ def visitProtocol(self, p):
36253619
hasAsyncReturns = True
36263620
break
36273621

3628-
inherits = []
36293622
if ptype.isToplevel():
3630-
inherits.append(Inherit(p.openedProtocolInterfaceType(), viz="public"))
3623+
inherits = [Inherit(Type("mozilla::ipc::IToplevelProtocol"))]
3624+
elif ptype.isRefcounted():
3625+
inherits = [Inherit(Type("mozilla::ipc::IRefCountedProtocol"))]
36313626
else:
3632-
inherits.append(Inherit(p.managerInterfaceType(), viz="public"))
3627+
inherits = [Inherit(Type("mozilla::ipc::IProtocol"))]
36333628

36343629
if ptype.isToplevel() and self.side == "parent":
36353630
self.hdrfile.addthings(
@@ -3812,11 +3807,12 @@ def visitProtocol(self, p):
38123807
)
38133808
]
38143809
else:
3815-
ctor.memberinits = [
3816-
ExprMemberInit(
3817-
ExprVar("mozilla::ipc::IProtocol"), [_protocolId(ptype), side]
3818-
)
3819-
]
3810+
baseCtor = (
3811+
ExprVar("mozilla::ipc::IRefCountedProtocol")
3812+
if ptype.isRefcounted()
3813+
else ExprVar("mozilla::ipc::IProtocol")
3814+
)
3815+
ctor.memberinits = [ExprMemberInit(baseCtor, [_protocolId(ptype), side])]
38203816

38213817
ctor.addcode("MOZ_COUNT_CTOR(${clsname});\n", clsname=self.clsname)
38223818
self.cls.addstmts([ctor, Whitespace.NL])
@@ -3853,12 +3849,6 @@ def visitProtocol(self, p):
38533849
)
38543850

38553851
if ptype.isRefcounted():
3856-
if not ptype.isToplevel():
3857-
self.cls.addcode(
3858-
"""
3859-
NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
3860-
"""
3861-
)
38623852
# Perform AddRef/Release in ActorAlloc/ActorDealloc if refcounted.
38633853
actoralloc.addcode("AddRef();\n")
38643854
actordealloc.addcode("Release();\n")

0 commit comments

Comments
 (0)