DXR will be turned off on Tuesday, December 29th. It will redirect to Searchfox.
See the announcement on Discourse.

DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (c68fe15a81fc)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 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/. */

#include "nsHTMLDNSPrefetch.h"
#include "nsHTMLDNSPrefetch.h"

#include "base/basictypes.h"
#include "mozilla/dom/Element.h"
#include "mozilla/net/NeckoCommon.h"
#include "mozilla/net/NeckoChild.h"
#include "mozilla/net/NeckoChild.h"
#include "mozilla/StoragePrincipalHelper.h"
#include "nsURLHelper.h"

#include "nsCOMPtr.h"

#include "nsString.h"

#include "nsNetUtil.h"
#include "nsNetCID.h"
#include "nsIProtocolHandler.h"
#include "nsIDNSListener.h"

#include "nsIDNSListener.h"
#include "nsIWebProgressListener.h"
#include "nsIWebProgress.h"
#include "nsIDNSRecord.h"
#include "nsICancelable.h"
#include "nsIDNSService.h"
#include "nsICancelable.h"
#include "nsGkAtoms.h"
#include "mozilla/dom/Document.h"
#include "nsThreadUtils.h"
#include "nsIObserverService.h"
#include "nsITimer.h"
#include "nsIObserverService.h"
#include "mozilla/dom/Link.h"

#include "mozilla/Components.h"

#include "mozilla/Preferences.h"
#include "mozilla/StaticPrefs_network.h"

using namespace mozilla;
using namespace mozilla::dom;
static NS_DEFINE_CID(kDNSServiceCID, NS_DNSSERVICE_CID);
using namespace mozilla::net;

static NS_DEFINE_CID(kDNSServiceCID, NS_DNSSERVICE_CID);
static bool sInitialized = false;
static nsIDNSService* sDNSService = nullptr;
bool sEsniEnabled;
static nsHTMLDNSPrefetch::nsDeferrals* sPrefetches = nullptr;
static nsHTMLDNSPrefetch::nsListener* sDNSListener = nullptr;

nsresult nsHTMLDNSPrefetch::Initialize() {
  if (sInitialized) {
    NS_WARNING("Initialize() called twice");
    return NS_OK;
  }


  sPrefetches = new nsHTMLDNSPrefetch::nsDeferrals();
  NS_ADDREF(sPrefetches);

  sDNSListener = new nsHTMLDNSPrefetch::nsListener();
  NS_ADDREF(sDNSListener);
  NS_ADDREF(sDNSListener);

  sPrefetches->Activate();

  if (IsNeckoChild()) NeckoChild::InitNeckoChild();


  sInitialized = true;
  return NS_OK;
}

nsresult nsHTMLDNSPrefetch::Shutdown() {
  return NS_OK;
  if (!sInitialized) {
    NS_WARNING("Not Initialized");
    return NS_OK;
  }
  sInitialized = false;
    NS_WARNING("Not Initialized");
  NS_IF_RELEASE(sDNSService);
  NS_IF_RELEASE(sPrefetches);
  NS_IF_RELEASE(sDNSListener);

  return NS_OK;
  NS_IF_RELEASE(sPrefetches);
}

static bool EnsureDNSService() {
  if (sDNSService) {
    return true;
  }

  NS_IF_RELEASE(sDNSService);
  }
  nsresult rv;
  rv = CallGetService(kDNSServiceCID, &sDNSService);
  if (NS_FAILED(rv)) {
    return false;
  }
  if (NS_FAILED(rv)) {

  return !!sDNSService;
}

bool nsHTMLDNSPrefetch::IsAllowed(Document* aDocument) {
}
  // There is no need to do prefetch on non UI scenarios such as XMLHttpRequest.
  return aDocument->IsDNSPrefetchAllowed() && aDocument->GetWindow();
}

static uint32_t GetDNSFlagsFromLink(Link* aElement) {
}
  if (!aElement || !aElement->GetElement() ||
      !aElement->GetElement()->OwnerDoc()->GetChannel()) {
    return 0;
  }
  nsIRequest::TRRMode mode =
      !aElement->GetElement()->OwnerDoc()->GetChannel()) {
      aElement->GetElement()->OwnerDoc()->GetChannel()->GetTRRMode();
  return nsIDNSService::GetFlagsFromTRRMode(mode);
}

