2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #ifndef WorkerContextExecutionProxy_h
33 #define WorkerContextExecutionProxy_h
38 #include "ScriptValue.h"
39 #include "V8EventListenerList.h"
41 #include <wtf/OwnPtr.h>
42 #include <wtf/Vector.h>
48 class V8EventListener;
49 class V8WorkerContextEventListener;
52 struct WorkerContextExecutionState {
53 WorkerContextExecutionState() : hadException(false), lineNumber(0) { }
56 ScriptValue exception;
62 class WorkerContextExecutionProxy {
64 WorkerContextExecutionProxy(WorkerContext*);
65 ~WorkerContextExecutionProxy();
67 // FIXME: following function should have camelCased names once V8 code-generating script is migrated.
68 v8::Local<v8::Context> GetContext() { return v8::Local<v8::Context>::New(m_context); }
69 v8::Local<v8::Function> GetConstructor(V8ClassIndex::V8WrapperType);
70 void RemoveEventListener(V8EventListener*);
72 static v8::Handle<v8::Value> ToV8Object(V8ClassIndex::V8WrapperType type, void* impl);
73 static v8::Handle<v8::Value> EventToV8Object(Event* event);
74 static v8::Handle<v8::Value> EventTargetToV8Object(EventTarget* target);
75 static v8::Handle<v8::Value> WorkerContextToV8Object(WorkerContext* wc);
77 // Finds/creates event listener wrappers.
78 PassRefPtr<V8EventListener> findOrCreateEventListener(v8::Local<v8::Value> listener, bool isInline, bool findOnly);
79 PassRefPtr<V8EventListener> findOrCreateObjectEventListener(v8::Local<v8::Value> object, bool isInline, bool findOnly);
81 // Track the event so that we can detach it from the JS wrapper when a worker
82 // terminates. This is needed because we need to be able to dispose these
83 // events and releases references to their event targets: WorkerContext.
84 void trackEvent(Event*);
86 // Evaluate a script file in the current execution environment.
87 ScriptValue evaluate(const String& script, const String& fileName, int baseLine, WorkerContextExecutionState*);
89 // Returns WorkerContext object.
90 WorkerContext* workerContext() { return m_workerContext; }
92 // Returns WorkerContextExecutionProxy object of the currently executing context. 0 will be returned if the current executing context is not the worker context.
93 static WorkerContextExecutionProxy* retrieve();
96 void initV8IfNeeded();
97 void initContextIfNeeded();
99 PassRefPtr<V8EventListener> findOrCreateEventListenerHelper(v8::Local<v8::Value> object, bool isInline, bool findOnly, bool createObjectEventListener);
101 // Run an already compiled script.
102 v8::Local<v8::Value> runScript(v8::Handle<v8::Script>);
104 static v8::Local<v8::Object> toV8(V8ClassIndex::V8WrapperType descType, V8ClassIndex::V8WrapperType cptrType, void* impl);
106 static bool forgetV8EventObject(Event*);
108 WorkerContext* m_workerContext;
109 v8::Persistent<v8::Context> m_context;
112 OwnPtr<V8EventListenerList> m_listeners;
113 Vector<Event*> m_events;
116 } // namespace WebCore
118 #endif // ENABLE(WORKERS)
120 #endif // WorkerContextExecutionProxy_h