Back to index

lightning-sunbird  0.9+nobinonly
nsAboutProtocolHandler.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; 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 mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsAboutProtocolHandler.h"
00040 #include "nsIURI.h"
00041 #include "nsIIOService.h"
00042 #include "nsCRT.h"
00043 #include "nsIComponentManager.h"
00044 #include "nsIServiceManager.h"
00045 #include "nsIAboutModule.h"
00046 #include "nsString.h"
00047 #include "nsReadableUtils.h"
00048 #include "nsNetCID.h"
00049 #include "nsAboutProtocolUtils.h"
00050 #include "nsNetError.h"
00051 
00052 static NS_DEFINE_CID(kSimpleURICID,     NS_SIMPLEURI_CID);
00053 
00055 
00056 nsAboutProtocolHandler::nsAboutProtocolHandler()
00057 {
00058 }
00059 
00060 nsresult
00061 nsAboutProtocolHandler::Init()
00062 {
00063     return NS_OK;
00064 }
00065 
00066 nsAboutProtocolHandler::~nsAboutProtocolHandler()
00067 {
00068 }
00069 
00070 NS_IMPL_ISUPPORTS1(nsAboutProtocolHandler, nsIProtocolHandler)
00071 
00072 NS_METHOD
00073 nsAboutProtocolHandler::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
00074 {
00075     if (aOuter)
00076         return NS_ERROR_NO_AGGREGATION;
00077 
00078     nsAboutProtocolHandler* ph = new nsAboutProtocolHandler();
00079     if (ph == nsnull)
00080         return NS_ERROR_OUT_OF_MEMORY;
00081     NS_ADDREF(ph);
00082     nsresult rv = ph->Init();
00083     if (NS_SUCCEEDED(rv)) {
00084         rv = ph->QueryInterface(aIID, aResult);
00085     }
00086     NS_RELEASE(ph);
00087     return rv;
00088 }
00089 
00091 // nsIProtocolHandler methods:
00092 
00093 NS_IMETHODIMP
00094 nsAboutProtocolHandler::GetScheme(nsACString &result)
00095 {
00096     result.AssignLiteral("about");
00097     return NS_OK;
00098 }
00099 
00100 NS_IMETHODIMP
00101 nsAboutProtocolHandler::GetDefaultPort(PRInt32 *result)
00102 {
00103     *result = -1;        // no port for about: URLs
00104     return NS_OK;
00105 }
00106 
00107 NS_IMETHODIMP
00108 nsAboutProtocolHandler::GetProtocolFlags(PRUint32 *result)
00109 {
00110     *result = URI_NORELATIVE | URI_NOAUTH;
00111     return NS_OK;
00112 }
00113 
00114 NS_IMETHODIMP
00115 nsAboutProtocolHandler::NewURI(const nsACString &aSpec,
00116                                const char *aCharset, // ignore charset info
00117                                nsIURI *aBaseURI,
00118                                nsIURI **result)
00119 {
00120     nsresult rv;
00121 
00122     nsIURI* url;
00123     rv = CallCreateInstance(kSimpleURICID, &url);
00124     if (NS_FAILED(rv)) return rv;
00125 
00126     rv = url->SetSpec(aSpec);
00127     if (NS_FAILED(rv)) {
00128         NS_RELEASE(url);
00129         return rv;
00130     }
00131 
00132     *result = url;
00133     return rv;
00134 }
00135 
00136 NS_IMETHODIMP
00137 nsAboutProtocolHandler::NewChannel(nsIURI* uri, nsIChannel* *result)
00138 {
00139     NS_ENSURE_ARG_POINTER(uri);
00140     // about:what you ask?
00141     nsresult rv;
00142     nsCAutoString contractID;
00143     rv = NS_GetAboutModuleName(uri, contractID);
00144     if (NS_FAILED(rv)) return rv;
00145 
00146     // look up a handler to deal with "what"
00147     contractID.Insert(NS_LITERAL_CSTRING(NS_ABOUT_MODULE_CONTRACTID_PREFIX), 0);
00148 
00149     nsCOMPtr<nsIAboutModule> aboutMod(do_GetService(contractID.get(), &rv));
00150     if (NS_SUCCEEDED(rv)) {
00151         // The standard return case:
00152         return aboutMod->NewChannel(uri, result);
00153     }
00154 
00155     // mumble...
00156 
00157     if (rv == NS_ERROR_FACTORY_NOT_REGISTERED) {
00158         // This looks like an about: we don't know about.  Convert
00159         // this to an invalid URI error.
00160         rv = NS_ERROR_MALFORMED_URI;
00161     }
00162 
00163     return rv;
00164 }
00165 
00166 NS_IMETHODIMP 
00167 nsAboutProtocolHandler::AllowPort(PRInt32 port, const char *scheme, PRBool *_retval)
00168 {
00169     // don't override anything.  
00170     *_retval = PR_FALSE;
00171     return NS_OK;
00172 }