MessageChannel.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include "MessageChannel.h"
  6. #include "mozilla/dom/MessageChannelBinding.h"
  7. #include "mozilla/dom/MessagePort.h"
  8. #include "mozilla/dom/Navigator.h"
  9. #include "mozilla/dom/WorkerPrivate.h"
  10. #include "mozilla/dom/WorkerRunnable.h"
  11. #include "nsContentUtils.h"
  12. #include "nsIDocument.h"
  13. #include "nsIGlobalObject.h"
  14. #include "nsIPrincipal.h"
  15. #include "nsServiceManagerUtils.h"
  16. namespace mozilla {
  17. namespace dom {
  18. NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MessageChannel, mGlobal, mPort1, mPort2)
  19. NS_IMPL_CYCLE_COLLECTING_ADDREF(MessageChannel)
  20. NS_IMPL_CYCLE_COLLECTING_RELEASE(MessageChannel)
  21. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MessageChannel)
  22. NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  23. NS_INTERFACE_MAP_ENTRY(nsISupports)
  24. NS_INTERFACE_MAP_END
  25. MessageChannel::MessageChannel(nsIGlobalObject* aGlobal)
  26. : mGlobal(aGlobal)
  27. {
  28. MOZ_ASSERT(aGlobal);
  29. }
  30. MessageChannel::~MessageChannel()
  31. {
  32. }
  33. JSObject*
  34. MessageChannel::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  35. {
  36. return MessageChannelBinding::Wrap(aCx, this, aGivenProto);
  37. }
  38. /* static */ already_AddRefed<MessageChannel>
  39. MessageChannel::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
  40. {
  41. nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
  42. return Constructor(global, aRv);
  43. }
  44. /* static */ already_AddRefed<MessageChannel>
  45. MessageChannel::Constructor(nsIGlobalObject* aGlobal, ErrorResult& aRv)
  46. {
  47. MOZ_ASSERT(aGlobal);
  48. nsID portUUID1;
  49. aRv = nsContentUtils::GenerateUUIDInPlace(portUUID1);
  50. if (aRv.Failed()) {
  51. return nullptr;
  52. }
  53. nsID portUUID2;
  54. aRv = nsContentUtils::GenerateUUIDInPlace(portUUID2);
  55. if (aRv.Failed()) {
  56. return nullptr;
  57. }
  58. RefPtr<MessageChannel> channel = new MessageChannel(aGlobal);
  59. channel->mPort1 = MessagePort::Create(aGlobal, portUUID1, portUUID2, aRv);
  60. if (NS_WARN_IF(aRv.Failed())) {
  61. return nullptr;
  62. }
  63. channel->mPort2 = MessagePort::Create(aGlobal, portUUID2, portUUID1, aRv);
  64. if (NS_WARN_IF(aRv.Failed())) {
  65. return nullptr;
  66. }
  67. channel->mPort1->UnshippedEntangle(channel->mPort2);
  68. channel->mPort2->UnshippedEntangle(channel->mPort1);
  69. return channel.forget();
  70. }
  71. } // namespace dom
  72. } // namespace mozilla