Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | #include "TestBridgeMain.h" |
michael@0 | 2 | |
michael@0 | 3 | #include "IPDLUnitTests.h" // fail etc. |
michael@0 | 4 | #include "IPDLUnitTestSubprocess.h" |
michael@0 | 5 | |
michael@0 | 6 | using namespace std; |
michael@0 | 7 | |
michael@0 | 8 | template<> |
michael@0 | 9 | struct RunnableMethodTraits<mozilla::_ipdltest::TestBridgeMainSubChild> |
michael@0 | 10 | { |
michael@0 | 11 | static void RetainCallee(mozilla::_ipdltest::TestBridgeMainSubChild* obj) { } |
michael@0 | 12 | static void ReleaseCallee(mozilla::_ipdltest::TestBridgeMainSubChild* obj) { } |
michael@0 | 13 | }; |
michael@0 | 14 | |
michael@0 | 15 | namespace mozilla { |
michael@0 | 16 | namespace _ipdltest { |
michael@0 | 17 | |
michael@0 | 18 | |
michael@0 | 19 | //----------------------------------------------------------------------------- |
michael@0 | 20 | // main process |
michael@0 | 21 | void |
michael@0 | 22 | TestBridgeMainParent::Main() |
michael@0 | 23 | { |
michael@0 | 24 | if (!SendStart()) |
michael@0 | 25 | fail("sending Start"); |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | PTestBridgeMainSubParent* |
michael@0 | 29 | TestBridgeMainParent::AllocPTestBridgeMainSubParent(Transport* transport, |
michael@0 | 30 | ProcessId otherProcess) |
michael@0 | 31 | { |
michael@0 | 32 | ProcessHandle h; |
michael@0 | 33 | if (!base::OpenProcessHandle(otherProcess, &h)) { |
michael@0 | 34 | return nullptr; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | nsAutoPtr<TestBridgeMainSubParent> a(new TestBridgeMainSubParent(transport)); |
michael@0 | 38 | if (!a->Open(transport, h, XRE_GetIOMessageLoop(), ipc::ParentSide)) { |
michael@0 | 39 | return nullptr; |
michael@0 | 40 | } |
michael@0 | 41 | return a.forget(); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | void |
michael@0 | 45 | TestBridgeMainParent::ActorDestroy(ActorDestroyReason why) |
michael@0 | 46 | { |
michael@0 | 47 | if (NormalShutdown != why) |
michael@0 | 48 | fail("unexpected destruction!"); |
michael@0 | 49 | passed("ok"); |
michael@0 | 50 | QuitParent(); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | bool |
michael@0 | 54 | TestBridgeMainSubParent::RecvHello() |
michael@0 | 55 | { |
michael@0 | 56 | return SendHi(); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | bool |
michael@0 | 60 | TestBridgeMainSubParent::RecvHelloSync() |
michael@0 | 61 | { |
michael@0 | 62 | return true; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | bool |
michael@0 | 66 | TestBridgeMainSubParent::AnswerHelloRpc() |
michael@0 | 67 | { |
michael@0 | 68 | return CallHiRpc(); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | void |
michael@0 | 72 | TestBridgeMainSubParent::ActorDestroy(ActorDestroyReason why) |
michael@0 | 73 | { |
michael@0 | 74 | if (NormalShutdown != why) |
michael@0 | 75 | fail("unexpected destruction!"); |
michael@0 | 76 | |
michael@0 | 77 | // ActorDestroy() is just a callback from IPDL-generated code, |
michael@0 | 78 | // which needs the top-level actor (this) to stay alive a little |
michael@0 | 79 | // longer so other things can be cleaned up. |
michael@0 | 80 | MessageLoop::current()->PostTask( |
michael@0 | 81 | FROM_HERE, |
michael@0 | 82 | new DeleteTask<TestBridgeMainSubParent>(this)); |
michael@0 | 83 | XRE_GetIOMessageLoop()->PostTask( |
michael@0 | 84 | FROM_HERE, |
michael@0 | 85 | new DeleteTask<Transport>(mTransport)); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | //----------------------------------------------------------------------------- |
michael@0 | 89 | // sub process --- child of main |
michael@0 | 90 | TestBridgeMainChild* gBridgeMainChild; |
michael@0 | 91 | |
michael@0 | 92 | TestBridgeMainChild::TestBridgeMainChild() |
michael@0 | 93 | : mSubprocess(nullptr) |
michael@0 | 94 | { |
michael@0 | 95 | gBridgeMainChild = this; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | bool |
michael@0 | 99 | TestBridgeMainChild::RecvStart() |
michael@0 | 100 | { |
michael@0 | 101 | vector<string> subsubArgs; |
michael@0 | 102 | subsubArgs.push_back("TestBridgeSub"); |
michael@0 | 103 | |
michael@0 | 104 | mSubprocess = new IPDLUnitTestSubprocess(); |
michael@0 | 105 | if (!mSubprocess->SyncLaunch(subsubArgs)) |
michael@0 | 106 | fail("problem launching subprocess"); |
michael@0 | 107 | |
michael@0 | 108 | IPC::Channel* transport = mSubprocess->GetChannel(); |
michael@0 | 109 | if (!transport) |
michael@0 | 110 | fail("no transport"); |
michael@0 | 111 | |
michael@0 | 112 | TestBridgeSubParent* bsp = new TestBridgeSubParent(); |
michael@0 | 113 | bsp->Open(transport, mSubprocess->GetChildProcessHandle()); |
michael@0 | 114 | |
michael@0 | 115 | bsp->Main(); |
michael@0 | 116 | return true; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | void |
michael@0 | 120 | TestBridgeMainChild::ActorDestroy(ActorDestroyReason why) |
michael@0 | 121 | { |
michael@0 | 122 | if (NormalShutdown != why) |
michael@0 | 123 | fail("unexpected destruction!"); |
michael@0 | 124 | // NB: this is kosher because QuitChild() joins with the IO thread |
michael@0 | 125 | XRE_GetIOMessageLoop()->PostTask( |
michael@0 | 126 | FROM_HERE, |
michael@0 | 127 | new DeleteTask<IPDLUnitTestSubprocess>(mSubprocess)); |
michael@0 | 128 | QuitChild(); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | void |
michael@0 | 132 | TestBridgeSubParent::Main() |
michael@0 | 133 | { |
michael@0 | 134 | if (!SendPing()) |
michael@0 | 135 | fail("sending Ping"); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | bool |
michael@0 | 139 | TestBridgeSubParent::RecvBridgeEm() |
michael@0 | 140 | { |
michael@0 | 141 | if (!PTestBridgeMainSub::Bridge(gBridgeMainChild, this)) |
michael@0 | 142 | fail("bridging Main and Sub"); |
michael@0 | 143 | return true; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | void |
michael@0 | 147 | TestBridgeSubParent::ActorDestroy(ActorDestroyReason why) |
michael@0 | 148 | { |
michael@0 | 149 | if (NormalShutdown != why) |
michael@0 | 150 | fail("unexpected destruction!"); |
michael@0 | 151 | gBridgeMainChild->Close(); |
michael@0 | 152 | |
michael@0 | 153 | // ActorDestroy() is just a callback from IPDL-generated code, |
michael@0 | 154 | // which needs the top-level actor (this) to stay alive a little |
michael@0 | 155 | // longer so other things can be cleaned up. |
michael@0 | 156 | MessageLoop::current()->PostTask( |
michael@0 | 157 | FROM_HERE, |
michael@0 | 158 | new DeleteTask<TestBridgeSubParent>(this)); |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | //----------------------------------------------------------------------------- |
michael@0 | 162 | // subsub process --- child of sub |
michael@0 | 163 | |
michael@0 | 164 | static TestBridgeSubChild* gBridgeSubChild; |
michael@0 | 165 | |
michael@0 | 166 | TestBridgeSubChild::TestBridgeSubChild() |
michael@0 | 167 | { |
michael@0 | 168 | gBridgeSubChild = this; |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | bool |
michael@0 | 172 | TestBridgeSubChild::RecvPing() |
michael@0 | 173 | { |
michael@0 | 174 | if (!SendBridgeEm()) |
michael@0 | 175 | fail("sending BridgeEm"); |
michael@0 | 176 | return true; |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | PTestBridgeMainSubChild* |
michael@0 | 180 | TestBridgeSubChild::AllocPTestBridgeMainSubChild(Transport* transport, |
michael@0 | 181 | ProcessId otherProcess) |
michael@0 | 182 | { |
michael@0 | 183 | ProcessHandle h; |
michael@0 | 184 | if (!base::OpenProcessHandle(otherProcess, &h)) { |
michael@0 | 185 | return nullptr; |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | nsAutoPtr<TestBridgeMainSubChild> a(new TestBridgeMainSubChild(transport)); |
michael@0 | 189 | if (!a->Open(transport, h, XRE_GetIOMessageLoop(), ipc::ChildSide)) { |
michael@0 | 190 | return nullptr; |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | if (!a->SendHello()) |
michael@0 | 194 | fail("sending Hello"); |
michael@0 | 195 | |
michael@0 | 196 | return a.forget(); |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | void |
michael@0 | 200 | TestBridgeSubChild::ActorDestroy(ActorDestroyReason why) |
michael@0 | 201 | { |
michael@0 | 202 | if (NormalShutdown != why) |
michael@0 | 203 | fail("unexpected destruction!"); |
michael@0 | 204 | QuitChild(); |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | bool |
michael@0 | 208 | TestBridgeMainSubChild::RecvHi() |
michael@0 | 209 | { |
michael@0 | 210 | if (!SendHelloSync()) |
michael@0 | 211 | fail("sending HelloSync"); |
michael@0 | 212 | if (!CallHelloRpc()) |
michael@0 | 213 | fail("calling HelloRpc"); |
michael@0 | 214 | if (!mGotHi) |
michael@0 | 215 | fail("didn't answer HiRpc"); |
michael@0 | 216 | |
michael@0 | 217 | // Need to close the channel without message-processing frames on |
michael@0 | 218 | // the C++ stack |
michael@0 | 219 | MessageLoop::current()->PostTask( |
michael@0 | 220 | FROM_HERE, |
michael@0 | 221 | NewRunnableMethod(this, &TestBridgeMainSubChild::Close)); |
michael@0 | 222 | return true; |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | bool |
michael@0 | 226 | TestBridgeMainSubChild::AnswerHiRpc() |
michael@0 | 227 | { |
michael@0 | 228 | mGotHi = true; // d00d |
michael@0 | 229 | return true; |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | void |
michael@0 | 233 | TestBridgeMainSubChild::ActorDestroy(ActorDestroyReason why) |
michael@0 | 234 | { |
michael@0 | 235 | if (NormalShutdown != why) |
michael@0 | 236 | fail("unexpected destruction!"); |
michael@0 | 237 | |
michael@0 | 238 | gBridgeSubChild->Close(); |
michael@0 | 239 | |
michael@0 | 240 | // ActorDestroy() is just a callback from IPDL-generated code, |
michael@0 | 241 | // which needs the top-level actor (this) to stay alive a little |
michael@0 | 242 | // longer so other things can be cleaned up. |
michael@0 | 243 | MessageLoop::current()->PostTask( |
michael@0 | 244 | FROM_HERE, |
michael@0 | 245 | new DeleteTask<TestBridgeMainSubChild>(this)); |
michael@0 | 246 | XRE_GetIOMessageLoop()->PostTask( |
michael@0 | 247 | FROM_HERE, |
michael@0 | 248 | new DeleteTask<Transport>(mTransport)); |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | } // namespace mozilla |
michael@0 | 252 | } // namespace _ipdltest |