[tbb-commits] [tor-browser/tor-browser-60.4.0esr-8.5-1] Bug 1473509 - store principal information with the URIs to avoid having to locate documents after the fact, r=mccr8

gk at torproject.org gk at torproject.org
Wed Jan 23 08:04:19 UTC 2019


commit 846b2530517793843a0006c41b15ff41b47a6f79
Author: Gijs Kruitbosch <gijskruitbosch at gmail.com>
Date:   Fri Jul 6 17:27:17 2018 +0100

    Bug 1473509 - store principal information with the URIs to avoid having to locate documents after the fact, r=mccr8
    
    The way we currently save resource URIs that we discovered by walking the document
    isn't guaranteed to be in-order with when we save the documents they came from.
    To deduce the correct principal to use to load these subresources, we need to
    store it with the resource in the map of URIs we work with.
    
    MozReview-Commit-ID: GNlNuS6TuVV
    
    --HG--
    extra : rebase_source : 99982f91971423730aa8661cf66c2dd14e276780
---
 dom/webbrowserpersist/nsWebBrowserPersist.cpp | 33 +++++++++++++--------------
 dom/webbrowserpersist/nsWebBrowserPersist.h   |  5 +++-
 2 files changed, 20 insertions(+), 18 deletions(-)

diff --git a/dom/webbrowserpersist/nsWebBrowserPersist.cpp b/dom/webbrowserpersist/nsWebBrowserPersist.cpp
index 12e6c41c37d8..68cb08eac0c0 100644
--- a/dom/webbrowserpersist/nsWebBrowserPersist.cpp
+++ b/dom/webbrowserpersist/nsWebBrowserPersist.cpp
@@ -84,7 +84,6 @@ struct nsWebBrowserPersist::DocData
     nsCOMPtr<nsIURI> mBaseURI;
     nsCOMPtr<nsIWebBrowserPersistDocument> mDocument;
     nsCOMPtr<nsIURI> mFile;
-    nsCOMPtr<nsIPrincipal> mPrincipal;
     nsCString mCharset;
 };
 
@@ -101,6 +100,7 @@ struct nsWebBrowserPersist::URIData
     nsCOMPtr<nsIURI> mFile;
     nsCOMPtr<nsIURI> mDataPath;
     nsCOMPtr<nsIURI> mRelativeDocumentURI;
+    nsCOMPtr<nsIPrincipal> mTriggeringPrincipal;
     nsCString mRelativePathToData;
     nsCString mCharset;
 
