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