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