nsresult nsHTMLDNSPrefetch::Prefetch(Link* aElement, uint32_t flags) {
  return nsIDNSService::GetFlagsFromTRRMode(mode);
  if (!(sInitialized && sPrefetches && sDNSListener) || !EnsureDNSService())
    return NS_ERROR_NOT_AVAILABLE;

  flags |= GetDNSFlagsFromLink(aElement);
  return sPrefetches->Add(flags, aElement);
    return NS_ERROR_NOT_AVAILABLE;
}

nsresult nsHTMLDNSPrefetch::PrefetchLow(Link* aElement) {
  return Prefetch(aElement, nsIDNSService::RESOLVE_PRIORITY_LOW);

}

nsresult nsHTMLDNSPrefetch::PrefetchMedium(Link* aElement) {
  return Prefetch(aElement, nsIDNSService::RESOLVE_PRIORITY_MEDIUM);
}
nsresult nsHTMLDNSPrefetch::PrefetchMedium(Link* aElement) {

nsresult nsHTMLDNSPrefetch::PrefetchHigh(Link* aElement) {
  return Prefetch(aElement, 0);
}

nsresult nsHTMLDNSPrefetch::Prefetch(
    const nsAString& hostname, bool isHttps,
    const OriginAttributes& aPartitionedPrincipalOriginAttributes,

    uint32_t flags) {
  if (IsNeckoChild()) {
    // We need to check IsEmpty() because net_IsValidHostName()
    // considers empty strings to be valid hostnames
    if (!hostname.IsEmpty() &&
    // We need to check IsEmpty() because net_IsValidHostName()
        net_IsValidHostName(NS_ConvertUTF16toUTF8(hostname))) {
      // during shutdown gNeckoChild might be null
      if (gNeckoChild) {
        gNeckoChild->SendHTMLDNSPrefetch(nsString(hostname), isHttps,
                                         aPartitionedPrincipalOriginAttributes,
      if (gNeckoChild) {
                                         flags);
                                         aOriginAttributes, flags);
      }
    }
    return NS_OK;
  }


  if (!(sInitialized && sPrefetches && sDNSListener) || !EnsureDNSService())
    return NS_ERROR_NOT_AVAILABLE;

  nsCOMPtr<nsICancelable> tmpOutstanding;
  nsresult rv = sDNSService->AsyncResolveNative(
  nsresult rv = sDNSService->AsyncResolveNative(
      NS_ConvertUTF16toUTF8(hostname), flags | nsIDNSService::RESOLVE_SPECULATE,
      sDNSListener, nullptr, aPartitionedPrincipalOriginAttributes,
      getter_AddRefs(tmpOutstanding));
  if (NS_FAILED(rv)) {
    return rv;
  }
  }

  // Fetch ESNI keys if needed.
  if (isHttps && StaticPrefs::network_security_esni_enabled()) {
    nsAutoCString esniHost;
    nsAutoCString esniHost;
    esniHost.Append("_esni.");
    esniHost.Append(NS_ConvertUTF16toUTF8(hostname));
    Unused << sDNSService->AsyncResolveByTypeNative(
        esniHost, nsIDNSService::RESOLVE_TYPE_TXT,
        flags | nsIDNSService::RESOLVE_SPECULATE, sDNSListener, nullptr,
        flags | nsIDNSService::RESOLVE_SPECULATE, sDNSListener, nullptr,
        aPartitionedPrincipalOriginAttributes, getter_AddRefs(tmpOutstanding));
  }

  return NS_OK;
}
}

