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 (1aeaa33a64f9)

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 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
/* -*- 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/. */

#include "nsPrefetchService.h"

#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/Attributes.h"
#include "mozilla/CORSMode.h"
#include "mozilla/dom/ClientInfo.h"
#include "mozilla/dom/HTMLLinkElement.h"
#include "mozilla/dom/ServiceWorkerDescriptor.h"
#include "mozilla/Preferences.h"

#include "nsICacheEntry.h"
#include "nsIServiceManager.h"
#include "nsICategoryManager.h"
#include "nsIObserverService.h"
#include "nsIWebProgress.h"
#include "nsCURILoader.h"
#include "nsICacheInfoChannel.h"
#include "nsIHttpChannel.h"
#include "nsIURL.h"
#include "nsISimpleEnumerator.h"
#include "nsISupportsPriority.h"
#include "nsNetUtil.h"
#include "nsString.h"
#include "nsReadableUtils.h"
#include "nsStreamUtils.h"
#include "nsAutoPtr.h"
#include "prtime.h"
#include "mozilla/Logging.h"
#include "plstr.h"
#include "nsIAsyncVerifyRedirectCallback.h"
#include "nsIDOMNode.h"
#include "nsINode.h"
#include "nsIDocument.h"
#include "nsContentUtils.h"
#include "nsStyleLinkElement.h"
#include "mozilla/AsyncEventDispatcher.h"

using namespace mozilla;

//
// To enable logging (see mozilla/Logging.h for full details):
//
//    set MOZ_LOG=nsPrefetch:5
//    set MOZ_LOG_FILE=prefetch.log
//
// this enables LogLevel::Debug level information and places all output in
// the file prefetch.log
//
static LazyLogModule gPrefetchLog("nsPrefetch");

#undef LOG
#define LOG(args) MOZ_LOG(gPrefetchLog, mozilla::LogLevel::Debug, args)

#undef LOG_ENABLED
#define LOG_ENABLED() MOZ_LOG_TEST(gPrefetchLog, mozilla::LogLevel::Debug)

#define PREFETCH_PREF "network.prefetch-next"
#define PRELOAD_PREF "network.preload"
#define PARALLELISM_PREF "network.prefetch-next.parallelism"
#define AGGRESSIVE_PREF "network.prefetch-next.aggressive"

//-----------------------------------------------------------------------------
// helpers
//-----------------------------------------------------------------------------

static inline uint32_t PRTimeToSeconds(PRTime t_usec) {
  PRTime usec_per_sec = PR_USEC_PER_SEC;
  return uint32_t(t_usec /= usec_per_sec);
}

#define NowInSeconds() PRTimeToSeconds(PR_Now())

//-----------------------------------------------------------------------------
// nsPrefetchNode <public>
//-----------------------------------------------------------------------------

nsPrefetchNode::nsPrefetchNode(nsPrefetchService *aService, nsIURI *aURI,
                               nsIURI *aReferrerURI, nsIDOMNode *aSource,
                               nsContentPolicyType aPolicyType, bool aPreload)
    : mURI(aURI),
      mReferrerURI(aReferrerURI),
      mPolicyType(aPolicyType),
      mPreload(aPreload),
      mService(aService),
      mChannel(nullptr),
      mBytesRead(0),
      mShouldFireLoadEvent(false) {
  nsCOMPtr<nsIWeakReference> source = do_GetWeakReference(aSource);
  mSources.AppendElement(source);
}

