Bug 851046: Patch 4 - Apply BluetoothSocket to BluetoothOppManager; r=mrbkap

--HG--
extra : rebase_source : 86f3d7acbfa097f92a2731d49ff5298ab4ad7a7c
This commit is contained in:
Eric Chou 2013-04-04 17:25:44 -07:00
parent 4dcbd2a0c8
commit 143a369f92
2 changed files with 68 additions and 61 deletions

View File

@ -9,6 +9,7 @@
#include "BluetoothReplyRunnable.h" #include "BluetoothReplyRunnable.h"
#include "BluetoothService.h" #include "BluetoothService.h"
#include "BluetoothSocket.h"
#include "BluetoothUtils.h" #include "BluetoothUtils.h"
#include "BluetoothUuid.h" #include "BluetoothUuid.h"
#include "ObexBase.h" #include "ObexBase.h"
@ -84,7 +85,7 @@ static const uint32_t kUpdateProgressBase = 50 * 1024;
*/ */
static const uint32_t kPutRequestHeaderSize = 6; static const uint32_t kPutRequestHeaderSize = 6;
StaticRefPtr<BluetoothOppManager> sInstance; StaticAutoPtr<BluetoothOppManager> sInstance;
StaticRefPtr<BluetoothOppManagerObserver> sOppObserver; StaticRefPtr<BluetoothOppManagerObserver> sOppObserver;
/* /*
@ -194,18 +195,23 @@ private:
class CloseSocketTask : public Task class CloseSocketTask : public Task
{ {
public: public:
CloseSocketTask(BluetoothSocket* aSocket) : mSocket(aSocket)
{
MOZ_ASSERT(aSocket);
}
void Run() MOZ_OVERRIDE void Run() MOZ_OVERRIDE
{ {
if (!sInstance) { MOZ_ASSERT(NS_IsMainThread());
NS_WARNING("BluetoothOppManager no longer exists, cannot close socket!");
return;
}
if (sInstance->GetConnectionStatus() == if (mSocket->GetConnectionStatus() ==
SocketConnectionStatus::SOCKET_CONNECTED) { SocketConnectionStatus::SOCKET_CONNECTED) {
sInstance->CloseSocket(); mSocket->Disconnect();
} }
} }
private:
nsRefPtr<BluetoothSocket> mSocket;
}; };
BluetoothOppManager::BluetoothOppManager() : mConnected(false) BluetoothOppManager::BluetoothOppManager() : mConnected(false)
@ -223,8 +229,13 @@ BluetoothOppManager::BluetoothOppManager() : mConnected(false)
, mSuccessFlag(false) , mSuccessFlag(false)
, mWaitingForConfirmationFlag(false) , mWaitingForConfirmationFlag(false)
{ {
mConnectedDeviceAddress.AssignLiteral("00:00:00:00:00:00"); mConnectedDeviceAddress.AssignLiteral(BLUETOOTH_ADDRESS_NONE);
mSocketStatus = GetConnectionStatus();
mSocket = new BluetoothSocket(this,
BluetoothSocketType::RFCOMM,
true,
true);
mSocketStatus = mSocket->GetConnectionStatus();
} }
BluetoothOppManager::~BluetoothOppManager() BluetoothOppManager::~BluetoothOppManager()
@ -237,7 +248,7 @@ BluetoothOppManager::Get()
{ {
MOZ_ASSERT(NS_IsMainThread()); MOZ_ASSERT(NS_IsMainThread());
if (sInstance == nullptr) { if (!sInstance) {
sInstance = new BluetoothOppManager(); sInstance = new BluetoothOppManager();
} }
@ -250,13 +261,15 @@ BluetoothOppManager::Connect(const nsAString& aDeviceObjectPath,
{ {
MOZ_ASSERT(NS_IsMainThread()); MOZ_ASSERT(NS_IsMainThread());
if (GetConnectionStatus() == SocketConnectionStatus::SOCKET_CONNECTED || SocketConnectionStatus s = mSocket->GetConnectionStatus();
GetConnectionStatus() == SocketConnectionStatus::SOCKET_CONNECTING) {
if (s == SocketConnectionStatus::SOCKET_CONNECTED ||
s == SocketConnectionStatus::SOCKET_CONNECTING) {
NS_WARNING("BluetoothOppManager has been already connected"); NS_WARNING("BluetoothOppManager has been already connected");
return false; return false;
} }
CloseSocket(); Disconnect();
BluetoothService* bs = BluetoothService::Get(); BluetoothService* bs = BluetoothService::Get();
if (!bs) { if (!bs) {
@ -274,7 +287,7 @@ BluetoothOppManager::Connect(const nsAString& aDeviceObjectPath,
BluetoothSocketType::RFCOMM, BluetoothSocketType::RFCOMM,
true, true,
true, true,
this, mSocket,
mRunnable); mRunnable);
return NS_FAILED(rv) ? false : true; return NS_FAILED(rv) ? false : true;
@ -283,12 +296,7 @@ BluetoothOppManager::Connect(const nsAString& aDeviceObjectPath,
void void
BluetoothOppManager::Disconnect() BluetoothOppManager::Disconnect()
{ {
if (GetConnectionStatus() == SocketConnectionStatus::SOCKET_DISCONNECTED) { mSocket->Disconnect();
NS_WARNING("BluetoothOppManager has been disconnected!");
return;
}
CloseSocket();
} }
nsresult nsresult
@ -296,7 +304,7 @@ BluetoothOppManager::HandleShutdown()
{ {
MOZ_ASSERT(NS_IsMainThread()); MOZ_ASSERT(NS_IsMainThread());
sInShutdown = true; sInShutdown = true;
CloseSocket(); Disconnect();
sInstance = nullptr; sInstance = nullptr;
return NS_OK; return NS_OK;
} }
@ -306,28 +314,21 @@ BluetoothOppManager::Listen()
{ {
MOZ_ASSERT(NS_IsMainThread()); MOZ_ASSERT(NS_IsMainThread());
if (GetConnectionStatus() == SocketConnectionStatus::SOCKET_LISTENING) { if (mSocket->GetConnectionStatus() ==
SocketConnectionStatus::SOCKET_LISTENING) {
NS_WARNING("BluetoothOppManager has been already listening"); NS_WARNING("BluetoothOppManager has been already listening");
return true; return true;
} }
CloseSocket(); Disconnect();
BluetoothService* bs = BluetoothService::Get(); if (!mSocket->Listen(BluetoothReservedChannels::CHANNEL_OPUSH)) {
if (!bs) { NS_WARNING("[OPP] Can't listen on socket!");
NS_WARNING("BluetoothService not available!");
return false; return false;
} }
nsresult rv = mSocketStatus = mSocket->GetConnectionStatus();
bs->ListenSocketViaService(BluetoothReservedChannels::CHANNEL_OPUSH, return true;
BluetoothSocketType::RFCOMM,
true,
true,
this);
mSocketStatus = GetConnectionStatus();
return NS_FAILED(rv) ? false : true;
} }
bool bool
@ -691,7 +692,8 @@ BluetoothOppManager::ServerDataHandler(UnixSocketRawData* aMessage)
// When there's a Put packet right after a PutFinal packet, // When there's a Put packet right after a PutFinal packet,
// which means it's the start point of a new file. // which means it's the start point of a new file.
if (mPutFinalFlag && if (mPutFinalFlag &&
(opCode == ObexRequestCode::Put || opCode == ObexRequestCode::PutFinal)) { (opCode == ObexRequestCode::Put ||
opCode == ObexRequestCode::PutFinal)) {
mNewFileFlag = true; mNewFileFlag = true;
AfterFirstPut(); AfterFirstPut();
} }
@ -864,7 +866,7 @@ BluetoothOppManager::ClientDataHandler(UnixSocketRawData* aMessage)
// Disconnect request, so we make a delay here. If the socket hasn't been // Disconnect request, so we make a delay here. If the socket hasn't been
// disconnected, we will close it. // disconnected, we will close it.
MessageLoop::current()-> MessageLoop::current()->
PostDelayedTask(FROM_HERE, new CloseSocketTask(), 1000); PostDelayedTask(FROM_HERE, new CloseSocketTask(mSocket), 1000);
} else if (mLastCommand == ObexRequestCode::Connect) { } else if (mLastCommand == ObexRequestCode::Connect) {
MOZ_ASSERT(!sFileName.IsEmpty()); MOZ_ASSERT(!sFileName.IsEmpty());
MOZ_ASSERT(mBlob); MOZ_ASSERT(mBlob);
@ -957,7 +959,7 @@ BluetoothOppManager::SendConnectRequest()
UnixSocketRawData* s = new UnixSocketRawData(index); UnixSocketRawData* s = new UnixSocketRawData(index);
memcpy(s->mData, req, s->mSize); memcpy(s->mData, req, s->mSize);
SendSocketData(s); mSocket->SendSocketData(s);
} }
void void
@ -987,7 +989,7 @@ BluetoothOppManager::SendPutHeaderRequest(const nsAString& aFileName,
UnixSocketRawData* s = new UnixSocketRawData(index); UnixSocketRawData* s = new UnixSocketRawData(index);
memcpy(s->mData, req, s->mSize); memcpy(s->mData, req, s->mSize);
SendSocketData(s); mSocket->SendSocketData(s);
delete [] fileName; delete [] fileName;
delete [] req; delete [] req;
@ -1017,7 +1019,7 @@ BluetoothOppManager::SendPutRequest(uint8_t* aFileBody,
UnixSocketRawData* s = new UnixSocketRawData(index); UnixSocketRawData* s = new UnixSocketRawData(index);
memcpy(s->mData, req, s->mSize); memcpy(s->mData, req, s->mSize);
SendSocketData(s); mSocket->SendSocketData(s);
delete [] req; delete [] req;
} }
@ -1043,7 +1045,7 @@ BluetoothOppManager::SendPutFinalRequest()
UnixSocketRawData* s = new UnixSocketRawData(index); UnixSocketRawData* s = new UnixSocketRawData(index);
memcpy(s->mData, req, s->mSize); memcpy(s->mData, req, s->mSize);
SendSocketData(s); mSocket->SendSocketData(s);
sWaitingToSendPutFinal = false; sWaitingToSendPutFinal = false;
@ -1063,7 +1065,7 @@ BluetoothOppManager::SendDisconnectRequest()
UnixSocketRawData* s = new UnixSocketRawData(index); UnixSocketRawData* s = new UnixSocketRawData(index);
memcpy(s->mData, req, s->mSize); memcpy(s->mData, req, s->mSize);
SendSocketData(s); mSocket->SendSocketData(s);
} }
void void
@ -1079,7 +1081,7 @@ BluetoothOppManager::SendAbortRequest()
UnixSocketRawData* s = new UnixSocketRawData(index); UnixSocketRawData* s = new UnixSocketRawData(index);
memcpy(s->mData, req, s->mSize); memcpy(s->mData, req, s->mSize);
SendSocketData(s); mSocket->SendSocketData(s);
} }
bool bool
@ -1109,7 +1111,7 @@ BluetoothOppManager::ReplyToConnect()
UnixSocketRawData* s = new UnixSocketRawData(index); UnixSocketRawData* s = new UnixSocketRawData(index);
memcpy(s->mData, req, s->mSize); memcpy(s->mData, req, s->mSize);
SendSocketData(s); mSocket->SendSocketData(s);
} }
void void
@ -1127,7 +1129,7 @@ BluetoothOppManager::ReplyToDisconnect()
UnixSocketRawData* s = new UnixSocketRawData(index); UnixSocketRawData* s = new UnixSocketRawData(index);
memcpy(s->mData, req, s->mSize); memcpy(s->mData, req, s->mSize);
SendSocketData(s); mSocket->SendSocketData(s);
} }
void void
@ -1158,7 +1160,7 @@ BluetoothOppManager::ReplyToPut(bool aFinal, bool aContinue)
UnixSocketRawData* s = new UnixSocketRawData(index); UnixSocketRawData* s = new UnixSocketRawData(index);
memcpy(s->mData, req, s->mSize); memcpy(s->mData, req, s->mSize);
SendSocketData(s); mSocket->SendSocketData(s);
} }
void void
@ -1313,9 +1315,8 @@ BluetoothOppManager::OnConnectSuccess()
// Cache device address since we can't get socket address when a remote // Cache device address since we can't get socket address when a remote
// device disconnect with us. // device disconnect with us.
GetSocketAddr(mConnectedDeviceAddress); mSocket->GetAddress(mConnectedDeviceAddress);
mSocketStatus = mSocket->GetConnectionStatus();
mSocketStatus = GetConnectionStatus();
} }
void void
@ -1332,8 +1333,8 @@ BluetoothOppManager::OnConnectError()
mRunnable.forget(); mRunnable.forget();
} }
CloseSocket(); mSocket->Disconnect();
mSocketStatus = GetConnectionStatus(); mSocketStatus = mSocket->GetConnectionStatus();
Listen(); Listen();
} }
@ -1355,7 +1356,8 @@ BluetoothOppManager::OnDisconnect()
nsString data; nsString data;
CopyASCIItoUTF16("modified", data); CopyASCIItoUTF16("modified", data);
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService(); nsCOMPtr<nsIObserverService> obs =
mozilla::services::GetObserverService();
if (obs) { if (obs) {
obs->NotifyObservers(mDsFile, "file-watcher-notify", data.get()); obs->NotifyObservers(mDsFile, "file-watcher-notify", data.get());
} }
@ -1372,6 +1374,6 @@ BluetoothOppManager::OnDisconnect()
} }
AfterOppDisconnected(); AfterOppDisconnected();
mConnectedDeviceAddress.AssignLiteral("00:00:00:00:00:00"); mConnectedDeviceAddress.AssignLiteral(BLUETOOTH_ADDRESS_NONE);
mSuccessFlag = false; mSuccessFlag = false;
} }

View File

@ -8,6 +8,7 @@
#define mozilla_dom_bluetooth_bluetoothoppmanager_h__ #define mozilla_dom_bluetooth_bluetoothoppmanager_h__
#include "BluetoothCommon.h" #include "BluetoothCommon.h"
#include "BluetoothSocketObserver.h"
#include "mozilla/dom/ipc/Blob.h" #include "mozilla/dom/ipc/Blob.h"
#include "mozilla/ipc/UnixSocket.h" #include "mozilla/ipc/UnixSocket.h"
#include "DeviceStorage.h" #include "DeviceStorage.h"
@ -18,9 +19,10 @@ class nsIInputStream;
BEGIN_BLUETOOTH_NAMESPACE BEGIN_BLUETOOTH_NAMESPACE
class BluetoothReplyRunnable; class BluetoothReplyRunnable;
class BluetoothSocket;
class ObexHeaderSet; class ObexHeaderSet;
class BluetoothOppManager : public mozilla::ipc::UnixSocketConsumer class BluetoothOppManager : public BluetoothSocketObserver
{ {
public: public:
/* /*
@ -33,8 +35,6 @@ public:
~BluetoothOppManager(); ~BluetoothOppManager();
static BluetoothOppManager* Get(); static BluetoothOppManager* Get();
void ReceiveSocketData(nsAutoPtr<mozilla::ipc::UnixSocketRawData>& aMessage)
MOZ_OVERRIDE;
void ClientDataHandler(mozilla::ipc::UnixSocketRawData* aMessage); void ClientDataHandler(mozilla::ipc::UnixSocketRawData* aMessage);
void ServerDataHandler(mozilla::ipc::UnixSocketRawData* aMessage); void ServerDataHandler(mozilla::ipc::UnixSocketRawData* aMessage);
@ -72,6 +72,14 @@ public:
// Return true if there is an ongoing file-transfer session, please see // Return true if there is an ongoing file-transfer session, please see
// Bug 827267 for more information. // Bug 827267 for more information.
bool IsTransferring(); bool IsTransferring();
// Implement interface BluetoothSocketObserver
void ReceiveSocketData(nsAutoPtr<mozilla::ipc::UnixSocketRawData>& aMessage)
MOZ_OVERRIDE;
void OnConnectSuccess() MOZ_OVERRIDE;
void OnConnectError() MOZ_OVERRIDE;
void OnDisconnect() MOZ_OVERRIDE;
private: private:
BluetoothOppManager(); BluetoothOppManager();
void StartFileTransfer(); void StartFileTransfer();
@ -90,10 +98,6 @@ private:
void ValidateFileName(); void ValidateFileName();
bool IsReservedChar(PRUnichar c); bool IsReservedChar(PRUnichar c);
virtual void OnConnectSuccess() MOZ_OVERRIDE;
virtual void OnConnectError() MOZ_OVERRIDE;
virtual void OnDisconnect() MOZ_OVERRIDE;
/** /**
* RFCOMM socket status. * RFCOMM socket status.
*/ */
@ -173,6 +177,7 @@ private:
nsRefPtr<BluetoothReplyRunnable> mRunnable; nsRefPtr<BluetoothReplyRunnable> mRunnable;
nsRefPtr<DeviceStorageFile> mDsFile; nsRefPtr<DeviceStorageFile> mDsFile;
nsRefPtr<BluetoothSocket> mSocket;
}; };
END_BLUETOOTH_NAMESPACE END_BLUETOOTH_NAMESPACE