SharedWorker.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "SharedWorker.h"
  6. #include "nsPIDOMWindow.h"
  7. #include "mozilla/dom/Event.h"
  8. #include "mozilla/EventDispatcher.h"
  9. #include "mozilla/Preferences.h"
  10. #include "mozilla/dom/MessagePort.h"
  11. #include "mozilla/dom/SharedWorkerBinding.h"
  12. #include "nsContentUtils.h"
  13. #include "nsIClassInfoImpl.h"
  14. #include "nsIDOMEvent.h"
  15. #include "RuntimeService.h"
  16. #include "WorkerPrivate.h"
  17. using mozilla::dom::Optional;
  18. using mozilla::dom::Sequence;
  19. using mozilla::dom::MessagePort;
  20. using namespace mozilla;
  21. USING_WORKERS_NAMESPACE
  22. SharedWorker::SharedWorker(nsPIDOMWindowInner* aWindow,
  23. WorkerPrivate* aWorkerPrivate,
  24. MessagePort* aMessagePort)
  25. : DOMEventTargetHelper(aWindow)
  26. , mWorkerPrivate(aWorkerPrivate)
  27. , mMessagePort(aMessagePort)
  28. , mFrozen(false)
  29. {
  30. AssertIsOnMainThread();
  31. MOZ_ASSERT(aWorkerPrivate);
  32. MOZ_ASSERT(aMessagePort);
  33. }
  34. SharedWorker::~SharedWorker()
  35. {
  36. AssertIsOnMainThread();
  37. }
  38. // static
  39. already_AddRefed<SharedWorker>
  40. SharedWorker::Constructor(const GlobalObject& aGlobal, JSContext* aCx,
  41. const nsAString& aScriptURL,
  42. const mozilla::dom::Optional<nsAString>& aName,
  43. ErrorResult& aRv)
  44. {
  45. AssertIsOnMainThread();
  46. RuntimeService* rts = RuntimeService::GetOrCreateService();
  47. if (!rts) {
  48. aRv = NS_ERROR_NOT_AVAILABLE;
  49. return nullptr;
  50. }
  51. nsCString name;
  52. if (aName.WasPassed()) {
  53. name = NS_ConvertUTF16toUTF8(aName.Value());
  54. }
  55. RefPtr<SharedWorker> sharedWorker;
  56. nsresult rv = rts->CreateSharedWorker(aGlobal, aScriptURL, name,
  57. getter_AddRefs(sharedWorker));
  58. if (NS_FAILED(rv)) {
  59. aRv = rv;
  60. return nullptr;
  61. }
  62. return sharedWorker.forget();
  63. }
  64. MessagePort*
  65. SharedWorker::Port()
  66. {
  67. AssertIsOnMainThread();
  68. return mMessagePort;
  69. }
  70. void
  71. SharedWorker::Freeze()
  72. {
  73. AssertIsOnMainThread();
  74. MOZ_ASSERT(!IsFrozen());
  75. mFrozen = true;
  76. }
  77. void
  78. SharedWorker::Thaw()
  79. {
  80. AssertIsOnMainThread();
  81. MOZ_ASSERT(IsFrozen());
  82. mFrozen = false;
  83. if (!mFrozenEvents.IsEmpty()) {
  84. nsTArray<nsCOMPtr<nsIDOMEvent>> events;
  85. mFrozenEvents.SwapElements(events);
  86. for (uint32_t index = 0; index < events.Length(); index++) {
  87. nsCOMPtr<nsIDOMEvent>& event = events[index];
  88. MOZ_ASSERT(event);
  89. nsCOMPtr<nsIDOMEventTarget> target;
  90. if (NS_SUCCEEDED(event->GetTarget(getter_AddRefs(target)))) {
  91. bool ignored;
  92. if (NS_FAILED(target->DispatchEvent(event, &ignored))) {
  93. NS_WARNING("Failed to dispatch event!");
  94. }
  95. } else {
  96. NS_WARNING("Failed to get target!");
  97. }
  98. }
  99. }
  100. }
  101. void
  102. SharedWorker::QueueEvent(nsIDOMEvent* aEvent)
  103. {
  104. AssertIsOnMainThread();
  105. MOZ_ASSERT(aEvent);
  106. MOZ_ASSERT(IsFrozen());
  107. mFrozenEvents.AppendElement(aEvent);
  108. }
  109. void
  110. SharedWorker::Close()
  111. {
  112. AssertIsOnMainThread();
  113. if (mMessagePort) {
  114. mMessagePort->Close();
  115. }
  116. }
  117. void
  118. SharedWorker::PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
  119. const Optional<Sequence<JS::Value>>& aTransferable,
  120. ErrorResult& aRv)
  121. {
  122. AssertIsOnMainThread();
  123. MOZ_ASSERT(mWorkerPrivate);
  124. MOZ_ASSERT(mMessagePort);
  125. mMessagePort->PostMessage(aCx, aMessage, aTransferable, aRv);
  126. }
  127. NS_IMPL_ADDREF_INHERITED(SharedWorker, DOMEventTargetHelper)
  128. NS_IMPL_RELEASE_INHERITED(SharedWorker, DOMEventTargetHelper)
  129. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(SharedWorker)
  130. NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
  131. NS_IMPL_CYCLE_COLLECTION_CLASS(SharedWorker)
  132. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(SharedWorker,
  133. DOMEventTargetHelper)
  134. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMessagePort)
  135. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFrozenEvents)
  136. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  137. NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(SharedWorker,
  138. DOMEventTargetHelper)
  139. NS_IMPL_CYCLE_COLLECTION_UNLINK(mMessagePort)
  140. NS_IMPL_CYCLE_COLLECTION_UNLINK(mFrozenEvents)
  141. NS_IMPL_CYCLE_COLLECTION_UNLINK_END
  142. JSObject*
  143. SharedWorker::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  144. {
  145. AssertIsOnMainThread();
  146. return SharedWorkerBinding::Wrap(aCx, this, aGivenProto);
  147. }
  148. nsresult
  149. SharedWorker::GetEventTargetParent(EventChainPreVisitor& aVisitor)
  150. {
  151. AssertIsOnMainThread();
  152. if (IsFrozen()) {
  153. nsCOMPtr<nsIDOMEvent> event = aVisitor.mDOMEvent;
  154. if (!event) {
  155. event = EventDispatcher::CreateEvent(aVisitor.mEvent->mOriginalTarget,
  156. aVisitor.mPresContext,
  157. aVisitor.mEvent, EmptyString());
  158. }
  159. QueueEvent(event);
  160. aVisitor.mCanHandle = false;
  161. aVisitor.SetParentTarget(nullptr, false);
  162. return NS_OK;
  163. }
  164. return DOMEventTargetHelper::GetEventTargetParent(aVisitor);
  165. }