Merge b2g-inbound to m-c

This commit is contained in:
Wes Kocher 2013-11-05 17:49:16 -08:00
commit beecd09e2b
7 changed files with 214 additions and 49 deletions

View File

@ -1,4 +1,4 @@
{ {
"revision": "53c6a2b0bf4c238d39ee0096b79d911aec2de0fc", "revision": "fe012cdbca542f414a72b0cba9cfb50a4aaf62ae",
"repo_path": "/integration/gaia-central" "repo_path": "/integration/gaia-central"
} }

View File

@ -31,6 +31,9 @@
#ifdef MOZ_B2G_RIL #ifdef MOZ_B2G_RIL
#include "mozilla/ipc/Ril.h" #include "mozilla/ipc/Ril.h"
#endif #endif
#ifdef MOZ_NFC
#include "mozilla/ipc/Nfc.h"
#endif
#include "mozilla/ipc/KeyStore.h" #include "mozilla/ipc/KeyStore.h"
#include "nsIObserverService.h" #include "nsIObserverService.h"
#include "nsCxPusher.h" #include "nsCxPusher.h"
@ -275,6 +278,10 @@ SystemWorkerManager::Shutdown()
RilConsumer::Shutdown(); RilConsumer::Shutdown();
#endif #endif
#ifdef MOZ_NFC
NfcConsumer::Shutdown();
#endif
StopNetd(); StopNetd();
mNetdWorker = nullptr; mNetdWorker = nullptr;
@ -361,6 +368,28 @@ SystemWorkerManager::RegisterRilWorker(unsigned int aClientId,
#endif // MOZ_B2G_RIL #endif // MOZ_B2G_RIL
} }
nsresult
SystemWorkerManager::RegisterNfcWorker(const JS::Value& aWorker,
JSContext* aCx)
{
#ifndef MOZ_NFC
return NS_ERROR_NOT_IMPLEMENTED;
#else
NS_ENSURE_TRUE(!JSVAL_IS_PRIMITIVE(aWorker), NS_ERROR_UNEXPECTED);
JSAutoCompartment ac(aCx, JSVAL_TO_OBJECT(aWorker));
WorkerCrossThreadDispatcher* wctd =
GetWorkerCrossThreadDispatcher(aCx, aWorker);
if (!wctd) {
NS_WARNING("Failed to GetWorkerCrossThreadDispatcher for nfc");
return NS_ERROR_FAILURE;
}
return NfcConsumer::Register(wctd);
#endif // MOZ_NFC
}
nsresult nsresult
SystemWorkerManager::InitNetd(JSContext *cx) SystemWorkerManager::InitNetd(JSContext *cx)
{ {

View File

@ -7,10 +7,13 @@
/** /**
* Information about networks that is exposed to network manager API consumers. * Information about networks that is exposed to network manager API consumers.
*/ */
[scriptable, builtinclass, uuid(02166330-2ff6-4ea5-895a-693c0c2b4c1f)] [scriptable, builtinclass, uuid(a9ea96a0-407d-11e3-aa6e-0800200c9a66)]
interface nsISystemWorkerManager : nsISupports interface nsISystemWorkerManager : nsISupports
{ {
[implicit_jscontext] [implicit_jscontext]
void registerRilWorker(in unsigned long aClientId, void registerRilWorker(in unsigned long aClientId,
in jsval aWorker); in jsval aWorker);
[implicit_jscontext]
void registerNfcWorker(in jsval aWorker);
}; };

View File

@ -17,7 +17,10 @@ if CONFIG['MOZ_B2G_RIL']:
if CONFIG['MOZ_B2G_BT_BLUEZ']: if CONFIG['MOZ_B2G_BT_BLUEZ']:
DIRS += ['dbus'] DIRS += ['dbus']
if CONFIG['MOZ_B2G_RIL'] or CONFIG['MOZ_B2G_BT'] or CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk': if CONFIG['MOZ_NFC']:
DIRS += ['nfc']
if CONFIG['MOZ_B2G_RIL'] or CONFIG['MOZ_B2G_BT'] or CONFIG['MOZ_NFC'] or CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk':
DIRS += ['unixsocket'] DIRS += ['unixsocket']
if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk': if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk':

View File

@ -4,10 +4,11 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <fcntl.h> /* Copyright © 2013, Deutsche Telekom, Inc. */
#include <limits.h>
#include <errno.h>
#include "mozilla/ipc/Nfc.h"
#include <fcntl.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
@ -21,7 +22,6 @@
#include "jsfriendapi.h" #include "jsfriendapi.h"
#include "nsThreadUtils.h" // For NS_IsMainThread. #include "nsThreadUtils.h" // For NS_IsMainThread.
#include "Nfc.h"
USING_WORKERS_NAMESPACE USING_WORKERS_NAMESPACE
using namespace mozilla::ipc; using namespace mozilla::ipc;
@ -32,34 +32,139 @@ const char* NFC_SOCKET_NAME = "/dev/socket/nfcd";
// Network port to connect to for adb forwarded sockets when doing // Network port to connect to for adb forwarded sockets when doing
// desktop development. // desktop development.
const uint32_t NFCD_TEST_PORT = 6400; const uint32_t NFC_TEST_PORT = 6400;
class DispatchNfcEvent : public WorkerTask nsRefPtr<mozilla::ipc::NfcConsumer> sNfcConsumer;
class ConnectWorkerToNFC : public WorkerTask
{ {
public: public:
DispatchNfcEvent(UnixSocketRawData* aMessage) ConnectWorkerToNFC()
: mMessage(aMessage)
{ } { }
virtual bool RunTask(JSContext *aCx); virtual bool RunTask(JSContext* aCx);
};
class SendNfcSocketDataTask : public nsRunnable
{
public:
SendNfcSocketDataTask(UnixSocketRawData* aRawData)
: mRawData(aRawData)
{ }
NS_IMETHOD Run()
{
MOZ_ASSERT(NS_IsMainThread());
if (!sNfcConsumer ||
sNfcConsumer->GetConnectionStatus() != SOCKET_CONNECTED) {
// Probably shuting down.
delete mRawData;
return NS_OK;
}
sNfcConsumer->SendSocketData(mRawData);
return NS_OK;
}
private:
UnixSocketRawData* mRawData;
};
bool
PostToNFC(JSContext* aCx,
unsigned aArgc,
JS::Value* aArgv)
{
NS_ASSERTION(!NS_IsMainThread(), "Expecting to be on the worker thread");
if (aArgc != 1) {
JS_ReportError(aCx, "Expecting one argument with the NFC message");
return false;
}
JS::Value v = JS_ARGV(aCx, aArgv)[0];
JSAutoByteString abs;
void* data;
size_t size;
if (JSVAL_IS_STRING(v)) {
JSString* str = JSVAL_TO_STRING(v);
if (!abs.encodeUtf8(aCx, str)) {
return false;
}
data = abs.ptr();
size = abs.length();
} else if (!JSVAL_IS_PRIMITIVE(v)) {
JSObject* obj = JSVAL_TO_OBJECT(v);
if (!JS_IsTypedArrayObject(obj)) {
JS_ReportError(aCx, "Object passed in wasn't a typed array");
return false;
}
uint32_t type = JS_GetArrayBufferViewType(obj);
if (type != js::ArrayBufferView::TYPE_INT8 &&
type != js::ArrayBufferView::TYPE_UINT8 &&
type != js::ArrayBufferView::TYPE_UINT8_CLAMPED) {
JS_ReportError(aCx, "Typed array data is not octets");
return false;
}
size = JS_GetTypedArrayByteLength(obj);
data = JS_GetArrayBufferViewData(obj);
} else {
JS_ReportError(aCx,
"Incorrect argument. Expecting a string or a typed array");
return false;
}
UnixSocketRawData* raw = new UnixSocketRawData(data, size);
nsRefPtr<SendNfcSocketDataTask> task =
new SendNfcSocketDataTask(raw);
NS_DispatchToMainThread(task);
return true;
}
bool
ConnectWorkerToNFC::RunTask(JSContext* aCx)
{
// Set up the postNFCMessage on the function for worker -> NFC 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::CurrentGlobalOrNull(aCx);
return !!JS_DefineFunction(aCx, workerGlobal,
"postNfcMessage", PostToNFC, 1, 0);
}
class DispatchNFCEvent : public WorkerTask
{
public:
DispatchNFCEvent(UnixSocketRawData* aMessage)
: mMessage(aMessage)
{ }
virtual bool RunTask(JSContext* aCx);
private: private:
nsAutoPtr<UnixSocketRawData> mMessage; nsAutoPtr<UnixSocketRawData> mMessage;
}; };
bool bool
DispatchNfcEvent::RunTask(JSContext *aCx) DispatchNFCEvent::RunTask(JSContext* aCx)
{ {
MOZ_ASSERT(NS_IsMainThread(), "DispatchNfcEvent on main thread"); JSObject* obj = JS::CurrentGlobalOrNull(aCx);
MOZ_ASSERT(aCx);
JSObject *obj = JS::CurrentGlobalOrNull(aCx); JSObject* array = JS_NewUint8Array(aCx, mMessage->mSize);
JSObject *array = JS_NewUint8Array(aCx, mMessage->mSize);
if (!array) { if (!array) {
return false; return false;
} }
memcpy(JS_GetArrayBufferViewData(array), mMessage->mData, mMessage->mSize); memcpy(JS_GetArrayBufferViewData(array), mMessage->mData, mMessage->mSize);
jsval argv[] = { OBJECT_TO_JSVAL(array) }; JS::Value argv[] = { OBJECT_TO_JSVAL(array) };
return JS_CallFunctionName(aCx, obj, "onNfcMessage", NS_ARRAY_LENGTH(argv), return JS_CallFunctionName(aCx, obj, "onNfcMessage", NS_ARRAY_LENGTH(argv),
argv, argv); argv, argv);
} }
@ -67,7 +172,9 @@ DispatchNfcEvent::RunTask(JSContext *aCx)
class NfcConnector : public mozilla::ipc::UnixSocketConnector class NfcConnector : public mozilla::ipc::UnixSocketConnector
{ {
public: public:
NfcConnector() {} NfcConnector()
{}
virtual ~NfcConnector() virtual ~NfcConnector()
{} {}
@ -97,7 +204,7 @@ NfcConnector::Create()
#endif #endif
if (fd < 0) { if (fd < 0) {
NS_WARNING("Could not open Nfc socket!"); NS_WARNING("Could not open nfc socket!");
return -1; return -1;
} }
@ -133,7 +240,7 @@ NfcConnector::CreateAddr(bool aIsServer,
break; break;
case AF_INET: case AF_INET:
aAddr.in.sin_family = af; aAddr.in.sin_family = af;
aAddr.in.sin_port = htons(NFCD_TEST_PORT); aAddr.in.sin_port = htons(NFC_TEST_PORT);
aAddr.in.sin_addr.s_addr = htons(INADDR_LOOPBACK); aAddr.in.sin_addr.s_addr = htons(INADDR_LOOPBACK);
aAddrSize = sizeof(sockaddr_in); aAddrSize = sizeof(sockaddr_in);
break; break;
@ -142,7 +249,6 @@ NfcConnector::CreateAddr(bool aIsServer,
return false; return false;
} }
return true; return true;
} }
bool bool
@ -168,7 +274,6 @@ NfcConnector::GetSocketAddr(const sockaddr_any& aAddr,
} // anonymous namespace } // anonymous namespace
namespace mozilla { namespace mozilla {
namespace ipc { namespace ipc {
@ -176,24 +281,46 @@ NfcConsumer::NfcConsumer(WorkerCrossThreadDispatcher* aDispatcher)
: mDispatcher(aDispatcher) : mDispatcher(aDispatcher)
, mShutdown(false) , mShutdown(false)
{ {
ConnectSocket(new NfcConnector(), NFC_SOCKET_NAME); mAddress = NFC_SOCKET_NAME;
ConnectSocket(new NfcConnector(), mAddress.get());
}
nsresult
NfcConsumer::Register(WorkerCrossThreadDispatcher* aDispatcher)
{
MOZ_ASSERT(NS_IsMainThread());
if (sNfcConsumer) {
return NS_ERROR_FAILURE;
}
nsRefPtr<ConnectWorkerToNFC> connection = new ConnectWorkerToNFC();
if (!aDispatcher->PostTask(connection)) {
return NS_ERROR_UNEXPECTED;
}
// Now that we're set up, connect ourselves to the NFC thread.
sNfcConsumer = new NfcConsumer(aDispatcher);
return NS_OK;
} }
void void
NfcConsumer::Shutdown() NfcConsumer::Shutdown()
{ {
mShutdown = true; MOZ_ASSERT(NS_IsMainThread());
CloseSocket();
sNfcConsumer->mShutdown = true;
sNfcConsumer->CloseSocket();
sNfcConsumer = nullptr;
} }
void void
NfcConsumer::ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage) NfcConsumer::ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage)
{ {
MOZ_ASSERT(NS_IsMainThread()); MOZ_ASSERT(NS_IsMainThread());
#ifdef DEBUG
LOG("ReceiveSocketData\n"); nsRefPtr<DispatchNFCEvent> dre(new DispatchNFCEvent(aMessage.forget()));
#endif
nsRefPtr<DispatchNfcEvent> dre(new DispatchNfcEvent(aMessage.forget()));
mDispatcher->PostTask(dre); mDispatcher->PostTask(dre);
} }
@ -201,26 +328,22 @@ void
NfcConsumer::OnConnectSuccess() NfcConsumer::OnConnectSuccess()
{ {
// Nothing to do here. // Nothing to do here.
LOG("Socket open for Nfc\n"); LOG("NFC: %s\n", __FUNCTION__);
} }
void void
NfcConsumer::OnConnectError() NfcConsumer::OnConnectError()
{ {
#ifdef DEBUG LOG("NFC: %s\n", __FUNCTION__);
LOG("%s\n", __FUNCTION__);
#endif
CloseSocket(); CloseSocket();
} }
void void
NfcConsumer::OnDisconnect() NfcConsumer::OnDisconnect()
{ {
#ifdef DEBUG LOG("NFC: %s\n", __FUNCTION__);
LOG("%s\n", __FUNCTION__);
#endif
if (!mShutdown) { if (!mShutdown) {
ConnectSocket(new NfcConnector(), NFC_SOCKET_NAME, 1000); ConnectSocket(new NfcConnector(), mAddress.get(), 1000);
} }
} }

View File

@ -4,6 +4,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* Copyright © 2013, Deutsche Telekom, Inc. */
#ifndef mozilla_ipc_Nfc_h #ifndef mozilla_ipc_Nfc_h
#define mozilla_ipc_Nfc_h 1 #define mozilla_ipc_Nfc_h 1
@ -16,19 +18,24 @@ namespace ipc {
class NfcConsumer : public mozilla::ipc::UnixSocketConsumer class NfcConsumer : public mozilla::ipc::UnixSocketConsumer
{ {
public: public:
NfcConsumer(mozilla::dom::workers::WorkerCrossThreadDispatcher* aDispatcher); virtual ~NfcConsumer() { }
virtual ~NfcConsumer() { }
void Shutdown();
private:
virtual void ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage);
virtual void OnConnectSuccess(); static nsresult Register(mozilla::dom::workers::WorkerCrossThreadDispatcher* aDispatcher);
virtual void OnConnectError(); static void Shutdown();
virtual void OnDisconnect();
private: private:
nsRefPtr<mozilla::dom::workers::WorkerCrossThreadDispatcher> mDispatcher; NfcConsumer(mozilla::dom::workers::WorkerCrossThreadDispatcher* aDispatcher);
bool mShutdown;
virtual void ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage);
virtual void OnConnectSuccess();
virtual void OnConnectError();
virtual void OnDisconnect();
private:
nsRefPtr<mozilla::dom::workers::WorkerCrossThreadDispatcher> mDispatcher;
nsCString mAddress;
bool mShutdown;
}; };
} // namespace ipc } // namespace ipc

View File

@ -3,7 +3,7 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this # 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/. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
if CONFIG['MOZ_B2G_NFC']: if CONFIG['MOZ_NFC']:
MODULE = 'ipc' MODULE = 'ipc'
EXPORTS.mozilla.ipc += [ EXPORTS.mozilla.ipc += [
'Nfc.h', 'Nfc.h',