StructuredCloneData.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_ipc_StructuredCloneData_h
  6. #define mozilla_dom_ipc_StructuredCloneData_h
  7. #include <algorithm>
  8. #include "mozilla/RefPtr.h"
  9. #include "mozilla/dom/StructuredCloneHolder.h"
  10. #include "nsISupportsImpl.h"
  11. namespace IPC {
  12. class Message;
  13. }
  14. class PickleIterator;
  15. namespace mozilla {
  16. namespace dom {
  17. namespace ipc {
  18. class SharedJSAllocatedData final
  19. {
  20. public:
  21. explicit SharedJSAllocatedData(JSStructuredCloneData&& aData)
  22. : mData(Move(aData))
  23. { }
  24. static already_AddRefed<SharedJSAllocatedData>
  25. CreateFromExternalData(const char* aData, size_t aDataLength)
  26. {
  27. JSStructuredCloneData buf(JS::StructuredCloneScope::DifferentProcess);
  28. buf.AppendBytes(aData, aDataLength);
  29. RefPtr<SharedJSAllocatedData> sharedData =
  30. new SharedJSAllocatedData(Move(buf));
  31. return sharedData.forget();
  32. }
  33. static already_AddRefed<SharedJSAllocatedData>
  34. CreateFromExternalData(const JSStructuredCloneData& aData)
  35. {
  36. JSStructuredCloneData buf(aData.scope());
  37. buf.Append(aData);
  38. RefPtr<SharedJSAllocatedData> sharedData =
  39. new SharedJSAllocatedData(Move(buf));
  40. return sharedData.forget();
  41. }
  42. NS_INLINE_DECL_REFCOUNTING(SharedJSAllocatedData)
  43. JSStructuredCloneData& Data() { return mData; }
  44. size_t DataLength() const { return mData.Size(); }
  45. private:
  46. ~SharedJSAllocatedData() { }
  47. JSStructuredCloneData mData;
  48. };
  49. class StructuredCloneData : public StructuredCloneHolder
  50. {
  51. public:
  52. StructuredCloneData()
  53. : StructuredCloneHolder(StructuredCloneHolder::CloningSupported,
  54. StructuredCloneHolder::TransferringSupported,
  55. StructuredCloneHolder::StructuredCloneScope::DifferentProcess)
  56. , mExternalData(StructuredCloneHolder::StructuredCloneScope::DifferentProcess)
  57. , mInitialized(false)
  58. {}
  59. StructuredCloneData(const StructuredCloneData&) = delete;
  60. StructuredCloneData(StructuredCloneData&& aOther) = default;
  61. ~StructuredCloneData()
  62. {}
  63. StructuredCloneData&
  64. operator=(const StructuredCloneData& aOther) = delete;
  65. StructuredCloneData&
  66. operator=(StructuredCloneData&& aOther) = default;
  67. const nsTArray<RefPtr<BlobImpl>>& BlobImpls() const
  68. {
  69. return mBlobImplArray;
  70. }
  71. nsTArray<RefPtr<BlobImpl>>& BlobImpls()
  72. {
  73. return mBlobImplArray;
  74. }
  75. bool Copy(const StructuredCloneData& aData);
  76. void Read(JSContext* aCx,
  77. JS::MutableHandle<JS::Value> aValue,
  78. ErrorResult &aRv);
  79. void Write(JSContext* aCx,
  80. JS::Handle<JS::Value> aValue,
  81. ErrorResult &aRv);
  82. void Write(JSContext* aCx,
  83. JS::Handle<JS::Value> aValue,
  84. JS::Handle<JS::Value> aTransfers,
  85. ErrorResult &aRv);
  86. bool UseExternalData(const JSStructuredCloneData& aData)
  87. {
  88. auto iter = aData.Start();
  89. bool success = false;
  90. mExternalData = aData.Borrow(iter, aData.Size(), &success);
  91. mInitialized = true;
  92. return success;
  93. }
  94. bool CopyExternalData(const char* aData, size_t aDataLength);
  95. JSStructuredCloneData& Data()
  96. {
  97. return mSharedData ? mSharedData->Data() : mExternalData;
  98. }
  99. const JSStructuredCloneData& Data() const
  100. {
  101. return mSharedData ? mSharedData->Data() : mExternalData;
  102. }
  103. void InitScope(JS::StructuredCloneScope aScope)
  104. {
  105. Data().initScope(aScope);
  106. }
  107. size_t DataLength() const
  108. {
  109. return mSharedData ? mSharedData->DataLength() : mExternalData.Size();
  110. }
  111. SharedJSAllocatedData* SharedData() const
  112. {
  113. return mSharedData;
  114. }
  115. // For IPC serialization
  116. void WriteIPCParams(IPC::Message* aMessage) const;
  117. bool ReadIPCParams(const IPC::Message* aMessage, PickleIterator* aIter);
  118. private:
  119. JSStructuredCloneData mExternalData;
  120. RefPtr<SharedJSAllocatedData> mSharedData;
  121. bool mInitialized;
  122. };
  123. } // namespace ipc
  124. } // namespace dom
  125. } // namespace mozilla
  126. #endif // mozilla_dom_ipc_StructuredCloneData_h