2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef V8PerIsolateData_h
27 #define V8PerIsolateData_h
29 #include "bindings/v8/ScopedPersistent.h"
30 #include "bindings/v8/UnsafePersistent.h"
31 #include "bindings/v8/WrapperTypeInfo.h"
32 #include "gin/public/gin_embedders.h"
34 #include "wtf/Forward.h"
35 #include "wtf/HashMap.h"
36 #include "wtf/OwnPtr.h"
37 #include "wtf/Vector.h"
44 struct WrapperTypeInfo;
46 class ExternalStringVisitor;
48 typedef WTF::Vector<DOMDataStore*> DOMDataList;
50 class V8PerIsolateData {
52 static V8PerIsolateData* create(v8::Isolate*);
53 static void ensureInitialized(v8::Isolate*);
54 static V8PerIsolateData* current()
56 return from(v8::Isolate::GetCurrent());
58 static V8PerIsolateData* from(v8::Isolate* isolate)
61 ASSERT(isolate->GetData(gin::kEmbedderBlink));
62 return static_cast<V8PerIsolateData*>(isolate->GetData(gin::kEmbedderBlink));
64 static void dispose(v8::Isolate*);
66 typedef HashMap<const void*, UnsafePersistent<v8::FunctionTemplate> > TemplateMap;
68 TemplateMap& templateMap(WrapperWorldType worldType)
70 if (worldType == MainWorld)
71 return m_templatesForMainWorld;
72 return m_templatesForNonMainWorld;
75 v8::Handle<v8::FunctionTemplate> toStringTemplate();
76 v8::Handle<v8::FunctionTemplate> lazyEventListenerToStringTemplate()
78 return v8::Local<v8::FunctionTemplate>::New(m_isolate, m_lazyEventListenerToStringTemplate);
81 StringCache* stringCache() { return m_stringCache.get(); }
83 v8::Persistent<v8::Value>& ensureLiveRoot();
85 DOMDataList& allStores() { return m_domDataList; }
87 void registerDOMDataStore(DOMDataStore* domDataStore)
89 ASSERT(m_domDataList.find(domDataStore) == kNotFound);
90 m_domDataList.append(domDataStore);
93 void unregisterDOMDataStore(DOMDataStore* domDataStore)
95 ASSERT(m_domDataList.find(domDataStore) != kNotFound);
96 m_domDataList.remove(m_domDataList.find(domDataStore));
99 // DOMDataStore is owned outside V8PerIsolateData.
100 DOMDataStore* workerDOMDataStore() { return m_workerDomDataStore; }
101 void setWorkerDOMDataStore(DOMDataStore* store) { m_workerDomDataStore = store; }
103 int recursionLevel() const { return m_recursionLevel; }
104 int incrementRecursionLevel() { return ++m_recursionLevel; }
105 int decrementRecursionLevel() { return --m_recursionLevel; }
108 int internalScriptRecursionLevel() const { return m_internalScriptRecursionLevel; }
109 int incrementInternalScriptRecursionLevel() { return ++m_internalScriptRecursionLevel; }
110 int decrementInternalScriptRecursionLevel() { return --m_internalScriptRecursionLevel; }
113 GCEventData* gcEventData() { return m_gcEventData.get(); }
115 // Gives the system a hint that we should request garbage collection
116 // upon the next close or navigation event, because some expensive
117 // objects have been allocated that we want to take every opportunity
119 void setShouldCollectGarbageSoon() { m_shouldCollectGarbageSoon = true; }
120 void clearShouldCollectGarbageSoon() { m_shouldCollectGarbageSoon = false; }
121 bool shouldCollectGarbageSoon() const { return m_shouldCollectGarbageSoon; }
123 v8::Handle<v8::FunctionTemplate> privateTemplate(WrapperWorldType, void* privatePointer, v8::FunctionCallback = 0, v8::Handle<v8::Value> data = v8::Handle<v8::Value>(), v8::Handle<v8::Signature> = v8::Handle<v8::Signature>(), int length = 0);
124 v8::Handle<v8::FunctionTemplate> privateTemplateIfExists(WrapperWorldType, void* privatePointer);
125 void setPrivateTemplate(WrapperWorldType, void* privatePointer, v8::Handle<v8::FunctionTemplate>);
127 bool hasInstanceInMainWorld(const WrapperTypeInfo*, v8::Handle<v8::Value>);
128 bool hasInstanceInNonMainWorld(const WrapperTypeInfo*, v8::Handle<v8::Value>);
130 v8::Local<v8::Context> ensureRegexContext();
132 const char* previousSamplingState() const { return m_previousSamplingState; }
133 void setPreviousSamplingState(const char* name) { m_previousSamplingState = name; }
136 explicit V8PerIsolateData(v8::Isolate*);
138 bool hasInstance(const WrapperTypeInfo*, v8::Handle<v8::Value>, TemplateMap&);
139 static void constructorOfToString(const v8::FunctionCallbackInfo<v8::Value>&);
141 v8::Isolate* m_isolate;
142 TemplateMap m_templatesForMainWorld;
143 TemplateMap m_templatesForNonMainWorld;
144 ScopedPersistent<v8::FunctionTemplate> m_toStringTemplate;
145 v8::Persistent<v8::FunctionTemplate> m_lazyEventListenerToStringTemplate;
146 OwnPtr<StringCache> m_stringCache;
148 Vector<DOMDataStore*> m_domDataList;
149 DOMDataStore* m_workerDomDataStore;
151 ScopedPersistent<v8::Value> m_liveRoot;
152 ScopedPersistent<v8::Context> m_regexContext;
154 const char* m_previousSamplingState;
156 bool m_constructorMode;
157 friend class ConstructorMode;
159 int m_recursionLevel;
162 int m_internalScriptRecursionLevel;
164 OwnPtr<GCEventData> m_gcEventData;
165 bool m_shouldCollectGarbageSoon;
168 } // namespace WebCore
170 #endif // V8PerIsolateData_h