nsresult nsPrefetchNode::OpenChannel() {
  if (mSources.IsEmpty()) {
    // Don't attempt to prefetch if we don't have a source node
    // (which should never happen).
    return NS_ERROR_FAILURE;
  }
  nsCOMPtr<nsINode> source;
  while (!mSources.IsEmpty() &&
         !(source = do_QueryReferent(mSources.ElementAt(0)))) {
    // If source is null remove it.
    // (which should never happen).
    mSources.RemoveElementAt(0);
  }

  if (!source) {
    // Don't attempt to prefetch if we don't have a source node
    // (which should never happen).

    return NS_ERROR_FAILURE;
  }
  nsCOMPtr<nsILoadGroup> loadGroup = source->OwnerDoc()->GetDocumentLoadGroup();
  CORSMode corsMode = CORS_NONE;
  net::ReferrerPolicy referrerPolicy = net::RP_Unset;
  if (source->IsHTMLElement(nsGkAtoms::link)) {
    dom::HTMLLinkElement *link =
        static_cast<dom::HTMLLinkElement *>(source.get());
    corsMode = link->GetCORSMode();
    referrerPolicy = link->GetLinkReferrerPolicy();
  }

  if (referrerPolicy == net::RP_Unset) {
    referrerPolicy = source->OwnerDoc()->GetReferrerPolicy();
  }

  uint32_t securityFlags;
  if (corsMode == CORS_NONE) {
    securityFlags = nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS;
  } else {
    securityFlags = nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS;
    if (corsMode == CORS_USE_CREDENTIALS) {
      securityFlags |= nsILoadInfo::SEC_COOKIES_INCLUDE;
    }
  }
  nsresult rv = NS_NewChannelInternal(
      getter_AddRefs(mChannel), mURI, source, source->NodePrincipal(),
      nullptr,  // aTriggeringPrincipal
      Maybe<ClientInfo>(), Maybe<ServiceWorkerDescriptor>(), securityFlags,
      mPolicyType,
      nullptr,    // aPerformanceStorage
      loadGroup,  // aLoadGroup
      this,       // aCallbacks
      nsIRequest::LOAD_BACKGROUND | nsICachingChannel::LOAD_ONLY_IF_MODIFIED);

  NS_ENSURE_SUCCESS(rv, rv);

  // configure HTTP specific stuff
  nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(mChannel);
  if (httpChannel) {
    rv = httpChannel->SetReferrerWithPolicy(mReferrerURI, referrerPolicy);
    MOZ_ASSERT(NS_SUCCEEDED(rv));
    rv = httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("X-Moz"),
                                       NS_LITERAL_CSTRING("prefetch"), false);
    MOZ_ASSERT(NS_SUCCEEDED(rv));
  }

  // Reduce the priority of prefetch network requests.
  nsCOMPtr<nsISupportsPriority> priorityChannel = do_QueryInterface(mChannel);
  if (priorityChannel) {
    priorityChannel->AdjustPriority(nsISupportsPriority::PRIORITY_LOWEST);
  }

  rv = mChannel->AsyncOpen2(this);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    // Drop the ref to the channel, because we don't want to end up with
    // cycles through it.
    mChannel = nullptr;
  }
  return rv;
}

nsresult nsPrefetchNode::CancelChannel(nsresult error) {
  mChannel->Cancel(error);
  mChannel = nullptr;

  return NS_OK;
}

//-----------------------------------------------------------------------------
// nsPrefetchNode::nsISupports
//-----------------------------------------------------------------------------

NS_IMPL_ISUPPORTS(nsPrefetchNode, nsIRequestObserver, nsIStreamListener,
                  nsIInterfaceRequestor, nsIChannelEventSink,
                  nsIRedirectResultListener)

//-----------------------------------------------------------------------------
// nsPrefetchNode::nsIStreamListener
//-----------------------------------------------------------------------------

NS_IMETHODIMP
nsPrefetchNode::OnStartRequest(nsIRequest *aRequest, nsISupports *aContext) {
  nsresult rv;

  nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aRequest, &rv);
  if (NS_FAILED(rv)) return rv;

  // if the load is cross origin without CORS, or the CORS access is rejected,
  // always fire load event to avoid leaking site information.
  nsCOMPtr<nsILoadInfo> loadInfo = httpChannel->GetLoadInfo();
  if (loadInfo) {
    mShouldFireLoadEvent =
        loadInfo->GetTainting() == LoadTainting::Opaque ||
        (loadInfo->GetTainting() == LoadTainting::CORS &&
         (NS_FAILED(httpChannel->GetStatus(&rv)) || NS_FAILED(rv)));
  }

  // no need to prefetch http error page
  bool requestSucceeded;
  if (NS_FAILED(httpChannel->GetRequestSucceeded(&requestSucceeded)) ||
      !requestSucceeded) {
    return NS_BINDING_ABORTED;
  }

  nsCOMPtr<nsICacheInfoChannel> cacheInfoChannel =
      do_QueryInterface(aRequest, &rv);
  if (NS_FAILED(rv)) return rv;

  // no need to prefetch a document that is already in the cache
  bool fromCache;
  if (NS_SUCCEEDED(cacheInfoChannel->IsFromCache(&fromCache)) && fromCache) {
    LOG(("document is already in the cache; canceling prefetch\n"));
    // although it's canceled we still want to fire load event
    mShouldFireLoadEvent = true;
    return NS_BINDING_ABORTED;
  }

  //
  // no need to prefetch a document that must be requested fresh each
  // and every time.
  //
  uint32_t expTime;
  if (NS_SUCCEEDED(cacheInfoChannel->GetCacheTokenExpirationTime(&expTime))) {
    if (NowInSeconds() >= expTime) {
      LOG(
          ("document cannot be reused from cache; "
           "canceling prefetch\n"));
      return NS_BINDING_ABORTED;
    }
  }

  return NS_OK;
}

NS_IMETHODIMP
nsPrefetchNode::OnDataAvailable(nsIRequest *aRequest, nsISupports *aContext,
                                nsIInputStream *aStream, uint64_t aOffset,
                                uint32_t aCount) {
  uint32_t bytesRead = 0;
  aStream->ReadSegments(NS_DiscardSegment, nullptr, aCount, &bytesRead);
  mBytesRead += bytesRead;
  LOG(("prefetched %u bytes [offset=%" PRIu64 "]\n", bytesRead, aOffset));
  return NS_OK;
}

