Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(194)

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/RemoteWindowProxy.cpp

Issue 2760793002: Use v8::Context::NewRemoteContext in RemoteWindowProxy. (Closed)
Patch Set: RemoteDOMWindow in DOMDataStore Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 <algorithm> 33 #include <algorithm>
34 #include <utility> 34 #include <utility>
35 35
36 #include "bindings/core/v8/DOMDataStore.h"
36 #include "bindings/core/v8/DOMWrapperWorld.h" 37 #include "bindings/core/v8/DOMWrapperWorld.h"
37 #include "bindings/core/v8/V8DOMWrapper.h" 38 #include "bindings/core/v8/V8DOMWrapper.h"
38 #include "bindings/core/v8/V8GCForContextDispose.h"
39 #include "bindings/core/v8/V8Initializer.h"
40 #include "bindings/core/v8/V8Window.h" 39 #include "bindings/core/v8/V8Window.h"
40 #include "core/frame/RemoteDOMWindow.h"
41 #include "platform/Histogram.h" 41 #include "platform/Histogram.h"
42 #include "platform/ScriptForbiddenScope.h" 42 #include "platform/ScriptForbiddenScope.h"
43 #include "platform/heap/Handle.h"
44 #include "platform/instrumentation/tracing/TraceEvent.h" 43 #include "platform/instrumentation/tracing/TraceEvent.h"
45 #include "v8/include/v8.h" 44 #include "v8/include/v8.h"
46 #include "wtf/Assertions.h" 45 #include "wtf/Assertions.h"
47 46
48 namespace blink { 47 namespace blink {
49 48
50 RemoteWindowProxy::RemoteWindowProxy(v8::Isolate* isolate, 49 RemoteWindowProxy::RemoteWindowProxy(v8::Isolate* isolate,
51 RemoteFrame& frame, 50 RemoteFrame& frame,
52 RefPtr<DOMWrapperWorld> world) 51 RefPtr<DOMWrapperWorld> world)
53 : WindowProxy(isolate, frame, std::move(world)) {} 52 : WindowProxy(isolate, frame, std::move(world)) {}
54 53
55 void RemoteWindowProxy::disposeContext(GlobalDetachmentBehavior behavior) { 54 void RemoteWindowProxy::disposeContext(GlobalDetachmentBehavior behavior) {
56 if (m_lifecycle != Lifecycle::ContextInitialized) 55 if (m_lifecycle != Lifecycle::ContextInitialized)
57 return; 56 return;
58 57
59 if (behavior == DetachGlobal) { 58 if (behavior == DetachGlobal && !m_globalProxy.isEmpty()) {
60 v8::Local<v8::Context> context = m_scriptState->context(); 59 m_globalProxy.get().SetWrapperClassId(0);
61 // Clean up state on the global proxy, which will be reused. 60 V8DOMWrapper::clearNativeInfo(isolate(), m_globalProxy.newLocal(isolate()));
62 if (!m_globalProxy.isEmpty()) {
63 CHECK(m_globalProxy == context->Global());
64 CHECK_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() 61 #if DCHECK_IS_ON()
73 didDetachGlobalObject(); 62 didDetachGlobalObject();
74 #endif 63 #endif
75 } 64 }
76 65
77 m_scriptState->disposePerContextData();
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); 66 DCHECK(m_lifecycle == Lifecycle::ContextInitialized);
86 m_lifecycle = Lifecycle::ContextDetached; 67 m_lifecycle = Lifecycle::ContextDetached;
87 } 68 }
88 69
89 void RemoteWindowProxy::initialize() { 70 void RemoteWindowProxy::initialize() {
90 TRACE_EVENT1("v8", "RemoteWindowProxy::initialize", "isMainWindow", 71 TRACE_EVENT1("v8", "RemoteWindowProxy::initialize", "isMainWindow",
91 frame()->isMainFrame()); 72 frame()->isMainFrame());
92 SCOPED_BLINK_UMA_HISTOGRAM_TIMER( 73 SCOPED_BLINK_UMA_HISTOGRAM_TIMER(
93 frame()->isMainFrame() ? "Blink.Binding.InitializeMainWindowProxy" 74 frame()->isMainFrame() ? "Blink.Binding.InitializeMainWindowProxy"
94 : "Blink.Binding.InitializeNonMainWindowProxy"); 75 : "Blink.Binding.InitializeNonMainWindowProxy");
95 76
96 ScriptForbiddenScope::AllowUserAgentScript allowScript; 77 ScriptForbiddenScope::AllowUserAgentScript allowScript;
97 78
98 v8::HandleScope handleScope(isolate()); 79 v8::HandleScope handleScope(isolate());
99
100 createContext(); 80 createContext();
101
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(); 81 setupWindowPrototypeChain();
110
111 // Remote frames always require a full canAccess() check.
112 context->UseDefaultSecurityToken();
113 } 82 }
114 83
115 void RemoteWindowProxy::createContext() { 84 void RemoteWindowProxy::createContext() {
116 // Create a new v8::Context with the window object as the global object 85 // Create a new v8::Context with the window object as the global object
117 // (aka the inner global). Reuse the outer global proxy if it already exists. 86 // (aka the inner global). Reuse the outer global proxy if it already exists.
118 v8::Local<v8::ObjectTemplate> globalTemplate = 87 v8::Local<v8::ObjectTemplate> globalTemplate =
119 V8Window::domTemplate(isolate(), *m_world)->InstanceTemplate(); 88 V8Window::domTemplate(isolate(), *m_world)->InstanceTemplate();
120 CHECK(!globalTemplate.IsEmpty()); 89 CHECK(!globalTemplate.IsEmpty());
121 90
122 v8::Local<v8::Context> context; 91 v8::Local<v8::Object> globalProxy =
123 { 92 v8::Context::NewRemoteContext(isolate(), globalTemplate,
124 V8PerIsolateData::UseCounterDisabledScope useCounterDisabled( 93 m_globalProxy.newLocal(isolate()))
125 V8PerIsolateData::from(isolate())); 94 .ToLocalChecked();
126 context = v8::Context::New(isolate(), nullptr, globalTemplate, 95 if (m_globalProxy.isEmpty())
127 m_globalProxy.newLocal(isolate())); 96 m_globalProxy.set(isolate(), globalProxy);
128 } 97 else
129 CHECK(!context.IsEmpty()); 98 DCHECK(m_globalProxy.get() == globalProxy);
99 CHECK(!m_globalProxy.isEmpty());
130 100
131 #if DCHECK_IS_ON() 101 #if DCHECK_IS_ON()
132 didAttachGlobalObject(); 102 didAttachGlobalObject();
133 #endif 103 #endif
134 104
135 m_scriptState = ScriptState::create(context, m_world);
136
137 // TODO(haraken): Currently we cannot enable the following DCHECK because 105 // TODO(haraken): Currently we cannot enable the following DCHECK because
138 // an already detached window proxy can be re-initialized. This is wrong. 106 // an already detached window proxy can be re-initialized. This is wrong.
139 // DCHECK(m_lifecycle == Lifecycle::ContextUninitialized); 107 // DCHECK(m_lifecycle == Lifecycle::ContextUninitialized);
140 m_lifecycle = Lifecycle::ContextInitialized; 108 m_lifecycle = Lifecycle::ContextInitialized;
141 DCHECK(m_scriptState->contextIsValid());
142 } 109 }
143 110
144 void RemoteWindowProxy::setupWindowPrototypeChain() { 111 void RemoteWindowProxy::setupWindowPrototypeChain() {
145 // Associate the window wrapper object and its prototype chain with the 112 // Associate the window wrapper object and its prototype chain with the
146 // corresponding native DOMWindow object. 113 // corresponding native DOMWindow object.
147 DOMWindow* window = frame()->domWindow(); 114 RemoteDOMWindow* window = frame()->domWindow();
148 const WrapperTypeInfo* wrapperTypeInfo = window->wrapperTypeInfo(); 115 const WrapperTypeInfo* wrapperTypeInfo = window->wrapperTypeInfo();
149 v8::Local<v8::Context> context = m_scriptState->context();
150 116
151 // The global proxy object. Note this is not the global object. 117 // The global proxy object. Note this is not the global object.
152 v8::Local<v8::Object> globalProxy = context->Global(); 118 v8::Local<v8::Object> globalProxy = m_globalProxy.newLocal(isolate());
153 CHECK(m_globalProxy == globalProxy); 119 CHECK(m_globalProxy == globalProxy);
154 V8DOMWrapper::setNativeInfo(isolate(), globalProxy, wrapperTypeInfo, window); 120 V8DOMWrapper::setNativeInfo(isolate(), globalProxy, wrapperTypeInfo, window);
155 // Mark the handle to be traced by Oilpan, since the global proxy has a 121 // Mark the handle to be traced by Oilpan, since the global proxy has a
156 // reference to the DOMWindow. 122 // reference to the DOMWindow.
157 m_globalProxy.get().SetWrapperClassId(wrapperTypeInfo->wrapperClassId); 123 m_globalProxy.get().SetWrapperClassId(wrapperTypeInfo->wrapperClassId);
158 124
159 // The global object, aka window wrapper object. 125 // The global object, aka window wrapper object.
160 v8::Local<v8::Object> windowWrapper = 126 v8::Local<v8::Object> windowWrapper =
161 globalProxy->GetPrototype().As<v8::Object>(); 127 globalProxy->GetPrototype().As<v8::Object>();
162 windowWrapper = V8DOMWrapper::associateObjectWithWrapper( 128 windowWrapper = associateWithWrapper(window, wrapperTypeInfo, windowWrapper);
dcheng 2017/03/23 01:47:09 I'm kind of uncertain what the best thing to do he
Yuki 2017/03/23 02:58:29 Yes, I think it's good. I did the same thing in m
dcheng 2017/03/23 06:03:06 Done.
163 isolate(), window, wrapperTypeInfo, windowWrapper); 129 }
164 130
165 // The prototype object of Window interface. 131 v8::Local<v8::Object> RemoteWindowProxy::associateWithWrapper(
dcheng 2017/03/23 01:47:09 An alternative is to make this a general function
Yuki 2017/03/23 02:58:29 Sounds good to me.
166 v8::Local<v8::Object> windowPrototype = 132 RemoteDOMWindow* window,
167 windowWrapper->GetPrototype().As<v8::Object>(); 133 const WrapperTypeInfo* wrapperTypeInfo,
168 CHECK(!windowPrototype.IsEmpty()); 134 v8::Local<v8::Object> wrapper) {
169 V8DOMWrapper::setNativeInfo(isolate(), windowPrototype, wrapperTypeInfo, 135 if (m_world->domDataStore().set(isolate(), window, wrapperTypeInfo,
170 window); 136 wrapper)) {
171 137 wrapperTypeInfo->wrapperCreated();
172 // The named properties object of Window interface. 138 V8DOMWrapper::setNativeInfo(isolate(), wrapper, wrapperTypeInfo, window);
173 v8::Local<v8::Object> windowProperties = 139 DCHECK(V8DOMWrapper::hasInternalFieldsSet(wrapper));
174 windowPrototype->GetPrototype().As<v8::Object>(); 140 }
175 CHECK(!windowProperties.IsEmpty()); 141 SECURITY_CHECK(toScriptWrappable(wrapper) == window);
176 V8DOMWrapper::setNativeInfo(isolate(), windowProperties, wrapperTypeInfo, 142 return wrapper;
177 window);
178 } 143 }
179 144
180 } // namespace blink 145 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698