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

Side by Side Diff: third_party/WebKit/Source/core/events/MessageEvent.cpp

Issue 2849373002: Stop storing ScriptValue in MessageEvent (Closed)
Patch Set: Created 3 years, 7 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) 2007 Henry Mason (hmason@mac.com) 2 * Copyright (C) 2007 Henry Mason (hmason@mac.com)
3 * Copyright (C) 2003, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. 3 * Copyright (C) 2003, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. 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 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 22 matching lines...) Expand all
33 #include "bindings/core/v8/V8PrivateProperty.h" 33 #include "bindings/core/v8/V8PrivateProperty.h"
34 #include <memory> 34 #include <memory>
35 35
36 namespace blink { 36 namespace blink {
37 37
38 static inline bool IsValidSource(EventTarget* source) { 38 static inline bool IsValidSource(EventTarget* source) {
39 return !source || source->ToLocalDOMWindow() || source->ToMessagePort() || 39 return !source || source->ToLocalDOMWindow() || source->ToMessagePort() ||
40 source->ToServiceWorker(); 40 source->ToServiceWorker();
41 } 41 }
42 42
43 MessageEvent::MessageEvent() : data_type_(kDataTypeScriptValue) {} 43 MessageEvent::MessageEvent()
44 : data_type_(kDataTypeV8Reference), data_as_v8_value_reference_(this) {}
44 45
45 MessageEvent::MessageEvent(const AtomicString& type, 46 MessageEvent::MessageEvent(const AtomicString& type,
46 const MessageEventInit& initializer) 47 const MessageEventInit& initializer)
47 : Event(type, initializer), 48 : Event(type, initializer),
48 data_type_(kDataTypeScriptValue), 49 data_type_(kDataTypeV8Reference),
50 data_as_v8_value_reference_(this),
49 source_(nullptr) { 51 source_(nullptr) {
50 if (initializer.hasData()) 52 if (initializer.hasData()) {
51 data_as_script_value_ = initializer.data(); 53 data_as_v8_value_reference_.Set(initializer.data().GetIsolate(),
54 initializer.data().V8Value());
55 }
52 if (initializer.hasOrigin()) 56 if (initializer.hasOrigin())
53 origin_ = initializer.origin(); 57 origin_ = initializer.origin();
54 if (initializer.hasLastEventId()) 58 if (initializer.hasLastEventId())
55 last_event_id_ = initializer.lastEventId(); 59 last_event_id_ = initializer.lastEventId();
56 if (initializer.hasSource() && IsValidSource(initializer.source())) 60 if (initializer.hasSource() && IsValidSource(initializer.source()))
57 source_ = initializer.source(); 61 source_ = initializer.source();
58 if (initializer.hasPorts()) 62 if (initializer.hasPorts())
59 ports_ = new MessagePortArray(initializer.ports()); 63 ports_ = new MessagePortArray(initializer.ports());
60 DCHECK(IsValidSource(source_.Get())); 64 DCHECK(IsValidSource(source_.Get()));
61 } 65 }
62 66
63 MessageEvent::MessageEvent(const String& origin, 67 MessageEvent::MessageEvent(const String& origin,
64 const String& last_event_id, 68 const String& last_event_id,
65 EventTarget* source, 69 EventTarget* source,
66 MessagePortArray* ports, 70 MessagePortArray* ports,
67 const String& suborigin) 71 const String& suborigin)
68 : Event(EventTypeNames::message, false, false), 72 : Event(EventTypeNames::message, false, false),
69 data_type_(kDataTypeScriptValue), 73 data_type_(kDataTypeV8Reference),
74 data_as_v8_value_reference_(this),
70 origin_(origin), 75 origin_(origin),
71 last_event_id_(last_event_id), 76 last_event_id_(last_event_id),
72 source_(source), 77 source_(source),
73 ports_(ports) { 78 ports_(ports) {
74 DCHECK(IsValidSource(source_.Get())); 79 DCHECK(IsValidSource(source_.Get()));
75 } 80 }
76 81
77 MessageEvent::MessageEvent(PassRefPtr<SerializedScriptValue> data, 82 MessageEvent::MessageEvent(PassRefPtr<SerializedScriptValue> data,
78 const String& origin, 83 const String& origin,
79 const String& last_event_id, 84 const String& last_event_id,
80 EventTarget* source, 85 EventTarget* source,
81 MessagePortArray* ports, 86 MessagePortArray* ports,
82 const String& suborigin) 87 const String& suborigin)
83 : Event(EventTypeNames::message, false, false), 88 : Event(EventTypeNames::message, false, false),
84 data_type_(kDataTypeSerializedScriptValue), 89 data_type_(kDataTypeSerializedScriptValue),
90 data_as_v8_value_reference_(this),
85 data_as_serialized_script_value_(std::move(data)), 91 data_as_serialized_script_value_(std::move(data)),
86 origin_(origin), 92 origin_(origin),
87 last_event_id_(last_event_id), 93 last_event_id_(last_event_id),
88 source_(source), 94 source_(source),
89 ports_(ports) { 95 ports_(ports) {
90 if (data_as_serialized_script_value_) 96 if (data_as_serialized_script_value_)
91 data_as_serialized_script_value_ 97 data_as_serialized_script_value_
92 ->RegisterMemoryAllocatedWithCurrentScriptContext(); 98 ->RegisterMemoryAllocatedWithCurrentScriptContext();
93 DCHECK(IsValidSource(source_.Get())); 99 DCHECK(IsValidSource(source_.Get()));
94 } 100 }
95 101
96 MessageEvent::MessageEvent(PassRefPtr<SerializedScriptValue> data, 102 MessageEvent::MessageEvent(PassRefPtr<SerializedScriptValue> data,
97 const String& origin, 103 const String& origin,
98 const String& last_event_id, 104 const String& last_event_id,
99 EventTarget* source, 105 EventTarget* source,
100 MessagePortChannelArray channels, 106 MessagePortChannelArray channels,
101 const String& suborigin) 107 const String& suborigin)
102 : Event(EventTypeNames::message, false, false), 108 : Event(EventTypeNames::message, false, false),
103 data_type_(kDataTypeSerializedScriptValue), 109 data_type_(kDataTypeSerializedScriptValue),
110 data_as_v8_value_reference_(this),
104 data_as_serialized_script_value_(std::move(data)), 111 data_as_serialized_script_value_(std::move(data)),
105 origin_(origin), 112 origin_(origin),
106 last_event_id_(last_event_id), 113 last_event_id_(last_event_id),
107 source_(source), 114 source_(source),
108 channels_(std::move(channels)), 115 channels_(std::move(channels)),
109 suborigin_(suborigin) { 116 suborigin_(suborigin) {
110 if (data_as_serialized_script_value_) 117 if (data_as_serialized_script_value_)
111 data_as_serialized_script_value_ 118 data_as_serialized_script_value_
112 ->RegisterMemoryAllocatedWithCurrentScriptContext(); 119 ->RegisterMemoryAllocatedWithCurrentScriptContext();
113 DCHECK(IsValidSource(source_.Get())); 120 DCHECK(IsValidSource(source_.Get()));
114 } 121 }
115 122
116 MessageEvent::MessageEvent(const String& data, 123 MessageEvent::MessageEvent(const String& data,
117 const String& origin, 124 const String& origin,
118 const String& suborigin) 125 const String& suborigin)
119 : Event(EventTypeNames::message, false, false), 126 : Event(EventTypeNames::message, false, false),
120 data_type_(kDataTypeString), 127 data_type_(kDataTypeString),
128 data_as_v8_value_reference_(this),
121 data_as_string_(data), 129 data_as_string_(data),
122 origin_(origin) {} 130 origin_(origin) {}
123 131
124 MessageEvent::MessageEvent(Blob* data, 132 MessageEvent::MessageEvent(Blob* data,
125 const String& origin, 133 const String& origin,
126 const String& suborigin) 134 const String& suborigin)
127 : Event(EventTypeNames::message, false, false), 135 : Event(EventTypeNames::message, false, false),
128 data_type_(kDataTypeBlob), 136 data_type_(kDataTypeBlob),
137 data_as_v8_value_reference_(this),
129 data_as_blob_(data), 138 data_as_blob_(data),
130 origin_(origin) {} 139 origin_(origin) {}
131 140
132 MessageEvent::MessageEvent(DOMArrayBuffer* data, 141 MessageEvent::MessageEvent(DOMArrayBuffer* data,
133 const String& origin, 142 const String& origin,
134 const String& suborigin) 143 const String& suborigin)
135 : Event(EventTypeNames::message, false, false), 144 : Event(EventTypeNames::message, false, false),
136 data_type_(kDataTypeArrayBuffer), 145 data_type_(kDataTypeArrayBuffer),
146 data_as_v8_value_reference_(this),
137 data_as_array_buffer_(data), 147 data_as_array_buffer_(data),
138 origin_(origin) {} 148 origin_(origin) {}
139 149
140 MessageEvent::~MessageEvent() {} 150 MessageEvent::~MessageEvent() {}
141 151
142 MessageEvent* MessageEvent::Create(const AtomicString& type, 152 MessageEvent* MessageEvent::Create(const AtomicString& type,
143 const MessageEventInit& initializer, 153 const MessageEventInit& initializer,
144 ExceptionState& exception_state) { 154 ExceptionState& exception_state) {
145 if (initializer.source() && !IsValidSource(initializer.source())) { 155 if (initializer.source() && !IsValidSource(initializer.source())) {
146 exception_state.ThrowTypeError( 156 exception_state.ThrowTypeError(
147 "The optional 'source' property is neither a Window nor MessagePort."); 157 "The optional 'source' property is neither a Window nor MessagePort.");
148 return nullptr; 158 return nullptr;
149 } 159 }
150 return new MessageEvent(type, initializer); 160 return new MessageEvent(type, initializer);
151 } 161 }
152 162
153 void MessageEvent::initMessageEvent(const AtomicString& type, 163 void MessageEvent::initMessageEvent(const AtomicString& type,
154 bool can_bubble, 164 bool can_bubble,
155 bool cancelable, 165 bool cancelable,
156 ScriptValue data, 166 ScriptValue data,
157 const String& origin, 167 const String& origin,
158 const String& last_event_id, 168 const String& last_event_id,
159 EventTarget* source, 169 EventTarget* source,
160 MessagePortArray* ports) { 170 MessagePortArray* ports) {
161 if (IsBeingDispatched()) 171 if (IsBeingDispatched())
162 return; 172 return;
163 173
164 initEvent(type, can_bubble, cancelable); 174 initEvent(type, can_bubble, cancelable);
165 175
166 data_type_ = kDataTypeScriptValue; 176 data_type_ = kDataTypeV8Reference;
167 data_as_script_value_ = data; 177 if (!data.IsEmpty())
178 data_as_v8_value_reference_.Set(data.GetIsolate(), data.V8Value());
168 origin_ = origin; 179 origin_ = origin;
169 last_event_id_ = last_event_id; 180 last_event_id_ = last_event_id;
170 source_ = source; 181 source_ = source;
171 ports_ = ports; 182 ports_ = ports;
172 suborigin_ = ""; 183 suborigin_ = "";
173 } 184 }
174 185
175 void MessageEvent::initMessageEvent(const AtomicString& type, 186 void MessageEvent::initMessageEvent(const AtomicString& type,
176 bool can_bubble, 187 bool can_bubble,
177 bool cancelable, 188 bool cancelable,
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 last_event_id_ = last_event_id; 228 last_event_id_ = last_event_id;
218 source_ = source; 229 source_ = source;
219 ports_ = ports; 230 ports_ = ports;
220 suborigin_ = ""; 231 suborigin_ = "";
221 } 232 }
222 233
223 const AtomicString& MessageEvent::InterfaceName() const { 234 const AtomicString& MessageEvent::InterfaceName() const {
224 return EventNames::MessageEvent; 235 return EventNames::MessageEvent;
225 } 236 }
226 237
238 ScriptValue MessageEvent::DataAsScriptValue(ScriptState* script_state) const {
239 DCHECK_EQ(data_type_, kDataTypeV8Reference);
240 DCHECK(script_state);
241 return ScriptValue(script_state, data_as_v8_value_reference_.NewLocal(
haraken 2017/05/02 07:24:41 Do you know why we don't need to check the world b
bashi 2017/05/02 07:38:12 In V8MessageEvent::dataAttributeGetterCustom() we
bashi 2017/05/09 08:09:33 Sorry I was wrong. We need to check world here. Ch
242 script_state->GetIsolate()));
243 }
244
227 MessagePortArray MessageEvent::ports(bool& is_null) const { 245 MessagePortArray MessageEvent::ports(bool& is_null) const {
228 // TODO(bashi): Currently we return a copied array because the binding 246 // TODO(bashi): Currently we return a copied array because the binding
229 // layer could modify the content of the array while executing JS callbacks. 247 // layer could modify the content of the array while executing JS callbacks.
230 // Avoid copying once we can make sure that the binding layer won't 248 // Avoid copying once we can make sure that the binding layer won't
231 // modify the content. 249 // modify the content.
232 if (ports_) { 250 if (ports_) {
233 is_null = false; 251 is_null = false;
234 return *ports_; 252 return *ports_;
235 } 253 }
236 is_null = true; 254 is_null = true;
(...skipping 10 matching lines...) Expand all
247 } 265 }
248 266
249 DEFINE_TRACE(MessageEvent) { 267 DEFINE_TRACE(MessageEvent) {
250 visitor->Trace(data_as_blob_); 268 visitor->Trace(data_as_blob_);
251 visitor->Trace(data_as_array_buffer_); 269 visitor->Trace(data_as_array_buffer_);
252 visitor->Trace(source_); 270 visitor->Trace(source_);
253 visitor->Trace(ports_); 271 visitor->Trace(ports_);
254 Event::Trace(visitor); 272 Event::Trace(visitor);
255 } 273 }
256 274
275 DEFINE_TRACE_WRAPPERS(MessageEvent) {
276 visitor->TraceWrappers(data_as_v8_value_reference_);
277 Event::TraceWrappers(visitor);
278 }
279
257 v8::Local<v8::Object> MessageEvent::AssociateWithWrapper( 280 v8::Local<v8::Object> MessageEvent::AssociateWithWrapper(
258 v8::Isolate* isolate, 281 v8::Isolate* isolate,
259 const WrapperTypeInfo* wrapper_type, 282 const WrapperTypeInfo* wrapper_type,
260 v8::Local<v8::Object> wrapper) { 283 v8::Local<v8::Object> wrapper) {
261 wrapper = Event::AssociateWithWrapper(isolate, wrapper_type, wrapper); 284 wrapper = Event::AssociateWithWrapper(isolate, wrapper_type, wrapper);
262 285
263 // Ensures a wrapper is created for the data to return now so that V8 knows 286 // Ensures a wrapper is created for the data to return now so that V8 knows
264 // how much memory is used via the wrapper. To keep the wrapper alive, it's 287 // how much memory is used via the wrapper. To keep the wrapper alive, it's
265 // set to the wrapper of the MessageEvent as a private value. 288 // set to the wrapper of the MessageEvent as a private value.
266 switch (GetDataType()) { 289 switch (GetDataType()) {
267 case MessageEvent::kDataTypeScriptValue: 290 case MessageEvent::kDataTypeV8Reference:
268 case MessageEvent::kDataTypeSerializedScriptValue: 291 case MessageEvent::kDataTypeSerializedScriptValue:
269 break; 292 break;
270 case MessageEvent::kDataTypeString: 293 case MessageEvent::kDataTypeString:
271 V8PrivateProperty::GetMessageEventCachedData(isolate).Set( 294 V8PrivateProperty::GetMessageEventCachedData(isolate).Set(
272 wrapper, V8String(isolate, DataAsString())); 295 wrapper, V8String(isolate, DataAsString()));
273 break; 296 break;
274 case MessageEvent::kDataTypeBlob: 297 case MessageEvent::kDataTypeBlob:
275 break; 298 break;
276 case MessageEvent::kDataTypeArrayBuffer: 299 case MessageEvent::kDataTypeArrayBuffer:
277 V8PrivateProperty::GetMessageEventCachedData(isolate).Set( 300 V8PrivateProperty::GetMessageEventCachedData(isolate).Set(
278 wrapper, ToV8(DataAsArrayBuffer(), wrapper, isolate)); 301 wrapper, ToV8(DataAsArrayBuffer(), wrapper, isolate));
279 break; 302 break;
280 } 303 }
281 304
282 return wrapper; 305 return wrapper;
283 } 306 }
284 307
285 } // namespace blink 308 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698