summaryrefslogtreecommitdiffstats
path: root/ipc/ipdl/test/cxx/TestEndpointBridgeMain.cpp
blob: 4a4366f0ba339888f989aeecbd6c21974db347a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */

#include "TestEndpointBridgeMain.h"

#include "base/task.h"
#include "IPDLUnitTests.h"  // fail etc.
#include "IPDLUnitTestSubprocess.h"

using namespace std;

namespace mozilla {
namespace _ipdltest {

//-----------------------------------------------------------------------------
// main process
void TestEndpointBridgeMainParent::Main() {
  if (!SendStart()) {
    fail("sending Start");
  }
}

mozilla::ipc::IPCResult TestEndpointBridgeMainParent::RecvBridged(
    Endpoint<PTestEndpointBridgeMainSubParent>&& endpoint) {
  TestEndpointBridgeMainSubParent* a = new TestEndpointBridgeMainSubParent();
  if (!endpoint.Bind(a)) {
    fail("Bind failed");
  }
  return IPC_OK();
}

void TestEndpointBridgeMainParent::ActorDestroy(ActorDestroyReason why) {
  if (NormalShutdown != why) {
    fail("unexpected destruction!");
  }
  passed("ok");
  QuitParent();
}

mozilla::ipc::IPCResult TestEndpointBridgeMainSubParent::RecvHello() {
  if (!SendHi()) {
    return IPC_FAIL_NO_REASON(this);
  }
  return IPC_OK();
}

mozilla::ipc::IPCResult TestEndpointBridgeMainSubParent::RecvHelloSync() {
  return IPC_OK();
}

mozilla::ipc::IPCResult TestEndpointBridgeMainSubParent::AnswerHelloRpc() {
  if (!CallHiRpc()) {
    return IPC_FAIL_NO_REASON(this);
  }
  return IPC_OK();
}

void TestEndpointBridgeMainSubParent::ActorDestroy(ActorDestroyReason why) {
  if (NormalShutdown != why) {
    fail("unexpected destruction!");
  }

  // ActorDestroy() is just a callback from IPDL-generated code,
  // which needs the top-level actor (this) to stay alive a little
  // longer so other things can be cleaned up.
  MessageLoop::current()->PostTask(
      do_AddRef(new DeleteTask<TestEndpointBridgeMainSubParent>(this)));
}

//-----------------------------------------------------------------------------
// sub process --- child of main
TestEndpointBridgeMainChild* gEndpointBridgeMainChild;

TestEndpointBridgeMainChild::TestEndpointBridgeMainChild()
    : mSubprocess(nullptr) {
  gEndpointBridgeMainChild = this;
}

mozilla::ipc::IPCResult TestEndpointBridgeMainChild::RecvStart() {
  vector<string> subsubArgs;
  subsubArgs.push_back("TestEndpointBridgeSub");

  mSubprocess = new IPDLUnitTestSubprocess();
  if (!mSubprocess->SyncLaunch(subsubArgs)) {
    fail("problem launching subprocess");
  }

  IPC::Channel* transport = mSubprocess->GetChannel();
  if (!transport) {
    fail("no transport");
  }

  TestEndpointBridgeSubParent* bsp = new TestEndpointBridgeSubParent();
  bsp->Open(transport, base::GetProcId(mSubprocess->GetChildProcessHandle()));

  bsp->Main();
  return IPC_OK();
}

void TestEndpointBridgeMainChild::ActorDestroy(ActorDestroyReason why) {
  if (NormalShutdown != why) {
    fail("unexpected destruction!");
  }
  // NB: this is kosher because QuitChild() joins with the IO thread
  mSubprocess->Destroy();
  mSubprocess = nullptr;
  QuitChild();
}

void TestEndpointBridgeSubParent::Main() {
  if (!SendPing()) {
    fail("sending Ping");
  }
}

mozilla::ipc::IPCResult TestEndpointBridgeSubParent::RecvBridgeEm() {
  Endpoint<PTestEndpointBridgeMainSubParent> parent;
  Endpoint<PTestEndpointBridgeMainSubChild> child;
  nsresult rv;
  rv = PTestEndpointBridgeMainSub::CreateEndpoints(
      gEndpointBridgeMainChild->OtherPid(), OtherPid(), &parent, &child);
  if (NS_FAILED(rv)) {
    fail("opening PTestEndpointOpensOpened");
  }

  if (!gEndpointBridgeMainChild->SendBridged(std::move(parent))) {
    fail("SendBridge failed for parent");
  }
  if (!SendBridged(std::move(child))) {
    fail("SendBridge failed for child");
  }

  return IPC_OK();
}

void TestEndpointBridgeSubParent::ActorDestroy(ActorDestroyReason why) {
  if (NormalShutdown != why) {
    fail("unexpected destruction!");
  }
  gEndpointBridgeMainChild->Close();

  // ActorDestroy() is just a callback from IPDL-generated code,
  // which needs the top-level actor (this) to stay alive a little
  // longer so other things can be cleaned up.
  MessageLoop::current()->PostTask(
      do_AddRef(new DeleteTask<TestEndpointBridgeSubParent>(this)));
}

//-----------------------------------------------------------------------------
// subsub process --- child of sub

static TestEndpointBridgeSubChild* gBridgeSubChild;

TestEndpointBridgeSubChild::TestEndpointBridgeSubChild() {
  gBridgeSubChild = this;
}

mozilla::ipc::IPCResult TestEndpointBridgeSubChild::RecvPing() {
  if (!SendBridgeEm()) {
    fail("sending BridgeEm");
  }
  return IPC_OK();
}

mozilla::ipc::IPCResult TestEndpointBridgeSubChild::RecvBridged(
    Endpoint<PTestEndpointBridgeMainSubChild>&& endpoint) {
  TestEndpointBridgeMainSubChild* a = new TestEndpointBridgeMainSubChild();

  if (!endpoint.Bind(a)) {
    fail("failed to Bind");
  }

  if (!a->SendHello()) {
    fail("sending Hello");
  }

  return IPC_OK();
}

void TestEndpointBridgeSubChild::ActorDestroy(ActorDestroyReason why) {
  if (NormalShutdown != why) {
    fail("unexpected destruction!");
  }
  QuitChild();
}

mozilla::ipc::IPCResult TestEndpointBridgeMainSubChild::RecvHi() {
  if (!SendHelloSync()) {
    fail("sending HelloSync");
  }
  if (!CallHelloRpc()) {
    fail("calling HelloRpc");
  }
  if (!mGotHi) {
    fail("didn't answer HiRpc");
  }

  // Need to close the channel without message-processing frames on
  // the C++ stack
  MessageLoop::current()->PostTask(
      NewNonOwningRunnableMethod("ipc::IToplevelProtocol::Close", this,
                                 &TestEndpointBridgeMainSubChild::Close));
  return IPC_OK();
}

mozilla::ipc::IPCResult TestEndpointBridgeMainSubChild::AnswerHiRpc() {
  mGotHi = true;  // d00d
  return IPC_OK();
}

void TestEndpointBridgeMainSubChild::ActorDestroy(ActorDestroyReason why) {
  if (NormalShutdown != why) {
    fail("unexpected destruction!");
  }

  gBridgeSubChild->Close();

  // ActorDestroy() is just a callback from IPDL-generated code,
  // which needs the top-level actor (this) to stay alive a little
  // longer so other things can be cleaned up.
  MessageLoop::current()->PostTask(
      do_AddRef(new DeleteTask<TestEndpointBridgeMainSubChild>(this)));
}

}  // namespace _ipdltest
}  // namespace mozilla