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 #ifndef V8ServiceWorkerMessageEventInternal_h | 5 #ifndef V8ServiceWorkerMessageEventInternal_h |
6 #define V8ServiceWorkerMessageEventInternal_h | 6 #define V8ServiceWorkerMessageEventInternal_h |
7 | 7 |
8 #include "bindings/core/v8/SerializedScriptValue.h" | 8 #include "bindings/core/v8/SerializedScriptValue.h" |
9 #include "bindings/core/v8/SerializedScriptValueFactory.h" | 9 #include "bindings/core/v8/SerializedScriptValueFactory.h" |
10 #include "bindings/core/v8/V8PrivateProperty.h" | 10 #include "bindings/core/v8/V8PrivateProperty.h" |
11 | 11 |
12 namespace blink { | 12 namespace blink { |
13 | 13 |
14 class V8ServiceWorkerMessageEventInternal { | 14 class V8ServiceWorkerMessageEventInternal { |
15 public: | 15 public: |
16 template <typename EventType, typename DictType> | 16 template <typename EventType, typename DictType> |
17 static void constructorCustom(const v8::FunctionCallbackInfo<v8::Value>&); | 17 static void constructorCustom(const v8::FunctionCallbackInfo<v8::Value>&); |
18 | 18 |
19 template <typename EventType> | 19 template <typename EventType> |
20 static void dataAttributeGetterCustom( | 20 static void dataAttributeGetterCustom( |
21 const v8::FunctionCallbackInfo<v8::Value>&); | 21 const v8::FunctionCallbackInfo<v8::Value>&); |
22 }; | 22 }; |
23 | 23 |
24 template <typename EventType, typename DictType> | 24 template <typename EventType, typename DictType> |
25 void V8ServiceWorkerMessageEventInternal::constructorCustom( | 25 void V8ServiceWorkerMessageEventInternal::constructorCustom( |
26 const v8::FunctionCallbackInfo<v8::Value>& info) { | 26 const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 27 v8::Isolate* isolate = info.GetIsolate(); |
27 ExceptionState exceptionState( | 28 ExceptionState exceptionState( |
28 info.GetIsolate(), ExceptionState::ConstructionContext, | 29 isolate, ExceptionState::ConstructionContext, |
29 V8TypeOf<EventType>::Type::wrapperTypeInfo.interfaceName); | 30 V8TypeOf<EventType>::Type::wrapperTypeInfo.interfaceName); |
30 if (UNLIKELY(info.Length() < 1)) { | 31 if (UNLIKELY(info.Length() < 1)) { |
31 exceptionState.throwTypeError( | 32 exceptionState.throwTypeError( |
32 ExceptionMessages::notEnoughArguments(1, info.Length())); | 33 ExceptionMessages::notEnoughArguments(1, info.Length())); |
33 return; | 34 return; |
34 } | 35 } |
35 | 36 |
36 V8StringResource<> type = info[0]; | 37 V8StringResource<> type = info[0]; |
37 if (!type.prepare()) | 38 if (!type.prepare()) |
38 return; | 39 return; |
39 | 40 |
40 DictType eventInitDict; | 41 DictType eventInitDict; |
41 if (!isUndefinedOrNull(info[1])) { | 42 if (!isUndefinedOrNull(info[1])) { |
42 if (!info[1]->IsObject()) { | 43 if (!info[1]->IsObject()) { |
43 exceptionState.throwTypeError( | 44 exceptionState.throwTypeError( |
44 "parameter 2 ('eventInitDict') is not an object."); | 45 "parameter 2 ('eventInitDict') is not an object."); |
45 return; | 46 return; |
46 } | 47 } |
47 V8TypeOf<DictType>::Type::toImpl(info.GetIsolate(), info[1], eventInitDict, | 48 V8TypeOf<DictType>::Type::toImpl(isolate, info[1], eventInitDict, |
48 exceptionState); | 49 exceptionState); |
49 if (exceptionState.hadException()) | 50 if (exceptionState.hadException()) |
50 return; | 51 return; |
51 } | 52 } |
52 | 53 |
53 EventType* impl = EventType::create(type, eventInitDict); | 54 EventType* impl = EventType::create(type, eventInitDict); |
54 v8::Local<v8::Object> wrapper = info.Holder(); | 55 v8::Local<v8::Object> wrapper = info.Holder(); |
55 wrapper = impl->associateWithWrapper( | 56 wrapper = impl->associateWithWrapper( |
56 info.GetIsolate(), &V8TypeOf<EventType>::Type::wrapperTypeInfo, wrapper); | 57 isolate, &V8TypeOf<EventType>::Type::wrapperTypeInfo, wrapper); |
57 | 58 |
58 // TODO(bashi): Workaround for http://crbug.com/529941. We need to store | 59 // TODO(bashi): Workaround for http://crbug.com/529941. We need to store |
59 // |data| as a private value to avoid cyclic references. | 60 // |data| as a private value to avoid cyclic references. |
60 if (eventInitDict.hasData()) { | 61 if (eventInitDict.hasData()) { |
61 v8::Local<v8::Value> v8Data = eventInitDict.data().v8Value(); | 62 v8::Local<v8::Value> v8Data = eventInitDict.data().v8Value(); |
62 V8PrivateProperty::getMessageEventCachedData(info.GetIsolate()) | 63 V8PrivateProperty::getMessageEventCachedData(isolate).set(wrapper, v8Data); |
63 .set(info.GetIsolate()->GetCurrentContext(), wrapper, v8Data); | 64 if (DOMWrapperWorld::current(isolate).isIsolatedWorld()) { |
64 if (DOMWrapperWorld::current(info.GetIsolate()).isIsolatedWorld()) | |
65 impl->setSerializedData( | 65 impl->setSerializedData( |
66 SerializedScriptValue::serializeAndSwallowExceptions( | 66 SerializedScriptValue::serializeAndSwallowExceptions(isolate, |
67 info.GetIsolate(), v8Data)); | 67 v8Data)); |
| 68 } |
68 } | 69 } |
69 v8SetReturnValue(info, wrapper); | 70 v8SetReturnValue(info, wrapper); |
70 } | 71 } |
71 | 72 |
72 template <typename EventType> | 73 template <typename EventType> |
73 void V8ServiceWorkerMessageEventInternal::dataAttributeGetterCustom( | 74 void V8ServiceWorkerMessageEventInternal::dataAttributeGetterCustom( |
74 const v8::FunctionCallbackInfo<v8::Value>& info) { | 75 const v8::FunctionCallbackInfo<v8::Value>& info) { |
75 EventType* event = V8TypeOf<EventType>::Type::toImpl(info.Holder()); | 76 EventType* event = V8TypeOf<EventType>::Type::toImpl(info.Holder()); |
76 v8::Isolate* isolate = info.GetIsolate(); | 77 v8::Isolate* isolate = info.GetIsolate(); |
77 ScriptState* scriptState = ScriptState::current(isolate); | |
78 auto privateCachedData = | 78 auto privateCachedData = |
79 V8PrivateProperty::getMessageEventCachedData(isolate); | 79 V8PrivateProperty::getMessageEventCachedData(isolate); |
80 v8::Local<v8::Value> result = | 80 v8::Local<v8::Value> result = privateCachedData.getOrEmpty(info.Holder()); |
81 privateCachedData.get(scriptState->context(), info.Holder()); | |
82 if (!result.IsEmpty()) { | 81 if (!result.IsEmpty()) { |
83 v8SetReturnValue(info, result); | 82 v8SetReturnValue(info, result); |
84 return; | 83 return; |
85 } | 84 } |
86 | 85 |
87 v8::Local<v8::Value> data; | 86 v8::Local<v8::Value> data; |
88 if (SerializedScriptValue* serializedValue = event->serializedData()) { | 87 if (SerializedScriptValue* serializedValue = event->serializedData()) { |
89 MessagePortArray ports = event->ports(); | 88 MessagePortArray ports = event->ports(); |
90 SerializedScriptValue::DeserializeOptions options; | 89 SerializedScriptValue::DeserializeOptions options; |
91 options.messagePorts = &ports; | 90 options.messagePorts = &ports; |
92 data = serializedValue->deserialize(isolate, options); | 91 data = serializedValue->deserialize(isolate, options); |
93 } else if (DOMWrapperWorld::current(isolate).isIsolatedWorld()) { | 92 } else if (DOMWrapperWorld::current(isolate).isIsolatedWorld()) { |
94 v8::Local<v8::Value> mainWorldData = | 93 v8::Local<v8::Value> mainWorldData = |
95 privateCachedData.getFromMainWorld(scriptState, event); | 94 privateCachedData.getFromMainWorld(event); |
96 if (!mainWorldData.IsEmpty()) { | 95 if (!mainWorldData.IsEmpty()) { |
97 // TODO(bashi): Enter the main world's ScriptState::Scope while | 96 // TODO(bashi): Enter the main world's ScriptState::Scope while |
98 // serializing the main world's value. | 97 // serializing the main world's value. |
99 event->setSerializedData( | 98 event->setSerializedData( |
100 SerializedScriptValue::serializeAndSwallowExceptions( | 99 SerializedScriptValue::serializeAndSwallowExceptions( |
101 info.GetIsolate(), mainWorldData)); | 100 info.GetIsolate(), mainWorldData)); |
102 data = event->serializedData()->deserialize(isolate); | 101 data = event->serializedData()->deserialize(isolate); |
103 } | 102 } |
104 } | 103 } |
105 if (data.IsEmpty()) | 104 if (data.IsEmpty()) |
106 data = v8::Null(isolate); | 105 data = v8::Null(isolate); |
107 privateCachedData.set(scriptState->context(), info.Holder(), data); | 106 privateCachedData.set(info.Holder(), data); |
108 v8SetReturnValue(info, data); | 107 v8SetReturnValue(info, data); |
109 } | 108 } |
110 | 109 |
111 } // namespace blink | 110 } // namespace blink |
112 | 111 |
113 #endif // V8ServiceWorkerMessageEventInternal_h | 112 #endif // V8ServiceWorkerMessageEventInternal_h |
OLD | NEW |