michael@0: /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "base/basictypes.h" michael@0: michael@0: #include "BluetoothChild.h" michael@0: michael@0: #include "mozilla/Assertions.h" michael@0: #include "nsDebug.h" michael@0: #include "nsISupportsImpl.h" michael@0: #include "nsThreadUtils.h" michael@0: michael@0: #include "BluetoothReplyRunnable.h" michael@0: #include "BluetoothService.h" michael@0: #include "BluetoothServiceChildProcess.h" michael@0: michael@0: USING_BLUETOOTH_NAMESPACE michael@0: michael@0: namespace { michael@0: michael@0: BluetoothServiceChildProcess* sBluetoothService; michael@0: michael@0: } // anonymous namespace michael@0: michael@0: /******************************************************************************* michael@0: * BluetoothChild michael@0: ******************************************************************************/ michael@0: michael@0: BluetoothChild::BluetoothChild(BluetoothServiceChildProcess* aBluetoothService) michael@0: : mShutdownState(Running) michael@0: { michael@0: MOZ_COUNT_CTOR(BluetoothChild); michael@0: MOZ_ASSERT(!sBluetoothService); michael@0: MOZ_ASSERT(aBluetoothService); michael@0: michael@0: sBluetoothService = aBluetoothService; michael@0: } michael@0: michael@0: BluetoothChild::~BluetoothChild() michael@0: { michael@0: MOZ_COUNT_DTOR(BluetoothChild); michael@0: MOZ_ASSERT(sBluetoothService); michael@0: MOZ_ASSERT(mShutdownState == Dead); michael@0: michael@0: sBluetoothService = nullptr; michael@0: } michael@0: michael@0: void michael@0: BluetoothChild::BeginShutdown() michael@0: { michael@0: // Only do something here if we haven't yet begun the shutdown sequence. michael@0: if (mShutdownState == Running) { michael@0: SendStopNotifying(); michael@0: mShutdownState = SentStopNotifying; michael@0: } michael@0: } michael@0: michael@0: void michael@0: BluetoothChild::ActorDestroy(ActorDestroyReason aWhy) michael@0: { michael@0: MOZ_ASSERT(sBluetoothService); michael@0: michael@0: sBluetoothService->NoteDeadActor(); michael@0: michael@0: #ifdef DEBUG michael@0: mShutdownState = Dead; michael@0: #endif michael@0: } michael@0: michael@0: bool michael@0: BluetoothChild::RecvNotify(const BluetoothSignal& aSignal) michael@0: { michael@0: MOZ_ASSERT(sBluetoothService); michael@0: michael@0: sBluetoothService->DistributeSignal(aSignal); michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: BluetoothChild::RecvEnabled(const bool& aEnabled) michael@0: { michael@0: MOZ_ASSERT(sBluetoothService); michael@0: michael@0: sBluetoothService->SetEnabled(aEnabled); michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: BluetoothChild::RecvBeginShutdown() michael@0: { michael@0: if (mShutdownState != Running && mShutdownState != SentStopNotifying) { michael@0: MOZ_ASSERT(false, "Bad state!"); michael@0: return false; michael@0: } michael@0: michael@0: SendStopNotifying(); michael@0: mShutdownState = SentStopNotifying; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: BluetoothChild::RecvNotificationsStopped() michael@0: { michael@0: if (mShutdownState != SentStopNotifying) { michael@0: MOZ_ASSERT(false, "Bad state!"); michael@0: return false; michael@0: } michael@0: michael@0: Send__delete__(this); michael@0: return true; michael@0: } michael@0: michael@0: PBluetoothRequestChild* michael@0: BluetoothChild::AllocPBluetoothRequestChild(const Request& aRequest) michael@0: { michael@0: MOZ_CRASH("Caller is supposed to manually construct a request!"); michael@0: } michael@0: michael@0: bool michael@0: BluetoothChild::DeallocPBluetoothRequestChild(PBluetoothRequestChild* aActor) michael@0: { michael@0: delete aActor; michael@0: return true; michael@0: } michael@0: michael@0: /******************************************************************************* michael@0: * BluetoothRequestChild michael@0: ******************************************************************************/ michael@0: michael@0: BluetoothRequestChild::BluetoothRequestChild( michael@0: BluetoothReplyRunnable* aReplyRunnable) michael@0: : mReplyRunnable(aReplyRunnable) michael@0: { michael@0: MOZ_COUNT_CTOR(BluetoothRequestChild); michael@0: MOZ_ASSERT(aReplyRunnable); michael@0: } michael@0: michael@0: BluetoothRequestChild::~BluetoothRequestChild() michael@0: { michael@0: MOZ_COUNT_DTOR(BluetoothRequestChild); michael@0: } michael@0: michael@0: void michael@0: BluetoothRequestChild::ActorDestroy(ActorDestroyReason aWhy) michael@0: { michael@0: // Nothing needed here. michael@0: } michael@0: michael@0: bool michael@0: BluetoothRequestChild::Recv__delete__(const BluetoothReply& aReply) michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: MOZ_ASSERT(mReplyRunnable); michael@0: michael@0: nsRefPtr replyRunnable; michael@0: mReplyRunnable.swap(replyRunnable); michael@0: michael@0: if (replyRunnable) { michael@0: // XXXbent Need to fix this, it copies unnecessarily. michael@0: replyRunnable->SetReply(new BluetoothReply(aReply)); michael@0: return NS_SUCCEEDED(NS_DispatchToCurrentThread(replyRunnable)); michael@0: } michael@0: michael@0: return true; michael@0: }