2011-12-04 23:58:27 -08:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-07-20 02:10:44 -07:00
|
|
|
/* Copyright 2012 Mozilla Foundation and Mozilla contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2011-12-04 23:58:27 -08:00
|
|
|
|
2012-01-09 15:18:23 -08:00
|
|
|
#include "SystemWorkerManager.h"
|
2011-12-06 06:14:55 -08:00
|
|
|
|
2012-01-09 15:18:23 -08:00
|
|
|
#include "nsIJSContextStack.h"
|
2012-08-29 13:35:53 -07:00
|
|
|
#include "nsINetworkManager.h"
|
2011-12-06 06:14:55 -08:00
|
|
|
#include "nsIWifi.h"
|
2012-01-09 15:18:23 -08:00
|
|
|
#include "nsIWorkerHolder.h"
|
|
|
|
#include "nsIXPConnect.h"
|
2011-12-04 23:58:27 -08:00
|
|
|
|
2012-01-14 09:43:00 -08:00
|
|
|
#include "jsfriendapi.h"
|
2012-01-09 15:18:23 -08:00
|
|
|
#include "mozilla/dom/workers/Workers.h"
|
2012-05-24 21:04:32 -07:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2012-08-29 13:35:53 -07:00
|
|
|
#include "mozilla/ipc/Netd.h"
|
2012-05-24 21:04:32 -07:00
|
|
|
#include "AutoMounter.h"
|
2012-10-30 08:34:50 -07:00
|
|
|
#include "TimeZoneSettingObserver.h"
|
2012-05-24 21:04:32 -07:00
|
|
|
#endif
|
2012-01-09 15:18:23 -08:00
|
|
|
#include "mozilla/ipc/Ril.h"
|
2012-12-11 15:15:07 -08:00
|
|
|
#include "nsIObserverService.h"
|
2012-01-09 15:18:23 -08:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2011-12-04 23:58:27 -08:00
|
|
|
#include "nsThreadUtils.h"
|
2012-01-19 12:53:32 -08:00
|
|
|
#include "nsRadioInterfaceLayer.h"
|
2012-03-16 10:05:28 -07:00
|
|
|
#include "WifiWorker.h"
|
2011-12-04 23:58:27 -08:00
|
|
|
|
|
|
|
USING_WORKERS_NAMESPACE
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2012-03-14 15:43:26 -07:00
|
|
|
using namespace mozilla::dom::gonk;
|
2011-12-04 23:58:27 -08:00
|
|
|
using namespace mozilla::ipc;
|
2012-05-24 21:04:32 -07:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
using namespace mozilla::system;
|
|
|
|
#endif
|
2011-12-04 23:58:27 -08:00
|
|
|
|
2012-08-29 13:35:53 -07:00
|
|
|
#define NS_NETWORKMANAGER_CID \
|
|
|
|
{ 0x33901e46, 0x33b8, 0x11e1, \
|
|
|
|
{ 0x98, 0x69, 0xf4, 0x6d, 0x04, 0xd2, 0x5b, 0xcc } }
|
|
|
|
|
2011-12-04 23:58:27 -08:00
|
|
|
namespace {
|
|
|
|
|
2012-01-09 15:18:23 -08:00
|
|
|
NS_DEFINE_CID(kWifiWorkerCID, NS_WIFIWORKER_CID);
|
2012-08-29 13:35:53 -07:00
|
|
|
NS_DEFINE_CID(kNetworkManagerCID, NS_NETWORKMANAGER_CID);
|
2012-01-09 15:18:23 -08:00
|
|
|
|
2011-12-04 23:58:27 -08:00
|
|
|
// Doesn't carry a reference, we're owned by services.
|
2012-07-30 07:20:58 -07:00
|
|
|
SystemWorkerManager *gInstance = nullptr;
|
2011-12-04 23:58:27 -08:00
|
|
|
|
2012-01-09 15:18:23 -08:00
|
|
|
class ConnectWorkerToRIL : public WorkerTask
|
|
|
|
{
|
2011-12-04 23:58:27 -08:00
|
|
|
public:
|
|
|
|
virtual bool RunTask(JSContext *aCx);
|
|
|
|
};
|
|
|
|
|
|
|
|
JSBool
|
2012-02-28 15:11:11 -08:00
|
|
|
PostToRIL(JSContext *cx, unsigned argc, jsval *vp)
|
2011-12-04 23:58:27 -08:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Expecting to be on the worker thread");
|
|
|
|
|
|
|
|
if (argc != 1) {
|
|
|
|
JS_ReportError(cx, "Expecting a single argument with the RIL message");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsval v = JS_ARGV(cx, vp)[0];
|
|
|
|
|
2011-12-07 02:58:56 -08:00
|
|
|
nsAutoPtr<RilRawData> rm(new RilRawData());
|
2011-12-04 23:58:27 -08:00
|
|
|
JSAutoByteString abs;
|
|
|
|
void *data;
|
|
|
|
size_t size;
|
|
|
|
if (JSVAL_IS_STRING(v)) {
|
|
|
|
JSString *str = JSVAL_TO_STRING(v);
|
|
|
|
if (!abs.encode(cx, str)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = JS_GetStringLength(str);
|
|
|
|
data = abs.ptr();
|
|
|
|
} else if (!JSVAL_IS_PRIMITIVE(v)) {
|
|
|
|
JSObject *obj = JSVAL_TO_OBJECT(v);
|
2012-11-14 09:56:26 -08:00
|
|
|
if (!JS_IsTypedArrayObject(obj)) {
|
2011-12-04 23:58:27 -08:00
|
|
|
JS_ReportError(cx, "Object passed in wasn't a typed array");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-14 09:56:26 -08:00
|
|
|
uint32_t type = JS_GetTypedArrayType(obj);
|
2012-01-14 09:43:00 -08:00
|
|
|
if (type != js::ArrayBufferView::TYPE_INT8 &&
|
|
|
|
type != js::ArrayBufferView::TYPE_UINT8 &&
|
|
|
|
type != js::ArrayBufferView::TYPE_UINT8_CLAMPED) {
|
2011-12-04 23:58:27 -08:00
|
|
|
JS_ReportError(cx, "Typed array data is not octets");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-14 09:56:26 -08:00
|
|
|
size = JS_GetTypedArrayByteLength(obj);
|
|
|
|
data = JS_GetArrayBufferViewData(obj);
|
2011-12-04 23:58:27 -08:00
|
|
|
} else {
|
|
|
|
JS_ReportError(cx,
|
|
|
|
"Incorrect argument. Expecting a string or a typed array");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-12-07 02:58:56 -08:00
|
|
|
if (size > RilRawData::MAX_DATA_SIZE) {
|
2011-12-04 23:58:27 -08:00
|
|
|
JS_ReportError(cx, "Passed-in data is too large");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rm->mSize = size;
|
|
|
|
memcpy(rm->mData, data, size);
|
|
|
|
|
2011-12-07 02:58:56 -08:00
|
|
|
RilRawData *tosend = rm.forget();
|
|
|
|
JS_ALWAYS_TRUE(SendRilRawData(&tosend));
|
2011-12-04 23:58:27 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ConnectWorkerToRIL::RunTask(JSContext *aCx)
|
|
|
|
{
|
|
|
|
// Set up the postRILMessage on the function for worker -> RIL thread
|
|
|
|
// communication.
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Expecting to be on the worker thread");
|
|
|
|
NS_ASSERTION(!JS_IsRunning(aCx), "Are we being called somehow?");
|
|
|
|
JSObject *workerGlobal = JS_GetGlobalObject(aCx);
|
|
|
|
|
2012-01-09 14:28:47 -08:00
|
|
|
return !!JS_DefineFunction(aCx, workerGlobal, "postRILMessage", PostToRIL, 1,
|
|
|
|
0);
|
2011-12-04 23:58:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
class RILReceiver : public RilConsumer
|
|
|
|
{
|
2012-01-09 15:18:23 -08:00
|
|
|
class DispatchRILEvent : public WorkerTask
|
|
|
|
{
|
2011-12-04 23:58:27 -08:00
|
|
|
public:
|
2011-12-07 02:58:56 -08:00
|
|
|
DispatchRILEvent(RilRawData *aMessage)
|
2011-12-04 23:58:27 -08:00
|
|
|
: mMessage(aMessage)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual bool RunTask(JSContext *aCx);
|
|
|
|
|
|
|
|
private:
|
2011-12-07 02:58:56 -08:00
|
|
|
nsAutoPtr<RilRawData> mMessage;
|
2011-12-04 23:58:27 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
RILReceiver(WorkerCrossThreadDispatcher *aDispatcher)
|
|
|
|
: mDispatcher(aDispatcher)
|
|
|
|
{ }
|
|
|
|
|
2011-12-07 02:58:56 -08:00
|
|
|
virtual void MessageReceived(RilRawData *aMessage) {
|
2011-12-04 23:58:27 -08:00
|
|
|
nsRefPtr<DispatchRILEvent> dre(new DispatchRILEvent(aMessage));
|
|
|
|
mDispatcher->PostTask(dre);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<WorkerCrossThreadDispatcher> mDispatcher;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
RILReceiver::DispatchRILEvent::RunTask(JSContext *aCx)
|
|
|
|
{
|
|
|
|
JSObject *obj = JS_GetGlobalObject(aCx);
|
|
|
|
|
2012-01-14 09:43:00 -08:00
|
|
|
JSObject *array = JS_NewUint8Array(aCx, mMessage->mSize);
|
2011-12-04 23:58:27 -08:00
|
|
|
if (!array) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-14 09:56:26 -08:00
|
|
|
memcpy(JS_GetArrayBufferViewData(array), mMessage->mData, mMessage->mSize);
|
2011-12-04 23:58:27 -08:00
|
|
|
jsval argv[] = { OBJECT_TO_JSVAL(array) };
|
|
|
|
return JS_CallFunctionName(aCx, obj, "onRILMessage", NS_ARRAY_LENGTH(argv),
|
|
|
|
argv, argv);
|
|
|
|
}
|
|
|
|
|
2012-08-30 07:46:04 -07:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
|
2012-08-29 13:35:53 -07:00
|
|
|
JSBool
|
|
|
|
DoNetdCommand(JSContext *cx, unsigned argc, jsval *vp)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Expecting to be on the worker thread");
|
|
|
|
|
|
|
|
if (argc != 1) {
|
|
|
|
JS_ReportError(cx, "Expecting a single argument with the Netd message");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsval v = JS_ARGV(cx, vp)[0];
|
|
|
|
|
|
|
|
JSAutoByteString abs;
|
|
|
|
void *data;
|
|
|
|
size_t size;
|
|
|
|
if (JSVAL_IS_STRING(v)) {
|
|
|
|
JSString *str = JSVAL_TO_STRING(v);
|
|
|
|
if (!abs.encode(cx, str)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = JS_GetStringLength(str);
|
|
|
|
if (!size) {
|
|
|
|
JS_ReportError(cx, "Command length is zero");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = abs.ptr();
|
|
|
|
if (!data) {
|
|
|
|
JS_ReportError(cx, "Command string is empty");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (!JSVAL_IS_PRIMITIVE(v)) {
|
|
|
|
JSObject *obj = JSVAL_TO_OBJECT(v);
|
2012-11-14 09:56:26 -08:00
|
|
|
if (!JS_IsTypedArrayObject(obj)) {
|
2012-08-29 13:35:53 -07:00
|
|
|
JS_ReportError(cx, "Object passed in wasn't a typed array");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-14 09:56:26 -08:00
|
|
|
uint32_t type = JS_GetTypedArrayType(obj);
|
2012-08-29 13:35:53 -07:00
|
|
|
if (type != js::ArrayBufferView::TYPE_INT8 &&
|
|
|
|
type != js::ArrayBufferView::TYPE_UINT8 &&
|
|
|
|
type != js::ArrayBufferView::TYPE_UINT8_CLAMPED) {
|
|
|
|
JS_ReportError(cx, "Typed array data is not octets");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-14 09:56:26 -08:00
|
|
|
size = JS_GetTypedArrayByteLength(obj);
|
2012-08-29 13:35:53 -07:00
|
|
|
if (!size) {
|
|
|
|
JS_ReportError(cx, "Typed array byte length is zero");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-14 09:56:26 -08:00
|
|
|
data = JS_GetArrayBufferViewData(obj);
|
2012-08-29 13:35:53 -07:00
|
|
|
if (!data) {
|
|
|
|
JS_ReportError(cx, "Array buffer view data is NULL");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
JS_ReportError(cx,
|
|
|
|
"Incorrect argument. Expecting a string or a typed array");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reserve one space for '\0'.
|
|
|
|
if (size > MAX_COMMAND_SIZE - 1 || size <= 0) {
|
|
|
|
JS_ReportError(cx, "Passed-in data size is invalid");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
NetdCommand* command = new NetdCommand();
|
|
|
|
|
|
|
|
memcpy(command->mData, data, size);
|
|
|
|
// Include the null terminate to the command to make netd happy.
|
|
|
|
command->mData[size] = 0;
|
|
|
|
command->mSize = size + 1;
|
|
|
|
SendNetdCommand(command);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ConnectWorkerToNetd : public WorkerTask
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool RunTask(JSContext *aCx);
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
ConnectWorkerToNetd::RunTask(JSContext *aCx)
|
|
|
|
{
|
|
|
|
// Set up the DoNetdCommand on the function for worker <--> Netd process
|
|
|
|
// communication.
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Expecting to be on the worker thread");
|
|
|
|
NS_ASSERTION(!JS_IsRunning(aCx), "Are we being called somehow?");
|
|
|
|
JSObject *workerGlobal = JS_GetGlobalObject(aCx);
|
|
|
|
return !!JS_DefineFunction(aCx, workerGlobal, "postNetdCommand",
|
|
|
|
DoNetdCommand, 1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
class NetdReceiver : public NetdConsumer
|
|
|
|
{
|
|
|
|
class DispatchNetdEvent : public WorkerTask
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DispatchNetdEvent(NetdCommand *aMessage)
|
|
|
|
: mMessage(aMessage)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual bool RunTask(JSContext *aCx);
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsAutoPtr<NetdCommand> mMessage;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
NetdReceiver(WorkerCrossThreadDispatcher *aDispatcher)
|
|
|
|
: mDispatcher(aDispatcher)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual void MessageReceived(NetdCommand *aMessage) {
|
|
|
|
nsRefPtr<DispatchNetdEvent> dre(new DispatchNetdEvent(aMessage));
|
|
|
|
if (!mDispatcher->PostTask(dre)) {
|
|
|
|
NS_WARNING("Failed to PostTask to net worker");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<WorkerCrossThreadDispatcher> mDispatcher;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
NetdReceiver::DispatchNetdEvent::RunTask(JSContext *aCx)
|
|
|
|
{
|
|
|
|
JSObject *obj = JS_GetGlobalObject(aCx);
|
|
|
|
|
|
|
|
JSObject *array = JS_NewUint8Array(aCx, mMessage->mSize);
|
|
|
|
if (!array) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-14 09:56:26 -08:00
|
|
|
memcpy(JS_GetUint8ArrayData(array), mMessage->mData, mMessage->mSize);
|
2012-08-29 13:35:53 -07:00
|
|
|
jsval argv[] = { OBJECT_TO_JSVAL(array) };
|
|
|
|
return JS_CallFunctionName(aCx, obj, "onNetdMessage", NS_ARRAY_LENGTH(argv),
|
|
|
|
argv, argv);
|
|
|
|
}
|
2012-08-30 07:46:04 -07:00
|
|
|
|
|
|
|
#endif // MOZ_WIDGET_GONK
|
|
|
|
|
2011-12-04 23:58:27 -08:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2012-01-09 15:18:23 -08:00
|
|
|
SystemWorkerManager::SystemWorkerManager()
|
2011-12-04 23:58:27 -08:00
|
|
|
: mShutdown(false)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!gInstance, "There should only be one instance!");
|
|
|
|
}
|
|
|
|
|
2012-01-09 15:18:23 -08:00
|
|
|
SystemWorkerManager::~SystemWorkerManager()
|
2011-12-04 23:58:27 -08:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
NS_ASSERTION(!gInstance || gInstance == this,
|
|
|
|
"There should only be one instance!");
|
2012-07-30 07:20:58 -07:00
|
|
|
gInstance = nullptr;
|
2011-12-04 23:58:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-01-09 15:18:23 -08:00
|
|
|
SystemWorkerManager::Init()
|
2011-12-04 23:58:27 -08:00
|
|
|
{
|
2012-05-05 20:22:53 -07:00
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2011-12-04 23:58:27 -08:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "We can only initialize on the main thread");
|
2012-01-09 15:18:23 -08:00
|
|
|
NS_ASSERTION(!mShutdown, "Already shutdown!");
|
2011-12-04 23:58:27 -08:00
|
|
|
|
2012-05-05 02:00:05 -07:00
|
|
|
JSContext* cx = nsContentUtils::ThreadJSContextStack()->GetSafeJSContext();
|
|
|
|
NS_ENSURE_TRUE(cx, NS_ERROR_FAILURE);
|
2012-01-09 15:18:23 -08:00
|
|
|
|
2011-12-04 23:58:27 -08:00
|
|
|
nsCxPusher pusher;
|
2012-05-05 02:00:05 -07:00
|
|
|
if (!pusher.Push(cx, false)) {
|
2011-12-04 23:58:27 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-05-05 02:00:05 -07:00
|
|
|
nsresult rv = InitRIL(cx);
|
2012-05-23 18:21:42 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to initialize RIL/Telephony!");
|
|
|
|
return rv;
|
|
|
|
}
|
2011-12-04 23:58:27 -08:00
|
|
|
|
2011-12-06 06:14:55 -08:00
|
|
|
rv = InitWifi(cx);
|
2012-05-23 18:21:42 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to initialize WiFi Networking!");
|
|
|
|
return rv;
|
|
|
|
}
|
2011-12-04 23:58:27 -08:00
|
|
|
|
2012-05-24 21:04:32 -07:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
InitAutoMounter();
|
2012-10-30 08:34:49 -07:00
|
|
|
InitializeTimeZoneSettingObserver();
|
2012-08-29 13:35:53 -07:00
|
|
|
rv = InitNetd(cx);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-05-24 21:04:32 -07:00
|
|
|
#endif
|
|
|
|
|
2012-01-09 15:18:23 -08:00
|
|
|
nsCOMPtr<nsIObserverService> obs =
|
|
|
|
do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
|
|
|
|
if (!obs) {
|
|
|
|
NS_WARNING("Failed to get observer service!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = obs->AddObserver(this, WORKERS_SHUTDOWN_TOPIC, false);
|
2012-05-23 18:21:42 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to initialize worker shutdown event!");
|
|
|
|
return rv;
|
|
|
|
}
|
2012-01-09 15:18:23 -08:00
|
|
|
|
2011-12-04 23:58:27 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-01-09 15:18:23 -08:00
|
|
|
SystemWorkerManager::Shutdown()
|
2011-12-04 23:58:27 -08:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-01-09 15:18:23 -08:00
|
|
|
mShutdown = true;
|
|
|
|
|
2012-05-24 21:04:32 -07:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
ShutdownAutoMounter();
|
|
|
|
#endif
|
|
|
|
|
2011-12-04 23:58:27 -08:00
|
|
|
StopRil();
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2012-08-31 10:59:27 -07:00
|
|
|
mRIL = nullptr;
|
2012-08-29 13:35:53 -07:00
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
StopNetd();
|
|
|
|
mNetdWorker = nullptr;
|
|
|
|
#endif
|
|
|
|
|
2012-03-20 06:57:07 -07:00
|
|
|
nsCOMPtr<nsIWifi> wifi(do_QueryInterface(mWifiWorker));
|
|
|
|
if (wifi) {
|
|
|
|
wifi->Shutdown();
|
2012-07-30 07:20:58 -07:00
|
|
|
wifi = nullptr;
|
2012-03-20 06:57:07 -07:00
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
mWifiWorker = nullptr;
|
2012-01-09 15:18:23 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs =
|
|
|
|
do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
|
|
|
|
if (obs) {
|
|
|
|
obs->RemoveObserver(this, WORKERS_SHUTDOWN_TOPIC);
|
|
|
|
}
|
2011-12-04 23:58:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2012-01-09 15:18:23 -08:00
|
|
|
already_AddRefed<SystemWorkerManager>
|
|
|
|
SystemWorkerManager::FactoryCreate()
|
2011-12-04 23:58:27 -08:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-01-09 15:18:23 -08:00
|
|
|
nsRefPtr<SystemWorkerManager> instance(gInstance);
|
2011-12-04 23:58:27 -08:00
|
|
|
|
|
|
|
if (!instance) {
|
2012-01-09 15:18:23 -08:00
|
|
|
instance = new SystemWorkerManager();
|
2011-12-04 23:58:27 -08:00
|
|
|
if (NS_FAILED(instance->Init())) {
|
2012-01-09 15:18:23 -08:00
|
|
|
instance->Shutdown();
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2011-12-04 23:58:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
gInstance = instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
return instance.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2012-01-09 15:18:23 -08:00
|
|
|
nsIInterfaceRequestor*
|
|
|
|
SystemWorkerManager::GetInterfaceRequestor()
|
|
|
|
{
|
|
|
|
return gInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SystemWorkerManager::GetInterface(const nsIID &aIID, void **aResult)
|
2011-12-04 23:58:27 -08:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
2012-01-19 12:53:32 -08:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsIRadioInterfaceLayer))) {
|
2012-08-31 10:59:27 -07:00
|
|
|
NS_IF_ADDREF(*reinterpret_cast<nsIRadioInterfaceLayer**>(aResult) = mRIL);
|
|
|
|
return NS_OK;
|
2011-12-04 23:58:27 -08:00
|
|
|
}
|
|
|
|
|
2012-01-09 15:18:23 -08:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsIWifi))) {
|
|
|
|
return CallQueryInterface(mWifiWorker,
|
|
|
|
reinterpret_cast<nsIWifi**>(aResult));
|
|
|
|
}
|
|
|
|
|
2012-08-30 07:46:04 -07:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2012-08-29 13:35:53 -07:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsINetworkManager))) {
|
|
|
|
return CallQueryInterface(mNetdWorker,
|
|
|
|
reinterpret_cast<nsINetworkManager**>(aResult));
|
|
|
|
}
|
2012-08-30 07:46:04 -07:00
|
|
|
#endif
|
2012-08-29 13:35:53 -07:00
|
|
|
|
2012-01-09 15:18:23 -08:00
|
|
|
NS_WARNING("Got nothing for the requested IID!");
|
|
|
|
return NS_ERROR_NO_INTERFACE;
|
2011-12-04 23:58:27 -08:00
|
|
|
}
|
|
|
|
|
2011-12-06 06:14:55 -08:00
|
|
|
nsresult
|
2012-01-19 12:53:32 -08:00
|
|
|
SystemWorkerManager::InitRIL(JSContext *cx)
|
2011-12-06 06:14:55 -08:00
|
|
|
{
|
2012-01-09 15:18:23 -08:00
|
|
|
// We're keeping as much of this implementation as possible in JS, so the real
|
2012-01-19 12:53:32 -08:00
|
|
|
// worker lives in RadioInterfaceLayer.js. All we do here is hold it alive and
|
2012-01-09 15:18:23 -08:00
|
|
|
// hook it up to the RIL thread.
|
2012-08-31 10:59:27 -07:00
|
|
|
nsCOMPtr<nsIRadioInterfaceLayer> ril = do_CreateInstance("@mozilla.org/ril;1");
|
|
|
|
NS_ENSURE_TRUE(ril, NS_ERROR_FAILURE);
|
2011-12-06 06:14:55 -08:00
|
|
|
|
2012-08-31 10:59:27 -07:00
|
|
|
nsCOMPtr<nsIWorkerHolder> worker = do_QueryInterface(ril);
|
|
|
|
if (worker) {
|
|
|
|
jsval workerval;
|
|
|
|
nsresult rv = worker->GetWorker(&workerval);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-12-06 06:14:55 -08:00
|
|
|
|
2012-08-31 10:59:27 -07:00
|
|
|
NS_ENSURE_TRUE(!JSVAL_IS_PRIMITIVE(workerval), NS_ERROR_UNEXPECTED);
|
2011-12-06 06:14:55 -08:00
|
|
|
|
2012-08-31 10:59:27 -07:00
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
JSAutoCompartment ac(cx, JSVAL_TO_OBJECT(workerval));
|
2011-12-06 06:14:55 -08:00
|
|
|
|
2012-08-31 10:59:27 -07:00
|
|
|
WorkerCrossThreadDispatcher *wctd =
|
|
|
|
GetWorkerCrossThreadDispatcher(cx, workerval);
|
|
|
|
if (!wctd) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2011-12-06 06:14:55 -08:00
|
|
|
|
2012-08-31 10:59:27 -07:00
|
|
|
nsRefPtr<ConnectWorkerToRIL> connection = new ConnectWorkerToRIL();
|
|
|
|
if (!wctd->PostTask(connection)) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2011-12-06 06:14:55 -08:00
|
|
|
|
2012-08-31 10:59:27 -07:00
|
|
|
// Now that we're set up, connect ourselves to the RIL thread.
|
|
|
|
mozilla::RefPtr<RILReceiver> receiver = new RILReceiver(wctd);
|
|
|
|
StartRil(receiver);
|
|
|
|
}
|
2011-12-06 06:14:55 -08:00
|
|
|
|
2012-08-31 10:59:27 -07:00
|
|
|
mRIL = ril;
|
2011-12-06 06:14:55 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-30 07:46:04 -07:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2012-08-29 13:35:53 -07:00
|
|
|
nsresult
|
|
|
|
SystemWorkerManager::InitNetd(JSContext *cx)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIWorkerHolder> worker = do_GetService(kNetworkManagerCID);
|
|
|
|
NS_ENSURE_TRUE(worker, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
jsval workerval;
|
|
|
|
nsresult rv = worker->GetWorker(&workerval);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(!JSVAL_IS_PRIMITIVE(workerval), NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
JSAutoCompartment ac(cx, JSVAL_TO_OBJECT(workerval));
|
|
|
|
|
|
|
|
WorkerCrossThreadDispatcher *wctd =
|
|
|
|
GetWorkerCrossThreadDispatcher(cx, workerval);
|
|
|
|
if (!wctd) {
|
|
|
|
NS_WARNING("Failed to GetWorkerCrossThreadDispatcher");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<ConnectWorkerToNetd> connection = new ConnectWorkerToNetd();
|
|
|
|
if (!wctd->PostTask(connection)) {
|
|
|
|
NS_WARNING("Failed to connect worker to netd");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we're set up, connect ourselves to the Netd process.
|
|
|
|
mozilla::RefPtr<NetdReceiver> receiver = new NetdReceiver(wctd);
|
|
|
|
StartNetd(receiver);
|
|
|
|
mNetdWorker = worker;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-08-30 07:46:04 -07:00
|
|
|
#endif
|
2012-08-29 13:35:53 -07:00
|
|
|
|
2011-12-06 06:14:55 -08:00
|
|
|
nsresult
|
2012-01-09 15:18:23 -08:00
|
|
|
SystemWorkerManager::InitWifi(JSContext *cx)
|
2011-12-06 06:14:55 -08:00
|
|
|
{
|
2012-01-09 15:18:23 -08:00
|
|
|
nsCOMPtr<nsIWorkerHolder> worker = do_CreateInstance(kWifiWorkerCID);
|
2011-12-06 06:14:55 -08:00
|
|
|
NS_ENSURE_TRUE(worker, NS_ERROR_FAILURE);
|
|
|
|
|
2012-01-09 15:18:23 -08:00
|
|
|
mWifiWorker = worker;
|
2011-12-06 06:14:55 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-09 15:18:23 -08:00
|
|
|
NS_IMPL_ISUPPORTS2(SystemWorkerManager, nsIObserver, nsIInterfaceRequestor)
|
2011-12-04 23:58:27 -08:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-01-09 15:18:23 -08:00
|
|
|
SystemWorkerManager::Observe(nsISupports *aSubject, const char *aTopic,
|
|
|
|
const PRUnichar *aData)
|
2011-12-04 23:58:27 -08:00
|
|
|
{
|
2012-01-09 15:18:23 -08:00
|
|
|
if (!strcmp(aTopic, WORKERS_SHUTDOWN_TOPIC)) {
|
2011-12-04 23:58:27 -08:00
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|