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