Home | History | Annotate | Download | only in v8
      1 /*
      2  * Copyright (C) 2009 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #ifndef DOMData_h
     32 #define DOMData_h
     33 
     34 #include "DOMDataStore.h"
     35 
     36 namespace WebCore {
     37 
     38     // DOMData
     39     //
     40     // DOMData represents the all the DOM wrappers for a given thread.  In
     41     // particular, DOMData holds wrappers for all the isolated worlds in the
     42     // thread.  The DOMData for the main thread and the DOMData for child threads
     43     // use different subclasses.
     44     //
     45     class DOMData : public Noncopyable {
     46     public:
     47         DOMData();
     48         virtual ~DOMData();
     49 
     50         static DOMData* getCurrent();
     51         virtual DOMDataStore& getStore() = 0;
     52 
     53         template<typename T>
     54         static void handleWeakObject(DOMDataStore::DOMWrapperMapType, v8::Persistent<v8::Object>, T* domObject);
     55 
     56         void forgetDelayedObject(void* object) { m_delayedObjectMap.take(object); }
     57 
     58         // This is to ensure that we will deref DOM objects from the owning thread,
     59         // not the GC thread. The helper function will be scheduled by the GC
     60         // thread to get called from the owning thread.
     61         static void derefDelayedObjectsInCurrentThread(void*);
     62         void derefDelayedObjects();
     63 
     64         template<typename T>
     65         static void removeObjectsFromWrapperMap(AbstractWeakReferenceMap<T, v8::Object>& domMap);
     66 
     67         ThreadIdentifier owningThread() const { return m_owningThread; }
     68 
     69     private:
     70         typedef WTF::HashMap<void*, V8ClassIndex::V8WrapperType> DelayedObjectMap;
     71 
     72         void ensureDeref(V8ClassIndex::V8WrapperType type, void* domObject);
     73         static void derefObject(V8ClassIndex::V8WrapperType type, void* domObject);
     74 
     75         template<typename T>
     76         class WrapperMapObjectRemover : public WeakReferenceMap<T, v8::Object>::Visitor {
     77         public:
     78             virtual void visitDOMWrapper(T* domObject, v8::Persistent<v8::Object> v8Object)
     79             {
     80                 V8ClassIndex::V8WrapperType type = V8DOMWrapper::domWrapperType(v8Object);
     81                 derefObject(type, domObject);
     82                 v8Object.Dispose();
     83             }
     84         };
     85 
     86         // Stores all the DOM objects that are delayed to be processed when the
     87         // owning thread gains control.
     88         DelayedObjectMap m_delayedObjectMap;
     89 
     90         // The flag to indicate if the task to do the delayed process has
     91         // already been posted.
     92         bool m_delayedProcessingScheduled;
     93 
     94         bool m_isMainThread;
     95         ThreadIdentifier m_owningThread;
     96     };
     97 
     98     template<typename T>
     99     void DOMData::handleWeakObject(DOMDataStore::DOMWrapperMapType mapType, v8::Persistent<v8::Object> v8Object, T* domObject)
    100     {
    101         ASSERT(WTF::isMainThread());
    102         DOMDataList& list = DOMDataStore::allStores();
    103         for (size_t i = 0; i < list.size(); ++i) {
    104             DOMDataStore* store = list[i];
    105 
    106             DOMDataStore::InternalDOMWrapperMap<T>* domMap = static_cast<DOMDataStore::InternalDOMWrapperMap<T>*>(store->getDOMWrapperMap(mapType));
    107 
    108             if (domMap->removeIfPresent(domObject, v8Object)) {
    109                 ASSERT(store->domData()->owningThread() == WTF::currentThread());
    110                 store->domData()->derefObject(V8DOMWrapper::domWrapperType(v8Object), domObject);
    111             }
    112         }
    113     }
    114 
    115     template<typename T>
    116     void DOMData::removeObjectsFromWrapperMap(AbstractWeakReferenceMap<T, v8::Object>& domMap)
    117     {
    118         WrapperMapObjectRemover<T> remover;
    119         domMap.visit(&remover);
    120         domMap.clear();
    121     }
    122 
    123 } // namespace WebCore
    124 
    125 #endif // DOMData_h
    126