NS_IMETHODIMP
nsPrefetchNode::OnStopRequest(nsIRequest *aRequest, nsISupports *aContext,
                              nsresult aStatus) {
  LOG(("done prefetching [status=%" PRIx32 "]\n",
       static_cast<uint32_t>(aStatus)));

  if (mBytesRead == 0 && aStatus == NS_OK && mChannel) {
    // we didn't need to read (because LOAD_ONLY_IF_MODIFIED was
    // specified), but the object should report loadedSize as if it
    // did.
    mChannel->GetContentLength(&mBytesRead);
  }

  mService->NotifyLoadCompleted(this);
  mService->DispatchEvent(this, mShouldFireLoadEvent || NS_SUCCEEDED(aStatus));
  mService->RemoveNodeAndMaybeStartNextPrefetchURI(this);
  return NS_OK;
}

//-----------------------------------------------------------------------------
// nsPrefetchNode::nsIInterfaceRequestor
//-----------------------------------------------------------------------------

NS_IMETHODIMP
nsPrefetchNode::GetInterface(const nsIID &aIID, void **aResult) {
  if (aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
    NS_ADDREF_THIS();
    *aResult = static_cast<nsIChannelEventSink *>(this);
    return NS_OK;
  }

  if (aIID.Equals(NS_GET_IID(nsIRedirectResultListener))) {
    NS_ADDREF_THIS();
    *aResult = static_cast<nsIRedirectResultListener *>(this);
    return NS_OK;
  }

  return NS_ERROR_NO_INTERFACE;
}

//-----------------------------------------------------------------------------
// nsPrefetchNode::nsIChannelEventSink
//-----------------------------------------------------------------------------

NS_IMETHODIMP
nsPrefetchNode::AsyncOnChannelRedirect(
    nsIChannel *aOldChannel, nsIChannel *aNewChannel, uint32_t aFlags,
    nsIAsyncVerifyRedirectCallback *callback) {
  nsCOMPtr<nsIURI> newURI;
  nsresult rv = aNewChannel->GetURI(getter_AddRefs(newURI));
  if (NS_FAILED(rv)) return rv;

  bool match;
  rv = newURI->SchemeIs("http", &match);
  if (NS_FAILED(rv) || !match) {
    rv = newURI->SchemeIs("https", &match);
    if (NS_FAILED(rv) || !match) {
      LOG(("rejected: URL is not of type http/https\n"));
      return NS_ERROR_ABORT;
    }
  }

  // HTTP request headers are not automatically forwarded to the new channel.
  nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aNewChannel);
  NS_ENSURE_STATE(httpChannel);

  rv = httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("X-Moz"),
                                     NS_LITERAL_CSTRING("prefetch"), false);
  MOZ_ASSERT(NS_SUCCEEDED(rv));

  // Assign to mChannel after we get notification about success of the
  // redirect in OnRedirectResult.
  mRedirectChannel = aNewChannel;

  callback->OnRedirectVerifyCallback(NS_OK);
  return NS_OK;
}

//-----------------------------------------------------------------------------
// nsPrefetchNode::nsIRedirectResultListener
//-----------------------------------------------------------------------------

NS_IMETHODIMP
nsPrefetchNode::OnRedirectResult(bool proceeding) {
  if (proceeding && mRedirectChannel) mChannel = mRedirectChannel;

  mRedirectChannel = nullptr;

  return NS_OK;
}

//-----------------------------------------------------------------------------
// nsPrefetchService <public>
//-----------------------------------------------------------------------------

nsPrefetchService::nsPrefetchService()
    : mMaxParallelism(6),
      mStopCount(0),
      mHaveProcessed(false),
      mPrefetchDisabled(true),
      mPreloadDisabled(true),
      mAggressive(false) {}

nsPrefetchService::~nsPrefetchService() {
  Preferences::RemoveObserver(this, PREFETCH_PREF);
  Preferences::RemoveObserver(this, PRELOAD_PREF);
  Preferences::RemoveObserver(this, PARALLELISM_PREF);
  Preferences::RemoveObserver(this, AGGRESSIVE_PREF);
  // cannot reach destructor if prefetch in progress (listener owns reference
  // to this service)
  EmptyPrefetchQueue();
}

