Commit 6b8e200b authored by Mike Conley's avatar Mike Conley
Browse files

Bug 1724748 - Add nsITopLevelNavigationDelegate to allow JSWindowActorChilds...

Bug 1724748 - Add nsITopLevelNavigationDelegate to allow JSWindowActorChilds to allow/deny top-level navigations. r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D122138
parent 341cd8f4
Loading
Loading
Loading
Loading
+21 −0
Original line number Original line Diff line number Diff line
@@ -9214,6 +9214,27 @@ nsresult nsDocShell::InternalLoad(nsDocShellLoadState* aLoadState,
    return rv;
    return rv;
  }
  }


  if (mBrowsingContext->IsTopContent() &&
      !aLoadState->GetPendingRedirectedChannel()) {
    nsCOMPtr<nsITopLevelNavigationDelegate> delegate =
        do_GetInterface(mTreeOwner);
    if (delegate) {
      bool shouldNavigate = false;
      nsCOMPtr<nsIURI> referrer;
      nsIReferrerInfo* referrerInfo = aLoadState->GetReferrerInfo();
      if (referrerInfo) {
        referrerInfo->GetOriginalReferrer(getter_AddRefs(referrer));
      }
      rv = delegate->ShouldNavigate(this, aLoadState->URI(), referrer,
                                    !!aLoadState->PostDataStream(),
                                    aLoadState->TriggeringPrincipal(),
                                    aLoadState->Csp(), &shouldNavigate);
      if (NS_SUCCEEDED(rv) && !shouldNavigate) {
        return NS_OK;
      }
    }
  }

  // mContentViewer->PermitUnload can destroy |this| docShell, which
  // mContentViewer->PermitUnload can destroy |this| docShell, which
  // causes the next call of CanSavePresentation to crash.
  // causes the next call of CanSavePresentation to crash.
  // Hold onto |this| until we return, to prevent a crash from happening.
  // Hold onto |this| until we return, to prevent a crash from happening.
