| 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 15 matching lines...) Expand all Loading... |
| 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/RemoteWindowProxy.h" | 31 #include "bindings/core/v8/RemoteWindowProxy.h" |
| 32 | 32 |
| 33 #include <utility> | 33 #include <utility> |
| 34 | 34 |
| 35 #include "bindings/core/v8/DOMWrapperWorld.h" | 35 #include "bindings/core/v8/DOMWrapperWorld.h" |
| 36 #include "bindings/core/v8/V8GCForContextDispose.h" | 36 #include "bindings/core/v8/V8DOMWrapper.h" |
| 37 #include "bindings/core/v8/V8Window.h" | 37 #include "bindings/core/v8/V8Window.h" |
| 38 #include "platform/Histogram.h" | 38 #include "platform/Histogram.h" |
| 39 #include "platform/RuntimeEnabledFeatures.h" | |
| 40 #include "platform/ScriptForbiddenScope.h" | |
| 41 #include "platform/heap/Handle.h" | |
| 42 #include "platform/instrumentation/tracing/TraceEvent.h" | 39 #include "platform/instrumentation/tracing/TraceEvent.h" |
| 43 #include "v8/include/v8.h" | 40 #include "v8/include/v8.h" |
| 44 #include "wtf/Assertions.h" | 41 #include "wtf/Assertions.h" |
| 45 | 42 |
| 46 namespace blink { | 43 namespace blink { |
| 47 | 44 |
| 48 RemoteWindowProxy::RemoteWindowProxy(v8::Isolate* isolate, | 45 RemoteWindowProxy::RemoteWindowProxy(v8::Isolate* isolate, |
| 49 RemoteFrame& frame, | 46 RemoteFrame& frame, |
| 50 RefPtr<DOMWrapperWorld> world) | 47 RefPtr<DOMWrapperWorld> world) |
| 51 : WindowProxy(isolate, frame, std::move(world)) {} | 48 : WindowProxy(isolate, frame, std::move(world)) {} |
| 52 | 49 |
| 53 void RemoteWindowProxy::disposeContext(GlobalDetachmentBehavior behavior) { | 50 void RemoteWindowProxy::disposeContext(GlobalDetachmentBehavior behavior) { |
| 54 if (m_lifecycle != Lifecycle::ContextInitialized) | 51 if (m_lifecycle != Lifecycle::ContextInitialized) |
| 55 return; | 52 return; |
| 56 | 53 |
| 57 if (behavior == DetachGlobal) { | 54 if (behavior == DetachGlobal && !m_globalProxy.isEmpty()) { |
| 58 v8::Local<v8::Context> context = m_scriptState->context(); | 55 m_globalProxy.get().SetWrapperClassId(0); |
| 59 // Clean up state on the global proxy, which will be reused. | 56 V8DOMWrapper::clearNativeInfo(isolate(), m_globalProxy.newLocal(isolate())); |
| 60 if (!m_globalProxy.isEmpty()) { | |
| 61 // TODO(yukishiino): This DCHECK failed on Canary (M57) and Dev (M56). | |
| 62 // We need to figure out why m_globalProxy != context->Global(). | |
| 63 DCHECK(m_globalProxy == context->Global()); | |
| 64 DCHECK_EQ(toScriptWrappable(context->Global()), | |
| 65 toScriptWrappable( | |
| 66 context->Global()->GetPrototype().As<v8::Object>())); | |
| 67 m_globalProxy.get().SetWrapperClassId(0); | |
| 68 } | |
| 69 V8DOMWrapper::clearNativeInfo(isolate(), context->Global()); | |
| 70 m_scriptState->detachGlobalObject(); | |
| 71 | |
| 72 #if DCHECK_IS_ON() | 57 #if DCHECK_IS_ON() |
| 73 didDetachGlobalProxy(); | 58 didDetachGlobalProxy(); |
| 74 #endif | 59 #endif |
| 75 } | 60 } |
| 76 | 61 |
| 77 m_scriptState->disposePerContextData(); | 62 DCHECK_EQ(Lifecycle::ContextInitialized, m_lifecycle); |
| 78 | |
| 79 // It's likely that disposing the context has created a lot of | |
| 80 // garbage. Notify V8 about this so it'll have a chance of cleaning | |
| 81 // it up when idle. | |
| 82 V8GCForContextDispose::instance().notifyContextDisposed( | |
| 83 frame()->isMainFrame()); | |
| 84 | |
| 85 DCHECK(m_lifecycle == Lifecycle::ContextInitialized); | |
| 86 m_lifecycle = Lifecycle::ContextDetached; | 63 m_lifecycle = Lifecycle::ContextDetached; |
| 87 } | 64 } |
| 88 | 65 |
| 89 void RemoteWindowProxy::initialize() { | 66 void RemoteWindowProxy::initialize() { |
| 90 TRACE_EVENT1("v8", "RemoteWindowProxy::initialize", "isMainWindow", | 67 TRACE_EVENT1("v8", "RemoteWindowProxy::initialize", "isMainWindow", |
| 91 frame()->isMainFrame()); | 68 frame()->isMainFrame()); |
| 92 SCOPED_BLINK_UMA_HISTOGRAM_TIMER( | 69 SCOPED_BLINK_UMA_HISTOGRAM_TIMER( |
| 93 frame()->isMainFrame() ? "Blink.Binding.InitializeMainWindowProxy" | 70 frame()->isMainFrame() ? "Blink.Binding.InitializeMainWindowProxy" |
| 94 : "Blink.Binding.InitializeNonMainWindowProxy"); | 71 : "Blink.Binding.InitializeNonMainWindowProxy"); |
| 95 | 72 |
| 96 ScriptForbiddenScope::AllowUserAgentScript allowScript; | |
| 97 | |
| 98 v8::HandleScope handleScope(isolate()); | 73 v8::HandleScope handleScope(isolate()); |
| 99 | 74 |
| 100 createContext(); | 75 createContext(); |
| 101 | 76 |
| 102 ScriptState::Scope scope(m_scriptState.get()); | |
| 103 v8::Local<v8::Context> context = m_scriptState->context(); | |
| 104 if (m_globalProxy.isEmpty()) { | |
| 105 m_globalProxy.set(isolate(), context->Global()); | |
| 106 CHECK(!m_globalProxy.isEmpty()); | |
| 107 } | |
| 108 | |
| 109 setupWindowPrototypeChain(); | 77 setupWindowPrototypeChain(); |
| 110 | |
| 111 // Remote frames always require a full canAccess() check. | |
| 112 context->UseDefaultSecurityToken(); | |
| 113 } | 78 } |
| 114 | 79 |
| 115 void RemoteWindowProxy::setupWindowPrototypeChain() { | 80 void RemoteWindowProxy::setupWindowPrototypeChain() { |
| 116 // Associate the window wrapper object and its prototype chain with the | |
| 117 // corresponding native DOMWindow object. | |
| 118 DOMWindow* window = frame()->domWindow(); | 81 DOMWindow* window = frame()->domWindow(); |
| 119 const WrapperTypeInfo* wrapperTypeInfo = window->wrapperTypeInfo(); | 82 const WrapperTypeInfo* wrapperTypeInfo = window->wrapperTypeInfo(); |
| 120 v8::Local<v8::Context> context = m_scriptState->context(); | |
| 121 | |
| 122 // The global proxy object. Note this is not the global object. | 83 // The global proxy object. Note this is not the global object. |
| 123 v8::Local<v8::Object> globalProxy = context->Global(); | 84 v8::Local<v8::Object> globalProxy = m_globalProxy.newLocal(isolate()); |
| 124 CHECK(m_globalProxy == globalProxy); | |
| 125 V8DOMWrapper::setNativeInfo(isolate(), globalProxy, wrapperTypeInfo, window); | 85 V8DOMWrapper::setNativeInfo(isolate(), globalProxy, wrapperTypeInfo, window); |
| 126 // Mark the handle to be traced by Oilpan, since the global proxy has a | 86 // Mark the handle to be traced by Oilpan, since the global proxy has a |
| 127 // reference to the DOMWindow. | 87 // reference to the DOMWindow. |
| 128 m_globalProxy.get().SetWrapperClassId(wrapperTypeInfo->wrapperClassId); | 88 m_globalProxy.get().SetWrapperClassId(wrapperTypeInfo->wrapperClassId); |
| 129 | 89 |
| 130 #if DCHECK_IS_ON() | 90 #if DCHECK_IS_ON() |
| 131 didAttachGlobalProxy(); | 91 didAttachGlobalProxy(); |
| 132 #endif | 92 #endif |
| 133 | 93 |
| 134 // The global object, aka window wrapper object. | 94 // The global object, aka window wrapper object. |
| 135 v8::Local<v8::Object> windowWrapper = | 95 v8::Local<v8::Object> windowWrapper = |
| 136 globalProxy->GetPrototype().As<v8::Object>(); | 96 globalProxy->GetPrototype().As<v8::Object>(); |
| 137 V8DOMWrapper::setNativeInfo(isolate(), windowWrapper, wrapperTypeInfo, | 97 V8DOMWrapper::setNativeInfo(isolate(), windowWrapper, wrapperTypeInfo, |
| 138 window); | 98 window); |
| 139 | |
| 140 // The prototype object of Window interface. | |
| 141 v8::Local<v8::Object> windowPrototype = | |
| 142 windowWrapper->GetPrototype().As<v8::Object>(); | |
| 143 CHECK(!windowPrototype.IsEmpty()); | |
| 144 V8DOMWrapper::setNativeInfo(isolate(), windowPrototype, wrapperTypeInfo, | |
| 145 window); | |
| 146 | |
| 147 // The named properties object of Window interface. | |
| 148 v8::Local<v8::Object> windowProperties = | |
| 149 windowPrototype->GetPrototype().As<v8::Object>(); | |
| 150 CHECK(!windowProperties.IsEmpty()); | |
| 151 V8DOMWrapper::setNativeInfo(isolate(), windowProperties, wrapperTypeInfo, | |
| 152 window); | |
| 153 } | 99 } |
| 154 | 100 |
| 155 void RemoteWindowProxy::createContext() { | 101 void RemoteWindowProxy::createContext() { |
| 156 // Create a new v8::Context with the window object as the global object | 102 // Create a new v8::Context with the window object as the global object |
| 157 // (aka the inner global). Reuse the outer global proxy if it already exists. | 103 // (aka the inner global). Reuse the outer global proxy if it already exists. |
| 158 v8::Local<v8::ObjectTemplate> globalTemplate = | 104 v8::Local<v8::ObjectTemplate> globalTemplate = |
| 159 V8Window::domTemplate(isolate(), *m_world)->InstanceTemplate(); | 105 V8Window::domTemplate(isolate(), *m_world)->InstanceTemplate(); |
| 160 CHECK(!globalTemplate.IsEmpty()); | 106 CHECK(!globalTemplate.IsEmpty()); |
| 161 | 107 |
| 162 v8::Local<v8::Context> context; | 108 v8::Local<v8::Object> globalProxy = |
| 163 { | 109 v8::Context::NewRemoteContext(isolate(), globalTemplate, |
| 164 V8PerIsolateData::UseCounterDisabledScope useCounterDisabled( | 110 m_globalProxy.newLocal(isolate())) |
| 165 V8PerIsolateData::from(isolate())); | 111 .ToLocalChecked(); |
| 166 context = v8::Context::New(isolate(), nullptr, globalTemplate, | 112 if (m_globalProxy.isEmpty()) |
| 167 m_globalProxy.newLocal(isolate())); | 113 m_globalProxy.set(isolate(), globalProxy); |
| 168 } | 114 else |
| 169 CHECK(!context.IsEmpty()); | 115 DCHECK(m_globalProxy.get() == globalProxy); |
| 170 | 116 CHECK(!m_globalProxy.isEmpty()); |
| 171 m_scriptState = ScriptState::create(context, m_world); | |
| 172 | 117 |
| 173 // TODO(haraken): Currently we cannot enable the following DCHECK because | 118 // TODO(haraken): Currently we cannot enable the following DCHECK because |
| 174 // an already detached window proxy can be re-initialized. This is wrong. | 119 // an already detached window proxy can be re-initialized. This is wrong. |
| 175 // DCHECK(m_lifecycle == Lifecycle::ContextUninitialized); | 120 // DCHECK(m_lifecycle == Lifecycle::ContextUninitialized); |
| 176 m_lifecycle = Lifecycle::ContextInitialized; | 121 m_lifecycle = Lifecycle::ContextInitialized; |
| 177 DCHECK(m_scriptState->contextIsValid()); | |
| 178 } | 122 } |
| 179 | 123 |
| 180 } // namespace blink | 124 } // namespace blink |
| OLD | NEW |