nsresult nsPrefetchService::Init() {
  nsresult rv;

  // read prefs and hook up pref observer
  mPrefetchDisabled = !Preferences::GetBool(PREFETCH_PREF, !mPrefetchDisabled);
  Preferences::AddWeakObserver(this, PREFETCH_PREF);

  mPreloadDisabled = !Preferences::GetBool(PRELOAD_PREF, !mPreloadDisabled);
  Preferences::AddWeakObserver(this, PRELOAD_PREF);

  mMaxParallelism = Preferences::GetInt(PARALLELISM_PREF, mMaxParallelism);
  if (mMaxParallelism < 1) {
    mMaxParallelism = 1;
  }
  Preferences::AddWeakObserver(this, PARALLELISM_PREF);

  mAggressive = Preferences::GetBool(AGGRESSIVE_PREF, false);
  Preferences::AddWeakObserver(this, AGGRESSIVE_PREF);

  // Observe xpcom-shutdown event
  nsCOMPtr<nsIObserverService> observerService =
      mozilla::services::GetObserverService();
  if (!observerService) return NS_ERROR_FAILURE;

  rv = observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, true);
  NS_ENSURE_SUCCESS(rv, rv);

  if (!mPrefetchDisabled || !mPreloadDisabled) {
    AddProgressListener();
  }

  return NS_OK;
}

void nsPrefetchService::RemoveNodeAndMaybeStartNextPrefetchURI(
    nsPrefetchNode *aFinished) {
  if (aFinished) {
    mCurrentNodes.RemoveElement(aFinished);
  }

  if ((!mStopCount && mHaveProcessed) || mAggressive) {
    ProcessNextPrefetchURI();
  }
}

void nsPrefetchService::ProcessNextPrefetchURI() {
  if (mCurrentNodes.Length() >= static_cast<uint32_t>(mMaxParallelism)) {
    // We already have enough prefetches going on, so hold off
    // for now.
    return;
  }

  nsresult rv;

  do {
    if (mPrefetchQueue.empty()) {
      break;
    }
    RefPtr<nsPrefetchNode> node = mPrefetchQueue.front().forget();
    mPrefetchQueue.pop_front();

    if (LOG_ENABLED()) {
      LOG(("ProcessNextPrefetchURI [%s]\n",
           node->mURI->GetSpecOrDefault().get()));
    }

    //
    // if opening the channel fails (e.g. security check returns an error),
    // send an error event and then just skip to the next uri
    //
    rv = node->OpenChannel();
    if (NS_SUCCEEDED(rv)) {
      mCurrentNodes.AppendElement(node);
    } else {
      DispatchEvent(node, false);
    }
  } while (NS_FAILED(rv));
}

void nsPrefetchService::NotifyLoadRequested(nsPrefetchNode *node) {
  nsCOMPtr<nsIObserverService> observerService =
      mozilla::services::GetObserverService();
  if (!observerService) return;

  observerService->NotifyObservers(
      static_cast<nsIStreamListener *>(node),
      (node->mPreload) ? "preload-load-requested" : "prefetch-load-requested",
      nullptr);
}

void nsPrefetchService::NotifyLoadCompleted(nsPrefetchNode *node) {
  nsCOMPtr<nsIObserverService> observerService =
      mozilla::services::GetObserverService();
  if (!observerService) return;

  observerService->NotifyObservers(
      static_cast<nsIStreamListener *>(node),
      (node->mPreload) ? "preload-load-completed" : "prefetch-load-completed",
      nullptr);
}

void nsPrefetchService::DispatchEvent(nsPrefetchNode *node, bool aSuccess) {
  for (uint32_t i = 0; i < node->mSources.Length(); i++) {
    nsCOMPtr<nsINode> domNode = do_QueryReferent(node->mSources.ElementAt(i));
    if (domNode && domNode->IsInComposedDoc()) {
      // We don't dispatch synchronously since |node| might be in a DocGroup
      // that we're not allowed to touch. (Our network request happens in the
      // DocGroup of one of the mSources nodes--not necessarily this one).
      RefPtr<AsyncEventDispatcher> dispatcher = new AsyncEventDispatcher(
          domNode,
          aSuccess ? NS_LITERAL_STRING("load") : NS_LITERAL_STRING("error"),
          /* aCanBubble = */ false);
      dispatcher->RequireNodeInDocument();
      dispatcher->PostDOMEvent();
    }
  }
}

//-----------------------------------------------------------------------------
// nsPrefetchService <private>
//-----------------------------------------------------------------------------

void nsPrefetchService::AddProgressListener() {
  // Register as an observer for the document loader
  nsCOMPtr<nsIWebProgress> progress =
      do_GetService(NS_DOCUMENTLOADER_SERVICE_CONTRACTID);
  if (progress)
    progress->AddProgressListener(this, nsIWebProgress::NOTIFY_STATE_DOCUMENT);
}

void nsPrefetchService::RemoveProgressListener() {
  // Register as an observer for the document loader
  nsCOMPtr<nsIWebProgress> progress =
      do_GetService(NS_DOCUMENTLOADER_SERVICE_CONTRACTID);
  if (progress) progress->RemoveProgressListener(this);
}

