Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / ipc / glue / ToplevelActorHolder.h
blob6e95ef8565cdf9cbdd7c1a4a0e8df6b13cb590a5
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 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef MOZILLA_IPC_TOPLEVELACTORHOLDER_H
8 #define MOZILLA_IPC_TOPLEVELACTORHOLDER_H
10 #include "nsISupports.h"
12 namespace mozilla::ipc {
14 // Class to let us close the actor when we're not using it anymore. You
15 // should create a single instance of this, and when you have no more
16 // references it will be destroyed and will Close() the underlying
17 // top-level channel.
18 // When you want to send something, you use something like
19 // aActor->Actor()->SendFoo()
21 // You can avoid calling Close() on an un-connected Actor (for example if
22 // Bind() fails) by calling RemoveActor();
23 template <typename T>
24 class ToplevelActorHolder final {
25 public:
26 NS_INLINE_DECL_REFCOUNTING_ONEVENTTARGET(ToplevelActorHolder)
28 explicit ToplevelActorHolder(T* aActor) : mActor(aActor) {}
30 constexpr T* Actor() const { return mActor; }
31 inline void RemoveActor() { mActor = nullptr; }
33 private:
34 inline ~ToplevelActorHolder() {
35 if (mActor) {
36 mActor->Close();
40 RefPtr<T> mActor;
43 } // namespace mozilla::ipc
45 #endif // MOZILLA_IPC_TOPLEVELACTORHOLDER_H