1/*
2 * Copyright (C) 2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#if ENABLE(SERVICE_WORKER)
29
30#include "ExtendableEvent.h"
31#include "ExtendableEventInit.h"
32#include "MessagePort.h"
33#include "ServiceWorker.h"
34#include "ServiceWorkerClient.h"
35#include <wtf/Variant.h>
36
37namespace JSC {
38class ExecState;
39class JSValue;
40}
41
42namespace WebCore {
43
44class MessagePort;
45class ServiceWorker;
46class ServiceWorkerClient;
47
48using ExtendableMessageEventSource = Variant<RefPtr<ServiceWorkerClient>, RefPtr<ServiceWorker>, RefPtr<MessagePort>>;
49
50class ExtendableMessageEvent final : public ExtendableEvent {
51public:
52 struct Init : ExtendableEventInit {
53 JSC::JSValue data;
54 String origin;
55 String lastEventId;
56 Optional<ExtendableMessageEventSource> source;
57 Vector<RefPtr<MessagePort>> ports;
58 };
59
60 static Ref<ExtendableMessageEvent> create(JSC::ExecState& state, const AtomicString& type, const Init& initializer, IsTrusted isTrusted = IsTrusted::No)
61 {
62 return adoptRef(*new ExtendableMessageEvent(state, type, initializer, isTrusted));
63 }
64
65 static Ref<ExtendableMessageEvent> create(Vector<RefPtr<MessagePort>>&&, RefPtr<SerializedScriptValue>&&, const String& origin = { }, const String& lastEventId = { }, Optional<ExtendableMessageEventSource>&& source = WTF::nullopt);
66
67 ~ExtendableMessageEvent();
68
69 SerializedScriptValue* data() const { return m_data.get(); }
70 const String& origin() const { return m_origin; }
71 const String& lastEventId() const { return m_lastEventId; }
72 const Optional<ExtendableMessageEventSource>& source() const { return m_source; }
73 const Vector<RefPtr<MessagePort>>& ports() const { return m_ports; }
74
75 EventInterface eventInterface() const final { return ExtendableMessageEventInterfaceType; }
76
77private:
78 ExtendableMessageEvent(JSC::ExecState&, const AtomicString&, const Init&, IsTrusted);
79 ExtendableMessageEvent(RefPtr<SerializedScriptValue>&& data, const String& origin, const String& lastEventId, Optional<ExtendableMessageEventSource>&&, Vector<RefPtr<MessagePort>>&&);
80
81 RefPtr<SerializedScriptValue> m_data;
82 String m_origin;
83 String m_lastEventId;
84 Optional<ExtendableMessageEventSource> m_source;
85 Vector<RefPtr<MessagePort>> m_ports;
86};
87
88} // namespace WebCore
89
90#endif // ENABLE(SERVICE_WORKER)
91