Bug 1038756: Extend nsNetutil to include requestingPrincipal, requestingNode, and contentPolicyType (r=mcmanus)

This commit is contained in:
Christoph Kerschbaumer 2014-07-17 14:52:25 -07:00
parent 3e0164ca53
commit 86ac863b72

View File

@ -83,6 +83,8 @@
#include "nsCategoryCache.h" #include "nsCategoryCache.h"
#include "nsStringStream.h" #include "nsStringStream.h"
#include "nsIViewSourceChannel.h" #include "nsIViewSourceChannel.h"
#include "mozilla/LoadInfo.h"
#include "nsINode.h"
#include <limits> #include <limits>
@ -186,54 +188,150 @@ NS_NewFileURI(nsIURI* *result,
return rv; return rv;
} }
/*
* How to create a new Channel using NS_NewChannel:
* 1) Please try to call NS_NewChannel providing a requesting *nsINode*
* 2) If no requesting nsINode is available,
* call NS_NewChannel providing a requesting *nsIPrincipal*.
* 3) Call NS_NewChannelInternal *only* if requesting Principal and
* the Node's Principal have to be different.
* >> Most likely this is not the case! <<
* Needs special approval!
*/
inline nsresult inline nsresult
NS_NewChannel(nsIChannel **result, NS_NewChannelInternal(nsIChannel** outChannel,
nsIURI *uri, nsIURI* aUri,
nsIIOService *ioService = nullptr, // pass in nsIIOService to optimize callers nsILoadInfo* aLoadInfo,
nsILoadGroup *loadGroup = nullptr, nsIChannelPolicy* aChannelPolicy = nullptr,
nsIInterfaceRequestor *callbacks = nullptr, nsILoadGroup* aLoadGroup = nullptr,
uint32_t loadFlags = nsIRequest::LOAD_NORMAL, nsIInterfaceRequestor* aCallbacks = nullptr,
nsIChannelPolicy *channelPolicy = nullptr) nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
nsIIOService* aIoService = nullptr)
{ {
nsresult rv; NS_ASSERTION(aLoadInfo, "Can not create channel without aLoadInfo!");
NS_ENSURE_ARG_POINTER(outChannel);
nsCOMPtr<nsIIOService> grip; nsCOMPtr<nsIIOService> grip;
rv = net_EnsureIOService(&ioService, grip); nsresult rv = net_EnsureIOService(&aIoService, grip);
if (ioService) { NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIChannel> chan;
rv = ioService->NewChannelFromURI(uri, getter_AddRefs(chan)); nsCOMPtr<nsIChannel> channel;
if (NS_SUCCEEDED(rv)) { rv = aIoService->NewChannelFromURI(aUri, getter_AddRefs(channel));
if (loadGroup) { NS_ENSURE_SUCCESS(rv, rv);
rv = chan->SetLoadGroup(loadGroup);
if (aLoadGroup) {
rv = channel->SetLoadGroup(aLoadGroup);
NS_ENSURE_SUCCESS(rv, rv);
} }
if (callbacks) {
nsresult tmp = chan->SetNotificationCallbacks(callbacks); if (aCallbacks) {
if (NS_FAILED(tmp)) { rv = channel->SetNotificationCallbacks(aCallbacks);
rv = tmp; NS_ENSURE_SUCCESS(rv, rv);
} }
}
if (loadFlags != nsIRequest::LOAD_NORMAL) { if (aLoadFlags != nsIRequest::LOAD_NORMAL) {
// Retain the LOAD_REPLACE load flag if set. // Retain the LOAD_REPLACE load flag if set.
nsLoadFlags normalLoadFlags = 0; nsLoadFlags normalLoadFlags = 0;
chan->GetLoadFlags(&normalLoadFlags); channel->GetLoadFlags(&normalLoadFlags);
nsresult tmp = chan->SetLoadFlags(loadFlags | rv = channel->SetLoadFlags(aLoadFlags | (normalLoadFlags & nsIChannel::LOAD_REPLACE));
(normalLoadFlags & NS_ENSURE_SUCCESS(rv, rv);
nsIChannel::LOAD_REPLACE));
if (NS_FAILED(tmp)) {
rv = tmp;
} }
}
if (channelPolicy) { if (aChannelPolicy) {
nsCOMPtr<nsIWritablePropertyBag2> props = do_QueryInterface(chan); nsCOMPtr<nsIWritablePropertyBag2> props = do_QueryInterface(channel);
if (props) { if (props) {
props->SetPropertyAsInterface(NS_CHANNEL_PROP_CHANNEL_POLICY, props->SetPropertyAsInterface(NS_CHANNEL_PROP_CHANNEL_POLICY, aChannelPolicy);
channelPolicy);
} }
} }
if (NS_SUCCEEDED(rv))
chan.forget(result); channel->SetLoadInfo(aLoadInfo);
channel.forget(outChannel);
return NS_OK;
} }
inline nsresult
NS_NewChannelInternal(nsIChannel** outChannel,
nsIURI* aUri,
nsINode* aRequestingNode,
nsIPrincipal* aRequestingPrincipal,
nsSecurityFlags aSecurityFlags,
nsContentPolicyType aContentPolicyType,
nsIChannelPolicy* aChannelPolicy = nullptr,
nsILoadGroup* aLoadGroup = nullptr,
nsIInterfaceRequestor* aCallbacks = nullptr,
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
nsIIOService* aIoService = nullptr)
{
NS_ASSERTION(aRequestingPrincipal, "Can not create channel without a requesting Principal!");
nsCOMPtr<nsILoadInfo> loadInfo =
new mozilla::LoadInfo(aRequestingPrincipal,
aRequestingNode,
aSecurityFlags,
aContentPolicyType);
if (!loadInfo) {
return NS_ERROR_UNEXPECTED;
} }
return rv; return NS_NewChannelInternal(outChannel,
aUri,
loadInfo,
aChannelPolicy,
aLoadGroup,
aCallbacks,
aLoadFlags,
aIoService);
}
inline nsresult /* NS_NewChannelNode */
NS_NewChannel(nsIChannel** outChannel,
nsIURI* aUri,
nsINode* aRequestingNode,
nsSecurityFlags aSecurityFlags,
nsContentPolicyType aContentPolicyType,
nsIChannelPolicy* aChannelPolicy = nullptr,
nsILoadGroup* aLoadGroup = nullptr,
nsIInterfaceRequestor* aCallbacks = nullptr,
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
nsIIOService* aIoService = nullptr)
{
NS_ASSERTION(aRequestingNode, "Can not create channel without a requesting Node!");
return NS_NewChannelInternal(outChannel,
aUri,
aRequestingNode,
aRequestingNode->NodePrincipal(),
aSecurityFlags,
aContentPolicyType,
aChannelPolicy,
aLoadGroup,
aCallbacks,
aLoadFlags,
aIoService);
}
inline nsresult /* NS_NewChannelPrincipal */
NS_NewChannel(nsIChannel** outChannel,
nsIURI* aUri,
nsIPrincipal* aRequestingPrincipal,
nsSecurityFlags aSecurityFlags,
nsContentPolicyType aContentPolicyType,
nsIChannelPolicy* aChannelPolicy = nullptr,
nsILoadGroup* aLoadGroup = nullptr,
nsIInterfaceRequestor* aCallbacks = nullptr,
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
nsIIOService* aIoService = nullptr)
{
return NS_NewChannelInternal(outChannel,
aUri,
nullptr, // aRequestingNode,
aRequestingPrincipal,
aSecurityFlags,
aContentPolicyType,
aChannelPolicy,
aLoadGroup,
aCallbacks,
aLoadFlags,
aIoService);
} }
// Use this function with CAUTION. It creates a stream that blocks when you // Use this function with CAUTION. It creates a stream that blocks when you
@ -241,48 +339,149 @@ NS_NewChannel(nsIChannel **result,
// to implement a full blown asynchronous consumer (via nsIStreamListener) look // to implement a full blown asynchronous consumer (via nsIStreamListener) look
// at nsIStreamLoader instead. // at nsIStreamLoader instead.
inline nsresult inline nsresult
NS_OpenURI(nsIInputStream **result, NS_OpenURIInternal(nsIInputStream** outStream,
nsIURI *uri, nsIURI* aUri,
nsIIOService *ioService = nullptr, // pass in nsIIOService to optimize callers nsINode* aRequestingNode,
nsILoadGroup *loadGroup = nullptr, nsIPrincipal* aRequestingPrincipal,
nsIInterfaceRequestor *callbacks = nullptr, nsSecurityFlags aSecurityFlags,
uint32_t loadFlags = nsIRequest::LOAD_NORMAL, nsContentPolicyType aContentPolicyType,
nsIChannel **channelOut = nullptr) nsILoadGroup* aLoadGroup = nullptr,
nsIInterfaceRequestor* aCallbacks = nullptr,
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
nsIIOService* aIoService = nullptr, // pass in nsIIOService to optimize callers
nsIChannel** outChannel = nullptr)
{ {
nsresult rv; NS_ASSERTION(aRequestingPrincipal, "Can not create channel without a requesting Principal!");
nsCOMPtr<nsIChannel> channel; nsCOMPtr<nsIChannel> channel;
rv = NS_NewChannel(getter_AddRefs(channel), uri, ioService, nsresult rv = NS_NewChannelInternal(getter_AddRefs(channel),
loadGroup, callbacks, loadFlags); aUri,
if (NS_SUCCEEDED(rv)) { aRequestingNode,
aRequestingPrincipal,
aSecurityFlags,
aContentPolicyType,
nullptr, // aChannelPolicy,
aLoadGroup,
aCallbacks,
aLoadFlags,
aIoService);
NS_ENSURE_SUCCESS(rv, rv);
nsIInputStream *stream; nsIInputStream *stream;
rv = channel->Open(&stream); rv = channel->Open(&stream);
if (NS_SUCCEEDED(rv)) { NS_ENSURE_SUCCESS(rv, rv);
*result = stream; *outStream = stream;
if (channelOut) { if (outChannel) {
*channelOut = nullptr; *outChannel = nullptr;
channel.swap(*channelOut); channel.swap(*outChannel);
} }
return NS_OK;
} }
}
return rv; inline nsresult /* NS_OpenURIprincipal */
NS_OpenURI(nsIInputStream** outStream,
nsIURI* aUri,
nsIPrincipal* aRequestingPrincipal,
nsSecurityFlags aSecurityFlags,
nsContentPolicyType aContentPolicyType,
nsILoadGroup* aLoadGroup = nullptr,
nsIInterfaceRequestor* aCallbacks = nullptr,
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
nsIIOService* aIoService = nullptr,
nsIChannel** outChannel = nullptr)
{
return NS_OpenURIInternal(outStream,
aUri,
nullptr, // aRequestingNode
aRequestingPrincipal,
aSecurityFlags,
aContentPolicyType,
aLoadGroup,
aCallbacks,
aLoadFlags,
aIoService,
outChannel);
} }
inline nsresult inline nsresult
NS_OpenURI(nsIStreamListener *listener, NS_OpenURIInternal(nsIStreamListener* aListener,
nsISupports *context, nsISupports* aContext,
nsIURI *uri, nsIURI* aUri,
nsIIOService *ioService = nullptr, // pass in nsIIOService to optimize callers nsILoadInfo* aLoadInfo,
nsILoadGroup *loadGroup = nullptr, nsILoadGroup* aLoadGroup = nullptr,
nsIInterfaceRequestor *callbacks = nullptr, nsIInterfaceRequestor* aCallbacks = nullptr,
uint32_t loadFlags = nsIRequest::LOAD_NORMAL) nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
nsIIOService* aIoService = nullptr)
{ {
nsresult rv;
nsCOMPtr<nsIChannel> channel; nsCOMPtr<nsIChannel> channel;
rv = NS_NewChannel(getter_AddRefs(channel), uri, ioService, nsresult rv = NS_NewChannelInternal(getter_AddRefs(channel),
loadGroup, callbacks, loadFlags); aUri,
if (NS_SUCCEEDED(rv)) aLoadInfo,
rv = channel->AsyncOpen(listener, context); nullptr, // aChannelPolicy
return rv; aLoadGroup,
aCallbacks,
aLoadFlags,
aIoService);
NS_ENSURE_SUCCESS(rv, rv);
return channel->AsyncOpen(aListener, aContext);
}
inline nsresult
NS_OpenURIInternal(nsIStreamListener* aListener,
nsISupports* aContext,
nsIURI* aUri,
nsINode* aRequestingNode,
nsIPrincipal* aRequestingPrincipal,
nsSecurityFlags aSecurityFlags,
nsContentPolicyType aContentPolicyType,
nsILoadGroup* aLoadGroup = nullptr,
nsIInterfaceRequestor* aCallbacks = nullptr,
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
nsIIOService* aIoService = nullptr)
{
NS_ASSERTION(aRequestingPrincipal, "Can not create channel without a requesting Principal!");
nsCOMPtr<nsILoadInfo> loadInfo =
new mozilla::LoadInfo(aRequestingPrincipal,
aRequestingNode,
aSecurityFlags,
aContentPolicyType);
if (!loadInfo) {
return NS_ERROR_UNEXPECTED;
}
return NS_OpenURIInternal(aListener,
aContext,
aUri,
loadInfo,
aLoadGroup,
aCallbacks,
aLoadFlags,
aIoService);
}
inline nsresult
NS_OpenURI(nsIStreamListener* aListener,
nsISupports* aContext,
nsIURI* aUri,
nsIPrincipal* aRequestingPrincipal,
nsSecurityFlags aSecurityFlags,
nsContentPolicyType aContentPolicyType,
nsILoadGroup* aLoadGroup = nullptr,
nsIInterfaceRequestor* aCallbacks = nullptr,
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
nsIIOService* aIoService = nullptr)
{
return NS_OpenURIInternal(aListener,
aContext,
aUri,
nullptr, // aRequestingNode
aRequestingPrincipal,
aSecurityFlags,
aContentPolicyType,
aLoadGroup,
aCallbacks,
aLoadFlags,
aIoService);
} }
inline nsresult inline nsresult
@ -609,32 +808,95 @@ NS_NewStreamLoader(nsIStreamLoader **result,
} }
inline nsresult inline nsresult
NS_NewStreamLoader(nsIStreamLoader **result, NS_NewStreamLoaderInternal(nsIStreamLoader** outStream,
nsIURI *uri, nsIURI* aUri,
nsIStreamLoaderObserver *observer, nsIStreamLoaderObserver* aObserver,
nsISupports *context = nullptr, nsINode* aRequestingNode,
nsILoadGroup *loadGroup = nullptr, nsIPrincipal* aRequestingPrincipal,
nsIInterfaceRequestor *callbacks = nullptr, nsSecurityFlags aSecurityFlags,
uint32_t loadFlags = nsIRequest::LOAD_NORMAL, nsContentPolicyType aContentPolicyType,
nsIURI *referrer = nullptr) nsISupports* aContext = nullptr,
nsILoadGroup* aLoadGroup = nullptr,
nsIInterfaceRequestor* aCallbacks = nullptr,
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
nsIURI* aReferrer = nullptr)
{ {
nsresult rv;
nsCOMPtr<nsIChannel> channel; nsCOMPtr<nsIChannel> channel;
rv = NS_NewChannel(getter_AddRefs(channel), nsresult rv = NS_NewChannelInternal(getter_AddRefs(channel),
uri, aUri,
nullptr, aRequestingNode,
loadGroup, aRequestingPrincipal,
callbacks, aSecurityFlags,
loadFlags); aContentPolicyType,
if (NS_SUCCEEDED(rv)) { nullptr, // aChannelPolicy
aLoadGroup,
aCallbacks,
aLoadFlags);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel)); nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
if (httpChannel) if (httpChannel) {
httpChannel->SetReferrer(referrer); httpChannel->SetReferrer(aReferrer);
rv = NS_NewStreamLoader(result, observer);
if (NS_SUCCEEDED(rv))
rv = channel->AsyncOpen(*result, context);
} }
return rv; rv = NS_NewStreamLoader(outStream, aObserver);
NS_ENSURE_SUCCESS(rv, rv);
return channel->AsyncOpen(*outStream, aContext);
}
inline nsresult /* NS_NewStreamLoaderNode */
NS_NewStreamLoader(nsIStreamLoader** outStream,
nsIURI* aUri,
nsIStreamLoaderObserver* aObserver,
nsINode* aRequestingNode,
nsSecurityFlags aSecurityFlags,
nsContentPolicyType aContentPolicyType,
nsISupports* aContext = nullptr,
nsILoadGroup* aLoadGroup = nullptr,
nsIInterfaceRequestor* aCallbacks = nullptr,
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
nsIURI* aReferrer = nullptr)
{
NS_ASSERTION(aRequestingNode, "Can not create stream loader without a requesting Node!");
return NS_NewStreamLoaderInternal(outStream,
aUri,
aObserver,
aRequestingNode,
aRequestingNode->NodePrincipal(),
aSecurityFlags,
aContentPolicyType,
aContext,
aLoadGroup,
aCallbacks,
aLoadFlags,
aReferrer);
}
inline nsresult /* NS_NewStreamLoaderPrincipal */
NS_NewStreamLoader(nsIStreamLoader** outStream,
nsIURI* aUri,
nsIStreamLoaderObserver* aObserver,
nsIPrincipal* aRequestingPrincipal,
nsSecurityFlags aSecurityFlags,
nsContentPolicyType aContentPolicyType,
nsISupports* aContext = nullptr,
nsILoadGroup* aLoadGroup = nullptr,
nsIInterfaceRequestor* aCallbacks = nullptr,
nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL,
nsIURI* aReferrer = nullptr)
{
return NS_NewStreamLoaderInternal(outStream,
aUri,
aObserver,
nullptr, // aRequestingNode
aRequestingPrincipal,
aSecurityFlags,
aContentPolicyType,
aContext,
aLoadGroup,
aCallbacks,
aLoadFlags,
aReferrer);
} }
inline nsresult inline nsresult
@ -1241,41 +1503,57 @@ NS_ReadInputStreamToString(nsIInputStream *aInputStream,
#endif #endif
inline nsresult inline nsresult
NS_LoadPersistentPropertiesFromURI(nsIPersistentProperties **result, NS_LoadPersistentPropertiesFromURI(nsIPersistentProperties** outResult,
nsIURI *uri, nsIURI* aUri,
nsIIOService *ioService = nullptr) nsIPrincipal* aRequestingPrincipal,
nsContentPolicyType aContentPolicyType,
nsIIOService* aIoService = nullptr)
{ {
nsCOMPtr<nsIInputStream> in; nsCOMPtr<nsIInputStream> in;
nsresult rv = NS_OpenURI(getter_AddRefs(in), uri, ioService); nsresult rv = NS_OpenURI(getter_AddRefs(in),
if (NS_SUCCEEDED(rv)) { aUri,
aRequestingPrincipal,
nsILoadInfo::SEC_NORMAL,
aContentPolicyType,
nullptr, // aLoadGroup
nullptr, // aCallbacks
nsIRequest::LOAD_NORMAL, //aLoadFlags
aIoService);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIPersistentProperties> properties = nsCOMPtr<nsIPersistentProperties> properties =
do_CreateInstance(NS_PERSISTENTPROPERTIES_CONTRACTID, &rv); do_CreateInstance(NS_PERSISTENTPROPERTIES_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv)) { NS_ENSURE_SUCCESS(rv, rv);
rv = properties->Load(in); rv = properties->Load(in);
if (NS_SUCCEEDED(rv)) { if (NS_SUCCEEDED(rv)) {
*result = nullptr; *outResult = nullptr;
properties.swap(*result); properties.swap(*outResult);
}
}
} }
return rv; return rv;
} }
inline nsresult inline nsresult
NS_LoadPersistentPropertiesFromURISpec(nsIPersistentProperties **result, NS_LoadPersistentPropertiesFromURISpec(nsIPersistentProperties** outResult,
const nsACString &spec, const nsACString& aSpec,
const char *charset = nullptr, nsIPrincipal* aRequestingPrincipal,
nsIURI *baseURI = nullptr, nsContentPolicyType aContentPolicyType,
nsIIOService *ioService = nullptr) const char* aCharset = nullptr,
nsIURI* aBaseURI = nullptr,
nsIIOService* aIoService = nullptr)
{ {
nsCOMPtr<nsIURI> uri; nsCOMPtr<nsIURI> uri;
nsresult rv = nsresult rv = NS_NewURI(getter_AddRefs(uri),
NS_NewURI(getter_AddRefs(uri), spec, charset, baseURI, ioService); aSpec,
aCharset,
aBaseURI,
aIoService);
NS_ENSURE_SUCCESS(rv, rv);
if (NS_SUCCEEDED(rv)) return NS_LoadPersistentPropertiesFromURI(outResult,
rv = NS_LoadPersistentPropertiesFromURI(result, uri, ioService); uri,
aRequestingPrincipal,
return rv; aContentPolicyType,
aIoService);
} }
/** /**