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