Skip to content

Commit

Permalink
Bug 1714645: Use nsSimpleURI instead of NullPrincipalURI. r=ckerschb,…
Browse files Browse the repository at this point in the history
  • Loading branch information
Niklas Goegge committed Jul 6, 2021
1 parent 1c8bf5c commit 5f20350
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
26 changes: 24 additions & 2 deletions caps/NullPrincipal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
#include "mozilla/ArrayUtils.h"

#include "mozilla/dom/BlobURLProtocolHandler.h"
#include "mozilla/StaticPrefs_network.h"
#include "nsDocShell.h"
#include "NullPrincipal.h"
#include "NullPrincipalURI.h"
#include "DefaultURI.h"
#include "nsSimpleURI.h"
#include "nsMemory.h"
#include "nsIClassInfoImpl.h"
#include "nsNetCID.h"
Expand All @@ -24,6 +26,7 @@
#include "nsScriptSecurityManager.h"
#include "pratom.h"
#include "nsIObjectInputStream.h"
#include "mozilla/GkRustUtils.h"

#include "json/json.h"

Expand Down Expand Up @@ -73,13 +76,32 @@ already_AddRefed<NullPrincipal> NullPrincipal::CreateWithoutOriginAttributes() {
return NullPrincipal::Create(OriginAttributes(), nullptr);
}

already_AddRefed<nsIURI> NullPrincipal::CreateURI() {
nsCOMPtr<nsIURIMutator> mutator;
if (StaticPrefs::network_url_useDefaultURI()) {
mutator = new mozilla::net::DefaultURI::Mutator();
} else {
mutator = new mozilla::net::nsSimpleURI::Mutator();
}

nsAutoCStringN<NSID_LENGTH> uuid;
GkRustUtils::GenerateUUID(uuid);

nsCOMPtr<nsIURI> uri;
MOZ_ALWAYS_SUCCEEDS(NS_MutateURI(mutator)
.SetSpec(NS_NULLPRINCIPAL_SCHEME ":"_ns + uuid)
.Finalize(uri));
return uri.forget();
}

