OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "bindings/modules/v8/V8ExtendableMessageEvent.h" | 5 #include "bindings/modules/v8/V8ExtendableMessageEvent.h" |
6 | 6 |
| 7 #include "bindings/core/v8/V8PrivateProperty.h" |
7 #include "bindings/modules/v8/V8ExtendableMessageEventInit.h" | 8 #include "bindings/modules/v8/V8ExtendableMessageEventInit.h" |
8 #include "bindings/modules/v8/V8ServiceWorkerMessageEventInternal.h" | |
9 | 9 |
10 namespace blink { | 10 namespace blink { |
11 | 11 |
12 void V8ExtendableMessageEvent::constructorCustom( | 12 void V8ExtendableMessageEvent::constructorCustom( |
13 const v8::FunctionCallbackInfo<v8::Value>& info) { | 13 const v8::FunctionCallbackInfo<v8::Value>& info) { |
14 V8ServiceWorkerMessageEventInternal::ConstructorCustom< | 14 v8::Isolate* isolate = info.GetIsolate(); |
15 ExtendableMessageEvent, ExtendableMessageEventInit>(info); | 15 ExceptionState exception_state(isolate, ExceptionState::kConstructionContext, |
| 16 "ExtendableMessageEvent"); |
| 17 if (UNLIKELY(info.Length() < 1)) { |
| 18 exception_state.ThrowTypeError( |
| 19 ExceptionMessages::NotEnoughArguments(1, info.Length())); |
| 20 return; |
| 21 } |
| 22 |
| 23 V8StringResource<> type = info[0]; |
| 24 if (!type.Prepare()) |
| 25 return; |
| 26 |
| 27 ExtendableMessageEventInit event_init_dict; |
| 28 if (!IsUndefinedOrNull(info[1])) { |
| 29 if (!info[1]->IsObject()) { |
| 30 exception_state.ThrowTypeError( |
| 31 "parameter 2 ('eventInitDict') is not an object."); |
| 32 return; |
| 33 } |
| 34 V8ExtendableMessageEventInit::toImpl(isolate, info[1], event_init_dict, |
| 35 exception_state); |
| 36 if (exception_state.HadException()) |
| 37 return; |
| 38 } |
| 39 |
| 40 ExtendableMessageEvent* impl = |
| 41 ExtendableMessageEvent::Create(type, event_init_dict); |
| 42 v8::Local<v8::Object> wrapper = info.Holder(); |
| 43 wrapper = impl->AssociateWithWrapper( |
| 44 isolate, &V8ExtendableMessageEvent::wrapperTypeInfo, wrapper); |
| 45 |
| 46 // TODO(bashi): Workaround for http://crbug.com/529941. We need to store |
| 47 // |data| as a private value to avoid cyclic references. |
| 48 if (event_init_dict.hasData()) { |
| 49 v8::Local<v8::Value> v8_data = event_init_dict.data().V8Value(); |
| 50 V8PrivateProperty::GetMessageEventCachedData(isolate).Set(wrapper, v8_data); |
| 51 if (DOMWrapperWorld::Current(isolate).IsIsolatedWorld()) { |
| 52 impl->SetSerializedData( |
| 53 SerializedScriptValue::SerializeAndSwallowExceptions(isolate, |
| 54 v8_data)); |
| 55 } |
| 56 } |
| 57 V8SetReturnValue(info, wrapper); |
16 } | 58 } |
17 | 59 |
18 void V8ExtendableMessageEvent::dataAttributeGetterCustom( | 60 void V8ExtendableMessageEvent::dataAttributeGetterCustom( |
19 const v8::FunctionCallbackInfo<v8::Value>& info) { | 61 const v8::FunctionCallbackInfo<v8::Value>& info) { |
20 V8ServiceWorkerMessageEventInternal::DataAttributeGetterCustom< | 62 ExtendableMessageEvent* event = |
21 ExtendableMessageEvent>(info); | 63 V8ExtendableMessageEvent::toImpl(info.Holder()); |
| 64 v8::Isolate* isolate = info.GetIsolate(); |
| 65 auto private_cached_data = |
| 66 V8PrivateProperty::GetMessageEventCachedData(isolate); |
| 67 v8::Local<v8::Value> result = private_cached_data.GetOrEmpty(info.Holder()); |
| 68 if (!result.IsEmpty()) { |
| 69 V8SetReturnValue(info, result); |
| 70 return; |
| 71 } |
| 72 |
| 73 v8::Local<v8::Value> data; |
| 74 if (SerializedScriptValue* serialized_value = event->SerializedData()) { |
| 75 MessagePortArray ports = event->ports(); |
| 76 SerializedScriptValue::DeserializeOptions options; |
| 77 options.message_ports = &ports; |
| 78 data = serialized_value->Deserialize(isolate, options); |
| 79 } else if (DOMWrapperWorld::Current(isolate).IsIsolatedWorld()) { |
| 80 v8::Local<v8::Value> main_world_data = |
| 81 private_cached_data.GetFromMainWorld(event); |
| 82 if (!main_world_data.IsEmpty()) { |
| 83 // TODO(bashi): Enter the main world's ScriptState::Scope while |
| 84 // serializing the main world's value. |
| 85 event->SetSerializedData( |
| 86 SerializedScriptValue::SerializeAndSwallowExceptions( |
| 87 info.GetIsolate(), main_world_data)); |
| 88 data = event->SerializedData()->Deserialize(isolate); |
| 89 } |
| 90 } |
| 91 if (data.IsEmpty()) |
| 92 data = v8::Null(isolate); |
| 93 private_cached_data.Set(info.Holder(), data); |
| 94 V8SetReturnValue(info, data); |
22 } | 95 } |
23 | 96 |
24 } // namespace blink | 97 } // namespace blink |
OLD | NEW |