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 "V8EventListenerList.h"
40 #include <wtf/OwnPtr.h>
41 #include <wtf/Vector.h>
47 class V8EventListener;
48 class V8WorkerContextEventListener;
51 class WorkerContextExecutionProxy {
53 WorkerContextExecutionProxy(WorkerContext*);
54 ~WorkerContextExecutionProxy();
56 // FIXME: following function sshould have camelCased names once V8 code-generating script is migrated.
57 v8::Local<v8::Context> GetContext() { return v8::Local<v8::Context>::New(m_context); }
58 v8::Local<v8::Function> GetConstructor(V8ClassIndex::V8WrapperType);
59 void RemoveEventListener(V8EventListener*);
61 static v8::Handle<v8::Value> ToV8Object(V8ClassIndex::V8WrapperType type, void* impl);
62 static v8::Handle<v8::Value> EventToV8Object(Event* event);
63 static v8::Handle<v8::Value> EventTargetToV8Object(EventTarget* target);
64 static v8::Handle<v8::Value> WorkerContextToV8Object(WorkerContext* wc);
66 // Finds/creates event listener wrappers.
67 PassRefPtr<V8EventListener> findOrCreateEventListener(v8::Local<v8::Value> listener, bool isInline, bool findOnly);
68 PassRefPtr<V8EventListener> findOrCreateObjectEventListener(v8::Local<v8::Value> object, bool isInline, bool findOnly);
70 // Track the event so that we can detach it from the JS wrapper when a worker
71 // terminates. This is needed because we need to be able to dispose these
72 // events and releases references to their event targets: WorkerContext.
73 void trackEvent(Event*);
75 // Evaluate a script file in the current execution environment.
76 v8::Local<v8::Value> evaluate(const String& script, const String& fileName, int baseLine);
78 // Returns WorkerContext object.
79 WorkerContext* workerContext() { return m_workerContext; }
81 // Returns WorkerContextExecutionProxy object of the currently executing context. 0 will be returned if the current executing context is not the worker context.
82 static WorkerContextExecutionProxy* retrieve();
84 // Enables HTML5 worker support.
85 static bool isWebWorkersEnabled();
86 static void setIsWebWorkersEnabled(bool);
89 void initV8IfNeeded();
90 void initContextIfNeeded();
92 PassRefPtr<V8EventListener> findOrCreateEventListenerHelper(v8::Local<v8::Value> object, bool isInline, bool findOnly, bool createObjectEventListener);
94 // Run an already compiled script.
95 v8::Local<v8::Value> runScript(v8::Handle<v8::Script>);
97 static v8::Local<v8::Object> toV8(V8ClassIndex::V8WrapperType descType, V8ClassIndex::V8WrapperType cptrType, void* impl);
99 static bool forgetV8EventObject(Event*);
101 WorkerContext* m_workerContext;
102 v8::Persistent<v8::Context> m_context;
105 OwnPtr<V8EventListenerList> m_listeners;
106 Vector<Event*> m_events;
109 } // namespace WebCore
111 #endif // ENABLE(WORKERS)
113 #endif // WorkerContextExecutionProxy_h