Home | History | Annotate | Download | only in dom
      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 "core/dom/ActiveDOMObject.h"
     31 #include "core/events/EventListener.h"
     32 #include "core/events/EventTarget.h"
     33 #include "public/platform/WebMessagePortChannel.h"
     34 #include "public/platform/WebMessagePortChannelClient.h"
     35 #include "wtf/OwnPtr.h"
     36 #include "wtf/PassOwnPtr.h"
     37 #include "wtf/PassRefPtr.h"
     38 #include "wtf/RefCounted.h"
     39 #include "wtf/RefPtr.h"
     40 #include "wtf/Vector.h"
     41 #include "wtf/WeakPtr.h"
     42 
     43 namespace blink {
     44 
     45 class ExceptionState;
     46 class MessagePort;
     47 class ExecutionContext;
     48 class SerializedScriptValue;
     49 
     50 // The overwhelmingly common case is sending a single port, so handle that efficiently with an inline buffer of size 1.
     51 typedef WillBeHeapVector<RefPtrWillBeMember<MessagePort>, 1> MessagePortArray;
     52 
     53 // Not to be confused with WebMessagePortChannelArray; this one uses Vector and OwnPtr instead of WebVector and raw pointers.
     54 typedef Vector<OwnPtr<WebMessagePortChannel>, 1> MessagePortChannelArray;
     55 
     56 class MessagePort FINAL : public RefCountedWillBeGarbageCollectedFinalized<MessagePort>
     57     , public ActiveDOMObject
     58     , public EventTargetWithInlineData
     59     , public WebMessagePortChannelClient {
     60     DEFINE_WRAPPERTYPEINFO();
     61     REFCOUNTED_EVENT_TARGET(MessagePort);
     62     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(MessagePort);
     63 public:
     64     static PassRefPtrWillBeRawPtr<MessagePort> create(ExecutionContext&);
     65     virtual ~MessagePort();
     66 
     67     void postMessage(ExecutionContext*, PassRefPtr<SerializedScriptValue> message, const MessagePortArray*, ExceptionState&);
     68 
     69     void start();
     70     void close();
     71 
     72     void entangle(PassOwnPtr<WebMessagePortChannel>);
     73     PassOwnPtr<WebMessagePortChannel> disentangle();
     74 
     75     static PassOwnPtr<WebMessagePortChannelArray> toWebMessagePortChannelArray(PassOwnPtr<MessagePortChannelArray>);
     76     static PassOwnPtrWillBeRawPtr<MessagePortArray> toMessagePortArray(ExecutionContext*, const WebMessagePortChannelArray&);
     77 
     78     // Returns 0 if there is an exception, or if the passed-in array is 0/empty.
     79     static PassOwnPtr<MessagePortChannelArray> disentanglePorts(const MessagePortArray*, ExceptionState&);
     80 
     81     // Returns 0 if the passed array is 0/empty.
     82     static PassOwnPtrWillBeRawPtr<MessagePortArray> entanglePorts(ExecutionContext&, PassOwnPtr<MessagePortChannelArray>);
     83 
     84     bool started() const { return m_started; }
     85 
     86     virtual const AtomicString& interfaceName() const OVERRIDE;
     87     virtual ExecutionContext* executionContext() const OVERRIDE { return ActiveDOMObject::executionContext(); }
     88     virtual MessagePort* toMessagePort() OVERRIDE { return this; }
     89 
     90     // ActiveDOMObject implementation.
     91     virtual bool hasPendingActivity() const OVERRIDE;
     92     virtual void stop() OVERRIDE { close(); }
     93 
     94     void setOnmessage(PassRefPtr<EventListener> listener)
     95     {
     96         setAttributeEventListener(EventTypeNames::message, listener);
     97         start();
     98     }
     99     EventListener* onmessage() { return getAttributeEventListener(EventTypeNames::message); }
    100 
    101     // A port starts out its life entangled, and remains entangled until it is closed or is cloned.
    102     bool isEntangled() const { return !m_closed && !isNeutered(); }
    103 
    104     // A port gets neutered when it is transferred to a new owner via postMessage().
    105     bool isNeutered() const { return !m_entangledChannel; }
    106 
    107 private:
    108     explicit MessagePort(ExecutionContext&);
    109 
    110     // WebMessagePortChannelClient implementation.
    111     virtual void messageAvailable() OVERRIDE;
    112     void dispatchMessages();
    113 
    114     OwnPtr<WebMessagePortChannel> m_entangledChannel;
    115 
    116     bool m_started;
    117     bool m_closed;
    118 
    119     WeakPtrFactory<MessagePort> m_weakFactory;
    120 };
    121 
    122 } // namespace blink
    123 
    124 #endif // MessagePort_h
    125