Back to index

lightning-sunbird  0.9+nobinonly
nsSimpleChromeHandler.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Minimo.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Doug Turner <dougt@meer.net>.
00018  * Portions created by the Initial Developer are Copyright (C) 2003
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 
00038 #include "nsSimpleChromeHandler.h"
00039 #include "nsIServiceManager.h"
00040 
00041 #include "nsILocalFile.h"
00042 #include "nsDirectoryServiceDefs.h"
00043 #include "nsAppDirectoryServiceDefs.h"
00044 
00045 #include "nsIURL.h"
00046 #include "nsIIOService.h"
00047 
00048 #include "nsNetUtil.h"
00049 #include "nsURLHelper.h"
00050 
00051 #include "nsIGenericFactory.h"
00052 #include "nsSimpleChromeRegistry.h"
00053 
00054 #include "nsURLHelper.h"
00055 #include "nsStandardURL.h"
00056 
00057 class nsSimpleChromeURL : public nsIFileURL
00058 {
00059 public:
00060     nsSimpleChromeURL(nsIFile* file);
00061 
00062     nsresult Init(PRUint32 urlType,
00063                 PRInt32 defaultPort,
00064                 const nsACString &spec,
00065                 const char *charset,
00066                 nsIURI *baseURI);
00067     
00068     NS_DECL_ISUPPORTS
00069     NS_DECL_NSIFILEURL
00070     NS_FORWARD_SAFE_NSIURI(mStandardURL)
00071     NS_FORWARD_SAFE_NSIURL(mStandardURL)
00072 
00073 private:
00074     nsCOMPtr<nsIFile> mChromeDir;
00075     nsCOMPtr<nsIURL>  mStandardURL;
00076 };
00077 
00078 nsSimpleChromeURL::nsSimpleChromeURL(nsIFile *file) : mChromeDir(file)
00079 {
00080 }
00081 
00082 nsresult
00083 nsSimpleChromeURL::Init(PRUint32 urlType,
00084                     PRInt32 defaultPort,
00085                     const nsACString &spec,
00086                     const char *charset,
00087                     nsIURI *baseURI)
00088 
00089 {
00090   nsresult rv;
00091   static NS_DEFINE_CID(kStandardURLCID, NS_STANDARDURL_CID);    
00092   mStandardURL = do_CreateInstance(kStandardURLCID, &rv);
00093   NS_ASSERTION(mStandardURL, "Could not create a Standard URL");
00094   
00095   if (NS_FAILED(rv)) return rv;
00096 
00097   nsCOMPtr<nsIStandardURL> surl = do_QueryInterface(mStandardURL);
00098   return surl->Init(urlType, defaultPort, spec, charset, baseURI);
00099 }
00100 
00101 NS_IMPL_ADDREF(nsSimpleChromeURL)
00102 NS_IMPL_RELEASE(nsSimpleChromeURL)
00103 
00104   // DO we need to implements a QI for equals?
00105 NS_INTERFACE_MAP_BEGIN(nsSimpleChromeURL)
00106     NS_INTERFACE_MAP_ENTRY(nsIURI)
00107     NS_INTERFACE_MAP_ENTRY(nsIURL)
00108     NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIFileURL, mChromeDir)
00109 NS_INTERFACE_MAP_END
00110 
00111 
00112 NS_IMETHODIMP 
00113 nsSimpleChromeURL::SetFile(nsIFile * aFile) { return NS_ERROR_FAILURE; } 
00114 
00115 NS_IMETHODIMP
00116 nsSimpleChromeURL::GetFile(nsIFile **result)
00117 {
00118     nsCAutoString fileName;
00119     GetFileName(fileName);
00120 
00121     nsCOMPtr<nsIFile> newFile;
00122     mChromeDir->Clone(getter_AddRefs(newFile));
00123     nsresult rv = newFile->AppendNative(fileName);
00124     NS_IF_ADDREF(*result = newFile);
00125     return rv;
00126 }
00127 
00128 nsSimpleChromeHandler::nsSimpleChromeHandler()
00129 {
00130 }
00131 
00132 nsSimpleChromeHandler::~nsSimpleChromeHandler()
00133 {
00134 }
00135 
00136 nsresult
00137 nsSimpleChromeHandler::Init()
00138 {
00139     nsresult rv;
00140 
00141     mIOService = do_GetIOService(&rv);
00142     if (NS_FAILED(rv)) return rv;
00143 
00144     rv = NS_GetSpecialDirectory(NS_APP_CHROME_DIR, getter_AddRefs(mChromeDir));
00145     
00146     return rv;
00147 }
00148 
00149 NS_IMPL_THREADSAFE_ISUPPORTS2(nsSimpleChromeHandler,
00150                               nsIProtocolHandler,
00151                               nsISupportsWeakReference)
00152 
00153 NS_IMETHODIMP
00154 nsSimpleChromeHandler::GetScheme(nsACString &result)
00155 {
00156     result = "chrome";
00157     return NS_OK;
00158 }
00159 
00160 NS_IMETHODIMP
00161 nsSimpleChromeHandler::GetDefaultPort(PRInt32 *result)
00162 {
00163     *result = -1;        
00164     return NS_OK;
00165 }
00166 
00167 NS_IMETHODIMP
00168 nsSimpleChromeHandler::GetProtocolFlags(PRUint32 *result)
00169 {
00170     *result = URI_STD;
00171     return NS_OK;
00172 }
00173 
00174 static NS_DEFINE_CID(kStandardURLCID, NS_STANDARDURL_CID);
00175 
00176 NS_IMETHODIMP
00177 nsSimpleChromeHandler::NewURI(const nsACString &aSpec,
00178                              const char *aCharset,
00179                              nsIURI *aBaseURI,
00180                              nsIURI **result)
00181 
00182 {
00183     nsresult rv;
00184 
00185     nsSimpleChromeURL *chromeURL = new nsSimpleChromeURL(mChromeDir);
00186     if (!chromeURL)
00187         return NS_ERROR_OUT_OF_MEMORY;
00188 
00189     NS_ADDREF(chromeURL);
00190 
00191     rv = chromeURL->Init(nsIStandardURL::URLTYPE_STANDARD, -1, aSpec, aCharset, aBaseURI);
00192     if (NS_SUCCEEDED(rv))
00193         rv = CallQueryInterface(chromeURL, result);
00194     
00195     NS_RELEASE(chromeURL);
00196     return rv;
00197 }
00198 
00199 NS_IMETHODIMP
00200 nsSimpleChromeHandler::NewChannel(nsIURI* uri, nsIChannel* *result)
00201 {
00202     nsCOMPtr<nsIURL> url = do_QueryInterface(uri);
00203     if (!url) return NS_ERROR_UNEXPECTED;
00204 
00205     nsCAutoString fileName;
00206     url->GetFileName(fileName);
00207 
00208     nsCOMPtr<nsIFile> newFile;
00209     mChromeDir->Clone(getter_AddRefs(newFile));
00210     newFile->AppendNative(fileName);
00211 
00212     nsCOMPtr<nsIURI> resultingURI;
00213     mIOService->NewFileURI(newFile, getter_AddRefs(resultingURI));
00214 
00215     nsresult rv = NS_NewChannel(result, 
00216                                 resultingURI,
00217                                 mIOService);
00218     
00219     (*result)->SetOriginalURI(uri);
00220     return rv;
00221 }
00222 
00223 NS_IMETHODIMP 
00224 nsSimpleChromeHandler::AllowPort(PRInt32 port, const char *scheme, PRBool *_retval)
00225 {
00226     // don't override anything.  
00227     *_retval = PR_FALSE;
00228     return NS_OK;
00229 }
00230 
00231 #define NS_SIMPLECHROMEPROTOCOLHANDLER_CLASSNAME \
00232     "nsSimpleChromeHandler"
00233 
00234 #define NS_SIMPLECHROMEPROTOCOLHANDLER_CID           \
00235 { /* f6b3c2cc-b2a3-11d7-82a0-000802c1aa31 */         \
00236     0xf6b3c2cc,                                      \
00237     0xb2a3,                                          \
00238     0x11d7,                                          \
00239     {0x82, 0xa0, 0x00, 0x08, 0x02, 0xc1, 0xaa, 0x31} \
00240 }
00241 
00242 
00243 #define NS_SIMPLECHROMEREGISTRY_CID                  \
00244 { /* 5972e8f4-a3a6-44d0-8994-57bf4eeb066d */         \
00245     0x5972e8f4,                                      \
00246     0xa3a6,                                          \
00247     0x44d0,                                          \
00248     {0x89, 0x94, 0x57, 0xbf, 0x4e, 0xeb, 0x06, 0x6d} \
00249 }
00250 
00251 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsSimpleChromeHandler, Init)
00252 NS_GENERIC_FACTORY_CONSTRUCTOR(nsSimpleChromeRegistry)
00253 
00254 static const nsModuleComponentInfo components[] =
00255 {
00256     { "Simple Chrome Registry", 
00257       NS_SIMPLECHROMEREGISTRY_CID,
00258       "@mozilla.org/chrome/chrome-registry;1", 
00259       nsSimpleChromeRegistryConstructor,
00260     },
00261     { NS_SIMPLECHROMEPROTOCOLHANDLER_CLASSNAME,
00262       NS_SIMPLECHROMEPROTOCOLHANDLER_CID,
00263       NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX "chrome",
00264       nsSimpleChromeHandlerConstructor
00265     },
00266 };
00267 
00268 NS_IMPL_NSGETMODULE(chromelite, components)