Bug 1265072 part 5. Get rid of uses of GetDocumentFromScriptContext in EventSource code. r=smaug

This commit is contained in:
Boris Zbarsky 2016-04-20 18:04:37 -04:00
Родитель 897473a184
Коммит 480a205762
2 изменённых файлов: 24 добавлений и 40 удалений

Просмотреть файл

@ -191,6 +191,7 @@ EventSource::Init(nsISupports* aOwner,
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aOwner);
NS_ENSURE_STATE(sgo);
// XXXbz why are we checking this? This doesn't match anything in the spec.
nsCOMPtr<nsIScriptContext> 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<nsIDocument> doc =
nsContentUtils::GetDocumentFromScriptContext(sc);
if (doc) {
mLoadGroup = doc->GetDocumentLoadGroup();
}
nsCOMPtr<nsIDocument> doc = GetDocumentIfCurrent();
if (doc) {
mLoadGroup = doc->GetDocumentLoadGroup();
}
// get the src
nsCOMPtr<nsIURI> baseURI;
rv = GetBaseURI(getter_AddRefs(baseURI));
nsresult rv = GetBaseURI(getter_AddRefs(baseURI));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIURI> 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<nsIURI> baseURI;
// first we try from document->GetBaseURI()
nsresult rv;
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
nsCOMPtr<nsIDocument> doc =
nsContentUtils::GetDocumentFromScriptContext(sc);
nsCOMPtr<nsIDocument> 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<nsIDocument> 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<nsIURI> codebase;
nsresult rv = GetBaseURI(getter_AddRefs(codebase));
if (NS_SUCCEEDED(rv)) {
rv = mHttpChannel->SetReferrerWithPolicy(codebase, this->GetReferrerPolicy());
nsresult
EventSource::SetupReferrerPolicy()
{
nsCOMPtr<nsIDocument> 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<nsIDocument> doc =
nsContentUtils::GetDocumentFromScriptContext(sc);
nsCOMPtr<nsIDocument> 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

Просмотреть файл

@ -107,9 +107,8 @@ protected:
nsresult GetBaseURI(nsIURI **aBaseURI);
net::ReferrerPolicy GetReferrerPolicy();
nsresult SetupHttpChannel();
void SetupHttpChannel();
nsresult SetupReferrerPolicy();
nsresult InitChannelAndRequestEventSource();
nsresult ResetConnection();
nsresult DispatchFailConnection();