1/*
2 * Copyright (C) 2009 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
32#ifndef WorkerContextExecutionProxy_h
33#define WorkerContextExecutionProxy_h
34
35#if ENABLE(WORKERS)
36
37#include "ScriptValue.h"
38#include <v8.h>
39#include <wtf/text/TextPosition.h>
40#include <wtf/OwnPtr.h>
41#include <wtf/Vector.h>
42
43namespace WebCore {
44
45    class Event;
46    class EventTarget;
47    class WorkerContext;
48
49    struct WorkerContextExecutionState {
50        WorkerContextExecutionState() : hadException(false), lineNumber(0) { }
51
52        bool hadException;
53        ScriptValue exception;
54        String errorMessage;
55        int lineNumber;
56        String sourceURL;
57    };
58
59    class WorkerContextExecutionProxy {
60    public:
61        WorkerContextExecutionProxy(WorkerContext*);
62        ~WorkerContextExecutionProxy();
63
64        // Track the event so that we can detach it from the JS wrapper when a worker
65        // terminates. This is needed because we need to be able to dispose these
66        // events and releases references to their event targets: WorkerContext.
67        void trackEvent(Event*);
68
69        // Evaluate a script file in the current execution environment.
70        ScriptValue evaluate(const String& script, const String& fileName, const TextPosition0& scriptStartPosition, WorkerContextExecutionState*);
71
72        // Returns a local handle of the context.
73        v8::Local<v8::Context> context() { return v8::Local<v8::Context>::New(m_context); }
74
75    private:
76        void initV8IfNeeded();
77        bool initContextIfNeeded();
78        void dispose();
79
80        // Run an already compiled script.
81        v8::Local<v8::Value> runScript(v8::Handle<v8::Script>);
82
83        static bool forgetV8EventObject(Event*);
84
85        static const int kWorkerMaxStackSize = 500 * 1024;
86
87        WorkerContext* m_workerContext;
88        v8::Persistent<v8::Context> m_context;
89        int m_recursion;
90
91        Vector<Event*> m_events;
92    };
93
94} // namespace WebCore
95
96#endif // ENABLE(WORKERS)
97
98#endif // WorkerContextExecutionProxy_h
99