1/* 2 * Copyright (C) 2007 Henry Mason (hmason@mac.com) 3 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 */ 27 28#ifndef MessageEvent_h 29#define MessageEvent_h 30 31#include "Blob.h" 32#include "DOMWindow.h" 33#include "Event.h" 34#include "MessagePort.h" 35#include "SerializedScriptValue.h" 36#include <bindings/ScriptValue.h> 37#include <memory> 38#include <runtime/ArrayBuffer.h> 39 40namespace WebCore { 41 42class EventTarget; 43 44struct MessageEventInit : public EventInit { 45 MessageEventInit(); 46 47 Deprecated::ScriptValue data; 48 String origin; 49 String lastEventId; 50 RefPtr<EventTarget> source; 51 MessagePortArray ports; 52}; 53 54class MessageEvent : public Event { 55public: 56 static PassRefPtr<MessageEvent> create() 57 { 58 return adoptRef(new MessageEvent); 59 } 60 static PassRefPtr<MessageEvent> create(std::unique_ptr<MessagePortArray> ports, const Deprecated::ScriptValue& data = Deprecated::ScriptValue(), const String& origin = String(), const String& lastEventId = String(), PassRefPtr<EventTarget> source = 0) 61 { 62 return adoptRef(new MessageEvent(data, origin, lastEventId, source, WTF::move(ports))); 63 } 64 static PassRefPtr<MessageEvent> create(std::unique_ptr<MessagePortArray> ports, PassRefPtr<SerializedScriptValue> data, const String& origin = String(), const String& lastEventId = String(), PassRefPtr<EventTarget> source = 0) 65 { 66 return adoptRef(new MessageEvent(data, origin, lastEventId, source, WTF::move(ports))); 67 } 68 static PassRefPtr<MessageEvent> create(const String& data, const String& origin = String()) 69 { 70 return adoptRef(new MessageEvent(data, origin)); 71 } 72 static PassRefPtr<MessageEvent> create(PassRefPtr<Blob> data, const String& origin = String()) 73 { 74 return adoptRef(new MessageEvent(data, origin)); 75 } 76 static PassRefPtr<MessageEvent> create(PassRefPtr<ArrayBuffer> data, const String& origin = String()) 77 { 78 return adoptRef(new MessageEvent(data, origin)); 79 } 80 static PassRefPtr<MessageEvent> create(const AtomicString& type, const MessageEventInit& initializer) 81 { 82 return adoptRef(new MessageEvent(type, initializer)); 83 } 84 virtual ~MessageEvent(); 85 86 void initMessageEvent(const AtomicString& type, bool canBubble, bool cancelable, const Deprecated::ScriptValue& data, const String& origin, const String& lastEventId, DOMWindow* source, std::unique_ptr<MessagePortArray>); 87 void initMessageEvent(const AtomicString& type, bool canBubble, bool cancelable, PassRefPtr<SerializedScriptValue> data, const String& origin, const String& lastEventId, DOMWindow* source, std::unique_ptr<MessagePortArray>); 88 89 const String& origin() const { return m_origin; } 90 const String& lastEventId() const { return m_lastEventId; } 91 EventTarget* source() const { return m_source.get(); } 92 MessagePortArray ports() const { return m_ports ? *m_ports : MessagePortArray(); } 93 94 // FIXME: Remove this when we have custom ObjC binding support. 95 SerializedScriptValue* data() const; 96 97 // Needed for Objective-C bindings (see bug 28774). 98 MessagePort* messagePort(); 99 void initMessageEvent(const AtomicString& type, bool canBubble, bool cancelable, PassRefPtr<SerializedScriptValue> data, const String& origin, const String& lastEventId, DOMWindow* source, MessagePort*); 100 101 virtual EventInterface eventInterface() const override; 102 103 enum DataType { 104 DataTypeScriptValue, 105 DataTypeSerializedScriptValue, 106 DataTypeString, 107 DataTypeBlob, 108 DataTypeArrayBuffer 109 }; 110 DataType dataType() const { return m_dataType; } 111 const Deprecated::ScriptValue& dataAsScriptValue() const { ASSERT(m_dataType == DataTypeScriptValue); return m_dataAsScriptValue; } 112 PassRefPtr<SerializedScriptValue> dataAsSerializedScriptValue() const { ASSERT(m_dataType == DataTypeSerializedScriptValue); return m_dataAsSerializedScriptValue; } 113 String dataAsString() const { ASSERT(m_dataType == DataTypeString); return m_dataAsString; } 114 Blob* dataAsBlob() const { ASSERT(m_dataType == DataTypeBlob); return m_dataAsBlob.get(); } 115 ArrayBuffer* dataAsArrayBuffer() const { ASSERT(m_dataType == DataTypeArrayBuffer); return m_dataAsArrayBuffer.get(); } 116 117private: 118 MessageEvent(); 119 MessageEvent(const AtomicString&, const MessageEventInit&); 120 MessageEvent(const Deprecated::ScriptValue& data, const String& origin, const String& lastEventId, PassRefPtr<EventTarget> source, std::unique_ptr<MessagePortArray>); 121 MessageEvent(PassRefPtr<SerializedScriptValue> data, const String& origin, const String& lastEventId, PassRefPtr<EventTarget> source, std::unique_ptr<MessagePortArray>); 122 123 explicit MessageEvent(const String& data, const String& origin); 124 explicit MessageEvent(PassRefPtr<Blob> data, const String& origin); 125 explicit MessageEvent(PassRefPtr<ArrayBuffer> data, const String& origin); 126 127 DataType m_dataType; 128 Deprecated::ScriptValue m_dataAsScriptValue; 129 RefPtr<SerializedScriptValue> m_dataAsSerializedScriptValue; 130 String m_dataAsString; 131 RefPtr<Blob> m_dataAsBlob; 132 RefPtr<ArrayBuffer> m_dataAsArrayBuffer; 133 String m_origin; 134 String m_lastEventId; 135 RefPtr<EventTarget> m_source; 136 std::unique_ptr<MessagePortArray> m_ports; 137}; 138 139} // namespace WebCore 140 141#endif // MessageEvent_h 142