Bug 1194049 - Part 2: add discovery timeout support; r=schien a=lizzard
☠☠ backed out by 96042c38d46c ☠ ☠
authorLiang-Heng Chen <xeonchen@mozilla.com>
Wed, 30 Sep 2015 23:55:00 +0200
changeset 606990 a59aa67a7ba675127f0fc039e25e72aa7cff3430
parent 606989 bc00f3a6a496b1ebc0a2dd6211fb4f0a2331f2fb
child 606991 dfd7ec48d4fe9e5ee3509a6d15540c9a1ffc71d1
push id93006
push userjyavenard@mozilla.com
push dateThu, 15 Oct 2015 05:15:33 +0000
treeherdertry@45ea2a01301e [default view] [failures only]
reviewersschien, lizzard
bugs1194049
milestone43.0a2
Bug 1194049 - Part 2: add discovery timeout support; r=schien a=lizzard
dom/presentation/provider/MulticastDNSDeviceProvider.cpp
dom/presentation/provider/MulticastDNSDeviceProvider.h
modules/libpref/init/all.js
--- a/dom/presentation/provider/MulticastDNSDeviceProvider.cpp
+++ b/dom/presentation/provider/MulticastDNSDeviceProvider.cpp
@@ -3,23 +3,25 @@
  * 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"
 #include "MainThreadUtils.h"
 #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"
 #include "nsIPresentationDevice.h"
 #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"
 
 #define TCP_PRESENTATION_SERVER_CONTACT_ID \
   "@mozilla.org/presentation-device/tcp-presentation-server;1"
 
 #define SERVICE_TYPE "_mozilla_papi._tcp."
 
@@ -35,16 +37,17 @@ GetProviderLog()
 #define LOG_E(...) MOZ_LOG(GetProviderLog(), mozilla::LogLevel::Error, (__VA_ARGS__))
 
 namespace mozilla {
 namespace dom {
 namespace presentation {
 
 static const char* kObservedPrefs[] = {
   PREF_PRESENTATION_DISCOVERY,
+  PREF_PRESENTATION_DISCOVERY_TIMEOUT_MS,
   PREF_PRESENTATION_DISCOVERABLE,
   PREF_PRESENTATION_DEVICE_NAME,
   nullptr
 };
 
 /**
  * This wrapper is used to break circular-reference problem.
  */
@@ -118,19 +121,25 @@ MulticastDNSDeviceProvider::Init()
     return rv;
   }
 
   mPresentationServer = do_CreateInstance(TCP_PRESENTATION_SERVER_CONTACT_ID, &rv);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     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);
 
   if (mDiscoveryEnabled && NS_WARN_IF(NS_FAILED(rv = ForceDiscovery()))) {
     return rv;
   }
 
   if (mDiscoverable && NS_WARN_IF(NS_FAILED(rv = RegisterService()))) {
@@ -231,16 +240,19 @@ MulticastDNSDeviceProvider::UnregisterSe
 
   return NS_OK;
 }
 
 nsresult
 MulticastDNSDeviceProvider::StopDiscovery(nsresult aReason)
 {
   MOZ_ASSERT(NS_IsMainThread());
+  MOZ_ASSERT(mDiscoveryTimer);
+
+  unused << mDiscoveryTimer->Cancel();
 
   if (mDiscoveryRequest) {
     mDiscoveryRequest->Cancel(aReason);
     mDiscoveryRequest = nullptr;
   }
 
   return NS_OK;
 }
@@ -308,16 +320,24 @@ MulticastDNSDeviceProvider::ForceDiscove
 }
 
 // nsIDNSServiceDiscoveryListener
 NS_IMETHODIMP
 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;
 }
 
 NS_IMETHODIMP
 MulticastDNSDeviceProvider::OnDiscoveryStopped(const nsACString& aServiceType)
 {
   LOG_I("OnDiscoveryStopped");
@@ -589,21 +609,25 @@ MulticastDNSDeviceProvider::Observe(nsIS
   MOZ_ASSERT(NS_IsMainThread());
 
   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));
+    } 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;
 }
 
 nsresult
 MulticastDNSDeviceProvider::OnDiscoveryChanged(bool aEnabled)
 {
@@ -615,16 +639,27 @@ MulticastDNSDeviceProvider::OnDiscoveryC
   if (mDiscoveryEnabled) {
     return ForceDiscovery();
   }
 
   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)
 {
   LOG_I("Discoverable = %d\n", aEnabled);
   MOZ_ASSERT(NS_IsMainThread());
 
   mDiscoverable = aEnabled;
 
   if (mDiscoverable) {
--- a/dom/presentation/provider/MulticastDNSDeviceProvider.h
+++ b/dom/presentation/provider/MulticastDNSDeviceProvider.h
@@ -1,23 +1,24 @@
 /* -*- 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/. */
 
 #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"
 
 namespace mozilla {
 namespace dom {
 namespace presentation {
 
 class DNSServiceWrappedListener;
@@ -46,30 +47,35 @@ public:
 
 private:
   virtual ~MulticastDNSDeviceProvider();
   nsresult RegisterService();
   nsresult UnregisterService(nsresult aReason);
   nsresult StopDiscovery(nsresult aReason);
 
   nsresult OnDiscoveryChanged(bool aEnabled);
+  nsresult OnDiscoveryTimeoutChanged(uint32_t aTimeoutMs);
   nsresult OnDiscoverableChanged(bool aEnabled);
   nsresult OnServiceNameChanged(const nsCString& aServiceName);
 
   bool mInitialized = false;
   nsWeakPtr mDeviceListener;
   nsCOMPtr<nsITCPPresentationServer> mPresentationServer;
   nsCOMPtr<nsIDNSServiceDiscovery> mMulticastDNS;
   nsRefPtr<DNSServiceWrappedListener> mWrappedListener;
 
   nsCOMPtr<nsICancelable> mDiscoveryRequest;
   nsCOMPtr<nsICancelable> mRegisterRequest;
 
   bool mDiscoveryEnabled = false;
+  uint32_t mDiscveryTimeoutMs;
+  nsCOMPtr<nsITimer> mDiscoveryTimer;
+
   bool mDiscoverable = false;
+
   nsCString mServiceName;
   nsCString mRegisteredName;
 };
 
 } // namespace presentation
 } // namespace dom
 } // namespace mozilla
 
--- a/modules/libpref/init/all.js
+++ b/modules/libpref/init/all.js
@@ -4882,16 +4882,17 @@ pref("dom.udpsocket.enabled", false);
 
 // Disable before keyboard events and after keyboard events by default.
 pref("dom.beforeAfterKeyboardEvent.enabled", false);
 
 // Presentation API
 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
 // Use raw ICU instead of CoreServices API in Unicode collation
 pref("intl.collation.mac.use_icu", true);
 
 // Enable NSTextInput protocol for use with IMEs that have not
 // been updated to use the NSTextInputClient protocol.