|
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/. */ |
|
5 |
|
6 #ifndef mozilla_dom_MessagePort_h |
|
7 #define mozilla_dom_MessagePort_h |
|
8 |
|
9 #include "mozilla/Attributes.h" |
|
10 #include "mozilla/DOMEventTargetHelper.h" |
|
11 |
|
12 class nsPIDOMWindow; |
|
13 |
|
14 namespace mozilla { |
|
15 namespace dom { |
|
16 |
|
17 class DispatchEventRunnable; |
|
18 class PostMessageRunnable; |
|
19 |
|
20 class MessagePortBase : public DOMEventTargetHelper |
|
21 { |
|
22 protected: |
|
23 MessagePortBase(nsPIDOMWindow* aWindow); |
|
24 MessagePortBase(); |
|
25 |
|
26 public: |
|
27 |
|
28 virtual void |
|
29 PostMessageMoz(JSContext* aCx, JS::Handle<JS::Value> aMessage, |
|
30 const Optional<Sequence<JS::Value>>& aTransferable, |
|
31 ErrorResult& aRv) = 0; |
|
32 |
|
33 virtual void |
|
34 Start() = 0; |
|
35 |
|
36 virtual void |
|
37 Close() = 0; |
|
38 |
|
39 // The 'message' event handler has to call |Start()| method, so we |
|
40 // cannot use IMPL_EVENT_HANDLER macro here. |
|
41 virtual EventHandlerNonNull* |
|
42 GetOnmessage() = 0; |
|
43 |
|
44 virtual void |
|
45 SetOnmessage(EventHandlerNonNull* aCallback) = 0; |
|
46 |
|
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> |
|
51 Clone() = 0; |
|
52 }; |
|
53 |
|
54 class MessagePort MOZ_FINAL : public MessagePortBase |
|
55 { |
|
56 friend class DispatchEventRunnable; |
|
57 friend class PostMessageRunnable; |
|
58 |
|
59 public: |
|
60 NS_DECL_ISUPPORTS_INHERITED |
|
61 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MessagePort, |
|
62 DOMEventTargetHelper) |
|
63 |
|
64 MessagePort(nsPIDOMWindow* aWindow); |
|
65 ~MessagePort(); |
|
66 |
|
67 virtual JSObject* |
|
68 WrapObject(JSContext* aCx) MOZ_OVERRIDE; |
|
69 |
|
70 virtual void |
|
71 PostMessageMoz(JSContext* aCx, JS::Handle<JS::Value> aMessage, |
|
72 const Optional<Sequence<JS::Value>>& aTransferable, |
|
73 ErrorResult& aRv) MOZ_OVERRIDE; |
|
74 |
|
75 virtual void |
|
76 Start() MOZ_OVERRIDE; |
|
77 |
|
78 virtual void |
|
79 Close() MOZ_OVERRIDE; |
|
80 |
|
81 virtual EventHandlerNonNull* |
|
82 GetOnmessage() MOZ_OVERRIDE; |
|
83 |
|
84 virtual void |
|
85 SetOnmessage(EventHandlerNonNull* aCallback) MOZ_OVERRIDE; |
|
86 |
|
87 // Non WebIDL methods |
|
88 |
|
89 // This method entangles this MessagePort with another one. |
|
90 // If it is already entangled, it's disentangled first and enatangle to the |
|
91 // new one. |
|
92 void |
|
93 Entangle(MessagePort* aMessagePort); |
|
94 |
|
95 virtual already_AddRefed<MessagePortBase> |
|
96 Clone() MOZ_OVERRIDE; |
|
97 |
|
98 private: |
|
99 // Dispatch events from the Message Queue using a nsRunnable. |
|
100 void Dispatch(); |
|
101 |
|
102 nsRefPtr<DispatchEventRunnable> mDispatchRunnable; |
|
103 |
|
104 nsRefPtr<MessagePort> mEntangledPort; |
|
105 |
|
106 nsTArray<nsRefPtr<PostMessageRunnable> > mMessageQueue; |
|
107 bool mMessageQueueEnabled; |
|
108 }; |
|
109 |
|
110 } // namespace dom |
|
111 } // namespace mozilla |
|
112 |
|
113 #endif // mozilla_dom_MessagePort_h |