OLD | NEW |
1 // Copyright 2015 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/V8ServiceWorkerMessageEvent.h" | 5 #ifndef V8ServiceWorkerMessageEventInternal_h |
| 6 #define V8ServiceWorkerMessageEventInternal_h |
6 | 7 |
7 #include "bindings/core/v8/SerializedScriptValue.h" | 8 #include "bindings/core/v8/SerializedScriptValue.h" |
8 #include "bindings/core/v8/SerializedScriptValueFactory.h" | 9 #include "bindings/core/v8/SerializedScriptValueFactory.h" |
9 #include "bindings/core/v8/V8HiddenValue.h" | 10 #include "bindings/core/v8/V8HiddenValue.h" |
10 #include "bindings/modules/v8/V8ServiceWorkerMessageEventInit.h" | |
11 | 11 |
12 namespace blink { | 12 namespace blink { |
13 | 13 |
14 void V8ServiceWorkerMessageEvent::constructorCustom(const v8::FunctionCallbackIn
fo<v8::Value>& info) | 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(const v8::FunctionCallbackInfo<v8::Val
ue>&); |
| 21 }; |
| 22 |
| 23 template <typename EventType, typename DictType> |
| 24 void V8ServiceWorkerMessageEventInternal::constructorCustom(const v8::FunctionCa
llbackInfo<v8::Value>& info) |
15 { | 25 { |
16 ExceptionState exceptionState(ExceptionState::ConstructionContext, "ServiceW
orkerMessageEvent", info.Holder(), info.GetIsolate()); | 26 ExceptionState exceptionState(ExceptionState::ConstructionContext, V8TypeOf<
EventType>::Type::wrapperTypeInfo.interfaceName, info.Holder(), info.GetIsolate(
)); |
17 if (UNLIKELY(info.Length() < 1)) { | 27 if (UNLIKELY(info.Length() < 1)) { |
18 setMinimumArityTypeError(exceptionState, 1, info.Length()); | 28 setMinimumArityTypeError(exceptionState, 1, info.Length()); |
19 exceptionState.throwIfNeeded(); | 29 exceptionState.throwIfNeeded(); |
20 return; | 30 return; |
21 } | 31 } |
22 | 32 |
23 V8StringResource<> type = info[0]; | 33 V8StringResource<> type = info[0]; |
24 if (!type.prepare()) | 34 if (!type.prepare()) |
25 return; | 35 return; |
26 | 36 |
27 ServiceWorkerMessageEventInit eventInitDict; | 37 DictType eventInitDict; |
28 if (!isUndefinedOrNull(info[1])) { | 38 if (!isUndefinedOrNull(info[1])) { |
29 if (!info[1]->IsObject()) { | 39 if (!info[1]->IsObject()) { |
30 exceptionState.throwTypeError("parameter 2 ('eventInitDict') is not
an object."); | 40 exceptionState.throwTypeError("parameter 2 ('eventInitDict') is not
an object."); |
31 exceptionState.throwIfNeeded(); | 41 exceptionState.throwIfNeeded(); |
32 return; | 42 return; |
33 } | 43 } |
34 V8ServiceWorkerMessageEventInit::toImpl(info.GetIsolate(), info[1], even
tInitDict, exceptionState); | 44 V8TypeOf<DictType>::Type::toImpl(info.GetIsolate(), info[1], eventInitDi
ct, exceptionState); |
35 if (exceptionState.throwIfNeeded()) | 45 if (exceptionState.throwIfNeeded()) |
36 return; | 46 return; |
37 } | 47 } |
38 | 48 |
39 RefPtrWillBeRawPtr<ServiceWorkerMessageEvent> impl = ServiceWorkerMessageEve
nt::create(type, eventInitDict); | 49 RefPtrWillBeRawPtr<EventType> impl = EventType::create(type, eventInitDict); |
40 v8::Local<v8::Object> wrapper = info.Holder(); | 50 v8::Local<v8::Object> wrapper = info.Holder(); |
41 wrapper = impl->associateWithWrapper(info.GetIsolate(), &V8ServiceWorkerMess
ageEvent::wrapperTypeInfo, wrapper); | 51 wrapper = impl->associateWithWrapper(info.GetIsolate(), &V8TypeOf<EventType>
::Type::wrapperTypeInfo, wrapper); |
42 | 52 |
43 // TODO(bashi): Workaround for http://crbug.com/529941. We need to store | 53 // TODO(bashi): Workaround for http://crbug.com/529941. We need to store |
44 // |data| as a hidden value to avoid cycle references. | 54 // |data| as a hidden value to avoid cycle references. |
45 if (eventInitDict.hasData()) { | 55 if (eventInitDict.hasData()) { |
46 v8::Local<v8::Value> v8Data = eventInitDict.data().v8Value(); | 56 v8::Local<v8::Value> v8Data = eventInitDict.data().v8Value(); |
47 V8HiddenValue::setHiddenValue(ScriptState::current(info.GetIsolate()), w
rapper, V8HiddenValue::data(info.GetIsolate()), v8Data); | 57 V8HiddenValue::setHiddenValue(ScriptState::current(info.GetIsolate()), w
rapper, V8HiddenValue::data(info.GetIsolate()), v8Data); |
48 if (DOMWrapperWorld::current(info.GetIsolate()).isIsolatedWorld()) | 58 if (DOMWrapperWorld::current(info.GetIsolate()).isIsolatedWorld()) |
49 impl->setSerializedData(SerializedScriptValueFactory::instance().cre
ateAndSwallowExceptions(info.GetIsolate(), v8Data)); | 59 impl->setSerializedData(SerializedScriptValueFactory::instance().cre
ateAndSwallowExceptions(info.GetIsolate(), v8Data)); |
50 } | 60 } |
51 v8SetReturnValue(info, wrapper); | 61 v8SetReturnValue(info, wrapper); |
52 } | 62 } |
53 | 63 |
54 void V8ServiceWorkerMessageEvent::dataAttributeGetterCustom(const v8::FunctionCa
llbackInfo<v8::Value>& info) | 64 template <typename EventType> |
| 65 void V8ServiceWorkerMessageEventInternal::dataAttributeGetterCustom(const v8::Fu
nctionCallbackInfo<v8::Value>& info) |
55 { | 66 { |
56 ServiceWorkerMessageEvent* event = V8ServiceWorkerMessageEvent::toImpl(info.
Holder()); | 67 EventType* event = V8TypeOf<EventType>::Type::toImpl(info.Holder()); |
57 v8::Isolate* isolate = info.GetIsolate(); | 68 v8::Isolate* isolate = info.GetIsolate(); |
58 ScriptState* scriptState = ScriptState::current(isolate); | 69 ScriptState* scriptState = ScriptState::current(isolate); |
59 v8::Local<v8::Value> result = V8HiddenValue::getHiddenValue(scriptState, inf
o.Holder(), V8HiddenValue::data(isolate)); | 70 v8::Local<v8::Value> result = V8HiddenValue::getHiddenValue(scriptState, inf
o.Holder(), V8HiddenValue::data(isolate)); |
60 | 71 |
61 if (!result.IsEmpty()) { | 72 if (!result.IsEmpty()) { |
62 v8SetReturnValue(info, result); | 73 v8SetReturnValue(info, result); |
63 return; | 74 return; |
64 } | 75 } |
65 | 76 |
66 v8::Local<v8::Value> data; | 77 v8::Local<v8::Value> data; |
67 if (SerializedScriptValue* serializedValue = event->serializedData()) { | 78 if (SerializedScriptValue* serializedValue = event->serializedData()) { |
68 MessagePortArray ports = event->ports(); | 79 MessagePortArray ports = event->ports(); |
69 data = serializedValue->deserialize(isolate, &ports); | 80 data = serializedValue->deserialize(isolate, &ports); |
70 } else if (DOMWrapperWorld::current(isolate).isIsolatedWorld()) { | 81 } else if (DOMWrapperWorld::current(isolate).isIsolatedWorld()) { |
71 v8::Local<v8::Value> mainWorldData = V8HiddenValue::getHiddenValueFromMa
inWorldWrapper(scriptState, event, V8HiddenValue::data(isolate)); | 82 v8::Local<v8::Value> mainWorldData = V8HiddenValue::getHiddenValueFromMa
inWorldWrapper(scriptState, event, V8HiddenValue::data(isolate)); |
72 if (!mainWorldData.IsEmpty()) { | 83 if (!mainWorldData.IsEmpty()) { |
73 // TODO(bashi): Enter the main world's ScriptState::Scope while | 84 // TODO(bashi): Enter the main world's ScriptState::Scope while |
74 // serializing the main world's value. | 85 // serializing the main world's value. |
75 event->setSerializedData(SerializedScriptValueFactory::instance().cr
eateAndSwallowExceptions(info.GetIsolate(), mainWorldData)); | 86 event->setSerializedData(SerializedScriptValueFactory::instance().cr
eateAndSwallowExceptions(info.GetIsolate(), mainWorldData)); |
76 data = event->serializedData()->deserialize(); | 87 data = event->serializedData()->deserialize(); |
77 } | 88 } |
78 } | 89 } |
79 if (data.IsEmpty()) | 90 if (data.IsEmpty()) |
80 data = v8::Null(isolate); | 91 data = v8::Null(isolate); |
81 V8HiddenValue::setHiddenValue(scriptState, info.Holder(), V8HiddenValue::dat
a(isolate), data); | 92 V8HiddenValue::setHiddenValue(scriptState, info.Holder(), V8HiddenValue::dat
a(isolate), data); |
82 v8SetReturnValue(info, data); | 93 v8SetReturnValue(info, data); |
83 } | 94 } |
84 | 95 |
85 } // namespace blink | 96 } // namespace blink |
| 97 |
| 98 #endif // V8ServiceWorkerMessageEventInternal_h |
OLD | NEW |