OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2008, Google Inc. |
| 2 // 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 #if ENABLE(WORKERS) |
| 31 |
| 32 #include "config.h" |
| 33 #include "WorkerScriptController.h" |
| 34 |
| 35 #include "ScriptSourceCode.h" |
| 36 #include "ScriptValue.h" |
| 37 #include "v8_proxy.h" |
| 38 #include "WorkerContext.h" |
| 39 #include "WorkerMessagingProxy.h" |
| 40 #include "WorkerThread.h" |
| 41 |
| 42 namespace WebCore { |
| 43 |
| 44 WorkerScriptController::WorkerScriptController(WorkerContext* workerContext) |
| 45 : m_workerContext(workerContext) |
| 46 , m_executionForbidden(false) |
| 47 { |
| 48 } |
| 49 |
| 50 WorkerScriptController::~WorkerScriptController() |
| 51 { |
| 52 Dispose(); |
| 53 } |
| 54 |
| 55 void WorkerScriptController::Dispose() |
| 56 { |
| 57 if (!m_context.IsEmpty()) { |
| 58 m_context.Dispose(); |
| 59 m_context.Clear(); |
| 60 } |
| 61 } |
| 62 |
| 63 void WorkerScriptController::InitContextIfNeeded() |
| 64 { |
| 65 // Bail out if the context has already been initialized. |
| 66 if (!m_context.IsEmpty()) |
| 67 return; |
| 68 |
| 69 // Create a new environment |
| 70 v8::Persistent<v8::ObjectTemplate> global_template; |
| 71 m_context = v8::Context::New(NULL, global_template); |
| 72 |
| 73 // TODO (jianli): to initialize the context. |
| 74 } |
| 75 |
| 76 ScriptValue WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode) |
| 77 { |
| 78 { |
| 79 MutexLocker lock(m_sharedDataMutex); |
| 80 if (m_executionForbidden) |
| 81 return ScriptValue(); |
| 82 } |
| 83 |
| 84 v8::Local<v8::Value> result; |
| 85 { |
| 86 v8::Locker locker; |
| 87 v8::HandleScope hs; |
| 88 |
| 89 InitContextIfNeeded(); |
| 90 v8::Context::Scope scope(m_context); |
| 91 |
| 92 v8::Local<v8::String> code = v8ExternalString(sourceCode.source()); |
| 93 v8::Handle<v8::Script> script = |
| 94 V8Proxy::CompileScript(code, |
| 95 sourceCode.url().string(), |
| 96 sourceCode.startLine() - 1); |
| 97 |
| 98 // TODO (jianli): handle infinite recursion. |
| 99 |
| 100 result = script->Run(); |
| 101 |
| 102 if (V8Proxy::HandleOutOfMemory()) |
| 103 return ScriptValue(); |
| 104 } |
| 105 |
| 106 m_workerContext->thread()->messagingProxy()-> |
| 107 reportWorkerThreadActivity(m_workerContext->hasPendingActivity()); |
| 108 |
| 109 return ScriptValue(result); |
| 110 } |
| 111 |
| 112 void WorkerScriptController::forbidExecution() |
| 113 { |
| 114 // This function is called from another thread. |
| 115 MutexLocker lock(m_sharedDataMutex); |
| 116 m_executionForbidden = true; |
| 117 |
| 118 Dispose(); |
| 119 } |
| 120 |
| 121 } // namespace WebCore |
| 122 |
| 123 #endif // ENABLE(WORKERS) |
OLD | NEW |