nsresult nsPrefetchService::EnqueueURI(nsIURI *aURI, nsIURI *aReferrerURI,
                                       nsIDOMNode *aSource,
                                       nsPrefetchNode **aNode) {
  RefPtr<nsPrefetchNode> node = new nsPrefetchNode(
      this, aURI, aReferrerURI, aSource, nsIContentPolicy::TYPE_OTHER, false);
  mPrefetchQueue.push_back(node);
  node.forget(aNode);
  return NS_OK;
}

void nsPrefetchService::EmptyPrefetchQueue() {
  while (!mPrefetchQueue.empty()) {
    mPrefetchQueue.pop_back();
  }
}

void nsPrefetchService::StartPrefetching() {
  //
  // at initialization time we might miss the first DOCUMENT START
  // notification, so we have to be careful to avoid letting our
  // stop count go negative.
  //
  if (mStopCount > 0) mStopCount--;

  LOG(("StartPrefetching [stopcount=%d]\n", mStopCount));

  // only start prefetching after we've received enough DOCUMENT
  // STOP notifications.  we do this inorder to defer prefetching
  // until after all sub-frames have finished loading.
  if (!mStopCount) {
    mHaveProcessed = true;
    while (!mPrefetchQueue.empty() &&
           mCurrentNodes.Length() < static_cast<uint32_t>(mMaxParallelism)) {
      ProcessNextPrefetchURI();
    }
  }
}

void nsPrefetchService::StopPrefetching() {
  mStopCount++;

  LOG(("StopPrefetching [stopcount=%d]\n", mStopCount));

  // When we start a load, we need to stop all prefetches that has been
  // added by the old load, therefore call StopAll only at the moment we
  // switch to a new page load (i.e. mStopCount == 1).
  // TODO: do not stop prefetches that are relevant for the new load.
  if (mStopCount == 1) {
    StopAll();
  }
}

void nsPrefetchService::StopCurrentPrefetchsPreloads(bool aPreload) {
  for (int32_t i = mCurrentNodes.Length() - 1; i >= 0; --i) {
    if (mCurrentNodes[i]->mPreload == aPreload) {
      mCurrentNodes[i]->CancelChannel(NS_BINDING_ABORTED);
      mCurrentNodes.RemoveElementAt(i);
    }
  }

  if (!aPreload) {
    EmptyPrefetchQueue();
  }
}

void nsPrefetchService::StopAll() {
  for (uint32_t i = 0; i < mCurrentNodes.Length(); ++i) {
    mCurrentNodes[i]->CancelChannel(NS_BINDING_ABORTED);
  }
  mCurrentNodes.Clear();
  EmptyPrefetchQueue();
}

nsresult nsPrefetchService::CheckURIScheme(nsIURI *aURI, nsIURI *aReferrerURI) {
  //
  // XXX we should really be asking the protocol handler if it supports
  // caching, so we can determine if there is any value to prefetching.
  // for now, we'll only prefetch http and https links since we know that's
  // the most common case.
  //
  bool match;
  nsresult rv = aURI->SchemeIs("http", &match);
  if (NS_FAILED(rv) || !match) {
    rv = aURI->SchemeIs("https", &match);
    if (NS_FAILED(rv) || !match) {
      LOG(("rejected: URL is not of type http/https\n"));
      return NS_ERROR_ABORT;
    }
  }

  //
  // the referrer URI must be http:
  //
  rv = aReferrerURI->SchemeIs("http", &match);
  if (NS_FAILED(rv) || !match) {
    rv = aReferrerURI->SchemeIs("https", &match);
    if (NS_FAILED(rv) || !match) {
      LOG(("rejected: referrer URL is neither http nor https\n"));
      return NS_ERROR_ABORT;
    }
  }

  return NS_OK;
}

//-----------------------------------------------------------------------------
// nsPrefetchService::nsISupports
//-----------------------------------------------------------------------------

NS_IMPL_ISUPPORTS(nsPrefetchService, nsIPrefetchService, nsIWebProgressListener,
                  nsIObserver, nsISupportsWeakReference)

//-----------------------------------------------------------------------------
// nsPrefetchService::nsIPrefetchService
//-----------------------------------------------------------------------------

