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

Side by Side Diff: Source/bindings/modules/v8/custom/V8ServiceWorkerMessageEventCustom.cpp

Issue 1130113006: ServiceWorker: Introduce ServiceWorkerMessageEvent to replace MessageEvent (3/3). (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: add initEvent tests Created 5 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
(Empty)
1 // Copyright 2015 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 #include "config.h"
6 #include "bindings/modules/v8/V8ServiceWorkerMessageEvent.h"
7
8 #include "bindings/core/v8/Dictionary.h"
9 #include "bindings/core/v8/SerializedScriptValue.h"
10 #include "bindings/core/v8/SerializedScriptValueFactory.h"
11 #include "bindings/core/v8/V8Binding.h"
12 #include "bindings/core/v8/V8DOMWrapper.h"
13 #include "bindings/core/v8/V8Event.h"
14 #include "bindings/core/v8/V8HiddenValue.h"
15 #include "bindings/core/v8/V8MessagePort.h"
16 #include "bindings/modules/v8/V8ServiceWorker.h"
17
18 namespace blink {
19
20 static bool initializeServiceWorkerMessageEvent(ServiceWorkerMessageEventInit& e ventInit, const Dictionary& options, ExceptionState& exceptionState, const v8::F unctionCallbackInfo<v8::Value>& info)
21 {
22 Dictionary::ConversionContext conversionContext(exceptionState);
23 if (!initializeEvent(eventInit, options, exceptionState, info))
24 return false;
25
26 if (!DictionaryHelper::convert(options, conversionContext.setConversionType( "DOMString", false), "origin", eventInit.origin))
27 return false;
28 if (!DictionaryHelper::convert(options, conversionContext.setConversionType( "DOMString", false), "lastEventId", eventInit.lastEventId))
29 return false;
30 if (!DictionaryHelper::convert(options, conversionContext.setConversionType( "MessagePort[]", false), "ports", eventInit.ports))
31 return false;
32 return true;
33 }
34
35 static void setEventSource(PassRefPtrWillBeRawPtr<ServiceWorkerMessageEvent> eve nt, v8::Isolate* isolate, v8::Local<v8::Value> value, ExceptionState& exceptionS tate)
36 {
37 if (value.IsEmpty() || isUndefinedOrNull(value))
38 return;
39 if (V8ServiceWorker::hasInstance(value, isolate)) {
40 ServiceWorker* source = V8ServiceWorker::toImpl(v8::Local<v8::Object>::C ast(value));
41 event->setSource(source);
42 return;
43 }
44 if (V8MessagePort::hasInstance(value, isolate)) {
45 MessagePort* source = V8MessagePort::toImpl(v8::Local<v8::Object>::Cast( value));
46 event->setSource(source);
47 return;
48 }
49 exceptionState.throwTypeError("The 'source' type is neither a ServiceWorker nor MessagePort");
50 }
51
52 static v8::Local<v8::Value> cacheState(v8::Isolate* isolate, v8::Local<v8::Objec t> event, v8::Local<v8::Value> data)
53 {
54 V8HiddenValue::setHiddenValue(isolate, event, V8HiddenValue::data(isolate), data);
55 return data;
56 }
57
58 void V8ServiceWorkerMessageEvent::constructorCustom(const v8::FunctionCallbackIn fo<v8::Value>& info)
59 {
60 ExceptionState exceptionState(ExceptionState::ConstructionContext, "ServiceW orkerMessageEvent", info.Holder(), info.GetIsolate());
61 if (info.Length() < 1) {
62 exceptionState.throwTypeError("An event name must be provided.");
63 exceptionState.throwIfNeeded();
64 return;
65 }
66
67 V8StringResource<> type(info[0]);
68 if (!type.prepare())
69 return;
70 v8::Local<v8::Value> source;
71 v8::Local<v8::Value> data;
72 ServiceWorkerMessageEventInit eventInit;
73 if (info.Length() >= 2) {
74 Dictionary options(info[1], info.GetIsolate(), exceptionState);
75 if (!initializeServiceWorkerMessageEvent(eventInit, options, exceptionSt ate, info)) {
76 exceptionState.throwIfNeeded();
77 return;
78 }
79 options.get("source", source);
80 options.get("data", data);
81 if (!data.IsEmpty())
82 V8HiddenValue::setHiddenValue(info.GetIsolate(), info.Holder(), v8At omicString(info.GetIsolate(), "data"), data);
83 }
84 RefPtrWillBeRawPtr<ServiceWorkerMessageEvent> event = ServiceWorkerMessageEv ent::create(type, eventInit);
85 setEventSource(event, info.GetIsolate(), source, exceptionState);
86 if (exceptionState.throwIfNeeded())
87 return;
88 if (DOMWrapperWorld::current(info.GetIsolate()).isIsolatedWorld()) {
89 if (!data.IsEmpty())
90 event->setSerializedData(SerializedScriptValueFactory::instance().cr eateAndSwallowExceptions(info.GetIsolate(), data));
91 }
92
93 v8::Local<v8::Object> wrapper = info.Holder();
94 event->associateWithWrapper(info.GetIsolate(), &V8ServiceWorkerMessageEvent: :wrapperTypeInfo, wrapper);
95 v8SetReturnValue(info, wrapper);
96 }
97
98 void V8ServiceWorkerMessageEvent::sourceAttributeGetterCustom(const v8::Property CallbackInfo<v8::Value>& info)
99 {
100 ServiceWorkerMessageEvent* event = V8ServiceWorkerMessageEvent::toImpl(info. Holder());
101 RefPtrWillBeRawPtr<ServiceWorker> sourceAsServiceWorker = event->sourceAsSer viceWorker();
102 RefPtrWillBeRawPtr<MessagePort> sourceAsMessagePort = event->sourceAsMessage Port();
103 ASSERT(sourceAsServiceWorker && sourceAsMessagePort);
104 if (sourceAsServiceWorker)
105 v8SetReturnValue(info, toV8(sourceAsServiceWorker, info.Holder(), info.G etIsolate()));
106 else if (sourceAsMessagePort)
107 v8SetReturnValue(info, toV8(sourceAsMessagePort, info.Holder(), info.Get Isolate()));
108 else
109 v8SetReturnValue(info, v8::Null(info.GetIsolate()));
110 }
111
112 void V8ServiceWorkerMessageEvent::dataAttributeGetterCustom(const v8::PropertyCa llbackInfo<v8::Value>& info)
113 {
114 ServiceWorkerMessageEvent* event = V8ServiceWorkerMessageEvent::toImpl(info. Holder());
115
116 v8::Local<v8::Value> result = V8HiddenValue::getHiddenValue(info.GetIsolate( ), info.Holder(), V8HiddenValue::data(info.GetIsolate()));
117
118 if (!result.IsEmpty()) {
119 v8SetReturnValue(info, result);
120 return;
121 }
122
123 if (!event->serializedData()) {
124 // If we're in an isolated world and the event was created in the main w orld,
125 // we need to find the 'data' property on the main world wrapper and clo ne it.
126 v8::Local<v8::Value> mainWorldData = V8HiddenValue::getHiddenValueFromMa inWorldWrapper(info.GetIsolate(), event, V8HiddenValue::data(info.GetIsolate())) ;
127 if (!mainWorldData.IsEmpty())
128 event->setSerializedData(SerializedScriptValueFactory::instance().cr eateAndSwallowExceptions(info.GetIsolate(), mainWorldData));
129 }
130
131 if (event->serializedData()) {
132 MessagePortArray ports = event->ports();
133 result = event->serializedData()->deserialize(info.GetIsolate(), &ports) ;
134 v8SetReturnValue(info, cacheState(info.GetIsolate(), info.Holder(), resu lt));
135 return;
136 }
137
138 v8SetReturnValue(info, cacheState(info.GetIsolate(), info.Holder(), v8::Null (info.GetIsolate())));
139 }
140
141 void V8ServiceWorkerMessageEvent::initServiceWorkerMessageEventMethodCustom(cons t v8::FunctionCallbackInfo<v8::Value>& info)
142 {
143 ExceptionState exceptionState(ExceptionState::ExecutionContext, "initService WorkerMessageEvent", "ServiceWorkerMessageEvent", info.Holder(), info.GetIsolate ());
144 ServiceWorkerMessageEvent* event = V8ServiceWorkerMessageEvent::toImpl(info. Holder());
145 TOSTRING_VOID(V8StringResource<>, typeArg, info[0]);
146 bool canBubbleArg;
147 bool cancelableArg;
148 if (!v8Call(info[1]->BooleanValue(info.GetIsolate()->GetCurrentContext()), c anBubbleArg)
149 || !v8Call(info[2]->BooleanValue(info.GetIsolate()->GetCurrentContext()) , cancelableArg))
150 return;
151 v8::Local<v8::Value> dataArg = info[3];
152 TOSTRING_VOID(V8StringResource<>, originArg, info[4]);
153 TOSTRING_VOID(V8StringResource<>, lastEventIdArg, info[5]);
154 v8::Local<v8::Value> sourceArg = info[6];
155 setEventSource(event, info.GetIsolate(), sourceArg, exceptionState);
156 if (exceptionState.throwIfNeeded())
157 return;
158 OwnPtrWillBeRawPtr<MessagePortArray> portArray = nullptr;
159 const int portArrayIndex = 7;
160 if (!isUndefinedOrNull(info[portArrayIndex])) {
161 portArray = adoptPtrWillBeNoop(new MessagePortArray);
162 *portArray = toRefPtrWillBeMemberNativeArray<MessagePort, V8MessagePort> (info[portArrayIndex], portArrayIndex + 1, info.GetIsolate(), exceptionState);
163 if (exceptionState.throwIfNeeded())
164 return;
165 }
166 event->initServiceWorkerMessageEvent(typeArg, canBubbleArg, cancelableArg, o riginArg, lastEventIdArg, portArray.release());
167
168 if (!dataArg.IsEmpty()) {
169 V8HiddenValue::setHiddenValue(info.GetIsolate(), info.Holder(), V8Hidden Value::data(info.GetIsolate()), dataArg);
170 if (DOMWrapperWorld::current(info.GetIsolate()).isIsolatedWorld())
171 event->setSerializedData(SerializedScriptValueFactory::instance().cr eateAndSwallowExceptions(info.GetIsolate(), dataArg));
172 }
173 }
174
175 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698