OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef ScriptState_h | 5 #ifndef ScriptState_h |
6 #define ScriptState_h | 6 #define ScriptState_h |
7 | 7 |
8 #include "bindings/v8/ScopedPersistent.h" | 8 #include "bindings/v8/ScopedPersistent.h" |
9 #include "bindings/v8/V8PerContextData.h" | 9 #include "bindings/v8/V8PerContextData.h" |
10 #include "wtf/RefCounted.h" | 10 #include "wtf/RefCounted.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 { | 39 { |
40 m_context->Exit(); | 40 m_context->Exit(); |
41 } | 41 } |
42 | 42 |
43 private: | 43 private: |
44 v8::HandleScope m_handleScope; | 44 v8::HandleScope m_handleScope; |
45 v8::Handle<v8::Context> m_context; | 45 v8::Handle<v8::Context> m_context; |
46 }; | 46 }; |
47 | 47 |
48 static PassRefPtr<ScriptState> create(v8::Handle<v8::Context>, PassRefPtr<DO
MWrapperWorld>); | 48 static PassRefPtr<ScriptState> create(v8::Handle<v8::Context>, PassRefPtr<DO
MWrapperWorld>); |
49 ~ScriptState(); | 49 virtual ~ScriptState(); |
50 | 50 |
51 static ScriptState* current(v8::Isolate* isolate) | 51 static ScriptState* current(v8::Isolate* isolate) |
52 { | 52 { |
53 return from(isolate->GetCurrentContext()); | 53 return from(isolate->GetCurrentContext()); |
54 } | 54 } |
55 | 55 |
56 static ScriptState* from(v8::Handle<v8::Context> context) | 56 static ScriptState* from(v8::Handle<v8::Context> context) |
57 { | 57 { |
58 ASSERT(!context.IsEmpty()); | 58 ASSERT(!context.IsEmpty()); |
59 ScriptState* scriptState = static_cast<ScriptState*>(context->GetAligned
PointerFromEmbedderData(v8ContextPerContextDataIndex)); | 59 ScriptState* scriptState = static_cast<ScriptState*>(context->GetAligned
PointerFromEmbedderData(v8ContextPerContextDataIndex)); |
60 // ScriptState::from() must not be called for a context that does not ha
ve | 60 // ScriptState::from() must not be called for a context that does not ha
ve |
61 // valid embedder data in the embedder field. | 61 // valid embedder data in the embedder field. |
62 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(scriptState); | 62 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(scriptState); |
63 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(scriptState->context() == conte
xt); | 63 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(scriptState->context() == conte
xt); |
64 return scriptState; | 64 return scriptState; |
65 } | 65 } |
66 | 66 |
67 static ScriptState* forMainWorld(LocalFrame*); | 67 static ScriptState* forMainWorld(LocalFrame*); |
68 | 68 |
69 v8::Isolate* isolate() const { return m_isolate; } | 69 v8::Isolate* isolate() const { return m_isolate; } |
70 DOMWrapperWorld& world() const { return *m_world; } | 70 DOMWrapperWorld& world() const { return *m_world; } |
71 ExecutionContext* executionContext() const; | |
72 DOMWindow* domWindow() const; | 71 DOMWindow* domWindow() const; |
| 72 virtual ExecutionContext* executionContext() const; |
| 73 virtual void setExecutionContext(PassRefPtr<ExecutionContext>); |
73 | 74 |
74 // This can return an empty handle if the v8::Context is gone. | 75 // This can return an empty handle if the v8::Context is gone. |
75 v8::Handle<v8::Context> context() const { return m_context.newLocal(m_isolat
e); } | 76 v8::Handle<v8::Context> context() const { return m_context.newLocal(m_isolat
e); } |
76 bool contextIsEmpty() const { return m_context.isEmpty(); } | 77 bool contextIsEmpty() const { return m_context.isEmpty(); } |
77 void clearContext() { return m_context.clear(); } | 78 void clearContext() { return m_context.clear(); } |
78 | 79 |
79 V8PerContextData* perContextData() const { return m_perContextData.get(); } | 80 V8PerContextData* perContextData() const { return m_perContextData.get(); } |
80 void disposePerContextData() { m_perContextData = nullptr; } | 81 void disposePerContextData() { m_perContextData = nullptr; } |
81 | 82 |
82 bool evalEnabled() const; | 83 bool evalEnabled() const; |
83 void setEvalEnabled(bool); | 84 void setEvalEnabled(bool); |
84 ScriptValue getFromGlobalObject(const char* name); | 85 ScriptValue getFromGlobalObject(const char* name); |
85 | 86 |
86 private: | 87 protected: |
87 ScriptState(v8::Handle<v8::Context>, PassRefPtr<DOMWrapperWorld>); | 88 ScriptState(v8::Handle<v8::Context>, PassRefPtr<DOMWrapperWorld>); |
88 | 89 |
| 90 private: |
89 v8::Isolate* m_isolate; | 91 v8::Isolate* m_isolate; |
90 // This persistent handle is weak. | 92 // This persistent handle is weak. |
91 ScopedPersistent<v8::Context> m_context; | 93 ScopedPersistent<v8::Context> m_context; |
92 | 94 |
93 // This RefPtr doesn't cause a cycle because all persistent handles that DOM
WrapperWorld holds are weak. | 95 // This RefPtr doesn't cause a cycle because all persistent handles that DOM
WrapperWorld holds are weak. |
94 RefPtr<DOMWrapperWorld> m_world; | 96 RefPtr<DOMWrapperWorld> m_world; |
95 | 97 |
96 // This OwnPtr causes a cycle: | 98 // This OwnPtr causes a cycle: |
97 // V8PerContextData --(Persistent)--> v8::Context --(RefPtr)--> ScriptState
--(OwnPtr)--> V8PerContextData | 99 // V8PerContextData --(Persistent)--> v8::Context --(RefPtr)--> ScriptState
--(OwnPtr)--> V8PerContextData |
98 // So you must explicitly clear the OwnPtr by calling disposePerContextData(
) | 100 // So you must explicitly clear the OwnPtr by calling disposePerContextData(
) |
99 // once you no longer need V8PerContextData. Otherwise, the v8::Context will
leak. | 101 // once you no longer need V8PerContextData. Otherwise, the v8::Context will
leak. |
100 OwnPtr<V8PerContextData> m_perContextData; | 102 OwnPtr<V8PerContextData> m_perContextData; |
101 }; | 103 }; |
102 | 104 |
| 105 class ScriptStateForTesting : public ScriptState { |
| 106 public: |
| 107 static PassRefPtr<ScriptStateForTesting> create(v8::Handle<v8::Context>, Pas
sRefPtr<DOMWrapperWorld>); |
| 108 |
| 109 virtual ExecutionContext* executionContext() const OVERRIDE; |
| 110 virtual void setExecutionContext(PassRefPtr<ExecutionContext>) OVERRIDE; |
| 111 |
| 112 private: |
| 113 ScriptStateForTesting(v8::Handle<v8::Context>, PassRefPtr<DOMWrapperWorld>); |
| 114 |
| 115 RefPtr<ExecutionContext> m_executionContext; |
| 116 }; |
| 117 |
103 // ScriptStateProtectingContext keeps the context associated with the ScriptStat
e alive. | 118 // ScriptStateProtectingContext keeps the context associated with the ScriptStat
e alive. |
104 // You need to call clear() once you no longer need the context. Otherwise, the
context will leak. | 119 // You need to call clear() once you no longer need the context. Otherwise, the
context will leak. |
105 class ScriptStateProtectingContext { | 120 class ScriptStateProtectingContext { |
106 WTF_MAKE_NONCOPYABLE(ScriptStateProtectingContext); | 121 WTF_MAKE_NONCOPYABLE(ScriptStateProtectingContext); |
107 public: | 122 public: |
108 ScriptStateProtectingContext(ScriptState* scriptState) | 123 ScriptStateProtectingContext(ScriptState* scriptState) |
109 : m_scriptState(scriptState) | 124 : m_scriptState(scriptState) |
110 { | 125 { |
111 if (m_scriptState) | 126 if (m_scriptState) |
112 m_context.set(m_scriptState->isolate(), m_scriptState->context()); | 127 m_context.set(m_scriptState->isolate(), m_scriptState->context()); |
113 } | 128 } |
114 | 129 |
115 ScriptState* operator->() const { return m_scriptState.get(); } | 130 ScriptState* operator->() const { return m_scriptState.get(); } |
116 ScriptState* get() const { return m_scriptState.get(); } | 131 ScriptState* get() const { return m_scriptState.get(); } |
117 void clear() | 132 void clear() |
118 { | 133 { |
119 m_scriptState = nullptr; | 134 m_scriptState = nullptr; |
120 m_context.clear(); | 135 m_context.clear(); |
121 } | 136 } |
122 | 137 |
123 private: | 138 private: |
124 RefPtr<ScriptState> m_scriptState; | 139 RefPtr<ScriptState> m_scriptState; |
125 ScopedPersistent<v8::Context> m_context; | 140 ScopedPersistent<v8::Context> m_context; |
126 }; | 141 }; |
127 | 142 |
128 } | 143 } |
129 | 144 |
130 #endif // ScriptState_h | 145 #endif // ScriptState_h |
OLD | NEW |