Commit fb1cb73f authored by Nika Layzell's avatar Nika Layzell
Browse files

Bug 1798986 - Track UnstrippedURI in SessionHistory, r=smaug,timhuang

Previously, we tracked UnstrippedURI on the nsDocShellLoadState and LoadInfo,
and manually filled it in to match the previous load when doing a
LOAD_CMD_RELOAD in nsDocShell. It is more consistent with other load types to
instead store the information in the load state, allowing it to be handled
consistently for reloads and other history operations.

Unfortunately, this patch has some extra complexity right now, as it needs to
support both SHIP and non-SHIP session history. This should disappear in the
future when we switch to using exclusively SHIP.

Differential Revision: https://phabricator.services.mozilla.com/D161196
parent b3214f51
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
@@ -2073,10 +2073,6 @@ nsresult BrowsingContext::LoadURI(nsDocShellLoadState* aLoadState,
      wgc->SendLoadURI(this, aLoadState, aSetNavigating);
    }
  } else if (XRE_IsParentProcess()) {
    // Strip the target query parameters before loading the URI in the parent.
    // The loading in content will be handled in nsDocShell.
    aLoadState->MaybeStripTrackerQueryStrings(this);

    if (Canonical()->LoadInParent(aLoadState, aSetNavigating)) {
      return NS_OK;
    }
+13 −15
Original line number Diff line number Diff line
@@ -9870,6 +9870,9 @@ nsIPrincipal* nsDocShell::GetInheritedPrincipal(
        isSrcdoc);
  }

  // Strip the target query parameters before creating the channel.
  aLoadState->MaybeStripTrackerQueryStrings(aBrowsingContext);

  OriginAttributes attrs;

  // Inherit origin attributes from PrincipalToInherit if inheritAttrs is
