|
1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ |
|
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_workers_sharedworker_h__ |
|
7 #define mozilla_dom_workers_sharedworker_h__ |
|
8 |
|
9 #include "Workers.h" |
|
10 |
|
11 #include "mozilla/dom/BindingDeclarations.h" |
|
12 #include "mozilla/dom/workers/bindings/MessagePort.h" |
|
13 #include "mozilla/DOMEventTargetHelper.h" |
|
14 |
|
15 class nsIDOMEvent; |
|
16 class nsPIDOMWindow; |
|
17 |
|
18 namespace mozilla { |
|
19 class EventChainPreVisitor; |
|
20 } // namespace mozilla |
|
21 |
|
22 BEGIN_WORKERS_NAMESPACE |
|
23 |
|
24 class MessagePort; |
|
25 class RuntimeService; |
|
26 class WorkerPrivate; |
|
27 |
|
28 class SharedWorker MOZ_FINAL : public DOMEventTargetHelper |
|
29 { |
|
30 friend class MessagePort; |
|
31 friend class RuntimeService; |
|
32 |
|
33 typedef mozilla::ErrorResult ErrorResult; |
|
34 typedef mozilla::dom::GlobalObject GlobalObject; |
|
35 |
|
36 nsRefPtr<WorkerPrivate> mWorkerPrivate; |
|
37 nsRefPtr<MessagePort> mMessagePort; |
|
38 nsTArray<nsCOMPtr<nsIDOMEvent>> mSuspendedEvents; |
|
39 uint64_t mSerial; |
|
40 bool mSuspended; |
|
41 |
|
42 public: |
|
43 static already_AddRefed<SharedWorker> |
|
44 Constructor(const GlobalObject& aGlobal, JSContext* aCx, |
|
45 const nsAString& aScriptURL, const Optional<nsAString>& aName, |
|
46 ErrorResult& aRv); |
|
47 |
|
48 already_AddRefed<MessagePort> |
|
49 Port(); |
|
50 |
|
51 uint64_t |
|
52 Serial() const |
|
53 { |
|
54 return mSerial; |
|
55 } |
|
56 |
|
57 bool |
|
58 IsSuspended() const |
|
59 { |
|
60 return mSuspended; |
|
61 } |
|
62 |
|
63 void |
|
64 Suspend(); |
|
65 |
|
66 void |
|
67 Resume(); |
|
68 |
|
69 void |
|
70 QueueEvent(nsIDOMEvent* aEvent); |
|
71 |
|
72 void |
|
73 Close(); |
|
74 |
|
75 NS_DECL_ISUPPORTS_INHERITED |
|
76 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(SharedWorker, DOMEventTargetHelper) |
|
77 |
|
78 IMPL_EVENT_HANDLER(error) |
|
79 |
|
80 virtual JSObject* |
|
81 WrapObject(JSContext* aCx) MOZ_OVERRIDE; |
|
82 |
|
83 virtual nsresult |
|
84 PreHandleEvent(EventChainPreVisitor& aVisitor) MOZ_OVERRIDE; |
|
85 |
|
86 WorkerPrivate* |
|
87 GetWorkerPrivate() const |
|
88 { |
|
89 return mWorkerPrivate; |
|
90 } |
|
91 |
|
92 private: |
|
93 // This class can only be created from the RuntimeService. |
|
94 SharedWorker(nsPIDOMWindow* aWindow, |
|
95 WorkerPrivate* aWorkerPrivate); |
|
96 |
|
97 // This class is reference-counted and will be destroyed from Release(). |
|
98 ~SharedWorker(); |
|
99 |
|
100 // Only called by MessagePort. |
|
101 void |
|
102 PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage, |
|
103 const Optional<Sequence<JS::Value>>& aTransferable, |
|
104 ErrorResult& aRv); |
|
105 |
|
106 // Only called by RuntimeService. |
|
107 void |
|
108 NoteDeadWorker(JSContext* aCx); |
|
109 }; |
|
110 |
|
111 END_WORKERS_NAMESPACE |
|
112 |
|
113 #endif // mozilla_dom_workers_sharedworker_h__ |