Bug 1660755 [wpt PR 25207] - [scroll-animations] Allow null source in ScrollTimeline...
[gecko.git] / ipc / glue / IPCStreamChild.cpp
blobd414a1eb95e2b8c30de7a8b43106e641222e5e8d
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 "IPCStreamDestination.h"
8 #include "IPCStreamSource.h"
10 #include "mozilla/Unused.h"
11 #include "mozilla/ipc/PChildToParentStreamChild.h"
12 #include "mozilla/ipc/PParentToChildStreamChild.h"
14 namespace mozilla {
15 namespace ipc {
17 // Child to Parent implementation
18 // ----------------------------------------------------------------------------
20 namespace {
22 class IPCStreamSourceChild final : public PChildToParentStreamChild,
23 public IPCStreamSource {
24 public:
25 static IPCStreamSourceChild* Create(nsIAsyncInputStream* aInputStream) {
26 MOZ_ASSERT(aInputStream);
28 IPCStreamSourceChild* source = new IPCStreamSourceChild(aInputStream);
29 if (!source->Initialize()) {
30 delete source;
31 return nullptr;
34 return source;
37 // PChildToParentStreamChild methods
39 void ActorDestroy(ActorDestroyReason aReason) override { ActorDestroyed(); }
41 IPCResult RecvStartReading() override {
42 Start();
43 return IPC_OK();
46 IPCResult RecvRequestClose(const nsresult& aRv) override {
47 OnEnd(aRv);
48 return IPC_OK();
51 void Close(nsresult aRv) override {
52 MOZ_ASSERT(IPCStreamSource::mState == IPCStreamSource::eClosed);
53 Unused << SendClose(aRv);
56 void SendData(const wr::ByteBuffer& aBuffer) override {
57 Unused << SendBuffer(aBuffer);
60 private:
61 explicit IPCStreamSourceChild(nsIAsyncInputStream* aInputStream)
62 : IPCStreamSource(aInputStream) {}
65 } // anonymous namespace
67 /* static */
68 PChildToParentStreamChild* IPCStreamSource::Create(
69 nsIAsyncInputStream* aInputStream,
70 ChildToParentStreamActorManager* aManager) {
71 MOZ_ASSERT(aInputStream);
72 MOZ_ASSERT(aManager);
74 IPCStreamSourceChild* source = IPCStreamSourceChild::Create(aInputStream);
75 if (!source) {
76 return nullptr;
79 if (!aManager->SendPChildToParentStreamConstructor(source)) {
80 return nullptr;
83 source->ActorConstructed();
84 return source;
87 /* static */
88 IPCStreamSource* IPCStreamSource::Cast(PChildToParentStreamChild* aActor) {
89 MOZ_ASSERT(aActor);
90 return static_cast<IPCStreamSourceChild*>(aActor);
93 // Parent to Child implementation
94 // ----------------------------------------------------------------------------
96 namespace {
98 class IPCStreamDestinationChild final : public PParentToChildStreamChild,
99 public IPCStreamDestination {
100 public:
101 nsresult Initialize() { return IPCStreamDestination::Initialize(); }
103 ~IPCStreamDestinationChild() = default;
105 private:
106 // PParentToChildStreamChild methods
108 void ActorDestroy(ActorDestroyReason aReason) override { ActorDestroyed(); }
110 IPCResult RecvBuffer(const wr::ByteBuffer& aBuffer) override {
111 BufferReceived(aBuffer);
112 return IPC_OK();
115 IPCResult RecvClose(const nsresult& aRv) override {
116 CloseReceived(aRv);
117 return IPC_OK();
120 // IPCStreamDestination methods
122 void StartReading() override {
123 MOZ_ASSERT(HasDelayedStart());
124 Unused << SendStartReading();
127 void RequestClose(nsresult aRv) override { Unused << SendRequestClose(aRv); }
129 void TerminateDestination() override { Unused << Send__delete__(this); }
132 } // anonymous namespace
134 PParentToChildStreamChild* AllocPParentToChildStreamChild() {
135 IPCStreamDestinationChild* actor = new IPCStreamDestinationChild();
137 if (NS_WARN_IF(NS_FAILED(actor->Initialize()))) {
138 delete actor;
139 actor = nullptr;
142 return actor;
145 void DeallocPParentToChildStreamChild(PParentToChildStreamChild* aActor) {
146 delete aActor;
149 /* static */
150 IPCStreamDestination* IPCStreamDestination::Cast(
151 PParentToChildStreamChild* aActor) {
152 MOZ_ASSERT(aActor);
153 return static_cast<IPCStreamDestinationChild*>(aActor);
156 } // namespace ipc
157 } // namespace mozilla