Home | History | Annotate | Download | only in v8
      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 "V8EventListenerList.h"
     39 #include "V8Index.h"
     40 #include <v8.h>
     41 #include <wtf/OwnPtr.h>
     42 #include <wtf/Vector.h>
     43 
     44 namespace WebCore {
     45 
     46     class Event;
     47     class EventTarget;
     48     class V8EventListener;
     49     class V8WorkerContextEventListener;
     50     class WorkerContext;
     51 
     52     struct WorkerContextExecutionState {
     53         WorkerContextExecutionState() : hadException(false), lineNumber(0) { }
     54 
     55         bool hadException;
     56         ScriptValue exception;
     57         String errorMessage;
     58         int lineNumber;
     59         String sourceURL;
     60     };
     61 
     62     class WorkerContextExecutionProxy {
     63     public:
     64         WorkerContextExecutionProxy(WorkerContext*);
     65         ~WorkerContextExecutionProxy();
     66 
     67         // Finds/creates event listener wrappers.
     68         PassRefPtr<V8EventListener> findOrCreateEventListener(v8::Local<v8::Value> listener, bool isInline, bool findOnly);
     69 
     70         // Track the event so that we can detach it from the JS wrapper when a worker
     71         // terminates. This is needed because we need to be able to dispose these
     72         // events and releases references to their event targets: WorkerContext.
     73         void trackEvent(Event*);
     74 
     75         // Evaluate a script file in the current execution environment.
     76         ScriptValue evaluate(const String& script, const String& fileName, int baseLine, WorkerContextExecutionState*);
     77 
     78         // Returns a local handle of the context.
     79         v8::Local<v8::Context> context() { return v8::Local<v8::Context>::New(m_context); }
     80 
     81         // Returns WorkerContext object.
     82         WorkerContext* workerContext() { return m_workerContext; }
     83 
     84         // Returns WorkerContextExecutionProxy object of the currently executing context. 0 will be returned if the current executing context is not the worker context.
     85         static WorkerContextExecutionProxy* retrieve();
     86 
     87     private:
     88         void initV8IfNeeded();
     89         void initContextIfNeeded();
     90         void dispose();
     91 
     92         // Run an already compiled script.
     93         v8::Local<v8::Value> runScript(v8::Handle<v8::Script>);
     94 
     95         static bool forgetV8EventObject(Event*);
     96 
     97         static const int kWorkerMaxStackSize = 500 * 1024;
     98 
     99         WorkerContext* m_workerContext;
    100         v8::Persistent<v8::Context> m_context;
    101         int m_recursion;
    102 
    103         Vector<Event*> m_events;
    104     };
    105 
    106 } // namespace WebCore
    107 
    108 #endif // ENABLE(WORKERS)
    109 
    110 #endif // WorkerContextExecutionProxy_h
    111