already_AddRefed<NullPrincipal> NullPrincipal::CreateInternal(
const OriginAttributes& aOriginAttributes, bool aIsFirstParty,
nsIURI* aURI) {
nsCOMPtr<nsIURI> uri = aURI;
if (!uri) {
uri = new NullPrincipalURI();
uri = NullPrincipal::CreateURI();
}

MOZ_RELEASE_ASSERT(uri->SchemeIs(NS_NULLPRINCIPAL_SCHEME));

nsAutoCString originNoSuffix;
Expand Down
2 changes: 2 additions & 0 deletions caps/NullPrincipal.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class NullPrincipal final : public BasePrincipal {

static already_AddRefed<NullPrincipal> CreateWithoutOriginAttributes();

static already_AddRefed<nsIURI> CreateURI();

virtual nsresult GetScriptLocation(nsACString& aStr) override;

nsresult GetSiteIdentifier(SiteIdentifier& aSite) override {
Expand Down
9 changes: 4 additions & 5 deletions dom/base/DOMParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "mozilla/BasePrincipal.h"
#include "mozilla/LoadInfo.h"
#include "mozilla/NullPrincipal.h"
#include "NullPrincipalURI.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/ScriptSettings.h"
Expand Down Expand Up @@ -252,8 +251,8 @@ already_AddRefed<DOMParser> DOMParser::Constructor(const GlobalObject& aOwner,
nsCOMPtr<nsIURI> documentURI;
nsIURI* baseURI = nullptr;
if (docPrincipal->IsSystemPrincipal()) {
documentURI = new NullPrincipalURI();
docPrincipal = NullPrincipal::Create(OriginAttributes(), documentURI);
docPrincipal = NullPrincipal::Create(OriginAttributes());
documentURI = docPrincipal->GetURI();
} else {
// Grab document and base URIs off the window our constructor was
// called on. Error out if anything untoward happens.
Expand Down Expand Up @@ -282,10 +281,10 @@ already_AddRefed<DOMParser> DOMParser::Constructor(const GlobalObject& aOwner,

// static
already_AddRefed<DOMParser> DOMParser::CreateWithoutGlobal(ErrorResult& aRv) {
nsCOMPtr<nsIURI> documentURI = new NullPrincipalURI();
nsCOMPtr<nsIPrincipal> docPrincipal =
NullPrincipal::Create(OriginAttributes(), documentURI);
NullPrincipal::Create(OriginAttributes());

nsCOMPtr<nsIURI> documentURI = docPrincipal->GetURI();
if (!documentURI) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
Expand Down
3 changes: 1 addition & 2 deletions layout/style/URLExtraData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "mozilla/URLExtraData.h"

#include "mozilla/NullPrincipalURI.h"
#include "nsProxyRelease.h"
#include "ReferrerInfo.h"

Expand All @@ -19,7 +18,7 @@ StaticRefPtr<URLExtraData> URLExtraData::sDummyChrome;

/* static */
void URLExtraData::Init() {
RefPtr<nsIURI> baseURI = new NullPrincipalURI();
RefPtr<nsIURI> baseURI = NullPrincipal::CreateURI();
nsCOMPtr<nsIReferrerInfo> referrerInfo = new dom::ReferrerInfo(nullptr);
sDummy = new URLExtraData(do_AddRef(baseURI), do_AddRef(referrerInfo),
NullPrincipal::CreateWithoutOriginAttributes());
Expand Down
8 changes: 4 additions & 4 deletions layout/style/test/gtest/StyloParsingBench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "mozilla/dom/DOMString.h"
#include "mozilla/Encoding.h"
#include "mozilla/Utf8.h"
#include "mozilla/NullPrincipalURI.h"
#include "mozilla/NullPrincipal.h"
#include "mozilla/css/SheetParsingMode.h"
#include "ReferrerInfo.h"
#include "nsCSSValue.h"
Expand All @@ -36,7 +36,7 @@ static void ServoParsingBench(const StyleUseCounters* aCounters) {
cssStr.Append(css);
ASSERT_EQ(Encoding::UTF8ValidUpTo(css), css.Length());

RefPtr<NullPrincipalURI> uri = new NullPrincipalURI();
RefPtr<nsIURI> uri = NullPrincipal::CreateURI();
nsCOMPtr<nsIReferrerInfo> referrerInfo = new ReferrerInfo(nullptr);
RefPtr<URLExtraData> data =
new URLExtraData(uri.forget(), referrerInfo.forget(),
Expand All @@ -56,7 +56,7 @@ static constexpr uint16_t STYLE_RULE = 1;
static void ServoSetPropertyByIdBench(const nsACString& css) {
RefPtr<RawServoDeclarationBlock> block =
Servo_DeclarationBlock_CreateEmpty().Consume();
RefPtr<NullPrincipalURI> uri = new NullPrincipalURI();
RefPtr<nsIURI> uri = NullPrincipal::CreateURI();
nsCOMPtr<nsIReferrerInfo> referrerInfo = new ReferrerInfo(nullptr);
RefPtr<URLExtraData> data =
new URLExtraData(uri.forget(), referrerInfo.forget(),
Expand All @@ -75,7 +75,7 @@ static void ServoGetPropertyValueById() {
RefPtr<RawServoDeclarationBlock> block =
Servo_DeclarationBlock_CreateEmpty().Consume();

RefPtr<NullPrincipalURI> uri = new NullPrincipalURI();
RefPtr<nsIURI> uri = NullPrincipal::CreateURI();
nsCOMPtr<nsIReferrerInfo> referrerInfo = new ReferrerInfo(nullptr);
RefPtr<URLExtraData> data =
new URLExtraData(uri.forget(), referrerInfo.forget(),
Expand Down

0 comments on commit 5f20350

Please sign in to comment.