nsresult nsPrefetchService::Preload(nsIURI *aURI, nsIURI *aReferrerURI,
                                    nsIDOMNode *aSource,
                                    nsContentPolicyType aPolicyType) {
  NS_ENSURE_ARG_POINTER(aURI);
  NS_ENSURE_ARG_POINTER(aReferrerURI);
  if (LOG_ENABLED()) {
    LOG(("PreloadURI [%s]\n", aURI->GetSpecOrDefault().get()));
  }

  if (mPreloadDisabled) {
    LOG(("rejected: preload service is disabled\n"));
    return NS_ERROR_ABORT;
  }

  nsresult rv = CheckURIScheme(aURI, aReferrerURI);
  if (NS_FAILED(rv)) {
    return rv;
  }

  // XXX we might want to either leverage nsIProtocolHandler::protocolFlags
  // or possibly nsIRequest::loadFlags to determine if this URI should be
  // prefetched.
  //

  if (aPolicyType == nsIContentPolicy::TYPE_INVALID) {
    nsCOMPtr<nsINode> domNode = do_QueryInterface(aSource);
    if (domNode && domNode->IsInComposedDoc()) {
      RefPtr<AsyncEventDispatcher> asyncDispatcher =
          new AsyncEventDispatcher(  // domNode->OwnerDoc(),
              domNode, NS_LITERAL_STRING("error"),
              /* aCanBubble = */ false,
              /* aCancelable = */ false);
      asyncDispatcher->RunDOMEventWhenSafe();
    }
    return NS_OK;
  }

  //
  // Check whether it is being preloaded.
  //
  for (uint32_t i = 0; i < mCurrentNodes.Length(); ++i) {
    bool equals;
    if ((mCurrentNodes[i]->mPolicyType == aPolicyType) &&
        NS_SUCCEEDED(mCurrentNodes[i]->mURI->Equals(aURI, &equals)) && equals) {
      nsCOMPtr<nsIWeakReference> source = do_GetWeakReference(aSource);
      if (mCurrentNodes[i]->mSources.IndexOf(source) ==
          mCurrentNodes[i]->mSources.NoIndex) {
        LOG(
            ("URL is already being preloaded, add a new reference "
             "document\n"));
        mCurrentNodes[i]->mSources.AppendElement(source);
        return NS_OK;
      } else {
        LOG(("URL is already being preloaded by this document"));
        return NS_ERROR_ABORT;
      }
    }
  }

  LOG(("This is a preload, so start loading immediately.\n"));
  RefPtr<nsPrefetchNode> enqueuedNode;
  enqueuedNode =
      new nsPrefetchNode(this, aURI, aReferrerURI, aSource, aPolicyType, true);

  NotifyLoadRequested(enqueuedNode);
  rv = enqueuedNode->OpenChannel();
  if (NS_SUCCEEDED(rv)) {
    mCurrentNodes.AppendElement(enqueuedNode);
  } else {
    nsCOMPtr<nsINode> domNode = do_QueryInterface(aSource);
    if (domNode && domNode->IsInComposedDoc()) {
      RefPtr<AsyncEventDispatcher> asyncDispatcher =
          new AsyncEventDispatcher(domNode, NS_LITERAL_STRING("error"),
                                   /* aCanBubble = */ false,
                                   /* aCancelable = */ false);
      asyncDispatcher->RunDOMEventWhenSafe();
    }
  }
  return NS_OK;
}

nsresult nsPrefetchService::Prefetch(nsIURI *aURI, nsIURI *aReferrerURI,
                                     nsIDOMNode *aSource, bool aExplicit) {
  NS_ENSURE_ARG_POINTER(aURI);
  NS_ENSURE_ARG_POINTER(aReferrerURI);

  if (LOG_ENABLED()) {
    LOG(("PrefetchURI [%s]\n", aURI->GetSpecOrDefault().get()));
  }

  if (mPrefetchDisabled) {
    LOG(("rejected: prefetch service is disabled\n"));
    return NS_ERROR_ABORT;
  }

  nsresult rv = CheckURIScheme(aURI, aReferrerURI);
  if (NS_FAILED(rv)) {
    return rv;
  }

  // XXX we might want to either leverage nsIProtocolHandler::protocolFlags
  // or possibly nsIRequest::loadFlags to determine if this URI should be
  // prefetched.
  //

  // skip URLs that contain query strings, except URLs for which prefetching
  // has been explicitly requested.
  if (!aExplicit) {
    nsCOMPtr<nsIURL> url(do_QueryInterface(aURI, &rv));
    if (NS_FAILED(rv)) return rv;
    nsAutoCString query;
    rv = url->GetQuery(query);
    if (NS_FAILED(rv) || !query.IsEmpty()) {
      LOG(("rejected: URL has a query string\n"));
      return NS_ERROR_ABORT;
    }
  }

  //
  // Check whether it is being prefetched.
  //
  for (uint32_t i = 0; i < mCurrentNodes.Length(); ++i) {
    bool equals;
    if (NS_SUCCEEDED(mCurrentNodes[i]->mURI->Equals(aURI, &equals)) && equals) {
      nsCOMPtr<nsIWeakReference> source = do_GetWeakReference(aSource);
      if (mCurrentNodes[i]->mSources.IndexOf(source) ==
          mCurrentNodes[i]->mSources.NoIndex) {
        LOG(
            ("URL is already being prefetched, add a new reference "
             "document\n"));
        mCurrentNodes[i]->mSources.AppendElement(source);
        return NS_OK;
      } else {
        LOG(("URL is already being prefetched by this document"));
        return NS_ERROR_ABORT;
      }
    }
  }

  //
  // Check whether it is on the prefetch queue.
  //
  for (std::deque<RefPtr<nsPrefetchNode>>::iterator nodeIt =
           mPrefetchQueue.begin();
       nodeIt != mPrefetchQueue.end(); nodeIt++) {
    bool equals;
    RefPtr<nsPrefetchNode> node = nodeIt->get();
    if (NS_SUCCEEDED(node->mURI->Equals(aURI, &equals)) && equals) {
      nsCOMPtr<nsIWeakReference> source = do_GetWeakReference(aSource);
      if (node->mSources.IndexOf(source) == node->mSources.NoIndex) {
        LOG(
            ("URL is already being prefetched, add a new reference "
             "document\n"));
        node->mSources.AppendElement(do_GetWeakReference(aSource));
        return NS_OK;
      } else {
        LOG(("URL is already being prefetched by this document"));
        return NS_ERROR_ABORT;
      }
    }
  }

  RefPtr<nsPrefetchNode> enqueuedNode;
  rv = EnqueueURI(aURI, aReferrerURI, aSource, getter_AddRefs(enqueuedNode));
  NS_ENSURE_SUCCESS(rv, rv);

  NotifyLoadRequested(enqueuedNode);

  // if there are no pages loading, kick off the request immediately
  if ((!mStopCount && mHaveProcessed) || mAggressive) {
    ProcessNextPrefetchURI();
  }

  return NS_OK;
}

