Skip to content

Commit

Permalink
Bug 826931 - Part 3/3: use mozilla::ipc::UnixSocket. r=qDot
Browse files Browse the repository at this point in the history
  • Loading branch information
Vicamo Yang committed Feb 1, 2013
1 parent bab7d11 commit 95c0116
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 429 deletions.
96 changes: 38 additions & 58 deletions dom/system/gonk/SystemWorkerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ class ConnectWorkerToRIL : public WorkerTask
virtual bool RunTask(JSContext *aCx);
};

class SendRilSocketDataTask : public nsRunnable
{
public:
SendRilSocketDataTask(UnixSocketRawData *aRawData)
: mRawData(aRawData)
{ }

NS_IMETHOD Run()
{
MOZ_ASSERT(NS_IsMainThread());
SystemWorkerManager::SendRilRawData(mRawData);
return NS_OK;
}

private:
UnixSocketRawData *mRawData;
};

JSBool
PostToRIL(JSContext *cx, unsigned argc, jsval *vp)
{
Expand All @@ -76,7 +94,6 @@ PostToRIL(JSContext *cx, unsigned argc, jsval *vp)

jsval v = JS_ARGV(cx, vp)[0];

nsAutoPtr<RilRawData> rm(new RilRawData());
JSAutoByteString abs;
void *data;
size_t size;
Expand Down Expand Up @@ -111,16 +128,11 @@ PostToRIL(JSContext *cx, unsigned argc, jsval *vp)
return false;
}

if (size > RilRawData::MAX_DATA_SIZE) {
JS_ReportError(cx, "Passed-in data is too large");
return false;
}

rm->mSize = size;
memcpy(rm->mData, data, size);
UnixSocketRawData* raw = new UnixSocketRawData(size);
memcpy(raw->mData, data, raw->mSize);

RilRawData *tosend = rm.forget();
JS_ALWAYS_TRUE(SendRilRawData(&tosend));
nsRefPtr<SendRilSocketDataTask> task = new SendRilSocketDataTask(raw);
NS_DispatchToMainThread(task);
return true;
}

Expand All @@ -137,51 +149,6 @@ ConnectWorkerToRIL::RunTask(JSContext *aCx)
0);
}

class RILReceiver : public RilConsumer
{
class DispatchRILEvent : public WorkerTask
{
public:
DispatchRILEvent(RilRawData *aMessage)
: mMessage(aMessage)
{ }

virtual bool RunTask(JSContext *aCx);

private:
nsAutoPtr<RilRawData> mMessage;
};

public:
RILReceiver(WorkerCrossThreadDispatcher *aDispatcher)
: mDispatcher(aDispatcher)
{ }

virtual void MessageReceived(RilRawData *aMessage) {
nsRefPtr<DispatchRILEvent> dre(new DispatchRILEvent(aMessage));
mDispatcher->PostTask(dre);
}

private:
nsRefPtr<WorkerCrossThreadDispatcher> mDispatcher;
};

bool
RILReceiver::DispatchRILEvent::RunTask(JSContext *aCx)
{
JSObject *obj = JS_GetGlobalObject(aCx);

JSObject *array = JS_NewUint8Array(aCx, mMessage->mSize);
if (!array) {
return false;
}

memcpy(JS_GetArrayBufferViewData(array), mMessage->mData, mMessage->mSize);
jsval argv[] = { OBJECT_TO_JSVAL(array) };
return JS_CallFunctionName(aCx, obj, "onRILMessage", NS_ARRAY_LENGTH(argv),
argv, argv);
}

#ifdef MOZ_WIDGET_GONK

JSBool
Expand Down Expand Up @@ -406,7 +373,10 @@ SystemWorkerManager::Shutdown()
ShutdownAutoMounter();
#endif

StopRil();
if (mRilConsumer) {
mRilConsumer->Shutdown();
mRilConsumer = nullptr;
}

#ifdef MOZ_WIDGET_GONK
StopNetd();
Expand Down Expand Up @@ -455,6 +425,17 @@ SystemWorkerManager::GetInterfaceRequestor()
return gInstance;
}

bool
SystemWorkerManager::SendRilRawData(UnixSocketRawData* aRaw)
{
if (!gInstance->mRilConsumer) {
// Probably shuting down.
delete aRaw;
return true;
}
return gInstance->mRilConsumer->SendSocketData(aRaw);
}

NS_IMETHODIMP
SystemWorkerManager::GetInterface(const nsIID &aIID, void **aResult)
{
Expand Down Expand Up @@ -497,8 +478,7 @@ SystemWorkerManager::RegisterRilWorker(const JS::Value& aWorker,
}

// Now that we're set up, connect ourselves to the RIL thread.
mozilla::RefPtr<RILReceiver> receiver = new RILReceiver(wctd);
StartRil(receiver);
mRilConsumer = new RilConsumer(wctd);
return NS_OK;
}

Expand Down
10 changes: 10 additions & 0 deletions dom/system/gonk/SystemWorkerManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
class nsIWorkerHolder;

namespace mozilla {

namespace ipc {
class RilConsumer;
class UnixSocketRawData;
}

namespace dom {
namespace gonk {

Expand All @@ -53,6 +59,8 @@ class SystemWorkerManager : public nsIObserver,
static nsIInterfaceRequestor*
GetInterfaceRequestor();

static bool SendRilRawData(ipc::UnixSocketRawData* aRaw);

private:
SystemWorkerManager();
~SystemWorkerManager();
Expand All @@ -67,6 +75,8 @@ class SystemWorkerManager : public nsIObserver,
#endif
nsCOMPtr<nsIWorkerHolder> mWifiWorker;

nsRefPtr<ipc::RilConsumer> mRilConsumer;

bool mShutdown;
};

Expand Down
Loading

0 comments on commit 95c0116

Please sign in to comment.