Back to index

lightning-sunbird  0.9+nobinonly
nsXTFBindableElementWrapper.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) 2005
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Alex Fritze <alex@croczilla.com> (original author)
00024  *   Olli Pettay <Olli.Pettay@helsinki.fi>
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 #include "nsCOMPtr.h"
00041 #include "nsXTFElementWrapper.h"
00042 #include "nsIXTFBindableElement.h"
00043 #include "nsXTFWeakTearoff.h"
00044 #include "nsIXTFBindableElementWrapper.h"
00045 #include "nsXTFInterfaceAggregator.h"
00046 
00047 typedef nsXTFStyledElementWrapper nsXTFBindableElementWrapperBase;
00048 
00050 // nsXTFBindableElementWrapper class
00051 class nsXTFBindableElementWrapper : public nsXTFBindableElementWrapperBase,
00052                                     public nsIXTFBindableElementWrapper
00053 {
00054 protected:
00055   friend nsresult NS_NewXTFBindableElementWrapper(nsIXTFBindableElement* xtfElement,
00056                                                   nsINodeInfo* ni,
00057                                                   nsIContent** aResult);
00058 
00059   nsXTFBindableElementWrapper(nsINodeInfo* aNodeInfo, nsIXTFBindableElement* xtfElement);
00060   virtual ~nsXTFBindableElementWrapper();
00061   nsresult Init();
00062 public:  
00063   // nsISupports interface
00064   NS_DECL_ISUPPORTS_INHERITED
00065 
00066   // nsIXTFElementWrapperPrivate interface
00067   virtual PRUint32 GetElementType() { return nsIXTFElement::ELEMENT_TYPE_BINDABLE; }
00068   
00069   // nsIXTFBindableElementWrapper interface
00070   NS_DECL_NSIXTFBINDABLEELEMENTWRAPPER
00071 
00072   // nsIXTFStyledElementWrapper
00073   NS_FORWARD_NSIXTFSTYLEDELEMENTWRAPPER(nsXTFStyledElementWrapper::)
00074 
00075   // nsIXTFElementWrapper interface
00076   NS_FORWARD_NSIXTFELEMENTWRAPPER(nsXTFBindableElementWrapperBase::)
00077 
00078   NS_IMETHOD GetInterfaces(PRUint32 *count, nsIID * **array);
00079 private:
00080   virtual nsIXTFElement *GetXTFElement() const { return mXTFElement; }
00081   
00082   nsCOMPtr<nsIXTFBindableElement> mXTFElement;
00083 };
00084 
00085 //----------------------------------------------------------------------
00086 // implementation:
00087 
00088 nsXTFBindableElementWrapper::nsXTFBindableElementWrapper(nsINodeInfo* aNodeInfo,
00089                                                          nsIXTFBindableElement* xtfElement)
00090 : nsXTFBindableElementWrapperBase(aNodeInfo), mXTFElement(xtfElement)
00091 {
00092 #ifdef DEBUG
00093 //  printf("nsXTFBindableElementWrapper CTOR\n");
00094 #endif
00095   NS_ASSERTION(mXTFElement, "xtfElement is null");
00096 }
00097 
00098 nsresult
00099 nsXTFBindableElementWrapper::Init()
00100 {
00101   nsXTFBindableElementWrapperBase::Init();
00102   
00103   // pass a weak wrapper (non base object ref-counted), so that
00104   // our mXTFElement can safely addref/release.
00105   nsISupports *weakWrapper = nsnull;
00106   nsresult rv = NS_NewXTFWeakTearoff(NS_GET_IID(nsIXTFBindableElementWrapper),
00107                                      (nsIXTFBindableElementWrapper*)this,
00108                                      &weakWrapper);
00109   if (!weakWrapper) {
00110     NS_ERROR("could not construct weak wrapper");
00111     return rv;
00112   }
00113   
00114   mXTFElement->OnCreated((nsIXTFBindableElementWrapper*)weakWrapper);
00115   NS_RELEASE(weakWrapper);
00116   
00117   return NS_OK;
00118 }
00119 
00120 NS_IMETHODIMP 
00121 nsXTFBindableElementWrapper::GetInterfaces(PRUint32 *count, nsIID * **array)
00122 {
00123   *count = 0;
00124   *array = nsnull;
00125   return NS_OK;
00126 }
00127 
00128 nsXTFBindableElementWrapper::~nsXTFBindableElementWrapper()
00129 {
00130   mXTFElement->OnDestroyed();
00131   mXTFElement = nsnull;
00132   
00133 #ifdef DEBUG
00134 //  printf("nsXTFBindableElementWrapper DTOR\n");
00135 #endif
00136 }
00137 
00138 nsresult
00139 NS_NewXTFBindableElementWrapper(nsIXTFBindableElement* xtfElement,
00140                                nsINodeInfo* ni,
00141                                nsIContent** aResult)
00142 {
00143   *aResult = nsnull;
00144   
00145   if (!xtfElement) {
00146     NS_ERROR("can't construct an xtf wrapper without an xtf element");
00147     return NS_ERROR_INVALID_ARG;
00148   }
00149   
00150   nsXTFBindableElementWrapper* result = new nsXTFBindableElementWrapper(ni, xtfElement);
00151   if (!result)
00152     return NS_ERROR_OUT_OF_MEMORY;
00153 
00154   NS_ADDREF(result);
00155 
00156   nsresult rv = result->Init();
00157 
00158   if (NS_FAILED(rv)) {
00159     NS_RELEASE(result);
00160     return rv;
00161   }
00162   
00163   *aResult = result;
00164   return NS_OK;
00165 }
00166 
00167 //----------------------------------------------------------------------
00168 // nsISupports implementation
00169 
00170 NS_IMPL_ADDREF_INHERITED(nsXTFBindableElementWrapper, nsXTFBindableElementWrapperBase)
00171 NS_IMPL_RELEASE_INHERITED(nsXTFBindableElementWrapper, nsXTFBindableElementWrapperBase)
00172 
00173 NS_IMETHODIMP
00174 nsXTFBindableElementWrapper::QueryInterface(REFNSIID aIID, void** aInstancePtr)
00175 {
00176   nsresult rv;
00177   
00178   if (aIID.Equals(NS_GET_IID(nsIXTFElementWrapperPrivate))) {
00179     *aInstancePtr = NS_STATIC_CAST(nsIXTFElementWrapperPrivate*, this);
00180     NS_ADDREF_THIS();
00181     return NS_OK;
00182   }
00183 
00184   if(aIID.Equals(NS_GET_IID(nsIXTFElementWrapper))) {
00185     *aInstancePtr =
00186       NS_STATIC_CAST(nsIXTFElementWrapper*, 
00187                      NS_STATIC_CAST(nsXTFBindableElementWrapperBase*, this));
00188 
00189     NS_ADDREF_THIS();
00190     return NS_OK;
00191   }
00192 
00193   if (aIID.Equals(NS_GET_IID(nsIXTFBindableElementWrapper))) {
00194     *aInstancePtr = NS_STATIC_CAST(nsIXTFBindableElementWrapper*, this);
00195     NS_ADDREF_THIS();
00196     return NS_OK;
00197   }
00198 
00199   if (aIID.Equals(NS_GET_IID(nsIXTFStyledElementWrapper))) {
00200     *aInstancePtr = NS_STATIC_CAST(nsIXTFStyledElementWrapper*, this);
00201     NS_ADDREF_THIS();
00202     return NS_OK;
00203   }
00204 
00205   // Note, using the nsIClassInfo from nsXMLElement.
00206   if (NS_SUCCEEDED(rv = nsXTFElementWrapperBase::QueryInterface(aIID, aInstancePtr))) {
00207     return rv;
00208   }
00209 
00210   // try to get get the interface from our wrapped element:
00211   nsCOMPtr<nsISupports> inner;
00212   QueryInterfaceInner(aIID, getter_AddRefs(inner));
00213 
00214   if (inner) {
00215     rv = NS_NewXTFInterfaceAggregator(aIID, inner,
00216                                       NS_STATIC_CAST(nsIContent*, this),
00217                                       aInstancePtr);
00218 
00219     return rv;
00220   }
00221 
00222   return NS_ERROR_NO_INTERFACE;
00223 }
00224 
00225 //----------------------------------------------------------------------
00226 // nsIXTFBindableElementWrapper implementation:
00227 
00228 // XXX nothing yet
00229