Skip to content

Commit

Permalink
Bug 1528697 - Expose ReferrerPolicy.webidl and use referrerpolicy enu…
Browse files Browse the repository at this point in the history
…m r=smaug

ReferrerPolicy gets tossed back and forth as a uint32_t and
ReferrerPolicy enum in header file. Expose ReferrerPolicyValues from
webidl file and use consistently in native code.

Differential Revision: https://phabricator.services.mozilla.com/D41954

--HG--
extra : moz-landing-system : lando
  • Loading branch information
Thomas Nguyen committed Aug 21, 2019
1 parent 5723964 commit 32ab829
Show file tree
Hide file tree
Showing 78 changed files with 255 additions and 409 deletions.
3 changes: 1 addition & 2 deletions docshell/base/nsDocShell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
#include "mozilla/dom/nsCSPContext.h"
#include "mozilla/dom/LoadURIOptionsBinding.h"

#include "mozilla/net/ReferrerPolicy.h"
#include "mozilla/net/UrlClassifierFeatureFactory.h"
#include "ReferrerInfo.h"

Expand Down Expand Up @@ -5793,7 +5792,7 @@ nsDocShell::ForceRefreshURI(nsIURI* aURI, nsIPrincipal* aPrincipal,
* For most refreshes the current URI is an appropriate
* internal referrer.
*/
referrerInfo = new ReferrerInfo(mCurrentURI, mozilla::net::RP_Unset, false);
referrerInfo = new ReferrerInfo(mCurrentURI, ReferrerPolicy::_empty, false);
}

loadState->SetReferrerInfo(referrerInfo);
Expand Down
1 change: 0 additions & 1 deletion docshell/base/nsDocShellLoadState.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "nsCOMPtr.h"
#include "nsString.h"
#include "nsDocShellLoadTypes.h"
#include "mozilla/net/ReferrerPolicy.h"

class nsIContentSecurityPolicy;
class nsIInputStream;
Expand Down
1 change: 0 additions & 1 deletion docshell/shistory/nsSHEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "nsSHEntryShared.h"
#include "nsSHistory.h"

#include "mozilla/net/ReferrerPolicy.h"
#include "mozilla/Logging.h"
#include "nsIReferrerInfo.h"

Expand Down
26 changes: 11 additions & 15 deletions dom/base/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3337,7 +3337,7 @@ nsresult Document::InitReferrerInfo(nsIChannel* aChannel) {
}

// Override policy if we get one from Referrerr-Policy header
mozilla::net::ReferrerPolicy policy =
mozilla::dom::ReferrerPolicy policy =
nsContentUtils::GetReferrerPolicyFromChannel(aChannel);
mReferrerInfo = static_cast<dom::ReferrerInfo*>(mReferrerInfo.get())
->CloneWithNewPolicy(policy);
Expand Down Expand Up @@ -5570,13 +5570,9 @@ already_AddRefed<nsIChannel> Document::CreateDummyChannelForCookies(
return channel.forget();
}