@@ -605,12 +605,6 @@ nsWebBrowserPersist::SerializeNextFile()
     }
 
     if (urisToPersist > 0) {
-        nsCOMPtr<nsIPrincipal> docPrincipal;
-        //XXXgijs I *think* this is already always true, but let's be sure.
-        MOZ_ASSERT(mDocList.Length() > 0,
-            "Should have the document for any walked URIs to persist!");
-        nsresult rv = mDocList.ElementAt(0)->mDocument->
-            GetPrincipal(getter_AddRefs(docPrincipal));
         NS_ENSURE_SUCCESS_VOID(rv);
         // Persist each file in the uri map. The document(s)
         // will be saved after the last one of these is saved.
@@ -642,7 +636,7 @@ nsWebBrowserPersist::SerializeNextFile()
 
             // The Referrer Policy doesn't matter here since the referrer is
             // nullptr.
-            rv = SaveURIInternal(uri, docPrincipal, nullptr, nullptr,
+            rv = SaveURIInternal(uri, data->mTriggeringPrincipal, nullptr, nullptr,
                                  mozilla::net::RP_Unset, nullptr, nullptr,
                                  fileAsURI, true, mIsPrivate);
             // If SaveURIInternal fails, then it will have called EndDownload,
@@ -1749,7 +1743,7 @@ NS_IMETHODIMP
 nsWebBrowserPersist::OnWalk::VisitResource(nsIWebBrowserPersistDocument* aDoc,
                                            const nsACString& aURI)
 {
-    return mParent->StoreURI(nsAutoCString(aURI).get());
+    return mParent->StoreURI(nsAutoCString(aURI).get(), aDoc);
 }
 
 NS_IMETHODIMP
@@ -1760,14 +1754,14 @@ nsWebBrowserPersist::OnWalk::VisitDocument(nsIWebBrowserPersistDocument* aDoc,
     nsAutoCString uriSpec;
     nsresult rv = aSubDoc->GetDocumentURI(uriSpec);
     NS_ENSURE_SUCCESS(rv, rv);
-    rv = mParent->StoreURI(uriSpec.get(), false, &data);
+    rv = mParent->StoreURI(uriSpec.get(), aDoc, false, &data);
     NS_ENSURE_SUCCESS(rv, rv);
     if (!data) {
         // If the URI scheme isn't persistable, then don't persist.
         return NS_OK;
     }
     data->mIsSubFrame = true;
-    return mParent->SaveSubframeContent(aSubDoc, uriSpec, data);
+    return mParent->SaveSubframeContent(aSubDoc, aDoc, uriSpec, data);
 }
 
 
@@ -2513,7 +2507,8 @@ nsWebBrowserPersist::CalcTotalProgress()
 
 nsresult
 nsWebBrowserPersist::StoreURI(
-    const char *aURI, bool aNeedsPersisting, URIData **aData)
+    const char *aURI, nsIWebBrowserPersistDocument *aDoc,
+    bool aNeedsPersisting, URIData **aData)
 {
     NS_ENSURE_ARG_POINTER(aURI);
 
@@ -2524,12 +2519,13 @@ nsWebBrowserPersist::StoreURI(
                             mCurrentBaseURI);
     NS_ENSURE_SUCCESS(rv, rv);
 
-    return StoreURI(uri, aNeedsPersisting, aData);
+    return StoreURI(uri, aDoc, aNeedsPersisting, aData);
 }
 
 nsresult
 nsWebBrowserPersist::StoreURI(
-    nsIURI *aURI, bool aNeedsPersisting, URIData **aData)
+    nsIURI *aURI, nsIWebBrowserPersistDocument *aDoc,
+    bool aNeedsPersisting, URIData **aData)
 {
     NS_ENSURE_ARG_POINTER(aURI);
     if (aData)
@@ -2554,7 +2550,7 @@ nsWebBrowserPersist::StoreURI(
     }
 
     URIData *data = nullptr;
-    MakeAndStoreLocalFilenameInURIMap(aURI, aNeedsPersisting, &data);
+    MakeAndStoreLocalFilenameInURIMap(aURI, aDoc, aNeedsPersisting, &data);
     if (aData)
     {
         *aData = data;
@@ -2661,6 +2657,7 @@ nsWebBrowserPersist::DocumentEncoderExists(const char *aContentType)
 nsresult
 nsWebBrowserPersist::SaveSubframeContent(
     nsIWebBrowserPersistDocument *aFrameContent,
+    nsIWebBrowserPersistDocument *aParentDocument,
     const nsCString& aURISpec,
     URIData *aData)
 {
@@ -2738,7 +2735,7 @@ nsWebBrowserPersist::SaveSubframeContent(
         toWalk->mDataPath = frameDataURI;
         mWalkStack.AppendElement(mozilla::Move(toWalk));
     } else {
-        rv = StoreURI(aURISpec.get());
+        rv = StoreURI(aURISpec.get(), aParentDocument);
     }
     NS_ENSURE_SUCCESS(rv, rv);
 
@@ -2772,7 +2769,7 @@ nsWebBrowserPersist::CreateChannelFromURI(nsIURI *aURI, nsIChannel **aChannel)
 // we store the current location as the key (absolutized version of domnode's attribute's value)
 nsresult
 nsWebBrowserPersist::MakeAndStoreLocalFilenameInURIMap(
-    nsIURI *aURI, bool aNeedsPersisting, URIData **aData)
+    nsIURI *aURI, nsIWebBrowserPersistDocument *aDoc, bool aNeedsPersisting, URIData **aData)
 {
     NS_ENSURE_ARG_POINTER(aURI);
 
@@ -2815,6 +2812,8 @@ nsWebBrowserPersist::MakeAndStoreLocalFilenameInURIMap(
     data->mRelativeDocumentURI = mTargetBaseURI;
     data->mCharset = mCurrentCharset;
 
+    aDoc->GetPrincipal(getter_AddRefs(data->mTriggeringPrincipal));
+
     if (aNeedsPersisting)
         mCurrentThingsToPersist++;
 
diff --git a/dom/webbrowserpersist/nsWebBrowserPersist.h b/dom/webbrowserpersist/nsWebBrowserPersist.h
index b9a2e3da73b6..f2cc91ee336d 100644
--- a/dom/webbrowserpersist/nsWebBrowserPersist.h
+++ b/dom/webbrowserpersist/nsWebBrowserPersist.h
@@ -94,7 +94,7 @@ private:
     static nsresult GetLocalFileFromURI(nsIURI *aURI, nsIFile **aLocalFile);
     static nsresult AppendPathToURI(nsIURI *aURI, const nsAString & aPath, nsCOMPtr<nsIURI>& aOutURI);
     nsresult MakeAndStoreLocalFilenameInURIMap(
-        nsIURI *aURI, bool aNeedsPersisting, URIData **aData);
+        nsIURI *aURI, nsIWebBrowserPersistDocument *aDoc, bool aNeedsPersisting, URIData **aData);
     nsresult MakeOutputStream(
         nsIURI *aFile, nsIOutputStream **aOutputStream);
     nsresult MakeOutputStreamFromFile(
@@ -113,16 +113,19 @@ private:
         nsIURI *aURI, nsString &aFilename);
     nsresult StoreURI(
         const char *aURI,
+        nsIWebBrowserPersistDocument *aDoc,
         bool aNeedsPersisting = true,
         URIData **aData = nullptr);
     nsresult StoreURI(
         nsIURI *aURI,
+        nsIWebBrowserPersistDocument *aDoc,
         bool aNeedsPersisting = true,
         URIData **aData = nullptr);
     bool DocumentEncoderExists(const char *aContentType);
 
     nsresult SaveSubframeContent(
         nsIWebBrowserPersistDocument *aFrameContent,
+        nsIWebBrowserPersistDocument *aParentDocument,
         const nsCString& aURISpec,
         URIData *aData);
     nsresult SendErrorStatusChange(





More information about the tbb-commits mailing list