Back to index

lightning-sunbird  0.9+nobinonly
nsXTFGenericElementWrapper.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 "nsXTFElementWrapper.h"
00041 #include "nsIXTFGenericElement.h"
00042 #include "nsXTFWeakTearoff.h"
00043 #include "nsIXTFGenericElementWrapper.h"
00044 
00045 typedef nsXTFElementWrapper nsXTFGenericElementWrapperBase;
00046 
00048 // nsXTFGenericElementWrapper class
00049 class nsXTFGenericElementWrapper : public nsXTFGenericElementWrapperBase,   
00050                                    public nsIXTFGenericElementWrapper
00051 {
00052 protected:
00053   friend nsresult
00054   NS_NewXTFGenericElementWrapper(nsIXTFGenericElement* xtfElement,
00055                             nsINodeInfo* ni,
00056                             nsIContent** aResult);
00057 
00058   nsXTFGenericElementWrapper(nsINodeInfo* aNodeInfo, nsIXTFGenericElement* xtfElement);
00059   virtual ~nsXTFGenericElementWrapper();
00060   nsresult Init();
00061   
00062 public:  
00063   // nsISupports interface
00064   NS_DECL_ISUPPORTS_INHERITED
00065 
00066   // nsIXTFElementWrapperPrivate interface
00067   virtual PRUint32 GetElementType() { return nsIXTFElement::ELEMENT_TYPE_GENERIC_ELEMENT; }
00068   
00069   // nsIXTFGenericElementWrapper interface
00070   NS_DECL_NSIXTFGENERICELEMENTWRAPPER
00071 
00072   // nsIXTFElementWrapper interface
00073   NS_FORWARD_NSIXTFELEMENTWRAPPER(nsXTFGenericElementWrapperBase::)
00074   
00075 private:
00076   virtual nsIXTFElement *GetXTFElement() const { return mXTFElement; }
00077   
00078   nsCOMPtr<nsIXTFGenericElement> mXTFElement;
00079 };
00080 
00081 //----------------------------------------------------------------------
00082 // implementation:
00083 
00084 nsXTFGenericElementWrapper::nsXTFGenericElementWrapper(nsINodeInfo* aNodeInfo,
00085                                                        nsIXTFGenericElement* xtfElement)
00086     : nsXTFGenericElementWrapperBase(aNodeInfo), mXTFElement(xtfElement)
00087 {
00088 #ifdef DEBUG
00089 //  printf("nsXTFGenericElementWrapper CTOR\n");
00090 #endif
00091   NS_ASSERTION(mXTFElement, "xtfElement is null");
00092 }
00093 
00094 nsresult
00095 nsXTFGenericElementWrapper::Init()
00096 {
00097   nsXTFGenericElementWrapperBase::Init();
00098   
00099   // pass a weak wrapper (non base object ref-counted), so that
00100   // our mXTFElement can safely addref/release.
00101   nsISupports *weakWrapper=nsnull;
00102   NS_NewXTFWeakTearoff(NS_GET_IID(nsIXTFGenericElementWrapper),
00103                        (nsIXTFGenericElementWrapper*)this,
00104                        &weakWrapper);
00105   if (!weakWrapper) {
00106     NS_ERROR("could not construct weak wrapper");
00107     return NS_ERROR_FAILURE;
00108   }
00109   
00110   mXTFElement->OnCreated((nsIXTFGenericElementWrapper*)weakWrapper);
00111   weakWrapper->Release();
00112   
00113   return NS_OK;
00114 }
00115 
00116 nsXTFGenericElementWrapper::~nsXTFGenericElementWrapper()
00117 {
00118   mXTFElement->OnDestroyed();
00119   mXTFElement = nsnull;
00120   
00121 #ifdef DEBUG
00122 //  printf("nsXTFGenericElementWrapper DTOR\n");
00123 #endif
00124 }
00125 
00126 nsresult
00127 NS_NewXTFGenericElementWrapper(nsIXTFGenericElement* xtfElement,
00128                                nsINodeInfo* ni,
00129                                nsIContent** aResult)
00130 {
00131   *aResult = nsnull;
00132   
00133   if (!xtfElement) {
00134     NS_ERROR("can't construct an xtf wrapper without an xtf element");
00135     return NS_ERROR_FAILURE;
00136   }
00137   
00138   nsXTFGenericElementWrapper* result = new nsXTFGenericElementWrapper(ni, xtfElement);
00139   if (!result)
00140     return NS_ERROR_OUT_OF_MEMORY;
00141 
00142   NS_ADDREF(result);
00143 
00144   nsresult rv = result->Init();
00145 
00146   if (NS_FAILED(rv)) {
00147     NS_RELEASE(result);
00148     return rv;
00149   }
00150   
00151   *aResult = result;
00152   return NS_OK;
00153 }
00154 
00155 //----------------------------------------------------------------------
00156 // nsISupports implementation
00157 
00158 
00159 NS_IMPL_ADDREF_INHERITED(nsXTFGenericElementWrapper, nsXTFGenericElementWrapperBase)
00160 NS_IMPL_RELEASE_INHERITED(nsXTFGenericElementWrapper, nsXTFGenericElementWrapperBase)
00161 
00162 NS_INTERFACE_MAP_BEGIN(nsXTFGenericElementWrapper)
00163   NS_INTERFACE_MAP_ENTRY(nsIXTFGenericElementWrapper)
00164 NS_INTERFACE_MAP_END_INHERITING(nsXTFGenericElementWrapperBase)
00165   
00166 //----------------------------------------------------------------------
00167 // nsIXTFGenericElementWrapper implementation:
00168 
00169 // XXX nothing yet
00170