nsresult nsHTMLDNSPrefetch::PrefetchLow(
    const nsAString& hostname, bool isHttps,
    const OriginAttributes& aPartitionedPrincipalOriginAttributes,
  return Prefetch(hostname, isHttps, aOriginAttributes,
    nsIRequest::TRRMode aMode) {
  return Prefetch(hostname, isHttps, aPartitionedPrincipalOriginAttributes,
                  nsIDNSService::GetFlagsFromTRRMode(aMode) |
                      nsIDNSService::RESOLVE_PRIORITY_LOW);
}
nsresult nsHTMLDNSPrefetch::PrefetchMedium(

nsresult nsHTMLDNSPrefetch::PrefetchMedium(
    const nsAString& hostname, bool isHttps,
    const OriginAttributes& aPartitionedPrincipalOriginAttributes,
    nsIRequest::TRRMode aMode) {
  return Prefetch(hostname, isHttps, aPartitionedPrincipalOriginAttributes,
                      nsIDNSService::RESOLVE_PRIORITY_MEDIUM);
                  nsIDNSService::GetFlagsFromTRRMode(aMode) |
                      nsIDNSService::RESOLVE_PRIORITY_MEDIUM);
}

nsresult nsHTMLDNSPrefetch::PrefetchHigh(
    const OriginAttributes& aOriginAttributes, nsIRequest::TRRMode aMode) {
    const nsAString& hostname, bool isHttps,
    const OriginAttributes& aPartitionedPrincipalOriginAttributes,
    nsIRequest::TRRMode aMode) {
  return Prefetch(hostname, isHttps, aPartitionedPrincipalOriginAttributes,
nsresult nsHTMLDNSPrefetch::CancelPrefetch(Link* aElement, uint32_t flags,
                  nsIDNSService::GetFlagsFromTRRMode(aMode));
}

nsresult nsHTMLDNSPrefetch::CancelPrefetch(Link* aElement, uint32_t flags,
                                           nsresult aReason) {
  nsAutoString hostname;
  if (!(sInitialized && sPrefetches && sDNSListener) || !EnsureDNSService())
    return NS_ERROR_NOT_AVAILABLE;

  nsAutoString hostname;
  aElement->GetHostname(hostname);


  Element* element = aElement->GetElement();
  NS_ENSURE_TRUE(element, NS_ERROR_FAILURE);

  nsAutoString protocol;
    isHttps = true;
  aElement->GetProtocol(protocol);
  bool isHttps = false;
  if (protocol.EqualsLiteral("https:")) {
    isHttps = true;
  }

  OriginAttributes oa;
  StoragePrincipalHelper::GetOriginAttributesForNetworkState(
nsresult nsHTMLDNSPrefetch::CancelPrefetch(
      element->OwnerDoc(), oa);

  return CancelPrefetch(hostname, isHttps, oa, flags, aReason);
}

nsresult nsHTMLDNSPrefetch::CancelPrefetch(
    const nsAString& hostname, bool isHttps,
    const OriginAttributes& aPartitionedPrincipalOriginAttributes,
        net_IsValidHostName(NS_ConvertUTF16toUTF8(hostname))) {
    uint32_t flags, nsresult aReason) {
  // Forward this request to Necko Parent if we're a child process
  if (IsNeckoChild()) {
    // We need to check IsEmpty() because net_IsValidHostName()
    // considers empty strings to be valid hostnames
      }
    if (!hostname.IsEmpty() &&
        net_IsValidHostName(NS_ConvertUTF16toUTF8(hostname))) {
      // during shutdown gNeckoChild might be null
      if (gNeckoChild) {
        gNeckoChild->SendCancelHTMLDNSPrefetch(
  if (!(sInitialized && sPrefetches && sDNSListener) || !EnsureDNSService())
            nsString(hostname), isHttps, aPartitionedPrincipalOriginAttributes,
            flags, aReason);
      }
    }
    return NS_OK;
  }
      NS_ConvertUTF16toUTF8(hostname), flags | nsIDNSService::RESOLVE_SPECULATE,

  if (!(sInitialized && sPrefetches && sDNSListener) || !EnsureDNSService())
    return NS_ERROR_NOT_AVAILABLE;

  // Forward cancellation to DNS service
    esniHost.Append("_esni.");
  nsresult rv = sDNSService->CancelAsyncResolveNative(
      NS_ConvertUTF16toUTF8(hostname), flags | nsIDNSService::RESOLVE_SPECULATE,
      sDNSListener, aReason, aPartitionedPrincipalOriginAttributes);
  // Cancel fetching ESNI keys if needed.
  if (StaticPrefs::network_security_esni_enabled() && isHttps) {
        aOriginAttributes);
    nsAutoCString esniHost;
    esniHost.Append("_esni.");
    esniHost.Append(NS_ConvertUTF16toUTF8(hostname));
    sDNSService->CancelAsyncResolveByTypeNative(

        esniHost, nsIDNSService::RESOLVE_TYPE_TXT,
        flags | nsIDNSService::RESOLVE_SPECULATE, sDNSListener, aReason,
        aPartitionedPrincipalOriginAttributes);
  }
  return rv;
}


nsresult nsHTMLDNSPrefetch::CancelPrefetchLow(Link* aElement,
                                              nsresult aReason) {
  return CancelPrefetch(
      aElement,
    nsresult aReason) {
      GetDNSFlagsFromLink(aElement) | nsIDNSService::RESOLVE_PRIORITY_LOW,
      aReason);
}

nsresult nsHTMLDNSPrefetch::CancelPrefetchLow(
}
    const nsAString& hostname, bool isHttps,
    const OriginAttributes& aPartitionedPrincipalOriginAttributes,
    nsIRequest::TRRMode aTRRMode, nsresult aReason) {
  return CancelPrefetch(hostname, isHttps,
                        aPartitionedPrincipalOriginAttributes,
    // Clean up all the possible links at once.
                        nsIDNSService::GetFlagsFromTRRMode(aTRRMode) |
                            nsIDNSService::RESOLVE_PRIORITY_LOW,
                        aReason);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
void nsHTMLDNSPrefetch::LinkDestroyed(Link* aLink) {
  MOZ_ASSERT(aLink->IsInDNSPrefetch());
  if (sPrefetches) {
    // Clean up all the possible links at once.
    sPrefetches->RemoveUnboundLinks();
nsHTMLDNSPrefetch::nsListener::OnLookupComplete(nsICancelable* request,
  }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////


NS_IMPL_ISUPPORTS(nsHTMLDNSPrefetch::nsListener, nsIDNSListener)

NS_IMETHODIMP
nsHTMLDNSPrefetch::nsListener::OnLookupComplete(nsICancelable* request,
                                                nsIDNSRecord* rec,
  mTimer = NS_NewTimer();
                                                nsresult status) {
  return NS_OK;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

nsHTMLDNSPrefetch::nsDeferrals::nsDeferrals()
    : mHead(0), mTail(0), mActiveLoaderCount(0), mTimerArmed(false) {
  }
  mTimer = NS_NewTimer();
  ;
}


nsHTMLDNSPrefetch::nsDeferrals::~nsDeferrals() {
  if (mTimerArmed) {
    mTimerArmed = false;
    mTimer->Cancel();
  }
  while (mHead != mTail) {

  Flush();
}

NS_IMPL_ISUPPORTS(nsHTMLDNSPrefetch::nsDeferrals, nsIWebProgressListener,
                  nsISupportsWeakReference, nsIObserver)
    mTail = (mTail + 1) & sMaxDeferredMask;

void nsHTMLDNSPrefetch::nsDeferrals::Flush() {
  while (mHead != mTail) {
    if (mEntries[mTail].mElement) {
      mEntries[mTail].mElement->ClearIsInDNSPrefetch();
  // The FIFO has no lock, so it can only be accessed on main thread
    }
    mEntries[mTail].mElement = nullptr;
    mTail = (mTail + 1) & sMaxDeferredMask;
  }
}
  if (((mHead + 1) & sMaxDeferredMask) == mTail)

nsresult nsHTMLDNSPrefetch::nsDeferrals::Add(uint32_t flags, Link* aElement) {
  // The FIFO has no lock, so it can only be accessed on main thread
  NS_ASSERTION(NS_IsMainThread(), "nsDeferrals::Add must be on main thread");

  mEntries[mHead].mElement = aElement;
  aElement->OnDNSPrefetchDeferred();

  if (((mHead + 1) & sMaxDeferredMask) == mTail)
    return NS_ERROR_DNS_LOOKUP_QUEUE_FULL;

    mTimer->InitWithNamedFuncCallback(Tick, this, 2000, nsITimer::TYPE_ONE_SHOT,
  aElement->SetIsInDNSPrefetch();
  mEntries[mHead].mFlags = flags;
  mEntries[mHead].mElement = aElement;
  mHead = (mHead + 1) & sMaxDeferredMask;
  return NS_OK;

  if (!mActiveLoaderCount && !mTimerArmed && mTimer) {
    mTimerArmed = true;
    mTimer->InitWithNamedFuncCallback(Tick, this, 2000, nsITimer::TYPE_ONE_SHOT,
                                      "nsHTMLDNSPrefetch::nsDeferrals::Tick");
               "nsDeferrals::SubmitQueue must be on main thread");
  }

  return NS_OK;
}


void nsHTMLDNSPrefetch::nsDeferrals::SubmitQueue() {
  NS_ASSERTION(NS_IsMainThread(),
               "nsDeferrals::SubmitQueue must be on main thread");
  nsCString hostName;
      // Only prefetch here if request was deferred and deferral not cancelled
  if (!EnsureDNSService()) {
    return;
  }

  while (mHead != mTail) {
    nsCOMPtr<Link> link = mEntries[mTail].mElement;
    if (link) {
        hostName.Truncate();
      link->ClearIsInDNSPrefetch();
      // Only prefetch here if request was deferred and deferral not cancelled
      if (link && link->HasDeferredDNSPrefetchRequest()) {
        nsCOMPtr<nsIURI> hrefURI(link ? link->GetURI() : nullptr);
        bool isLocalResource = false;
                                   nsIProtocolHandler::URI_IS_LOCAL_RESOURCE,
        nsresult rv = NS_OK;
        Element* element = link->GetElement();

        hostName.Truncate();
        bool isHttps = false;
        if (!hostName.IsEmpty() && NS_SUCCEEDED(rv) && !isLocalResource &&
        if (hrefURI) {
          hrefURI->GetAsciiHost(hostName);
          rv = NS_URIChainHasFlags(hrefURI,
                                   nsIProtocolHandler::URI_IS_LOCAL_RESOURCE,
                                   &isLocalResource);
              gNeckoChild->SendHTMLDNSPrefetch(
          isHttps = hrefURI->SchemeIs("https");
        }

                  mEntries[mTail].mFlags);
        OriginAttributes oa;
        StoragePrincipalHelper::GetOriginAttributesForNetworkState(
            element->OwnerDoc(), oa);

        if (!hostName.IsEmpty() && NS_SUCCEEDED(rv) && !isLocalResource &&
            rv = sDNSService->AsyncResolveNative(
            element) {
          if (IsNeckoChild()) {
            // during shutdown gNeckoChild might be null
            if (gNeckoChild) {
              gNeckoChild->SendHTMLDNSPrefetch(NS_ConvertUTF8toUTF16(hostName),
                                               isHttps, oa,
                                               mEntries[mTail].mFlags);
            }
          } else {
              esniHost.Append("_esni.");
            nsCOMPtr<nsICancelable> tmpOutstanding;

            rv = sDNSService->AsyncResolveNative(
                hostName,
                mEntries[mTail].mFlags | nsIDNSService::RESOLVE_SPECULATE,
                  sDNSListener, nullptr,
                sDNSListener, nullptr, oa, getter_AddRefs(tmpOutstanding));
            // Fetch ESNI keys if needed.
            if (NS_SUCCEEDED(rv) &&
                StaticPrefs::network_security_esni_enabled() && isHttps) {
              nsAutoCString esniHost;
            if (NS_SUCCEEDED(rv)) link->OnDNSPrefetchRequested();
              esniHost.Append("_esni.");
              esniHost.Append(hostName);
              sDNSService->AsyncResolveByTypeNative(
                  esniHost, nsIDNSService::RESOLVE_TYPE_TXT,
                  mEntries[mTail].mFlags | nsIDNSService::RESOLVE_SPECULATE,

                  sDNSListener, nullptr, oa, getter_AddRefs(tmpOutstanding));
            }
            // Tell link that deferred prefetch was requested
            if (NS_SUCCEEDED(rv)) link->OnDNSPrefetchRequested();
          }
  if (mTimerArmed) {
        }
      }
    }

    mEntries[mTail].mElement = nullptr;
    mTail = (mTail + 1) & sMaxDeferredMask;
  }

  nsCOMPtr<nsIWebProgress> progress = components::DocLoader::Service();
  if (mTimerArmed) {
    mTimerArmed = false;
    mTimer->Cancel();
  }
}
  // element refs

void nsHTMLDNSPrefetch::nsDeferrals::Activate() {
  // Register as an observer for the document loader
  nsCOMPtr<nsIWebProgress> progress = components::DocLoader::Service();
  if (progress)
}
    progress->AddProgressListener(this, nsIWebProgress::NOTIFY_STATE_DOCUMENT);

  // Register as an observer for xpcom shutdown events so we can drop any
  uint16_t tail = mTail;
  // element refs
  while (mHead != tail) {
  nsCOMPtr<nsIObserverService> observerService =
      mozilla::services::GetObserverService();
  if (observerService)
      mEntries[tail].mElement->ClearIsInDNSPrefetch();
    observerService->AddObserver(this, "xpcom-shutdown", true);
}

void nsHTMLDNSPrefetch::nsDeferrals::RemoveUnboundLinks() {
  uint16_t tail = mTail;
}
  while (mHead != tail) {
    if (mEntries[tail].mElement &&
        !mEntries[tail].mElement->GetElement()->IsInComposedDoc()) {
      mEntries[tail].mElement->ClearIsInDNSPrefetch();
      mEntries[tail].mElement = nullptr;
  nsHTMLDNSPrefetch::nsDeferrals* self =
    }
    tail = (tail + 1) & sMaxDeferredMask;
  }
}


// nsITimer related method

void nsHTMLDNSPrefetch::nsDeferrals::Tick(nsITimer* aTimer, void* aClosure) {
  nsHTMLDNSPrefetch::nsDeferrals* self =
      (nsHTMLDNSPrefetch::nsDeferrals*)aClosure;

  if (!self->mActiveLoaderCount) self->SubmitQueue();
  NS_ASSERTION(NS_IsMainThread(), "nsDeferrals::Tick must be on main thread");
  NS_ASSERTION(self->mTimerArmed, "Timer is not armed");

  self->mTimerArmed = false;

  // If the queue is not submitted here because there are outstanding pages
NS_IMETHODIMP
  // being loaded, there is no need to rearm the timer as the queue will be
  // submtited when those loads complete.
  if (!self->mActiveLoaderCount) self->SubmitQueue();
}

//////////// nsIWebProgressListener methods
               "nsDeferrals::OnStateChange must be on main thread");

NS_IMETHODIMP
nsHTMLDNSPrefetch::nsDeferrals::OnStateChange(nsIWebProgress* aWebProgress,
                                              nsIRequest* aRequest,
                                              uint32_t progressStateFlags,
      // not go negative
                                              nsresult aStatus) {
  // The FIFO has no lock, so it can only be accessed on main thread
  NS_ASSERTION(NS_IsMainThread(),
               "nsDeferrals::OnStateChange must be on main thread");

      mActiveLoaderCount++;
  if (progressStateFlags & STATE_IS_DOCUMENT) {
    if (progressStateFlags & STATE_STOP) {
      // Initialization may have missed a STATE_START notification, so do
      // not go negative
      if (mActiveLoaderCount) mActiveLoaderCount--;


      if (!mActiveLoaderCount) SubmitQueue();
    } else if (progressStateFlags & STATE_START)
      mActiveLoaderCount++;
  }
                                                 int32_t maxSelfProgress,

  return NS_OK;
}

NS_IMETHODIMP
}
nsHTMLDNSPrefetch::nsDeferrals::OnProgressChange(nsIWebProgress* aProgress,
                                                 nsIRequest* aRequest,
                                                 int32_t curSelfProgress,
                                                 int32_t maxSelfProgress,
                                                 nsIURI* location,
                                                 int32_t curTotalProgress,
                                                 int32_t maxTotalProgress) {
  return NS_OK;
}

NS_IMETHODIMP
NS_IMETHODIMP
nsHTMLDNSPrefetch::nsDeferrals::OnLocationChange(nsIWebProgress* aWebProgress,
                                                 nsIRequest* aRequest,
                                                 nsIURI* location,
                                                 uint32_t aFlags) {
  return NS_OK;
  return NS_OK;
}

NS_IMETHODIMP
nsHTMLDNSPrefetch::nsDeferrals::OnStatusChange(nsIWebProgress* aWebProgress,
                                                 nsIRequest* aRequest,
                                               nsIRequest* aRequest,
                                               nsresult aStatus,
                                               const char16_t* aMessage) {
  return NS_OK;
}
NS_IMETHODIMP

NS_IMETHODIMP
nsHTMLDNSPrefetch::nsDeferrals::OnSecurityChange(nsIWebProgress* aWebProgress,
                                                 nsIRequest* aRequest,
  return NS_OK;
}
                                                 uint32_t aState) {
  return NS_OK;
}


NS_IMETHODIMP
nsHTMLDNSPrefetch::nsDeferrals::OnContentBlockingEvent(
    nsIWebProgress* aWebProgress, nsIRequest* aRequest, uint32_t aEvent) {
  return NS_OK;
}

//////////// nsIObserver method

NS_IMETHODIMP
nsHTMLDNSPrefetch::nsDeferrals::Observe(nsISupports* subject, const char* topic,
                                        const char16_t* data) {
  if (!strcmp(topic, "xpcom-shutdown")) Flush();

  return NS_OK;
}