mozilla::net::ReferrerPolicy Document::GetReferrerPolicy() const {
if (!mReferrerInfo) {
return mozilla::net::RP_Unset;
}

return static_cast<mozilla::net::ReferrerPolicy>(
mReferrerInfo->GetReferrerPolicy());
ReferrerPolicy Document::GetReferrerPolicy() const {
return mReferrerInfo ? mReferrerInfo->ReferrerPolicy()
: ReferrerPolicy::_empty;
}

void Document::GetAlinkColor(nsAString& aAlinkColor) {
Expand Down Expand Up @@ -11306,9 +11302,9 @@ already_AddRefed<nsIURI> Document::ResolvePreloadImage(
return uri.forget();
}

void Document::MaybePreLoadImage(
nsIURI* uri, const nsAString& aCrossOriginAttr,
enum mozilla::net::ReferrerPolicy aReferrerPolicy, bool aIsImgSet) {
void Document::MaybePreLoadImage(nsIURI* uri, const nsAString& aCrossOriginAttr,
enum ReferrerPolicy aReferrerPolicy,
bool aIsImgSet) {
// Early exit if the img is already present in the img-cache
// which indicates that the "real" load has already started and
// that we shouldn't preload it.
Expand Down Expand Up @@ -11444,10 +11440,10 @@ NS_IMPL_ISUPPORTS(StubCSSLoaderObserver, nsICSSLoaderObserver)

} // namespace

void Document::PreloadStyle(
nsIURI* uri, const Encoding* aEncoding, const nsAString& aCrossOriginAttr,
const enum mozilla::net::ReferrerPolicy aReferrerPolicy,
const nsAString& aIntegrity) {
void Document::PreloadStyle(nsIURI* uri, const Encoding* aEncoding,
const nsAString& aCrossOriginAttr,
const enum ReferrerPolicy aReferrerPolicy,
const nsAString& aIntegrity) {
// The CSSLoader will retain this object after we return.
nsCOMPtr<nsICSSLoaderObserver> obs = new StubCSSLoaderObserver();

Expand Down
9 changes: 4 additions & 5 deletions dom/base/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#include "nsStubMutationObserver.h"
#include "nsTHashtable.h" // for member
#include "nsURIHashKey.h"
#include "mozilla/net/ReferrerPolicy.h" // for member
#include "mozilla/UseCounter.h"
#include "mozilla/WeakPtr.h"
#include "mozilla/StaticPresData.h"
Expand Down Expand Up @@ -478,7 +477,7 @@ class Document : public nsINode,

public:
typedef dom::ExternalResourceMap::ExternalResourceLoad ExternalResourceLoad;
typedef net::ReferrerPolicy ReferrerPolicyEnum;
typedef dom::ReferrerPolicy ReferrerPolicyEnum;

/**
* Called when XPCOM shutdown.
Expand Down Expand Up @@ -775,7 +774,7 @@ class Document : public nsINode,
/**
* GetReferrerPolicy() for Document.webidl.
*/
uint32_t ReferrerPolicy() const { return GetReferrerPolicy(); }
ReferrerPolicyEnum ReferrerPolicy() const { return GetReferrerPolicy(); }

/**
* If true, this flag indicates that all mixed content subresource
Expand Down Expand Up @@ -814,11 +813,11 @@ class Document : public nsINode,
*/
void UpdateReferrerInfoFromMeta(const nsAString& aMetaReferrer,
bool aPreload) {
net::ReferrerPolicy policy =
ReferrerPolicyEnum policy =
mozilla::net::ReferrerPolicyFromString(aMetaReferrer);
// The empty string "" corresponds to no referrer policy, causing a fallback
// to a referrer policy defined elsewhere.
if (policy == mozilla::net::RP_Unset) {
if (policy == ReferrerPolicy::_empty) {
return;
}

Expand Down
10 changes: 5 additions & 5 deletions dom/base/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3828,19 +3828,19 @@ float Element::FontSizeInflation() {
return 1.0;
}

net::ReferrerPolicy Element::GetReferrerPolicyAsEnum() {
ReferrerPolicy Element::GetReferrerPolicyAsEnum() {
if (IsHTMLElement()) {
const nsAttrValue* referrerValue = GetParsedAttr(nsGkAtoms::referrerpolicy);
return ReferrerPolicyFromAttr(referrerValue);
}
return net::RP_Unset;
return ReferrerPolicy::_empty;
}

net::ReferrerPolicy Element::ReferrerPolicyFromAttr(const nsAttrValue* aValue) {
ReferrerPolicy Element::ReferrerPolicyFromAttr(const nsAttrValue* aValue) {
if (aValue && aValue->Type() == nsAttrValue::eEnum) {
return net::ReferrerPolicy(aValue->GetEnumValue());
return ReferrerPolicy(aValue->GetEnumValue());
}
return net::RP_Unset;
return ReferrerPolicy::_empty;
}

already_AddRefed<nsDOMStringMap> Element::Dataset() {
Expand Down
4 changes: 2 additions & 2 deletions dom/base/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -1588,8 +1588,8 @@ class Element : public FragmentOrElement {
*/
float FontSizeInflation();

net::ReferrerPolicy GetReferrerPolicyAsEnum();
net::ReferrerPolicy ReferrerPolicyFromAttr(const nsAttrValue* aValue);
ReferrerPolicy GetReferrerPolicyAsEnum();
ReferrerPolicy ReferrerPolicyFromAttr(const nsAttrValue* aValue);

/*
* Helpers for .dataset. This is implemented on Element, though only some
Expand Down
2 changes: 0 additions & 2 deletions dom/base/IdentifierMapEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "mozilla/MemoryReporting.h"
#include "mozilla/Move.h"
#include "mozilla/dom/TreeOrderedArray.h"
#include "mozilla/net/ReferrerPolicy.h"

#include "nsCOMPtr.h"
#include "nsAtom.h"
Expand Down Expand Up @@ -49,7 +48,6 @@ class Element;
class IdentifierMapEntry : public PLDHashEntryHdr {
typedef dom::Document Document;
typedef dom::Element Element;
typedef net::ReferrerPolicy ReferrerPolicy;

/**
* @see Document::IDTargetObserver, this is just here to avoid include hell.
Expand Down
2 changes: 1 addition & 1 deletion dom/base/Location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ already_AddRefed<nsDocShellLoadState> Location::CheckURL(

nsCOMPtr<nsIPrincipal> triggeringPrincipal;
nsCOMPtr<nsIURI> sourceURI;
net::ReferrerPolicy referrerPolicy = net::RP_Unset;
ReferrerPolicy referrerPolicy = ReferrerPolicy::_empty;

// Get security manager.
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
Expand Down
14 changes: 7 additions & 7 deletions dom/base/nsContentUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7882,40 +7882,40 @@ bool nsContentUtils::IsUpgradableDisplayType(nsContentPolicyType aType) {
}

// static
net::ReferrerPolicy nsContentUtils::GetReferrerPolicyFromHeader(
ReferrerPolicy nsContentUtils::GetReferrerPolicyFromHeader(
const nsAString& aHeader) {
// Multiple headers could be concatenated into one comma-separated
// list of policies. Need to tokenize the multiple headers.
nsCharSeparatedTokenizer tokenizer(aHeader, ',');
nsAutoString token;
net::ReferrerPolicy referrerPolicy = mozilla::net::RP_Unset;
ReferrerPolicy referrerPolicy = ReferrerPolicy::_empty;
while (tokenizer.hasMoreTokens()) {
token = tokenizer.nextToken();
if (token.IsEmpty()) {
continue;
}
net::ReferrerPolicy policy = net::ReferrerPolicyFromString(token);
if (policy != net::RP_Unset) {
ReferrerPolicy policy = net::ReferrerPolicyFromString(token);
if (policy != ReferrerPolicy::_empty) {
referrerPolicy = policy;
}
}
return referrerPolicy;
}

// static
net::ReferrerPolicy nsContentUtils::GetReferrerPolicyFromChannel(
ReferrerPolicy nsContentUtils::GetReferrerPolicyFromChannel(
nsIChannel* aChannel) {
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aChannel);
if (!httpChannel) {
return net::RP_Unset;
return ReferrerPolicy::_empty;
}

nsresult rv;
nsAutoCString headerValue;
rv = httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("referrer-policy"),
headerValue);
if (NS_FAILED(rv) || headerValue.IsEmpty()) {
return net::RP_Unset;
return ReferrerPolicy::_empty;
}

return GetReferrerPolicyFromHeader(NS_ConvertUTF8toUTF16(headerValue));
Expand Down
5 changes: 2 additions & 3 deletions dom/base/nsContentUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include "mozilla/FloatingPoint.h"
#include "mozilla/intl/LineBreaker.h"
#include "mozilla/intl/WordBreaker.h"
#include "mozilla/net/ReferrerPolicy.h"
#include "mozilla/Logging.h"
#include "mozilla/NotNull.h"
#include "mozilla/Maybe.h"
Expand Down Expand Up @@ -2839,7 +2838,7 @@ class nsContentUtils {
* @param the channel from which to get the Referrer-Policy header
* @return referrer policy from the response header in aChannel
*/
static mozilla::net::ReferrerPolicy GetReferrerPolicyFromChannel(
static mozilla::dom::ReferrerPolicy GetReferrerPolicyFromChannel(
nsIChannel* aChannel);

/*
Expand All @@ -2849,7 +2848,7 @@ class nsContentUtils {
* @param aHeader the response's Referrer-Policy header to parse
* @return referrer policy from the response header.
*/
static mozilla::net::ReferrerPolicy GetReferrerPolicyFromHeader(
static mozilla::dom::ReferrerPolicy GetReferrerPolicyFromHeader(
const nsAString& aHeader);

static bool IsNonSubresourceRequest(nsIChannel* aChannel);
Expand Down
6 changes: 3 additions & 3 deletions dom/base/nsImageLoadingContent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,7 @@ nsresult nsImageLoadingContent::LoadImage(nsIURI* aNewURI, bool aForce,
do_QueryInterface(static_cast<nsIImageLoadingContent*>(this));
nsCOMPtr<nsIReferrerInfo> referrerInfo = new ReferrerInfo();
referrerInfo->InitWithNode(thisNode);
nsCOMPtr<nsIURI> referrer = referrerInfo->GetOriginalReferrer();

nsresult rv = nsContentUtils::LoadImage(
aNewURI, thisNode, aDocument, triggeringPrincipal, 0, referrerInfo, this,
loadFlags, content->LocalName(), getter_AddRefs(req), policyType,
Expand Down Expand Up @@ -1794,8 +1794,8 @@ bool nsImageLoadingContent::ScriptedImageObserver::CancelRequests() {

// Only HTMLInputElement.h overrides this for <img> tags
// all other subclasses use this one, i.e. ignore referrer attributes
mozilla::net::ReferrerPolicy nsImageLoadingContent::GetImageReferrerPolicy() {
return mozilla::net::RP_Unset;
mozilla::dom::ReferrerPolicy nsImageLoadingContent::GetImageReferrerPolicy() {
return mozilla::dom::ReferrerPolicy::_empty;
}

Element* nsImageLoadingContent::FindImageMap() {
Expand Down
3 changes: 1 addition & 2 deletions dom/base/nsImageLoadingContent.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "nsIContentPolicy.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/net/ReferrerPolicy.h"
#include "nsAttrValue.h"

class nsIURI;
Expand Down Expand Up @@ -214,7 +213,7 @@ class nsImageLoadingContent : public nsIImageLoadingContent {
*/
virtual mozilla::CORSMode GetCORSMode();

virtual mozilla::net::ReferrerPolicy GetImageReferrerPolicy();
virtual mozilla::dom::ReferrerPolicy GetImageReferrerPolicy();

// Subclasses are *required* to call BindToTree/UnbindFromTree.
void BindToTree(mozilla::dom::BindContext&, nsINode& aParent);
Expand Down
1 change: 0 additions & 1 deletion dom/base/nsOpenURIInFrameParams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "mozilla/BasePrincipal.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/ToJSValue.h"
#include "mozilla/net/ReferrerPolicy.h"

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsOpenURIInFrameParams)
NS_INTERFACE_MAP_ENTRY(nsIOpenURIInFrameParams)
Expand Down
2 changes: 1 addition & 1 deletion dom/base/nsSyncLoadService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
using namespace mozilla;
using namespace mozilla::dom;

using mozilla::net::ReferrerPolicy;
using mozilla::dom::ReferrerPolicy;

/**
* This class manages loading a single XML document
Expand Down
3 changes: 1 addition & 2 deletions dom/base/nsSyncLoadService.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#define nsSyncLoadService_h__

#include "nscore.h"
#include "mozilla/net/ReferrerPolicy.h"

class nsICookieSettings;
class nsIInputStream;
Expand Down Expand Up @@ -49,7 +48,7 @@ class nsSyncLoadService {
nsIURI* aURI, nsContentPolicyType aContentPolicyType,
nsIPrincipal* aLoaderPrincipal, nsSecurityFlags aSecurityFlags,
nsILoadGroup* aLoadGroup, nsICookieSettings* aCookieSettings,
bool aForceToXML, mozilla::net::ReferrerPolicy aReferrerPolicy,
bool aForceToXML, mozilla::dom::ReferrerPolicy aReferrerPolicy,
mozilla::dom::Document** aResult);

/**
Expand Down
12 changes: 5 additions & 7 deletions dom/fetch/FetchDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,19 +579,18 @@ nsresult FetchDriver::HttpFetch(
// associated referrer policy.
// Basically, "client" is not in our implementation, we use
// EnvironmentReferrerPolicy of the worker or document context
net::ReferrerPolicy net_referrerPolicy =
mRequest->GetEnvironmentReferrerPolicy();
ReferrerPolicy referrerPolicy = mRequest->GetEnvironmentReferrerPolicy();
if (mRequest->ReferrerPolicy_() == ReferrerPolicy::_empty) {
mRequest->SetReferrerPolicy(net_referrerPolicy);
mRequest->SetReferrerPolicy(referrerPolicy);
}
// Step 6 of https://fetch.spec.whatwg.org/#main-fetch
// If request’s referrer policy is the empty string,
// then set request’s referrer policy to the user-set default policy.
if (mRequest->ReferrerPolicy_() == ReferrerPolicy::_empty) {
nsCOMPtr<nsILoadInfo> loadInfo = httpChan->LoadInfo();
bool isPrivate = loadInfo->GetOriginAttributes().mPrivateBrowsingId > 0;
net::ReferrerPolicy referrerPolicy = static_cast<net::ReferrerPolicy>(
ReferrerInfo::GetDefaultReferrerPolicy(httpChan, uri, isPrivate));
referrerPolicy =
ReferrerInfo::GetDefaultReferrerPolicy(httpChan, uri, isPrivate);
mRequest->SetReferrerPolicy(referrerPolicy);
}

Expand Down Expand Up @@ -1351,8 +1350,7 @@ FetchDriver::AsyncOnChannelRedirect(nsIChannel* aOldChannel,
nsAutoString computedReferrerSpec;
nsCOMPtr<nsIReferrerInfo> referrerInfo = httpChannel->GetReferrerInfo();
if (referrerInfo) {
mRequest->SetReferrerPolicy(
static_cast<net::ReferrerPolicy>(referrerInfo->GetReferrerPolicy()));
mRequest->SetReferrerPolicy(referrerInfo->ReferrerPolicy());
Unused << referrerInfo->GetComputedReferrerSpec(computedReferrerSpec);
}

Expand Down
1 change: 0 additions & 1 deletion dom/fetch/FetchDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "mozilla/RefPtr.h"

#include "mozilla/DebugOnly.h"
#include "mozilla/net/ReferrerPolicy.h"

class nsIConsoleReportCollector;
class nsICookieSettings;
Expand Down
4 changes: 2 additions & 2 deletions dom/fetch/FetchUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ nsresult FetchUtil::SetRequestReferrer(nsIPrincipal* aPrincipal, Document* aDoc,
nsAutoString referrer;
aRequest->GetReferrer(referrer);

net::ReferrerPolicy policy = aRequest->GetReferrerPolicy();
ReferrerPolicy policy = aRequest->ReferrerPolicy_();
nsCOMPtr<nsIReferrerInfo> referrerInfo;
if (referrer.IsEmpty()) {
// This is the case request’s referrer is "no-referrer"
referrerInfo = new ReferrerInfo(nullptr, net::RP_No_Referrer);
referrerInfo = new ReferrerInfo(nullptr, ReferrerPolicy::No_referrer);
} else if (referrer.EqualsLiteral(kFETCH_CLIENT_REFERRER_STR)) {
referrerInfo = ReferrerInfo::CreateForFetch(aPrincipal, aDoc);
// In the first step, we should use referrer info from requetInit
Expand Down
Loading

0 comments on commit 32ab829

Please sign in to comment.