2015-01-07 13:19:59 +03:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-12-07 14:58:56 +04:00
|
|
|
|
2013-10-29 12:12:45 +04:00
|
|
|
#include "mozilla/ipc/Ril.h"
|
2011-12-07 14:58:56 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2015-07-17 13:10:01 +03:00
|
|
|
#include "jsfriendapi.h"
|
|
|
|
#include "mozilla/ArrayUtils.h"
|
2015-07-17 13:10:01 +03:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2015-07-17 13:10:01 +03:00
|
|
|
#include "mozilla/dom/workers/Workers.h"
|
2015-07-17 13:10:01 +03:00
|
|
|
#include "mozilla/ipc/RilSocket.h"
|
|
|
|
#include "mozilla/ipc/RilSocketConsumer.h"
|
2015-07-17 13:10:01 +03:00
|
|
|
#include "nsThreadUtils.h" // For NS_IsMainThread.
|
|
|
|
#include "RilConnector.h"
|
2011-12-07 14:58:56 +04:00
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
#ifdef CHROMIUM_LOG
|
2013-12-17 22:26:45 +04:00
|
|
|
#undef CHROMIUM_LOG
|
2015-07-17 13:10:01 +03:00
|
|
|
#endif
|
|
|
|
|
2011-12-07 14:58:56 +04:00
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
|
|
|
#include <android/log.h>
|
2013-12-17 22:26:45 +04:00
|
|
|
#define CHROMIUM_LOG(args...) __android_log_print(ANDROID_LOG_INFO, "Gonk", args)
|
2011-12-07 14:58:56 +04:00
|
|
|
#else
|
2013-12-17 22:26:45 +04:00
|
|
|
#define CHROMIUM_LOG(args...) printf(args);
|
2011-12-07 14:58:56 +04:00
|
|
|
#endif
|
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
USING_WORKERS_NAMESPACE;
|
|
|
|
using namespace JS;
|
2015-07-17 13:10:01 +03:00
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
class RilConsumer;
|
2013-02-01 16:28:22 +04:00
|
|
|
|
2015-01-07 13:19:59 +03:00
|
|
|
static const char RIL_SOCKET_NAME[] = "/dev/socket/rilproxy";
|
2011-12-07 14:58:56 +04:00
|
|
|
|
2016-04-05 14:40:11 +03:00
|
|
|
static nsTArray<UniquePtr<RilConsumer>> sRilConsumers;
|
2015-07-17 13:10:01 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
// RilConsumer
|
|
|
|
//
|
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
class RilConsumer final : public RilSocketConsumer
|
2015-07-17 13:10:01 +03:00
|
|
|
{
|
|
|
|
public:
|
2015-07-17 13:10:01 +03:00
|
|
|
RilConsumer();
|
2015-07-17 13:10:01 +03:00
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
nsresult ConnectWorkerToRIL(JSContext* aCx);
|
2015-07-17 13:10:01 +03:00
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
nsresult Register(unsigned long aClientId,
|
|
|
|
WorkerCrossThreadDispatcher* aDispatcher);
|
|
|
|
void Unregister();
|
|
|
|
|
|
|
|
// Methods for |RilSocketConsumer|
|
2015-07-17 13:10:01 +03:00
|
|
|
//
|
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
void ReceiveSocketData(JSContext* aCx,
|
|
|
|
int aIndex,
|
2016-04-05 14:40:11 +03:00
|
|
|
UniquePtr<UnixSocketBuffer>& aBuffer) override;
|
2015-07-17 13:10:01 +03:00
|
|
|
void OnConnectSuccess(int aIndex) override;
|
|
|
|
void OnConnectError(int aIndex) override;
|
|
|
|
void OnDisconnect(int aIndex) override;
|
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
protected:
|
|
|
|
static bool PostRILMessage(JSContext* aCx, unsigned aArgc, Value* aVp);
|
|
|
|
|
|
|
|
nsresult Send(JSContext* aCx, const CallArgs& aArgs);
|
|
|
|
nsresult Receive(JSContext* aCx,
|
|
|
|
uint32_t aClientId,
|
|
|
|
const UnixSocketBuffer* aBuffer);
|
|
|
|
void Close();
|
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<RilSocket> mSocket;
|
2015-07-17 13:10:01 +03:00
|
|
|
nsCString mAddress;
|
|
|
|
bool mShutdown;
|
|
|
|
};
|
2015-07-14 17:57:00 +03:00
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
RilConsumer::RilConsumer()
|
|
|
|
: mShutdown(false)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RilConsumer::ConnectWorkerToRIL(JSContext* aCx)
|
2015-07-14 17:57:00 +03:00
|
|
|
{
|
2015-07-17 13:10:01 +03:00
|
|
|
// Set up the postRILMessage on the function for worker -> RIL thread
|
|
|
|
// communication.
|
|
|
|
Rooted<JSObject*> workerGlobal(aCx, CurrentGlobalOrNull(aCx));
|
|
|
|
|
|
|
|
// Check whether |postRILMessage| has been defined. No one but this class
|
|
|
|
// should ever define |postRILMessage| in a RIL worker.
|
|
|
|
Rooted<Value> val(aCx);
|
|
|
|
if (!JS_GetProperty(aCx, workerGlobal, "postRILMessage", &val)) {
|
2016-03-01 21:49:21 +03:00
|
|
|
// Just returning failure here will cause the exception on the JSContext to
|
|
|
|
// be reported as needed.
|
2015-07-17 13:10:01 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that |postRILMessage| is a function.
|
|
|
|
if (JSTYPE_FUNCTION == JS_TypeOfValue(aCx, val)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSFunction* postRILMessage = JS_DefineFunction(aCx, workerGlobal,
|
|
|
|
"postRILMessage",
|
|
|
|
PostRILMessage, 2, 0);
|
|
|
|
if (NS_WARN_IF(!postRILMessage)) {
|
2016-03-01 21:49:21 +03:00
|
|
|
// Just returning failure here will cause the exception on the JSContext to
|
|
|
|
// be reported as needed.
|
2015-07-17 13:10:01 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RilConsumer::Register(unsigned long aClientId,
|
|
|
|
WorkerCrossThreadDispatcher* aDispatcher)
|
|
|
|
{
|
|
|
|
// Only append client id after RIL_SOCKET_NAME when it's not connected to
|
|
|
|
// the first(0) rilproxy for compatibility.
|
|
|
|
if (!aClientId) {
|
|
|
|
mAddress = RIL_SOCKET_NAME;
|
|
|
|
} else {
|
|
|
|
struct sockaddr_un addr_un;
|
|
|
|
snprintf(addr_un.sun_path, sizeof addr_un.sun_path, "%s%lu",
|
|
|
|
RIL_SOCKET_NAME, aClientId);
|
|
|
|
mAddress = addr_un.sun_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSocket = new RilSocket(aDispatcher, this, aClientId);
|
|
|
|
|
|
|
|
nsresult rv = mSocket->Connect(new RilConnector(mAddress, aClientId));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-10-29 12:12:45 +04:00
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
void
|
|
|
|
RilConsumer::Unregister()
|
2015-07-14 17:57:00 +03:00
|
|
|
{
|
2015-07-17 13:10:01 +03:00
|
|
|
mShutdown = true;
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RilConsumer::PostRILMessage(JSContext* aCx, unsigned aArgc, Value* aVp)
|
|
|
|
{
|
|
|
|
CallArgs args = CallArgsFromVp(aArgc, aVp);
|
2015-01-07 13:19:59 +03:00
|
|
|
|
|
|
|
if (args.length() != 2) {
|
2016-08-14 14:39:28 +03:00
|
|
|
JS_ReportErrorASCII(aCx, "Expecting two arguments with the RIL message");
|
2015-01-07 13:19:59 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int clientId = args[0].toInt32();
|
2015-07-17 13:10:01 +03:00
|
|
|
|
|
|
|
if ((ssize_t)sRilConsumers.Length() <= clientId || !sRilConsumers[clientId]) {
|
|
|
|
// Probably shutting down.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = sRilConsumers[clientId]->Send(aCx, args);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RilConsumer::Send(JSContext* aCx, const CallArgs& aArgs)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!mSocket) ||
|
|
|
|
NS_WARN_IF(mSocket->GetConnectionStatus() == SOCKET_DISCONNECTED)) {
|
|
|
|
// Probably shutting down.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-07-14 17:57:00 +03:00
|
|
|
|
2016-04-05 14:40:11 +03:00
|
|
|
UniquePtr<UnixSocketRawData> raw;
|
2015-07-14 17:57:00 +03:00
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
Value v = aArgs[1];
|
|
|
|
|
2015-01-07 13:19:59 +03:00
|
|
|
if (v.isString()) {
|
|
|
|
JSAutoByteString abs;
|
2015-07-17 13:10:01 +03:00
|
|
|
Rooted<JSString*> str(aCx, v.toString());
|
2015-01-07 13:19:59 +03:00
|
|
|
if (!abs.encodeUtf8(aCx, str)) {
|
2015-07-17 13:10:01 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2013-10-29 12:12:45 +04:00
|
|
|
}
|
|
|
|
|
2016-04-05 14:40:11 +03:00
|
|
|
raw = MakeUnique<UnixSocketRawData>(abs.ptr(), abs.length());
|
2015-01-07 13:19:59 +03:00
|
|
|
} else if (!v.isPrimitive()) {
|
|
|
|
JSObject* obj = v.toObjectOrNull();
|
|
|
|
if (!JS_IsTypedArrayObject(obj)) {
|
2016-08-14 14:39:28 +03:00
|
|
|
JS_ReportErrorASCII(aCx, "Object passed in wasn't a typed array");
|
2015-07-17 13:10:01 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2013-10-29 12:12:45 +04:00
|
|
|
}
|
|
|
|
|
2015-01-07 13:19:59 +03:00
|
|
|
uint32_t type = JS_GetArrayBufferViewType(obj);
|
|
|
|
if (type != js::Scalar::Int8 &&
|
|
|
|
type != js::Scalar::Uint8 &&
|
|
|
|
type != js::Scalar::Uint8Clamped) {
|
2016-08-14 14:39:28 +03:00
|
|
|
JS_ReportErrorASCII(aCx, "Typed array data is not octets");
|
2015-07-17 13:10:01 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-10-07 21:44:07 +04:00
|
|
|
}
|
2013-10-29 12:12:45 +04:00
|
|
|
|
2015-01-07 13:19:59 +03:00
|
|
|
size_t size = JS_GetTypedArrayByteLength(obj);
|
2015-11-30 10:58:54 +03:00
|
|
|
bool isShared;
|
|
|
|
void* data;
|
|
|
|
{
|
|
|
|
AutoCheckCannotGC nogc;
|
|
|
|
data = JS_GetArrayBufferViewData(obj, &isShared, nogc);
|
|
|
|
}
|
|
|
|
if (isShared) {
|
2016-08-14 14:39:28 +03:00
|
|
|
JS_ReportErrorASCII(
|
2015-11-30 10:58:54 +03:00
|
|
|
aCx, "Incorrect argument. Shared memory not supported");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2016-04-05 14:40:11 +03:00
|
|
|
raw = MakeUnique<UnixSocketRawData>(data, size);
|
2015-01-07 13:19:59 +03:00
|
|
|
} else {
|
2016-08-14 14:39:28 +03:00
|
|
|
JS_ReportErrorASCII(
|
2015-01-07 13:19:59 +03:00
|
|
|
aCx, "Incorrect argument. Expecting a string or a typed array");
|
2015-07-17 13:10:01 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-01-07 13:19:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!raw) {
|
2016-08-14 14:39:28 +03:00
|
|
|
JS_ReportErrorASCII(aCx, "Unable to post to RIL");
|
2015-07-17 13:10:01 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-07-17 13:10:01 +03:00
|
|
|
}
|
|
|
|
|
2016-04-05 14:40:11 +03:00
|
|
|
mSocket->SendSocketData(raw.release());
|
2015-07-17 13:10:01 +03:00
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
return NS_OK;
|
2013-10-29 12:12:45 +04:00
|
|
|
}
|
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
nsresult
|
|
|
|
RilConsumer::Receive(JSContext* aCx,
|
|
|
|
uint32_t aClientId,
|
|
|
|
const UnixSocketBuffer* aBuffer)
|
2013-10-29 12:12:45 +04:00
|
|
|
{
|
2015-07-17 13:10:01 +03:00
|
|
|
MOZ_ASSERT(aBuffer);
|
2015-01-07 13:19:59 +03:00
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
Rooted<JSObject*> obj(aCx, CurrentGlobalOrNull(aCx));
|
2015-07-15 19:53:28 +03:00
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
Rooted<JSObject*> array(aCx, JS_NewUint8Array(aCx, aBuffer->GetSize()));
|
|
|
|
if (NS_WARN_IF(!array)) {
|
2016-03-01 21:49:21 +03:00
|
|
|
// Just suppress the exception, since our callers don't have a way to
|
|
|
|
// indicate they failed.
|
|
|
|
JS_ClearPendingException(aCx);
|
2015-07-17 13:10:01 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-01-07 13:19:59 +03:00
|
|
|
}
|
2015-07-17 13:10:01 +03:00
|
|
|
{
|
|
|
|
AutoCheckCannotGC nogc;
|
2015-11-30 10:58:54 +03:00
|
|
|
bool isShared;
|
|
|
|
memcpy(JS_GetArrayBufferViewData(array, &isShared, nogc),
|
2015-07-17 13:10:01 +03:00
|
|
|
aBuffer->GetData(), aBuffer->GetSize());
|
2015-11-30 10:58:54 +03:00
|
|
|
MOZ_ASSERT(!isShared); // Array was constructed above.
|
2015-01-07 13:19:59 +03:00
|
|
|
}
|
2011-12-07 14:58:56 +04:00
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
AutoValueArray<2> args(aCx);
|
|
|
|
args[0].setNumber(aClientId);
|
|
|
|
args[1].setObject(*array);
|
2015-07-17 13:10:01 +03:00
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
Rooted<Value> rval(aCx);
|
|
|
|
JS_CallFunctionName(aCx, obj, "onRILMessage", args, &rval);
|
2016-03-01 21:49:21 +03:00
|
|
|
// Just suppress the exception, since our callers don't have a way to
|
|
|
|
// indicate they failed.
|
|
|
|
JS_ClearPendingException(aCx);
|
2015-07-17 13:10:01 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-15 19:53:28 +03:00
|
|
|
void
|
|
|
|
RilConsumer::Close()
|
2015-07-14 17:57:00 +03:00
|
|
|
{
|
2015-07-15 19:53:28 +03:00
|
|
|
if (mSocket) {
|
|
|
|
mSocket->Close();
|
|
|
|
mSocket = nullptr;
|
2015-07-14 17:57:00 +03:00
|
|
|
}
|
2015-07-15 19:53:28 +03:00
|
|
|
}
|
2015-07-14 17:57:00 +03:00
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
// |RilSocketConnector|
|
2015-07-14 17:57:00 +03:00
|
|
|
|
2015-07-15 19:53:28 +03:00
|
|
|
void
|
2015-07-17 13:10:01 +03:00
|
|
|
RilConsumer::ReceiveSocketData(JSContext* aCx,
|
|
|
|
int aIndex,
|
2016-04-05 14:40:11 +03:00
|
|
|
UniquePtr<UnixSocketBuffer>& aBuffer)
|
2015-07-14 17:57:00 +03:00
|
|
|
{
|
2016-04-05 14:40:11 +03:00
|
|
|
Receive(aCx, (uint32_t)aIndex, aBuffer.get());
|
2015-07-14 17:57:00 +03:00
|
|
|
}
|
|
|
|
|
2015-07-15 19:53:28 +03:00
|
|
|
void
|
|
|
|
RilConsumer::OnConnectSuccess(int aIndex)
|
2015-07-14 17:57:00 +03:00
|
|
|
{
|
2015-07-15 19:53:28 +03:00
|
|
|
// Nothing to do here.
|
|
|
|
CHROMIUM_LOG("RIL[%d]: %s\n", aIndex, __FUNCTION__);
|
|
|
|
}
|
2015-07-14 17:57:00 +03:00
|
|
|
|
2015-07-14 17:57:00 +03:00
|
|
|
void
|
2015-07-15 19:53:28 +03:00
|
|
|
RilConsumer::OnConnectError(int aIndex)
|
2015-07-14 17:57:00 +03:00
|
|
|
{
|
2015-07-15 19:53:28 +03:00
|
|
|
CHROMIUM_LOG("RIL[%d]: %s\n", aIndex, __FUNCTION__);
|
|
|
|
Close();
|
|
|
|
}
|
2015-07-14 17:57:00 +03:00
|
|
|
|
2015-07-15 19:53:28 +03:00
|
|
|
void
|
|
|
|
RilConsumer::OnDisconnect(int aIndex)
|
|
|
|
{
|
|
|
|
CHROMIUM_LOG("RIL[%d]: %s\n", aIndex, __FUNCTION__);
|
|
|
|
if (mShutdown) {
|
2015-07-14 17:57:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-07-15 19:53:28 +03:00
|
|
|
mSocket->Connect(new RilConnector(mAddress, aIndex),
|
|
|
|
mSocket->GetSuggestedConnectDelayMs());
|
2015-07-14 17:57:00 +03:00
|
|
|
}
|
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
//
|
|
|
|
// RilWorker
|
|
|
|
//
|
|
|
|
|
2016-04-05 14:40:11 +03:00
|
|
|
nsTArray<UniquePtr<RilWorker>> RilWorker::sRilWorkers;
|
2015-07-17 13:10:01 +03:00
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
nsresult
|
|
|
|
RilWorker::Register(unsigned int aClientId,
|
|
|
|
WorkerCrossThreadDispatcher* aDispatcher)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
sRilWorkers.EnsureLengthAtLeast(aClientId + 1);
|
2015-07-17 13:10:01 +03:00
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
if (sRilWorkers[aClientId]) {
|
|
|
|
NS_WARNING("RilWorkers already registered");
|
2015-07-17 13:10:01 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
// Now that we're set up, connect ourselves to the RIL thread.
|
2016-04-05 14:40:11 +03:00
|
|
|
sRilWorkers[aClientId] = MakeUnique<RilWorker>(aDispatcher);
|
2015-07-17 13:10:01 +03:00
|
|
|
|
|
|
|
nsresult rv = sRilWorkers[aClientId]->RegisterConsumer(aClientId);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2015-07-17 13:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RilWorker::Shutdown()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
for (size_t i = 0; i < sRilWorkers.Length(); ++i) {
|
|
|
|
if (!sRilWorkers[i]) {
|
2015-07-17 13:10:01 +03:00
|
|
|
continue;
|
|
|
|
}
|
2015-07-17 13:10:01 +03:00
|
|
|
sRilWorkers[i]->UnregisterConsumer(i);
|
|
|
|
sRilWorkers[i] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2015-07-17 13:10:01 +03:00
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
RilWorker::RilWorker(WorkerCrossThreadDispatcher* aDispatcher)
|
|
|
|
: mDispatcher(aDispatcher)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mDispatcher);
|
|
|
|
}
|
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
class RilWorker::RegisterConsumerTask : public WorkerTask
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RegisterConsumerTask(unsigned int aClientId,
|
|
|
|
WorkerCrossThreadDispatcher* aDispatcher)
|
|
|
|
: mClientId(aClientId)
|
|
|
|
, mDispatcher(aDispatcher)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mDispatcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RunTask(JSContext* aCx) override
|
|
|
|
{
|
|
|
|
sRilConsumers.EnsureLengthAtLeast(mClientId + 1);
|
|
|
|
|
|
|
|
MOZ_ASSERT(!sRilConsumers[mClientId]);
|
|
|
|
|
2016-04-05 14:40:11 +03:00
|
|
|
auto rilConsumer = MakeUnique<RilConsumer>();
|
2015-07-17 13:10:01 +03:00
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
nsresult rv = rilConsumer->ConnectWorkerToRIL(aCx);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = rilConsumer->Register(mClientId, mDispatcher);
|
2015-07-17 13:10:01 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-05 14:40:11 +03:00
|
|
|
sRilConsumers[mClientId] = Move(rilConsumer);
|
2015-07-17 13:10:01 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned int mClientId;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerCrossThreadDispatcher> mDispatcher;
|
2015-07-17 13:10:01 +03:00
|
|
|
};
|
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
nsresult
|
|
|
|
RilWorker::RegisterConsumer(unsigned int aClientId)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<RegisterConsumerTask> task = new RegisterConsumerTask(aClientId,
|
2015-07-17 13:10:01 +03:00
|
|
|
mDispatcher);
|
|
|
|
if (!mDispatcher->PostTask(task)) {
|
|
|
|
NS_WARNING("Failed to post register-consumer task.");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2015-07-17 13:10:01 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
class RilWorker::UnregisterConsumerTask : public WorkerTask
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UnregisterConsumerTask(unsigned int aClientId)
|
|
|
|
: mClientId(aClientId)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool RunTask(JSContext* aCx) override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mClientId < sRilConsumers.Length());
|
|
|
|
MOZ_ASSERT(sRilConsumers[mClientId]);
|
|
|
|
|
|
|
|
sRilConsumers[mClientId]->Unregister();
|
|
|
|
sRilConsumers[mClientId] = nullptr;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned int mClientId;
|
|
|
|
};
|
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
void
|
|
|
|
RilWorker::UnregisterConsumer(unsigned int aClientId)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<UnregisterConsumerTask> task =
|
2015-07-17 13:10:01 +03:00
|
|
|
new UnregisterConsumerTask(aClientId);
|
2015-07-17 13:10:01 +03:00
|
|
|
|
2015-07-17 13:10:01 +03:00
|
|
|
if (!mDispatcher->PostTask(task)) {
|
|
|
|
NS_WARNING("Failed to post unregister-consumer task.");
|
|
|
|
return;
|
|
|
|
}
|
2015-07-17 13:10:01 +03:00
|
|
|
}
|
|
|
|
|
2011-12-07 14:58:56 +04:00
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|