fixes bug 97528 "1200 urls created on startup about:blank"

r=dp, sr=dougt
This commit is contained in:
darin%netscape.com 2001-09-07 22:32:50 +00:00
parent b9423a07f4
commit ca77403ab0
9 changed files with 263 additions and 57 deletions

View File

@ -34,6 +34,7 @@
#include "nsIProgressEventSink.h"
#include "nsXPIDLString.h"
#include "nsIJAR.h"
#include "nsIResProtocolHandler.h"
static NS_DEFINE_CID(kFileTransportServiceCID, NS_FILETRANSPORTSERVICE_CID);
static NS_DEFINE_CID(kZipReaderCID, NS_ZIPREADER_CID);
@ -286,6 +287,7 @@ nsresult
nsJARChannel::EnsureJARFileAvailable()
{
nsresult rv;
nsCOMPtr<nsIFileURL> fileURL;
#ifdef PR_LOGGING
nsXPIDLCString jarURLStr;
@ -300,13 +302,28 @@ nsJARChannel::EnsureJARFileAvailable()
rv = mURI->GetJAREntry(&mJAREntry);
if (NS_FAILED(rv)) goto error;
rv = NS_NewDownloader(getter_AddRefs(mDownloader),
mJARBaseURI, this, nsnull, mSynchronousRead,
mLoadGroup, mCallbacks, mLoadFlags);
// try to get a nsIFile directly from the url, which will often succeed.
fileURL = do_QueryInterface(mJARBaseURI);
if (fileURL)
fileURL->GetFile(getter_AddRefs(mDownloadedJARFile));
// if DownloadComplete() was called early, need to release the reference.
if (mSynchronousRead && mSynchronousInputStream)
mDownloader = null_nsCOMPtr();
if (mDownloadedJARFile) {
// after successfully downloading the jar file to the cache,
// start the extraction process:
if (mSynchronousRead)
rv = OpenJARElement();
else
rv = AsyncReadJARElement();
}
else {
rv = NS_NewDownloader(getter_AddRefs(mDownloader),
mJARBaseURI, this, nsnull, mSynchronousRead,
mLoadGroup, mCallbacks, mLoadFlags);
// if DownloadComplete() was called early, need to release the reference.
if (mSynchronousRead && mSynchronousInputStream)
mDownloader = 0;
}
error:
if (NS_FAILED(rv) && mLoadGroup) {

View File

@ -34,6 +34,7 @@
#include "nsIProgressEventSink.h"
#include "nsXPIDLString.h"
#include "nsIJAR.h"
#include "nsIResProtocolHandler.h"
static NS_DEFINE_CID(kFileTransportServiceCID, NS_FILETRANSPORTSERVICE_CID);
static NS_DEFINE_CID(kZipReaderCID, NS_ZIPREADER_CID);
@ -286,6 +287,7 @@ nsresult
nsJARChannel::EnsureJARFileAvailable()
{
nsresult rv;
nsCOMPtr<nsIFileURL> fileURL;
#ifdef PR_LOGGING
nsXPIDLCString jarURLStr;
@ -300,13 +302,28 @@ nsJARChannel::EnsureJARFileAvailable()
rv = mURI->GetJAREntry(&mJAREntry);
if (NS_FAILED(rv)) goto error;
rv = NS_NewDownloader(getter_AddRefs(mDownloader),
mJARBaseURI, this, nsnull, mSynchronousRead,
mLoadGroup, mCallbacks, mLoadFlags);
// try to get a nsIFile directly from the url, which will often succeed.
fileURL = do_QueryInterface(mJARBaseURI);
if (fileURL)
fileURL->GetFile(getter_AddRefs(mDownloadedJARFile));
// if DownloadComplete() was called early, need to release the reference.
if (mSynchronousRead && mSynchronousInputStream)
mDownloader = null_nsCOMPtr();
if (mDownloadedJARFile) {
// after successfully downloading the jar file to the cache,
// start the extraction process:
if (mSynchronousRead)
rv = OpenJARElement();
else
rv = AsyncReadJARElement();
}
else {
rv = NS_NewDownloader(getter_AddRefs(mDownloader),
mJARBaseURI, this, nsnull, mSynchronousRead,
mLoadGroup, mCallbacks, mLoadFlags);
// if DownloadComplete() was called early, need to release the reference.
if (mSynchronousRead && mSynchronousInputStream)
mDownloader = 0;
}
error:
if (NS_FAILED(rv) && mLoadGroup) {

View File

@ -75,4 +75,9 @@ interface nsIResProtocolHandler : nsIProtocolHandler
* Returns true if substitutions are already defined for the specified root.
*/
boolean hasSubstitutions(in string root);
/**
* Resolution utility function.
*/
string resolveURI(in nsIURI resourceURI);
};

View File

@ -31,7 +31,7 @@ LIBRARY_NAME = nkres_s
REQUIRES = xpcom string exthandler mimetype
CPPSRCS = \
nsResChannel.cpp \
nsResURL.cpp \
nsResProtocolHandler.cpp \
$(NULL)

View File

@ -27,7 +27,7 @@ LIBRARY_NAME=nkres_s
LCFLAGS = -DWIN32_LEAN_AND_MEAN -D_IMPL_NS_NET
CPP_OBJS= \
.\$(OBJDIR)\nsResChannel.obj \
.\$(OBJDIR)\nsResURL.obj \
.\$(OBJDIR)\nsResProtocolHandler.obj \
$(NULL)

View File

@ -39,8 +39,6 @@
#include "nsIURL.h"
#include "nsIIOService.h"
#include "nsIServiceManager.h"
#include "nsResChannel.h"
#include "nsIFileChannel.h"
#include "prenv.h"
#include "prmem.h"
#include "prprf.h"
@ -48,16 +46,30 @@
#include "nsIFile.h"
#include "nsDirectoryServiceDefs.h"
#include "nsNetCID.h"
#include "nsNetUtil.h"
#include "nsResURL.h"
static NS_DEFINE_CID(kStandardURLCID, NS_STANDARDURL_CID);
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
////////////////////////////////////////////////////////////////////////////////
nsResProtocolHandler *nsResProtocolHandler::mGlobalInstance = nsnull;
nsResProtocolHandler::nsResProtocolHandler()
: mLock(nsnull), mSubstitutions(32)
{
NS_INIT_REFCNT();
NS_ASSERTION(!mGlobalInstance, "res handler already created!");
mGlobalInstance = this;
}
nsResProtocolHandler::~nsResProtocolHandler()
{
if (mLock)
PR_DestroyLock(mLock);
mGlobalInstance = nsnull;
}
nsresult
@ -68,14 +80,8 @@ nsResProtocolHandler::SetSpecialDir(const char* rootName, const char* sysDir)
rv = NS_GetSpecialDirectory(sysDir, getter_AddRefs(file));
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIFileURL> fileURL(do_CreateInstance(kStandardURLCID, &rv));
if (NS_FAILED(rv)) return rv;
rv = fileURL->SetFile(file);
if (NS_FAILED(rv)) return rv;
nsXPIDLCString spec;
rv = fileURL->GetSpec(getter_Copies(spec));
rv = file->GetURL(getter_Copies(spec));
if (NS_FAILED(rv)) return rv;
return AppendSubstitution(rootName, spec);
@ -86,6 +92,9 @@ nsResProtocolHandler::Init()
{
nsresult rv;
mIOService = do_GetService(kIOServiceCID, &rv);
if (NS_FAILED(rv)) return rv;
mLock = PR_NewLock();
if (mLock == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
@ -134,12 +143,6 @@ nsResProtocolHandler::Init()
return rv;
}
nsResProtocolHandler::~nsResProtocolHandler()
{
if (mLock)
PR_DestroyLock(mLock);
}
NS_IMPL_THREADSAFE_ISUPPORTS3(nsResProtocolHandler, nsIResProtocolHandler, nsIProtocolHandler, nsISupportsWeakReference)
NS_METHOD
@ -192,23 +195,31 @@ nsResProtocolHandler::NewURI(const char *aSpec, nsIURI *aBaseURI,
{
nsresult rv;
nsCOMPtr<nsIURI> url(do_CreateInstance(kStandardURLCID, &rv));
nsCOMPtr<nsIURI> uri(do_CreateInstance(kStandardURLCID, &rv));
if (NS_FAILED(rv)) return rv;
if (aBaseURI) {
nsXPIDLCString aResolvedURI;
rv = aBaseURI->Resolve(aSpec, getter_Copies(aResolvedURI));
if (NS_FAILED(rv)) return rv;
rv = url->SetSpec(aResolvedURI);
rv = uri->SetSpec(aResolvedURI);
} else {
rv = url->SetSpec((char*)aSpec);
rv = uri->SetSpec((char*)aSpec);
}
if (NS_FAILED(rv))
return rv;
*result = url;
NS_ADDREF(*result);
nsResURL *resURL;
NS_NEWXPCOM(resURL, nsResURL);
if (!resURL)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(resURL);
rv = resURL->Init(uri);
if (NS_SUCCEEDED(rv))
rv = CallQueryInterface(resURL, result);
NS_RELEASE(resURL);
return rv;
}
@ -216,19 +227,15 @@ NS_IMETHODIMP
nsResProtocolHandler::NewChannel(nsIURI* uri, nsIChannel* *result)
{
nsresult rv;
nsResChannel* channel;
rv = nsResChannel::Create(nsnull, NS_GET_IID(nsIResChannel), (void**)&channel);
nsXPIDLCString spec;
rv = ResolveURI(uri, getter_Copies(spec));
if (NS_FAILED(rv)) return rv;
rv = channel->Init(this, uri);
if (NS_FAILED(rv)) {
NS_RELEASE(channel);
return rv;
}
rv = mIOService->NewChannel(spec, nsnull, result);
if (NS_FAILED(rv)) return rv;
*result = (nsIChannel*)(nsIResChannel*)channel;
return NS_OK;
return (*result)->SetOriginalURI(uri);
}
NS_IMETHODIMP
@ -246,10 +253,8 @@ nsResProtocolHandler::PrependSubstitution(const char *root, const char *urlStr)
nsresult rv;
nsAutoLock lock(mLock);
nsCOMPtr<nsIIOService> ioServ = do_GetIOService(&rv);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIURI> url;
rv = ioServ->NewURI(urlStr, nsnull, getter_AddRefs(url));
rv = mIOService->NewURI(urlStr, nsnull, getter_AddRefs(url));
if (NS_FAILED(rv)) return rv;
nsCStringKey key(root);
@ -283,10 +288,8 @@ nsResProtocolHandler::AppendSubstitution(const char *root, const char *urlStr)
nsresult rv;
nsAutoLock lock(mLock);
nsCOMPtr<nsIIOService> ioServ = do_GetIOService(&rv);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIURI> url;
rv = ioServ->NewURI(urlStr, nsnull, getter_AddRefs(url));
rv = mIOService->NewURI(urlStr, nsnull, getter_AddRefs(url));
if (NS_FAILED(rv)) return rv;
nsCStringKey key(root);
@ -339,10 +342,8 @@ nsResProtocolHandler::RemoveSubstitution(const char *root, const char *urlStr)
nsresult rv;
nsAutoLock lock(mLock);
nsCOMPtr<nsIIOService> ioServ = do_GetIOService(&rv);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIURI> url;
rv = ioServ->NewURI(urlStr, nsnull, getter_AddRefs(url));
rv = mIOService->NewURI(urlStr, nsnull, getter_AddRefs(url));
if (NS_FAILED(rv)) return rv;
nsCStringKey key(root);
@ -395,4 +396,29 @@ nsResProtocolHandler::HasSubstitutions(const char *root, PRBool *result)
return NS_OK;
}
NS_IMETHODIMP
nsResProtocolHandler::ResolveURI(nsIURI *uri, char **result)
{
nsresult rv;
nsXPIDLCString host, path;
rv = uri->GetHost(getter_Copies(host));
if (NS_FAILED(rv)) return rv;
rv = uri->GetPath(getter_Copies(path));
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsISupportsArray> substitutions;
rv = GetSubstitutions(host.get() ?
host.get() : "", getter_AddRefs(substitutions));
if (NS_FAILED(rv)) return rv;
// always use the first substitution
nsCOMPtr<nsIURI> substURI;
rv = substitutions->GetElementAt(0, getter_AddRefs(substURI));
if (NS_FAILED(rv)) return rv;
return substURI->Resolve(path[0] == '/' ? path+1 : path, result);
}
////////////////////////////////////////////////////////////////////////////////

View File

@ -26,6 +26,7 @@
#include "nsIResProtocolHandler.h"
#include "nsHashtable.h"
#include "nsISupportsArray.h"
#include "nsIIOService.h"
#include "nsWeakReference.h"
class nsResProtocolHandler : public nsIResProtocolHandler, public nsSupportsWeakReference
@ -45,9 +46,14 @@ public:
nsresult Init();
nsresult SetSpecialDir(const char* rootName, const char* specialDir);
protected:
PRLock* mLock;
nsSupportsHashtable mSubstitutions;
static nsResProtocolHandler *get() { return mGlobalInstance; }
private:
static nsResProtocolHandler *mGlobalInstance;
PRLock* mLock;
nsSupportsHashtable mSubstitutions;
nsCOMPtr<nsIIOService> mIOService;
};
#endif /* nsResProtocolHandler_h___ */

View File

@ -0,0 +1,84 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla.
*
* The Initial Developer of the Original Code is Netscape
* Communications. Portions created by Netscape Communications are
* Copyright (C) 2001 by Netscape Communications. All
* Rights Reserved.
*
* Contributor(s):
* Darin Fisher <darin@netscape.com> (original author)
*/
#include "nsResURL.h"
#include "nsResProtocolHandler.h"
#include "nsNetCID.h"
#include "nsILocalFile.h"
#include "nsXPIDLString.h"
static NS_DEFINE_CID(kStandardURLCID, NS_STANDARDURL_CID);
//-----------------------------------------------------------------------------
// nsResURL <public>
//-----------------------------------------------------------------------------
nsresult
nsResURL::Init(nsIURI *uri)
{
nsresult rv;
mStdURL = do_QueryInterface(uri, &rv);
return rv;
}
//-----------------------------------------------------------------------------
// nsResURL::nsISupports
//-----------------------------------------------------------------------------
NS_IMPL_ISUPPORTS3(nsResURL,
nsIURI,
nsIURL,
nsIFileURL)
//-----------------------------------------------------------------------------
// nsResURL::nsIFileURL
//-----------------------------------------------------------------------------
NS_IMETHODIMP
nsResURL::GetFile(nsIFile **result)
{
nsresult rv;
NS_ENSURE_TRUE(nsResProtocolHandler::get(), NS_ERROR_NOT_AVAILABLE);
NS_ENSURE_TRUE(mStdURL, NS_ERROR_NOT_INITIALIZED);
nsXPIDLCString spec;
rv = nsResProtocolHandler::get()->ResolveURI(mStdURL, getter_Copies(spec));
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsILocalFile> localFile =
do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
if (NS_FAILED(rv)) return rv;
rv = localFile->SetURL(spec);
if (NS_FAILED(rv)) return rv;
return CallQueryInterface(localFile, result);
}
NS_IMETHODIMP
nsResURL::SetFile(nsIFile *file)
{
NS_NOTREACHED("nsResURL::SetFile not implemented\n");
return NS_ERROR_NOT_IMPLEMENTED;
}

View File

@ -0,0 +1,51 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla.
*
* The Initial Developer of the Original Code is Netscape
* Communications. Portions created by Netscape Communications are
* Copyright (C) 2001 by Netscape Communications. All
* Rights Reserved.
*
* Contributor(s):
* Darin Fisher <darin@netscape.com> (original author)
*/
#ifndef nsResURL_h__
#define nsResURL_h__
#include "nsIFileChannel.h"
#include "nsCOMPtr.h"
//-----------------------------------------------------------------------------
// nsResURL : overrides nsStdURL::nsIFileURL to provide nsIFile resolution
//-----------------------------------------------------------------------------
class nsResURL : nsIFileURL
{
public:
NS_DECL_ISUPPORTS
NS_FORWARD_SAFE_NSIURI(mStdURL)
NS_FORWARD_SAFE_NSIURL(mStdURL)
NS_DECL_NSIFILEURL
nsResURL() { NS_INIT_ISUPPORTS(); }
virtual ~nsResURL() {}
nsresult Init(nsIURI *);
private:
nsCOMPtr<nsIURL> mStdURL;
};
#endif // nsResURL_h__