1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_dom_MessagePort_h
7 #define mozilla_dom_MessagePort_h
9 #include "mozilla/Attributes.h"
10 #include "mozilla/DOMEventTargetHelper.h"
17 class DispatchEventRunnable
;
18 class PostMessageRunnable
;
20 class MessagePortBase
: public DOMEventTargetHelper
23 explicit MessagePortBase(nsPIDOMWindow
* aWindow
);
29 PostMessageMoz(JSContext
* aCx
, JS::Handle
<JS::Value
> aMessage
,
30 const Optional
<Sequence
<JS::Value
>>& aTransferable
,
31 ErrorResult
& aRv
) = 0;
39 // The 'message' event handler has to call |Start()| method, so we
40 // cannot use IMPL_EVENT_HANDLER macro here.
41 virtual EventHandlerNonNull
*
45 SetOnmessage(EventHandlerNonNull
* aCallback
) = 0;
47 // Duplicate this message port. This method is used by the Structured Clone
48 // Algorithm and makes the new MessagePort active with the entangled
49 // MessagePort of this object.
50 virtual already_AddRefed
<MessagePortBase
>
54 class MessagePort MOZ_FINAL
: public MessagePortBase
56 friend class DispatchEventRunnable
;
57 friend class PostMessageRunnable
;
60 NS_DECL_ISUPPORTS_INHERITED
61 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MessagePort
,
64 explicit MessagePort(nsPIDOMWindow
* aWindow
);
67 WrapObject(JSContext
* aCx
) MOZ_OVERRIDE
;
70 PostMessageMoz(JSContext
* aCx
, JS::Handle
<JS::Value
> aMessage
,
71 const Optional
<Sequence
<JS::Value
>>& aTransferable
,
72 ErrorResult
& aRv
) MOZ_OVERRIDE
;
80 virtual EventHandlerNonNull
*
81 GetOnmessage() MOZ_OVERRIDE
;
84 SetOnmessage(EventHandlerNonNull
* aCallback
) MOZ_OVERRIDE
;
88 // This method entangles this MessagePort with another one.
89 // If it is already entangled, it's disentangled first and enatangle to the
92 Entangle(MessagePort
* aMessagePort
);
94 virtual already_AddRefed
<MessagePortBase
>
100 // Dispatch events from the Message Queue using a nsRunnable.
103 nsRefPtr
<DispatchEventRunnable
> mDispatchRunnable
;
105 nsRefPtr
<MessagePort
> mEntangledPort
;
107 nsTArray
<nsRefPtr
<PostMessageRunnable
> > mMessageQueue
;
108 bool mMessageQueueEnabled
;
112 } // namespace mozilla
114 #endif // mozilla_dom_MessagePort_h