2015-05-21 06:06:00 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "MulticastDNSDeviceProvider.h"
|
2015-09-09 00:42:00 +03:00
|
|
|
#include "MainThreadUtils.h"
|
2015-05-21 06:06:00 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2015-07-30 19:40:00 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/Services.h"
|
2015-10-01 00:55:00 +03:00
|
|
|
#include "mozilla/unused.h"
|
2015-05-21 06:06:00 +03:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2015-07-30 19:40:00 +03:00
|
|
|
#include "nsIObserverService.h"
|
2016-05-27 02:22:27 +03:00
|
|
|
#include "nsIWritablePropertyBag2.h"
|
2015-05-21 06:06:00 +03:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2016-04-28 10:05:53 +03:00
|
|
|
#include "nsTCPDeviceInfo.h"
|
2015-11-03 03:04:00 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2015-05-21 06:06:00 +03:00
|
|
|
|
2015-10-20 11:47:56 +03:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
#include "nsIPropertyBag2.h"
|
|
|
|
#endif // MOZ_WIDGET_ANDROID
|
|
|
|
|
2015-07-30 19:40:00 +03:00
|
|
|
#define PREF_PRESENTATION_DISCOVERY "dom.presentation.discovery.enabled"
|
2015-10-01 00:55:00 +03:00
|
|
|
#define PREF_PRESENTATION_DISCOVERY_TIMEOUT_MS "dom.presentation.discovery.timeout_ms"
|
2015-07-30 19:40:00 +03:00
|
|
|
#define PREF_PRESENTATION_DISCOVERABLE "dom.presentation.discoverable"
|
|
|
|
#define PREF_PRESENTATION_DEVICE_NAME "dom.presentation.device.name"
|
|
|
|
|
2016-05-27 02:22:27 +03:00
|
|
|
#define SERVICE_TYPE "_presentation-ctrl._tcp"
|
|
|
|
#define PROTOCOL_VERSION_TAG "version"
|
2015-07-30 19:40:00 +03:00
|
|
|
|
2015-11-03 03:04:00 +03:00
|
|
|
static mozilla::LazyLogModule sMulticastDNSProviderLogModule("MulticastDNSDeviceProvider");
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
#undef LOG_I
|
2015-11-03 03:04:00 +03:00
|
|
|
#define LOG_I(...) MOZ_LOG(sMulticastDNSProviderLogModule, mozilla::LogLevel::Debug, (__VA_ARGS__))
|
2015-05-21 06:06:00 +03:00
|
|
|
#undef LOG_E
|
2015-11-03 03:04:00 +03:00
|
|
|
#define LOG_E(...) MOZ_LOG(sMulticastDNSProviderLogModule, mozilla::LogLevel::Error, (__VA_ARGS__))
|
2015-05-21 06:06:00 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace presentation {
|
|
|
|
|
2015-07-30 19:40:00 +03:00
|
|
|
static const char* kObservedPrefs[] = {
|
|
|
|
PREF_PRESENTATION_DISCOVERY,
|
2015-10-01 00:55:00 +03:00
|
|
|
PREF_PRESENTATION_DISCOVERY_TIMEOUT_MS,
|
2015-07-30 19:40:00 +03:00
|
|
|
PREF_PRESENTATION_DISCOVERABLE,
|
|
|
|
PREF_PRESENTATION_DEVICE_NAME,
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
2015-10-19 05:44:59 +03:00
|
|
|
namespace {
|
|
|
|
|
2015-10-20 11:47:56 +03:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
static void
|
|
|
|
GetAndroidDeviceName(nsACString& aRetVal)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPropertyBag2> infoService = do_GetService("@mozilla.org/system-info;1");
|
|
|
|
MOZ_ASSERT(infoService, "Could not find a system info service");
|
|
|
|
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << NS_WARN_IF(NS_FAILED(infoService->GetPropertyAsACString(
|
2015-10-20 11:47:56 +03:00
|
|
|
NS_LITERAL_STRING("device"), aRetVal)));
|
|
|
|
}
|
|
|
|
#endif // MOZ_WIDGET_ANDROID
|
|
|
|
|
2015-10-19 05:44:59 +03:00
|
|
|
} //anonymous namespace
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
/**
|
|
|
|
* This wrapper is used to break circular-reference problem.
|
|
|
|
*/
|
|
|
|
class DNSServiceWrappedListener final
|
|
|
|
: public nsIDNSServiceDiscoveryListener
|
|
|
|
, public nsIDNSRegistrationListener
|
|
|
|
, public nsIDNSServiceResolveListener
|
2016-05-20 09:53:30 +03:00
|
|
|
, public nsIPresentationControlServerListener
|
2015-05-21 06:06:00 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_FORWARD_SAFE_NSIDNSSERVICEDISCOVERYLISTENER(mListener)
|
|
|
|
NS_FORWARD_SAFE_NSIDNSREGISTRATIONLISTENER(mListener)
|
|
|
|
NS_FORWARD_SAFE_NSIDNSSERVICERESOLVELISTENER(mListener)
|
2016-05-20 09:53:30 +03:00
|
|
|
NS_FORWARD_SAFE_NSIPRESENTATIONCONTROLSERVERLISTENER(mListener)
|
2015-05-21 06:06:00 +03:00
|
|
|
|
|
|
|
explicit DNSServiceWrappedListener() = default;
|
|
|
|
|
|
|
|
nsresult SetListener(MulticastDNSDeviceProvider* aListener)
|
|
|
|
{
|
|
|
|
mListener = aListener;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~DNSServiceWrappedListener() = default;
|
|
|
|
|
|
|
|
MulticastDNSDeviceProvider* mListener = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(DNSServiceWrappedListener,
|
|
|
|
nsIDNSServiceDiscoveryListener,
|
|
|
|
nsIDNSRegistrationListener,
|
|
|
|
nsIDNSServiceResolveListener,
|
2016-05-20 09:53:30 +03:00
|
|
|
nsIPresentationControlServerListener)
|
2015-05-21 06:06:00 +03:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(MulticastDNSDeviceProvider,
|
|
|
|
nsIPresentationDeviceProvider,
|
|
|
|
nsIDNSServiceDiscoveryListener,
|
|
|
|
nsIDNSRegistrationListener,
|
|
|
|
nsIDNSServiceResolveListener,
|
2016-05-20 09:53:30 +03:00
|
|
|
nsIPresentationControlServerListener,
|
2015-07-30 19:40:00 +03:00
|
|
|
nsIObserver)
|
2015-05-21 06:06:00 +03:00
|
|
|
|
|
|
|
MulticastDNSDeviceProvider::~MulticastDNSDeviceProvider()
|
|
|
|
{
|
|
|
|
Uninit();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MulticastDNSDeviceProvider::Init()
|
|
|
|
{
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
if (mInitialized) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
mMulticastDNS = do_GetService(DNSSERVICEDISCOVERY_CONTRACT_ID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mWrappedListener = new DNSServiceWrappedListener();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = mWrappedListener->SetListener(this)))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-05-20 09:53:30 +03:00
|
|
|
mPresentationService = do_CreateInstance(PRESENTATION_CONTROL_SERVICE_CONTACT_ID, &rv);
|
2015-05-21 06:06:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-10-01 00:55:00 +03:00
|
|
|
mDiscoveryTimer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-07-30 19:40:00 +03:00
|
|
|
Preferences::AddStrongObservers(this, kObservedPrefs);
|
|
|
|
|
|
|
|
mDiscoveryEnabled = Preferences::GetBool(PREF_PRESENTATION_DISCOVERY);
|
2016-06-01 04:45:16 +03:00
|
|
|
mDiscoveryTimeoutMs = Preferences::GetUint(PREF_PRESENTATION_DISCOVERY_TIMEOUT_MS);
|
2015-07-30 19:40:00 +03:00
|
|
|
mDiscoverable = Preferences::GetBool(PREF_PRESENTATION_DISCOVERABLE);
|
|
|
|
mServiceName = Preferences::GetCString(PREF_PRESENTATION_DEVICE_NAME);
|
|
|
|
|
2015-10-20 11:47:56 +03:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
// FIXME: Bug 1185806 - Provide a common device name setting.
|
|
|
|
if (mServiceName.IsEmpty()) {
|
|
|
|
GetAndroidDeviceName(mServiceName);
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << Preferences::SetCString(PREF_PRESENTATION_DEVICE_NAME, mServiceName);
|
2015-10-20 11:47:56 +03:00
|
|
|
}
|
|
|
|
#endif // MOZ_WIDGET_ANDROID
|
|
|
|
|
2016-05-20 09:53:30 +03:00
|
|
|
Unused << mPresentationService->SetId(mServiceName);
|
2015-10-20 11:47:56 +03:00
|
|
|
|
2015-07-30 19:40:00 +03:00
|
|
|
if (mDiscoveryEnabled && NS_WARN_IF(NS_FAILED(rv = ForceDiscovery()))) {
|
2015-05-21 06:06:00 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-07-30 19:40:00 +03:00
|
|
|
if (mDiscoverable && NS_WARN_IF(NS_FAILED(rv = RegisterService()))) {
|
2015-05-21 06:06:00 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mInitialized = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MulticastDNSDeviceProvider::Uninit()
|
|
|
|
{
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
if (!mInitialized) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-30 02:48:00 +03:00
|
|
|
ClearDevices();
|
|
|
|
|
2015-07-30 19:40:00 +03:00
|
|
|
Preferences::RemoveObservers(this, kObservedPrefs);
|
|
|
|
|
|
|
|
StopDiscovery(NS_OK);
|
|
|
|
UnregisterService(NS_OK);
|
2015-05-21 06:06:00 +03:00
|
|
|
|
|
|
|
mMulticastDNS = nullptr;
|
|
|
|
|
|
|
|
if (mWrappedListener) {
|
|
|
|
mWrappedListener->SetListener(nullptr);
|
|
|
|
mWrappedListener = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mInitialized = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-07-30 19:40:00 +03:00
|
|
|
MulticastDNSDeviceProvider::RegisterService()
|
2015-05-21 06:06:00 +03:00
|
|
|
{
|
2015-07-30 19:40:00 +03:00
|
|
|
LOG_I("RegisterService: %s (%d)", mServiceName.get(), mDiscoverable);
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-07-30 19:40:00 +03:00
|
|
|
|
|
|
|
if (!mDiscoverable) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
nsresult rv;
|
2015-11-03 03:04:00 +03:00
|
|
|
|
2015-07-30 19:40:00 +03:00
|
|
|
uint16_t servicePort;
|
2016-05-20 09:53:30 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = mPresentationService->GetPort(&servicePort)))) {
|
2015-07-30 19:40:00 +03:00
|
|
|
return rv;
|
|
|
|
}
|
2015-05-21 06:06:00 +03:00
|
|
|
|
2015-11-03 03:04:00 +03:00
|
|
|
/**
|
|
|
|
* If |servicePort| is non-zero, it means PresentationServer is running.
|
|
|
|
* Otherwise, we should make it start serving.
|
|
|
|
*/
|
|
|
|
if (!servicePort) {
|
2016-05-20 09:53:30 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = mPresentationService->SetListener(mWrappedListener)))) {
|
2015-11-03 03:04:00 +03:00
|
|
|
return rv;
|
|
|
|
}
|
2016-05-20 09:53:30 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = mPresentationService->StartServer(0)))) {
|
2015-11-03 03:04:00 +03:00
|
|
|
return rv;
|
|
|
|
}
|
2016-05-20 09:53:30 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = mPresentationService->GetPort(&servicePort)))) {
|
2015-11-03 03:04:00 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel on going service registration.
|
|
|
|
if (mRegisterRequest) {
|
|
|
|
mRegisterRequest->Cancel(NS_OK);
|
|
|
|
mRegisterRequest = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-30 19:40:00 +03:00
|
|
|
/**
|
|
|
|
* Register the presentation control channel server as an mDNS service.
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsIDNSServiceInfo> serviceInfo =
|
|
|
|
do_CreateInstance(DNSSERVICEINFO_CONTRACT_ID, &rv);
|
2015-05-21 06:06:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2015-07-30 19:40:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = serviceInfo->SetServiceType(
|
|
|
|
NS_LITERAL_CSTRING(SERVICE_TYPE))))) {
|
2015-05-21 06:06:00 +03:00
|
|
|
return rv;
|
|
|
|
}
|
2015-07-30 19:40:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = serviceInfo->SetServiceName(mServiceName)))) {
|
2015-05-21 06:06:00 +03:00
|
|
|
return rv;
|
|
|
|
}
|
2015-07-30 19:40:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = serviceInfo->SetPort(servicePort)))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-05-27 02:22:27 +03:00
|
|
|
nsCOMPtr<nsIWritablePropertyBag2> propBag =
|
|
|
|
do_CreateInstance("@mozilla.org/hash-property-bag;1");
|
|
|
|
MOZ_ASSERT(propBag);
|
|
|
|
|
|
|
|
uint32_t version;
|
|
|
|
rv = mPresentationService->GetVersion(&version);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
rv = propBag->SetPropertyAsUint32(NS_LITERAL_STRING(PROTOCOL_VERSION_TAG),
|
|
|
|
version);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = serviceInfo->SetAttributes(propBag)))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-07-30 19:40:00 +03:00
|
|
|
return mMulticastDNS->RegisterService(serviceInfo,
|
|
|
|
mWrappedListener,
|
|
|
|
getter_AddRefs(mRegisterRequest));
|
|
|
|
}
|
2015-05-21 06:06:00 +03:00
|
|
|
|
2015-07-30 19:40:00 +03:00
|
|
|
nsresult
|
|
|
|
MulticastDNSDeviceProvider::UnregisterService(nsresult aReason)
|
|
|
|
{
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
if (mRegisterRequest) {
|
2015-07-30 19:40:00 +03:00
|
|
|
mRegisterRequest->Cancel(aReason);
|
2015-05-21 06:06:00 +03:00
|
|
|
mRegisterRequest = nullptr;
|
|
|
|
}
|
2015-07-30 19:40:00 +03:00
|
|
|
|
2016-05-20 09:53:30 +03:00
|
|
|
if (mPresentationService) {
|
|
|
|
mPresentationService->SetListener(nullptr);
|
|
|
|
mPresentationService->Close();
|
2015-07-30 19:40:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MulticastDNSDeviceProvider::StopDiscovery(nsresult aReason)
|
|
|
|
{
|
2015-09-30 02:48:00 +03:00
|
|
|
LOG_I("StopDiscovery (0x%08x)", aReason);
|
|
|
|
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-01 00:55:00 +03:00
|
|
|
MOZ_ASSERT(mDiscoveryTimer);
|
|
|
|
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << mDiscoveryTimer->Cancel();
|
2015-09-09 00:42:00 +03:00
|
|
|
|
2015-07-30 19:40:00 +03:00
|
|
|
if (mDiscoveryRequest) {
|
|
|
|
mDiscoveryRequest->Cancel(aReason);
|
|
|
|
mDiscoveryRequest = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2015-05-21 06:06:00 +03:00
|
|
|
}
|
|
|
|
|
2015-10-19 05:44:59 +03:00
|
|
|
nsresult
|
2016-07-04 13:12:04 +03:00
|
|
|
MulticastDNSDeviceProvider::Connect(Device* aDevice,
|
|
|
|
nsIPresentationControlChannel** aRetVal)
|
2015-10-19 05:44:59 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aDevice);
|
2016-05-20 09:53:30 +03:00
|
|
|
MOZ_ASSERT(mPresentationService);
|
2015-10-19 05:44:59 +03:00
|
|
|
|
|
|
|
RefPtr<TCPDeviceInfo> deviceInfo = new TCPDeviceInfo(aDevice->Id(),
|
2015-10-21 03:54:00 +03:00
|
|
|
aDevice->Address(),
|
2015-10-19 05:44:59 +03:00
|
|
|
aDevice->Port());
|
|
|
|
|
2016-07-04 13:12:04 +03:00
|
|
|
return mPresentationService->Connect(deviceInfo, aRetVal);
|
2015-10-19 05:44:59 +03:00
|
|
|
}
|
|
|
|
|
2016-05-27 02:22:27 +03:00
|
|
|
bool
|
|
|
|
MulticastDNSDeviceProvider::IsCompatibleServer(nsIDNSServiceInfo* aServiceInfo)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aServiceInfo);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPropertyBag2> propBag;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(
|
|
|
|
aServiceInfo->GetAttributes(getter_AddRefs(propBag)))) || !propBag) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t remoteVersion;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(
|
|
|
|
propBag->GetPropertyAsUint32(NS_LITERAL_STRING(PROTOCOL_VERSION_TAG),
|
|
|
|
&remoteVersion)))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isCompatible = false;
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(
|
|
|
|
mPresentationService->IsCompatibleServer(remoteVersion,
|
|
|
|
&isCompatible)));
|
|
|
|
|
|
|
|
return isCompatible;
|
|
|
|
}
|
|
|
|
|
2015-09-30 02:48:00 +03:00
|
|
|
nsresult
|
2015-10-21 03:54:00 +03:00
|
|
|
MulticastDNSDeviceProvider::AddDevice(const nsACString& aId,
|
|
|
|
const nsACString& aServiceName,
|
2015-09-30 02:48:00 +03:00
|
|
|
const nsACString& aServiceType,
|
2015-10-21 03:54:00 +03:00
|
|
|
const nsACString& aAddress,
|
2015-09-30 02:48:00 +03:00
|
|
|
const uint16_t aPort)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-05-20 09:53:30 +03:00
|
|
|
MOZ_ASSERT(mPresentationService);
|
2015-09-30 02:48:00 +03:00
|
|
|
|
2015-10-21 03:54:00 +03:00
|
|
|
RefPtr<Device> device = new Device(aId, /* ID */
|
2015-10-19 05:44:59 +03:00
|
|
|
aServiceName,
|
|
|
|
aServiceType,
|
2015-10-21 03:54:00 +03:00
|
|
|
aAddress,
|
2015-10-19 05:44:59 +03:00
|
|
|
aPort,
|
|
|
|
DeviceState::eActive,
|
|
|
|
this);
|
2015-09-30 02:48:00 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPresentationDeviceListener> listener;
|
|
|
|
if (NS_SUCCEEDED(GetListener(getter_AddRefs(listener))) && listener) {
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << listener->AddDevice(device);
|
2015-09-30 02:48:00 +03:00
|
|
|
}
|
|
|
|
|
2015-10-19 05:44:59 +03:00
|
|
|
mDevices.AppendElement(device);
|
2015-09-30 02:48:00 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MulticastDNSDeviceProvider::UpdateDevice(const uint32_t aIndex,
|
|
|
|
const nsACString& aServiceName,
|
|
|
|
const nsACString& aServiceType,
|
2015-10-21 03:54:00 +03:00
|
|
|
const nsACString& aAddress,
|
2015-09-30 02:48:00 +03:00
|
|
|
const uint16_t aPort)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-05-20 09:53:30 +03:00
|
|
|
MOZ_ASSERT(mPresentationService);
|
2015-09-30 02:48:00 +03:00
|
|
|
|
|
|
|
if (NS_WARN_IF(aIndex >= mDevices.Length())) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2015-10-19 05:44:59 +03:00
|
|
|
RefPtr<Device> device = mDevices[aIndex];
|
2015-10-21 03:54:00 +03:00
|
|
|
device->Update(aServiceName, aServiceType, aAddress, aPort);
|
2015-10-19 05:44:59 +03:00
|
|
|
device->ChangeState(DeviceState::eActive);
|
2015-09-30 02:48:00 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPresentationDeviceListener> listener;
|
|
|
|
if (NS_SUCCEEDED(GetListener(getter_AddRefs(listener))) && listener) {
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << listener->UpdateDevice(device);
|
2015-09-30 02:48:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MulticastDNSDeviceProvider::RemoveDevice(const uint32_t aIndex)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-05-20 09:53:30 +03:00
|
|
|
MOZ_ASSERT(mPresentationService);
|
2015-09-30 02:48:00 +03:00
|
|
|
|
|
|
|
if (NS_WARN_IF(aIndex >= mDevices.Length())) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2015-10-19 05:44:59 +03:00
|
|
|
RefPtr<Device> device = mDevices[aIndex];
|
2015-09-30 02:48:00 +03:00
|
|
|
|
2015-10-19 05:44:59 +03:00
|
|
|
LOG_I("RemoveDevice: %s", device->Id().get());
|
|
|
|
mDevices.RemoveElementAt(aIndex);
|
2015-09-30 02:48:00 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPresentationDeviceListener> listener;
|
|
|
|
if (NS_SUCCEEDED(GetListener(getter_AddRefs(listener))) && listener) {
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << listener->RemoveDevice(device);
|
2015-09-30 02:48:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-10-19 05:44:59 +03:00
|
|
|
MulticastDNSDeviceProvider::FindDeviceById(const nsACString& aId,
|
|
|
|
uint32_t& aIndex)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
RefPtr<Device> device = new Device(aId,
|
|
|
|
/* aName = */ EmptyCString(),
|
|
|
|
/* aType = */ EmptyCString(),
|
|
|
|
/* aHost = */ EmptyCString(),
|
|
|
|
/* aPort = */ 0,
|
|
|
|
/* aState = */ DeviceState::eUnknown,
|
|
|
|
/* aProvider = */ nullptr);
|
|
|
|
size_t index = mDevices.IndexOf(device, 0, DeviceIdComparator());
|
|
|
|
|
|
|
|
if (index == mDevices.NoIndex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aIndex = index;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-10-21 03:54:00 +03:00
|
|
|
MulticastDNSDeviceProvider::FindDeviceByAddress(const nsACString& aAddress,
|
|
|
|
uint32_t& aIndex)
|
2015-09-30 02:48:00 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-10-19 05:44:59 +03:00
|
|
|
RefPtr<Device> device = new Device(/* aId = */ EmptyCString(),
|
|
|
|
/* aName = */ EmptyCString(),
|
|
|
|
/* aType = */ EmptyCString(),
|
2015-10-21 03:54:00 +03:00
|
|
|
aAddress,
|
2015-10-19 05:44:59 +03:00
|
|
|
/* aPort = */ 0,
|
|
|
|
/* aState = */ DeviceState::eUnknown,
|
|
|
|
/* aProvider = */ nullptr);
|
2015-10-21 03:54:00 +03:00
|
|
|
size_t index = mDevices.IndexOf(device, 0, DeviceAddressComparator());
|
2015-09-30 02:48:00 +03:00
|
|
|
|
|
|
|
if (index == mDevices.NoIndex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aIndex = index;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MulticastDNSDeviceProvider::MarkAllDevicesUnknown()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
for (auto& device : mDevices) {
|
2015-10-19 05:44:59 +03:00
|
|
|
device->ChangeState(DeviceState::eUnknown);
|
2015-09-30 02:48:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MulticastDNSDeviceProvider::ClearUnknownDevices()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
size_t i = mDevices.Length();
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
2015-10-19 05:44:59 +03:00
|
|
|
if (mDevices[i]->State() == DeviceState::eUnknown) {
|
2015-09-30 02:48:00 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(RemoveDevice(i)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MulticastDNSDeviceProvider::ClearDevices()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
size_t i = mDevices.Length();
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
|
|
|
NS_WARN_IF(NS_FAILED(RemoveDevice(i)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
// nsIPresentationDeviceProvider
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MulticastDNSDeviceProvider::GetListener(nsIPresentationDeviceListener** aListener)
|
|
|
|
{
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
if (NS_WARN_IF(!aListener)) {
|
|
|
|
return NS_ERROR_INVALID_POINTER;
|
|
|
|
}
|
|
|
|
|
2015-09-30 02:48:00 +03:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIPresentationDeviceListener> listener =
|
|
|
|
do_QueryReferent(mDeviceListener, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
listener.forget(aListener);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MulticastDNSDeviceProvider::SetListener(nsIPresentationDeviceListener* aListener)
|
|
|
|
{
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
mDeviceListener = do_GetWeakReference(aListener);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
if (mDeviceListener) {
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = Init()))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = Uninit()))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MulticastDNSDeviceProvider::ForceDiscovery()
|
|
|
|
{
|
2015-07-30 19:40:00 +03:00
|
|
|
LOG_I("ForceDiscovery (%d)", mDiscoveryEnabled);
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-07-30 19:40:00 +03:00
|
|
|
|
|
|
|
if (!mDiscoveryEnabled) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-30 02:48:00 +03:00
|
|
|
MOZ_ASSERT(mDiscoveryTimer);
|
2015-05-21 06:06:00 +03:00
|
|
|
MOZ_ASSERT(mMulticastDNS);
|
|
|
|
|
2015-09-30 02:48:00 +03:00
|
|
|
// if it's already discovering, extend existing discovery timeout.
|
2015-12-10 09:56:00 +03:00
|
|
|
nsresult rv;
|
2015-09-30 02:48:00 +03:00
|
|
|
if (mIsDiscovering) {
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << mDiscoveryTimer->Cancel();
|
2015-09-30 02:48:00 +03:00
|
|
|
|
2015-12-10 09:56:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED( rv = mDiscoveryTimer->Init(this,
|
2016-06-01 04:45:16 +03:00
|
|
|
mDiscoveryTimeoutMs,
|
2015-12-10 09:56:00 +03:00
|
|
|
nsITimer::TYPE_ONE_SHOT)))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2015-09-30 02:48:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-30 19:40:00 +03:00
|
|
|
StopDiscovery(NS_OK);
|
2015-05-21 06:06:00 +03:00
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = mMulticastDNS->StartDiscovery(
|
|
|
|
NS_LITERAL_CSTRING(SERVICE_TYPE),
|
|
|
|
mWrappedListener,
|
|
|
|
getter_AddRefs(mDiscoveryRequest))))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIDNSServiceDiscoveryListener
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MulticastDNSDeviceProvider::OnDiscoveryStarted(const nsACString& aServiceType)
|
|
|
|
{
|
|
|
|
LOG_I("OnDiscoveryStarted");
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-01 00:55:00 +03:00
|
|
|
MOZ_ASSERT(mDiscoveryTimer);
|
|
|
|
|
2015-09-30 02:48:00 +03:00
|
|
|
MarkAllDevicesUnknown();
|
|
|
|
|
2015-10-01 00:55:00 +03:00
|
|
|
nsresult rv;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = mDiscoveryTimer->Init(this,
|
2016-06-01 04:45:16 +03:00
|
|
|
mDiscoveryTimeoutMs,
|
2015-10-01 00:55:00 +03:00
|
|
|
nsITimer::TYPE_ONE_SHOT)))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2015-09-09 00:42:00 +03:00
|
|
|
|
2015-09-30 02:48:00 +03:00
|
|
|
mIsDiscovering = true;
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MulticastDNSDeviceProvider::OnDiscoveryStopped(const nsACString& aServiceType)
|
|
|
|
{
|
|
|
|
LOG_I("OnDiscoveryStopped");
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-09-30 02:48:00 +03:00
|
|
|
ClearUnknownDevices();
|
|
|
|
|
|
|
|
mIsDiscovering = false;
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MulticastDNSDeviceProvider::OnServiceFound(nsIDNSServiceInfo* aServiceInfo)
|
|
|
|
{
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
if (NS_WARN_IF(!aServiceInfo)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv ;
|
|
|
|
|
|
|
|
nsAutoCString serviceName;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = aServiceInfo->GetServiceName(serviceName)))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_I("OnServiceFound: %s", serviceName.get());
|
|
|
|
|
|
|
|
if (mMulticastDNS) {
|
2015-07-30 19:40:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = mMulticastDNS->ResolveService(
|
|
|
|
aServiceInfo, mWrappedListener)))) {
|
2015-05-21 06:06:00 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MulticastDNSDeviceProvider::OnServiceLost(nsIDNSServiceInfo* aServiceInfo)
|
|
|
|
{
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
if (NS_WARN_IF(!aServiceInfo)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsAutoCString serviceName;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = aServiceInfo->GetServiceName(serviceName)))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_I("OnServiceLost: %s", serviceName.get());
|
|
|
|
|
2015-09-30 02:48:00 +03:00
|
|
|
nsAutoCString host;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = aServiceInfo->GetHost(host)))) {
|
|
|
|
return rv;
|
2015-05-21 06:06:00 +03:00
|
|
|
}
|
|
|
|
|
2015-09-30 02:48:00 +03:00
|
|
|
uint32_t index;
|
2015-10-19 05:44:59 +03:00
|
|
|
if (!FindDeviceById(host, index)) {
|
2015-09-30 02:48:00 +03:00
|
|
|
// given device was not found
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-05-21 06:06:00 +03:00
|
|
|
|
2015-09-30 02:48:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = RemoveDevice(index)))) {
|
|
|
|
return rv;
|
2015-05-21 06:06:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-07-30 19:40:00 +03:00
|
|
|
MulticastDNSDeviceProvider::OnStartDiscoveryFailed(const nsACString& aServiceType,
|
|
|
|
int32_t aErrorCode)
|
2015-05-21 06:06:00 +03:00
|
|
|
{
|
|
|
|
LOG_E("OnStartDiscoveryFailed: %d", aErrorCode);
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-07-30 19:40:00 +03:00
|
|
|
MulticastDNSDeviceProvider::OnStopDiscoveryFailed(const nsACString& aServiceType,
|
|
|
|
int32_t aErrorCode)
|
2015-05-21 06:06:00 +03:00
|
|
|
{
|
|
|
|
LOG_E("OnStopDiscoveryFailed: %d", aErrorCode);
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIDNSRegistrationListener
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MulticastDNSDeviceProvider::OnServiceRegistered(nsIDNSServiceInfo* aServiceInfo)
|
|
|
|
{
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
if (NS_WARN_IF(!aServiceInfo)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsAutoCString name;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = aServiceInfo->GetServiceName(name)))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_I("OnServiceRegistered (%s)", name.get());
|
|
|
|
mRegisteredName = name;
|
|
|
|
|
2015-10-21 08:09:00 +03:00
|
|
|
if (mMulticastDNS) {
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = mMulticastDNS->ResolveService(
|
|
|
|
aServiceInfo, mWrappedListener)))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2015-05-21 06:06:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MulticastDNSDeviceProvider::OnServiceUnregistered(nsIDNSServiceInfo* aServiceInfo)
|
|
|
|
{
|
|
|
|
LOG_I("OnServiceUnregistered");
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-07-30 19:40:00 +03:00
|
|
|
MulticastDNSDeviceProvider::OnRegistrationFailed(nsIDNSServiceInfo* aServiceInfo,
|
|
|
|
int32_t aErrorCode)
|
2015-05-21 06:06:00 +03:00
|
|
|
{
|
|
|
|
LOG_E("OnRegistrationFailed: %d", aErrorCode);
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-05-21 06:06:00 +03:00
|
|
|
|
2015-07-30 19:40:00 +03:00
|
|
|
mRegisterRequest = nullptr;
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
if (aErrorCode == nsIDNSRegistrationListener::ERROR_SERVICE_NOT_RUNNING) {
|
2015-11-03 03:04:00 +03:00
|
|
|
return NS_DispatchToMainThread(
|
2016-05-05 11:45:00 +03:00
|
|
|
NewRunnableMethod(this, &MulticastDNSDeviceProvider::RegisterService));
|
2015-05-21 06:06:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-07-30 19:40:00 +03:00
|
|
|
MulticastDNSDeviceProvider::OnUnregistrationFailed(nsIDNSServiceInfo* aServiceInfo,
|
|
|
|
int32_t aErrorCode)
|
2015-05-21 06:06:00 +03:00
|
|
|
{
|
|
|
|
LOG_E("OnUnregistrationFailed: %d", aErrorCode);
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIDNSServiceResolveListener
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MulticastDNSDeviceProvider::OnServiceResolved(nsIDNSServiceInfo* aServiceInfo)
|
|
|
|
{
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
if (NS_WARN_IF(!aServiceInfo)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsAutoCString serviceName;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = aServiceInfo->GetServiceName(serviceName)))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_I("OnServiceResolved: %s", serviceName.get());
|
|
|
|
|
|
|
|
nsAutoCString host;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = aServiceInfo->GetHost(host)))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-10-21 08:09:00 +03:00
|
|
|
if (mRegisteredName == serviceName) {
|
|
|
|
LOG_I("ignore self");
|
|
|
|
|
2016-05-20 09:53:30 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = mPresentationService->SetId(host)))) {
|
2015-10-21 08:09:00 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-05-27 02:22:27 +03:00
|
|
|
if (!IsCompatibleServer(aServiceInfo)) {
|
|
|
|
LOG_I("ignore incompatible service: %s", serviceName.get());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-21 03:54:00 +03:00
|
|
|
nsAutoCString address;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = aServiceInfo->GetAddress(address)))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
uint16_t port;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = aServiceInfo->GetPort(&port)))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString serviceType;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = aServiceInfo->GetServiceType(serviceType)))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-09-30 02:48:00 +03:00
|
|
|
uint32_t index;
|
2015-10-19 05:44:59 +03:00
|
|
|
if (FindDeviceById(host, index)) {
|
2015-09-30 02:48:00 +03:00
|
|
|
return UpdateDevice(index,
|
|
|
|
serviceName,
|
|
|
|
serviceType,
|
2015-10-21 03:54:00 +03:00
|
|
|
address,
|
2015-09-30 02:48:00 +03:00
|
|
|
port);
|
|
|
|
} else {
|
2015-10-21 03:54:00 +03:00
|
|
|
return AddDevice(host,
|
|
|
|
serviceName,
|
2015-09-30 02:48:00 +03:00
|
|
|
serviceType,
|
2015-10-21 03:54:00 +03:00
|
|
|
address,
|
2015-09-30 02:48:00 +03:00
|
|
|
port);
|
2015-05-21 06:06:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-07-30 19:40:00 +03:00
|
|
|
MulticastDNSDeviceProvider::OnResolveFailed(nsIDNSServiceInfo* aServiceInfo,
|
|
|
|
int32_t aErrorCode)
|
2015-05-21 06:06:00 +03:00
|
|
|
{
|
|
|
|
LOG_E("OnResolveFailed: %d", aErrorCode);
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-05-20 09:53:30 +03:00
|
|
|
// nsIPresentationControlServerListener
|
2015-05-21 06:06:00 +03:00
|
|
|
NS_IMETHODIMP
|
2015-11-03 03:04:00 +03:00
|
|
|
MulticastDNSDeviceProvider::OnPortChange(uint16_t aPort)
|
2015-05-21 06:06:00 +03:00
|
|
|
{
|
2015-11-03 03:04:00 +03:00
|
|
|
LOG_I("OnPortChange: %d", aPort);
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-05-21 06:06:00 +03:00
|
|
|
|
2015-11-03 03:04:00 +03:00
|
|
|
if (mDiscoverable) {
|
|
|
|
RegisterService();
|
2015-07-30 19:40:00 +03:00
|
|
|
}
|
2015-05-21 06:06:00 +03:00
|
|
|
|
2015-10-19 05:44:59 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MulticastDNSDeviceProvider::OnSessionRequest(nsITCPDeviceInfo* aDeviceInfo,
|
|
|
|
const nsAString& aUrl,
|
|
|
|
const nsAString& aPresentationId,
|
|
|
|
nsIPresentationControlChannel* aControlChannel)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-10-21 03:54:00 +03:00
|
|
|
nsAutoCString address;
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << aDeviceInfo->GetAddress(address);
|
2015-10-19 05:44:59 +03:00
|
|
|
|
2015-10-21 03:54:00 +03:00
|
|
|
LOG_I("OnSessionRequest: %s", address.get());
|
2015-10-19 05:44:59 +03:00
|
|
|
|
|
|
|
RefPtr<Device> device;
|
|
|
|
uint32_t index;
|
2015-10-21 03:54:00 +03:00
|
|
|
if (FindDeviceByAddress(address, index)) {
|
2015-10-19 05:44:59 +03:00
|
|
|
device = mDevices[index];
|
|
|
|
} else {
|
|
|
|
// create a one-time device object for non-discoverable controller
|
|
|
|
// this device will not be listed in available device list and cannot
|
|
|
|
// be used for requesting session.
|
|
|
|
nsAutoCString id;
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << aDeviceInfo->GetId(id);
|
2015-10-19 05:44:59 +03:00
|
|
|
uint16_t port;
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << aDeviceInfo->GetPort(&port);
|
2015-10-19 05:44:59 +03:00
|
|
|
|
|
|
|
device = new Device(id,
|
|
|
|
/* aName = */ id,
|
|
|
|
/* aType = */ EmptyCString(),
|
2015-10-21 03:54:00 +03:00
|
|
|
address,
|
2015-10-19 05:44:59 +03:00
|
|
|
port,
|
|
|
|
DeviceState::eActive,
|
|
|
|
/* aProvider = */ nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresentationDeviceListener> listener;
|
|
|
|
if (NS_SUCCEEDED(GetListener(getter_AddRefs(listener))) && listener) {
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << listener->OnSessionRequest(device, aUrl, aPresentationId,
|
2015-10-19 05:44:59 +03:00
|
|
|
aControlChannel);
|
2015-07-30 19:40:00 +03:00
|
|
|
}
|
2015-05-21 06:06:00 +03:00
|
|
|
|
2015-07-30 19:40:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIObserver
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MulticastDNSDeviceProvider::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const char16_t* aData)
|
|
|
|
{
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-07-30 19:40:00 +03:00
|
|
|
NS_ConvertUTF16toUTF8 data(aData);
|
|
|
|
LOG_I("Observe: topic = %s, data = %s", aTopic, data.get());
|
|
|
|
|
|
|
|
if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
|
|
|
|
if (data.EqualsLiteral(PREF_PRESENTATION_DISCOVERY)) {
|
|
|
|
OnDiscoveryChanged(Preferences::GetBool(PREF_PRESENTATION_DISCOVERY));
|
2015-10-01 00:55:00 +03:00
|
|
|
} else if (data.EqualsLiteral(PREF_PRESENTATION_DISCOVERY_TIMEOUT_MS)) {
|
|
|
|
OnDiscoveryTimeoutChanged(Preferences::GetUint(PREF_PRESENTATION_DISCOVERY_TIMEOUT_MS));
|
2015-07-30 19:40:00 +03:00
|
|
|
} else if (data.EqualsLiteral(PREF_PRESENTATION_DISCOVERABLE)) {
|
|
|
|
OnDiscoverableChanged(Preferences::GetBool(PREF_PRESENTATION_DISCOVERABLE));
|
|
|
|
} else if (data.EqualsLiteral(PREF_PRESENTATION_DEVICE_NAME)) {
|
2015-10-20 11:47:56 +03:00
|
|
|
nsAdoptingCString newServiceName = Preferences::GetCString(PREF_PRESENTATION_DEVICE_NAME);
|
|
|
|
if (!mServiceName.Equals(newServiceName)) {
|
|
|
|
OnServiceNameChanged(newServiceName);
|
|
|
|
}
|
2015-05-21 06:06:00 +03:00
|
|
|
}
|
2015-10-01 00:55:00 +03:00
|
|
|
} else if (!strcmp(aTopic, NS_TIMER_CALLBACK_TOPIC)) {
|
|
|
|
StopDiscovery(NS_OK);
|
2015-05-21 06:06:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-30 19:40:00 +03:00
|
|
|
nsresult
|
|
|
|
MulticastDNSDeviceProvider::OnDiscoveryChanged(bool aEnabled)
|
|
|
|
{
|
|
|
|
LOG_I("DiscoveryEnabled = %d\n", aEnabled);
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-07-30 19:40:00 +03:00
|
|
|
|
|
|
|
mDiscoveryEnabled = aEnabled;
|
|
|
|
|
|
|
|
if (mDiscoveryEnabled) {
|
|
|
|
return ForceDiscovery();
|
|
|
|
}
|
|
|
|
|
|
|
|
return StopDiscovery(NS_OK);
|
|
|
|
}
|
|
|
|
|
2015-10-01 00:55:00 +03:00
|
|
|
nsresult
|
|
|
|
MulticastDNSDeviceProvider::OnDiscoveryTimeoutChanged(uint32_t aTimeoutMs)
|
|
|
|
{
|
|
|
|
LOG_I("OnDiscoveryTimeoutChanged = %d\n", aTimeoutMs);
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2016-06-01 04:45:16 +03:00
|
|
|
mDiscoveryTimeoutMs = aTimeoutMs;
|
2015-10-01 00:55:00 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-30 19:40:00 +03:00
|
|
|
nsresult
|
|
|
|
MulticastDNSDeviceProvider::OnDiscoverableChanged(bool aEnabled)
|
|
|
|
{
|
|
|
|
LOG_I("Discoverable = %d\n", aEnabled);
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-07-30 19:40:00 +03:00
|
|
|
|
|
|
|
mDiscoverable = aEnabled;
|
|
|
|
|
|
|
|
if (mDiscoverable) {
|
|
|
|
return RegisterService();
|
|
|
|
}
|
|
|
|
|
|
|
|
return UnregisterService(NS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-09-30 02:48:00 +03:00
|
|
|
MulticastDNSDeviceProvider::OnServiceNameChanged(const nsACString& aServiceName)
|
2015-07-30 19:40:00 +03:00
|
|
|
{
|
2015-09-30 02:48:00 +03:00
|
|
|
LOG_I("serviceName = %s\n", PromiseFlatCString(aServiceName).get());
|
2015-09-09 00:42:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-07-30 19:40:00 +03:00
|
|
|
|
|
|
|
mServiceName = aServiceName;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv = UnregisterService(NS_OK)))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mDiscoverable) {
|
|
|
|
return RegisterService();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-19 05:44:59 +03:00
|
|
|
// MulticastDNSDeviceProvider::Device
|
|
|
|
NS_IMPL_ISUPPORTS(MulticastDNSDeviceProvider::Device,
|
|
|
|
nsIPresentationDevice)
|
|
|
|
|
|
|
|
// nsIPresentationDevice
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MulticastDNSDeviceProvider::Device::GetId(nsACString& aId)
|
|
|
|
{
|
|
|
|
aId = mId;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MulticastDNSDeviceProvider::Device::GetName(nsACString& aName)
|
|
|
|
{
|
|
|
|
aName = mName;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MulticastDNSDeviceProvider::Device::GetType(nsACString& aType)
|
|
|
|
{
|
|
|
|
aType = mType;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-07-04 13:12:04 +03:00
|
|
|
MulticastDNSDeviceProvider::Device::EstablishControlChannel(
|
|
|
|
nsIPresentationControlChannel** aRetVal)
|
2015-10-19 05:44:59 +03:00
|
|
|
{
|
|
|
|
if (!mProvider) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-07-04 13:12:04 +03:00
|
|
|
return mProvider->Connect(this, aRetVal);
|
2015-10-19 05:44:59 +03:00
|
|
|
}
|
|
|
|
|
2016-04-28 10:05:16 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
MulticastDNSDeviceProvider::Device::Disconnect()
|
|
|
|
{
|
2016-04-28 10:05:25 +03:00
|
|
|
// No need to do anything when disconnect.
|
2016-04-28 10:05:16 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-21 06:06:00 +03:00
|
|
|
} // namespace presentation
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|