Back to index

lightning-sunbird  0.9+nobinonly
nsXTFService.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 the Mozilla XTF project.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Alex Fritze.
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Alex Fritze <alex@croczilla.com> (original author)
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 "nsCOMPtr.h"
00040 #include "nsINodeInfo.h"
00041 #include "nsIServiceManager.h"
00042 #include "nsIXTFElement.h"
00043 #include "nsIXTFElementFactory.h"
00044 #include "nsIXTFGenericElement.h"
00045 #include "nsIXTFService.h"
00046 #include "nsIXTFXMLVisual.h"
00047 #include "nsIXTFXULVisual.h"
00048 #include "nsInterfaceHashtable.h"
00049 #include "nsString.h"
00050 #include "nsXTFGenericElementWrapper.h"
00051 #include "nsXTFXMLVisualWrapper.h"
00052 #include "nsXTFXULVisualWrapper.h"
00053 #include "nsIXTFBindableElement.h"
00054 #include "nsXTFBindableElementWrapper.h"
00055 
00056 #ifdef MOZ_SVG
00057 #include "nsXTFSVGVisualWrapper.h"
00058 #include "nsIXTFSVGVisual.h"
00059 #endif
00060 
00062 // nsXTFService class 
00063 class nsXTFService : public nsIXTFService
00064 {
00065 protected:
00066   friend nsresult NS_NewXTFService(nsIXTFService** aResult);
00067   
00068   nsXTFService();
00069 
00070 public:
00071   // nsISupports interface
00072   NS_DECL_ISUPPORTS
00073 
00074   // nsIXTFService interface
00075   nsresult CreateElement(nsIContent** aResult, nsINodeInfo* aNodeInfo);
00076 
00077 private:
00078   nsInterfaceHashtable<nsUint32HashKey, nsIXTFElementFactory> mFactoryHash;
00079 };
00080 
00081 //----------------------------------------------------------------------
00082 // implementation:
00083 
00084 nsXTFService::nsXTFService()
00085 {
00086   mFactoryHash.Init(); // XXX this can fail. move to Init()
00087 }
00088 
00089 nsresult
00090 NS_NewXTFService(nsIXTFService** aResult)
00091 {
00092   NS_PRECONDITION(aResult != nsnull, "null ptr");
00093   if (! aResult)
00094     return NS_ERROR_NULL_POINTER;
00095 
00096   nsXTFService* result = new nsXTFService();
00097   if (! result)
00098     return NS_ERROR_OUT_OF_MEMORY;
00099 
00100   NS_ADDREF(result);
00101   *aResult = result;
00102   return NS_OK;
00103 }
00104 
00105 //----------------------------------------------------------------------
00106 // nsISupports methods
00107 
00108 NS_IMPL_ISUPPORTS1(nsXTFService, nsIXTFService)
00109 
00110 //----------------------------------------------------------------------
00111 // nsIXTFService methods
00112 
00113 nsresult
00114 nsXTFService::CreateElement(nsIContent** aResult, nsINodeInfo* aNodeInfo)
00115 {
00116   nsCOMPtr<nsIXTFElementFactory> factory;
00117 
00118   // Check if we have an xtf factory for the given namespaceid in our cache:
00119   if (!mFactoryHash.Get(aNodeInfo->NamespaceID(), getter_AddRefs(factory))) {
00120     // No. See if there is one registered with the component manager:
00121     nsCAutoString xtf_contract_id(NS_XTF_ELEMENT_FACTORY_CONTRACTID_PREFIX);
00122     nsAutoString uri;
00123     aNodeInfo->GetNamespaceURI(uri);
00124     AppendUTF16toUTF8(uri, xtf_contract_id);
00125 #ifdef DEBUG_xtf_verbose
00126     printf("Testing for XTF factory at %s\n", xtf_contract_id.get());
00127 #endif
00128     factory = do_GetService(xtf_contract_id.get());
00129     if (factory) {
00130 #ifdef DEBUG
00131       printf("We've got an XTF factory.\n");
00132 #endif
00133       // Put into hash:
00134       mFactoryHash.Put(aNodeInfo->NamespaceID(), factory);
00135     }
00136   }
00137   if (!factory) return NS_ERROR_FAILURE;
00138 
00139   // We have an xtf factory. Now try to create an element for the given tag name:
00140   nsCOMPtr<nsIXTFElement> elem;
00141   nsAutoString tagName;
00142   aNodeInfo->GetName(tagName);
00143   factory->CreateElement(tagName, getter_AddRefs(elem));
00144   if (!elem) return NS_ERROR_FAILURE;
00145   
00146   // We've got an xtf element. Create an appropriate wrapper for it:
00147   PRUint32 elementType;
00148   elem->GetElementType(&elementType);
00149   switch (elementType) {
00150     case nsIXTFElement::ELEMENT_TYPE_GENERIC_ELEMENT:
00151     {
00152       nsCOMPtr<nsIXTFGenericElement> elem2 = do_QueryInterface(elem);
00153       return NS_NewXTFGenericElementWrapper(elem2, aNodeInfo, aResult);
00154     }
00155     case nsIXTFElement::ELEMENT_TYPE_BINDABLE:
00156     {
00157       nsCOMPtr<nsIXTFBindableElement> elem2 = do_QueryInterface(elem);
00158       return NS_NewXTFBindableElementWrapper(elem2, aNodeInfo, aResult);
00159     }
00160     case nsIXTFElement::ELEMENT_TYPE_SVG_VISUAL:
00161     {
00162 #ifdef MOZ_SVG
00163       nsCOMPtr<nsIXTFSVGVisual> elem2 = do_QueryInterface(elem);
00164       return NS_NewXTFSVGVisualWrapper(elem2, aNodeInfo, aResult);
00165 #else
00166       NS_ERROR("xtf svg visuals are only supported in mozilla builds with native svg support");
00167       break;
00168 #endif
00169     }
00170     case nsIXTFElement::ELEMENT_TYPE_XML_VISUAL:
00171     {
00172       nsCOMPtr<nsIXTFXMLVisual> elem2 = do_QueryInterface(elem);
00173       return NS_NewXTFXMLVisualWrapper(elem2, aNodeInfo, aResult);
00174     }
00175     case nsIXTFElement::ELEMENT_TYPE_XUL_VISUAL:
00176     {
00177       nsCOMPtr<nsIXTFXULVisual> elem2 = do_QueryInterface(elem);
00178       return NS_NewXTFXULVisualWrapper(elem2, aNodeInfo, aResult);
00179     }
00180     default:
00181       NS_ERROR("unknown xtf element type");
00182       break;
00183   }
00184   return NS_ERROR_FAILURE;
00185 }
00186