Home | History | Annotate | Download | only in v8
      1 /*
      2  * Copyright (C) 2012 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 V8PerContextData_h
     32 #define V8PerContextData_h
     33 
     34 #include "bindings/core/v8/CustomElementBinding.h"
     35 #include "bindings/core/v8/ScopedPersistent.h"
     36 #include "bindings/core/v8/V8PersistentValueMap.h"
     37 #include "bindings/core/v8/WrapperTypeInfo.h"
     38 #include "gin/public/context_holder.h"
     39 #include "gin/public/gin_embedders.h"
     40 #include "wtf/HashMap.h"
     41 #include "wtf/PassOwnPtr.h"
     42 #include "wtf/Vector.h"
     43 #include "wtf/text/AtomicString.h"
     44 #include "wtf/text/AtomicStringHash.h"
     45 #include <v8.h>
     46 
     47 namespace blink {
     48 
     49 class CustomElementDefinition;
     50 class V8DOMActivityLogger;
     51 class V8PerContextData;
     52 struct V8NPObject;
     53 typedef WTF::Vector<V8NPObject*> V8NPObjectVector;
     54 typedef WTF::HashMap<int, V8NPObjectVector> V8NPObjectMap;
     55 
     56 enum V8ContextEmbedderDataField {
     57     v8ContextDebugIdIndex = static_cast<int>(gin::kDebugIdIndex),
     58     v8ContextPerContextDataIndex = static_cast<int>(gin::kPerContextDataStartIndex + gin::kEmbedderBlink),
     59 };
     60 
     61 class V8PerContextData {
     62 public:
     63     static PassOwnPtr<V8PerContextData> create(v8::Handle<v8::Context>);
     64 
     65     static V8PerContextData* from(v8::Handle<v8::Context>);
     66 
     67     ~V8PerContextData();
     68 
     69     v8::Handle<v8::Context> context() { return m_context.newLocal(m_isolate); }
     70 
     71     // To create JS Wrapper objects, we create a cache of a 'boiler plate'
     72     // object, and then simply Clone that object each time we need a new one.
     73     // This is faster than going through the full object creation process.
     74     v8::Local<v8::Object> createWrapperFromCache(const WrapperTypeInfo* type)
     75     {
     76         v8::Local<v8::Object> boilerplate = m_wrapperBoilerplates.Get(type);
     77         return !boilerplate.IsEmpty() ? boilerplate->Clone() : createWrapperFromCacheSlowCase(type);
     78     }
     79 
     80     v8::Local<v8::Function> constructorForType(const WrapperTypeInfo* type)
     81     {
     82         v8::Local<v8::Function> function = m_constructorMap.Get(type);
     83         return (!function.IsEmpty()) ? function : constructorForTypeSlowCase(type);
     84     }
     85 
     86     v8::Local<v8::Object> prototypeForType(const WrapperTypeInfo*);
     87 
     88     V8NPObjectMap* v8NPObjectMap() { return &m_v8NPObjectMap; }
     89 
     90     void addCustomElementBinding(CustomElementDefinition*, PassOwnPtr<CustomElementBinding>);
     91     void clearCustomElementBinding(CustomElementDefinition*);
     92     CustomElementBinding* customElementBinding(CustomElementDefinition*);
     93 
     94     V8DOMActivityLogger* activityLogger() const { return m_activityLogger; }
     95     void setActivityLogger(V8DOMActivityLogger* activityLogger) { m_activityLogger = activityLogger; }
     96 
     97     v8::Handle<v8::Value> compiledPrivateScript(String);
     98     void setCompiledPrivateScript(String, v8::Handle<v8::Value>);
     99 
    100 private:
    101     V8PerContextData(v8::Handle<v8::Context>);
    102 
    103     v8::Local<v8::Object> createWrapperFromCacheSlowCase(const WrapperTypeInfo*);
    104     v8::Local<v8::Function> constructorForTypeSlowCase(const WrapperTypeInfo*);
    105 
    106     v8::Isolate* m_isolate;
    107 
    108     // For each possible type of wrapper, we keep a boilerplate object.
    109     // The boilerplate is used to create additional wrappers of the same type.
    110     typedef V8PersistentValueMap<const WrapperTypeInfo*, v8::Object, false> WrapperBoilerplateMap;
    111     WrapperBoilerplateMap m_wrapperBoilerplates;
    112 
    113     typedef V8PersistentValueMap<const WrapperTypeInfo*, v8::Function, false> ConstructorMap;
    114     ConstructorMap m_constructorMap;
    115 
    116     V8NPObjectMap m_v8NPObjectMap;
    117 
    118     OwnPtr<gin::ContextHolder> m_contextHolder;
    119 
    120     ScopedPersistent<v8::Context> m_context;
    121     ScopedPersistent<v8::Value> m_errorPrototype;
    122 
    123     typedef WTF::HashMap<CustomElementDefinition*, OwnPtr<CustomElementBinding> > CustomElementBindingMap;
    124     OwnPtr<CustomElementBindingMap> m_customElementBindings;
    125 
    126     // This is owned by a static hash map in V8DOMActivityLogger.
    127     V8DOMActivityLogger* m_activityLogger;
    128 
    129     V8PersistentValueMap<String, v8::Value, false> m_compiledPrivateScript;
    130 };
    131 
    132 class V8PerContextDebugData {
    133 public:
    134     static bool setContextDebugData(v8::Handle<v8::Context>, const char* worldName, int debugId);
    135     static int contextDebugId(v8::Handle<v8::Context>);
    136 };
    137 
    138 } // namespace blink
    139 
    140 #endif // V8PerContextData_h
    141