diff --git a/dom/system/gonk/SystemWorkerManager.cpp b/dom/system/gonk/SystemWorkerManager.cpp index a5622f57c995..4ae4baac50ef 100644 --- a/dom/system/gonk/SystemWorkerManager.cpp +++ b/dom/system/gonk/SystemWorkerManager.cpp @@ -119,7 +119,7 @@ SystemWorkerManager::Shutdown() ShutdownAutoMounter(); #ifdef MOZ_B2G_RIL - RilConsumer::Shutdown(); + RilWorker::Shutdown(); #endif nsCOMPtr wifi(do_QueryInterface(mWifiWorker)); @@ -201,7 +201,7 @@ SystemWorkerManager::RegisterRilWorker(unsigned int aClientId, return NS_ERROR_FAILURE; } - return RilConsumer::Register(aClientId, wctd); + return RilWorker::Register(aClientId, wctd); #endif // MOZ_B2G_RIL } diff --git a/ipc/ril/Ril.cpp b/ipc/ril/Ril.cpp index 66c7a8d4c39c..3b73aad0d7d7 100644 --- a/ipc/ril/Ril.cpp +++ b/ipc/ril/Ril.cpp @@ -10,8 +10,19 @@ #include #include #include // For gethostbyname. +#include "jsfriendapi.h" +#include "mozilla/ArrayUtils.h" +#include "mozilla/dom/workers/Workers.h" +#include "mozilla/ipc/StreamSocket.h" +#include "mozilla/ipc/StreamSocketConsumer.h" +#include "nsTArray.h" +#include "nsThreadUtils.h" // For NS_IsMainThread. +#include "RilConnector.h" +#ifdef CHROMIUM_LOG #undef CHROMIUM_LOG +#endif + #if defined(MOZ_WIDGET_GONK) #include #define CHROMIUM_LOG(args...) __android_log_print(ANDROID_LOG_INFO, "Gonk", args) @@ -19,20 +30,47 @@ #define CHROMIUM_LOG(args...) printf(args); #endif -#include "jsfriendapi.h" -#include "mozilla/ArrayUtils.h" -#include "nsTArray.h" -#include "nsThreadUtils.h" // For NS_IsMainThread. -#include "RilConnector.h" - USING_WORKERS_NAMESPACE -using namespace mozilla::ipc; -namespace { +namespace mozilla { +namespace ipc { + +class RilConsumer; static const char RIL_SOCKET_NAME[] = "/dev/socket/rilproxy"; -static nsTArray> sRilConsumers; +static nsTArray> sRilConsumers; + +// +// RilConsumer +// + +class RilConsumer final : public StreamSocketConsumer +{ +public: + friend class RilWorker; + + RilConsumer(unsigned long aClientId, + WorkerCrossThreadDispatcher* aDispatcher); + + void Send(UnixSocketRawData* aRawData); + void Close(); + + // Methods for |StreamSocketConsumer| + // + + void ReceiveSocketData(int aIndex, + nsAutoPtr& aBuffer) override; + void OnConnectSuccess(int aIndex) override; + void OnConnectError(int aIndex) override; + void OnDisconnect(int aIndex) override; + +private: + nsRefPtr mSocket; + nsRefPtr mDispatcher; + nsCString mAddress; + bool mShutdown; +}; class ConnectWorkerToRIL final : public WorkerTask { @@ -193,11 +231,6 @@ DispatchRILEvent::RunTask(JSContext* aCx) return JS_CallFunctionName(aCx, obj, "onRILMessage", args, &rval); } -} // namespace - -namespace mozilla { -namespace ipc { - RilConsumer::RilConsumer(unsigned long aClientId, WorkerCrossThreadDispatcher* aDispatcher) : mDispatcher(aDispatcher) @@ -218,47 +251,6 @@ RilConsumer::RilConsumer(unsigned long aClientId, mSocket->Connect(new RilConnector(mAddress, aClientId)); } -nsresult -RilConsumer::Register(unsigned int aClientId, - WorkerCrossThreadDispatcher* aDispatcher) -{ - MOZ_ASSERT(NS_IsMainThread()); - - sRilConsumers.EnsureLengthAtLeast(aClientId + 1); - - if (sRilConsumers[aClientId]) { - NS_WARNING("RilConsumer already registered"); - return NS_ERROR_FAILURE; - } - - nsRefPtr connection = new ConnectWorkerToRIL(); - if (!aDispatcher->PostTask(connection)) { - NS_WARNING("Failed to connect worker to ril"); - return NS_ERROR_UNEXPECTED; - } - - // Now that we're set up, connect ourselves to the RIL thread. - sRilConsumers[aClientId] = new RilConsumer(aClientId, aDispatcher); - return NS_OK; -} - -void -RilConsumer::Shutdown() -{ - MOZ_ASSERT(NS_IsMainThread()); - - for (unsigned long i = 0; i < sRilConsumers.Length(); i++) { - nsAutoPtr instance(sRilConsumers[i]); - if (!instance) { - continue; - } - - instance->mShutdown = true; - instance->Close(); - instance = nullptr; - } -} - void RilConsumer::Send(UnixSocketRawData* aRawData) { @@ -316,5 +308,50 @@ RilConsumer::OnDisconnect(int aIndex) mSocket->GetSuggestedConnectDelayMs()); } +// +// RilWorker +// + +nsresult +RilWorker::Register(unsigned int aClientId, + WorkerCrossThreadDispatcher* aDispatcher) +{ + MOZ_ASSERT(NS_IsMainThread()); + + sRilConsumers.EnsureLengthAtLeast(aClientId + 1); + + if (sRilConsumers[aClientId]) { + NS_WARNING("RilConsumer already registered"); + return NS_ERROR_FAILURE; + } + + nsRefPtr connection = new ConnectWorkerToRIL(); + if (!aDispatcher->PostTask(connection)) { + NS_WARNING("Failed to connect worker to ril"); + return NS_ERROR_UNEXPECTED; + } + + // Now that we're set up, connect ourselves to the RIL thread. + sRilConsumers[aClientId] = new RilConsumer(aClientId, aDispatcher); + return NS_OK; +} + +void +RilWorker::Shutdown() +{ + MOZ_ASSERT(NS_IsMainThread()); + + for (unsigned long i = 0; i < sRilConsumers.Length(); i++) { + nsAutoPtr instance(sRilConsumers[i]); + if (!instance) { + continue; + } + + instance->mShutdown = true; + instance->Close(); + instance = nullptr; + } +} + } // namespace ipc } // namespace mozilla diff --git a/ipc/ril/Ril.h b/ipc/ril/Ril.h index 5d0ae62eff02..a8172c4db12e 100644 --- a/ipc/ril/Ril.h +++ b/ipc/ril/Ril.h @@ -7,42 +7,28 @@ #ifndef mozilla_ipc_Ril_h #define mozilla_ipc_Ril_h 1 -#include -#include -#include +#include "nsError.h" namespace mozilla { + +namespace dom { +namespace workers { + +class WorkerCrossThreadDispatcher; + +} // namespace workers +} // namespace dom + namespace ipc { -class RilConsumer final : public StreamSocketConsumer +class RilWorker final { public: static nsresult Register( unsigned int aClientId, mozilla::dom::workers::WorkerCrossThreadDispatcher* aDispatcher); + static void Shutdown(); - - void Send(UnixSocketRawData* aRawData); - -private: - RilConsumer(unsigned long aClientId, - mozilla::dom::workers::WorkerCrossThreadDispatcher* aDispatcher); - - void Close(); - - // Methods for |StreamSocketConsumer| - // - - void ReceiveSocketData(int aIndex, - nsAutoPtr& aBuffer) override; - void OnConnectSuccess(int aIndex) override; - void OnConnectError(int aIndex) override; - void OnDisconnect(int aIndex) override; - - nsRefPtr mSocket; - nsRefPtr mDispatcher; - nsCString mAddress; - bool mShutdown; }; } // namespace ipc