gecko-dev/chrome/nsChromeProtocolHandler.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

195 строки
5.9 KiB
C++
Исходник Обычный вид История

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=4 sw=2 sts=2 et cin: */
2012-05-21 15:12:37 +04:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
A protocol handler for ``chrome:''
*/
Bit checkin for bug 68045, r/sr=waterson&shaver, second attempt. It all works for me on optimized and debug gcc2.96, rh7.1. - Better failure codes from nsXULPrototypeScript::Deserialize. - Call nsXULDocument::AbortFastLoads after nsXULPrototypeScript::Serialize failure, instead of just nulling the FastLoad service's output stream. - Expose nsXULDocument::AbortFastLoads via nsIXULPrototypeCache, for use from nsChromeProtocolHandler.cpp. AbortFastLoads flushes the XUL cache now, for good measure. - The needless "Current" adjective in nsIFastLoadService attribute and method names is no more. - Add a do_GetFastLoadService() helper, to use CID instead of contractid, and to let the compiler consolidate the static inline CID. - Add "nglayout.debug.checksum_xul_fastload_file" pref so people can do without the checksum verification step when reading a FastLoad file. - Verify the FastLoad file checksum, by default. Also, cache it in the FastLoad service so we don't recompute it when re-opening the FastLoad file as mailnews and other top-levels start up. Fill the checksum cache in EndFastLoad, when the last pseudo-concurrent top-level finishes loading. My hope to compute the checksum while writing the FastLoad file ran afoul of misordered writes. The old code to checksum the in-memory nsFastLoadHeader also was broken on little endian platforms. Now all checksumming is done via a separate read pass over the complete file, save for the header's checksum field, which is summed as if it contained zero. - Track and check FastLoad file dependencies. This required groveling with a bunch of Necko interfaces in nsChromeProtocolHandler::NewChannel -- read it and weep. Dependency checking, as well as checksum access and computation, use better-factored nsIFastLoad{File,Read,Write}Control interfaces. - nsBufferedStream::Seek wasn't flushing the buffer when seeking backward within the buffer, but it must, because mCursor bounds the amount to write if the buffer contains the end of file. - Add an unbufferedStream readonly attribute to nsIStreamBufferAccess, so we don't have to screw around with the bufferying layer when checksumming. Also implement nsIStreamBufferAccess in nsBufferedOutputStream. - nsISeekableOutputStream was bogus, based on a bad state I had put the nsBufferedOutputStream code in on its way from being completely broken when you seek backwards outside of the buffer. Removing this interface required using nsIFastLoadFileIO in nsFastLoadFileWriter, and it also required careful ordering of Close calls (the Reader must close after the Writer or Updater, so that the Reader's underlying, unbuffered input stream can be read by nsFastLoadFileWriter::Close to compute the checksum. - Miscellaneous tab/indentation, comment typo, bracing, if( => if ( style, nsnull vs. 0, useless variable elimination, tortured control flow, AutoString instead of String, and gratuitous ; after nsISupportsUtils.h macro call cleanups.
2001-08-22 00:51:34 +04:00
#include "nsChromeProtocolHandler.h"
#include "nsChromeRegistry.h"
#include "nsCOMPtr.h"
#include "nsContentUtils.h"
#include "nsThreadUtils.h"
#include "nsIChannel.h"
#include "nsIChromeRegistry.h"
Bit checkin for bug 68045, r/sr=waterson&shaver, second attempt. It all works for me on optimized and debug gcc2.96, rh7.1. - Better failure codes from nsXULPrototypeScript::Deserialize. - Call nsXULDocument::AbortFastLoads after nsXULPrototypeScript::Serialize failure, instead of just nulling the FastLoad service's output stream. - Expose nsXULDocument::AbortFastLoads via nsIXULPrototypeCache, for use from nsChromeProtocolHandler.cpp. AbortFastLoads flushes the XUL cache now, for good measure. - The needless "Current" adjective in nsIFastLoadService attribute and method names is no more. - Add a do_GetFastLoadService() helper, to use CID instead of contractid, and to let the compiler consolidate the static inline CID. - Add "nglayout.debug.checksum_xul_fastload_file" pref so people can do without the checksum verification step when reading a FastLoad file. - Verify the FastLoad file checksum, by default. Also, cache it in the FastLoad service so we don't recompute it when re-opening the FastLoad file as mailnews and other top-levels start up. Fill the checksum cache in EndFastLoad, when the last pseudo-concurrent top-level finishes loading. My hope to compute the checksum while writing the FastLoad file ran afoul of misordered writes. The old code to checksum the in-memory nsFastLoadHeader also was broken on little endian platforms. Now all checksumming is done via a separate read pass over the complete file, save for the header's checksum field, which is summed as if it contained zero. - Track and check FastLoad file dependencies. This required groveling with a bunch of Necko interfaces in nsChromeProtocolHandler::NewChannel -- read it and weep. Dependency checking, as well as checksum access and computation, use better-factored nsIFastLoad{File,Read,Write}Control interfaces. - nsBufferedStream::Seek wasn't flushing the buffer when seeking backward within the buffer, but it must, because mCursor bounds the amount to write if the buffer contains the end of file. - Add an unbufferedStream readonly attribute to nsIStreamBufferAccess, so we don't have to screw around with the bufferying layer when checksumming. Also implement nsIStreamBufferAccess in nsBufferedOutputStream. - nsISeekableOutputStream was bogus, based on a bad state I had put the nsBufferedOutputStream code in on its way from being completely broken when you seek backwards outside of the buffer. Removing this interface required using nsIFastLoadFileIO in nsFastLoadFileWriter, and it also required careful ordering of Close calls (the Reader must close after the Writer or Updater, so that the Reader's underlying, unbuffered input stream can be read by nsFastLoadFileWriter::Close to compute the checksum. - Miscellaneous tab/indentation, comment typo, bracing, if( => if ( style, nsnull vs. 0, useless variable elimination, tortured control flow, AutoString instead of String, and gratuitous ; after nsISupportsUtils.h macro call cleanups.
2001-08-22 00:51:34 +04:00
#include "nsIFile.h"
#include "nsIFileChannel.h"
#include "nsIIOService.h"
#include "nsILoadGroup.h"
#include "nsIScriptSecurityManager.h"
#include "nsIStandardURL.h"
#include "nsNetUtil.h"
#include "nsNetCID.h"
#include "nsIURL.h"
#include "nsString.h"
#include "nsStandardURL.h"
////////////////////////////////////////////////////////////////////////////////
NS_IMPL_ISUPPORTS(nsChromeProtocolHandler, nsIProtocolHandler,
nsISupportsWeakReference)
1999-07-02 08:15:43 +04:00
////////////////////////////////////////////////////////////////////////////////
// nsIProtocolHandler methods:
NS_IMETHODIMP
nsChromeProtocolHandler::GetScheme(nsACString& result) {
result.AssignLiteral("chrome");
1999-07-02 08:15:43 +04:00
return NS_OK;
}
NS_IMETHODIMP
nsChromeProtocolHandler::GetDefaultPort(int32_t* result) {
1999-07-02 08:15:43 +04:00
*result = -1; // no port for chrome: URLs
return NS_OK;
}
Bit checkin for bug 68045, r/sr=waterson&shaver, second attempt. It all works for me on optimized and debug gcc2.96, rh7.1. - Better failure codes from nsXULPrototypeScript::Deserialize. - Call nsXULDocument::AbortFastLoads after nsXULPrototypeScript::Serialize failure, instead of just nulling the FastLoad service's output stream. - Expose nsXULDocument::AbortFastLoads via nsIXULPrototypeCache, for use from nsChromeProtocolHandler.cpp. AbortFastLoads flushes the XUL cache now, for good measure. - The needless "Current" adjective in nsIFastLoadService attribute and method names is no more. - Add a do_GetFastLoadService() helper, to use CID instead of contractid, and to let the compiler consolidate the static inline CID. - Add "nglayout.debug.checksum_xul_fastload_file" pref so people can do without the checksum verification step when reading a FastLoad file. - Verify the FastLoad file checksum, by default. Also, cache it in the FastLoad service so we don't recompute it when re-opening the FastLoad file as mailnews and other top-levels start up. Fill the checksum cache in EndFastLoad, when the last pseudo-concurrent top-level finishes loading. My hope to compute the checksum while writing the FastLoad file ran afoul of misordered writes. The old code to checksum the in-memory nsFastLoadHeader also was broken on little endian platforms. Now all checksumming is done via a separate read pass over the complete file, save for the header's checksum field, which is summed as if it contained zero. - Track and check FastLoad file dependencies. This required groveling with a bunch of Necko interfaces in nsChromeProtocolHandler::NewChannel -- read it and weep. Dependency checking, as well as checksum access and computation, use better-factored nsIFastLoad{File,Read,Write}Control interfaces. - nsBufferedStream::Seek wasn't flushing the buffer when seeking backward within the buffer, but it must, because mCursor bounds the amount to write if the buffer contains the end of file. - Add an unbufferedStream readonly attribute to nsIStreamBufferAccess, so we don't have to screw around with the bufferying layer when checksumming. Also implement nsIStreamBufferAccess in nsBufferedOutputStream. - nsISeekableOutputStream was bogus, based on a bad state I had put the nsBufferedOutputStream code in on its way from being completely broken when you seek backwards outside of the buffer. Removing this interface required using nsIFastLoadFileIO in nsFastLoadFileWriter, and it also required careful ordering of Close calls (the Reader must close after the Writer or Updater, so that the Reader's underlying, unbuffered input stream can be read by nsFastLoadFileWriter::Close to compute the checksum. - Miscellaneous tab/indentation, comment typo, bracing, if( => if ( style, nsnull vs. 0, useless variable elimination, tortured control flow, AutoString instead of String, and gratuitous ; after nsISupportsUtils.h macro call cleanups.
2001-08-22 00:51:34 +04:00
NS_IMETHODIMP
nsChromeProtocolHandler::AllowPort(int32_t port, const char* scheme,
bool* _retval) {
Bit checkin for bug 68045, r/sr=waterson&shaver, second attempt. It all works for me on optimized and debug gcc2.96, rh7.1. - Better failure codes from nsXULPrototypeScript::Deserialize. - Call nsXULDocument::AbortFastLoads after nsXULPrototypeScript::Serialize failure, instead of just nulling the FastLoad service's output stream. - Expose nsXULDocument::AbortFastLoads via nsIXULPrototypeCache, for use from nsChromeProtocolHandler.cpp. AbortFastLoads flushes the XUL cache now, for good measure. - The needless "Current" adjective in nsIFastLoadService attribute and method names is no more. - Add a do_GetFastLoadService() helper, to use CID instead of contractid, and to let the compiler consolidate the static inline CID. - Add "nglayout.debug.checksum_xul_fastload_file" pref so people can do without the checksum verification step when reading a FastLoad file. - Verify the FastLoad file checksum, by default. Also, cache it in the FastLoad service so we don't recompute it when re-opening the FastLoad file as mailnews and other top-levels start up. Fill the checksum cache in EndFastLoad, when the last pseudo-concurrent top-level finishes loading. My hope to compute the checksum while writing the FastLoad file ran afoul of misordered writes. The old code to checksum the in-memory nsFastLoadHeader also was broken on little endian platforms. Now all checksumming is done via a separate read pass over the complete file, save for the header's checksum field, which is summed as if it contained zero. - Track and check FastLoad file dependencies. This required groveling with a bunch of Necko interfaces in nsChromeProtocolHandler::NewChannel -- read it and weep. Dependency checking, as well as checksum access and computation, use better-factored nsIFastLoad{File,Read,Write}Control interfaces. - nsBufferedStream::Seek wasn't flushing the buffer when seeking backward within the buffer, but it must, because mCursor bounds the amount to write if the buffer contains the end of file. - Add an unbufferedStream readonly attribute to nsIStreamBufferAccess, so we don't have to screw around with the bufferying layer when checksumming. Also implement nsIStreamBufferAccess in nsBufferedOutputStream. - nsISeekableOutputStream was bogus, based on a bad state I had put the nsBufferedOutputStream code in on its way from being completely broken when you seek backwards outside of the buffer. Removing this interface required using nsIFastLoadFileIO in nsFastLoadFileWriter, and it also required careful ordering of Close calls (the Reader must close after the Writer or Updater, so that the Reader's underlying, unbuffered input stream can be read by nsFastLoadFileWriter::Close to compute the checksum. - Miscellaneous tab/indentation, comment typo, bracing, if( => if ( style, nsnull vs. 0, useless variable elimination, tortured control flow, AutoString instead of String, and gratuitous ; after nsISupportsUtils.h macro call cleanups.
2001-08-22 00:51:34 +04:00
// don't override anything.
*_retval = false;
return NS_OK;
}
NS_IMETHODIMP
nsChromeProtocolHandler::GetProtocolFlags(uint32_t* result) {
*result = URI_STD | URI_IS_UI_RESOURCE | URI_IS_LOCAL_RESOURCE;
return NS_OK;
}
/* static */ nsresult nsChromeProtocolHandler::CreateNewURI(
const nsACString& aSpec, const char* aCharset, nsIURI* aBaseURI,
nsIURI** result) {
// Chrome: URLs (currently) have no additional structure beyond that provided
// by standard URLs, so there is no "outer" given to CreateInstance
nsresult rv;
nsCOMPtr<nsIURI> surl;
nsCOMPtr<nsIURI> base(aBaseURI);
rv = NS_MutateURI(new mozilla::net::nsStandardURL::Mutator())
.Apply(NS_MutatorMethod(&nsIStandardURLMutator::Init,
nsIStandardURL::URLTYPE_STANDARD, -1,
nsCString(aSpec), aCharset, base, nullptr))
.Finalize(surl);
if (NS_FAILED(rv)) {
return rv;
}
// Canonify the "chrome:" URL; e.g., so that we collapse
// "chrome://navigator/content/" and "chrome://navigator/content"
// and "chrome://navigator/content/navigator.xul".
rv = nsChromeRegistry::Canonify(surl);
if (NS_FAILED(rv)) return rv;
surl.forget(result);
return NS_OK;
1999-07-02 08:15:43 +04:00
}
NS_IMETHODIMP
nsChromeProtocolHandler::NewChannel(nsIURI* aURI, nsILoadInfo* aLoadInfo,
nsIChannel** aResult) {
nsresult rv;
NS_ENSURE_ARG_POINTER(aURI);
NS_ENSURE_ARG_POINTER(aLoadInfo);
MOZ_ASSERT(aResult, "Null out param");
#ifdef DEBUG
// Check that the uri we got is already canonified
nsresult debug_rv;
nsCOMPtr<nsIURI> debugURL = aURI;
debug_rv = nsChromeRegistry::Canonify(debugURL);
if (NS_SUCCEEDED(debug_rv)) {
bool same;
debug_rv = aURI->Equals(debugURL, &same);
if (NS_SUCCEEDED(debug_rv)) {
NS_ASSERTION(same,
"Non-canonified chrome uri passed to "
"nsChromeProtocolHandler::NewChannel!");
}
}
#endif
nsCOMPtr<nsIChannel> result;
1999-07-02 08:15:43 +04:00
if (!nsChromeRegistry::gChromeRegistry) {
// We don't actually want this ref, we just want the service to
// initialize if it hasn't already.
nsCOMPtr<nsIChromeRegistry> reg =
mozilla::services::GetChromeRegistryService();
NS_ENSURE_TRUE(nsChromeRegistry::gChromeRegistry, NS_ERROR_FAILURE);
}
nsCOMPtr<nsIURI> resolvedURI;
rv = nsChromeRegistry::gChromeRegistry->ConvertChromeURL(
aURI, getter_AddRefs(resolvedURI));
if (NS_FAILED(rv)) {
#ifdef DEBUG
printf("Couldn't convert chrome URL: %s\n", aURI->GetSpecOrDefault().get());
#endif
return rv;
}
// We don't want to allow the inner protocol handler modify the result
// principal URI since we want either |aURI| or anything pre-set by upper
// layers to prevail.
nsCOMPtr<nsIURI> savedResultPrincipalURI;
rv =
aLoadInfo->GetResultPrincipalURI(getter_AddRefs(savedResultPrincipalURI));
NS_ENSURE_SUCCESS(rv, rv);
rv = NS_NewChannelInternal(getter_AddRefs(result), resolvedURI, aLoadInfo);
NS_ENSURE_SUCCESS(rv, rv);
#ifdef DEBUG
nsCOMPtr<nsIFileChannel> fileChan(do_QueryInterface(result));
if (fileChan) {
nsCOMPtr<nsIFile> file;
fileChan->GetFile(getter_AddRefs(file));
bool exists = false;
file->Exists(&exists);
if (!exists) {
printf("Chrome file doesn't exist: %s\n",
file->HumanReadablePath().get());
}
}
#endif
// Make sure that the channel remembers where it was
// originally loaded from.
rv = aLoadInfo->SetResultPrincipalURI(savedResultPrincipalURI);
NS_ENSURE_SUCCESS(rv, rv);
rv = result->SetOriginalURI(aURI);
if (NS_FAILED(rv)) return rv;
// Get a system principal for content files and set the owner
// property of the result
nsCOMPtr<nsIURL> url = do_QueryInterface(aURI);
nsAutoCString path;
rv = url->GetPathQueryRef(path);
if (StringBeginsWith(path, NS_LITERAL_CSTRING("/content/"))) {
result->SetOwner(nsContentUtils::GetSystemPrincipal());
}
// XXX Removed dependency-tracking code from here, because we're not
// tracking them anyways (with fastload we checked only in DEBUG
// and with startupcache not at all), but this is where we would start
// if we need to re-add.
// See bug 531886, bug 533038.
result->SetContentCharset(NS_LITERAL_CSTRING("UTF-8"));
*aResult = result;
NS_ADDREF(*aResult);
return NS_OK;
1999-07-02 08:15:43 +04:00
}
////////////////////////////////////////////////////////////////////////////////