28dcd09b7f85cc4a91dbac6fc0f4f12257a0bcd6
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / V8PerIsolateData.h
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 struct WrapperTypeInfo;
45
46 class ExternalStringVisitor;
47
48 typedef WTF::Vector<DOMDataStore*> DOMDataList;
49
50 class V8PerIsolateData {
51 public:
52     static V8PerIsolateData* create(v8::Isolate*);
53     static void ensureInitialized(v8::Isolate*);
54     static V8PerIsolateData* current()
55     {
56         return from(v8::Isolate::GetCurrent());
57     }
58     static V8PerIsolateData* from(v8::Isolate* isolate)
59     {
60         ASSERT(isolate);
61         ASSERT(isolate->GetData(gin::kEmbedderBlink));
62         return static_cast<V8PerIsolateData*>(isolate->GetData(gin::kEmbedderBlink));
63     }
64     static void dispose(v8::Isolate*);
65
66     typedef HashMap<const void*, UnsafePersistent<v8::FunctionTemplate> > TemplateMap;
67
68     TemplateMap& templateMap(WrapperWorldType worldType)
69     {
70         if (worldType == MainWorld)
71             return m_templatesForMainWorld;
72         return m_templatesForNonMainWorld;
73     }
74
75     v8::Handle<v8::FunctionTemplate> toStringTemplate();
76     v8::Handle<v8::FunctionTemplate> lazyEventListenerToStringTemplate()
77     {
78         return v8::Local<v8::FunctionTemplate>::New(m_isolate, m_lazyEventListenerToStringTemplate);
79     }
80
81     StringCache* stringCache() { return m_stringCache.get(); }
82
83     v8::Persistent<v8::Value>& ensureLiveRoot();
84
85     DOMDataList& allStores() { return m_domDataList; }
86
87     void registerDOMDataStore(DOMDataStore* domDataStore)
88     {
89         ASSERT(m_domDataList.find(domDataStore) == kNotFound);
90         m_domDataList.append(domDataStore);
91     }
92
93     void unregisterDOMDataStore(DOMDataStore* domDataStore)
94     {
95         ASSERT(m_domDataList.find(domDataStore) != kNotFound);
96         m_domDataList.remove(m_domDataList.find(domDataStore));
97     }
98
99     // DOMDataStore is owned outside V8PerIsolateData.
100     DOMDataStore* workerDOMDataStore() { return m_workerDomDataStore; }
101     void setWorkerDOMDataStore(DOMDataStore* store) { m_workerDomDataStore = store; }
102
103     int recursionLevel() const { return m_recursionLevel; }
104     int incrementRecursionLevel() { return ++m_recursionLevel; }
105     int decrementRecursionLevel() { return --m_recursionLevel; }
106
107 #ifndef NDEBUG
108     int internalScriptRecursionLevel() const { return m_internalScriptRecursionLevel; }
109     int incrementInternalScriptRecursionLevel() { return ++m_internalScriptRecursionLevel; }
110     int decrementInternalScriptRecursionLevel() { return --m_internalScriptRecursionLevel; }
111 #endif
112
113     GCEventData* gcEventData() { return m_gcEventData.get(); }
114
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
118     // to collect.
119     void setShouldCollectGarbageSoon() { m_shouldCollectGarbageSoon = true; }
120     void clearShouldCollectGarbageSoon() { m_shouldCollectGarbageSoon = false; }
121     bool shouldCollectGarbageSoon() const { return m_shouldCollectGarbageSoon; }
122
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>);
126
127     bool hasInstanceInMainWorld(const WrapperTypeInfo*, v8::Handle<v8::Value>);
128     bool hasInstanceInNonMainWorld(const WrapperTypeInfo*, v8::Handle<v8::Value>);
129
130     v8::Local<v8::Context> ensureRegexContext();
131
132     const char* previousSamplingState() const { return m_previousSamplingState; }
133     void setPreviousSamplingState(const char* name) { m_previousSamplingState = name; }
134
135 private:
136     explicit V8PerIsolateData(v8::Isolate*);
137     ~V8PerIsolateData();
138     bool hasInstance(const WrapperTypeInfo*, v8::Handle<v8::Value>, TemplateMap&);
139     static void constructorOfToString(const v8::FunctionCallbackInfo<v8::Value>&);
140
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;
147
148     Vector<DOMDataStore*> m_domDataList;
149     DOMDataStore* m_workerDomDataStore;
150
151     ScopedPersistent<v8::Value> m_liveRoot;
152     ScopedPersistent<v8::Context> m_regexContext;
153
154     const char* m_previousSamplingState;
155
156     bool m_constructorMode;
157     friend class ConstructorMode;
158
159     int m_recursionLevel;
160
161 #ifndef NDEBUG
162     int m_internalScriptRecursionLevel;
163 #endif
164     OwnPtr<GCEventData> m_gcEventData;
165     bool m_shouldCollectGarbageSoon;
166 };
167
168 } // namespace WebCore
169
170 #endif // V8PerIsolateData_h