LCOV - code coverage report
Current view: directory - objdir/dist/include - nsWrapperCache.h (source / functions) Found Hit Coverage
Test: app.info Lines: 28 25 89.3 %
Date: 2012-06-02 Functions: 9 8 88.9 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2                 : /* ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       9                 :  *
      10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :  * for the specific language governing rights and limitations under the
      13                 :  * License.
      14                 :  *
      15                 :  * The Original Code is Gecko DOM code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is
      18                 :  *   Mozilla Foundation.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 2008
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *    Peter Van der Beken <peterv@propagandism.org>
      24                 :  *
      25                 :  * Alternatively, the contents of this file may be used under the terms of
      26                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      27                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      28                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      29                 :  * of those above. If you wish to allow use of your version of this file only
      30                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      31                 :  * use your version of this file under the terms of the MPL, indicate your
      32                 :  * decision by deleting the provisions above and replace them with the notice
      33                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      34                 :  * the provisions above, a recipient may use your version of this file under
      35                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      36                 :  *
      37                 :  * ***** END LICENSE BLOCK ***** */
      38                 : 
      39                 : #ifndef nsWrapperCache_h___
      40                 : #define nsWrapperCache_h___
      41                 : 
      42                 : #include "nsCycleCollectionParticipant.h"
      43                 : 
      44                 : struct JSObject;
      45                 : struct JSContext;
      46                 : class nsContentUtils;
      47                 : class XPCWrappedNativeScope;
      48                 : 
      49                 : typedef PRUptrdiff PtrBits;
      50                 : 
      51                 : #define NS_WRAPPERCACHE_IID \
      52                 : { 0x6f3179a1, 0x36f7, 0x4a5c, \
      53                 :   { 0x8c, 0xf1, 0xad, 0xc8, 0x7c, 0xde, 0x3e, 0x87 } }
      54                 : 
      55                 : /**
      56                 :  * Class to store the wrapper for an object. This can only be used with objects
      57                 :  * that only have one non-security wrapper at a time (for an XPCWrappedNative
      58                 :  * this is usually ensured by setting an explicit parent in the PreCreate hook
      59                 :  * for the class).
      60                 :  *
      61                 :  * An instance of nsWrapperCache can be gotten from an object that implements
      62                 :  * a wrapper cache by calling QueryInterface on it. Note that this breaks XPCOM
      63                 :  * rules a bit (this object doesn't derive from nsISupports).
      64                 :  *
      65                 :  * The cache can store objects other than wrappers. We allow wrappers to use a
      66                 :  * separate JSObject to store their state (mostly expandos). If the wrapper is
      67                 :  * collected and we want to preserve this state we actually store the state
      68                 :  * object in the cache.
      69                 :  *
      70                 :  * The cache can store 3 types of objects:
      71                 :  *
      72                 :  *  If WRAPPER_IS_PROXY is not set (IsProxy() returns false):
      73                 :  *    - a slim wrapper or the JSObject of an XPCWrappedNative wrapper
      74                 :  *
      75                 :  *  If WRAPPER_IS_PROXY is set (IsProxy() returns true):
      76                 :  *    - a proxy wrapper
      77                 :  *    - an expando object
      78                 :  *
      79                 :  * If a proxy wrapper is GCed and it has an expando object we'll store the
      80                 :  * expando object in the cache. If we create a new proxy wrapper and the cache
      81                 :  * contains an expando object we'll store the expando object in the new wrapper
      82                 :  * and store the new wrapper in the cache. Unlinking from the cycle collector
      83                 :  * clears anything stored in the cache.
      84                 :  *
      85                 :  * A number of the methods are implemented in nsWrapperCacheInlines.h because we
      86                 :  * have to include some JS headers that don't play nicely with the rest of the
      87                 :  * codebase. Include nsWrapperCacheInlines.h if you need to call those methods.
      88                 :  */
      89                 : class nsWrapperCache
      90                 : {
      91                 :   friend class nsContentUtils;
      92                 : 
      93                 : public:
      94                 :   NS_DECLARE_STATIC_IID_ACCESSOR(NS_WRAPPERCACHE_IID)
      95                 : 
      96          123903 :   nsWrapperCache() : mWrapperPtrBits(0)
      97                 :   {
      98          123903 :   }
      99          123897 :   ~nsWrapperCache()
     100          123897 :   {
     101          123897 :     NS_ASSERTION(!PreservingWrapper(),
     102                 :                  "Destroying cache with a preserved wrapper!");
     103          123897 :     RemoveExpandoObject();
     104          123897 :   }
     105                 : 
     106                 :   /**
     107                 :    * Get the cached wrapper.
     108                 :    *
     109                 :    * This getter clears the gray bit before handing out the JSObject which means
     110                 :    * that the object is guaranteed to be kept alive past the next CC.
     111                 :    */
     112                 :   JSObject* GetWrapper() const;
     113                 : 
     114                 :   /**
     115                 :    * Get the cached wrapper.
     116                 :    *
     117                 :    * This getter does not change the color of the JSObject meaning that the
     118                 :    * object returned is not guaranteed to be kept alive past the next CC.
     119                 :    *
     120                 :    * This should only be called if you are certain that the return value won't
     121                 :    * be passed into a JS API function and that it won't be stored without being
     122                 :    * rooted (or otherwise signaling the stored value to the CC).
     123                 :    */
     124                 :   JSObject* GetWrapperPreserveColor() const;
     125                 : 
     126                 :   /**
     127                 :    * Get the expando object, used for storing expando properties, if there is
     128                 :    * one available. If the cache holds a DOM proxy binding that proxy's expando
     129                 :    * object will be returned.
     130                 :    *
     131                 :    * This getter does not change the color of the JSObject meaning that the
     132                 :    * object returned is not guaranteed to be kept alive past the next CC.
     133                 :    *
     134                 :    * This should only be called if you are certain that the return value won't
     135                 :    * be passed into a JS API function and that it won't be stored without being
     136                 :    * rooted (or otherwise signaling the stored value to the CC).
     137                 :    */
     138                 :   JSObject* GetExpandoObjectPreserveColor() const;
     139                 : 
     140                 :   void SetWrapper(JSObject* aWrapper);
     141                 : 
     142                 :   /**
     143                 :    * Clear the wrapper, but keep the expando object alive if the wrapper has
     144                 :    * one. This should be called from the finalizer for the wrapper.
     145                 :    */
     146                 :   void ClearWrapper();
     147                 : 
     148                 :   /**
     149                 :    * Clear the wrapper if it's a proxy, doesn't keep the expando object alive.
     150                 :    * This should be called when unlinking the cache.
     151                 :    */
     152                 :   void ClearWrapperIfProxy();
     153                 : 
     154          655709 :   bool PreservingWrapper()
     155                 :   {
     156          655709 :     return (mWrapperPtrBits & WRAPPER_BIT_PRESERVED) != 0;
     157                 :   }
     158                 : 
     159            4939 :   void SetIsProxy()
     160                 :   {
     161            4939 :     NS_ASSERTION(!mWrapperPtrBits,
     162                 :                  "This flag should be set before creating any wrappers.");
     163            4939 :     mWrapperPtrBits = WRAPPER_IS_PROXY;
     164            4939 :   }
     165                 :   void ClearIsProxy()
     166                 :   {
     167                 :     NS_ASSERTION(!mWrapperPtrBits || mWrapperPtrBits == WRAPPER_IS_PROXY,
     168                 :                  "This flag should be cleared before creating any wrappers.");
     169                 :     mWrapperPtrBits = 0;
     170                 :   }
     171                 : 
     172         1137949 :   bool IsProxy() const
     173                 :   {
     174         1137949 :     return (mWrapperPtrBits & WRAPPER_IS_PROXY) != 0;
     175                 :   }
     176                 : 
     177                 : 
     178                 :   /**
     179                 :    * Wrap the object corresponding to this wrapper cache. If non-null is
     180                 :    * returned, the object has already been stored in the wrapper cache and the
     181                 :    * value set in triedToWrap is meaningless. If null is returned then
     182                 :    * triedToWrap indicates whether an error occurred, if it's false then the
     183                 :    * object doesn't actually support creating a wrapper through its WrapObject
     184                 :    * hook.
     185                 :    */
     186               0 :   virtual JSObject* WrapObject(JSContext *cx, XPCWrappedNativeScope *scope,
     187                 :                                bool *triedToWrap)
     188                 :   {
     189               0 :     *triedToWrap = false;
     190               0 :     return nsnull;
     191                 :   }
     192                 : 
     193                 :   /**
     194                 :    * Returns true if the object has a non-gray wrapper.
     195                 :    */
     196                 :   bool IsBlack();
     197                 : 
     198                 : private:
     199                 :   // Only meant to be called by nsContentUtils.
     200               8 :   void SetPreservingWrapper(bool aPreserve)
     201                 :   {
     202               8 :     if(aPreserve) {
     203               5 :       mWrapperPtrBits |= WRAPPER_BIT_PRESERVED;
     204                 :     }
     205                 :     else {
     206               3 :       mWrapperPtrBits &= ~WRAPPER_BIT_PRESERVED;
     207                 :     }
     208               8 :   }
     209         1210854 :   JSObject *GetJSObjectFromBits() const
     210                 :   {
     211         1210854 :     return reinterpret_cast<JSObject*>(mWrapperPtrBits & ~kWrapperBitMask);
     212                 :   }
     213           49762 :   void SetWrapperBits(void *aWrapper)
     214                 :   {
     215                 :     mWrapperPtrBits = reinterpret_cast<PtrBits>(aWrapper) |
     216           49762 :                       (mWrapperPtrBits & WRAPPER_IS_PROXY);
     217           49762 :   }
     218                 :   void RemoveExpandoObject();
     219                 : 
     220                 :   static JSObject *GetExpandoFromSlot(JSObject *obj);
     221                 : 
     222                 :   /**
     223                 :    * If this bit is set then we're preserving the wrapper, which in effect ties
     224                 :    * the lifetime of the JS object stored in the cache to the lifetime of the
     225                 :    * native object. We rely on the cycle collector to break the cycle that this
     226                 :    * causes between the native object and the JS object, so it is important that
     227                 :    * any native object that supports preserving of its wrapper
     228                 :    * traces/traverses/unlinks the cached JS object (see
     229                 :    * NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER,
     230                 :    * NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS and
     231                 :    * NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER).
     232                 :    */
     233                 :   enum { WRAPPER_BIT_PRESERVED = 1 << 0 };
     234                 : 
     235                 :   /**
     236                 :    * If this bit is set then the wrapper for the native object is a proxy. Note
     237                 :    * that that doesn't necessarily mean that the JS object stored in the cache
     238                 :    * is a JS proxy, as we sometimes store objects other than the wrapper in the
     239                 :    * cache.
     240                 :    */
     241                 :   enum { WRAPPER_IS_PROXY = 1 << 1 };
     242                 : 
     243                 :   enum { kWrapperBitMask = (WRAPPER_BIT_PRESERVED | WRAPPER_IS_PROXY) };
     244                 : 
     245                 :   PtrBits mWrapperPtrBits;
     246                 : };
     247                 : 
     248                 : NS_DEFINE_STATIC_IID_ACCESSOR(nsWrapperCache, NS_WRAPPERCACHE_IID)
     249                 : 
     250                 : #define NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY                                   \
     251                 :   if ( aIID.Equals(NS_GET_IID(nsWrapperCache)) ) {                            \
     252                 :     *aInstancePtr = static_cast<nsWrapperCache*>(this);                       \
     253                 :     return NS_OK;                                                             \
     254                 :   }
     255                 : 
     256                 : #endif /* nsWrapperCache_h___ */

Generated by: LCOV version 1.7