Back to index

lightning-sunbird  0.9+nobinonly
nsXPTZipLoader.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the XPT zip reader.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corp.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   John Bandhauer <jband@netscape.com>
00024  *   Alec Flett <alecf@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 
00041 #include "nsXPTZipLoader.h"
00042 #include "nsIZipReader.h"
00043 #include "nsXPIDLString.h"
00044 #include "nsISimpleEnumerator.h"
00045 
00046 static const char gCacheContractID[] =
00047   "@mozilla.org/libjar/zip-reader-cache;1";
00048 static const PRUint32 gCacheSize = 1;
00049 
00050 nsXPTZipLoader::nsXPTZipLoader() {
00051 }
00052 
00053 NS_IMPL_ISUPPORTS1(nsXPTZipLoader, nsIXPTLoader)
00054 
00055 nsresult
00056 nsXPTZipLoader::LoadEntry(nsILocalFile* aFile,
00057                           const char* aName,
00058                           nsIInputStream** aResult)
00059 {
00060     nsCOMPtr<nsIZipReader> zip = dont_AddRef(GetZipReader(aFile));
00061 
00062     if (!zip)
00063         return NS_OK;
00064 
00065     return zip->GetInputStream(aName, aResult);
00066 }
00067     
00068 nsresult
00069 nsXPTZipLoader::EnumerateEntries(nsILocalFile* aFile,
00070                                  nsIXPTLoaderSink* aSink)
00071 {
00072     nsCOMPtr<nsIZipReader> zip = dont_AddRef(GetZipReader(aFile));
00073 
00074     if (!zip) {
00075         NS_WARNING("Could not get Zip Reader");
00076         return NS_OK;
00077     }
00078 
00079     nsCOMPtr<nsISimpleEnumerator> entries;
00080     if (NS_FAILED(zip->FindEntries("*.xpt", getter_AddRefs(entries))) ||
00081         !entries) {
00082         // no problem, just no .xpt files in this archive
00083         return NS_OK;
00084     }
00085 
00086     
00087     PRBool hasMore;
00088     while (NS_SUCCEEDED(entries->HasMoreElements(&hasMore)) && hasMore) {
00089         int index=0;
00090 
00091         nsCOMPtr<nsISupports> sup;
00092         if (NS_FAILED(entries->GetNext(getter_AddRefs(sup))) || !sup)
00093             return NS_ERROR_UNEXPECTED;
00094 
00095         nsCOMPtr<nsIZipEntry> entry = do_QueryInterface(sup);
00096         if (!entry)
00097             return NS_ERROR_UNEXPECTED;
00098 
00099         nsXPIDLCString itemName;
00100         if (NS_FAILED(entry->GetName(getter_Copies(itemName))))
00101             return NS_ERROR_UNEXPECTED;
00102 
00103         nsCOMPtr<nsIInputStream> stream;
00104         if (NS_FAILED(zip->GetInputStream(itemName, getter_AddRefs(stream))))
00105             return NS_ERROR_FAILURE;
00106 
00107         // ignore the result
00108         aSink->FoundEntry(itemName, index++, stream);
00109     }
00110 
00111     return NS_OK;
00112 }
00113 
00114 nsIZipReader*
00115 nsXPTZipLoader::GetZipReader(nsILocalFile* file)
00116 {
00117     NS_ASSERTION(file, "bad file");
00118     
00119     if(!mCache)
00120     {
00121         mCache = do_CreateInstance(gCacheContractID);
00122         if(!mCache || NS_FAILED(mCache->Init(gCacheSize)))
00123             return nsnull;
00124     }
00125 
00126     nsIZipReader* reader = nsnull;
00127 
00128     if(NS_FAILED(mCache->GetZip(file, &reader)))
00129         return nsnull;
00130 
00131     return reader;
00132 }