StructuredCloneData.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "StructuredCloneData.h"
  6. #include "nsIDOMDOMException.h"
  7. #include "nsIMutable.h"
  8. #include "nsIXPConnect.h"
  9. #include "ipc/IPCMessageUtils.h"
  10. #include "mozilla/dom/BindingUtils.h"
  11. #include "mozilla/dom/BlobBinding.h"
  12. #include "mozilla/dom/DOMTypes.h"
  13. #include "mozilla/dom/File.h"
  14. #include "mozilla/dom/ToJSValue.h"
  15. #include "nsContentUtils.h"
  16. #include "nsJSEnvironment.h"
  17. #include "MainThreadUtils.h"
  18. #include "StructuredCloneTags.h"
  19. #include "jsapi.h"
  20. namespace mozilla {
  21. namespace dom {
  22. namespace ipc {
  23. bool
  24. StructuredCloneData::Copy(const StructuredCloneData& aData)
  25. {
  26. if (!aData.mInitialized) {
  27. return true;
  28. }
  29. if (aData.SharedData()) {
  30. mSharedData = aData.SharedData();
  31. } else {
  32. mSharedData =
  33. SharedJSAllocatedData::CreateFromExternalData(aData.Data());
  34. NS_ENSURE_TRUE(mSharedData, false);
  35. }
  36. PortIdentifiers().AppendElements(aData.PortIdentifiers());
  37. MOZ_ASSERT(BlobImpls().IsEmpty());
  38. BlobImpls().AppendElements(aData.BlobImpls());
  39. MOZ_ASSERT(GetSurfaces().IsEmpty());
  40. MOZ_ASSERT(WasmModules().IsEmpty());
  41. mInitialized = true;
  42. return true;
  43. }
  44. void
  45. StructuredCloneData::Read(JSContext* aCx,
  46. JS::MutableHandle<JS::Value> aValue,
  47. ErrorResult &aRv)
  48. {
  49. MOZ_ASSERT(mInitialized);
  50. nsIGlobalObject *global = xpc::NativeGlobal(JS::CurrentGlobalOrNull(aCx));
  51. MOZ_ASSERT(global);
  52. ReadFromBuffer(global, aCx, Data(), aValue, aRv);
  53. }
  54. void
  55. StructuredCloneData::Write(JSContext* aCx,
  56. JS::Handle<JS::Value> aValue,
  57. ErrorResult &aRv)
  58. {
  59. Write(aCx, aValue, JS::UndefinedHandleValue, aRv);
  60. }
  61. void
  62. StructuredCloneData::Write(JSContext* aCx,
  63. JS::Handle<JS::Value> aValue,
  64. JS::Handle<JS::Value> aTransfer,
  65. ErrorResult &aRv)
  66. {
  67. MOZ_ASSERT(!mInitialized);
  68. StructuredCloneHolder::Write(aCx, aValue, aTransfer,
  69. JS::CloneDataPolicy().denySharedArrayBuffer(), aRv);
  70. if (NS_WARN_IF(aRv.Failed())) {
  71. return;
  72. }
  73. JSStructuredCloneData data(mBuffer->scope());
  74. mBuffer->abandon();
  75. mBuffer->steal(&data);
  76. mBuffer = nullptr;
  77. mSharedData = new SharedJSAllocatedData(Move(data));
  78. mInitialized = true;
  79. }
  80. void
  81. StructuredCloneData::WriteIPCParams(IPC::Message* aMsg) const
  82. {
  83. WriteParam(aMsg, Data());
  84. }
  85. bool
  86. StructuredCloneData::ReadIPCParams(const IPC::Message* aMsg,
  87. PickleIterator* aIter)
  88. {
  89. MOZ_ASSERT(!mInitialized);
  90. JSStructuredCloneData data(JS::StructuredCloneScope::DifferentProcess);
  91. if (!ReadParam(aMsg, aIter, &data)) {
  92. return false;
  93. }
  94. mSharedData = new SharedJSAllocatedData(Move(data));
  95. mInitialized = true;
  96. return true;
  97. }
  98. bool
  99. StructuredCloneData::CopyExternalData(const char* aData,
  100. size_t aDataLength)
  101. {
  102. MOZ_ASSERT(!mInitialized);
  103. mSharedData = SharedJSAllocatedData::CreateFromExternalData(aData,
  104. aDataLength);
  105. NS_ENSURE_TRUE(mSharedData, false);
  106. mInitialized = true;
  107. return true;
  108. }
  109. } // namespace ipc
  110. } // namespace dom
  111. } // namespace mozilla