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 #include "config.h"
     32 
     33 #include "V8IsolatedContext.h"
     34 
     35 #include "Frame.h"
     36 #include "FrameLoaderClient.h"
     37 #include "HashMap.h"
     38 #include "ScriptController.h"
     39 #include "V8DOMWindow.h"
     40 #include "V8HiddenPropertyName.h"
     41 #include <v8.h>
     42 
     43 namespace WebCore {
     44 
     45 
     46 void V8IsolatedContext::contextWeakReferenceCallback(v8::Persistent<v8::Value> object, void* isolatedContext)
     47 {
     48     // Our context is going away.  Time to clean up the world.
     49     V8IsolatedContext* context = static_cast<V8IsolatedContext*>(isolatedContext);
     50     delete context;
     51 }
     52 
     53 V8IsolatedContext::V8IsolatedContext(V8Proxy* proxy, int extensionGroup)
     54     : m_world(IsolatedWorld::create())
     55 {
     56     v8::HandleScope scope;
     57     // FIXME: We should be creating a new V8DOMWindowShell here instead of riping out the context.
     58     m_context = SharedPersistent<v8::Context>::create(proxy->windowShell()->createNewContext(v8::Handle<v8::Object>(), extensionGroup));
     59     if (m_context->get().IsEmpty())
     60         return;
     61 
     62     // Run code in the new context.
     63     v8::Context::Scope contextScope(m_context->get());
     64 
     65     getGlobalObject(m_context->get())->SetPointerInInternalField(V8DOMWindow::enteredIsolatedWorldIndex, this);
     66 
     67     V8DOMWindowShell::installHiddenObjectPrototype(m_context->get());
     68     // FIXME: This will go away once we have a windowShell for the isolated world.
     69     proxy->windowShell()->installDOMWindow(m_context->get(), proxy->frame()->domWindow());
     70 
     71     // Using the default security token means that the canAccess is always
     72     // called, which is slow.
     73     // FIXME: Use tokens where possible. This will mean keeping track of all
     74     //        created contexts so that they can all be updated when the
     75     //        document domain
     76     //        changes.
     77     m_context->get()->UseDefaultSecurityToken();
     78 
     79     proxy->frame()->loader()->client()->didCreateIsolatedScriptContext();
     80 }
     81 
     82 void V8IsolatedContext::destroy()
     83 {
     84     m_context->get().MakeWeak(this, &contextWeakReferenceCallback);
     85 }
     86 
     87 V8IsolatedContext::~V8IsolatedContext()
     88 {
     89     m_context->disposeHandle();
     90 }
     91 
     92 } // namespace WebCore
     93