Bug 1123651: Fix coding style in Nfc.cpp, r=allstars.chh

This commit is contained in:
Thomas Zimmermann 2015-01-21 10:59:21 +01:00
parent aac4993ed2
commit 32d1c4be8b

View File

@ -1,5 +1,5 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set sw=4 ts=8 et ft=cpp: */ /* vim: set sw=2 ts=8 et ft=cpp: */
/* This Source Code Form is subject to the terms of the Mozilla Public /* 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 * 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/. */
@ -37,73 +37,74 @@ const uint32_t NFC_TEST_PORT = 6400;
class SendNfcSocketDataTask : public nsRunnable class SendNfcSocketDataTask : public nsRunnable
{ {
public: public:
SendNfcSocketDataTask(NfcConsumer* aConsumer, UnixSocketRawData* aRawData) SendNfcSocketDataTask(NfcConsumer* aConsumer, UnixSocketRawData* aRawData)
: mConsumer(aConsumer), mRawData(aRawData) : mConsumer(aConsumer)
{ } , mRawData(aRawData)
{ }
NS_IMETHOD Run() NS_IMETHOD Run()
{ {
MOZ_ASSERT(NS_IsMainThread()); MOZ_ASSERT(NS_IsMainThread());
if (!mConsumer || if (!mConsumer ||
mConsumer->GetConnectionStatus() != SOCKET_CONNECTED) { mConsumer->GetConnectionStatus() != SOCKET_CONNECTED) {
// Probably shuting down. // Probably shuting down.
delete mRawData; delete mRawData;
return NS_OK; return NS_OK;
}
mConsumer->SendSocketData(mRawData);
return NS_OK;
} }
mConsumer->SendSocketData(mRawData);
return NS_OK;
}
private: private:
NfcConsumer* mConsumer; NfcConsumer* mConsumer;
UnixSocketRawData* mRawData; UnixSocketRawData* mRawData;
}; };
class NfcConnector : public mozilla::ipc::UnixSocketConnector class NfcConnector : public mozilla::ipc::UnixSocketConnector
{ {
public: public:
NfcConnector() NfcConnector()
{} { }
virtual ~NfcConnector() virtual ~NfcConnector()
{} { }
virtual int Create(); virtual int Create();
virtual bool CreateAddr(bool aIsServer, virtual bool CreateAddr(bool aIsServer,
socklen_t& aAddrSize, socklen_t& aAddrSize,
sockaddr_any& aAddr, sockaddr_any& aAddr,
const char* aAddress); const char* aAddress);
virtual bool SetUp(int aFd); virtual bool SetUp(int aFd);
virtual bool SetUpListenSocket(int aFd); virtual bool SetUpListenSocket(int aFd);
virtual void GetSocketAddr(const sockaddr_any& aAddr, virtual void GetSocketAddr(const sockaddr_any& aAddr,
nsAString& aAddrStr); nsAString& aAddrStr);
}; };
int int
NfcConnector::Create() NfcConnector::Create()
{ {
MOZ_ASSERT(!NS_IsMainThread()); MOZ_ASSERT(!NS_IsMainThread());
int fd = -1; int fd = -1;
#if defined(MOZ_WIDGET_GONK) #if defined(MOZ_WIDGET_GONK)
fd = socket(AF_LOCAL, SOCK_STREAM, 0); fd = socket(AF_LOCAL, SOCK_STREAM, 0);
#else #else
// If we can't hit a local loopback, fail later in connect. // If we can't hit a local loopback, fail later in connect.
fd = socket(AF_INET, SOCK_STREAM, 0); fd = socket(AF_INET, SOCK_STREAM, 0);
#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;
} }
if (!SetUp(fd)) { if (!SetUp(fd)) {
NS_WARNING("Could not set up socket!"); NS_WARNING("Could not set up socket!");
} }
return fd; return fd;
} }
bool bool
@ -112,56 +113,56 @@ NfcConnector::CreateAddr(bool aIsServer,
sockaddr_any& aAddr, sockaddr_any& aAddr,
const char* aAddress) const char* aAddress)
{ {
// We never open nfc socket as server. // We never open nfc socket as server.
MOZ_ASSERT(!aIsServer); MOZ_ASSERT(!aIsServer);
uint32_t af; uint32_t af;
#if defined(MOZ_WIDGET_GONK) #if defined(MOZ_WIDGET_GONK)
af = AF_LOCAL; af = AF_LOCAL;
#else #else
af = AF_INET; af = AF_INET;
#endif #endif
switch (af) { switch (af) {
case AF_LOCAL: case AF_LOCAL:
aAddr.un.sun_family = af; aAddr.un.sun_family = af;
if(strlen(aAddress) > sizeof(aAddr.un.sun_path)) { if(strlen(aAddress) > sizeof(aAddr.un.sun_path)) {
NS_WARNING("Address too long for socket struct!"); NS_WARNING("Address too long for socket struct!");
return false; return false;
}
strcpy((char*)&aAddr.un.sun_path, aAddress);
aAddrSize = strlen(aAddress) + offsetof(struct sockaddr_un, sun_path) + 1;
break;
case AF_INET:
aAddr.in.sin_family = af;
aAddr.in.sin_port = htons(NFC_TEST_PORT);
aAddr.in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
aAddrSize = sizeof(sockaddr_in);
break;
default:
NS_WARNING("Socket type not handled by connector!");
return false;
} }
return true; strcpy((char*)&aAddr.un.sun_path, aAddress);
aAddrSize = strlen(aAddress) + offsetof(struct sockaddr_un, sun_path) + 1;
break;
case AF_INET:
aAddr.in.sin_family = af;
aAddr.in.sin_port = htons(NFC_TEST_PORT);
aAddr.in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
aAddrSize = sizeof(sockaddr_in);
break;
default:
NS_WARNING("Socket type not handled by connector!");
return false;
}
return true;
} }
bool bool
NfcConnector::SetUp(int aFd) NfcConnector::SetUp(int aFd)
{ {
// Nothing to do here. // Nothing to do here.
return true; return true;
} }
bool bool
NfcConnector::SetUpListenSocket(int aFd) NfcConnector::SetUpListenSocket(int aFd)
{ {
// Nothing to do here. // Nothing to do here.
return true; return true;
} }
void void
NfcConnector::GetSocketAddr(const sockaddr_any& aAddr, NfcConnector::GetSocketAddr(const sockaddr_any& aAddr,
nsAString& aAddrStr) nsAString& aAddrStr)
{ {
MOZ_CRASH("This should never be called!"); MOZ_CRASH("This should never be called!");
} }
} // anonymous namespace } // anonymous namespace
@ -170,66 +171,66 @@ namespace mozilla {
namespace ipc { namespace ipc {
NfcConsumer::NfcConsumer(NfcSocketListener* aListener) NfcConsumer::NfcConsumer(NfcSocketListener* aListener)
: mListener(aListener) : mListener(aListener)
, mShutdown(false) , mShutdown(false)
{ {
mAddress = NFC_SOCKET_NAME; mAddress = NFC_SOCKET_NAME;
ConnectSocket(new NfcConnector(), mAddress.get()); ConnectSocket(new NfcConnector(), mAddress.get());
} }
void void
NfcConsumer::Shutdown() NfcConsumer::Shutdown()
{ {
MOZ_ASSERT(NS_IsMainThread()); MOZ_ASSERT(NS_IsMainThread());
mShutdown = true; mShutdown = true;
CloseSocket(); CloseSocket();
} }
bool bool
NfcConsumer::PostToNfcDaemon(const uint8_t* aData, size_t aSize) NfcConsumer::PostToNfcDaemon(const uint8_t* aData, size_t aSize)
{ {
MOZ_ASSERT(!NS_IsMainThread()); MOZ_ASSERT(!NS_IsMainThread());
UnixSocketRawData* raw = new UnixSocketRawData(aData, aSize); UnixSocketRawData* raw = new UnixSocketRawData(aData, aSize);
nsRefPtr<SendNfcSocketDataTask> task = new SendNfcSocketDataTask(this, raw); nsRefPtr<SendNfcSocketDataTask> task = new SendNfcSocketDataTask(this, raw);
NS_DispatchToMainThread(task); NS_DispatchToMainThread(task);
return true; return true;
} }
void void
NfcConsumer::ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aData) NfcConsumer::ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aData)
{ {
MOZ_ASSERT(NS_IsMainThread()); MOZ_ASSERT(NS_IsMainThread());
if (mListener) { if (mListener) {
mListener->ReceiveSocketData(aData); mListener->ReceiveSocketData(aData);
} }
} }
void void
NfcConsumer::OnConnectSuccess() NfcConsumer::OnConnectSuccess()
{ {
// Nothing to do here. // Nothing to do here.
CHROMIUM_LOG("NFC: %s\n", __FUNCTION__); CHROMIUM_LOG("NFC: %s\n", __FUNCTION__);
} }
void void
NfcConsumer::OnConnectError() NfcConsumer::OnConnectError()
{ {
CHROMIUM_LOG("NFC: %s\n", __FUNCTION__); CHROMIUM_LOG("NFC: %s\n", __FUNCTION__);
CloseSocket(); CloseSocket();
} }
void void
NfcConsumer::OnDisconnect() NfcConsumer::OnDisconnect()
{ {
CHROMIUM_LOG("NFC: %s\n", __FUNCTION__); CHROMIUM_LOG("NFC: %s\n", __FUNCTION__);
if (!mShutdown) { if (!mShutdown) {
ConnectSocket(new NfcConnector(), mAddress.get(), ConnectSocket(new NfcConnector(), mAddress.get(),
GetSuggestedConnectDelayMs()); GetSuggestedConnectDelayMs());
} }
} }
} // namespace ipc } // namespace ipc