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 #include "mozilla/ipc/ScopedPort.h"
8 #include "mozilla/ipc/NodeController.h"
9 #include "chrome/common/ipc_message_utils.h"
11 namespace mozilla::ipc
{
13 void ScopedPort::Reset() {
15 mController
->ClosePort(mPort
);
19 mController
= nullptr;
22 auto ScopedPort::Release() -> PortRef
{
27 mController
= nullptr;
28 return std::exchange(mPort
, PortRef
{});
31 ScopedPort::ScopedPort() = default;
33 ScopedPort::~ScopedPort() { Reset(); }
35 ScopedPort::ScopedPort(PortRef aPort
, NodeController
* aController
)
36 : mValid(true), mPort(std::move(aPort
)), mController(aController
) {
37 MOZ_ASSERT(mPort
.is_valid() && mController
);
40 ScopedPort::ScopedPort(ScopedPort
&& aOther
)
41 : mValid(std::exchange(aOther
.mValid
, false)),
42 mPort(std::move(aOther
.mPort
)),
43 mController(std::move(aOther
.mController
)) {}
45 ScopedPort
& ScopedPort::operator=(ScopedPort
&& aOther
) {
46 if (this != &aOther
) {
48 mValid
= std::exchange(aOther
.mValid
, false);
49 mPort
= std::move(aOther
.mPort
);
50 mController
= std::move(aOther
.mController
);
55 } // namespace mozilla::ipc
57 void IPC::ParamTraits
<mozilla::ipc::ScopedPort
>::Write(MessageWriter
* aWriter
,
59 aWriter
->WriteBool(aParam
.IsValid());
60 if (!aParam
.IsValid()) {
63 aWriter
->WritePort(std::move(aParam
));
66 bool IPC::ParamTraits
<mozilla::ipc::ScopedPort
>::Read(MessageReader
* aReader
,
69 if (!aReader
->ReadBool(&isValid
)) {
76 return aReader
->ConsumePort(aResult
);