• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
6  * are met:
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.
12  *
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.
24  */
25 
26 #ifndef V8PerIsolateData_h
27 #define V8PerIsolateData_h
28 
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"
33 #include <v8.h>
34 #include "wtf/Forward.h"
35 #include "wtf/HashMap.h"
36 #include "wtf/OwnPtr.h"
37 #include "wtf/Vector.h"
38 
39 namespace WebCore {
40 
41 class DOMDataStore;
42 class GCEventData;
43 class StringCache;
44 class V8HiddenPropertyName;
45 struct WrapperTypeInfo;
46 
47 class ExternalStringVisitor;
48 
49 typedef WTF::Vector<DOMDataStore*> DOMDataList;
50 
51 class V8PerIsolateData {
52 public:
53     static V8PerIsolateData* create(v8::Isolate*);
54     static void ensureInitialized(v8::Isolate*);
current()55     static V8PerIsolateData* current()
56     {
57         return from(v8::Isolate::GetCurrent());
58     }
from(v8::Isolate * isolate)59     static V8PerIsolateData* from(v8::Isolate* isolate)
60     {
61         ASSERT(isolate);
62         ASSERT(isolate->GetData(gin::kEmbedderBlink));
63         return static_cast<V8PerIsolateData*>(isolate->GetData(gin::kEmbedderBlink));
64     }
65     static void dispose(v8::Isolate*);
66 
67     typedef HashMap<const void*, UnsafePersistent<v8::FunctionTemplate> > TemplateMap;
68 
rawDOMTemplateMap(WrapperWorldType worldType)69     TemplateMap& rawDOMTemplateMap(WrapperWorldType worldType)
70     {
71         if (worldType == MainWorld)
72             return m_rawDOMTemplatesForMainWorld;
73         return m_rawDOMTemplatesForNonMainWorld;
74     }
75 
templateMap(WrapperWorldType worldType)76     TemplateMap& templateMap(WrapperWorldType worldType)
77     {
78         if (worldType == MainWorld)
79             return m_templatesForMainWorld;
80         return m_templatesForNonMainWorld;
81     }
82 
83     v8::Handle<v8::FunctionTemplate> toStringTemplate();
lazyEventListenerToStringTemplate()84     v8::Handle<v8::FunctionTemplate> lazyEventListenerToStringTemplate()
85     {
86         return v8::Local<v8::FunctionTemplate>::New(m_isolate, m_lazyEventListenerToStringTemplate);
87     }
88 
stringCache()89     StringCache* stringCache() { return m_stringCache.get(); }
90 
91     v8::Persistent<v8::Value>& ensureLiveRoot();
92 
allStores()93     DOMDataList& allStores() { return m_domDataList; }
94 
hiddenPropertyName()95     V8HiddenPropertyName* hiddenPropertyName() { return m_hiddenPropertyName.get(); }
96 
registerDOMDataStore(DOMDataStore * domDataStore)97     void registerDOMDataStore(DOMDataStore* domDataStore)
98     {
99         ASSERT(m_domDataList.find(domDataStore) == kNotFound);
100         m_domDataList.append(domDataStore);
101     }
102 
unregisterDOMDataStore(DOMDataStore * domDataStore)103     void unregisterDOMDataStore(DOMDataStore* domDataStore)
104     {
105         ASSERT(m_domDataList.find(domDataStore) != kNotFound);
106         m_domDataList.remove(m_domDataList.find(domDataStore));
107     }
108 
109     // DOMDataStore is owned outside V8PerIsolateData.
workerDOMDataStore()110     DOMDataStore* workerDOMDataStore() { return m_workerDomDataStore; }
setWorkerDOMDataStore(DOMDataStore * store)111     void setWorkerDOMDataStore(DOMDataStore* store) { m_workerDomDataStore = store; }
112 
recursionLevel()113     int recursionLevel() const { return m_recursionLevel; }
incrementRecursionLevel()114     int incrementRecursionLevel() { return ++m_recursionLevel; }
decrementRecursionLevel()115     int decrementRecursionLevel() { return --m_recursionLevel; }
116 
117 #ifndef NDEBUG
internalScriptRecursionLevel()118     int internalScriptRecursionLevel() const { return m_internalScriptRecursionLevel; }
incrementInternalScriptRecursionLevel()119     int incrementInternalScriptRecursionLevel() { return ++m_internalScriptRecursionLevel; }
decrementInternalScriptRecursionLevel()120     int decrementInternalScriptRecursionLevel() { return --m_internalScriptRecursionLevel; }
121 #endif
122 
gcEventData()123     GCEventData* gcEventData() { return m_gcEventData.get(); }
124 
125     // Gives the system a hint that we should request garbage collection
126     // upon the next close or navigation event, because some expensive
127     // objects have been allocated that we want to take every opportunity
128     // to collect.
setShouldCollectGarbageSoon()129     void setShouldCollectGarbageSoon() { m_shouldCollectGarbageSoon = true; }
clearShouldCollectGarbageSoon()130     void clearShouldCollectGarbageSoon() { m_shouldCollectGarbageSoon = false; }
shouldCollectGarbageSoon()131     bool shouldCollectGarbageSoon() const { return m_shouldCollectGarbageSoon; }
132 
133     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);
134     v8::Handle<v8::FunctionTemplate> privateTemplateIfExists(WrapperWorldType, void* privatePointer);
135     void setPrivateTemplate(WrapperWorldType, void* privatePointer, v8::Handle<v8::FunctionTemplate>);
136 
137     v8::Handle<v8::FunctionTemplate> rawDOMTemplate(const WrapperTypeInfo*, WrapperWorldType);
138 
139     bool hasInstance(const WrapperTypeInfo*, v8::Handle<v8::Value>, WrapperWorldType);
140 
141     v8::Local<v8::Context> ensureRegexContext();
142 
previousSamplingState()143     const char* previousSamplingState() const { return m_previousSamplingState; }
setPreviousSamplingState(const char * name)144     void setPreviousSamplingState(const char* name) { m_previousSamplingState = name; }
145 
146 private:
147     explicit V8PerIsolateData(v8::Isolate*);
148     ~V8PerIsolateData();
149     static void constructorOfToString(const v8::FunctionCallbackInfo<v8::Value>&);
150 
151     v8::Isolate* m_isolate;
152     TemplateMap m_rawDOMTemplatesForMainWorld;
153     TemplateMap m_rawDOMTemplatesForNonMainWorld;
154     TemplateMap m_templatesForMainWorld;
155     TemplateMap m_templatesForNonMainWorld;
156     ScopedPersistent<v8::FunctionTemplate> m_toStringTemplate;
157     v8::Persistent<v8::FunctionTemplate> m_lazyEventListenerToStringTemplate;
158     OwnPtr<StringCache> m_stringCache;
159 
160     Vector<DOMDataStore*> m_domDataList;
161     DOMDataStore* m_workerDomDataStore;
162 
163     OwnPtr<V8HiddenPropertyName> m_hiddenPropertyName;
164     ScopedPersistent<v8::Value> m_liveRoot;
165     ScopedPersistent<v8::Context> m_regexContext;
166 
167     const char* m_previousSamplingState;
168 
169     bool m_constructorMode;
170     friend class ConstructorMode;
171 
172     int m_recursionLevel;
173 
174 #ifndef NDEBUG
175     int m_internalScriptRecursionLevel;
176 #endif
177     OwnPtr<GCEventData> m_gcEventData;
178     bool m_shouldCollectGarbageSoon;
179 };
180 
181 } // namespace WebCore
182 
183 #endif // V8PerIsolateData_h
184