Bug 1200132 - Use adderss to create socketTransport, r=schien, junior

This commit is contained in:
Kershaw Chang 2015-10-21 02:54:00 +02:00
parent c2b002db5f
commit 92d27d1149
6 changed files with 66 additions and 49 deletions

View File

@ -14,11 +14,11 @@ interface nsIPresentationControlChannel;
/*
* The device information required for establishing TCP control channel.
*/
[scriptable, uuid(7fce55c0-2470-4a41-a3b9-c35fbe55f206)]
[scriptable, uuid(296fd171-e4d0-4de0-99ff-ad8ed52ddef3)]
interface nsITCPDeviceInfo: nsISupports
{
readonly attribute AUTF8String id;
readonly attribute AUTF8String host;
readonly attribute AUTF8String address;
readonly attribute uint16_t port;
};

View File

@ -53,10 +53,10 @@ public:
NS_DECL_NSITCPDEVICEINFO
explicit TCPDeviceInfo(const nsACString& aId,
const nsACString& aHost,
const nsACString& aAddress,
const uint16_t aPort)
: mId(aId)
, mHost(aHost)
, mAddress(aAddress)
, mPort(aPort)
{
}
@ -65,7 +65,7 @@ private:
virtual ~TCPDeviceInfo() {}
nsCString mId;
nsCString mHost;
nsCString mAddress;
uint16_t mPort;
};
@ -81,9 +81,9 @@ TCPDeviceInfo::GetId(nsACString& aId)
}
NS_IMETHODIMP
TCPDeviceInfo::GetHost(nsACString& aHost)
TCPDeviceInfo::GetAddress(nsACString& aAddress)
{
aHost = mHost;
aAddress = mAddress;
return NS_OK;
}
@ -319,25 +319,26 @@ MulticastDNSDeviceProvider::RequestSession(Device* aDevice,
MOZ_ASSERT(mPresentationServer);
RefPtr<TCPDeviceInfo> deviceInfo = new TCPDeviceInfo(aDevice->Id(),
aDevice->Host(),
aDevice->Address(),
aDevice->Port());
return mPresentationServer->RequestSession(deviceInfo, aUrl, aPresentationId, aRetVal);
}
nsresult
MulticastDNSDeviceProvider::AddDevice(const nsACString& aServiceName,
MulticastDNSDeviceProvider::AddDevice(const nsACString& aId,
const nsACString& aServiceName,
const nsACString& aServiceType,
const nsACString& aHost,
const nsACString& aAddress,
const uint16_t aPort)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mPresentationServer);
RefPtr<Device> device = new Device(aHost, /* ID */
RefPtr<Device> device = new Device(aId, /* ID */
aServiceName,
aServiceType,
aHost,
aAddress,
aPort,
DeviceState::eActive,
this);
@ -356,7 +357,7 @@ nsresult
MulticastDNSDeviceProvider::UpdateDevice(const uint32_t aIndex,
const nsACString& aServiceName,
const nsACString& aServiceType,
const nsACString& aHost,
const nsACString& aAddress,
const uint16_t aPort)
{
MOZ_ASSERT(NS_IsMainThread());
@ -367,7 +368,7 @@ MulticastDNSDeviceProvider::UpdateDevice(const uint32_t aIndex,
}
RefPtr<Device> device = mDevices[aIndex];
device->Update(aServiceName, aServiceType, aHost, aPort);
device->Update(aServiceName, aServiceType, aAddress, aPort);
device->ChangeState(DeviceState::eActive);
nsCOMPtr<nsIPresentationDeviceListener> listener;
@ -425,19 +426,19 @@ MulticastDNSDeviceProvider::FindDeviceById(const nsACString& aId,
}
bool
MulticastDNSDeviceProvider::FindDeviceByHost(const nsACString& aHost,
uint32_t& aIndex)
MulticastDNSDeviceProvider::FindDeviceByAddress(const nsACString& aAddress,
uint32_t& aIndex)
{
MOZ_ASSERT(NS_IsMainThread());
RefPtr<Device> device = new Device(/* aId = */ EmptyCString(),
/* aName = */ EmptyCString(),
/* aType = */ EmptyCString(),
aHost,
aAddress,
/* aPort = */ 0,
/* aState = */ DeviceState::eUnknown,
/* aProvider = */ nullptr);
size_t index = mDevices.IndexOf(device, 0, DeviceHostComparator());
size_t index = mDevices.IndexOf(device, 0, DeviceAddressComparator());
if (index == mDevices.NoIndex) {
return false;
@ -775,6 +776,11 @@ MulticastDNSDeviceProvider::OnServiceResolved(nsIDNSServiceInfo* aServiceInfo)
return rv;
}
nsAutoCString address;
if (NS_WARN_IF(NS_FAILED(rv = aServiceInfo->GetAddress(address)))) {
return rv;
}
uint16_t port;
if (NS_WARN_IF(NS_FAILED(rv = aServiceInfo->GetPort(&port)))) {
return rv;
@ -790,12 +796,13 @@ MulticastDNSDeviceProvider::OnServiceResolved(nsIDNSServiceInfo* aServiceInfo)
return UpdateDevice(index,
serviceName,
serviceType,
host,
address,
port);
} else {
return AddDevice(serviceName,
return AddDevice(host,
serviceName,
serviceType,
host,
address,
port);
}
@ -838,14 +845,14 @@ MulticastDNSDeviceProvider::OnSessionRequest(nsITCPDeviceInfo* aDeviceInfo,
{
MOZ_ASSERT(NS_IsMainThread());
nsAutoCString host;
unused << aDeviceInfo->GetHost(host);
nsAutoCString address;
unused << aDeviceInfo->GetAddress(address);
LOG_I("OnSessionRequest: %s", host.get());
LOG_I("OnSessionRequest: %s", address.get());
RefPtr<Device> device;
uint32_t index;
if (FindDeviceByHost(host, index)) {
if (FindDeviceByAddress(address, index)) {
device = mDevices[index];
} else {
// create a one-time device object for non-discoverable controller
@ -859,7 +866,7 @@ MulticastDNSDeviceProvider::OnSessionRequest(nsITCPDeviceInfo* aDeviceInfo,
device = new Device(id,
/* aName = */ id,
/* aType = */ EmptyCString(),
host,
address,
port,
DeviceState::eActive,
/* aProvider = */ nullptr);

View File

@ -62,14 +62,14 @@ private:
explicit Device(const nsACString& aId,
const nsACString& aName,
const nsACString& aType,
const nsACString& aHost,
const nsACString& aAddress,
const uint16_t aPort,
DeviceState aState,
MulticastDNSDeviceProvider* aProvider)
: mId(aId)
, mName(aName)
, mType(aType)
, mHost(aHost)
, mAddress(aAddress)
, mPort(aPort)
, mState(aState)
, mProvider(aProvider)
@ -81,9 +81,9 @@ private:
return mId;
}
const nsCString& Host() const
const nsCString& Address() const
{
return mHost;
return mAddress;
}
const uint16_t Port() const
@ -103,12 +103,12 @@ private:
void Update(const nsACString& aName,
const nsACString& aType,
const nsACString& aHost,
const nsACString& aAddress,
const uint16_t aPort)
{
mName = aName;
mType = aType;
mHost = aHost;
mAddress = aAddress;
mPort = aPort;
}
@ -118,7 +118,7 @@ private:
nsCString mId;
nsCString mName;
nsCString mType;
nsCString mHost;
nsCString mAddress;
uint16_t mPort;
DeviceState mState;
MulticastDNSDeviceProvider* mProvider;
@ -130,9 +130,9 @@ private:
}
};
struct DeviceHostComparator {
struct DeviceAddressComparator {
bool Equals(const RefPtr<Device>& aA, const RefPtr<Device>& aB) const {
return aA->Host() == aB->Host();
return aA->Address() == aB->Address();
}
};
@ -146,21 +146,22 @@ private:
nsIPresentationControlChannel** aRetVal);
// device manipulation
nsresult AddDevice(const nsACString& aServiceName,
nsresult AddDevice(const nsACString& aId,
const nsACString& aServiceName,
const nsACString& aServiceType,
const nsACString& aHost,
const nsACString& aAddress,
const uint16_t aPort);
nsresult UpdateDevice(const uint32_t aIndex,
const nsACString& aServiceName,
const nsACString& aServiceType,
const nsACString& aHost,
const nsACString& aAddress,
const uint16_t aPort);
nsresult RemoveDevice(const uint32_t aIndex);
bool FindDeviceById(const nsACString& aId,
uint32_t& aIndex);
bool FindDeviceByHost(const nsACString& aHost,
uint32_t& aIndex);
bool FindDeviceByAddress(const nsACString& aAddress,
uint32_t& aIndex);
void MarkAllDevicesUnknown();
void ClearUnknownDevices();

View File

@ -14,8 +14,8 @@ function log(aMsg) {
dump("-*- TCPPresentationServer.js: " + aMsg + "\n");
}
function TCPDeviceInfo(aHost, aPort, aId) {
this.host = aHost;
function TCPDeviceInfo(aAddress, aPort, aId) {
this.address = aAddress;
this.port = aPort;
this.id = aId;
}
@ -125,7 +125,7 @@ TCPPresentationServer.prototype = {
try {
socketTransport = sts.createTransport(null,
0,
aDeviceInfo.host,
aDeviceInfo.address,
aDeviceInfo.port,
null);
} catch (e) {
@ -160,7 +160,7 @@ TCPPresentationServer.prototype = {
// Triggered by TCPControlChannel
onSessionRequest: function(aDeviceInfo, aUrl, aPresentationId, aControlChannel) {
DEBUG && log("TCPPresentationServer - onSessionRequest: "
+ aDeviceInfo.host + ":" + aDeviceInfo.port);
+ aDeviceInfo.address + ":" + aDeviceInfo.port);
this.listener.onSessionRequest(aDeviceInfo,
aUrl,
aPresentationId,

View File

@ -119,6 +119,14 @@ MockDNSServiceInfo.prototype = {
return this._host;
},
set address(aAddress) {
this._address = aAddress;
},
get address() {
return this._address;
},
set port(aPort) {
this._port = aPort;
},
@ -187,6 +195,7 @@ function createDevice(host, port, serviceName, serviceType, domainName, attribut
let device = new MockDNSServiceInfo();
device.host = host || "";
device.port = port || 0;
device.address = host || "";
device.serviceName = serviceName || "";
device.serviceType = serviceType || "";
device.domainName = domainName || "";
@ -432,7 +441,7 @@ function handleSessionRequest() {
let controlChannel = listener.device.establishControlChannel(testUrl, testPresentationId);
Assert.equal(mockServerObj.request.deviceInfo.id, mockDevice.host);
Assert.equal(mockServerObj.request.deviceInfo.host, mockDevice.host);
Assert.equal(mockServerObj.request.deviceInfo.address, mockDevice.host);
Assert.equal(mockServerObj.request.deviceInfo.port, mockDevice.port);
Assert.equal(mockServerObj.request.url, testUrl);
Assert.equal(mockServerObj.request.presentationId, testPresentationId);
@ -505,7 +514,7 @@ function handleOnSessionRequest() {
const deviceInfo = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsITCPDeviceInfo]),
id: mockDevice.host,
host: mockDevice.host,
address: mockDevice.host,
port: 54321,
};
@ -574,7 +583,7 @@ function handleOnSessionRequestFromUnknownDevice() {
const deviceInfo = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsITCPDeviceInfo]),
id: "unknown-device.local",
host: "unknown-device.local",
address: "unknown-device.local",
port: 12345,
};

View File

@ -72,7 +72,7 @@ function testPresentationServer() {
onSessionRequest: function(deviceInfo, url, presentationId, controlChannel) {
controllerControlChannel = controlChannel;
Assert.equal(deviceInfo.id, tps.id, 'expected device id');
Assert.equal(deviceInfo.host, '127.0.0.1', 'expected device host');
Assert.equal(deviceInfo.address, '127.0.0.1', 'expected device address');
Assert.equal(url, 'http://example.com', 'expected url');
Assert.equal(presentationId, 'testPresentationId', 'expected presentation id');
@ -117,7 +117,7 @@ function testPresentationServer() {
let presenterDeviceInfo = {
id: 'presentatorID',
host: '127.0.0.1',
address: '127.0.0.1',
port: PRESENTER_CONTROL_CHANNEL_PORT,
QueryInterface: XPCOMUtils.generateQI([Ci.nsITCPDeviceInfo]),
};