initial import
[vuplus_webkit] / Source / WebCore / bindings / v8 / WorkerScriptController.cpp
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 #include "config.h"
32
33 #if ENABLE(WORKERS)
34
35 #include "WorkerScriptController.h"
36
37 #include "DOMTimer.h"
38 #include "ScriptCallStack.h"
39 #include "ScriptSourceCode.h"
40 #include "ScriptValue.h"
41 #include "V8DOMMap.h"
42 #include "V8Proxy.h"
43 #include "V8WorkerContext.h"
44 #include "WorkerContext.h"
45 #include "WorkerContextExecutionProxy.h"
46 #include "WorkerObjectProxy.h"
47 #include "WorkerThread.h"
48 #include <v8.h>
49
50 namespace WebCore {
51
52 WorkerScriptController::WorkerScriptController(WorkerContext* workerContext)
53     : m_workerContext(workerContext)
54     , m_isolate(v8::Isolate::New())
55     , m_executionForbidden(false)
56 {
57     V8BindingPerIsolateData* data = V8BindingPerIsolateData::create(m_isolate);
58     data->allStores().append(&m_DOMDataStore);
59     data->setDOMDataStore(&m_DOMDataStore);
60     m_isolate->Enter();
61     m_proxy = adoptPtr(new WorkerContextExecutionProxy(workerContext));
62 }
63
64 WorkerScriptController::~WorkerScriptController()
65 {
66     removeAllDOMObjects();
67     m_proxy.clear();
68     m_isolate->Exit();
69     V8BindingPerIsolateData::dispose(m_isolate);
70     m_isolate->Dispose();
71 }
72
73 ScriptValue WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode)
74 {
75     return evaluate(sourceCode, 0);
76 }
77
78 ScriptValue WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode, ScriptValue* exception)
79 {
80     if (isExecutionForbidden())
81         return ScriptValue();
82
83     WorkerContextExecutionState state;
84     ScriptValue result = m_proxy->evaluate(sourceCode.source(), sourceCode.url().string(), WTF::toZeroBasedTextPosition(sourceCode.startPosition()), &state);
85     if (state.hadException) {
86         if (exception)
87             *exception = state.exception;
88         else
89             m_workerContext->reportException(state.errorMessage, state.lineNumber, state.sourceURL, 0);
90     }
91
92     return result;
93 }
94
95 void WorkerScriptController::scheduleExecutionTermination()
96 {
97     v8::V8::TerminateExecution(m_isolate);
98 }
99
100 void WorkerScriptController::forbidExecution()
101 {
102     ASSERT(m_workerContext->isContextThread());
103     m_executionForbidden = true;
104 }
105
106 bool WorkerScriptController::isExecutionForbidden() const
107 {
108     ASSERT(m_workerContext->isContextThread());
109     return m_executionForbidden;
110 }
111
112 void WorkerScriptController::setException(ScriptValue exception)
113 {
114     throwError(*exception.v8Value());
115 }
116
117 WorkerScriptController* WorkerScriptController::controllerForContext()
118 {
119     // Happens on frame destruction, check otherwise GetCurrent() will crash.
120     if (!v8::Context::InContext())
121         return 0;
122     v8::Handle<v8::Context> context = v8::Context::GetCurrent();
123     v8::Handle<v8::Object> global = context->Global();
124     global = V8DOMWrapper::lookupDOMWrapper(V8WorkerContext::GetTemplate(), global);
125     // Return 0 if the current executing context is not the worker context.
126     if (global.IsEmpty())
127         return 0;
128     WorkerContext* workerContext = V8WorkerContext::toNative(global);
129     return workerContext->script();
130 }
131
132 } // namespace WebCore
133
134 #endif // ENABLE(WORKERS)