diff --git a/dom/base/EventSource.cpp b/dom/base/EventSource.cpp index 5747e7d874d6..c99a3351c679 100644 --- a/dom/base/EventSource.cpp +++ b/dom/base/EventSource.cpp @@ -191,6 +191,7 @@ EventSource::Init(nsISupports* aOwner, nsCOMPtr sgo = do_QueryInterface(aOwner); NS_ENSURE_STATE(sgo); + // XXXbz why are we checking this? This doesn't match anything in the spec. nsCOMPtr scriptContext = sgo->GetContext(); NS_ENSURE_STATE(scriptContext); @@ -213,19 +214,13 @@ EventSource::Init(nsISupports* aOwner, // Get the load group for the page. When requesting we'll add ourselves to it. // This way any pending requests will be automatically aborted if the user // leaves the page. - nsresult rv; - nsIScriptContext* sc = GetContextForEventHandlers(&rv); - if (sc) { - nsCOMPtr doc = - nsContentUtils::GetDocumentFromScriptContext(sc); - if (doc) { - mLoadGroup = doc->GetDocumentLoadGroup(); - } + nsCOMPtr doc = GetDocumentIfCurrent(); + if (doc) { + mLoadGroup = doc->GetDocumentLoadGroup(); } - // get the src nsCOMPtr baseURI; - rv = GetBaseURI(getter_AddRefs(baseURI)); + nsresult rv = GetBaseURI(getter_AddRefs(baseURI)); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr srcURI; @@ -526,8 +521,9 @@ EventSource::AsyncOnChannelRedirect(nsIChannel *aOldChannel, mHttpChannel = do_QueryInterface(aNewChannel); NS_ENSURE_STATE(mHttpChannel); - rv = SetupHttpChannel(); - NS_ENSURE_SUCCESS(rv, rv); + SetupHttpChannel(); + // The HTTP impl already copies over the referrer and referrer policy on + // redirects, so we don't need to SetupReferrerPolicy(). if ((aFlags & nsIChannelEventSink::REDIRECT_PERMANENT) != 0) { rv = NS_GetFinalChannelURI(mHttpChannel, getter_AddRefs(mSrc)); @@ -593,17 +589,14 @@ EventSource::GetBaseURI(nsIURI **aBaseURI) nsCOMPtr baseURI; // first we try from document->GetBaseURI() - nsresult rv; - nsIScriptContext* sc = GetContextForEventHandlers(&rv); - nsCOMPtr doc = - nsContentUtils::GetDocumentFromScriptContext(sc); + nsCOMPtr doc = GetDocumentIfCurrent(); if (doc) { baseURI = doc->GetBaseURI(); } // otherwise we get from the doc's principal if (!baseURI) { - rv = mPrincipal->GetURI(getter_AddRefs(baseURI)); + nsresult rv = mPrincipal->GetURI(getter_AddRefs(baseURI)); NS_ENSURE_SUCCESS(rv, rv); } @@ -613,18 +606,7 @@ EventSource::GetBaseURI(nsIURI **aBaseURI) return NS_OK; } -net::ReferrerPolicy -EventSource::GetReferrerPolicy() -{ - nsresult rv; - nsIScriptContext* sc = GetContextForEventHandlers(&rv); - NS_ENSURE_SUCCESS(rv, mozilla::net::RP_Default); - - nsCOMPtr doc = nsContentUtils::GetDocumentFromScriptContext(sc); - return doc ? doc->GetReferrerPolicy() : mozilla::net::RP_Default; -} - -nsresult +void EventSource::SetupHttpChannel() { mHttpChannel->SetRequestMethod(NS_LITERAL_CSTRING("GET")); @@ -640,11 +622,15 @@ EventSource::SetupHttpChannel() mHttpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Last-Event-ID"), NS_ConvertUTF16toUTF8(mLastEventID), false); } +} - nsCOMPtr codebase; - nsresult rv = GetBaseURI(getter_AddRefs(codebase)); - if (NS_SUCCEEDED(rv)) { - rv = mHttpChannel->SetReferrerWithPolicy(codebase, this->GetReferrerPolicy()); +nsresult +EventSource::SetupReferrerPolicy() +{ + nsCOMPtr doc = GetDocumentIfCurrent(); + if (doc) { + nsresult rv = mHttpChannel->SetReferrerWithPolicy(doc->GetDocumentURI(), + doc->GetReferrerPolicy()); NS_ENSURE_SUCCESS(rv, rv); } @@ -671,9 +657,7 @@ EventSource::InitChannelAndRequestEventSource() nsLoadFlags loadFlags; loadFlags = nsIRequest::LOAD_BACKGROUND | nsIRequest::LOAD_BYPASS_CACHE; - nsIScriptContext* sc = GetContextForEventHandlers(&rv); - nsCOMPtr doc = - nsContentUtils::GetDocumentFromScriptContext(sc); + nsCOMPtr doc = GetDocumentIfCurrent(); nsSecurityFlags securityFlags = nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS; @@ -710,7 +694,8 @@ EventSource::InitChannelAndRequestEventSource() mHttpChannel = do_QueryInterface(channel); NS_ENSURE_TRUE(mHttpChannel, NS_ERROR_NO_INTERFACE); - rv = SetupHttpChannel(); + SetupHttpChannel(); + rv = SetupReferrerPolicy(); NS_ENSURE_SUCCESS(rv, rv); #ifdef DEBUG diff --git a/dom/base/EventSource.h b/dom/base/EventSource.h index 8d617fb6e643..cbbfb2180884 100644 --- a/dom/base/EventSource.h +++ b/dom/base/EventSource.h @@ -107,9 +107,8 @@ protected: nsresult GetBaseURI(nsIURI **aBaseURI); - net::ReferrerPolicy GetReferrerPolicy(); - - nsresult SetupHttpChannel(); + void SetupHttpChannel(); + nsresult SetupReferrerPolicy(); nsresult InitChannelAndRequestEventSource(); nsresult ResetConnection(); nsresult DispatchFailConnection();