1/*
2 * Copyright (C) 2008 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 COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
27#ifndef MessagePort_h
28#define MessagePort_h
29
30#include "bindings/v8/ScriptWrappable.h"
31#include "core/dom/ActiveDOMObject.h"
32#include "core/events/EventListener.h"
33#include "core/events/EventTarget.h"
34#include "public/platform/WebMessagePortChannel.h"
35#include "public/platform/WebMessagePortChannelClient.h"
36#include "wtf/OwnPtr.h"
37#include "wtf/PassOwnPtr.h"
38#include "wtf/PassRefPtr.h"
39#include "wtf/RefCounted.h"
40#include "wtf/RefPtr.h"
41#include "wtf/Vector.h"
42#include "wtf/WeakPtr.h"
43
44namespace WebCore {
45
46class Event;
47class ExceptionState;
48class LocalFrame;
49class MessagePort;
50class ExecutionContext;
51class SerializedScriptValue;
52
53// The overwhelmingly common case is sending a single port, so handle that efficiently with an inline buffer of size 1.
54typedef Vector<RefPtr<MessagePort>, 1> MessagePortArray;
55
56// Not to be confused with blink::WebMessagePortChannelArray; this one uses Vector and OwnPtr instead of WebVector and raw pointers.
57typedef Vector<OwnPtr<blink::WebMessagePortChannel>, 1> MessagePortChannelArray;
58
59class MessagePort FINAL : public RefCountedWillBeRefCountedGarbageCollected<MessagePort>
60    , public ActiveDOMObject
61    , public EventTargetWithInlineData
62    , public ScriptWrappable
63    , public blink::WebMessagePortChannelClient {
64    REFCOUNTED_EVENT_TARGET(MessagePort);
65    WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(MessagePort);
66public:
67    static PassRefPtrWillBeRawPtr<MessagePort> create(ExecutionContext&);
68    virtual ~MessagePort();
69
70    void postMessage(PassRefPtr<SerializedScriptValue> message, const MessagePortArray*, ExceptionState&);
71
72    void start();
73    void close();
74
75    void entangle(PassOwnPtr<blink::WebMessagePortChannel>);
76    PassOwnPtr<blink::WebMessagePortChannel> disentangle();
77
78    static PassOwnPtr<blink::WebMessagePortChannelArray> toWebMessagePortChannelArray(PassOwnPtr<MessagePortChannelArray>);
79    static PassOwnPtr<MessagePortArray> toMessagePortArray(ExecutionContext*, const blink::WebMessagePortChannelArray&);
80
81    // Returns 0 if there is an exception, or if the passed-in array is 0/empty.
82    static PassOwnPtr<MessagePortChannelArray> disentanglePorts(const MessagePortArray*, ExceptionState&);
83
84    // Returns 0 if the passed array is 0/empty.
85    static PassOwnPtr<MessagePortArray> entanglePorts(ExecutionContext&, PassOwnPtr<MessagePortChannelArray>);
86
87    bool started() const { return m_started; }
88
89    virtual const AtomicString& interfaceName() const OVERRIDE;
90    virtual ExecutionContext* executionContext() const OVERRIDE { return ActiveDOMObject::executionContext(); }
91    virtual MessagePort* toMessagePort() OVERRIDE { return this; }
92
93    // ActiveDOMObject implementation.
94    virtual bool hasPendingActivity() const OVERRIDE;
95    virtual void stop() OVERRIDE { close(); }
96
97    void setOnmessage(PassRefPtr<EventListener> listener)
98    {
99        setAttributeEventListener(EventTypeNames::message, listener);
100        start();
101    }
102    EventListener* onmessage() { return getAttributeEventListener(EventTypeNames::message); }
103
104    // A port starts out its life entangled, and remains entangled until it is closed or is cloned.
105    bool isEntangled() const { return !m_closed && !isNeutered(); }
106
107    // A port gets neutered when it is transferred to a new owner via postMessage().
108    bool isNeutered() const { return !m_entangledChannel; }
109
110private:
111    explicit MessagePort(ExecutionContext&);
112
113    // WebMessagePortChannelClient implementation.
114    virtual void messageAvailable() OVERRIDE;
115    void dispatchMessages();
116
117    OwnPtr<blink::WebMessagePortChannel> m_entangledChannel;
118
119    bool m_started;
120    bool m_closed;
121
122    WeakPtrFactory<MessagePort> m_weakFactory;
123};
124
125} // namespace WebCore
126
127#endif // MessagePort_h
128