Home | History | Annotate | Download | only in v8
      1 /*
      2  * Copyright (C) 2009, 2012 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 #ifndef WorkerScriptController_h
     32 #define WorkerScriptController_h
     33 
     34 #include "bindings/v8/ScriptValue.h"
     35 #include "bindings/v8/V8Binding.h"
     36 #include "core/events/ErrorEvent.h"
     37 #include "gin/public/context_holder.h"
     38 #include "gin/public/isolate_holder.h"
     39 #include "wtf/OwnPtr.h"
     40 #include "wtf/ThreadingPrimitives.h"
     41 #include "wtf/text/TextPosition.h"
     42 #include <v8.h>
     43 
     44 namespace gin {
     45 class IsolateHolder;
     46 }
     47 
     48 namespace WebCore {
     49 
     50     class ScriptSourceCode;
     51     class ScriptValue;
     52     class WorkerGlobalScope;
     53 
     54     struct WorkerGlobalScopeExecutionState {
     55         WorkerGlobalScopeExecutionState()
     56             : hadException(false)
     57             , lineNumber(0)
     58             , columnNumber(0)
     59         {
     60         }
     61 
     62         bool hadException;
     63         String errorMessage;
     64         int lineNumber;
     65         int columnNumber;
     66         String sourceURL;
     67         ScriptValue exception;
     68     };
     69 
     70     class WorkerScriptController {
     71     public:
     72         explicit WorkerScriptController(WorkerGlobalScope&);
     73         ~WorkerScriptController();
     74 
     75         WorkerGlobalScope& workerGlobalScope() { return m_workerGlobalScope; }
     76 
     77         void evaluate(const ScriptSourceCode&, RefPtr<ErrorEvent>* = 0);
     78 
     79         void rethrowExceptionFromImportedScript(PassRefPtr<ErrorEvent>);
     80 
     81         // Async request to terminate a future JS execution. Eventually causes termination
     82         // exception raised during JS execution, if the worker thread happens to run JS.
     83         // After JS execution was terminated in this way, the Worker thread has to use
     84         // forbidExecution()/isExecutionForbidden() to guard against reentry into JS.
     85         // Can be called from any thread.
     86         void scheduleExecutionTermination();
     87         bool isExecutionTerminating() const;
     88 
     89         // Called on Worker thread when JS exits with termination exception caused by forbidExecution() request,
     90         // or by Worker thread termination code to prevent future entry into JS.
     91         void forbidExecution();
     92         bool isExecutionForbidden() const;
     93 
     94         void disableEval(const String&);
     95 
     96         // Returns WorkerScriptController for the currently executing context. 0 will be returned if the current executing context is not the worker context.
     97         static WorkerScriptController* controllerForContext();
     98 
     99         // Evaluate a script file in the current execution environment.
    100         ScriptValue evaluate(const String& script, const String& fileName, const TextPosition& scriptStartPosition, WorkerGlobalScopeExecutionState*);
    101 
    102         // Returns a local handle of the context.
    103         v8::Local<v8::Context> context() { return m_contextHolder ? m_contextHolder->context() : v8::Local<v8::Context>(); }
    104 
    105         // Send a notification about current thread is going to be idle.
    106         // Returns true if the embedder should stop calling idleNotification
    107         // until real work has been done.
    108         bool idleNotification() { return v8::V8::IdleNotification(); }
    109 
    110         v8::Isolate* isolate() const { return m_isolateHolder->isolate(); }
    111 
    112     private:
    113         bool initializeContextIfNeeded();
    114         void disposeContext();
    115 
    116         WorkerGlobalScope& m_workerGlobalScope;
    117         OwnPtr<gin::IsolateHolder> m_isolateHolder;
    118         OwnPtr<gin::ContextHolder> m_contextHolder;
    119         OwnPtr<V8PerContextData> m_perContextData;
    120         String m_disableEvalPending;
    121         OwnPtr<DOMDataStore> m_domDataStore;
    122         bool m_executionForbidden;
    123         bool m_executionScheduledToTerminate;
    124         mutable Mutex m_scheduledTerminationMutex;
    125         RefPtr<ErrorEvent> m_errorEventFromImportedScript;
    126     };
    127 
    128 } // namespace WebCore
    129 
    130 #endif // WorkerScriptController_h
    131