Bug 1554976 - Add methods to register/unregister mDNS hostnames to StunAddrsRequestParent; r=mjf
authorDan Minor <dminor@mozilla.com>
Wed, 28 Aug 2019 19:49:12 +0000
changeset 551121 95f34f8de2c4514e366c5e5ffa33f029864a9a83
parent 551120 e70a875ead243db2c7e56b006591c0f8924798e7
child 551122 3356f4e4828e6b171b67686bcebaf2c83b5a6fac
push id11865
push userbtara@mozilla.com
push dateMon, 02 Sep 2019 08:54:37 +0000
treeherdermozilla-beta@37f59c4671b3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmjf
bugs1554976
milestone70.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1554976 - Add methods to register/unregister mDNS hostnames to StunAddrsRequestParent; r=mjf Differential Revision: https://phabricator.services.mozilla.com/D38492
media/mtransport/ipc/PStunAddrsRequest.ipdl
media/mtransport/ipc/StunAddrsRequestParent.cpp
media/mtransport/ipc/StunAddrsRequestParent.h
--- a/media/mtransport/ipc/PStunAddrsRequest.ipdl
+++ b/media/mtransport/ipc/PStunAddrsRequest.ipdl
@@ -13,16 +13,19 @@ namespace net {
 
 async protocol PStunAddrsRequest
 {
   manager PNecko;
 
 parent:
   async GetStunAddrs();
 
+  async RegisterMDNSHostname(nsCString hostname, nsCString address);
+  async UnregisterMDNSHostname(nsCString hostname);
+
   async __delete__();
 
 child:
   async OnStunAddrsAvailable(NrIceStunAddrArray iceStunAddrs);
 };
 
 } // namespace net
 } // namespace mozilla
--- a/media/mtransport/ipc/StunAddrsRequestParent.cpp
+++ b/media/mtransport/ipc/StunAddrsRequestParent.cpp
@@ -6,44 +6,89 @@
 
 #include "../runnable_utils.h"
 #include "nsNetUtil.h"
 
 #include "mtransport/nricectx.h"
 #include "mtransport/nricemediastream.h"  // needed only for including nricectx.h
 #include "mtransport/nricestunaddr.h"
 
+#include "../mdns_service/mdns_service.h"
+
 using namespace mozilla::ipc;
 
 namespace mozilla {
 namespace net {
 
-StunAddrsRequestParent::StunAddrsRequestParent() : mIPCClosed(false) {
+StunAddrsRequestParent::StunAddrsRequestParent()
+    : mIPCClosed(false), mMDNSService(nullptr) {
   NS_GetMainThread(getter_AddRefs(mMainThread));
 
   nsresult res;
   mSTSThread = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &res);
   MOZ_ASSERT(mSTSThread);
 }
 
+StunAddrsRequestParent::~StunAddrsRequestParent() {
+  if (mMDNSService) {
+    mdns_service_stop(mMDNSService);
+    mMDNSService = nullptr;
+  }
+}
+
 mozilla::ipc::IPCResult StunAddrsRequestParent::RecvGetStunAddrs() {
   ASSERT_ON_THREAD(mMainThread);
 
   if (mIPCClosed) {
     return IPC_OK();
   }
 
   RUN_ON_THREAD(mSTSThread,
                 WrapRunnable(RefPtr<StunAddrsRequestParent>(this),
                              &StunAddrsRequestParent::GetStunAddrs_s),
                 NS_DISPATCH_NORMAL);
 
   return IPC_OK();
 }
 
+mozilla::ipc::IPCResult StunAddrsRequestParent::RecvRegisterMDNSHostname(
+    const nsCString& aHostname, const nsCString& aAddress) {
+  ASSERT_ON_THREAD(mMainThread);
+
+  if (mIPCClosed) {
+    return IPC_OK();
+  }
+
+  if (!mMDNSService) {
+    mMDNSService = mdns_service_start();
+  }
+
+  if (mMDNSService) {
+    mdns_service_register_hostname(mMDNSService, aHostname.BeginReading(),
+                                   aAddress.BeginReading());
+  }
+
+  return IPC_OK();
+}
+
+mozilla::ipc::IPCResult StunAddrsRequestParent::RecvUnregisterMDNSHostname(
+    const nsCString& aHostname) {
+  ASSERT_ON_THREAD(mMainThread);
+
+  if (mIPCClosed) {
+    return IPC_OK();
+  }
+
+  if (mMDNSService) {
+    mdns_service_unregister_hostname(mMDNSService, aHostname.BeginReading());
+  }
+
+  return IPC_OK();
+}
+
 mozilla::ipc::IPCResult StunAddrsRequestParent::Recv__delete__() {
   // see note below in ActorDestroy
   mIPCClosed = true;
   return IPC_OK();
 }
 
 void StunAddrsRequestParent::ActorDestroy(ActorDestroyReason why) {
   // We may still have refcount>0 if we haven't made it through
@@ -74,17 +119,16 @@ void StunAddrsRequestParent::GetStunAddr
 void StunAddrsRequestParent::SendStunAddrs_m(const NrIceStunAddrArray& addrs) {
   ASSERT_ON_THREAD(mMainThread);
 
   if (mIPCClosed) {
     // nothing to do: child probably crashed
     return;
   }
 
-  mIPCClosed = true;
   // send the new addresses back to the child
   Unused << SendOnStunAddrsAvailable(addrs);
 }
 
 NS_IMPL_ADDREF(StunAddrsRequestParent)
 NS_IMPL_RELEASE(StunAddrsRequestParent)
 
 }  // namespace net
--- a/media/mtransport/ipc/StunAddrsRequestParent.h
+++ b/media/mtransport/ipc/StunAddrsRequestParent.h
@@ -2,45 +2,54 @@
  * 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_net_StunAddrsRequestParent_h
 #define mozilla_net_StunAddrsRequestParent_h
 
 #include "mozilla/net/PStunAddrsRequestParent.h"
 
+#include "nsICancelable.h"
+#include "nsIDNSServiceDiscovery.h"
+
+struct MDNSService;
+
 namespace mozilla {
 namespace net {
 
 class StunAddrsRequestParent : public PStunAddrsRequestParent {
   friend class PStunAddrsRequestParent;
 
  public:
   StunAddrsRequestParent();
 
   NS_IMETHOD_(MozExternalRefCountType) AddRef();
   NS_IMETHOD_(MozExternalRefCountType) Release();
 
   mozilla::ipc::IPCResult Recv__delete__() override;
 
  protected:
-  virtual ~StunAddrsRequestParent() {}
+  virtual ~StunAddrsRequestParent();
 
   virtual mozilla::ipc::IPCResult RecvGetStunAddrs() override;
+  virtual mozilla::ipc::IPCResult RecvRegisterMDNSHostname(const nsCString& hostname, const nsCString& address) override;
+  virtual mozilla::ipc::IPCResult RecvUnregisterMDNSHostname(const nsCString& hostname) override;
   virtual void ActorDestroy(ActorDestroyReason why) override;
 
   nsCOMPtr<nsIThread> mMainThread;
   nsCOMPtr<nsIEventTarget> mSTSThread;
 
   void GetStunAddrs_s();
   void SendStunAddrs_m(const NrIceStunAddrArray& addrs);
 
   ThreadSafeAutoRefCnt mRefCnt;
   NS_DECL_OWNINGTHREAD
 
  private:
   bool mIPCClosed;  // true if IPDL channel has been closed (child crash)
+
+  MDNSService* mMDNSService;
 };
 
 }  // namespace net
 }  // namespace mozilla
 
 #endif  // mozilla_net_StunAddrsRequestParent_h