Back to index

lightning-sunbird  0.9+nobinonly
nsWebNavigationInfo.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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  * Boris Zbarsky <bzbarsky@mit.edu>.
00019  * Portions created by the Initial Developer are Copyright (C) 2005
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsWebNavigationInfo.h"
00039 #include "nsIWebNavigation.h"
00040 #include "nsString.h"
00041 #include "nsServiceManagerUtils.h"
00042 #include "nsIDocumentLoaderFactory.h"
00043 #include "nsIPluginManager.h"
00044 
00045 NS_IMPL_ISUPPORTS1(nsWebNavigationInfo, nsIWebNavigationInfo)
00046 
00047 #define CONTENT_DLF_CONTRACT "@mozilla.org/content/document-loader-factory;1"
00048 #define PLUGIN_DLF_CONTRACT \
00049     "@mozilla.org/content/plugin/document-loader-factory;1"
00050 
00051 nsresult
00052 nsWebNavigationInfo::Init()
00053 {
00054   nsresult rv;
00055   mCategoryManager = do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
00056   NS_ENSURE_SUCCESS(rv, rv);
00057 
00058   mImgLoader = do_GetService("@mozilla.org/image/loader;1", &rv);
00059 
00060   return rv;
00061 }
00062 
00063 NS_IMETHODIMP
00064 nsWebNavigationInfo::IsTypeSupported(const nsACString& aType,
00065                                      nsIWebNavigation* aWebNav,
00066                                      PRUint32* aIsTypeSupported)
00067 {
00068   NS_PRECONDITION(aIsTypeSupported, "null out param?");
00069 
00070   // Note to self: aWebNav could be an nsWebBrowser or an nsDocShell here (or
00071   // an nsSHistory, but not much we can do with that).  So if we start using
00072   // it here, we need to be careful to get to the docshell correctly.
00073   
00074   // For now just report what the Gecko-Content-Viewers category has
00075   // to say for itself.
00076   *aIsTypeSupported = nsIWebNavigationInfo::UNSUPPORTED;
00077 
00078   const nsCString& flatType = PromiseFlatCString(aType);
00079   nsresult rv = IsTypeSupportedInternal(flatType, aIsTypeSupported);
00080   NS_ENSURE_SUCCESS(rv, rv);
00081 
00082   if (*aIsTypeSupported) {
00083     return rv;
00084   }
00085   
00086   // Try reloading plugins in case they've changed.
00087   nsCOMPtr<nsIPluginManager> pluginManager =
00088     do_GetService("@mozilla.org/plugin/manager;1");
00089   if (pluginManager) {
00090     // PR_FALSE will ensure that currently running plugins will not
00091     // be shut down
00092     rv = pluginManager->ReloadPlugins(PR_FALSE);
00093     if (NS_SUCCEEDED(rv)) {
00094       // OK, we reloaded plugins and there were new ones
00095       // (otherwise NS_ERROR_PLUGINS_PLUGINSNOTCHANGED would have
00096       // been returned).  Try checking whether we can handle the
00097       // content now.
00098       return IsTypeSupportedInternal(flatType, aIsTypeSupported);
00099     }
00100   }
00101 
00102   return NS_OK;
00103 }
00104 
00105 nsresult
00106 nsWebNavigationInfo::IsTypeSupportedInternal(const nsCString& aType,
00107                                              PRUint32* aIsSupported)
00108 {
00109   NS_PRECONDITION(mCategoryManager, "Must have category manager");
00110   NS_PRECONDITION(aIsSupported, "Null out param?");
00111 
00112   nsXPIDLCString value;
00113   nsresult rv = mCategoryManager->GetCategoryEntry("Gecko-Content-Viewers",
00114                                                    aType.get(),
00115                                                    getter_Copies(value));
00116 
00117   // If the category manager can't find what we're looking for
00118   // it returns NS_ERROR_NOT_AVAILABLE, we don't want to propagate
00119   // that to the caller since it's really not a failure
00120 
00121   if (NS_FAILED(rv) && rv != NS_ERROR_NOT_AVAILABLE)
00122     return rv;
00123 
00124   // Now try to get an actual document loader factory for this contractid.  If
00125   // there is no contractid, don't try and just return false for *aIsSupported.
00126   nsCOMPtr<nsIDocumentLoaderFactory> docLoaderFactory;
00127   if (!value.IsEmpty()) {
00128     docLoaderFactory = do_GetService(value.get());
00129   }
00130 
00131   // If we got a factory, we should be able to handle this type
00132   if (!docLoaderFactory) {
00133     *aIsSupported = nsIWebNavigationInfo::UNSUPPORTED;
00134   }
00135   else if (value.EqualsLiteral(CONTENT_DLF_CONTRACT)) {
00136     PRBool isImage = PR_FALSE;
00137     mImgLoader->SupportImageWithMimeType(aType.get(), &isImage);
00138     if (isImage) {
00139       *aIsSupported = nsIWebNavigationInfo::IMAGE;
00140     }
00141     else {
00142       *aIsSupported = nsIWebNavigationInfo::OTHER;
00143     }
00144   }
00145   else if (value.EqualsLiteral(PLUGIN_DLF_CONTRACT)) {
00146     *aIsSupported = nsIWebNavigationInfo::PLUGIN;
00147   }
00148   else {
00149     *aIsSupported = nsIWebNavigationInfo::OTHER;
00150   }
00151   
00152   return NS_OK;
00153 }