Bug 1834537 - Part 6: Simplify GCRuntime::checkAllocatorState a little r=sfink
[gecko.git] / ipc / glue / ScopedPort.cpp
blobe874796fb24b0193508f5d1caf100da5e334eeae
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() {
14 if (mValid) {
15 mController->ClosePort(mPort);
17 mValid = false;
18 mPort = {};
19 mController = nullptr;
22 auto ScopedPort::Release() -> PortRef {
23 if (!mValid) {
24 return {};
26 mValid = false;
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) {
47 Reset();
48 mValid = std::exchange(aOther.mValid, false);
49 mPort = std::move(aOther.mPort);
50 mController = std::move(aOther.mController);
52 return *this;
55 } // namespace mozilla::ipc
57 void IPC::ParamTraits<mozilla::ipc::ScopedPort>::Write(MessageWriter* aWriter,
58 paramType&& aParam) {
59 aWriter->WriteBool(aParam.IsValid());
60 if (!aParam.IsValid()) {
61 return;
63 aWriter->WritePort(std::move(aParam));
66 bool IPC::ParamTraits<mozilla::ipc::ScopedPort>::Read(MessageReader* aReader,
67 paramType* aResult) {
68 bool isValid = false;
69 if (!aReader->ReadBool(&isValid)) {
70 return false;
72 if (!isValid) {
73 *aResult = {};
74 return true;
76 return aReader->ConsumePort(aResult);