@@ -10540,19 +10543,6 @@ nsresult nsDocShell::DoURILoad(nsDocShellLoadState* aLoadState,
  nsLoadFlags loadFlags = aLoadState->CalculateChannelLoadFlags(
      mBrowsingContext, Some(uriModified), Some(isXFOError));

  // Get the unstripped URI from the current document channel. The unstripped
  // URI will be preserved if it's a reload.
  nsCOMPtr<nsIURI> currentUnstrippedURI;
  nsCOMPtr<nsIChannel> docChannel = GetCurrentDocChannel();
  if (docChannel) {
    nsCOMPtr<nsILoadInfo> docLoadInfo = docChannel->LoadInfo();
    docLoadInfo->GetUnstrippedURI(getter_AddRefs(currentUnstrippedURI));
  }

  // Strip the target query parameters before creating the channel.
  aLoadState->MaybeStripTrackerQueryStrings(mBrowsingContext,
                                            currentUnstrippedURI);

  nsCOMPtr<nsIChannel> channel;
  if (DocumentChannel::CanUseDocumentChannel(aLoadState->URI()) &&
      !isAboutBlankLoadOntoInitialAboutBlank) {
@@ -11571,6 +11561,9 @@ nsresult nsDocShell::UpdateURLAndHistory(Document* aDocument, nsIURI* aNewURI,

    newSHEntry->SetURI(aNewURI);
    newSHEntry->SetOriginalURI(aNewURI);
    // We replaced the URI of the entry, clear the unstripped URI as it
    // shouldn't be used for reloads anymore.
    newSHEntry->SetUnstrippedURI(nullptr);
    // Setting the resultPrincipalURI to nullptr is fine here: it will cause
    // NS_GetFinalChannelURI to use the originalURI as the URI, which is aNewURI
    // in our case.  We could also set it to aNewURI, with the same result.
@@ -11798,6 +11791,7 @@ nsresult nsDocShell::AddToSessionHistory(
  nsCOMPtr<nsIInputStream> inputStream;
  nsCOMPtr<nsIURI> originalURI;
  nsCOMPtr<nsIURI> resultPrincipalURI;
  nsCOMPtr<nsIURI> unstrippedURI;
  bool loadReplace = false;
  nsCOMPtr<nsIReferrerInfo> referrerInfo;
  uint32_t cacheKey = 0;
@@ -11851,6 +11845,8 @@ nsresult nsDocShell::AddToSessionHistory(

    loadInfo->GetResultPrincipalURI(getter_AddRefs(resultPrincipalURI));

    loadInfo->GetUnstrippedURI(getter_AddRefs(unstrippedURI));

    userActivation = loadInfo->GetHasValidUserGestureActivation();

    // For now keep storing just the principal in the SHEntry.
@@ -11920,8 +11916,9 @@ nsresult nsDocShell::AddToSessionHistory(
                triggeringPrincipal,  // Channel or provided principal
                principalToInherit, partitionedPrincipalToInherit, csp,
                HistoryID(), GetCreatedDynamically(), originalURI,
                resultPrincipalURI, loadReplace, referrerInfo, srcdoc,
                srcdocEntry, baseURI, saveLayoutState, expired, userActivation);
                resultPrincipalURI, unstrippedURI, loadReplace, referrerInfo,
                srcdoc, srcdocEntry, baseURI, saveLayoutState, expired,
                userActivation);

  if (mBrowsingContext->IsTop() && GetSessionHistory()) {
    bool shouldPersist = ShouldAddToSessionHistory(aURI, aChannel);
@@ -12006,6 +12003,7 @@ void nsDocShell::UpdateActiveEntry(
        aURI, aTriggeringPrincipal, nullptr, nullptr, aCsp, mContentTypeHint);
  }
  mActiveEntry->SetOriginalURI(aOriginalURI);
  mActiveEntry->SetUnstrippedURI(nullptr);
  mActiveEntry->SetReferrerInfo(aReferrerInfo);
  mActiveEntry->SetTitle(aTitle);
  mActiveEntry->SetStateData(static_cast<nsStructuredCloneContainer*>(aData));
+8 −7
Original line number Diff line number Diff line
@@ -578,7 +578,7 @@ bool nsDocShellLoadState::LoadIsFromSessionHistory() const {
}

void nsDocShellLoadState::MaybeStripTrackerQueryStrings(
    BrowsingContext* aContext, nsIURI* aCurrentUnstrippedURI) {
    BrowsingContext* aContext) {
  MOZ_ASSERT(aContext);

  // Return early if the triggering principal doesn't exist. This could happen
@@ -619,17 +619,14 @@ void nsDocShellLoadState::MaybeStripTrackerQueryStrings(
  uint32_t numStripped = URLQueryStringStripper::Strip(
      URI(), aContext->UsePrivateBrowsing(), strippedURI);
  if (numStripped) {
    if (!mUnstrippedURI) {
      mUnstrippedURI = URI();
    }
    SetURI(strippedURI);

    Telemetry::AccumulateCategorical(
        Telemetry::LABELS_QUERY_STRIPPING_COUNT::StripForNavigation);
    Telemetry::Accumulate(Telemetry::QUERY_STRIPPING_PARAM_COUNT, numStripped);
  } else if (LoadType() & nsIDocShell::LOAD_CMD_RELOAD) {
    // Preserve the Unstripped URI if it's a reload. By doing this, we can
    // restore the stripped query parameters once the ETP has been toggled to
    // off.
    mUnstrippedURI = aCurrentUnstrippedURI;
  }

#ifdef DEBUG
@@ -1076,3 +1073,7 @@ DocShellLoadStateInit nsDocShellLoadState::Serialize() {
}

nsIURI* nsDocShellLoadState::GetUnstrippedURI() const { return mUnstrippedURI; }

void nsDocShellLoadState::SetUnstrippedURI(nsIURI* aUnstrippedURI) {
  mUnstrippedURI = aUnstrippedURI;
}
+3 −2
Original line number Diff line number Diff line
@@ -249,6 +249,8 @@ class nsDocShellLoadState final {

  nsIURI* GetUnstrippedURI() const;

  void SetUnstrippedURI(nsIURI* aUnstrippedURI);

  // Give the type of DocShell we're loading into (chrome/content/etc) and
  // origin attributes for the URI we're loading, figure out if we should
  // inherit our principal from the document the load was requested from, or
@@ -329,8 +331,7 @@ class nsDocShellLoadState final {
  void SetLoadIsFromSessionHistory(int32_t aOffset, bool aLoadingCurrentEntry);
  void ClearLoadIsFromSessionHistory();

  void MaybeStripTrackerQueryStrings(mozilla::dom::BrowsingContext* aContext,
                                     nsIURI* aCurrentUnstrippedURI = nullptr);
  void MaybeStripTrackerQueryStrings(mozilla::dom::BrowsingContext* aContext);

 protected:
  // Destructor can't be defaulted or inlined, as header doesn't have all type
+22 −3
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ SessionHistoryInfo::SessionHistoryInfo(nsDocShellLoadState* aLoadState,
    : mURI(aLoadState->URI()),
      mOriginalURI(aLoadState->OriginalURI()),
      mResultPrincipalURI(aLoadState->ResultPrincipalURI()),
      mUnstrippedURI(aLoadState->GetUnstrippedURI()),
      mLoadType(aLoadState->LoadType()),
      mSrcdocData(aLoadState->SrcdocData().IsVoid()
                      ? Nothing()
@@ -103,6 +104,7 @@ SessionHistoryInfo::SessionHistoryInfo(
  aChannel->GetLoadInfo(getter_AddRefs(loadInfo));

  loadInfo->GetResultPrincipalURI(getter_AddRefs(mResultPrincipalURI));
  loadInfo->GetUnstrippedURI(getter_AddRefs(mUnstrippedURI));
  loadInfo->GetTriggeringPrincipal(
      getter_AddRefs(mSharedState.Get()->mTriggeringPrincipal));
  loadInfo->GetPrincipalToInherit(
@@ -135,6 +137,7 @@ void SessionHistoryInfo::Reset(nsIURI* aURI, const nsID& aDocShellID,
  mURI = aURI;
  mOriginalURI = nullptr;
  mResultPrincipalURI = nullptr;
  mUnstrippedURI = nullptr;
  mReferrerInfo = nullptr;
  // Default title is the URL.
  nsAutoCString spec;
@@ -245,6 +248,7 @@ void SessionHistoryInfo::SetSaveLayoutStateFlag(bool aSaveLayoutStateFlag) {
void SessionHistoryInfo::FillLoadInfo(nsDocShellLoadState& aLoadState) const {
  aLoadState.SetOriginalURI(mOriginalURI);
  aLoadState.SetMaybeResultPrincipalURI(Some(mResultPrincipalURI));
  aLoadState.SetUnstrippedURI(mUnstrippedURI);
  aLoadState.SetLoadReplace(mLoadReplace);
  nsCOMPtr<nsIInputStream> postData = GetPostData();
  aLoadState.SetPostDataStream(postData);
@@ -528,6 +532,19 @@ SessionHistoryEntry::SetResultPrincipalURI(nsIURI* aResultPrincipalURI) {
  return NS_OK;
}

NS_IMETHODIMP
SessionHistoryEntry::GetUnstrippedURI(nsIURI** aUnstrippedURI) {
  nsCOMPtr<nsIURI> unstrippedURI = mInfo->mUnstrippedURI;
  unstrippedURI.forget(aUnstrippedURI);
  return NS_OK;
}

NS_IMETHODIMP
SessionHistoryEntry::SetUnstrippedURI(nsIURI* aUnstrippedURI) {
  mInfo->mUnstrippedURI = aUnstrippedURI;
  return NS_OK;
}

NS_IMETHODIMP
SessionHistoryEntry::GetLoadReplace(bool* aLoadReplace) {
  *aLoadReplace = mInfo->mLoadReplace;
@@ -1049,9 +1066,9 @@ SessionHistoryEntry::Create(
    nsIPrincipal* aPartitionedPrincipalToInherit,
    nsIContentSecurityPolicy* aCsp, const nsID& aDocshellID,
    bool aDynamicCreation, nsIURI* aOriginalURI, nsIURI* aResultPrincipalURI,
    bool aLoadReplace, nsIReferrerInfo* aReferrerInfo, const nsAString& aSrcdoc,
    bool aSrcdocEntry, nsIURI* aBaseURI, bool aSaveLayoutState, bool aExpired,
    bool aUserActivation) {
    nsIURI* aUnstrippedURI, bool aLoadReplace, nsIReferrerInfo* aReferrerInfo,
    const nsAString& aSrcdoc, bool aSrcdocEntry, nsIURI* aBaseURI,
    bool aSaveLayoutState, bool aExpired, bool aUserActivation) {
  MOZ_CRASH("Might need to implement this");
  return NS_ERROR_NOT_IMPLEMENTED;
}
@@ -1513,6 +1530,7 @@ void IPDLParamTraits<dom::SessionHistoryInfo>::Write(
  WriteIPDLParam(aWriter, aActor, aParam.mURI);
  WriteIPDLParam(aWriter, aActor, aParam.mOriginalURI);
  WriteIPDLParam(aWriter, aActor, aParam.mResultPrincipalURI);
  WriteIPDLParam(aWriter, aActor, aParam.mUnstrippedURI);
  WriteIPDLParam(aWriter, aActor, aParam.mReferrerInfo);
  WriteIPDLParam(aWriter, aActor, aParam.mTitle);
  WriteIPDLParam(aWriter, aActor, aParam.mName);
@@ -1554,6 +1572,7 @@ bool IPDLParamTraits<dom::SessionHistoryInfo>::Read(
  if (!ReadIPDLParam(aReader, aActor, &aResult->mURI) ||
      !ReadIPDLParam(aReader, aActor, &aResult->mOriginalURI) ||
      !ReadIPDLParam(aReader, aActor, &aResult->mResultPrincipalURI) ||
      !ReadIPDLParam(aReader, aActor, &aResult->mUnstrippedURI) ||
      !ReadIPDLParam(aReader, aActor, &aResult->mReferrerInfo) ||
      !ReadIPDLParam(aReader, aActor, &aResult->mTitle) ||
      !ReadIPDLParam(aReader, aActor, &aResult->mName) ||
Loading