1 /* 2 * Copyright (C) 2008, 2009, 2011 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 are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #ifndef ScriptState_h 32 #define ScriptState_h 33 34 #include "DOMWrapperWorld.h" 35 #include <v8.h> 36 #include <wtf/Noncopyable.h> 37 #include <wtf/RefCounted.h> 38 39 namespace WebCore { 40 class DOMWrapperWorld; 41 class Frame; 42 class Node; 43 class Page; 44 class WorkerContext; 45 46 class ScriptState { 47 WTF_MAKE_NONCOPYABLE(ScriptState); 48 public: 49 bool hadException() { return !m_exception.IsEmpty(); } 50 void setException(v8::Local<v8::Value> exception) 51 { 52 m_exception = exception; 53 } 54 v8::Local<v8::Value> exception() { return m_exception; } 55 56 v8::Local<v8::Context> context() const 57 { 58 return v8::Local<v8::Context>::New(m_context); 59 } 60 61 static ScriptState* forContext(v8::Local<v8::Context>); 62 static ScriptState* current(); 63 64 protected: 65 ScriptState() { } 66 ~ScriptState(); 67 68 private: 69 friend ScriptState* mainWorldScriptState(Frame*); 70 explicit ScriptState(v8::Handle<v8::Context>); 71 72 static void weakReferenceCallback(v8::Persistent<v8::Value> object, void* parameter); 73 74 v8::Local<v8::Value> m_exception; 75 v8::Persistent<v8::Context> m_context; 76 }; 77 78 class EmptyScriptState : public ScriptState { 79 public: 80 EmptyScriptState() : ScriptState() { } 81 ~EmptyScriptState() { } 82 }; 83 84 class ScriptStateProtectedPtr { 85 WTF_MAKE_NONCOPYABLE(ScriptStateProtectedPtr); 86 public: 87 ScriptStateProtectedPtr() : m_scriptState(0) { } 88 ScriptStateProtectedPtr(ScriptState* scriptState) : m_scriptState(scriptState) 89 { 90 v8::HandleScope handleScope; 91 // Keep the context from being GC'ed. ScriptState is guaranteed to be live while the context is live. 92 m_context = v8::Persistent<v8::Context>::New(scriptState->context()); 93 } 94 ~ScriptStateProtectedPtr() 95 { 96 if (!m_context.IsEmpty()) { 97 m_context.Dispose(); 98 m_context.Clear(); 99 } 100 } 101 ScriptState* get() const { return m_scriptState; } 102 private: 103 ScriptState* m_scriptState; 104 v8::Persistent<v8::Context> m_context; 105 }; 106 107 ScriptState* mainWorldScriptState(Frame*); 108 109 ScriptState* scriptStateFromNode(DOMWrapperWorld*, Node*); 110 ScriptState* scriptStateFromPage(DOMWrapperWorld*, Page*); 111 112 #if ENABLE(WORKERS) 113 ScriptState* scriptStateFromWorkerContext(WorkerContext*); 114 #endif 115 116 inline DOMWrapperWorld* debuggerWorld() { return mainThreadNormalWorld(); } 117 inline DOMWrapperWorld* pluginWorld() { return mainThreadNormalWorld(); } 118 119 } 120 121 #endif // ScriptState_h 122