Bug 1108547 - Part 2: Create a dummy channel for accessing the document's cookie when we don't have one already; r=jdm

This ensures that the cookie service can know which cookie database
to query from.  This is a gross hack, please see the discussion on
the bug as to why we did this.
This commit is contained in:
Ehsan Akhgari 2015-01-21 20:57:29 -05:00
parent c8ddc0896b
commit b1f6ca76ad
3 changed files with 47 additions and 7 deletions

View File

@ -1225,6 +1225,32 @@ nsHTMLDocument::GetCookie(nsAString& aCookie)
return rv.ErrorCode();
}
already_AddRefed<nsIChannel>
nsHTMLDocument::CreateDummyChannelForCookies(nsIURI* aCodebaseURI)
{
// The cookie service reads the privacy status of the channel we pass to it in
// order to determine which cookie database to query. In some cases we don't
// have a proper channel to hand it to the cookie service though. This
// function creates a dummy channel that is not used to load anything, for the
// sole purpose of handing it to the cookie service. DO NOT USE THIS CHANNEL
// FOR ANY OTHER PURPOSE.
MOZ_ASSERT(!mChannel);
nsCOMPtr<nsIChannel> channel;
NS_NewChannel(getter_AddRefs(channel), aCodebaseURI, this,
nsILoadInfo::SEC_NORMAL,
nsIContentPolicy::TYPE_INVALID);
nsCOMPtr<nsIPrivateBrowsingChannel> pbChannel =
do_QueryInterface(channel);
nsCOMPtr<nsIDocShell> docShell(mDocumentContainer);
nsCOMPtr<nsILoadContext> loadContext = do_QueryInterface(docShell);
if (!pbChannel || !loadContext) {
return nullptr;
}
pbChannel->SetPrivate(loadContext->UsePrivateBrowsing());
return channel.forget();
}
void
nsHTMLDocument::GetCookie(nsAString& aCookie, ErrorResult& rv)
{
@ -1257,8 +1283,16 @@ nsHTMLDocument::GetCookie(nsAString& aCookie, ErrorResult& rv)
return;
}
nsCOMPtr<nsIChannel> channel(mChannel);
if (!channel) {
channel = CreateDummyChannelForCookies(codebaseURI);
if (!channel) {
return;
}
}
nsXPIDLCString cookie;
service->GetCookieString(codebaseURI, mChannel, getter_Copies(cookie));
service->GetCookieString(codebaseURI, channel, getter_Copies(cookie));
// CopyUTF8toUTF16 doesn't handle error
// because it assumes that the input is valid.
nsContentUtils::ConvertStringFromEncoding(NS_LITERAL_CSTRING("UTF-8"),
@ -1302,8 +1336,16 @@ nsHTMLDocument::SetCookie(const nsAString& aCookie, ErrorResult& rv)
return;
}
nsCOMPtr<nsIChannel> channel(mChannel);
if (!channel) {
channel = CreateDummyChannelForCookies(codebaseURI);
if (!channel) {
return;
}
}
NS_ConvertUTF16toUTF8 cookie(aCookie);
service->SetCookieString(codebaseURI, nullptr, cookie.get(), mChannel);
service->SetCookieString(codebaseURI, nullptr, cookie.get(), channel);
}
}

View File

@ -275,6 +275,9 @@ protected:
nsresult CreateAndAddWyciwygChannel(void);
nsresult RemoveWyciwygChannel(void);
// This should *ONLY* be used in GetCookie/SetCookie.
already_AddRefed<nsIChannel> CreateDummyChannelForCookies(nsIURI* aCodebaseURI);
/**
* Like IsEditingOn(), but will flush as needed first.
*/

View File

@ -1,5 +0,0 @@
[document-cookie.html]
type: testharness
[getting cookie for a cookie-averse document returns empty string, setting does nothing]
expected: FAIL