OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2008, 2009, 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2008, 2009, 2011 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 14 matching lines...) Expand all Loading... |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "bindings/core/v8/WindowProxy.h" | 31 #include "bindings/core/v8/WindowProxy.h" |
32 | 32 |
33 #include <utility> | 33 #include <utility> |
34 | 34 |
| 35 #include "bindings/core/v8/V8Binding.h" |
35 #include "bindings/core/v8/V8DOMWrapper.h" | 36 #include "bindings/core/v8/V8DOMWrapper.h" |
| 37 #include "bindings/core/v8/V8GCForContextDispose.h" |
| 38 #include "bindings/core/v8/V8PagePopupControllerBinding.h" |
| 39 #include "core/frame/DOMWindow.h" |
36 #include "core/frame/Frame.h" | 40 #include "core/frame/Frame.h" |
37 #include "v8/include/v8.h" | 41 #include "v8/include/v8.h" |
38 #include "wtf/Assertions.h" | 42 #include "wtf/Assertions.h" |
39 | 43 |
40 namespace blink { | 44 namespace blink { |
41 | 45 |
42 WindowProxy::~WindowProxy() { | 46 WindowProxy::~WindowProxy() { |
43 // clearForClose() or clearForNavigation() must be invoked before destruction | 47 // clearForClose() or clearForNavigation() must be invoked before destruction |
44 // starts. | 48 // starts. |
45 DCHECK(m_lifecycle != Lifecycle::ContextInitialized); | 49 DCHECK(m_lifecycle != Lifecycle::ContextInitialized); |
46 } | 50 } |
47 | 51 |
48 DEFINE_TRACE(WindowProxy) { | 52 DEFINE_TRACE(WindowProxy) { |
49 visitor->trace(m_frame); | 53 visitor->trace(m_frame); |
50 } | 54 } |
51 | 55 |
52 WindowProxy::WindowProxy(v8::Isolate* isolate, | 56 WindowProxy::WindowProxy(v8::Isolate* isolate, |
53 Frame& frame, | 57 Frame& frame, |
54 RefPtr<DOMWrapperWorld> world) | 58 RefPtr<DOMWrapperWorld> world) |
55 : m_isolate(isolate), | 59 : m_isolate(isolate), |
56 m_frame(frame), | 60 m_frame(frame), |
57 | 61 |
58 m_world(std::move(world)), | 62 m_world(std::move(world)), |
59 m_lifecycle(Lifecycle::ContextUninitialized) {} | 63 m_lifecycle(Lifecycle::ContextUninitialized) {} |
60 | 64 |
| 65 void WindowProxy::disposeContext(GlobalDetachmentBehavior behavior) { |
| 66 DCHECK(m_lifecycle == Lifecycle::ContextInitialized); |
| 67 |
| 68 if (behavior == DetachGlobal) { |
| 69 v8::Local<v8::Context> context = m_scriptState->context(); |
| 70 // Clean up state on the global proxy, which will be reused. |
| 71 if (!m_globalProxy.isEmpty()) { |
| 72 // TODO(yukishiino): This DCHECK failed on Canary (M57) and Dev (M56). |
| 73 // We need to figure out why m_globalProxy != context->Global(). |
| 74 DCHECK(m_globalProxy == context->Global()); |
| 75 DCHECK_EQ(toScriptWrappable(context->Global()), |
| 76 toScriptWrappable( |
| 77 context->Global()->GetPrototype().As<v8::Object>())); |
| 78 m_globalProxy.get().SetWrapperClassId(0); |
| 79 } |
| 80 V8DOMWrapper::clearNativeInfo(m_isolate, context->Global()); |
| 81 m_scriptState->detachGlobalObject(); |
| 82 } |
| 83 |
| 84 m_scriptState->disposePerContextData(); |
| 85 |
| 86 // It's likely that disposing the context has created a lot of |
| 87 // garbage. Notify V8 about this so it'll have a chance of cleaning |
| 88 // it up when idle. |
| 89 V8GCForContextDispose::instance().notifyContextDisposed( |
| 90 m_frame->isMainFrame()); |
| 91 |
| 92 DCHECK(m_lifecycle == Lifecycle::ContextInitialized); |
| 93 m_lifecycle = Lifecycle::ContextDetached; |
| 94 } |
| 95 |
61 void WindowProxy::clearForClose() { | 96 void WindowProxy::clearForClose() { |
62 disposeContext(DoNotDetachGlobal); | 97 disposeContext(DoNotDetachGlobal); |
63 } | 98 } |
64 | 99 |
65 void WindowProxy::clearForNavigation() { | 100 void WindowProxy::clearForNavigation() { |
66 disposeContext(DetachGlobal); | 101 disposeContext(DetachGlobal); |
67 } | 102 } |
68 | 103 |
69 v8::Local<v8::Object> WindowProxy::globalIfNotDetached() { | 104 v8::Local<v8::Object> WindowProxy::globalIfNotDetached() { |
70 if (m_lifecycle == Lifecycle::ContextInitialized) | 105 if (m_lifecycle == Lifecycle::ContextInitialized) { |
| 106 DCHECK(m_scriptState->contextIsValid()); |
| 107 DCHECK(m_globalProxy == m_scriptState->context()->Global()); |
71 return m_globalProxy.newLocal(m_isolate); | 108 return m_globalProxy.newLocal(m_isolate); |
| 109 } |
72 return v8::Local<v8::Object>(); | 110 return v8::Local<v8::Object>(); |
73 } | 111 } |
74 | 112 |
75 v8::Local<v8::Object> WindowProxy::releaseGlobal() { | 113 v8::Local<v8::Object> WindowProxy::releaseGlobal() { |
76 DCHECK(m_lifecycle != Lifecycle::ContextInitialized); | 114 DCHECK(m_lifecycle != Lifecycle::ContextInitialized); |
77 DLOG_IF(FATAL, m_isGlobalProxyAttached) | 115 // Make sure the global object was detached from the proxy by calling |
78 << "Context not detached by calling clearForNavigation()"; | 116 // clearForNavigation(). |
| 117 if (m_lifecycle == Lifecycle::ContextDetached) |
| 118 ASSERT(m_scriptState->isGlobalObjectDetached()); |
79 | 119 |
80 v8::Local<v8::Object> global = m_globalProxy.newLocal(m_isolate); | 120 v8::Local<v8::Object> global = m_globalProxy.newLocal(m_isolate); |
81 m_globalProxy.clear(); | 121 m_globalProxy.clear(); |
82 return global; | 122 return global; |
83 } | 123 } |
84 | 124 |
85 void WindowProxy::setGlobal(v8::Local<v8::Object> global) { | 125 void WindowProxy::setGlobal(v8::Local<v8::Object> global) { |
86 m_globalProxy.set(m_isolate, global); | 126 m_globalProxy.set(m_isolate, global); |
87 | 127 |
88 // Initialize the window proxy now, to re-establish the connection between | 128 // Initialize the window proxy now, to re-establish the connection between |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 // If there are JS code holds a closure to the old inner window, | 170 // If there are JS code holds a closure to the old inner window, |
131 // it won't be able to reach the outer window via its global object. | 171 // it won't be able to reach the outer window via its global object. |
132 void WindowProxy::initializeIfNeeded() { | 172 void WindowProxy::initializeIfNeeded() { |
133 // TODO(haraken): It is wrong to re-initialize an already detached window | 173 // TODO(haraken): It is wrong to re-initialize an already detached window |
134 // proxy. This must be 'if(m_lifecycle == Lifecycle::ContextUninitialized)'. | 174 // proxy. This must be 'if(m_lifecycle == Lifecycle::ContextUninitialized)'. |
135 if (m_lifecycle != Lifecycle::ContextInitialized) { | 175 if (m_lifecycle != Lifecycle::ContextInitialized) { |
136 initialize(); | 176 initialize(); |
137 } | 177 } |
138 } | 178 } |
139 | 179 |
| 180 void WindowProxy::setupWindowPrototypeChain() { |
| 181 // Associate the window wrapper object and its prototype chain with the |
| 182 // corresponding native DOMWindow object. |
| 183 // The full structure of the global object's prototype chain is as follows: |
| 184 // |
| 185 // global proxy object [1] |
| 186 // -- has prototype --> global object (window wrapper object) [2] |
| 187 // -- has prototype --> Window.prototype |
| 188 // -- has prototype --> WindowProperties [3] |
| 189 // -- has prototype --> EventTarget.prototype |
| 190 // -- has prototype --> Object.prototype |
| 191 // -- has prototype --> null |
| 192 // |
| 193 // [1] Global proxy object is as known as "outer global object". It's an |
| 194 // empty object and remains after navigation. When navigated, points to |
| 195 // a different global object as the prototype object. |
| 196 // [2] Global object is as known as "inner global object" or "window wrapper |
| 197 // object". The prototype chain between global proxy object and global |
| 198 // object is NOT observable from user JavaScript code. All other |
| 199 // prototype chains are observable. Global proxy object and global object |
| 200 // together appear to be the same single JavaScript object. See also: |
| 201 // https://wiki.mozilla.org/Gecko:SplitWindow |
| 202 // global object (= window wrapper object) provides most of Window's DOM |
| 203 // attributes and operations. Also global variables defined by user |
| 204 // JavaScript are placed on this object. When navigated, a new global |
| 205 // object is created together with a new v8::Context, but the global proxy |
| 206 // object doesn't change. |
| 207 // [3] WindowProperties is a named properties object of Window interface. |
| 208 |
| 209 DOMWindow* window = m_frame->domWindow(); |
| 210 const WrapperTypeInfo* wrapperTypeInfo = window->wrapperTypeInfo(); |
| 211 v8::Local<v8::Context> context = m_scriptState->context(); |
| 212 |
| 213 // The global proxy object. Note this is not the global object. |
| 214 v8::Local<v8::Object> globalProxy = context->Global(); |
| 215 CHECK(m_globalProxy == globalProxy); |
| 216 V8DOMWrapper::setNativeInfo(m_isolate, globalProxy, wrapperTypeInfo, window); |
| 217 // Mark the handle to be traced by Oilpan, since the global proxy has a |
| 218 // reference to the DOMWindow. |
| 219 m_globalProxy.get().SetWrapperClassId(wrapperTypeInfo->wrapperClassId); |
| 220 |
| 221 // The global object, aka window wrapper object. |
| 222 v8::Local<v8::Object> windowWrapper = |
| 223 globalProxy->GetPrototype().As<v8::Object>(); |
| 224 windowWrapper = V8DOMWrapper::associateObjectWithWrapper( |
| 225 m_isolate, window, wrapperTypeInfo, windowWrapper); |
| 226 |
| 227 // The prototype object of Window interface. |
| 228 v8::Local<v8::Object> windowPrototype = |
| 229 windowWrapper->GetPrototype().As<v8::Object>(); |
| 230 CHECK(!windowPrototype.IsEmpty()); |
| 231 V8DOMWrapper::setNativeInfo(m_isolate, windowPrototype, wrapperTypeInfo, |
| 232 window); |
| 233 |
| 234 // The named properties object of Window interface. |
| 235 v8::Local<v8::Object> windowProperties = |
| 236 windowPrototype->GetPrototype().As<v8::Object>(); |
| 237 CHECK(!windowProperties.IsEmpty()); |
| 238 V8DOMWrapper::setNativeInfo(m_isolate, windowProperties, wrapperTypeInfo, |
| 239 window); |
| 240 |
| 241 // TODO(keishi): Remove installPagePopupController and implement |
| 242 // PagePopupController in another way. |
| 243 V8PagePopupControllerBinding::installPagePopupController(context, |
| 244 windowWrapper); |
| 245 } |
| 246 |
140 } // namespace blink | 247 } // namespace blink |
OLD | NEW |