michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* vim:set ts=4 sw=4 sts=4 et cin: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsNetUtil_h__ michael@0: #define nsNetUtil_h__ michael@0: michael@0: #include "nsError.h" michael@0: #include "nsNetCID.h" michael@0: #include "nsStringGlue.h" michael@0: #include "nsMemory.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "prio.h" // for read/write flags, permissions, etc. michael@0: #include "nsHashKeys.h" michael@0: michael@0: #include "plstr.h" michael@0: #include "nsIURI.h" michael@0: #include "nsIStandardURL.h" michael@0: #include "nsIURLParser.h" michael@0: #include "nsIUUIDGenerator.h" michael@0: #include "nsIInputStream.h" michael@0: #include "nsIOutputStream.h" michael@0: #include "nsISafeOutputStream.h" michael@0: #include "nsIStreamListener.h" michael@0: #include "nsIRequestObserverProxy.h" michael@0: #include "nsISimpleStreamListener.h" michael@0: #include "nsILoadGroup.h" michael@0: #include "nsIInterfaceRequestor.h" michael@0: #include "nsIInterfaceRequestorUtils.h" michael@0: #include "nsIIOService.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsIChannel.h" michael@0: #include "nsChannelProperties.h" michael@0: #include "nsIInputStreamChannel.h" michael@0: #include "nsITransport.h" michael@0: #include "nsIStreamTransportService.h" michael@0: #include "nsIHttpChannel.h" michael@0: #include "nsIDownloader.h" michael@0: #include "nsIStreamLoader.h" michael@0: #include "nsIUnicharStreamLoader.h" michael@0: #include "nsIPipe.h" michael@0: #include "nsIProtocolHandler.h" michael@0: #include "nsIFileProtocolHandler.h" michael@0: #include "nsIStringStream.h" michael@0: #include "nsIFile.h" michael@0: #include "nsIFileStreams.h" michael@0: #include "nsIFileURL.h" michael@0: #include "nsIProtocolProxyService.h" michael@0: #include "nsIProxyInfo.h" michael@0: #include "nsIFileStreams.h" michael@0: #include "nsIBufferedStreams.h" michael@0: #include "nsIInputStreamPump.h" michael@0: #include "nsIAsyncStreamCopier.h" michael@0: #include "nsIPersistentProperties2.h" michael@0: #include "nsISyncStreamListener.h" michael@0: #include "nsInterfaceRequestorAgg.h" michael@0: #include "nsINetUtil.h" michael@0: #include "nsIURIWithPrincipal.h" michael@0: #include "nsIAuthPrompt.h" michael@0: #include "nsIAuthPrompt2.h" michael@0: #include "nsIAuthPromptAdapterFactory.h" michael@0: #include "nsComponentManagerUtils.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "nsINestedURI.h" michael@0: #include "nsIMutable.h" michael@0: #include "nsIPropertyBag2.h" michael@0: #include "nsIWritablePropertyBag2.h" michael@0: #include "nsIIDNService.h" michael@0: #include "nsIChannelEventSink.h" michael@0: #include "nsIChannelPolicy.h" michael@0: #include "nsISocketProviderService.h" michael@0: #include "nsISocketProvider.h" michael@0: #include "nsIRedirectChannelRegistrar.h" michael@0: #include "nsIMIMEHeaderParam.h" michael@0: #include "nsILoadContext.h" michael@0: #include "mozilla/Services.h" michael@0: #include "nsIPrivateBrowsingChannel.h" michael@0: #include "mozIApplicationClearPrivateDataParams.h" michael@0: #include "nsIOfflineCacheUpdate.h" michael@0: #include "nsIContentSniffer.h" michael@0: #include "nsCategoryCache.h" michael@0: #include "nsStringStream.h" michael@0: #include "nsIViewSourceChannel.h" michael@0: michael@0: #include michael@0: michael@0: #ifdef MOZILLA_INTERNAL_API michael@0: michael@0: #include "nsReadableUtils.h" michael@0: michael@0: inline already_AddRefed michael@0: do_GetIOService(nsresult* error = 0) michael@0: { michael@0: nsCOMPtr io = mozilla::services::GetIOService(); michael@0: if (error) michael@0: *error = io ? NS_OK : NS_ERROR_FAILURE; michael@0: return io.forget(); michael@0: } michael@0: michael@0: inline already_AddRefed michael@0: do_GetNetUtil(nsresult *error = 0) michael@0: { michael@0: nsCOMPtr io = mozilla::services::GetIOService(); michael@0: nsCOMPtr util; michael@0: if (io) michael@0: util = do_QueryInterface(io); michael@0: michael@0: if (error) michael@0: *error = !!util ? NS_OK : NS_ERROR_FAILURE; michael@0: return util.forget(); michael@0: } michael@0: #else michael@0: // Helper, to simplify getting the I/O service. michael@0: inline const nsGetServiceByContractIDWithError michael@0: do_GetIOService(nsresult* error = 0) michael@0: { michael@0: return nsGetServiceByContractIDWithError(NS_IOSERVICE_CONTRACTID, error); michael@0: } michael@0: michael@0: // An alias to do_GetIOService michael@0: inline const nsGetServiceByContractIDWithError michael@0: do_GetNetUtil(nsresult* error = 0) michael@0: { michael@0: return do_GetIOService(error); michael@0: } michael@0: #endif michael@0: michael@0: // private little helper function... don't call this directly! michael@0: inline nsresult michael@0: net_EnsureIOService(nsIIOService **ios, nsCOMPtr &grip) michael@0: { michael@0: nsresult rv = NS_OK; michael@0: if (!*ios) { michael@0: grip = do_GetIOService(&rv); michael@0: *ios = grip; michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewURI(nsIURI **result, michael@0: const nsACString &spec, michael@0: const char *charset = nullptr, michael@0: nsIURI *baseURI = nullptr, michael@0: nsIIOService *ioService = nullptr) // pass in nsIIOService to optimize callers michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr grip; michael@0: rv = net_EnsureIOService(&ioService, grip); michael@0: if (ioService) michael@0: rv = ioService->NewURI(spec, charset, baseURI, result); michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewURI(nsIURI* *result, michael@0: const nsAString& spec, michael@0: const char *charset = nullptr, michael@0: nsIURI* baseURI = nullptr, michael@0: nsIIOService* ioService = nullptr) // pass in nsIIOService to optimize callers michael@0: { michael@0: return NS_NewURI(result, NS_ConvertUTF16toUTF8(spec), charset, baseURI, ioService); michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewURI(nsIURI* *result, michael@0: const char *spec, michael@0: nsIURI* baseURI = nullptr, michael@0: nsIIOService* ioService = nullptr) // pass in nsIIOService to optimize callers michael@0: { michael@0: return NS_NewURI(result, nsDependentCString(spec), nullptr, baseURI, ioService); michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewFileURI(nsIURI* *result, michael@0: nsIFile* spec, michael@0: nsIIOService* ioService = nullptr) // pass in nsIIOService to optimize callers michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr grip; michael@0: rv = net_EnsureIOService(&ioService, grip); michael@0: if (ioService) michael@0: rv = ioService->NewFileURI(spec, result); michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewChannel(nsIChannel **result, michael@0: nsIURI *uri, michael@0: nsIIOService *ioService = nullptr, // pass in nsIIOService to optimize callers michael@0: nsILoadGroup *loadGroup = nullptr, michael@0: nsIInterfaceRequestor *callbacks = nullptr, michael@0: uint32_t loadFlags = nsIRequest::LOAD_NORMAL, michael@0: nsIChannelPolicy *channelPolicy = nullptr) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr grip; michael@0: rv = net_EnsureIOService(&ioService, grip); michael@0: if (ioService) { michael@0: nsCOMPtr chan; michael@0: rv = ioService->NewChannelFromURI(uri, getter_AddRefs(chan)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: if (loadGroup) { michael@0: rv = chan->SetLoadGroup(loadGroup); michael@0: } michael@0: if (callbacks) { michael@0: nsresult tmp = chan->SetNotificationCallbacks(callbacks); michael@0: if (NS_FAILED(tmp)) { michael@0: rv = tmp; michael@0: } michael@0: } michael@0: if (loadFlags != nsIRequest::LOAD_NORMAL) { michael@0: // Retain the LOAD_REPLACE load flag if set. michael@0: nsLoadFlags normalLoadFlags = 0; michael@0: chan->GetLoadFlags(&normalLoadFlags); michael@0: nsresult tmp = chan->SetLoadFlags(loadFlags | michael@0: (normalLoadFlags & michael@0: nsIChannel::LOAD_REPLACE)); michael@0: if (NS_FAILED(tmp)) { michael@0: rv = tmp; michael@0: } michael@0: } michael@0: if (channelPolicy) { michael@0: nsCOMPtr props = do_QueryInterface(chan); michael@0: if (props) { michael@0: props->SetPropertyAsInterface(NS_CHANNEL_PROP_CHANNEL_POLICY, michael@0: channelPolicy); michael@0: } michael@0: } michael@0: if (NS_SUCCEEDED(rv)) michael@0: chan.forget(result); michael@0: } michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: // Use this function with CAUTION. It creates a stream that blocks when you michael@0: // Read() from it and blocking the UI thread is a bad idea. If you don't want michael@0: // to implement a full blown asynchronous consumer (via nsIStreamListener) look michael@0: // at nsIStreamLoader instead. michael@0: inline nsresult michael@0: NS_OpenURI(nsIInputStream **result, michael@0: nsIURI *uri, michael@0: nsIIOService *ioService = nullptr, // pass in nsIIOService to optimize callers michael@0: nsILoadGroup *loadGroup = nullptr, michael@0: nsIInterfaceRequestor *callbacks = nullptr, michael@0: uint32_t loadFlags = nsIRequest::LOAD_NORMAL, michael@0: nsIChannel **channelOut = nullptr) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr channel; michael@0: rv = NS_NewChannel(getter_AddRefs(channel), uri, ioService, michael@0: loadGroup, callbacks, loadFlags); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: nsIInputStream *stream; michael@0: rv = channel->Open(&stream); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: *result = stream; michael@0: if (channelOut) { michael@0: *channelOut = nullptr; michael@0: channel.swap(*channelOut); michael@0: } michael@0: } michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_OpenURI(nsIStreamListener *listener, michael@0: nsISupports *context, michael@0: nsIURI *uri, michael@0: nsIIOService *ioService = nullptr, // pass in nsIIOService to optimize callers michael@0: nsILoadGroup *loadGroup = nullptr, michael@0: nsIInterfaceRequestor *callbacks = nullptr, michael@0: uint32_t loadFlags = nsIRequest::LOAD_NORMAL) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr channel; michael@0: rv = NS_NewChannel(getter_AddRefs(channel), uri, ioService, michael@0: loadGroup, callbacks, loadFlags); michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = channel->AsyncOpen(listener, context); michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_MakeAbsoluteURI(nsACString &result, michael@0: const nsACString &spec, michael@0: nsIURI *baseURI) michael@0: { michael@0: nsresult rv; michael@0: if (!baseURI) { michael@0: NS_WARNING("It doesn't make sense to not supply a base URI"); michael@0: result = spec; michael@0: rv = NS_OK; michael@0: } michael@0: else if (spec.IsEmpty()) michael@0: rv = baseURI->GetSpec(result); michael@0: else michael@0: rv = baseURI->Resolve(spec, result); michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_MakeAbsoluteURI(char **result, michael@0: const char *spec, michael@0: nsIURI *baseURI) michael@0: { michael@0: nsresult rv; michael@0: nsAutoCString resultBuf; michael@0: rv = NS_MakeAbsoluteURI(resultBuf, nsDependentCString(spec), baseURI); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: *result = ToNewCString(resultBuf); michael@0: if (!*result) michael@0: rv = NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_MakeAbsoluteURI(nsAString &result, michael@0: const nsAString &spec, michael@0: nsIURI *baseURI) michael@0: { michael@0: nsresult rv; michael@0: if (!baseURI) { michael@0: NS_WARNING("It doesn't make sense to not supply a base URI"); michael@0: result = spec; michael@0: rv = NS_OK; michael@0: } michael@0: else { michael@0: nsAutoCString resultBuf; michael@0: if (spec.IsEmpty()) michael@0: rv = baseURI->GetSpec(resultBuf); michael@0: else michael@0: rv = baseURI->Resolve(NS_ConvertUTF16toUTF8(spec), resultBuf); michael@0: if (NS_SUCCEEDED(rv)) michael@0: CopyUTF8toUTF16(resultBuf, result); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: /** michael@0: * This function is a helper function to get a scheme's default port. michael@0: */ michael@0: inline int32_t michael@0: NS_GetDefaultPort(const char *scheme, michael@0: nsIIOService* ioService = nullptr) michael@0: { michael@0: nsresult rv; michael@0: michael@0: nsCOMPtr grip; michael@0: net_EnsureIOService(&ioService, grip); michael@0: if (!ioService) michael@0: return -1; michael@0: michael@0: nsCOMPtr handler; michael@0: rv = ioService->GetProtocolHandler(scheme, getter_AddRefs(handler)); michael@0: if (NS_FAILED(rv)) michael@0: return -1; michael@0: int32_t port; michael@0: rv = handler->GetDefaultPort(&port); michael@0: return NS_SUCCEEDED(rv) ? port : -1; michael@0: } michael@0: michael@0: /** michael@0: * This function is a helper function to apply the ToAscii conversion michael@0: * to a string michael@0: */ michael@0: inline bool michael@0: NS_StringToACE(const nsACString &idn, nsACString &result) michael@0: { michael@0: nsCOMPtr idnSrv = do_GetService(NS_IDNSERVICE_CONTRACTID); michael@0: if (!idnSrv) michael@0: return false; michael@0: nsresult rv = idnSrv->ConvertUTF8toACE(idn, result); michael@0: if (NS_FAILED(rv)) michael@0: return false; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: /** michael@0: * This function is a helper function to get a protocol's default port if the michael@0: * URI does not specify a port explicitly. Returns -1 if this protocol has no michael@0: * concept of ports or if there was an error getting the port. michael@0: */ michael@0: inline int32_t michael@0: NS_GetRealPort(nsIURI* aURI) michael@0: { michael@0: int32_t port; michael@0: nsresult rv = aURI->GetPort(&port); michael@0: if (NS_FAILED(rv)) michael@0: return -1; michael@0: michael@0: if (port != -1) michael@0: return port; // explicitly specified michael@0: michael@0: // Otherwise, we have to get the default port from the protocol handler michael@0: michael@0: // Need the scheme first michael@0: nsAutoCString scheme; michael@0: rv = aURI->GetScheme(scheme); michael@0: if (NS_FAILED(rv)) michael@0: return -1; michael@0: michael@0: return NS_GetDefaultPort(scheme.get()); michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewInputStreamChannel(nsIChannel **result, michael@0: nsIURI *uri, michael@0: nsIInputStream *stream, michael@0: const nsACString &contentType, michael@0: const nsACString *contentCharset) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr isc = michael@0: do_CreateInstance(NS_INPUTSTREAMCHANNEL_CONTRACTID, &rv); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: rv = isc->SetURI(uri); michael@0: nsresult tmp = isc->SetContentStream(stream); michael@0: if (NS_FAILED(tmp)) { michael@0: rv = tmp; michael@0: } michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: nsCOMPtr chan = do_QueryInterface(isc, &rv); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: if (!contentType.IsEmpty()) michael@0: rv = chan->SetContentType(contentType); michael@0: if (contentCharset && !contentCharset->IsEmpty()) { michael@0: tmp = chan->SetContentCharset(*contentCharset); michael@0: if (NS_FAILED(tmp)) { michael@0: rv = tmp; michael@0: } michael@0: } michael@0: if (NS_SUCCEEDED(rv)) { michael@0: *result = nullptr; michael@0: chan.swap(*result); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewInputStreamChannel(nsIChannel **result, michael@0: nsIURI *uri, michael@0: nsIInputStream *stream, michael@0: const nsACString &contentType = EmptyCString()) michael@0: { michael@0: return NS_NewInputStreamChannel(result, uri, stream, contentType, nullptr); michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewInputStreamChannel(nsIChannel **result, michael@0: nsIURI *uri, michael@0: nsIInputStream *stream, michael@0: const nsACString &contentType, michael@0: const nsACString &contentCharset) michael@0: { michael@0: return NS_NewInputStreamChannel(result, uri, stream, contentType, michael@0: &contentCharset); michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewInputStreamChannel(nsIChannel **result, michael@0: nsIURI *uri, michael@0: const nsAString &data, michael@0: const nsACString &contentType, michael@0: bool isSrcdocChannel = false) michael@0: { michael@0: michael@0: nsresult rv; michael@0: michael@0: nsCOMPtr stream; michael@0: stream = do_CreateInstance(NS_STRINGINPUTSTREAM_CONTRACTID, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: #ifdef MOZILLA_INTERNAL_API michael@0: uint32_t len; michael@0: char* utf8Bytes = ToNewUTF8String(data, &len); michael@0: rv = stream->AdoptData(utf8Bytes, len); michael@0: #else michael@0: char* utf8Bytes = ToNewUTF8String(data); michael@0: rv = stream->AdoptData(utf8Bytes, strlen(utf8Bytes)); michael@0: #endif michael@0: michael@0: nsCOMPtr chan; michael@0: michael@0: rv = NS_NewInputStreamChannel(getter_AddRefs(chan), uri, stream, michael@0: contentType, NS_LITERAL_CSTRING("UTF-8")); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if (isSrcdocChannel) { michael@0: nsCOMPtr inStrmChan = do_QueryInterface(chan); michael@0: NS_ENSURE_TRUE(inStrmChan, NS_ERROR_FAILURE); michael@0: inStrmChan->SetSrcdocData(data); michael@0: } michael@0: michael@0: *result = nullptr; michael@0: chan.swap(*result); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewInputStreamPump(nsIInputStreamPump **result, michael@0: nsIInputStream *stream, michael@0: int64_t streamPos = int64_t(-1), michael@0: int64_t streamLen = int64_t(-1), michael@0: uint32_t segsize = 0, michael@0: uint32_t segcount = 0, michael@0: bool closeWhenDone = false) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr pump = michael@0: do_CreateInstance(NS_INPUTSTREAMPUMP_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = pump->Init(stream, streamPos, streamLen, michael@0: segsize, segcount, closeWhenDone); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: *result = nullptr; michael@0: pump.swap(*result); michael@0: } michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: // NOTE: you will need to specify whether or not your streams are buffered michael@0: // (i.e., do they implement ReadSegments/WriteSegments). the default michael@0: // assumption of TRUE for both streams might not be right for you! michael@0: inline nsresult michael@0: NS_NewAsyncStreamCopier(nsIAsyncStreamCopier **result, michael@0: nsIInputStream *source, michael@0: nsIOutputStream *sink, michael@0: nsIEventTarget *target, michael@0: bool sourceBuffered = true, michael@0: bool sinkBuffered = true, michael@0: uint32_t chunkSize = 0, michael@0: bool closeSource = true, michael@0: bool closeSink = true) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr copier = michael@0: do_CreateInstance(NS_ASYNCSTREAMCOPIER_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = copier->Init(source, sink, target, sourceBuffered, sinkBuffered, michael@0: chunkSize, closeSource, closeSink); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: *result = nullptr; michael@0: copier.swap(*result); michael@0: } michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewLoadGroup(nsILoadGroup **result, michael@0: nsIRequestObserver *obs) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr group = michael@0: do_CreateInstance(NS_LOADGROUP_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = group->SetGroupObserver(obs); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: *result = nullptr; michael@0: group.swap(*result); michael@0: } michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewDownloader(nsIStreamListener **result, michael@0: nsIDownloadObserver *observer, michael@0: nsIFile *downloadLocation = nullptr) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr downloader = michael@0: do_CreateInstance(NS_DOWNLOADER_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = downloader->Init(observer, downloadLocation); michael@0: if (NS_SUCCEEDED(rv)) michael@0: NS_ADDREF(*result = downloader); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewStreamLoader(nsIStreamLoader **result, michael@0: nsIStreamLoaderObserver *observer) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr loader = michael@0: do_CreateInstance(NS_STREAMLOADER_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = loader->Init(observer); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: *result = nullptr; michael@0: loader.swap(*result); michael@0: } michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewStreamLoader(nsIStreamLoader **result, michael@0: nsIURI *uri, michael@0: nsIStreamLoaderObserver *observer, michael@0: nsISupports *context = nullptr, michael@0: nsILoadGroup *loadGroup = nullptr, michael@0: nsIInterfaceRequestor *callbacks = nullptr, michael@0: uint32_t loadFlags = nsIRequest::LOAD_NORMAL, michael@0: nsIURI *referrer = nullptr) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr channel; michael@0: rv = NS_NewChannel(getter_AddRefs(channel), michael@0: uri, michael@0: nullptr, michael@0: loadGroup, michael@0: callbacks, michael@0: loadFlags); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: nsCOMPtr httpChannel(do_QueryInterface(channel)); michael@0: if (httpChannel) michael@0: httpChannel->SetReferrer(referrer); michael@0: rv = NS_NewStreamLoader(result, observer); michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = channel->AsyncOpen(*result, context); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewUnicharStreamLoader(nsIUnicharStreamLoader **result, michael@0: nsIUnicharStreamLoaderObserver *observer) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr loader = michael@0: do_CreateInstance(NS_UNICHARSTREAMLOADER_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = loader->Init(observer); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: *result = nullptr; michael@0: loader.swap(*result); michael@0: } michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewSyncStreamListener(nsIStreamListener **result, michael@0: nsIInputStream **stream) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr listener = michael@0: do_CreateInstance(NS_SYNCSTREAMLISTENER_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = listener->GetInputStream(stream); michael@0: if (NS_SUCCEEDED(rv)) michael@0: NS_ADDREF(*result = listener); // cannot use nsCOMPtr::swap michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: /** michael@0: * Implement the nsIChannel::Open(nsIInputStream**) method using the channel's michael@0: * AsyncOpen method. michael@0: * michael@0: * NOTE: Reading from the returned nsIInputStream may spin the current michael@0: * thread's event queue, which could result in any event being processed. michael@0: */ michael@0: inline nsresult michael@0: NS_ImplementChannelOpen(nsIChannel *channel, michael@0: nsIInputStream **result) michael@0: { michael@0: nsCOMPtr listener; michael@0: nsCOMPtr stream; michael@0: nsresult rv = NS_NewSyncStreamListener(getter_AddRefs(listener), michael@0: getter_AddRefs(stream)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = channel->AsyncOpen(listener, nullptr); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: uint64_t n; michael@0: // block until the initial response is received or an error occurs. michael@0: rv = stream->Available(&n); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: *result = nullptr; michael@0: stream.swap(*result); michael@0: } michael@0: } michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewRequestObserverProxy(nsIRequestObserver **result, michael@0: nsIRequestObserver *observer, michael@0: nsISupports *context) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr proxy = michael@0: do_CreateInstance(NS_REQUESTOBSERVERPROXY_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = proxy->Init(observer, context); michael@0: if (NS_SUCCEEDED(rv)) michael@0: NS_ADDREF(*result = proxy); // cannot use nsCOMPtr::swap michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewSimpleStreamListener(nsIStreamListener **result, michael@0: nsIOutputStream *sink, michael@0: nsIRequestObserver *observer = nullptr) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr listener = michael@0: do_CreateInstance(NS_SIMPLESTREAMLISTENER_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = listener->Init(sink, observer); michael@0: if (NS_SUCCEEDED(rv)) michael@0: NS_ADDREF(*result = listener); // cannot use nsCOMPtr::swap michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_CheckPortSafety(int32_t port, michael@0: const char *scheme, michael@0: nsIIOService *ioService = nullptr) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr grip; michael@0: rv = net_EnsureIOService(&ioService, grip); michael@0: if (ioService) { michael@0: bool allow; michael@0: rv = ioService->AllowPort(port, scheme, &allow); michael@0: if (NS_SUCCEEDED(rv) && !allow) { michael@0: NS_WARNING("port blocked"); michael@0: rv = NS_ERROR_PORT_ACCESS_NOT_ALLOWED; michael@0: } michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: // Determine if this URI is using a safe port. michael@0: inline nsresult michael@0: NS_CheckPortSafety(nsIURI *uri) { michael@0: int32_t port; michael@0: nsresult rv = uri->GetPort(&port); michael@0: if (NS_FAILED(rv) || port == -1) // port undefined or default-valued michael@0: return NS_OK; michael@0: nsAutoCString scheme; michael@0: uri->GetScheme(scheme); michael@0: return NS_CheckPortSafety(port, scheme.get()); michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewProxyInfo(const nsACString &type, michael@0: const nsACString &host, michael@0: int32_t port, michael@0: uint32_t flags, michael@0: nsIProxyInfo **result) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr pps = michael@0: do_GetService(NS_PROTOCOLPROXYSERVICE_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = pps->NewProxyInfo(type, host, port, flags, UINT32_MAX, nullptr, michael@0: result); michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_GetFileProtocolHandler(nsIFileProtocolHandler **result, michael@0: nsIIOService *ioService = nullptr) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr grip; michael@0: rv = net_EnsureIOService(&ioService, grip); michael@0: if (ioService) { michael@0: nsCOMPtr handler; michael@0: rv = ioService->GetProtocolHandler("file", getter_AddRefs(handler)); michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = CallQueryInterface(handler, result); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_GetFileFromURLSpec(const nsACString &inURL, michael@0: nsIFile **result, michael@0: nsIIOService *ioService = nullptr) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr fileHandler; michael@0: rv = NS_GetFileProtocolHandler(getter_AddRefs(fileHandler), ioService); michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = fileHandler->GetFileFromURLSpec(inURL, result); michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_GetURLSpecFromFile(nsIFile *file, michael@0: nsACString &url, michael@0: nsIIOService *ioService = nullptr) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr fileHandler; michael@0: rv = NS_GetFileProtocolHandler(getter_AddRefs(fileHandler), ioService); michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = fileHandler->GetURLSpecFromFile(file, url); michael@0: return rv; michael@0: } michael@0: michael@0: /** michael@0: * Converts the nsIFile to the corresponding URL string. michael@0: * Should only be called on files which are not directories, michael@0: * is otherwise identical to NS_GetURLSpecFromFile, but is michael@0: * usually more efficient. michael@0: * Warning: this restriction may not be enforced at runtime! michael@0: */ michael@0: inline nsresult michael@0: NS_GetURLSpecFromActualFile(nsIFile *file, michael@0: nsACString &url, michael@0: nsIIOService *ioService = nullptr) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr fileHandler; michael@0: rv = NS_GetFileProtocolHandler(getter_AddRefs(fileHandler), ioService); michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = fileHandler->GetURLSpecFromActualFile(file, url); michael@0: return rv; michael@0: } michael@0: michael@0: /** michael@0: * Converts the nsIFile to the corresponding URL string. michael@0: * Should only be called on files which are directories, michael@0: * is otherwise identical to NS_GetURLSpecFromFile, but is michael@0: * usually more efficient. michael@0: * Warning: this restriction may not be enforced at runtime! michael@0: */ michael@0: inline nsresult michael@0: NS_GetURLSpecFromDir(nsIFile *file, michael@0: nsACString &url, michael@0: nsIIOService *ioService = nullptr) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr fileHandler; michael@0: rv = NS_GetFileProtocolHandler(getter_AddRefs(fileHandler), ioService); michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = fileHandler->GetURLSpecFromDir(file, url); michael@0: return rv; michael@0: } michael@0: michael@0: /** michael@0: * Obtains the referrer for a given channel. This first tries to obtain the michael@0: * referrer from the property docshell.internalReferrer, and if that doesn't michael@0: * work and the channel is an nsIHTTPChannel, we check it's referrer property. michael@0: * michael@0: * @returns NS_ERROR_NOT_AVAILABLE if no referrer is available. michael@0: */ michael@0: inline nsresult michael@0: NS_GetReferrerFromChannel(nsIChannel *channel, michael@0: nsIURI **referrer) michael@0: { michael@0: nsresult rv = NS_ERROR_NOT_AVAILABLE; michael@0: *referrer = nullptr; michael@0: michael@0: nsCOMPtr props(do_QueryInterface(channel)); michael@0: if (props) { michael@0: // We have to check for a property on a property bag because the michael@0: // referrer may be empty for security reasons (for example, when loading michael@0: // an http page with an https referrer). michael@0: rv = props->GetPropertyAsInterface(NS_LITERAL_STRING("docshell.internalReferrer"), michael@0: NS_GET_IID(nsIURI), michael@0: reinterpret_cast(referrer)); michael@0: if (NS_FAILED(rv)) michael@0: *referrer = nullptr; michael@0: } michael@0: michael@0: // if that didn't work, we can still try to get the referrer from the michael@0: // nsIHttpChannel (if we can QI to it) michael@0: if (!(*referrer)) { michael@0: nsCOMPtr chan(do_QueryInterface(channel)); michael@0: if (chan) { michael@0: rv = chan->GetReferrer(referrer); michael@0: if (NS_FAILED(rv)) michael@0: *referrer = nullptr; michael@0: } michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_ParseContentType(const nsACString &rawContentType, michael@0: nsCString &contentType, michael@0: nsCString &contentCharset) michael@0: { michael@0: // contentCharset is left untouched if not present in rawContentType michael@0: nsresult rv; michael@0: nsCOMPtr util = do_GetNetUtil(&rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: nsCString charset; michael@0: bool hadCharset; michael@0: rv = util->ParseContentType(rawContentType, charset, &hadCharset, michael@0: contentType); michael@0: if (NS_SUCCEEDED(rv) && hadCharset) michael@0: contentCharset = charset; michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_ExtractCharsetFromContentType(const nsACString &rawContentType, michael@0: nsCString &contentCharset, michael@0: bool *hadCharset, michael@0: int32_t *charsetStart, michael@0: int32_t *charsetEnd) michael@0: { michael@0: // contentCharset is left untouched if not present in rawContentType michael@0: nsresult rv; michael@0: nsCOMPtr util = do_GetNetUtil(&rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return util->ExtractCharsetFromContentType(rawContentType, michael@0: contentCharset, michael@0: charsetStart, michael@0: charsetEnd, michael@0: hadCharset); michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewLocalFileInputStream(nsIInputStream **result, michael@0: nsIFile *file, michael@0: int32_t ioFlags = -1, michael@0: int32_t perm = -1, michael@0: int32_t behaviorFlags = 0) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr in = michael@0: do_CreateInstance(NS_LOCALFILEINPUTSTREAM_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = in->Init(file, ioFlags, perm, behaviorFlags); michael@0: if (NS_SUCCEEDED(rv)) michael@0: in.forget(result); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewPartialLocalFileInputStream(nsIInputStream **result, michael@0: nsIFile *file, michael@0: uint64_t offset, michael@0: uint64_t length, michael@0: int32_t ioFlags = -1, michael@0: int32_t perm = -1, michael@0: int32_t behaviorFlags = 0) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr in = michael@0: do_CreateInstance(NS_PARTIALLOCALFILEINPUTSTREAM_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = in->Init(file, offset, length, ioFlags, perm, behaviorFlags); michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = CallQueryInterface(in, result); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewLocalFileOutputStream(nsIOutputStream **result, michael@0: nsIFile *file, michael@0: int32_t ioFlags = -1, michael@0: int32_t perm = -1, michael@0: int32_t behaviorFlags = 0) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr out = michael@0: do_CreateInstance(NS_LOCALFILEOUTPUTSTREAM_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = out->Init(file, ioFlags, perm, behaviorFlags); michael@0: if (NS_SUCCEEDED(rv)) michael@0: out.forget(result); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: // returns a file output stream which can be QI'ed to nsISafeOutputStream. michael@0: inline nsresult michael@0: NS_NewAtomicFileOutputStream(nsIOutputStream **result, michael@0: nsIFile *file, michael@0: int32_t ioFlags = -1, michael@0: int32_t perm = -1, michael@0: int32_t behaviorFlags = 0) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr out = michael@0: do_CreateInstance(NS_ATOMICLOCALFILEOUTPUTSTREAM_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = out->Init(file, ioFlags, perm, behaviorFlags); michael@0: if (NS_SUCCEEDED(rv)) michael@0: out.forget(result); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: // returns a file output stream which can be QI'ed to nsISafeOutputStream. michael@0: inline nsresult michael@0: NS_NewSafeLocalFileOutputStream(nsIOutputStream **result, michael@0: nsIFile *file, michael@0: int32_t ioFlags = -1, michael@0: int32_t perm = -1, michael@0: int32_t behaviorFlags = 0) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr out = michael@0: do_CreateInstance(NS_SAFELOCALFILEOUTPUTSTREAM_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = out->Init(file, ioFlags, perm, behaviorFlags); michael@0: if (NS_SUCCEEDED(rv)) michael@0: out.forget(result); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewLocalFileStream(nsIFileStream **result, michael@0: nsIFile *file, michael@0: int32_t ioFlags = -1, michael@0: int32_t perm = -1, michael@0: int32_t behaviorFlags = 0) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr stream = michael@0: do_CreateInstance(NS_LOCALFILESTREAM_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = stream->Init(file, ioFlags, perm, behaviorFlags); michael@0: if (NS_SUCCEEDED(rv)) michael@0: stream.forget(result); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: // returns the input end of a pipe. the output end of the pipe michael@0: // is attached to the original stream. data from the original michael@0: // stream is read into the pipe on a background thread. michael@0: inline nsresult michael@0: NS_BackgroundInputStream(nsIInputStream **result, michael@0: nsIInputStream *stream, michael@0: uint32_t segmentSize = 0, michael@0: uint32_t segmentCount = 0) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr sts = michael@0: do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: nsCOMPtr inTransport; michael@0: rv = sts->CreateInputTransport(stream, int64_t(-1), int64_t(-1), michael@0: true, getter_AddRefs(inTransport)); michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = inTransport->OpenInputStream(nsITransport::OPEN_BLOCKING, michael@0: segmentSize, segmentCount, michael@0: result); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: // returns the output end of a pipe. the input end of the pipe michael@0: // is attached to the original stream. data written to the pipe michael@0: // is copied to the original stream on a background thread. michael@0: inline nsresult michael@0: NS_BackgroundOutputStream(nsIOutputStream **result, michael@0: nsIOutputStream *stream, michael@0: uint32_t segmentSize = 0, michael@0: uint32_t segmentCount = 0) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr sts = michael@0: do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: nsCOMPtr inTransport; michael@0: rv = sts->CreateOutputTransport(stream, int64_t(-1), int64_t(-1), michael@0: true, getter_AddRefs(inTransport)); michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = inTransport->OpenOutputStream(nsITransport::OPEN_BLOCKING, michael@0: segmentSize, segmentCount, michael@0: result); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: MOZ_WARN_UNUSED_RESULT inline nsresult michael@0: NS_NewBufferedInputStream(nsIInputStream **result, michael@0: nsIInputStream *str, michael@0: uint32_t bufferSize) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr in = michael@0: do_CreateInstance(NS_BUFFEREDINPUTSTREAM_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = in->Init(str, bufferSize); michael@0: if (NS_SUCCEEDED(rv)) michael@0: NS_ADDREF(*result = in); // cannot use nsCOMPtr::swap michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: // note: the resulting stream can be QI'ed to nsISafeOutputStream iff the michael@0: // provided stream supports it. michael@0: inline nsresult michael@0: NS_NewBufferedOutputStream(nsIOutputStream **result, michael@0: nsIOutputStream *str, michael@0: uint32_t bufferSize) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr out = michael@0: do_CreateInstance(NS_BUFFEREDOUTPUTSTREAM_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = out->Init(str, bufferSize); michael@0: if (NS_SUCCEEDED(rv)) michael@0: NS_ADDREF(*result = out); // cannot use nsCOMPtr::swap michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: /** michael@0: * Attempts to buffer a given output stream. If this fails, it returns the michael@0: * passed-in output stream. michael@0: * michael@0: * @param aOutputStream michael@0: * The output stream we want to buffer. This cannot be null. michael@0: * @param aBufferSize michael@0: * The size of the buffer for the buffered output stream. michael@0: * @returns an nsIOutputStream that is buffered with the specified buffer size, michael@0: * or is aOutputStream if creating the new buffered stream failed. michael@0: */ michael@0: inline already_AddRefed michael@0: NS_BufferOutputStream(nsIOutputStream *aOutputStream, michael@0: uint32_t aBufferSize) michael@0: { michael@0: NS_ASSERTION(aOutputStream, "No output stream given!"); michael@0: michael@0: nsCOMPtr bos; michael@0: nsresult rv = NS_NewBufferedOutputStream(getter_AddRefs(bos), aOutputStream, michael@0: aBufferSize); michael@0: if (NS_SUCCEEDED(rv)) michael@0: return bos.forget(); michael@0: michael@0: bos = aOutputStream; michael@0: return bos.forget(); michael@0: } michael@0: michael@0: // returns an input stream compatible with nsIUploadChannel::SetUploadStream() michael@0: inline nsresult michael@0: NS_NewPostDataStream(nsIInputStream **result, michael@0: bool isFile, michael@0: const nsACString &data) michael@0: { michael@0: nsresult rv; michael@0: michael@0: if (isFile) { michael@0: nsCOMPtr file; michael@0: nsCOMPtr fileStream; michael@0: michael@0: rv = NS_NewNativeLocalFile(data, false, getter_AddRefs(file)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = NS_NewLocalFileInputStream(getter_AddRefs(fileStream), file); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: // wrap the file stream with a buffered input stream michael@0: rv = NS_NewBufferedInputStream(result, fileStream, 8192); michael@0: } michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: // otherwise, create a string stream for the data (copies) michael@0: nsCOMPtr stream michael@0: (do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv)); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: rv = stream->SetData(data.BeginReading(), data.Length()); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: NS_ADDREF(*result = stream); michael@0: return NS_OK; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_ReadInputStreamToBuffer(nsIInputStream *aInputStream, michael@0: void** aDest, michael@0: uint32_t aCount) michael@0: { michael@0: nsresult rv; michael@0: michael@0: if (!*aDest) { michael@0: *aDest = malloc(aCount); michael@0: if (!*aDest) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: char * p = reinterpret_cast(*aDest); michael@0: uint32_t bytesRead; michael@0: uint32_t totalRead = 0; michael@0: while (1) { michael@0: rv = aInputStream->Read(p + totalRead, aCount - totalRead, &bytesRead); michael@0: if (!NS_SUCCEEDED(rv)) michael@0: return rv; michael@0: totalRead += bytesRead; michael@0: if (totalRead == aCount) michael@0: break; michael@0: // if Read reads 0 bytes, we've hit EOF michael@0: if (bytesRead == 0) michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: // external code can't see fallible_t michael@0: #ifdef MOZILLA_INTERNAL_API michael@0: michael@0: inline nsresult michael@0: NS_ReadInputStreamToString(nsIInputStream *aInputStream, michael@0: nsACString &aDest, michael@0: uint32_t aCount) michael@0: { michael@0: if (!aDest.SetLength(aCount, mozilla::fallible_t())) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: void* dest = aDest.BeginWriting(); michael@0: return NS_ReadInputStreamToBuffer(aInputStream, &dest, aCount); michael@0: } michael@0: michael@0: #endif michael@0: michael@0: inline nsresult michael@0: NS_LoadPersistentPropertiesFromURI(nsIPersistentProperties **result, michael@0: nsIURI *uri, michael@0: nsIIOService *ioService = nullptr) michael@0: { michael@0: nsCOMPtr in; michael@0: nsresult rv = NS_OpenURI(getter_AddRefs(in), uri, ioService); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: nsCOMPtr properties = michael@0: do_CreateInstance(NS_PERSISTENTPROPERTIES_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = properties->Load(in); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: *result = nullptr; michael@0: properties.swap(*result); michael@0: } michael@0: } michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_LoadPersistentPropertiesFromURISpec(nsIPersistentProperties **result, michael@0: const nsACString &spec, michael@0: const char *charset = nullptr, michael@0: nsIURI *baseURI = nullptr, michael@0: nsIIOService *ioService = nullptr) michael@0: { michael@0: nsCOMPtr uri; michael@0: nsresult rv = michael@0: NS_NewURI(getter_AddRefs(uri), spec, charset, baseURI, ioService); michael@0: michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = NS_LoadPersistentPropertiesFromURI(result, uri, ioService); michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: /** michael@0: * NS_QueryNotificationCallbacks implements the canonical algorithm for michael@0: * querying interfaces from a channel's notification callbacks. It first michael@0: * searches the channel's notificationCallbacks attribute, and if the interface michael@0: * is not found there, then it inspects the notificationCallbacks attribute of michael@0: * the channel's loadGroup. michael@0: * michael@0: * Note: templatized only because nsIWebSocketChannel is currently not an michael@0: * nsIChannel. michael@0: */ michael@0: template inline void michael@0: NS_QueryNotificationCallbacks(T *channel, michael@0: const nsIID &iid, michael@0: void **result) michael@0: { michael@0: NS_PRECONDITION(channel, "null channel"); michael@0: *result = nullptr; michael@0: michael@0: nsCOMPtr cbs; michael@0: channel->GetNotificationCallbacks(getter_AddRefs(cbs)); michael@0: if (cbs) michael@0: cbs->GetInterface(iid, result); michael@0: if (!*result) { michael@0: // try load group's notification callbacks... michael@0: nsCOMPtr loadGroup; michael@0: channel->GetLoadGroup(getter_AddRefs(loadGroup)); michael@0: if (loadGroup) { michael@0: loadGroup->GetNotificationCallbacks(getter_AddRefs(cbs)); michael@0: if (cbs) michael@0: cbs->GetInterface(iid, result); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // template helper: michael@0: // Note: "class C" templatized only because nsIWebSocketChannel is currently not michael@0: // an nsIChannel. michael@0: michael@0: template inline void michael@0: NS_QueryNotificationCallbacks(C *channel, michael@0: nsCOMPtr &result) michael@0: { michael@0: NS_QueryNotificationCallbacks(channel, NS_GET_TEMPLATE_IID(T), michael@0: getter_AddRefs(result)); michael@0: } michael@0: michael@0: /** michael@0: * Alternate form of NS_QueryNotificationCallbacks designed for use by michael@0: * nsIChannel implementations. michael@0: */ michael@0: inline void michael@0: NS_QueryNotificationCallbacks(nsIInterfaceRequestor *callbacks, michael@0: nsILoadGroup *loadGroup, michael@0: const nsIID &iid, michael@0: void **result) michael@0: { michael@0: *result = nullptr; michael@0: michael@0: if (callbacks) michael@0: callbacks->GetInterface(iid, result); michael@0: if (!*result) { michael@0: // try load group's notification callbacks... michael@0: if (loadGroup) { michael@0: nsCOMPtr cbs; michael@0: loadGroup->GetNotificationCallbacks(getter_AddRefs(cbs)); michael@0: if (cbs) michael@0: cbs->GetInterface(iid, result); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Returns true if channel is using Private Browsing, or false if not. michael@0: * Returns false if channel's callbacks don't implement nsILoadContext. michael@0: */ michael@0: inline bool michael@0: NS_UsePrivateBrowsing(nsIChannel *channel) michael@0: { michael@0: bool isPrivate = false; michael@0: bool isOverriden = false; michael@0: nsCOMPtr pbChannel = do_QueryInterface(channel); michael@0: if (pbChannel && michael@0: NS_SUCCEEDED(pbChannel->IsPrivateModeOverriden(&isPrivate, &isOverriden)) && michael@0: isOverriden) { michael@0: return isPrivate; michael@0: } michael@0: nsCOMPtr loadContext; michael@0: NS_QueryNotificationCallbacks(channel, loadContext); michael@0: return loadContext && loadContext->UsePrivateBrowsing(); michael@0: } michael@0: michael@0: // Constants duplicated from nsIScriptSecurityManager so we avoid having necko michael@0: // know about script security manager. michael@0: #define NECKO_NO_APP_ID 0 michael@0: #define NECKO_UNKNOWN_APP_ID UINT32_MAX michael@0: // special app id reserved for separating the safebrowsing cookie michael@0: #define NECKO_SAFEBROWSING_APP_ID UINT32_MAX - 1 michael@0: michael@0: /** michael@0: * Gets AppId and isInBrowserElement from channel's nsILoadContext. michael@0: * Returns false if error or channel's callbacks don't implement nsILoadContext. michael@0: */ michael@0: inline bool michael@0: NS_GetAppInfo(nsIChannel *aChannel, uint32_t *aAppID, bool *aIsInBrowserElement) michael@0: { michael@0: nsCOMPtr loadContext; michael@0: NS_QueryNotificationCallbacks(aChannel, loadContext); michael@0: if (!loadContext) { michael@0: return false; michael@0: } michael@0: michael@0: nsresult rv = loadContext->GetAppId(aAppID); michael@0: NS_ENSURE_SUCCESS(rv, false); michael@0: michael@0: rv = loadContext->GetIsInBrowserElement(aIsInBrowserElement); michael@0: NS_ENSURE_SUCCESS(rv, false); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: /** michael@0: * Gets appId and browserOnly parameters from the TOPIC_WEB_APP_CLEAR_DATA michael@0: * nsIObserverService notification. Used when clearing user data or michael@0: * uninstalling web apps. michael@0: */ michael@0: inline nsresult michael@0: NS_GetAppInfoFromClearDataNotification(nsISupports *aSubject, michael@0: uint32_t *aAppID, bool* aBrowserOnly) michael@0: { michael@0: nsresult rv; michael@0: michael@0: nsCOMPtr michael@0: clearParams(do_QueryInterface(aSubject)); michael@0: MOZ_ASSERT(clearParams); michael@0: if (!clearParams) { michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: michael@0: uint32_t appId; michael@0: rv = clearParams->GetAppId(&appId); michael@0: MOZ_ASSERT(NS_SUCCEEDED(rv)); michael@0: MOZ_ASSERT(appId != NECKO_UNKNOWN_APP_ID); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (appId == NECKO_UNKNOWN_APP_ID) { michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: michael@0: bool browserOnly = false; michael@0: rv = clearParams->GetBrowserOnly(&browserOnly); michael@0: MOZ_ASSERT(NS_SUCCEEDED(rv)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: *aAppID = appId; michael@0: *aBrowserOnly = browserOnly; michael@0: return NS_OK; michael@0: } michael@0: michael@0: /** michael@0: * Determines whether appcache should be checked for a given URI. michael@0: */ michael@0: inline bool michael@0: NS_ShouldCheckAppCache(nsIURI *aURI, bool usePrivateBrowsing) michael@0: { michael@0: if (usePrivateBrowsing) { michael@0: return false; michael@0: } michael@0: michael@0: nsCOMPtr offlineService = michael@0: do_GetService("@mozilla.org/offlinecacheupdate-service;1"); michael@0: if (!offlineService) { michael@0: return false; michael@0: } michael@0: michael@0: bool allowed; michael@0: nsresult rv = offlineService->OfflineAppAllowedForURI(aURI, michael@0: nullptr, michael@0: &allowed); michael@0: return NS_SUCCEEDED(rv) && allowed; michael@0: } michael@0: michael@0: inline bool michael@0: NS_ShouldCheckAppCache(nsIPrincipal * aPrincipal, bool usePrivateBrowsing) michael@0: { michael@0: if (usePrivateBrowsing) { michael@0: return false; michael@0: } michael@0: michael@0: nsCOMPtr offlineService = michael@0: do_GetService("@mozilla.org/offlinecacheupdate-service;1"); michael@0: if (!offlineService) { michael@0: return false; michael@0: } michael@0: michael@0: bool allowed; michael@0: nsresult rv = offlineService->OfflineAppAllowed(aPrincipal, michael@0: nullptr, michael@0: &allowed); michael@0: return NS_SUCCEEDED(rv) && allowed; michael@0: } michael@0: michael@0: /** michael@0: * Wraps an nsIAuthPrompt so that it can be used as an nsIAuthPrompt2. This michael@0: * method is provided mainly for use by other methods in this file. michael@0: * michael@0: * *aAuthPrompt2 should be set to null before calling this function. michael@0: */ michael@0: inline void michael@0: NS_WrapAuthPrompt(nsIAuthPrompt *aAuthPrompt, nsIAuthPrompt2** aAuthPrompt2) michael@0: { michael@0: nsCOMPtr factory = michael@0: do_GetService(NS_AUTHPROMPT_ADAPTER_FACTORY_CONTRACTID); michael@0: if (!factory) michael@0: return; michael@0: michael@0: NS_WARNING("Using deprecated nsIAuthPrompt"); michael@0: factory->CreateAdapter(aAuthPrompt, aAuthPrompt2); michael@0: } michael@0: michael@0: /** michael@0: * Gets an auth prompt from an interface requestor. This takes care of wrapping michael@0: * an nsIAuthPrompt so that it can be used as an nsIAuthPrompt2. michael@0: */ michael@0: inline void michael@0: NS_QueryAuthPrompt2(nsIInterfaceRequestor *aCallbacks, michael@0: nsIAuthPrompt2 **aAuthPrompt) michael@0: { michael@0: CallGetInterface(aCallbacks, aAuthPrompt); michael@0: if (*aAuthPrompt) michael@0: return; michael@0: michael@0: // Maybe only nsIAuthPrompt is provided and we have to wrap it. michael@0: nsCOMPtr prompt(do_GetInterface(aCallbacks)); michael@0: if (!prompt) michael@0: return; michael@0: michael@0: NS_WrapAuthPrompt(prompt, aAuthPrompt); michael@0: } michael@0: michael@0: /** michael@0: * Gets an nsIAuthPrompt2 from a channel. Use this instead of michael@0: * NS_QueryNotificationCallbacks for better backwards compatibility. michael@0: */ michael@0: inline void michael@0: NS_QueryAuthPrompt2(nsIChannel *aChannel, michael@0: nsIAuthPrompt2 **aAuthPrompt) michael@0: { michael@0: *aAuthPrompt = nullptr; michael@0: michael@0: // We want to use any auth prompt we can find on the channel's callbacks, michael@0: // and if that fails use the loadgroup's prompt (if any) michael@0: // Therefore, we can't just use NS_QueryNotificationCallbacks, because michael@0: // that would prefer a loadgroup's nsIAuthPrompt2 over a channel's michael@0: // nsIAuthPrompt. michael@0: nsCOMPtr callbacks; michael@0: aChannel->GetNotificationCallbacks(getter_AddRefs(callbacks)); michael@0: if (callbacks) { michael@0: NS_QueryAuthPrompt2(callbacks, aAuthPrompt); michael@0: if (*aAuthPrompt) michael@0: return; michael@0: } michael@0: michael@0: nsCOMPtr group; michael@0: aChannel->GetLoadGroup(getter_AddRefs(group)); michael@0: if (!group) michael@0: return; michael@0: michael@0: group->GetNotificationCallbacks(getter_AddRefs(callbacks)); michael@0: if (!callbacks) michael@0: return; michael@0: NS_QueryAuthPrompt2(callbacks, aAuthPrompt); michael@0: } michael@0: michael@0: /* template helper */ michael@0: template inline void michael@0: NS_QueryNotificationCallbacks(nsIInterfaceRequestor *callbacks, michael@0: nsILoadGroup *loadGroup, michael@0: nsCOMPtr &result) michael@0: { michael@0: NS_QueryNotificationCallbacks(callbacks, loadGroup, michael@0: NS_GET_TEMPLATE_IID(T), michael@0: getter_AddRefs(result)); michael@0: } michael@0: michael@0: /* template helper */ michael@0: template inline void michael@0: NS_QueryNotificationCallbacks(const nsCOMPtr &aCallbacks, michael@0: const nsCOMPtr &aLoadGroup, michael@0: nsCOMPtr &aResult) michael@0: { michael@0: NS_QueryNotificationCallbacks(aCallbacks.get(), aLoadGroup.get(), aResult); michael@0: } michael@0: michael@0: /* template helper */ michael@0: template inline void michael@0: NS_QueryNotificationCallbacks(const nsCOMPtr &aChannel, michael@0: nsCOMPtr &aResult) michael@0: { michael@0: NS_QueryNotificationCallbacks(aChannel.get(), aResult); michael@0: } michael@0: michael@0: /** michael@0: * This function returns a nsIInterfaceRequestor instance that returns the michael@0: * same result as NS_QueryNotificationCallbacks when queried. It is useful michael@0: * as the value for nsISocketTransport::securityCallbacks. michael@0: */ michael@0: inline nsresult michael@0: NS_NewNotificationCallbacksAggregation(nsIInterfaceRequestor *callbacks, michael@0: nsILoadGroup *loadGroup, michael@0: nsIEventTarget *target, michael@0: nsIInterfaceRequestor **result) michael@0: { michael@0: nsCOMPtr cbs; michael@0: if (loadGroup) michael@0: loadGroup->GetNotificationCallbacks(getter_AddRefs(cbs)); michael@0: return NS_NewInterfaceRequestorAggregation(callbacks, cbs, target, result); michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_NewNotificationCallbacksAggregation(nsIInterfaceRequestor *callbacks, michael@0: nsILoadGroup *loadGroup, michael@0: nsIInterfaceRequestor **result) michael@0: { michael@0: return NS_NewNotificationCallbacksAggregation(callbacks, loadGroup, nullptr, result); michael@0: } michael@0: michael@0: /** michael@0: * Helper function for testing online/offline state of the browser. michael@0: */ michael@0: inline bool michael@0: NS_IsOffline() michael@0: { michael@0: bool offline = true; michael@0: nsCOMPtr ios = do_GetIOService(); michael@0: if (ios) michael@0: ios->GetOffline(&offline); michael@0: return offline; michael@0: } michael@0: michael@0: /** michael@0: * Helper functions for implementing nsINestedURI::innermostURI. michael@0: * michael@0: * Note that NS_DoImplGetInnermostURI is "private" -- call michael@0: * NS_ImplGetInnermostURI instead. michael@0: */ michael@0: inline nsresult michael@0: NS_DoImplGetInnermostURI(nsINestedURI* nestedURI, nsIURI** result) michael@0: { michael@0: NS_PRECONDITION(nestedURI, "Must have a nested URI!"); michael@0: NS_PRECONDITION(!*result, "Must have null *result"); michael@0: michael@0: nsCOMPtr inner; michael@0: nsresult rv = nestedURI->GetInnerURI(getter_AddRefs(inner)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // We may need to loop here until we reach the innermost michael@0: // URI. michael@0: nsCOMPtr nestedInner(do_QueryInterface(inner)); michael@0: while (nestedInner) { michael@0: rv = nestedInner->GetInnerURI(getter_AddRefs(inner)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: nestedInner = do_QueryInterface(inner); michael@0: } michael@0: michael@0: // Found the innermost one if we reach here. michael@0: inner.swap(*result); michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_ImplGetInnermostURI(nsINestedURI* nestedURI, nsIURI** result) michael@0: { michael@0: // Make it safe to use swap() michael@0: *result = nullptr; michael@0: michael@0: return NS_DoImplGetInnermostURI(nestedURI, result); michael@0: } michael@0: michael@0: /** michael@0: * Helper function that ensures that |result| is a URI that's safe to michael@0: * return. If |uri| is immutable, just returns it, otherwise returns michael@0: * a clone. |uri| must not be null. michael@0: */ michael@0: inline nsresult michael@0: NS_EnsureSafeToReturn(nsIURI* uri, nsIURI** result) michael@0: { michael@0: NS_PRECONDITION(uri, "Must have a URI"); michael@0: michael@0: // Assume mutable until told otherwise michael@0: bool isMutable = true; michael@0: nsCOMPtr mutableObj(do_QueryInterface(uri)); michael@0: if (mutableObj) { michael@0: nsresult rv = mutableObj->GetMutable(&isMutable); michael@0: isMutable = NS_FAILED(rv) || isMutable; michael@0: } michael@0: michael@0: if (!isMutable) { michael@0: NS_ADDREF(*result = uri); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult rv = uri->Clone(result); michael@0: if (NS_SUCCEEDED(rv) && !*result) { michael@0: NS_ERROR("nsIURI.clone contract was violated"); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: /** michael@0: * Helper function that tries to set the argument URI to be immutable michael@0: */ michael@0: inline void michael@0: NS_TryToSetImmutable(nsIURI* uri) michael@0: { michael@0: nsCOMPtr mutableObj(do_QueryInterface(uri)); michael@0: if (mutableObj) { michael@0: mutableObj->SetMutable(false); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Helper function for calling ToImmutableURI. If all else fails, returns michael@0: * the input URI. The optional second arg indicates whether we had to fall michael@0: * back to the input URI. Passing in a null URI is ok. michael@0: */ michael@0: inline already_AddRefed michael@0: NS_TryToMakeImmutable(nsIURI* uri, michael@0: nsresult* outRv = nullptr) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr util = do_GetNetUtil(&rv); michael@0: michael@0: nsCOMPtr result; michael@0: if (NS_SUCCEEDED(rv)) { michael@0: NS_ASSERTION(util, "do_GetNetUtil lied"); michael@0: rv = util->ToImmutableURI(uri, getter_AddRefs(result)); michael@0: } michael@0: michael@0: if (NS_FAILED(rv)) { michael@0: result = uri; michael@0: } michael@0: michael@0: if (outRv) { michael@0: *outRv = rv; michael@0: } michael@0: michael@0: return result.forget(); michael@0: } michael@0: michael@0: /** michael@0: * Helper function for testing whether the given URI, or any of its michael@0: * inner URIs, has all the given protocol flags. michael@0: */ michael@0: inline nsresult michael@0: NS_URIChainHasFlags(nsIURI *uri, michael@0: uint32_t flags, michael@0: bool *result) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr util = do_GetNetUtil(&rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return util->URIChainHasFlags(uri, flags, result); michael@0: } michael@0: michael@0: /** michael@0: * Helper function for getting the innermost URI for a given URI. The return michael@0: * value could be just the object passed in if it's not a nested URI. michael@0: */ michael@0: inline already_AddRefed michael@0: NS_GetInnermostURI(nsIURI* aURI) michael@0: { michael@0: NS_PRECONDITION(aURI, "Must have URI"); michael@0: michael@0: nsCOMPtr uri = aURI; michael@0: michael@0: nsCOMPtr nestedURI(do_QueryInterface(uri)); michael@0: if (!nestedURI) { michael@0: return uri.forget(); michael@0: } michael@0: michael@0: nsresult rv = nestedURI->GetInnermostURI(getter_AddRefs(uri)); michael@0: if (NS_FAILED(rv)) { michael@0: return nullptr; michael@0: } michael@0: michael@0: return uri.forget(); michael@0: } michael@0: michael@0: /** michael@0: * Get the "final" URI for a channel. This is either the same as GetURI or michael@0: * GetOriginalURI, depending on whether this channel has michael@0: * nsIChanel::LOAD_REPLACE set. For channels without that flag set, the final michael@0: * URI is the original URI, while for ones with the flag the final URI is the michael@0: * channel URI. michael@0: */ michael@0: inline nsresult michael@0: NS_GetFinalChannelURI(nsIChannel* channel, nsIURI** uri) michael@0: { michael@0: *uri = nullptr; michael@0: nsLoadFlags loadFlags = 0; michael@0: nsresult rv = channel->GetLoadFlags(&loadFlags); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if (loadFlags & nsIChannel::LOAD_REPLACE) { michael@0: return channel->GetURI(uri); michael@0: } michael@0: michael@0: return channel->GetOriginalURI(uri); michael@0: } michael@0: michael@0: // NS_SecurityHashURI must return the same hash value for any two URIs that michael@0: // compare equal according to NS_SecurityCompareURIs. Unfortunately, in the michael@0: // case of files, it's not clear we can do anything better than returning michael@0: // the schemeHash, so hashing files degenerates to storing them in a list. michael@0: inline uint32_t michael@0: NS_SecurityHashURI(nsIURI* aURI) michael@0: { michael@0: nsCOMPtr baseURI = NS_GetInnermostURI(aURI); michael@0: michael@0: nsAutoCString scheme; michael@0: uint32_t schemeHash = 0; michael@0: if (NS_SUCCEEDED(baseURI->GetScheme(scheme))) michael@0: schemeHash = mozilla::HashString(scheme); michael@0: michael@0: // TODO figure out how to hash file:// URIs michael@0: if (scheme.EqualsLiteral("file")) michael@0: return schemeHash; // sad face michael@0: michael@0: if (scheme.EqualsLiteral("imap") || michael@0: scheme.EqualsLiteral("mailbox") || michael@0: scheme.EqualsLiteral("news")) michael@0: { michael@0: nsAutoCString spec; michael@0: uint32_t specHash; michael@0: nsresult res = baseURI->GetSpec(spec); michael@0: if (NS_SUCCEEDED(res)) michael@0: specHash = mozilla::HashString(spec); michael@0: else michael@0: specHash = static_cast(res); michael@0: return specHash; michael@0: } michael@0: michael@0: nsAutoCString host; michael@0: uint32_t hostHash = 0; michael@0: if (NS_SUCCEEDED(baseURI->GetAsciiHost(host))) michael@0: hostHash = mozilla::HashString(host); michael@0: michael@0: return mozilla::AddToHash(schemeHash, hostHash, NS_GetRealPort(baseURI)); michael@0: } michael@0: michael@0: inline bool michael@0: NS_SecurityCompareURIs(nsIURI* aSourceURI, michael@0: nsIURI* aTargetURI, michael@0: bool aStrictFileOriginPolicy) michael@0: { michael@0: // Note that this is not an Equals() test on purpose -- for URIs that don't michael@0: // support host/port, we want equality to basically be object identity, for michael@0: // security purposes. Otherwise, for example, two javascript: URIs that michael@0: // are otherwise unrelated could end up "same origin", which would be michael@0: // unfortunate. michael@0: if (aSourceURI && aSourceURI == aTargetURI) michael@0: { michael@0: return true; michael@0: } michael@0: michael@0: if (!aTargetURI || !aSourceURI) michael@0: { michael@0: return false; michael@0: } michael@0: michael@0: // If either URI is a nested URI, get the base URI michael@0: nsCOMPtr sourceBaseURI = NS_GetInnermostURI(aSourceURI); michael@0: nsCOMPtr targetBaseURI = NS_GetInnermostURI(aTargetURI); michael@0: michael@0: // If either uri is an nsIURIWithPrincipal michael@0: nsCOMPtr uriPrinc = do_QueryInterface(sourceBaseURI); michael@0: if (uriPrinc) { michael@0: uriPrinc->GetPrincipalUri(getter_AddRefs(sourceBaseURI)); michael@0: } michael@0: michael@0: uriPrinc = do_QueryInterface(targetBaseURI); michael@0: if (uriPrinc) { michael@0: uriPrinc->GetPrincipalUri(getter_AddRefs(targetBaseURI)); michael@0: } michael@0: michael@0: if (!sourceBaseURI || !targetBaseURI) michael@0: return false; michael@0: michael@0: // Compare schemes michael@0: nsAutoCString targetScheme; michael@0: bool sameScheme = false; michael@0: if (NS_FAILED( targetBaseURI->GetScheme(targetScheme) ) || michael@0: NS_FAILED( sourceBaseURI->SchemeIs(targetScheme.get(), &sameScheme) ) || michael@0: !sameScheme) michael@0: { michael@0: // Not same-origin if schemes differ michael@0: return false; michael@0: } michael@0: michael@0: // For file scheme, reject unless the files are identical. See michael@0: // NS_RelaxStrictFileOriginPolicy for enforcing file same-origin checking michael@0: if (targetScheme.EqualsLiteral("file")) michael@0: { michael@0: // in traditional unsafe behavior all files are the same origin michael@0: if (!aStrictFileOriginPolicy) michael@0: return true; michael@0: michael@0: nsCOMPtr sourceFileURL(do_QueryInterface(sourceBaseURI)); michael@0: nsCOMPtr targetFileURL(do_QueryInterface(targetBaseURI)); michael@0: michael@0: if (!sourceFileURL || !targetFileURL) michael@0: return false; michael@0: michael@0: nsCOMPtr sourceFile, targetFile; michael@0: michael@0: sourceFileURL->GetFile(getter_AddRefs(sourceFile)); michael@0: targetFileURL->GetFile(getter_AddRefs(targetFile)); michael@0: michael@0: if (!sourceFile || !targetFile) michael@0: return false; michael@0: michael@0: // Otherwise they had better match michael@0: bool filesAreEqual = false; michael@0: nsresult rv = sourceFile->Equals(targetFile, &filesAreEqual); michael@0: return NS_SUCCEEDED(rv) && filesAreEqual; michael@0: } michael@0: michael@0: // Special handling for mailnews schemes michael@0: if (targetScheme.EqualsLiteral("imap") || michael@0: targetScheme.EqualsLiteral("mailbox") || michael@0: targetScheme.EqualsLiteral("news")) michael@0: { michael@0: // Each message is a distinct trust domain; use the michael@0: // whole spec for comparison michael@0: nsAutoCString targetSpec; michael@0: nsAutoCString sourceSpec; michael@0: return ( NS_SUCCEEDED( targetBaseURI->GetSpec(targetSpec) ) && michael@0: NS_SUCCEEDED( sourceBaseURI->GetSpec(sourceSpec) ) && michael@0: targetSpec.Equals(sourceSpec) ); michael@0: } michael@0: michael@0: // Compare hosts michael@0: nsAutoCString targetHost; michael@0: nsAutoCString sourceHost; michael@0: if (NS_FAILED( targetBaseURI->GetAsciiHost(targetHost) ) || michael@0: NS_FAILED( sourceBaseURI->GetAsciiHost(sourceHost) )) michael@0: { michael@0: return false; michael@0: } michael@0: michael@0: nsCOMPtr targetURL(do_QueryInterface(targetBaseURI)); michael@0: nsCOMPtr sourceURL(do_QueryInterface(sourceBaseURI)); michael@0: if (!targetURL || !sourceURL) michael@0: { michael@0: return false; michael@0: } michael@0: michael@0: #ifdef MOZILLA_INTERNAL_API michael@0: if (!targetHost.Equals(sourceHost, nsCaseInsensitiveCStringComparator() )) michael@0: #else michael@0: if (!targetHost.Equals(sourceHost, CaseInsensitiveCompare)) michael@0: #endif michael@0: { michael@0: return false; michael@0: } michael@0: michael@0: return NS_GetRealPort(targetBaseURI) == NS_GetRealPort(sourceBaseURI); michael@0: } michael@0: michael@0: inline bool michael@0: NS_URIIsLocalFile(nsIURI *aURI) michael@0: { michael@0: nsCOMPtr util = do_GetNetUtil(); michael@0: michael@0: bool isFile; michael@0: return util && NS_SUCCEEDED(util->ProtocolHasFlags(aURI, michael@0: nsIProtocolHandler::URI_IS_LOCAL_FILE, michael@0: &isFile)) && michael@0: isFile; michael@0: } michael@0: michael@0: // When strict file origin policy is enabled, SecurityCompareURIs will fail for michael@0: // file URIs that do not point to the same local file. This call provides an michael@0: // alternate file-specific origin check that allows target files that are michael@0: // contained in the same directory as the source. michael@0: // michael@0: // https://developer.mozilla.org/en-US/docs/Same-origin_policy_for_file:_URIs michael@0: inline bool michael@0: NS_RelaxStrictFileOriginPolicy(nsIURI *aTargetURI, michael@0: nsIURI *aSourceURI, michael@0: bool aAllowDirectoryTarget = false) michael@0: { michael@0: if (!NS_URIIsLocalFile(aTargetURI)) { michael@0: // This is probably not what the caller intended michael@0: NS_NOTREACHED("NS_RelaxStrictFileOriginPolicy called with non-file URI"); michael@0: return false; michael@0: } michael@0: michael@0: if (!NS_URIIsLocalFile(aSourceURI)) { michael@0: // If the source is not also a file: uri then forget it michael@0: // (don't want resource: principals in a file: doc) michael@0: // michael@0: // note: we're not de-nesting jar: uris here, we want to michael@0: // keep archive content bottled up in its own little island michael@0: return false; michael@0: } michael@0: michael@0: // michael@0: // pull out the internal files michael@0: // michael@0: nsCOMPtr targetFileURL(do_QueryInterface(aTargetURI)); michael@0: nsCOMPtr sourceFileURL(do_QueryInterface(aSourceURI)); michael@0: nsCOMPtr targetFile; michael@0: nsCOMPtr sourceFile; michael@0: bool targetIsDir; michael@0: michael@0: // Make sure targetFile is not a directory (bug 209234) michael@0: // and that it exists w/out unescaping (bug 395343) michael@0: if (!sourceFileURL || !targetFileURL || michael@0: NS_FAILED(targetFileURL->GetFile(getter_AddRefs(targetFile))) || michael@0: NS_FAILED(sourceFileURL->GetFile(getter_AddRefs(sourceFile))) || michael@0: !targetFile || !sourceFile || michael@0: NS_FAILED(targetFile->Normalize()) || michael@0: #ifndef MOZ_WIDGET_ANDROID michael@0: NS_FAILED(sourceFile->Normalize()) || michael@0: #endif michael@0: (!aAllowDirectoryTarget && michael@0: (NS_FAILED(targetFile->IsDirectory(&targetIsDir)) || targetIsDir))) { michael@0: return false; michael@0: } michael@0: michael@0: // michael@0: // If the file to be loaded is in a subdirectory of the source michael@0: // (or same-dir if source is not a directory) then it will michael@0: // inherit its source principal and be scriptable by that source. michael@0: // michael@0: bool sourceIsDir; michael@0: bool allowed = false; michael@0: nsresult rv = sourceFile->IsDirectory(&sourceIsDir); michael@0: if (NS_SUCCEEDED(rv) && sourceIsDir) { michael@0: rv = sourceFile->Contains(targetFile, true, &allowed); michael@0: } else { michael@0: nsCOMPtr sourceParent; michael@0: rv = sourceFile->GetParent(getter_AddRefs(sourceParent)); michael@0: if (NS_SUCCEEDED(rv) && sourceParent) { michael@0: rv = sourceParent->Equals(targetFile, &allowed); michael@0: if (NS_FAILED(rv) || !allowed) { michael@0: rv = sourceParent->Contains(targetFile, true, &allowed); michael@0: } else { michael@0: MOZ_ASSERT(aAllowDirectoryTarget, michael@0: "sourceFile->Parent == targetFile, but targetFile " michael@0: "should've been disallowed if it is a directory"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (NS_SUCCEEDED(rv) && allowed) { michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: inline bool michael@0: NS_IsInternalSameURIRedirect(nsIChannel *aOldChannel, michael@0: nsIChannel *aNewChannel, michael@0: uint32_t aFlags) michael@0: { michael@0: if (!(aFlags & nsIChannelEventSink::REDIRECT_INTERNAL)) { michael@0: return false; michael@0: } michael@0: michael@0: nsCOMPtr oldURI, newURI; michael@0: aOldChannel->GetURI(getter_AddRefs(oldURI)); michael@0: aNewChannel->GetURI(getter_AddRefs(newURI)); michael@0: michael@0: if (!oldURI || !newURI) { michael@0: return false; michael@0: } michael@0: michael@0: bool res; michael@0: return NS_SUCCEEDED(oldURI->Equals(newURI, &res)) && res; michael@0: } michael@0: michael@0: inline nsresult michael@0: NS_LinkRedirectChannels(uint32_t channelId, michael@0: nsIParentChannel *parentChannel, michael@0: nsIChannel** _result) michael@0: { michael@0: nsresult rv; michael@0: michael@0: nsCOMPtr registrar = michael@0: do_GetService("@mozilla.org/redirectchannelregistrar;1", &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return registrar->LinkChannels(channelId, michael@0: parentChannel, michael@0: _result); michael@0: } michael@0: michael@0: /** michael@0: * Helper function to create a random URL string that's properly formed michael@0: * but guaranteed to be invalid. michael@0: */ michael@0: #define NS_FAKE_SCHEME "http://" michael@0: #define NS_FAKE_TLD ".invalid" michael@0: inline nsresult michael@0: NS_MakeRandomInvalidURLString(nsCString& result) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr uuidgen = michael@0: do_GetService("@mozilla.org/uuid-generator;1", &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsID idee; michael@0: rv = uuidgen->GenerateUUIDInPlace(&idee); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: char chars[NSID_LENGTH]; michael@0: idee.ToProvidedString(chars); michael@0: michael@0: result.AssignLiteral(NS_FAKE_SCHEME); michael@0: // Strip off the '{' and '}' at the beginning and end of the UUID michael@0: result.Append(chars + 1, NSID_LENGTH - 3); michael@0: result.AppendLiteral(NS_FAKE_TLD); michael@0: michael@0: return NS_OK; michael@0: } michael@0: #undef NS_FAKE_SCHEME michael@0: #undef NS_FAKE_TLD michael@0: michael@0: /** michael@0: * Helper function to determine whether urlString is Java-compatible -- michael@0: * whether it can be passed to the Java URL(String) constructor without the michael@0: * latter throwing a MalformedURLException, or without Java otherwise michael@0: * mishandling it. This function (in effect) implements a scheme whitelist michael@0: * for Java. michael@0: */ michael@0: inline nsresult michael@0: NS_CheckIsJavaCompatibleURLString(nsCString& urlString, bool *result) michael@0: { michael@0: *result = false; // Default to "no" michael@0: michael@0: nsresult rv = NS_OK; michael@0: nsCOMPtr urlParser = michael@0: do_GetService(NS_STDURLPARSER_CONTRACTID, &rv); michael@0: if (NS_FAILED(rv) || !urlParser) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: bool compatible = true; michael@0: uint32_t schemePos = 0; michael@0: int32_t schemeLen = 0; michael@0: urlParser->ParseURL(urlString.get(), -1, &schemePos, &schemeLen, michael@0: nullptr, nullptr, nullptr, nullptr); michael@0: if (schemeLen != -1) { michael@0: nsCString scheme; michael@0: scheme.Assign(urlString.get() + schemePos, schemeLen); michael@0: // By default Java only understands a small number of URL schemes, and of michael@0: // these only some can legitimately represent a browser page's "origin" michael@0: // (and be something we can legitimately expect Java to handle ... or not michael@0: // to mishandle). michael@0: // michael@0: // Besides those listed below, the OJI plugin understands the "jar", michael@0: // "mailto", "netdoc", "javascript" and "rmi" schemes, and Java Plugin2 michael@0: // also understands the "about" scheme. We actually pass "about" URLs michael@0: // to Java ("about:blank" when processing a javascript: URL (one that michael@0: // calls Java) from the location bar of a blank page, and (in FF4 and up) michael@0: // "about:home" when processing a javascript: URL from the home page). michael@0: // And Java doesn't appear to mishandle them (for example it doesn't allow michael@0: // connections to "about" URLs). But it doesn't make any sense to do michael@0: // same-origin checks on "about" URLs, so we don't include them in our michael@0: // scheme whitelist. michael@0: // michael@0: // The OJI plugin doesn't understand "chrome" URLs (only Java Plugin2 michael@0: // does) -- so we mustn't pass them to the OJI plugin. But we do need to michael@0: // pass "chrome" URLs to Java Plugin2: Java Plugin2 grants additional michael@0: // privileges to chrome "origins", and some extensions take advantage of michael@0: // this. For more information see bug 620773. michael@0: // michael@0: // As of FF4, we no longer support the OJI plugin. michael@0: if (PL_strcasecmp(scheme.get(), "http") && michael@0: PL_strcasecmp(scheme.get(), "https") && michael@0: PL_strcasecmp(scheme.get(), "file") && michael@0: PL_strcasecmp(scheme.get(), "ftp") && michael@0: PL_strcasecmp(scheme.get(), "gopher") && michael@0: PL_strcasecmp(scheme.get(), "chrome")) michael@0: compatible = false; michael@0: } else { michael@0: compatible = false; michael@0: } michael@0: michael@0: *result = compatible; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: /** Given the first (disposition) token from a Content-Disposition header, michael@0: * tell whether it indicates the content is inline or attachment michael@0: * @param aDispToken the disposition token from the content-disposition header michael@0: */ michael@0: inline uint32_t michael@0: NS_GetContentDispositionFromToken(const nsAString& aDispToken) michael@0: { michael@0: // RFC 2183, section 2.8 says that an unknown disposition michael@0: // value should be treated as "attachment" michael@0: // If all of these tests eval to false, then we have a content-disposition of michael@0: // "attachment" or unknown michael@0: if (aDispToken.IsEmpty() || michael@0: aDispToken.LowerCaseEqualsLiteral("inline") || michael@0: // Broken sites just send michael@0: // Content-Disposition: filename="file" michael@0: // without a disposition token... screen those out. michael@0: StringHead(aDispToken, 8).LowerCaseEqualsLiteral("filename")) michael@0: return nsIChannel::DISPOSITION_INLINE; michael@0: michael@0: return nsIChannel::DISPOSITION_ATTACHMENT; michael@0: } michael@0: michael@0: /** Determine the disposition (inline/attachment) of the content based on the michael@0: * Content-Disposition header michael@0: * @param aHeader the content-disposition header (full value) michael@0: * @param aChan the channel the header came from michael@0: */ michael@0: inline uint32_t michael@0: NS_GetContentDispositionFromHeader(const nsACString& aHeader, nsIChannel *aChan = nullptr) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr mimehdrpar = do_GetService(NS_MIMEHEADERPARAM_CONTRACTID, &rv); michael@0: if (NS_FAILED(rv)) michael@0: return nsIChannel::DISPOSITION_ATTACHMENT; michael@0: michael@0: nsAutoCString fallbackCharset; michael@0: if (aChan) { michael@0: nsCOMPtr uri; michael@0: aChan->GetURI(getter_AddRefs(uri)); michael@0: if (uri) michael@0: uri->GetOriginCharset(fallbackCharset); michael@0: } michael@0: michael@0: nsAutoString dispToken; michael@0: rv = mimehdrpar->GetParameterHTTP(aHeader, "", fallbackCharset, true, nullptr, michael@0: dispToken); michael@0: michael@0: if (NS_FAILED(rv)) { michael@0: // special case (see bug 272541): empty disposition type handled as "inline" michael@0: if (rv == NS_ERROR_FIRST_HEADER_FIELD_COMPONENT_EMPTY) michael@0: return nsIChannel::DISPOSITION_INLINE; michael@0: return nsIChannel::DISPOSITION_ATTACHMENT; michael@0: } michael@0: michael@0: return NS_GetContentDispositionFromToken(dispToken); michael@0: } michael@0: michael@0: /** Extracts the filename out of a content-disposition header michael@0: * @param aFilename [out] The filename. Can be empty on error. michael@0: * @param aDisposition Value of a Content-Disposition header michael@0: * @param aURI Optional. Will be used to get a fallback charset for the michael@0: * filename, if it is QI'able to nsIURL michael@0: */ michael@0: inline nsresult michael@0: NS_GetFilenameFromDisposition(nsAString& aFilename, michael@0: const nsACString& aDisposition, michael@0: nsIURI* aURI = nullptr) michael@0: { michael@0: aFilename.Truncate(); michael@0: michael@0: nsresult rv; michael@0: nsCOMPtr mimehdrpar = michael@0: do_GetService(NS_MIMEHEADERPARAM_CONTRACTID, &rv); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: nsCOMPtr url = do_QueryInterface(aURI); michael@0: michael@0: nsAutoCString fallbackCharset; michael@0: if (url) michael@0: url->GetOriginCharset(fallbackCharset); michael@0: // Get the value of 'filename' parameter michael@0: rv = mimehdrpar->GetParameterHTTP(aDisposition, "filename", michael@0: fallbackCharset, true, nullptr, michael@0: aFilename); michael@0: michael@0: if (NS_FAILED(rv)) { michael@0: aFilename.Truncate(); michael@0: return rv; michael@0: } michael@0: michael@0: if (aFilename.IsEmpty()) michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: /** michael@0: * Make sure Personal Security Manager is initialized michael@0: */ michael@0: inline void michael@0: net_EnsurePSMInit() michael@0: { michael@0: nsCOMPtr spserv = michael@0: do_GetService(NS_SOCKETPROVIDERSERVICE_CONTRACTID); michael@0: if (spserv) { michael@0: nsCOMPtr provider; michael@0: spserv->GetSocketProvider("ssl", getter_AddRefs(provider)); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Test whether a URI is "about:blank". |uri| must not be null michael@0: */ michael@0: inline bool michael@0: NS_IsAboutBlank(nsIURI *uri) michael@0: { michael@0: // GetSpec can be expensive for some URIs, so check the scheme first. michael@0: bool isAbout = false; michael@0: if (NS_FAILED(uri->SchemeIs("about", &isAbout)) || !isAbout) { michael@0: return false; michael@0: } michael@0: michael@0: nsAutoCString str; michael@0: uri->GetSpec(str); michael@0: return str.EqualsLiteral("about:blank"); michael@0: } michael@0: michael@0: michael@0: inline nsresult michael@0: NS_GenerateHostPort(const nsCString& host, int32_t port, michael@0: nsCString& hostLine) michael@0: { michael@0: if (strchr(host.get(), ':')) { michael@0: // host is an IPv6 address literal and must be encapsulated in []'s michael@0: hostLine.Assign('['); michael@0: // scope id is not needed for Host header. michael@0: int scopeIdPos = host.FindChar('%'); michael@0: if (scopeIdPos == -1) michael@0: hostLine.Append(host); michael@0: else if (scopeIdPos > 0) michael@0: hostLine.Append(Substring(host, 0, scopeIdPos)); michael@0: else michael@0: return NS_ERROR_MALFORMED_URI; michael@0: hostLine.Append(']'); michael@0: } michael@0: else michael@0: hostLine.Assign(host); michael@0: if (port != -1) { michael@0: hostLine.Append(':'); michael@0: hostLine.AppendInt(port); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: /** michael@0: * Sniff the content type for a given request or a given buffer. michael@0: * michael@0: * aSnifferType can be either NS_CONTENT_SNIFFER_CATEGORY or michael@0: * NS_DATA_SNIFFER_CATEGORY. The function returns the sniffed content type michael@0: * in the aSniffedType argument. This argument will not be modified if the michael@0: * content type could not be sniffed. michael@0: */ michael@0: inline void michael@0: NS_SniffContent(const char* aSnifferType, nsIRequest* aRequest, michael@0: const uint8_t* aData, uint32_t aLength, michael@0: nsACString& aSniffedType) michael@0: { michael@0: typedef nsCategoryCache ContentSnifferCache; michael@0: extern NS_HIDDEN_(ContentSnifferCache*) gNetSniffers; michael@0: extern NS_HIDDEN_(ContentSnifferCache*) gDataSniffers; michael@0: ContentSnifferCache* cache = nullptr; michael@0: if (!strcmp(aSnifferType, NS_CONTENT_SNIFFER_CATEGORY)) { michael@0: if (!gNetSniffers) { michael@0: gNetSniffers = new ContentSnifferCache(NS_CONTENT_SNIFFER_CATEGORY); michael@0: } michael@0: cache = gNetSniffers; michael@0: } else if (!strcmp(aSnifferType, NS_DATA_SNIFFER_CATEGORY)) { michael@0: if (!gDataSniffers) { michael@0: gDataSniffers = new ContentSnifferCache(NS_DATA_SNIFFER_CATEGORY); michael@0: } michael@0: cache = gDataSniffers; michael@0: } else { michael@0: // Invalid content sniffer type was requested michael@0: MOZ_ASSERT(false); michael@0: return; michael@0: } michael@0: michael@0: nsCOMArray sniffers; michael@0: cache->GetEntries(sniffers); michael@0: for (int32_t i = 0; i < sniffers.Count(); ++i) { michael@0: nsresult rv = sniffers[i]->GetMIMETypeFromContent(aRequest, aData, aLength, aSniffedType); michael@0: if (NS_SUCCEEDED(rv) && !aSniffedType.IsEmpty()) { michael@0: return; michael@0: } michael@0: } michael@0: michael@0: aSniffedType.Truncate(); michael@0: } michael@0: michael@0: /** michael@0: * Whether the channel was created to load a srcdoc document. michael@0: * Note that view-source:about:srcdoc is classified as a srcdoc document by michael@0: * this function, which may not be applicable everywhere. michael@0: */ michael@0: inline bool michael@0: NS_IsSrcdocChannel(nsIChannel *aChannel) michael@0: { michael@0: bool isSrcdoc; michael@0: nsCOMPtr isr = do_QueryInterface(aChannel); michael@0: if (isr) { michael@0: isr->GetIsSrcdocChannel(&isSrcdoc); michael@0: return isSrcdoc; michael@0: } michael@0: nsCOMPtr vsc = do_QueryInterface(aChannel); michael@0: if (vsc) { michael@0: vsc->GetIsSrcdocChannel(&isSrcdoc); michael@0: return isSrcdoc; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: #endif // !nsNetUtil_h__