netwerk/dns/DNSListenerProxy.h
author Mozilla Releng Treescript <release+treescript@mozilla.org>
Mon, 30 Mar 2020 09:14:12 +0000
changeset 580725 aae70d505c4e193448a8934ee952bfb6174cdff5
parent 505471 66eb1f485c1a3ea81372758bc92292c9428b17cd
permissions -rw-r--r--
"No bug - tagging src with FIREFOX_RELEASE_75_BASE a=release DONTBUILD CLOSED TREE"

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=8 et tw=80 : */
/* 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 DNSListenerProxy_h__
#define DNSListenerProxy_h__

#include "nsIDNSListener.h"
#include "nsIDNSRecord.h"
#include "nsProxyRelease.h"
#include "nsThreadUtils.h"

class nsIEventTarget;
class nsICancelable;

namespace mozilla {
namespace net {

class DNSListenerProxy final : public nsIDNSListener,
                               public nsIDNSListenerProxy {
 public:
  DNSListenerProxy(nsIDNSListener* aListener, nsIEventTarget* aTargetThread)
      // Sometimes aListener is a main-thread only object like XPCWrappedJS, and
      // sometimes it's a threadsafe object like nsSOCKSSocketInfo. Use a main-
      // thread pointer holder, but disable strict enforcement of thread
      // invariants. The AddRef implementation of XPCWrappedJS will assert if we
      // go wrong here.
      : mListener(new nsMainThreadPtrHolder<nsIDNSListener>(
            "DNSListenerProxy::mListener", aListener, false)),
        mTargetThread(aTargetThread) {}

  NS_DECL_THREADSAFE_ISUPPORTS
  NS_DECL_NSIDNSLISTENER
  NS_DECL_NSIDNSLISTENERPROXY

  class OnLookupCompleteRunnable : public Runnable {
   public:
    OnLookupCompleteRunnable(
        const nsMainThreadPtrHandle<nsIDNSListener>& aListener,
        nsICancelable* aRequest, nsIDNSRecord* aRecord, nsresult aStatus)
        : Runnable("DNSListenerProxy::OnLookupCompleteRunnable"),
          mListener(aListener),
          mRequest(aRequest),
          mRecord(aRecord),
          mStatus(aStatus) {}

    NS_DECL_NSIRUNNABLE

   private:
    nsMainThreadPtrHandle<nsIDNSListener> mListener;
    nsCOMPtr<nsICancelable> mRequest;
    nsCOMPtr<nsIDNSRecord> mRecord;
    nsresult mStatus;
  };

  class OnLookupByTypeCompleteRunnable : public Runnable {
   public:
    OnLookupByTypeCompleteRunnable(
        const nsMainThreadPtrHandle<nsIDNSListener>& aListener,
        nsICancelable* aRequest, nsIDNSByTypeRecord* aRes, nsresult aStatus)
        : Runnable("DNSListenerProxy::OnLookupByTypeCompleteRunnable"),
          mListener(aListener),
          mRequest(aRequest),
          mResult(aRes),
          mStatus(aStatus) {}

    NS_DECL_NSIRUNNABLE

   private:
    nsMainThreadPtrHandle<nsIDNSListener> mListener;
    nsCOMPtr<nsICancelable> mRequest;
    nsCOMPtr<nsIDNSByTypeRecord> mResult;
    nsresult mStatus;
  };

 private:
  ~DNSListenerProxy() {}

  nsMainThreadPtrHandle<nsIDNSListener> mListener;
  nsCOMPtr<nsIEventTarget> mTargetThread;
};

}  // namespace net
}  // namespace mozilla

#endif  // DNSListenerProxy_h__