NS_IMETHODIMP
nsPrefetchService::CancelPrefetchPreloadURI(nsIURI *aURI, nsIDOMNode *aSource) {
  NS_ENSURE_ARG_POINTER(aURI);

  if (LOG_ENABLED()) {
    LOG(("CancelPrefetchURI [%s]\n", aURI->GetSpecOrDefault().get()));
  }

  //
  // look in current prefetches
  //
  for (uint32_t i = 0; i < mCurrentNodes.Length(); ++i) {
    bool equals;
    if (NS_SUCCEEDED(mCurrentNodes[i]->mURI->Equals(aURI, &equals)) && equals) {
      nsCOMPtr<nsIWeakReference> source = do_GetWeakReference(aSource);
      if (mCurrentNodes[i]->mSources.IndexOf(source) !=
          mCurrentNodes[i]->mSources.NoIndex) {
        mCurrentNodes[i]->mSources.RemoveElement(source);
        if (mCurrentNodes[i]->mSources.IsEmpty()) {
          mCurrentNodes[i]->CancelChannel(NS_BINDING_ABORTED);
          mCurrentNodes.RemoveElementAt(i);
        }
        return NS_OK;
      }
      return NS_ERROR_FAILURE;
    }
  }

  //
  // look into the prefetch queue
  //
  for (std::deque<RefPtr<nsPrefetchNode>>::iterator nodeIt =
           mPrefetchQueue.begin();
       nodeIt != mPrefetchQueue.end(); nodeIt++) {
    bool equals;
    RefPtr<nsPrefetchNode> node = nodeIt->get();
    if (NS_SUCCEEDED(node->mURI->Equals(aURI, &equals)) && equals) {
      nsCOMPtr<nsIWeakReference> source = do_GetWeakReference(aSource);
      if (node->mSources.IndexOf(source) != node->mSources.NoIndex) {
#ifdef DEBUG
        int32_t inx = node->mSources.IndexOf(source);
        nsCOMPtr<nsIDOMNode> domNode =
            do_QueryReferent(node->mSources.ElementAt(inx));
        MOZ_ASSERT(domNode);
#endif

        node->mSources.RemoveElement(source);
        if (node->mSources.IsEmpty()) {
          mPrefetchQueue.erase(nodeIt);
        }
        return NS_OK;
      }
      return NS_ERROR_FAILURE;
    }
  }

  // not found!
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
nsPrefetchService::PreloadURI(nsIURI *aURI, nsIURI *aReferrerURI,
                              nsIDOMNode *aSource,
                              nsContentPolicyType aPolicyType) {
  return Preload(aURI, aReferrerURI, aSource, aPolicyType);
}

NS_IMETHODIMP
nsPrefetchService::PrefetchURI(nsIURI *aURI, nsIURI *aReferrerURI,
                               nsIDOMNode *aSource, bool aExplicit) {
  return Prefetch(aURI, aReferrerURI, aSource, aExplicit);
}

NS_IMETHODIMP
nsPrefetchService::HasMoreElements(bool *aHasMore) {
  *aHasMore = (mCurrentNodes.Length() || !mPrefetchQueue.empty());
  return NS_OK;
}

//-----------------------------------------------------------------------------
// nsPrefetchService::nsIWebProgressListener
//-----------------------------------------------------------------------------

NS_IMETHODIMP
nsPrefetchService::OnProgressChange(nsIWebProgress *aProgress,
                                    nsIRequest *aRequest,
                                    int32_t curSelfProgress,
                                    int32_t maxSelfProgress,
                                    int32_t curTotalProgress,
                                    int32_t maxTotalProgress) {
  NS_NOTREACHED("notification excluded in AddProgressListener(...)");
  return NS_OK;
}

NS_IMETHODIMP
nsPrefetchService::OnStateChange(nsIWebProgress *aWebProgress,
                                 nsIRequest *aRequest,
                                 uint32_t progressStateFlags,
                                 nsresult aStatus) {
  if (progressStateFlags & STATE_IS_DOCUMENT) {
    if (progressStateFlags & STATE_STOP)
      StartPrefetching();
    else if (progressStateFlags & STATE_START)
      StopPrefetching();
  }

  return NS_OK;
}

NS_IMETHODIMP
nsPrefetchService::OnLocationChange(nsIWebProgress *aWebProgress,
                                    nsIRequest *aRequest, nsIURI *location,
                                    uint32_t aFlags) {
  NS_NOTREACHED("notification excluded in AddProgressListener(...)");
  return NS_OK;
}

NS_IMETHODIMP
nsPrefetchService::OnStatusChange(nsIWebProgress *aWebProgress,
                                  nsIRequest *aRequest, nsresult aStatus,
                                  const char16_t *aMessage) {
  NS_NOTREACHED("notification excluded in AddProgressListener(...)");
  return NS_OK;
}

NS_IMETHODIMP
nsPrefetchService::OnSecurityChange(nsIWebProgress *aWebProgress,
                                    nsIRequest *aRequest, uint32_t state) {
  NS_NOTREACHED("notification excluded in AddProgressListener(...)");
  return NS_OK;
}

//-----------------------------------------------------------------------------
// nsPrefetchService::nsIObserver
//-----------------------------------------------------------------------------

NS_IMETHODIMP
nsPrefetchService::Observe(nsISupports *aSubject, const char *aTopic,
                           const char16_t *aData) {
  LOG(("nsPrefetchService::Observe [topic=%s]\n", aTopic));

  if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
    StopAll();
    mPrefetchDisabled = true;
    mPreloadDisabled = true;
  } else if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
    const nsCString converted = NS_ConvertUTF16toUTF8(aData);
    const char *pref = converted.get();
    if (!strcmp(pref, PREFETCH_PREF)) {
      if (Preferences::GetBool(PREFETCH_PREF, false)) {
        if (mPrefetchDisabled) {
          LOG(("enabling prefetching\n"));
          mPrefetchDisabled = false;
          if (mPreloadDisabled) {
            AddProgressListener();
          }
        }
      } else {
        if (!mPrefetchDisabled) {
          LOG(("disabling prefetching\n"));
          StopCurrentPrefetchsPreloads(false);
          mPrefetchDisabled = true;
          if (mPreloadDisabled) {
            RemoveProgressListener();
          }
        }
      }
    } else if (!strcmp(pref, PRELOAD_PREF)) {
      if (Preferences::GetBool(PRELOAD_PREF, false)) {
        if (mPreloadDisabled) {
          LOG(("enabling preloading\n"));
          mPreloadDisabled = false;
          if (mPrefetchDisabled) {
            AddProgressListener();
          }
        }
      } else {
        if (!mPreloadDisabled) {
          LOG(("disabling preloading\n"));
          StopCurrentPrefetchsPreloads(true);
          mPreloadDisabled = true;
          if (mPrefetchDisabled) {
            RemoveProgressListener();
          }
        }
      }
    } else if (!strcmp(pref, PARALLELISM_PREF)) {
      mMaxParallelism = Preferences::GetInt(PARALLELISM_PREF, mMaxParallelism);
      if (mMaxParallelism < 1) {
        mMaxParallelism = 1;
      }
      // If our parallelism has increased, go ahead and kick off enough
      // prefetches to fill up our allowance. If we're now over our
      // allowance, we'll just silently let some of them finish to get
      // back below our limit.
      while (((!mStopCount && mHaveProcessed) || mAggressive) &&
             !mPrefetchQueue.empty() &&
             mCurrentNodes.Length() < static_cast<uint32_t>(mMaxParallelism)) {
        ProcessNextPrefetchURI();
      }
    } else if (!strcmp(pref, AGGRESSIVE_PREF)) {
      mAggressive = Preferences::GetBool(AGGRESSIVE_PREF, false);
      // in aggressive mode, start prefetching immediately
      if (mAggressive) {
        while (mStopCount && !mPrefetchQueue.empty() &&
               mCurrentNodes.Length() <
                   static_cast<uint32_t>(mMaxParallelism)) {
          ProcessNextPrefetchURI();
        }
      }
    }
  }

  return NS_OK;
}

// vim: ts=4 sw=4 expandtab