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