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 #ifndef V8IsolatedContext_h
     32 #define V8IsolatedContext_h
     33 
     34 #include "IsolatedWorld.h"
     35 #include "ScriptSourceCode.h" // for WebCore::ScriptSourceCode
     36 #include "V8DOMWindow.h"
     37 #include "V8Index.h"
     38 #include "V8Proxy.h"
     39 #include "V8Utilities.h"
     40 #include <v8.h>
     41 
     42 namespace WebCore {
     43 
     44 class V8Proxy;
     45 
     46 // V8IsolatedContext
     47 //
     48 // V8IsolatedContext represents a isolated execution environment for
     49 // JavaScript.  Each isolated world executes in parallel with the main
     50 // JavaScript world.  An isolated world has access to the same DOM data
     51 // structures as the main world but none of the JavaScript pointers.
     52 //
     53 // It is an error to ever share a JavaScript pointer between two isolated
     54 // worlds or between an isolated world and the main world.  Because
     55 // isolated worlds have access to the DOM, they need their own DOM wrappers
     56 // to avoid having pointers to the main world's DOM wrappers (which are
     57 // JavaScript objects).
     58 class V8IsolatedContext {
     59 public:
     60     // Creates an isolated world. To destroy it, call destroy().
     61     // This will delete the isolated world when the context it owns is GC'd.
     62     V8IsolatedContext(V8Proxy* proxy, int extensionGroup);
     63     ~V8IsolatedContext();
     64 
     65     // Call this to destroy the isolated world. It will be deleted sometime
     66     // after this call, once all script references to the world's context
     67     // have been dropped.
     68     void destroy();
     69 
     70     // Returns the isolated world associated with
     71     // v8::Context::GetEntered().  Because worlds are isolated, the entire
     72     // JavaScript call stack should be from the same isolated world.
     73     // Returns 0 if the entered context is from the main world.
     74     //
     75     // FIXME: Consider edge cases with DOM mutation events that might
     76     // violate this invariant.
     77     //
     78     static V8IsolatedContext* getEntered()
     79     {
     80         // This is a temporary performance optimization.   Essentially,
     81         // GetHiddenValue is too slow for this code path.  We need to get the
     82         // V8 team to add a real property to v8::Context for isolated worlds.
     83         // Until then, we optimize the common case of not having any isolated
     84         // worlds at all.
     85         if (!IsolatedWorld::count())
     86             return 0;
     87         if (!v8::Context::InContext())
     88             return 0;
     89         return reinterpret_cast<V8IsolatedContext*>(getGlobalObject(v8::Context::GetEntered())->GetPointerFromInternalField(V8DOMWindow::enteredIsolatedWorldIndex));
     90     }
     91 
     92     v8::Handle<v8::Context> context() { return m_context->get(); }
     93     PassRefPtr<SharedPersistent<v8::Context> > sharedContext() { return m_context; }
     94 
     95     IsolatedWorld* world() const { return m_world.get(); }
     96 
     97 private:
     98     static v8::Handle<v8::Object> getGlobalObject(v8::Handle<v8::Context> context)
     99     {
    100         return v8::Handle<v8::Object>::Cast(context->Global()->GetPrototype());
    101     }
    102 
    103     // Called by the garbage collector when our JavaScript context is about
    104     // to be destroyed.
    105     static void contextWeakReferenceCallback(v8::Persistent<v8::Value> object, void* isolatedContext);
    106 
    107     // The underlying v8::Context. This object is keep on the heap as
    108     // long as |m_context| has not been garbage collected.
    109     RefPtr<SharedPersistent<v8::Context> > m_context;
    110 
    111     RefPtr<IsolatedWorld> m_world;
    112 };
    113 
    114 } // namespace WebCore
    115 
    116 #endif // V8IsolatedContext_h
    117