Back to index

lightning-sunbird  0.9+nobinonly
nsNodeInfoManager.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  * 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  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or 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 "nsNodeInfoManager.h"
00039 #include "nsNodeInfo.h"
00040 #include "nsCOMPtr.h"
00041 #include "nsString.h"
00042 #include "nsIAtom.h"
00043 #include "nsIDocument.h"
00044 #include "nsIPrincipal.h"
00045 #include "nsIURI.h"
00046 #include "nsArray.h"
00047 #include "nsContentUtils.h"
00048 #include "nsReadableUtils.h"
00049 
00050 PRUint32 nsNodeInfoManager::gNodeManagerCount;
00051 
00052 PLHashNumber
00053 nsNodeInfoManager::GetNodeInfoInnerHashValue(const void *key)
00054 {
00055   NS_ASSERTION(key, "Null key passed to nsNodeInfo::GetHashValue!");
00056 
00057   const nsINodeInfo::nsNodeInfoInner *node =
00058     NS_REINTERPRET_CAST(const nsINodeInfo::nsNodeInfoInner *, key);
00059 
00060   // Is this an acceptable hash value?
00061   return (PLHashNumber(NS_PTR_TO_INT32(node->mName)) & 0xffff) >> 8;
00062 }
00063 
00064 
00065 PRIntn
00066 nsNodeInfoManager::NodeInfoInnerKeyCompare(const void *key1, const void *key2)
00067 {
00068   NS_ASSERTION(key1 && key2, "Null key passed to NodeInfoInnerKeyCompare!");
00069 
00070   const nsINodeInfo::nsNodeInfoInner *node1 =
00071     NS_REINTERPRET_CAST(const nsINodeInfo::nsNodeInfoInner *, key1);
00072   const nsINodeInfo::nsNodeInfoInner *node2 =
00073     NS_REINTERPRET_CAST(const nsINodeInfo::nsNodeInfoInner *, key2);
00074 
00075   return (node1->mName == node2->mName &&
00076           node1->mPrefix == node2->mPrefix &&
00077           node1->mNamespaceID == node2->mNamespaceID);
00078 }
00079 
00080 
00081 nsNodeInfoManager::nsNodeInfoManager() : mDocument(nsnull)
00082 {
00083   ++gNodeManagerCount;
00084 
00085   mNodeInfoHash = PL_NewHashTable(32, GetNodeInfoInnerHashValue,
00086                                   NodeInfoInnerKeyCompare,
00087                                   PL_CompareValues, nsnull, nsnull);
00088 
00089 #ifdef DEBUG_jst
00090   printf ("Creating NodeInfoManager, gcount = %d\n", gNodeManagerCount);
00091 #endif
00092 }
00093 
00094 
00095 nsNodeInfoManager::~nsNodeInfoManager()
00096 {
00097   --gNodeManagerCount;
00098 
00099   if (mNodeInfoHash)
00100     PL_HashTableDestroy(mNodeInfoHash);
00101 
00102 
00103   if (gNodeManagerCount == 0) {
00104     nsNodeInfo::ClearCache();
00105   }
00106 
00107 #ifdef DEBUG_jst
00108   printf ("Removing NodeInfoManager, gcount = %d\n", gNodeManagerCount);
00109 #endif
00110 }
00111 
00112 
00113 nsrefcnt
00114 nsNodeInfoManager::AddRef()
00115 {
00116   NS_PRECONDITION(PRInt32(mRefCnt) >= 0, "illegal refcnt");
00117 
00118   nsrefcnt count = PR_AtomicIncrement((PRInt32*)&mRefCnt);
00119   NS_LOG_ADDREF(this, count, "nsNodeInfoManager", sizeof(*this));
00120 
00121   return count;
00122 }
00123 
00124 nsrefcnt
00125 nsNodeInfoManager::Release()
00126 {
00127   NS_PRECONDITION(0 != mRefCnt, "dup release");
00128 
00129   nsrefcnt count = PR_AtomicDecrement((PRInt32 *)&mRefCnt);
00130   NS_LOG_RELEASE(this, count, "nsNodeInfoManager");
00131   if (count == 0) {
00132     mRefCnt = 1; /* stabilize */
00133     delete this;
00134   }
00135 
00136   return count;
00137 }
00138 
00139 nsresult
00140 nsNodeInfoManager::Init(nsIDocument *aDocument)
00141 {
00142   NS_ENSURE_TRUE(mNodeInfoHash, NS_ERROR_OUT_OF_MEMORY);
00143 
00144   mDocument = aDocument;
00145   if (aDocument) {
00146     mPrincipal = nsnull;
00147   }
00148 
00149   return NS_OK;
00150 }
00151 
00152 void
00153 nsNodeInfoManager::DropDocumentReference()
00154 {
00155   if (mDocument) {
00156     // If the document has a uri we'll ask for it's principal. Otherwise we'll
00157     // consider this document 'anonymous'. We don't want to call GetPrincipal
00158     // on a document that doesn't have a URI since that'll give an assertion
00159     // that we're creating a principal without having a uri.
00160     // This happens in a few cases where a document is created and then
00161     // immediately dropped without ever getting a URI.
00162     if (mDocument->GetDocumentURI()) {
00163       mPrincipal = mDocument->GetPrincipal();
00164     }
00165   }
00166   mDocument = nsnull;
00167 }
00168 
00169 
00170 nsresult
00171 nsNodeInfoManager::GetNodeInfo(nsIAtom *aName, nsIAtom *aPrefix,
00172                                PRInt32 aNamespaceID, nsINodeInfo** aNodeInfo)
00173 {
00174   NS_ENSURE_ARG_POINTER(aName);
00175   NS_ASSERTION(!aName->Equals(EmptyString()),
00176                "Don't pass an empty string to GetNodeInfo, fix caller.");
00177 
00178   nsINodeInfo::nsNodeInfoInner tmpKey(aName, aPrefix, aNamespaceID);
00179 
00180   void *node = PL_HashTableLookup(mNodeInfoHash, &tmpKey);
00181 
00182   if (node) {
00183     *aNodeInfo = NS_STATIC_CAST(nsINodeInfo *, node);
00184 
00185     NS_ADDREF(*aNodeInfo);
00186 
00187     return NS_OK;
00188   }
00189 
00190   nsNodeInfo *newNodeInfo = nsNodeInfo::Create();
00191   NS_ENSURE_TRUE(newNodeInfo, NS_ERROR_OUT_OF_MEMORY);
00192 
00193   NS_ADDREF(newNodeInfo);
00194 
00195   nsresult rv = newNodeInfo->Init(aName, aPrefix, aNamespaceID, this);
00196   NS_ENSURE_SUCCESS(rv, rv);
00197 
00198   PLHashEntry *he;
00199   he = PL_HashTableAdd(mNodeInfoHash, &newNodeInfo->mInner, newNodeInfo);
00200   NS_ENSURE_TRUE(he, NS_ERROR_OUT_OF_MEMORY);
00201 
00202   *aNodeInfo = newNodeInfo;
00203 
00204   return NS_OK;
00205 }
00206 
00207 
00208 nsresult
00209 nsNodeInfoManager::GetNodeInfo(const nsAString& aName, nsIAtom *aPrefix,
00210                                PRInt32 aNamespaceID, nsINodeInfo** aNodeInfo)
00211 {
00212   nsCOMPtr<nsIAtom> name = do_GetAtom(aName);
00213   return nsNodeInfoManager::GetNodeInfo(name, aPrefix, aNamespaceID,
00214                                         aNodeInfo);
00215 }
00216 
00217 
00218 nsresult
00219 nsNodeInfoManager::GetNodeInfo(const nsAString& aQualifiedName,
00220                                const nsAString& aNamespaceURI,
00221                                nsINodeInfo** aNodeInfo)
00222 {
00223   NS_ENSURE_ARG(!aQualifiedName.IsEmpty());
00224 
00225   nsAString::const_iterator start, end;
00226   aQualifiedName.BeginReading(start);
00227   aQualifiedName.EndReading(end);
00228 
00229   nsCOMPtr<nsIAtom> prefixAtom;
00230 
00231   nsAString::const_iterator iter(start);
00232 
00233   if (FindCharInReadable(':', iter, end)) {
00234     prefixAtom = do_GetAtom(Substring(start, iter));
00235     NS_ENSURE_TRUE(prefixAtom, NS_ERROR_OUT_OF_MEMORY);
00236 
00237     start = ++iter; // step over the ':'
00238 
00239     if (iter == end) {
00240       // No data after the ':'.
00241 
00242       return NS_ERROR_INVALID_ARG;
00243     }
00244   }
00245 
00246   nsCOMPtr<nsIAtom> nameAtom = do_GetAtom(Substring(start, end));
00247   NS_ENSURE_TRUE(nameAtom, NS_ERROR_OUT_OF_MEMORY);
00248 
00249   PRInt32 nsid = kNameSpaceID_None;
00250 
00251   if (!aNamespaceURI.IsEmpty()) {
00252     nsresult rv = nsContentUtils::GetNSManagerWeakRef()->
00253       RegisterNameSpace(aNamespaceURI, nsid);
00254     NS_ENSURE_SUCCESS(rv, rv);
00255   }
00256 
00257   return GetNodeInfo(nameAtom, prefixAtom, nsid, aNodeInfo);
00258 }
00259 
00260 nsIPrincipal*
00261 nsNodeInfoManager::GetDocumentPrincipal()
00262 {
00263   NS_ASSERTION(!mDocument || !mPrincipal,
00264                "how'd we end up with both a document and a principal?");
00265 
00266   if (mDocument) {
00267     // If the document has a uri we'll ask for it's principal. Otherwise we'll
00268     // consider this document 'anonymous'
00269     if (!mDocument->GetDocumentURI()) {
00270       return nsnull;
00271     }
00272 
00273     return mDocument->GetPrincipal();
00274   }
00275 
00276   return mPrincipal;
00277 }
00278 
00279 void
00280 nsNodeInfoManager::SetDocumentPrincipal(nsIPrincipal *aPrincipal)
00281 {
00282   NS_ASSERTION(!mDocument,
00283                "Don't set a principal, we already have a document.");
00284   if (!mDocument) {
00285     mPrincipal = aPrincipal;
00286   }
00287 }
00288 
00289 void
00290 nsNodeInfoManager::RemoveNodeInfo(nsNodeInfo *aNodeInfo)
00291 {
00292   NS_WARN_IF_FALSE(aNodeInfo, "Trying to remove null nodeinfo from manager!");
00293 
00294   if (aNodeInfo) {
00295 #ifdef DEBUG
00296     PRBool ret =
00297 #endif
00298     PL_HashTableRemove(mNodeInfoHash, &aNodeInfo->mInner);
00299 
00300     NS_WARN_IF_FALSE(ret, "Can't find nsINodeInfo to remove!!!");
00301   }
00302 }