Bug 1194049 - Part 2: add discovery timeout support; r=schien

This commit is contained in:
Liang-Heng Chen 2015-09-30 23:55:00 +02:00
parent 2437eeb9b2
commit 63cdeef8d2
3 changed files with 43 additions and 1 deletions

View File

@ -8,6 +8,7 @@
#include "mozilla/Logging.h"
#include "mozilla/Preferences.h"
#include "mozilla/Services.h"
#include "mozilla/unused.h"
#include "nsAutoPtr.h"
#include "nsComponentManagerUtils.h"
#include "nsIObserverService.h"
@ -15,6 +16,7 @@
#include "nsServiceManagerUtils.h"
#define PREF_PRESENTATION_DISCOVERY "dom.presentation.discovery.enabled"
#define PREF_PRESENTATION_DISCOVERY_TIMEOUT_MS "dom.presentation.discovery.timeout_ms"
#define PREF_PRESENTATION_DISCOVERABLE "dom.presentation.discoverable"
#define PREF_PRESENTATION_DEVICE_NAME "dom.presentation.device.name"
@ -40,6 +42,7 @@ namespace presentation {
static const char* kObservedPrefs[] = {
PREF_PRESENTATION_DISCOVERY,
PREF_PRESENTATION_DISCOVERY_TIMEOUT_MS,
PREF_PRESENTATION_DISCOVERABLE,
PREF_PRESENTATION_DEVICE_NAME,
nullptr
@ -123,9 +126,15 @@ MulticastDNSDeviceProvider::Init()
return rv;
}
mDiscoveryTimer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
Preferences::AddStrongObservers(this, kObservedPrefs);
mDiscoveryEnabled = Preferences::GetBool(PREF_PRESENTATION_DISCOVERY);
mDiscveryTimeoutMs = Preferences::GetUint(PREF_PRESENTATION_DISCOVERY_TIMEOUT_MS);
mDiscoverable = Preferences::GetBool(PREF_PRESENTATION_DISCOVERABLE);
mServiceName = Preferences::GetCString(PREF_PRESENTATION_DEVICE_NAME);
@ -236,6 +245,9 @@ nsresult
MulticastDNSDeviceProvider::StopDiscovery(nsresult aReason)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mDiscoveryTimer);
unused << mDiscoveryTimer->Cancel();
if (mDiscoveryRequest) {
mDiscoveryRequest->Cancel(aReason);
@ -313,6 +325,14 @@ MulticastDNSDeviceProvider::OnDiscoveryStarted(const nsACString& aServiceType)
{
LOG_I("OnDiscoveryStarted");
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mDiscoveryTimer);
nsresult rv;
if (NS_WARN_IF(NS_FAILED(rv = mDiscoveryTimer->Init(this,
mDiscveryTimeoutMs,
nsITimer::TYPE_ONE_SHOT)))) {
return rv;
}
return NS_OK;
}
@ -594,11 +614,15 @@ MulticastDNSDeviceProvider::Observe(nsISupports* aSubject,
if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
if (data.EqualsLiteral(PREF_PRESENTATION_DISCOVERY)) {
OnDiscoveryChanged(Preferences::GetBool(PREF_PRESENTATION_DISCOVERY));
} else if (data.EqualsLiteral(PREF_PRESENTATION_DISCOVERY_TIMEOUT_MS)) {
OnDiscoveryTimeoutChanged(Preferences::GetUint(PREF_PRESENTATION_DISCOVERY_TIMEOUT_MS));
} else if (data.EqualsLiteral(PREF_PRESENTATION_DISCOVERABLE)) {
OnDiscoverableChanged(Preferences::GetBool(PREF_PRESENTATION_DISCOVERABLE));
} else if (data.EqualsLiteral(PREF_PRESENTATION_DEVICE_NAME)) {
OnServiceNameChanged(Preferences::GetCString(PREF_PRESENTATION_DEVICE_NAME));
}
} else if (!strcmp(aTopic, NS_TIMER_CALLBACK_TOPIC)) {
StopDiscovery(NS_OK);
}
return NS_OK;
@ -619,6 +643,17 @@ MulticastDNSDeviceProvider::OnDiscoveryChanged(bool aEnabled)
return StopDiscovery(NS_OK);
}
nsresult
MulticastDNSDeviceProvider::OnDiscoveryTimeoutChanged(uint32_t aTimeoutMs)
{
LOG_I("OnDiscoveryTimeoutChanged = %d\n", aTimeoutMs);
MOZ_ASSERT(NS_IsMainThread());
mDiscveryTimeoutMs = aTimeoutMs;
return NS_OK;
}
nsresult
MulticastDNSDeviceProvider::OnDiscoverableChanged(bool aEnabled)
{

View File

@ -6,13 +6,14 @@
#ifndef mozilla_dom_presentation_provider_MulticastDNSDeviceProvider_h
#define mozilla_dom_presentation_provider_MulticastDNSDeviceProvider_h
#include "mozilla/nsRefPtr.h"
#include "nsCOMPtr.h"
#include "nsICancelable.h"
#include "nsIDNSServiceDiscovery.h"
#include "nsIObserver.h"
#include "nsIPresentationDeviceProvider.h"
#include "nsITCPPresentationServer.h"
#include "mozilla/nsRefPtr.h"
#include "nsITimer.h"
#include "nsString.h"
#include "nsWeakPtr.h"
@ -51,6 +52,7 @@ private:
nsresult StopDiscovery(nsresult aReason);
nsresult OnDiscoveryChanged(bool aEnabled);
nsresult OnDiscoveryTimeoutChanged(uint32_t aTimeoutMs);
nsresult OnDiscoverableChanged(bool aEnabled);
nsresult OnServiceNameChanged(const nsCString& aServiceName);
@ -64,7 +66,11 @@ private:
nsCOMPtr<nsICancelable> mRegisterRequest;
bool mDiscoveryEnabled = false;
uint32_t mDiscveryTimeoutMs;
nsCOMPtr<nsITimer> mDiscoveryTimer;
bool mDiscoverable = false;
nsCString mServiceName;
nsCString mRegisteredName;
};

View File

@ -4896,6 +4896,7 @@ pref("dom.beforeAfterKeyboardEvent.enabled", false);
pref("dom.presentation.enabled", false);
pref("dom.presentation.tcp_server.debug", false);
pref("dom.presentation.discovery.enabled", true);
pref("dom.presentation.discovery.timeout_ms", 10000);
pref("dom.presentation.discoverable", false);
#ifdef XP_MACOSX