SharedMessagePortMessage.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* -*- Mode: C++; tab-width: 2; 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 "SharedMessagePortMessage.h"
  6. #include "MessagePort.h"
  7. #include "MessagePortChild.h"
  8. #include "MessagePortParent.h"
  9. #include "mozilla/dom/ipc/BlobChild.h"
  10. #include "mozilla/dom/ipc/BlobParent.h"
  11. #include "mozilla/dom/File.h"
  12. #include "mozilla/dom/PMessagePort.h"
  13. #include "mozilla/ipc/BackgroundChild.h"
  14. #include "mozilla/ipc/BackgroundParent.h"
  15. namespace mozilla {
  16. using namespace ipc;
  17. namespace dom {
  18. /* static */ void
  19. SharedMessagePortMessage::FromSharedToMessagesChild(
  20. MessagePortChild* aActor,
  21. const nsTArray<RefPtr<SharedMessagePortMessage>>& aData,
  22. nsTArray<MessagePortMessage>& aArray)
  23. {
  24. MOZ_ASSERT(aActor);
  25. MOZ_ASSERT(aArray.IsEmpty());
  26. aArray.SetCapacity(aData.Length());
  27. PBackgroundChild* backgroundManager = aActor->Manager();
  28. MOZ_ASSERT(backgroundManager);
  29. for (auto& data : aData) {
  30. MessagePortMessage* message = aArray.AppendElement();
  31. data->mBuffer->abandon();
  32. data->mBuffer->steal(&message->data().data);
  33. const nsTArray<RefPtr<BlobImpl>>& blobImpls = data->BlobImpls();
  34. if (!blobImpls.IsEmpty()) {
  35. message->blobsChild().SetCapacity(blobImpls.Length());
  36. for (uint32_t i = 0, len = blobImpls.Length(); i < len; ++i) {
  37. PBlobChild* blobChild =
  38. BackgroundChild::GetOrCreateActorForBlobImpl(backgroundManager,
  39. blobImpls[i]);
  40. message->blobsChild().AppendElement(blobChild);
  41. }
  42. }
  43. message->transferredPorts().AppendElements(data->PortIdentifiers());
  44. }
  45. }
  46. /* static */ bool
  47. SharedMessagePortMessage::FromMessagesToSharedChild(
  48. nsTArray<MessagePortMessage>& aArray,
  49. FallibleTArray<RefPtr<SharedMessagePortMessage>>& aData)
  50. {
  51. MOZ_ASSERT(aData.IsEmpty());
  52. if (NS_WARN_IF(!aData.SetCapacity(aArray.Length(), mozilla::fallible))) {
  53. return false;
  54. }
  55. for (auto& message : aArray) {
  56. RefPtr<SharedMessagePortMessage> data = new SharedMessagePortMessage();
  57. data->mBuffer = MakeUnique<JSAutoStructuredCloneBuffer>(
  58. JS::StructuredCloneScope::DifferentProcess, nullptr, nullptr);
  59. data->mBuffer->adopt(Move(message.data().data), JS_STRUCTURED_CLONE_VERSION,
  60. &StructuredCloneHolder::sCallbacks, data.get());
  61. const nsTArray<PBlobChild*>& blobs = message.blobsChild();
  62. if (!blobs.IsEmpty()) {
  63. data->BlobImpls().SetCapacity(blobs.Length());
  64. for (uint32_t i = 0, len = blobs.Length(); i < len; ++i) {
  65. RefPtr<BlobImpl> impl =
  66. static_cast<BlobChild*>(blobs[i])->GetBlobImpl();
  67. data->BlobImpls().AppendElement(impl);
  68. }
  69. }
  70. data->PortIdentifiers().AppendElements(message.transferredPorts());
  71. if (!aData.AppendElement(data, mozilla::fallible)) {
  72. return false;
  73. }
  74. }
  75. return true;
  76. }
  77. /* static */ bool
  78. SharedMessagePortMessage::FromSharedToMessagesParent(
  79. MessagePortParent* aActor,
  80. const nsTArray<RefPtr<SharedMessagePortMessage>>& aData,
  81. FallibleTArray<MessagePortMessage>& aArray)
  82. {
  83. MOZ_ASSERT(aArray.IsEmpty());
  84. if (NS_WARN_IF(!aArray.SetCapacity(aData.Length(), mozilla::fallible))) {
  85. return false;
  86. }
  87. PBackgroundParent* backgroundManager = aActor->Manager();
  88. MOZ_ASSERT(backgroundManager);
  89. for (auto& data : aData) {
  90. MessagePortMessage* message = aArray.AppendElement(mozilla::fallible);
  91. data->mBuffer->abandon();
  92. data->mBuffer->steal(&message->data().data);
  93. const nsTArray<RefPtr<BlobImpl>>& blobImpls = data->BlobImpls();
  94. if (!blobImpls.IsEmpty()) {
  95. message->blobsParent().SetCapacity(blobImpls.Length());
  96. for (uint32_t i = 0, len = blobImpls.Length(); i < len; ++i) {
  97. PBlobParent* blobParent =
  98. BackgroundParent::GetOrCreateActorForBlobImpl(backgroundManager,
  99. blobImpls[i]);
  100. message->blobsParent().AppendElement(blobParent);
  101. }
  102. }
  103. message->transferredPorts().AppendElements(data->PortIdentifiers());
  104. }
  105. return true;
  106. }
  107. /* static */ bool
  108. SharedMessagePortMessage::FromMessagesToSharedParent(
  109. nsTArray<MessagePortMessage>& aArray,
  110. FallibleTArray<RefPtr<SharedMessagePortMessage>>& aData)
  111. {
  112. MOZ_ASSERT(aData.IsEmpty());
  113. if (NS_WARN_IF(!aData.SetCapacity(aArray.Length(), mozilla::fallible))) {
  114. return false;
  115. }
  116. for (auto& message : aArray) {
  117. RefPtr<SharedMessagePortMessage> data = new SharedMessagePortMessage();
  118. data->mBuffer = MakeUnique<JSAutoStructuredCloneBuffer>(
  119. JS::StructuredCloneScope::DifferentProcess, nullptr, nullptr);
  120. data->mBuffer->adopt(Move(message.data().data), JS_STRUCTURED_CLONE_VERSION,
  121. &StructuredCloneHolder::sCallbacks, data.get());
  122. const nsTArray<PBlobParent*>& blobs = message.blobsParent();
  123. if (!blobs.IsEmpty()) {
  124. data->BlobImpls().SetCapacity(blobs.Length());
  125. for (uint32_t i = 0, len = blobs.Length(); i < len; ++i) {
  126. RefPtr<BlobImpl> impl =
  127. static_cast<BlobParent*>(blobs[i])->GetBlobImpl();
  128. data->BlobImpls().AppendElement(impl);
  129. }
  130. }
  131. data->PortIdentifiers().AppendElements(message.transferredPorts());
  132. if (!aData.AppendElement(data, mozilla::fallible)) {
  133. return false;
  134. }
  135. }
  136. return true;
  137. }
  138. } // namespace dom
  139. } // namespace mozilla