3d71809f1b8805c787e0fa5933429aa0610060b9
[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*> DOMDataStoreList;
49
50 class V8PerIsolateData {
51 public:
52     static void ensureInitialized(v8::Isolate*);
53     static V8PerIsolateData* current()
54     {
55         return from(v8::Isolate::GetCurrent());
56     }
57     static V8PerIsolateData* from(v8::Isolate* isolate)
58     {
59         ASSERT(isolate);
60         ASSERT(isolate->GetData(gin::kEmbedderBlink));
61         return static_cast<V8PerIsolateData*>(isolate->GetData(gin::kEmbedderBlink));
62     }
63     static void dispose(v8::Isolate*);
64     static v8::Isolate* mainThreadIsolate();
65
66     bool isMainThread() { return m_isMainThread; };
67
68     typedef HashMap<const void*, UnsafePersistent<v8::FunctionTemplate> > TemplateMap;
69
70     TemplateMap& templateMap(WrapperWorldType worldType)
71     {
72         if (worldType == MainWorld)
73             return m_templatesForMainWorld;
74         return m_templatesForNonMainWorld;
75     }
76
77     v8::Handle<v8::FunctionTemplate> toStringTemplate();
78
79     StringCache* stringCache() { return m_stringCache.get(); }
80
81     v8::Persistent<v8::Value>& ensureLiveRoot();
82
83     DOMDataStoreList& allStores() { return m_domDataStoreList; }
84
85     void registerDOMDataStore(DOMDataStore* domDataStore)
86     {
87         ASSERT(m_domDataStoreList.find(domDataStore) == kNotFound);
88         m_domDataStoreList.append(domDataStore);
89     }
90
91     void unregisterDOMDataStore(DOMDataStore* domDataStore)
92     {
93         ASSERT(m_domDataStoreList.find(domDataStore) != kNotFound);
94         m_domDataStoreList.remove(m_domDataStoreList.find(domDataStore));
95     }
96
97     int recursionLevel() const { return m_recursionLevel; }
98     int incrementRecursionLevel() { return ++m_recursionLevel; }
99     int decrementRecursionLevel() { return --m_recursionLevel; }
100
101     bool performingMicrotaskCheckpoint() const { return m_performingMicrotaskCheckpoint; }
102     void setPerformingMicrotaskCheckpoint(bool performingMicrotaskCheckpoint) { m_performingMicrotaskCheckpoint = performingMicrotaskCheckpoint; }
103
104 #ifndef NDEBUG
105     int internalScriptRecursionLevel() const { return m_internalScriptRecursionLevel; }
106     int incrementInternalScriptRecursionLevel() { return ++m_internalScriptRecursionLevel; }
107     int decrementInternalScriptRecursionLevel() { return --m_internalScriptRecursionLevel; }
108 #endif
109
110     GCEventData* gcEventData() { return m_gcEventData.get(); }
111
112     // Gives the system a hint that we should request garbage collection
113     // upon the next close or navigation event, because some expensive
114     // objects have been allocated that we want to take every opportunity
115     // to collect.
116     void setShouldCollectGarbageSoon() { m_shouldCollectGarbageSoon = true; }
117     void clearShouldCollectGarbageSoon() { m_shouldCollectGarbageSoon = false; }
118     bool shouldCollectGarbageSoon() const { return m_shouldCollectGarbageSoon; }
119
120     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);
121     v8::Handle<v8::FunctionTemplate> privateTemplateIfExists(WrapperWorldType, void* privatePointer);
122     void setPrivateTemplate(WrapperWorldType, void* privatePointer, v8::Handle<v8::FunctionTemplate>);
123
124     bool hasInstanceInMainWorld(const WrapperTypeInfo*, v8::Handle<v8::Value>);
125     bool hasInstanceInNonMainWorld(const WrapperTypeInfo*, v8::Handle<v8::Value>);
126
127     v8::Local<v8::Context> ensureRegexContext();
128
129     const char* previousSamplingState() const { return m_previousSamplingState; }
130     void setPreviousSamplingState(const char* name) { m_previousSamplingState = name; }
131
132 private:
133     explicit V8PerIsolateData(v8::Isolate*);
134     ~V8PerIsolateData();
135     bool hasInstance(const WrapperTypeInfo*, v8::Handle<v8::Value>, TemplateMap&);
136     static void constructorOfToString(const v8::FunctionCallbackInfo<v8::Value>&);
137
138     v8::Isolate* m_isolate;
139     bool m_isMainThread; // Caches the result of isMainThread() for performance.
140     TemplateMap m_templatesForMainWorld;
141     TemplateMap m_templatesForNonMainWorld;
142     ScopedPersistent<v8::FunctionTemplate> m_toStringTemplate;
143     OwnPtr<StringCache> m_stringCache;
144
145     Vector<DOMDataStore*> m_domDataStoreList;
146
147     ScopedPersistent<v8::Value> m_liveRoot;
148     ScopedPersistent<v8::Context> m_regexContext;
149
150     const char* m_previousSamplingState;
151
152     bool m_constructorMode;
153     friend class ConstructorMode;
154
155     int m_recursionLevel;
156
157 #ifndef NDEBUG
158     int m_internalScriptRecursionLevel;
159 #endif
160     OwnPtr<GCEventData> m_gcEventData;
161     bool m_shouldCollectGarbageSoon;
162     bool m_performingMicrotaskCheckpoint;
163 };
164
165 } // namespace WebCore
166
167 #endif // V8PerIsolateData_h