Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 /* vim: set ts=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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "base/basictypes.h"
9 #include "BluetoothChild.h"
11 #include "mozilla/Assertions.h"
12 #include "nsDebug.h"
13 #include "nsISupportsImpl.h"
14 #include "nsThreadUtils.h"
16 #include "BluetoothReplyRunnable.h"
17 #include "BluetoothService.h"
18 #include "BluetoothServiceChildProcess.h"
20 USING_BLUETOOTH_NAMESPACE
22 namespace {
24 BluetoothServiceChildProcess* sBluetoothService;
26 } // anonymous namespace
28 /*******************************************************************************
29 * BluetoothChild
30 ******************************************************************************/
32 BluetoothChild::BluetoothChild(BluetoothServiceChildProcess* aBluetoothService)
33 : mShutdownState(Running)
34 {
35 MOZ_COUNT_CTOR(BluetoothChild);
36 MOZ_ASSERT(!sBluetoothService);
37 MOZ_ASSERT(aBluetoothService);
39 sBluetoothService = aBluetoothService;
40 }
42 BluetoothChild::~BluetoothChild()
43 {
44 MOZ_COUNT_DTOR(BluetoothChild);
45 MOZ_ASSERT(sBluetoothService);
46 MOZ_ASSERT(mShutdownState == Dead);
48 sBluetoothService = nullptr;
49 }
51 void
52 BluetoothChild::BeginShutdown()
53 {
54 // Only do something here if we haven't yet begun the shutdown sequence.
55 if (mShutdownState == Running) {
56 SendStopNotifying();
57 mShutdownState = SentStopNotifying;
58 }
59 }
61 void
62 BluetoothChild::ActorDestroy(ActorDestroyReason aWhy)
63 {
64 MOZ_ASSERT(sBluetoothService);
66 sBluetoothService->NoteDeadActor();
68 #ifdef DEBUG
69 mShutdownState = Dead;
70 #endif
71 }
73 bool
74 BluetoothChild::RecvNotify(const BluetoothSignal& aSignal)
75 {
76 MOZ_ASSERT(sBluetoothService);
78 sBluetoothService->DistributeSignal(aSignal);
79 return true;
80 }
82 bool
83 BluetoothChild::RecvEnabled(const bool& aEnabled)
84 {
85 MOZ_ASSERT(sBluetoothService);
87 sBluetoothService->SetEnabled(aEnabled);
88 return true;
89 }
91 bool
92 BluetoothChild::RecvBeginShutdown()
93 {
94 if (mShutdownState != Running && mShutdownState != SentStopNotifying) {
95 MOZ_ASSERT(false, "Bad state!");
96 return false;
97 }
99 SendStopNotifying();
100 mShutdownState = SentStopNotifying;
102 return true;
103 }
105 bool
106 BluetoothChild::RecvNotificationsStopped()
107 {
108 if (mShutdownState != SentStopNotifying) {
109 MOZ_ASSERT(false, "Bad state!");
110 return false;
111 }
113 Send__delete__(this);
114 return true;
115 }
117 PBluetoothRequestChild*
118 BluetoothChild::AllocPBluetoothRequestChild(const Request& aRequest)
119 {
120 MOZ_CRASH("Caller is supposed to manually construct a request!");
121 }
123 bool
124 BluetoothChild::DeallocPBluetoothRequestChild(PBluetoothRequestChild* aActor)
125 {
126 delete aActor;
127 return true;
128 }
130 /*******************************************************************************
131 * BluetoothRequestChild
132 ******************************************************************************/
134 BluetoothRequestChild::BluetoothRequestChild(
135 BluetoothReplyRunnable* aReplyRunnable)
136 : mReplyRunnable(aReplyRunnable)
137 {
138 MOZ_COUNT_CTOR(BluetoothRequestChild);
139 MOZ_ASSERT(aReplyRunnable);
140 }
142 BluetoothRequestChild::~BluetoothRequestChild()
143 {
144 MOZ_COUNT_DTOR(BluetoothRequestChild);
145 }
147 void
148 BluetoothRequestChild::ActorDestroy(ActorDestroyReason aWhy)
149 {
150 // Nothing needed here.
151 }
153 bool
154 BluetoothRequestChild::Recv__delete__(const BluetoothReply& aReply)
155 {
156 MOZ_ASSERT(NS_IsMainThread());
157 MOZ_ASSERT(mReplyRunnable);
159 nsRefPtr<BluetoothReplyRunnable> replyRunnable;
160 mReplyRunnable.swap(replyRunnable);
162 if (replyRunnable) {
163 // XXXbent Need to fix this, it copies unnecessarily.
164 replyRunnable->SetReply(new BluetoothReply(aReply));
165 return NS_SUCCEEDED(NS_DispatchToCurrentThread(replyRunnable));
166 }
168 return true;
169 }