|
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/. */ |
|
6 |
|
7 #include "base/basictypes.h" |
|
8 |
|
9 #include "BluetoothChild.h" |
|
10 |
|
11 #include "mozilla/Assertions.h" |
|
12 #include "nsDebug.h" |
|
13 #include "nsISupportsImpl.h" |
|
14 #include "nsThreadUtils.h" |
|
15 |
|
16 #include "BluetoothReplyRunnable.h" |
|
17 #include "BluetoothService.h" |
|
18 #include "BluetoothServiceChildProcess.h" |
|
19 |
|
20 USING_BLUETOOTH_NAMESPACE |
|
21 |
|
22 namespace { |
|
23 |
|
24 BluetoothServiceChildProcess* sBluetoothService; |
|
25 |
|
26 } // anonymous namespace |
|
27 |
|
28 /******************************************************************************* |
|
29 * BluetoothChild |
|
30 ******************************************************************************/ |
|
31 |
|
32 BluetoothChild::BluetoothChild(BluetoothServiceChildProcess* aBluetoothService) |
|
33 : mShutdownState(Running) |
|
34 { |
|
35 MOZ_COUNT_CTOR(BluetoothChild); |
|
36 MOZ_ASSERT(!sBluetoothService); |
|
37 MOZ_ASSERT(aBluetoothService); |
|
38 |
|
39 sBluetoothService = aBluetoothService; |
|
40 } |
|
41 |
|
42 BluetoothChild::~BluetoothChild() |
|
43 { |
|
44 MOZ_COUNT_DTOR(BluetoothChild); |
|
45 MOZ_ASSERT(sBluetoothService); |
|
46 MOZ_ASSERT(mShutdownState == Dead); |
|
47 |
|
48 sBluetoothService = nullptr; |
|
49 } |
|
50 |
|
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 } |
|
60 |
|
61 void |
|
62 BluetoothChild::ActorDestroy(ActorDestroyReason aWhy) |
|
63 { |
|
64 MOZ_ASSERT(sBluetoothService); |
|
65 |
|
66 sBluetoothService->NoteDeadActor(); |
|
67 |
|
68 #ifdef DEBUG |
|
69 mShutdownState = Dead; |
|
70 #endif |
|
71 } |
|
72 |
|
73 bool |
|
74 BluetoothChild::RecvNotify(const BluetoothSignal& aSignal) |
|
75 { |
|
76 MOZ_ASSERT(sBluetoothService); |
|
77 |
|
78 sBluetoothService->DistributeSignal(aSignal); |
|
79 return true; |
|
80 } |
|
81 |
|
82 bool |
|
83 BluetoothChild::RecvEnabled(const bool& aEnabled) |
|
84 { |
|
85 MOZ_ASSERT(sBluetoothService); |
|
86 |
|
87 sBluetoothService->SetEnabled(aEnabled); |
|
88 return true; |
|
89 } |
|
90 |
|
91 bool |
|
92 BluetoothChild::RecvBeginShutdown() |
|
93 { |
|
94 if (mShutdownState != Running && mShutdownState != SentStopNotifying) { |
|
95 MOZ_ASSERT(false, "Bad state!"); |
|
96 return false; |
|
97 } |
|
98 |
|
99 SendStopNotifying(); |
|
100 mShutdownState = SentStopNotifying; |
|
101 |
|
102 return true; |
|
103 } |
|
104 |
|
105 bool |
|
106 BluetoothChild::RecvNotificationsStopped() |
|
107 { |
|
108 if (mShutdownState != SentStopNotifying) { |
|
109 MOZ_ASSERT(false, "Bad state!"); |
|
110 return false; |
|
111 } |
|
112 |
|
113 Send__delete__(this); |
|
114 return true; |
|
115 } |
|
116 |
|
117 PBluetoothRequestChild* |
|
118 BluetoothChild::AllocPBluetoothRequestChild(const Request& aRequest) |
|
119 { |
|
120 MOZ_CRASH("Caller is supposed to manually construct a request!"); |
|
121 } |
|
122 |
|
123 bool |
|
124 BluetoothChild::DeallocPBluetoothRequestChild(PBluetoothRequestChild* aActor) |
|
125 { |
|
126 delete aActor; |
|
127 return true; |
|
128 } |
|
129 |
|
130 /******************************************************************************* |
|
131 * BluetoothRequestChild |
|
132 ******************************************************************************/ |
|
133 |
|
134 BluetoothRequestChild::BluetoothRequestChild( |
|
135 BluetoothReplyRunnable* aReplyRunnable) |
|
136 : mReplyRunnable(aReplyRunnable) |
|
137 { |
|
138 MOZ_COUNT_CTOR(BluetoothRequestChild); |
|
139 MOZ_ASSERT(aReplyRunnable); |
|
140 } |
|
141 |
|
142 BluetoothRequestChild::~BluetoothRequestChild() |
|
143 { |
|
144 MOZ_COUNT_DTOR(BluetoothRequestChild); |
|
145 } |
|
146 |
|
147 void |
|
148 BluetoothRequestChild::ActorDestroy(ActorDestroyReason aWhy) |
|
149 { |
|
150 // Nothing needed here. |
|
151 } |
|
152 |
|
153 bool |
|
154 BluetoothRequestChild::Recv__delete__(const BluetoothReply& aReply) |
|
155 { |
|
156 MOZ_ASSERT(NS_IsMainThread()); |
|
157 MOZ_ASSERT(mReplyRunnable); |
|
158 |
|
159 nsRefPtr<BluetoothReplyRunnable> replyRunnable; |
|
160 mReplyRunnable.swap(replyRunnable); |
|
161 |
|
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 } |
|
167 |
|
168 return true; |
|
169 } |