Back to index

lightning-sunbird  0.9+nobinonly
morkHandle.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-  */
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) 1999
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 #ifndef _MDB_
00039 #include "mdb.h"
00040 #endif
00041 
00042 #ifndef _MORK_
00043 #include "mork.h"
00044 #endif
00045 
00046 #ifndef _MORKENV_
00047 #include "morkEnv.h"
00048 #endif
00049 
00050 #ifndef _MORKFACTORY_
00051 #include "morkFactory.h"
00052 #endif
00053 
00054 #ifndef _MORKPOOL_
00055 #include "morkPool.h"
00056 #endif
00057 
00058 #ifndef _MORKHANDLE_
00059 #include "morkHandle.h"
00060 #endif
00061 
00062 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
00063 
00064 // ````` ````` ````` ````` ````` 
00065 // { ===== begin morkNode interface =====
00066 
00067 /*public virtual*/ void
00068 morkHandle::CloseMorkNode(morkEnv* ev) // CloseHandle() only if open
00069 {
00070   if ( this->IsOpenNode() )
00071   {
00072     this->MarkClosing();
00073     this->CloseHandle(ev);
00074     this->MarkShut();
00075   }
00076 }
00077 
00078 /*public virtual*/
00079 morkHandle::~morkHandle() // assert CloseHandle() executed earlier
00080 {
00081   MORK_ASSERT(mHandle_Env==0);
00082   MORK_ASSERT(mHandle_Face==0);
00083   MORK_ASSERT(mHandle_Object==0);
00084   MORK_ASSERT(mHandle_Magic==0);
00085   MORK_ASSERT(mHandle_Tag==morkHandle_kTag); // should still have correct tag
00086 }
00087 
00088 /*public non-poly*/
00089 morkHandle::morkHandle(morkEnv* ev, // note morkUsage is always morkUsage_kPool
00090     morkHandleFace* ioFace,  // must not be nil, cookie for this handle
00091     morkObject* ioObject,    // must not be nil, the object for this handle
00092     mork_magic inMagic)      // magic sig to denote specific subclass
00093 : morkNode(ev, morkUsage::kPool, (nsIMdbHeap*) 0L)
00094 , mHandle_Tag( 0 )
00095 , mHandle_Env( ev )
00096 , mHandle_Face( ioFace )
00097 , mHandle_Object( 0 )
00098 , mHandle_Magic( 0 )
00099 {
00100   if ( ioFace && ioObject )
00101   {
00102     if ( ev->Good() )
00103     {
00104       mHandle_Tag = morkHandle_kTag;
00105       morkObject::SlotStrongObject(ioObject, ev, &mHandle_Object);
00106       morkHandle::SlotWeakHandle(this, ev, &ioObject->mObject_Handle);
00107       if ( ev->Good() )
00108       {
00109         mHandle_Magic = inMagic;
00110         mNode_Derived = morkDerived_kHandle;
00111       }
00112     }
00113     else
00114       ev->CantMakeWhenBadError();
00115   }
00116   else
00117     ev->NilPointerError();
00118 }
00119 
00120 /*public non-poly*/ void
00121 morkHandle::CloseHandle(morkEnv* ev) // called by CloseMorkNode();
00122 {
00123   if ( this )
00124   {
00125     if ( this->IsNode() )
00126     {
00127       morkObject* obj = mHandle_Object;
00128       mork_bool objDidRefSelf = ( obj && obj->mObject_Handle == this );
00129       if ( objDidRefSelf )
00130         obj->mObject_Handle = 0; // drop the reference
00131       
00132       morkObject::SlotStrongObject((morkObject*) 0, ev, &mHandle_Object);
00133       mHandle_Magic = 0;
00134       // note mHandle_Tag MUST stay morkHandle_kTag for morkNode::ZapOld()
00135       this->MarkShut();
00136 
00137       if ( objDidRefSelf )
00138         this->CutWeakRef(ev); // do last, because it might self destroy
00139     }
00140     else
00141       this->NonNodeError(ev);
00142   }
00143   else
00144     ev->NilPointerError();
00145 }
00146 
00147 // } ===== end morkNode methods =====
00148 // ````` ````` ````` ````` ````` 
00149 
00150 void morkHandle::NilFactoryError(morkEnv* ev) const
00151 {
00152   ev->NewError("nil mHandle_Factory");
00153 }
00154   
00155 void morkHandle::NilHandleObjectError(morkEnv* ev) const
00156 {
00157   ev->NewError("nil mHandle_Object");
00158 }
00159   
00160 void morkHandle::NonNodeObjectError(morkEnv* ev) const
00161 {
00162   ev->NewError("non-node mHandle_Object");
00163 }
00164   
00165 void morkHandle::NonOpenObjectError(morkEnv* ev) const
00166 {
00167   ev->NewError("non-open mHandle_Object");
00168 }
00169   
00170 void morkHandle::NewBadMagicHandleError(morkEnv* ev, mork_magic inMagic) const
00171 {
00172   MORK_USED_1(inMagic);
00173   ev->NewError("wrong mHandle_Magic");
00174 }
00175 
00176 void morkHandle::NewDownHandleError(morkEnv* ev) const
00177 {
00178   if ( this->IsHandle() )
00179   {
00180     if ( this->GoodHandleTag() )
00181     {
00182       if ( this->IsOpenNode() )
00183         ev->NewError("unknown down morkHandle error");
00184       else
00185         this->NonOpenNodeError(ev);
00186     }
00187     else
00188       ev->NewError("wrong morkHandle tag");
00189   }
00190   else
00191     ev->NewError("non morkHandle");
00192 }
00193 
00194 morkObject* morkHandle::GetGoodHandleObject(morkEnv* ev,
00195   mork_bool inMutable, mork_magic inMagicType, mork_bool inClosedOkay) const
00196 {
00197   morkObject* outObject = 0;
00198   if ( this->IsHandle() && this->GoodHandleTag() &&
00199     ( inClosedOkay || this->IsOpenNode() ) )
00200   {
00201     if ( !inMagicType || mHandle_Magic == inMagicType )
00202     {
00203       morkObject* obj = this->mHandle_Object;
00204       if ( obj )
00205       {
00206         if ( obj->IsNode() )
00207         {
00208           if ( inClosedOkay || obj->IsOpenNode() )
00209           {
00210             if ( this->IsMutable() || !inMutable )
00211               outObject = obj;
00212             else
00213               this->NonMutableNodeError(ev);
00214           }
00215           else
00216             this->NonOpenObjectError(ev);
00217         }
00218         else
00219           this->NonNodeObjectError(ev);
00220       }
00221       else if ( !inClosedOkay )
00222         this->NilHandleObjectError(ev);
00223     }
00224     else
00225       this->NewBadMagicHandleError(ev, inMagicType);
00226   }
00227   else
00228     this->NewDownHandleError(ev);
00229   
00230   MORK_ASSERT(outObject || inClosedOkay);
00231   return outObject;
00232 }
00233 
00234 
00235 morkEnv*
00236 morkHandle::CanUseHandle(nsIMdbEnv* mev, mork_bool inMutable,
00237   mork_bool inClosedOkay, mdb_err* outErr) const
00238 {
00239   morkEnv* outEnv = 0;
00240   morkEnv* ev = morkEnv::FromMdbEnv(mev);
00241   if ( ev )
00242   {
00243     morkObject* obj = this->GetGoodHandleObject(ev, inMutable,
00244       /*magic*/ 0, inClosedOkay);
00245     if ( obj )
00246     {
00247       outEnv = ev;
00248     }
00249     *outErr = ev->AsErr();
00250   }
00251   MORK_ASSERT(outEnv || inClosedOkay);
00252   return outEnv;
00253 }
00254 
00255 // { ===== begin nsIMdbObject methods =====
00256 
00257 // { ----- begin attribute methods -----
00258 /*virtual*/ mdb_err
00259 morkHandle::Handle_IsFrozenMdbObject(nsIMdbEnv* mev, mdb_bool* outIsReadonly)
00260 {
00261   mdb_err outErr = 0;
00262   mdb_bool readOnly = mdbBool_kTrue;
00263   
00264   morkEnv* ev = CanUseHandle(mev, /*inMutable*/ morkBool_kFalse,
00265     /*inClosedOkay*/ morkBool_kTrue, &outErr);
00266   if ( ev )
00267   {
00268     readOnly = mHandle_Object->IsFrozen();
00269     
00270     outErr = ev->AsErr();
00271   }
00272   MORK_ASSERT(outIsReadonly);
00273   if ( outIsReadonly )
00274     *outIsReadonly = readOnly;
00275 
00276   return outErr;
00277 }
00278 // same as nsIMdbPort::GetIsPortReadonly() when this object is inside a port.
00279 // } ----- end attribute methods -----
00280 
00281 // { ----- begin factory methods -----
00282 /*virtual*/ mdb_err
00283 morkHandle::Handle_GetMdbFactory(nsIMdbEnv* mev, nsIMdbFactory** acqFactory)
00284 {
00285   mdb_err outErr = 0;
00286   nsIMdbFactory* handle = 0;
00287   
00288   morkEnv* ev = CanUseHandle(mev, /*inMutable*/ morkBool_kFalse,
00289     /*inClosedOkay*/ morkBool_kTrue, &outErr);
00290   if ( ev )
00291   {
00292     morkFactory* factory = ev->mEnv_Factory;
00293     if ( factory )
00294     {
00295       handle = factory;
00296       NS_ADDREF(handle);
00297     }
00298     else
00299       this->NilFactoryError(ev);
00300       
00301     outErr = ev->AsErr();
00302   }
00303 
00304   MORK_ASSERT(acqFactory);
00305   if ( acqFactory )
00306     *acqFactory = handle;
00307 
00308   return outErr;
00309 } 
00310 // } ----- end factory methods -----
00311 
00312 // { ----- begin ref counting for well-behaved cyclic graphs -----
00313 /*virtual*/ mdb_err
00314 morkHandle::Handle_GetWeakRefCount(nsIMdbEnv* mev, // weak refs
00315   mdb_count* outCount)
00316 {
00317   mdb_err outErr = 0;
00318   mdb_count count = 0;
00319   
00320   morkEnv* ev = CanUseHandle(mev, /*inMutable*/ morkBool_kFalse,
00321     /*inClosedOkay*/ morkBool_kTrue, &outErr);
00322   if ( ev )
00323   {
00324     count = this->WeakRefsOnly();
00325     
00326     outErr = ev->AsErr();
00327   }
00328   MORK_ASSERT(outCount);
00329   if ( outCount )
00330     *outCount = count;
00331     
00332   return outErr;
00333 }  
00334 /*virtual*/ mdb_err
00335 morkHandle::Handle_GetStrongRefCount(nsIMdbEnv* mev, // strong refs
00336   mdb_count* outCount)
00337 {
00338   mdb_err outErr = 0;
00339   mdb_count count = 0;
00340   
00341   morkEnv* ev = CanUseHandle(mev, /*inMutable*/ morkBool_kFalse,
00342     /*inClosedOkay*/ morkBool_kTrue, &outErr);
00343   if ( ev )
00344   {
00345     count = this->StrongRefsOnly();
00346     
00347     outErr = ev->AsErr();
00348   }
00349   MORK_ASSERT(outCount);
00350   if ( outCount )
00351     *outCount = count;
00352     
00353   return outErr;
00354 }
00355 
00356 /*virtual*/ mdb_err
00357 morkHandle::Handle_AddWeakRef(nsIMdbEnv* mev)
00358 {
00359   mdb_err outErr = 0;
00360   
00361   morkEnv* ev = CanUseHandle(mev, /*inMutable*/ morkBool_kFalse,
00362     /*inClosedOkay*/ morkBool_kTrue, &outErr);
00363   if ( ev )
00364   {
00365     this->AddWeakRef(ev);
00366     outErr = ev->AsErr();
00367   }
00368     
00369   return outErr;
00370 }
00371 /*virtual*/ mdb_err
00372 morkHandle::Handle_AddStrongRef(nsIMdbEnv* mev)
00373 {
00374   mdb_err outErr = 0;
00375   
00376   morkEnv* ev = CanUseHandle(mev, /*inMutable*/ morkBool_kFalse,
00377     /*inClosedOkay*/ morkBool_kFalse, &outErr);
00378   if ( ev )
00379   {
00380     this->AddStrongRef(ev);
00381     outErr = ev->AsErr();
00382   }
00383     
00384   return outErr;
00385 }
00386 
00387 /*virtual*/ mdb_err
00388 morkHandle::Handle_CutWeakRef(nsIMdbEnv* mev)
00389 {
00390   mdb_err outErr = 0;
00391   
00392   morkEnv* ev = CanUseHandle(mev, /*inMutable*/ morkBool_kFalse,
00393     /*inClosedOkay*/ morkBool_kTrue, &outErr);
00394   if ( ev )
00395   {
00396     this->CutWeakRef(ev);
00397     outErr = ev->AsErr();
00398   }
00399     
00400   return outErr;
00401 }
00402 /*virtual*/ mdb_err
00403 morkHandle::Handle_CutStrongRef(nsIMdbEnv* mev)
00404 {
00405   mdb_err outErr = 0;
00406   morkEnv* ev = CanUseHandle(mev, /*inMutable*/ morkBool_kFalse,
00407     /*inClosedOkay*/ morkBool_kTrue, &outErr);
00408   if ( ev )
00409   {
00410     this->CutStrongRef(ev);
00411     outErr = ev->AsErr();
00412   }
00413   return outErr;
00414 }
00415 
00416 /*virtual*/ mdb_err
00417 morkHandle::Handle_CloseMdbObject(nsIMdbEnv* mev)
00418 // called at strong refs zero
00419 {
00420   // if only one ref, Handle_CutStrongRef will clean up better.
00421   if (mNode_Uses == 1)
00422     return Handle_CutStrongRef(mev);
00423 
00424   mdb_err outErr = 0;
00425   
00426   if ( this->IsNode() && this->IsOpenNode() )
00427   {
00428     morkEnv* ev = CanUseHandle(mev, /*inMutable*/ morkBool_kFalse,
00429     /*inClosedOkay*/ morkBool_kTrue, &outErr);
00430     if ( ev )
00431     {
00432       morkObject* object = mHandle_Object;
00433       if ( object && object->IsNode() && object->IsOpenNode() )
00434         object->CloseMorkNode(ev);
00435         
00436       this->CloseMorkNode(ev);
00437       outErr = ev->AsErr();
00438     }
00439   }
00440   return outErr;
00441 }
00442 
00443 /*virtual*/ mdb_err
00444 morkHandle::Handle_IsOpenMdbObject(nsIMdbEnv* mev, mdb_bool* outOpen)
00445 {
00446   MORK_USED_1(mev);
00447   mdb_err outErr = 0;
00448   
00449   MORK_ASSERT(outOpen);
00450   if ( outOpen )
00451     *outOpen = this->IsOpenNode();
00452       
00453   return outErr;
00454 }
00455 // } ----- end ref counting -----
00456 
00457 // } ===== end nsIMdbObject methods =====
00458 
00459 
00460 //3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789