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 "V8IsolatedWorld.h"
34
35 #include <v8.h>
36
37 #include "Frame.h"
38 #include "FrameLoaderClient.h"
39 #include "HashMap.h"
40 #include "ScriptController.h"
41 #include "V8DOMWindow.h"
42 #include "V8HiddenPropertyName.h"
43
44 namespace WebCore {
45
46 static int isolatedWorldCount = 0;
47
contextWeakReferenceCallback(v8::Persistent<v8::Value> object,void * isolated_world)48 static void contextWeakReferenceCallback(v8::Persistent<v8::Value> object, void* isolated_world)
49 {
50 // Our context is going away. Time to clean up the world.
51 V8IsolatedWorld* world = static_cast<V8IsolatedWorld*>(isolated_world);
52 delete world;
53 }
54
evaluate(const Vector<ScriptSourceCode> & sources,V8Proxy * proxy,int extensionGroup)55 void V8IsolatedWorld::evaluate(const Vector<ScriptSourceCode>& sources, V8Proxy* proxy, int extensionGroup)
56 {
57 v8::HandleScope scope;
58 v8::Persistent<v8::Context> context = proxy->createNewContext(v8::Handle<v8::Object>(), extensionGroup);
59
60 // Run code in the new context.
61 v8::Context::Scope context_scope(context);
62
63 // The lifetime of this object is controlled by the V8 GC.
64 // We need to create the world before touching DOM wrappers.
65 V8IsolatedWorld* world = new V8IsolatedWorld(context);
66
67 V8Proxy::installHiddenObjectPrototype(context);
68 proxy->installDOMWindow(context, proxy->frame()->domWindow());
69
70 proxy->frame()->loader()->client()->didCreateIsolatedScriptContext();
71
72 for (size_t i = 0; i < sources.size(); ++i)
73 proxy->evaluate(sources[i], 0);
74
75 // Using the default security token means that the canAccess is always
76 // called, which is slow.
77 // FIXME: Use tokens where possible. This will mean keeping track of all
78 // created contexts so that they can all be updated when the
79 // document domain
80 // changes.
81 // FIXME: Move this statement above proxy->evaluate? It seems like we
82 // should set up the token before running the script.
83 context->UseDefaultSecurityToken();
84
85 context.Dispose();
86 // WARNING! This might well delete |world|.
87 }
88
V8IsolatedWorld(v8::Handle<v8::Context> context)89 V8IsolatedWorld::V8IsolatedWorld(v8::Handle<v8::Context> context)
90 : m_context(v8::Persistent<v8::Context>::New(context))
91 {
92 ++isolatedWorldCount;
93 m_context.MakeWeak(this, &contextWeakReferenceCallback);
94 m_context->Global()->SetHiddenValue(V8HiddenPropertyName::isolatedWorld(), v8::External::Wrap(this));
95 }
96
~V8IsolatedWorld()97 V8IsolatedWorld::~V8IsolatedWorld()
98 {
99 --isolatedWorldCount;
100 m_context.Dispose();
101 m_context.Clear();
102 }
103
getEntered()104 V8IsolatedWorld* V8IsolatedWorld::getEntered()
105 {
106 if (isolatedWorldCount == 0) {
107 // This is a temporary performance optimization. Essentially,
108 // GetHiddenValue is too slow for this code path. We need to get the
109 // V8 team to add a real property to v8::Context for isolated worlds.
110 // Until then, we optimize the common case of not having any isolated
111 // worlds at all.
112 return 0;
113 }
114
115 if (!v8::Context::InContext())
116 return 0;
117 v8::HandleScope scope;
118
119 v8::Local<v8::Value> world = v8::Context::GetEntered()->Global()->GetHiddenValue(V8HiddenPropertyName::isolatedWorld());
120 if (world.IsEmpty())
121 return 0;
122
123 return static_cast<V8IsolatedWorld*>(v8::External::Unwrap(world));
124 }
125
126 } // namespace WebCore
127