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

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

Issue 2760793002: Use v8::Context::NewRemoteContext in RemoteWindowProxy. (Closed)
Patch Set: associateObjectWithWrapper -> setNativeInfo 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
« no previous file with comments | « third_party/WebKit/Source/bindings/core/v8/RemoteWindowProxy.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 17 matching lines...) Expand all
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/DOMWrapperWorld.h" 36 #include "bindings/core/v8/DOMWrapperWorld.h"
37 #include "bindings/core/v8/V8DOMWrapper.h" 37 #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" 38 #include "bindings/core/v8/V8Window.h"
41 #include "platform/Histogram.h" 39 #include "platform/Histogram.h"
42 #include "platform/ScriptForbiddenScope.h" 40 #include "platform/ScriptForbiddenScope.h"
43 #include "platform/heap/Handle.h"
44 #include "platform/instrumentation/tracing/TraceEvent.h" 41 #include "platform/instrumentation/tracing/TraceEvent.h"
45 #include "v8/include/v8.h" 42 #include "v8/include/v8.h"
46 #include "wtf/Assertions.h" 43 #include "wtf/Assertions.h"
47 44
48 namespace blink { 45 namespace blink {
49 46
50 RemoteWindowProxy::RemoteWindowProxy(v8::Isolate* isolate, 47 RemoteWindowProxy::RemoteWindowProxy(v8::Isolate* isolate,
51 RemoteFrame& frame, 48 RemoteFrame& frame,
52 RefPtr<DOMWrapperWorld> world) 49 RefPtr<DOMWrapperWorld> world)
53 : WindowProxy(isolate, frame, std::move(world)) {} 50 : WindowProxy(isolate, frame, std::move(world)) {}
54 51
55 void RemoteWindowProxy::disposeContext(GlobalDetachmentBehavior behavior) { 52 void RemoteWindowProxy::disposeContext(GlobalDetachmentBehavior behavior) {
56 if (m_lifecycle != Lifecycle::ContextInitialized) 53 if (m_lifecycle != Lifecycle::ContextInitialized)
57 return; 54 return;
58 55
59 if (behavior == DetachGlobal) { 56 if (behavior == DetachGlobal && !m_globalProxy.isEmpty()) {
60 v8::Local<v8::Context> context = m_scriptState->context(); 57 m_globalProxy.get().SetWrapperClassId(0);
61 // Clean up state on the global proxy, which will be reused. 58 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() 59 #if DCHECK_IS_ON()
73 didDetachGlobalObject(); 60 didDetachGlobalObject();
74 #endif 61 #endif
75 } 62 }
76 63
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); 64 DCHECK(m_lifecycle == Lifecycle::ContextInitialized);
86 m_lifecycle = Lifecycle::ContextDetached; 65 m_lifecycle = Lifecycle::ContextDetached;
87 } 66 }
88 67
89 void RemoteWindowProxy::initialize() { 68 void RemoteWindowProxy::initialize() {
90 TRACE_EVENT1("v8", "RemoteWindowProxy::initialize", "isMainWindow", 69 TRACE_EVENT1("v8", "RemoteWindowProxy::initialize", "isMainWindow",
91 frame()->isMainFrame()); 70 frame()->isMainFrame());
92 SCOPED_BLINK_UMA_HISTOGRAM_TIMER( 71 SCOPED_BLINK_UMA_HISTOGRAM_TIMER(
93 frame()->isMainFrame() ? "Blink.Binding.InitializeMainWindowProxy" 72 frame()->isMainFrame() ? "Blink.Binding.InitializeMainWindowProxy"
94 : "Blink.Binding.InitializeNonMainWindowProxy"); 73 : "Blink.Binding.InitializeNonMainWindowProxy");
95 74
96 ScriptForbiddenScope::AllowUserAgentScript allowScript; 75 ScriptForbiddenScope::AllowUserAgentScript allowScript;
97 76
98 v8::HandleScope handleScope(isolate()); 77 v8::HandleScope handleScope(isolate());
99
100 createContext(); 78 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(); 79 setupWindowPrototypeChain();
110
111 // Remote frames always require a full canAccess() check.
112 context->UseDefaultSecurityToken();
113 } 80 }
114 81
115 void RemoteWindowProxy::createContext() { 82 void RemoteWindowProxy::createContext() {
116 // Create a new v8::Context with the window object as the global object 83 // 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. 84 // (aka the inner global). Reuse the outer global proxy if it already exists.
118 v8::Local<v8::ObjectTemplate> globalTemplate = 85 v8::Local<v8::ObjectTemplate> globalTemplate =
119 V8Window::domTemplate(isolate(), *m_world)->InstanceTemplate(); 86 V8Window::domTemplate(isolate(), *m_world)->InstanceTemplate();
120 CHECK(!globalTemplate.IsEmpty()); 87 CHECK(!globalTemplate.IsEmpty());
121 88
122 v8::Local<v8::Context> context; 89 v8::Local<v8::Object> globalProxy =
123 { 90 v8::Context::NewRemoteContext(isolate(), globalTemplate,
124 V8PerIsolateData::UseCounterDisabledScope useCounterDisabled( 91 m_globalProxy.newLocal(isolate()))
125 V8PerIsolateData::from(isolate())); 92 .ToLocalChecked();
126 context = v8::Context::New(isolate(), nullptr, globalTemplate, 93 if (m_globalProxy.isEmpty())
127 m_globalProxy.newLocal(isolate())); 94 m_globalProxy.set(isolate(), globalProxy);
128 } 95 else
129 CHECK(!context.IsEmpty()); 96 DCHECK(m_globalProxy.get() == globalProxy);
97 CHECK(!m_globalProxy.isEmpty());
130 98
131 #if DCHECK_IS_ON() 99 #if DCHECK_IS_ON()
132 didAttachGlobalObject(); 100 didAttachGlobalObject();
133 #endif 101 #endif
134 102
135 m_scriptState = ScriptState::create(context, m_world);
136
137 // TODO(haraken): Currently we cannot enable the following DCHECK because 103 // TODO(haraken): Currently we cannot enable the following DCHECK because
138 // an already detached window proxy can be re-initialized. This is wrong. 104 // an already detached window proxy can be re-initialized. This is wrong.
139 // DCHECK(m_lifecycle == Lifecycle::ContextUninitialized); 105 // DCHECK(m_lifecycle == Lifecycle::ContextUninitialized);
140 m_lifecycle = Lifecycle::ContextInitialized; 106 m_lifecycle = Lifecycle::ContextInitialized;
141 DCHECK(m_scriptState->contextIsValid());
142 } 107 }
143 108
144 void RemoteWindowProxy::setupWindowPrototypeChain() { 109 void RemoteWindowProxy::setupWindowPrototypeChain() {
145 // Associate the window wrapper object and its prototype chain with the 110 // Associate the window wrapper object and its prototype chain with the
146 // corresponding native DOMWindow object. 111 // corresponding native DOMWindow object.
147 DOMWindow* window = frame()->domWindow(); 112 DOMWindow* window = frame()->domWindow();
148 const WrapperTypeInfo* wrapperTypeInfo = window->wrapperTypeInfo(); 113 const WrapperTypeInfo* wrapperTypeInfo = window->wrapperTypeInfo();
149 v8::Local<v8::Context> context = m_scriptState->context();
150 114
151 // The global proxy object. Note this is not the global object. 115 // The global proxy object. Note this is not the global object.
152 v8::Local<v8::Object> globalProxy = context->Global(); 116 v8::Local<v8::Object> globalProxy = m_globalProxy.newLocal(isolate());
153 CHECK(m_globalProxy == globalProxy); 117 CHECK(m_globalProxy == globalProxy);
154 V8DOMWrapper::setNativeInfo(isolate(), globalProxy, wrapperTypeInfo, window); 118 V8DOMWrapper::setNativeInfo(isolate(), globalProxy, wrapperTypeInfo, window);
155 // Mark the handle to be traced by Oilpan, since the global proxy has a 119 // Mark the handle to be traced by Oilpan, since the global proxy has a
156 // reference to the DOMWindow. 120 // reference to the DOMWindow.
157 m_globalProxy.get().SetWrapperClassId(wrapperTypeInfo->wrapperClassId); 121 m_globalProxy.get().SetWrapperClassId(wrapperTypeInfo->wrapperClassId);
158 122
159 // The global object, aka window wrapper object. 123 // The global object, aka window wrapper object.
160 v8::Local<v8::Object> windowWrapper = 124 v8::Local<v8::Object> windowWrapper =
161 globalProxy->GetPrototype().As<v8::Object>(); 125 globalProxy->GetPrototype().As<v8::Object>();
162 windowWrapper = V8DOMWrapper::associateObjectWithWrapper( 126 V8DOMWrapper::setNativeInfo(isolate(), windowWrapper, wrapperTypeInfo,
dcheng 2017/03/20 06:51:30 PTAL... I had to change this to setNativeInf inste
haraken 2017/03/20 07:36:49 After the navigation, the C++ window object will c
dcheng 2017/03/20 08:06:53 Ah... I see. Why is it important to store the glob
163 isolate(), window, wrapperTypeInfo, windowWrapper);
164
165 // The prototype object of Window interface.
166 v8::Local<v8::Object> windowPrototype =
167 windowWrapper->GetPrototype().As<v8::Object>();
168 CHECK(!windowPrototype.IsEmpty());
169 V8DOMWrapper::setNativeInfo(isolate(), windowPrototype, wrapperTypeInfo,
170 window);
171
172 // The named properties object of Window interface.
173 v8::Local<v8::Object> windowProperties =
174 windowPrototype->GetPrototype().As<v8::Object>();
175 CHECK(!windowProperties.IsEmpty());
176 V8DOMWrapper::setNativeInfo(isolate(), windowProperties, wrapperTypeInfo,
177 window); 127 window);
178 } 128 }
179 129
180 } // namespace blink 130 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/bindings/core/v8/RemoteWindowProxy.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698