SharedWorker.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* -*- Mode: C++; tab-width: 8; 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. #ifndef mozilla_dom_workers_sharedworker_h__
  6. #define mozilla_dom_workers_sharedworker_h__
  7. #include "Workers.h"
  8. #include "mozilla/dom/BindingDeclarations.h"
  9. #include "mozilla/DOMEventTargetHelper.h"
  10. class nsIDOMEvent;
  11. class nsPIDOMWindowInner;
  12. namespace mozilla {
  13. class EventChainPreVisitor;
  14. namespace dom {
  15. class MessagePort;
  16. }
  17. } // namespace mozilla
  18. BEGIN_WORKERS_NAMESPACE
  19. class RuntimeService;
  20. class WorkerPrivate;
  21. class SharedWorker final : public DOMEventTargetHelper
  22. {
  23. friend class RuntimeService;
  24. typedef mozilla::ErrorResult ErrorResult;
  25. typedef mozilla::dom::GlobalObject GlobalObject;
  26. RefPtr<WorkerPrivate> mWorkerPrivate;
  27. RefPtr<MessagePort> mMessagePort;
  28. nsTArray<nsCOMPtr<nsIDOMEvent>> mFrozenEvents;
  29. bool mFrozen;
  30. public:
  31. static already_AddRefed<SharedWorker>
  32. Constructor(const GlobalObject& aGlobal, JSContext* aCx,
  33. const nsAString& aScriptURL, const Optional<nsAString>& aName,
  34. ErrorResult& aRv);
  35. MessagePort*
  36. Port();
  37. bool
  38. IsFrozen() const
  39. {
  40. return mFrozen;
  41. }
  42. void
  43. Freeze();
  44. void
  45. Thaw();
  46. void
  47. QueueEvent(nsIDOMEvent* aEvent);
  48. void
  49. Close();
  50. NS_DECL_ISUPPORTS_INHERITED
  51. NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(SharedWorker, DOMEventTargetHelper)
  52. IMPL_EVENT_HANDLER(error)
  53. virtual JSObject*
  54. WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  55. virtual nsresult
  56. GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
  57. WorkerPrivate*
  58. GetWorkerPrivate() const
  59. {
  60. return mWorkerPrivate;
  61. }
  62. private:
  63. // This class can only be created from the RuntimeService.
  64. SharedWorker(nsPIDOMWindowInner* aWindow,
  65. WorkerPrivate* aWorkerPrivate,
  66. MessagePort* aMessagePort);
  67. // This class is reference-counted and will be destroyed from Release().
  68. ~SharedWorker();
  69. // Only called by MessagePort.
  70. void
  71. PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
  72. const Optional<Sequence<JS::Value>>& aTransferable,
  73. ErrorResult& aRv);
  74. };
  75. END_WORKERS_NAMESPACE
  76. #endif // mozilla_dom_workers_sharedworker_h__