OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef V8ServiceWorkerMessageEventInternal_h | |
6 #define V8ServiceWorkerMessageEventInternal_h | |
7 | |
8 #include "bindings/core/v8/SerializedScriptValue.h" | |
9 #include "bindings/core/v8/SerializedScriptValueFactory.h" | |
10 #include "bindings/core/v8/V8PrivateProperty.h" | |
11 | |
12 namespace blink { | |
13 | |
14 class V8ServiceWorkerMessageEventInternal { | |
15 public: | |
16 template <typename EventType, typename DictType> | |
17 static void ConstructorCustom(const v8::FunctionCallbackInfo<v8::Value>&); | |
18 | |
19 template <typename EventType> | |
20 static void DataAttributeGetterCustom( | |
21 const v8::FunctionCallbackInfo<v8::Value>&); | |
22 }; | |
23 | |
24 template <typename EventType, typename DictType> | |
25 void V8ServiceWorkerMessageEventInternal::ConstructorCustom( | |
26 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
27 v8::Isolate* isolate = info.GetIsolate(); | |
28 ExceptionState exception_state( | |
29 isolate, ExceptionState::kConstructionContext, | |
30 V8TypeOf<EventType>::Type::wrapperTypeInfo.interface_name); | |
31 if (UNLIKELY(info.Length() < 1)) { | |
32 exception_state.ThrowTypeError( | |
33 ExceptionMessages::NotEnoughArguments(1, info.Length())); | |
34 return; | |
35 } | |
36 | |
37 V8StringResource<> type = info[0]; | |
38 if (!type.Prepare()) | |
39 return; | |
40 | |
41 DictType event_init_dict; | |
42 if (!IsUndefinedOrNull(info[1])) { | |
43 if (!info[1]->IsObject()) { | |
44 exception_state.ThrowTypeError( | |
45 "parameter 2 ('eventInitDict') is not an object."); | |
46 return; | |
47 } | |
48 V8TypeOf<DictType>::Type::toImpl(isolate, info[1], event_init_dict, | |
49 exception_state); | |
50 if (exception_state.HadException()) | |
51 return; | |
52 } | |
53 | |
54 EventType* impl = EventType::Create(type, event_init_dict); | |
55 v8::Local<v8::Object> wrapper = info.Holder(); | |
56 wrapper = impl->AssociateWithWrapper( | |
57 isolate, &V8TypeOf<EventType>::Type::wrapperTypeInfo, wrapper); | |
58 | |
59 // TODO(bashi): Workaround for http://crbug.com/529941. We need to store | |
60 // |data| as a private value to avoid cyclic references. | |
61 if (event_init_dict.hasData()) { | |
62 v8::Local<v8::Value> v8_data = event_init_dict.data().V8Value(); | |
63 V8PrivateProperty::GetMessageEventCachedData(isolate).Set(wrapper, v8_data); | |
64 if (DOMWrapperWorld::Current(isolate).IsIsolatedWorld()) { | |
65 impl->SetSerializedData( | |
66 SerializedScriptValue::SerializeAndSwallowExceptions(isolate, | |
67 v8_data)); | |
68 } | |
69 } | |
70 V8SetReturnValue(info, wrapper); | |
71 } | |
72 | |
73 template <typename EventType> | |
74 void V8ServiceWorkerMessageEventInternal::DataAttributeGetterCustom( | |
75 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
76 EventType* event = V8TypeOf<EventType>::Type::toImpl(info.Holder()); | |
77 v8::Isolate* isolate = info.GetIsolate(); | |
78 auto private_cached_data = | |
79 V8PrivateProperty::GetMessageEventCachedData(isolate); | |
80 v8::Local<v8::Value> result = private_cached_data.GetOrEmpty(info.Holder()); | |
81 if (!result.IsEmpty()) { | |
82 V8SetReturnValue(info, result); | |
83 return; | |
84 } | |
85 | |
86 v8::Local<v8::Value> data; | |
87 if (SerializedScriptValue* serialized_value = event->SerializedData()) { | |
88 MessagePortArray ports = event->ports(); | |
89 SerializedScriptValue::DeserializeOptions options; | |
90 options.message_ports = &ports; | |
91 data = serialized_value->Deserialize(isolate, options); | |
92 } else if (DOMWrapperWorld::Current(isolate).IsIsolatedWorld()) { | |
93 v8::Local<v8::Value> main_world_data = | |
94 private_cached_data.GetFromMainWorld(event); | |
95 if (!main_world_data.IsEmpty()) { | |
96 // TODO(bashi): Enter the main world's ScriptState::Scope while | |
97 // serializing the main world's value. | |
98 event->SetSerializedData( | |
99 SerializedScriptValue::SerializeAndSwallowExceptions( | |
100 info.GetIsolate(), main_world_data)); | |
101 data = event->SerializedData()->Deserialize(isolate); | |
102 } | |
103 } | |
104 if (data.IsEmpty()) | |
105 data = v8::Null(isolate); | |
106 private_cached_data.Set(info.Holder(), data); | |
107 V8SetReturnValue(info, data); | |
108 } | |
109 | |
110 } // namespace blink | |
111 | |
112 #endif // V8ServiceWorkerMessageEventInternal_h | |
OLD | NEW |