+19 −0
Original line number Original line Diff line number Diff line
@@ -804,6 +804,15 @@ BrowserChild::FocusPrevElement(bool aForDocumentNavigation) {


NS_IMETHODIMP
NS_IMETHODIMP
BrowserChild::GetInterface(const nsIID& aIID, void** aSink) {
BrowserChild::GetInterface(const nsIID& aIID, void** aSink) {
  if (aIID.Equals(NS_GET_IID(nsITopLevelNavigationDelegate))) {
    nsCOMPtr<nsITopLevelNavigationDelegate> delegate =
        GetTopLevelNavigationDelegate();
    if (delegate) {
      delegate.forget(aSink);
      return NS_OK;
    }
  }

  // XXXbz should we restrict the set of interfaces we hand out here?
  // XXXbz should we restrict the set of interfaces we hand out here?
  // See bug 537429
  // See bug 537429
  return QueryInterface(aIID, aSink);
  return QueryInterface(aIID, aSink);
@@ -3060,6 +3069,16 @@ BrowserChild::GetMessageManager(ContentFrameMessageManager** aResult) {
  return *aResult ? NS_OK : NS_ERROR_FAILURE;
  return *aResult ? NS_OK : NS_ERROR_FAILURE;
}
}


already_AddRefed<nsITopLevelNavigationDelegate>
BrowserChild::GetTopLevelNavigationDelegate() {
  nsCOMPtr<nsIDocShell> docShell = do_GetInterface(WebNavigation());
  if (nsCOMPtr<nsITopLevelNavigationDelegate> delegate = do_QueryActor(
          "TopLevelNavigationDelegate", docShell->GetDocument())) {
    return delegate.forget();
  }
  return nullptr;
}

void BrowserChild::SendRequestFocus(bool aCanFocus, CallerType aCallerType) {
void BrowserChild::SendRequestFocus(bool aCanFocus, CallerType aCallerType) {
  nsFocusManager* fm = nsFocusManager::GetFocusManager();
  nsFocusManager* fm = nsFocusManager::GetFocusManager();
  if (!fm) {
  if (!fm) {
+3 −0
Original line number Original line Diff line number Diff line
@@ -39,6 +39,7 @@
#include "mozilla/layers/CompositorOptions.h"
#include "mozilla/layers/CompositorOptions.h"
#include "mozilla/layers/GeckoContentControllerTypes.h"
#include "mozilla/layers/GeckoContentControllerTypes.h"
#include "nsIWebBrowserChrome.h"
#include "nsIWebBrowserChrome.h"
#include "nsITopLevelNavigationDelegate.h"
#include "mozilla/dom/ipc/IdType.h"
#include "mozilla/dom/ipc/IdType.h"
#include "AudioChannelService.h"
#include "AudioChannelService.h"
#include "PuppetWidget.h"
#include "PuppetWidget.h"
@@ -785,6 +786,8 @@ class BrowserChild final : public nsMessageManagerScriptExecutor,
                                       nsIRequest* aRequest,
                                       nsIRequest* aRequest,
                                       WebProgressData& aWebProgressData,
                                       WebProgressData& aWebProgressData,
                                       RequestData& aRequestData);
                                       RequestData& aRequestData);
  already_AddRefed<nsITopLevelNavigationDelegate>
  GetTopLevelNavigationDelegate();


  MOZ_CAN_RUN_SCRIPT_BOUNDARY
  MOZ_CAN_RUN_SCRIPT_BOUNDARY
  nsresult UpdateRemotePrintSettings(const embedding::PrintData& aPrintData);
  nsresult UpdateRemotePrintSettings(const embedding::PrintData& aPrintData);
+1 −0
Original line number Original line Diff line number Diff line
@@ -11,6 +11,7 @@ DIRS += ["build"]


XPIDL_SOURCES += [
XPIDL_SOURCES += [
    "nsIEmbeddingSiteWindow.idl",
    "nsIEmbeddingSiteWindow.idl",
    "nsITopLevelNavigationDelegate.idl",
    "nsIWebBrowser.idl",
    "nsIWebBrowser.idl",
    "nsIWebBrowserChrome.idl",
    "nsIWebBrowserChrome.idl",
    "nsIWebBrowserChromeFocus.idl",
    "nsIWebBrowserChromeFocus.idl",
+45 −0
Original line number Original line Diff line number Diff line
/* 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 "nsISupports.idl"

interface nsIContentSecurityPolicy;
interface nsIDocShell;
interface nsIPrincipal;
interface nsIURI;

/**
 * An interface for a component that nsIDocShell will look for during
 * top-level navigations to offer an opportunity to cancel the navigation.
 *
 * Interested parties can register the delegate just by defining a
 * JSWindowActorChild that implements this interface.
 */
[scriptable, uuid(bc6a26d5-0346-4062-9ad8-831789e2db5b)]
interface nsITopLevelNavigationDelegate : nsISupports
{
  /**
   * @param nsIDocShell docShell The nsIDocShell performing the navigation.
   * @param nsIURI linkURI The URI that the nsIDocShell is trying to
   *   navigate to.
   * @param nsIURI referrer
   *        The referrer of the load.
   * @param boolean hasPostData
   *        True if the load which is being asked about has associated post data
   *        which would be discarded if the load was cancelled.
   * @param nsIPrincipal triggeringPrincipal
   *        The principal that initiated the load of aURI.
   * @param nsIContentSecurityPolicy csp
   *        The CSP to be used for that load. That is the CSP that e.g. upgrades
   *        the load to HTTPS in case upgrade-insecure-requests is set.
   * @return boolean True if the nsIDocShell should proceed with the
   *   navigation.
   */
  bool shouldNavigate(in nsIDocShell docShell,
                      in nsIURI linkURI,
                      in nsIURI referrer,
                      in boolean hasPostData,
                      in nsIPrincipal triggeringPrincipal,
                      in nsIContentSecurityPolicy csp);
};