bool AesKeyAlgorithm::WriteStructuredClone(JSStructuredCloneWriter* aWriter) const { return JS_WriteUint32Pair(aWriter, SCTAG_AESKEYALG, 0) && JS_WriteUint32Pair(aWriter, mLength, 0) && WriteString(aWriter, mName); }
bool StructuredCloneHelper::WriteCallback(JSContext* aCx, JSStructuredCloneWriter* aWriter, JS::Handle<JSObject*> aObj) { if (!mSupportsCloning) { return false; } // See if this is a File/Blob object. { Blob* blob = nullptr; if (NS_SUCCEEDED(UNWRAP_OBJECT(Blob, aObj, blob))) { BlobImpl* blobImpl = blob->Impl(); if (JS_WriteUint32Pair(aWriter, SCTAG_DOM_BLOB, mBlobImplArray.Length())) { mBlobImplArray.AppendElement(blobImpl); return true; } return false; } } { FileList* fileList = nullptr; if (NS_SUCCEEDED(UNWRAP_OBJECT(FileList, aObj, fileList))) { // A FileList is serialized writing the X number of elements and the offset // from mBlobImplArray. The Read will take X elements from mBlobImplArray // starting from the offset. if (!JS_WriteUint32Pair(aWriter, SCTAG_DOM_FILELIST, fileList->Length()) || !JS_WriteUint32Pair(aWriter, 0, mBlobImplArray.Length())) { return false; } for (uint32_t i = 0; i < fileList->Length(); ++i) { mBlobImplArray.AppendElement(fileList->Item(i)->Impl()); } return true; } } // See if this is an ImageBitmap object. { ImageBitmap* imageBitmap = nullptr; if (NS_SUCCEEDED(UNWRAP_OBJECT(ImageBitmap, aObj, imageBitmap))) { return ImageBitmap::WriteStructuredClone(aWriter, GetImages(), imageBitmap); } } return NS_DOMWriteStructuredClone(aCx, aWriter, aObj, nullptr); }
bool WriteStructuredCloneImageData(JSContext* aCx, JSStructuredCloneWriter* aWriter, ImageData* aImageData) { uint32_t width = aImageData->Width(); uint32_t height = aImageData->Height(); JS::Rooted<JSObject*> dataArray(aCx, aImageData->GetDataObject()); JSAutoCompartment ac(aCx, dataArray); JS::Rooted<JS::Value> arrayValue(aCx, JS::ObjectValue(*dataArray)); return JS_WriteUint32Pair(aWriter, SCTAG_DOM_IMAGEDATA, 0) && JS_WriteUint32Pair(aWriter, width, height) && JS_WriteTypedArray(aWriter, arrayValue); }
bool CryptoKey::WriteStructuredClone(JSStructuredCloneWriter* aWriter) const { nsNSSShutDownPreventionLock locker; if (isAlreadyShutDown()) { return false; } // Write in five pieces // 1. Attributes // 2. Symmetric key as raw (if present) // 3. Private key as pkcs8 (if present) // 4. Public key as spki (if present) // 5. Algorithm in whatever form it chooses CryptoBuffer priv, pub; if (mPrivateKey) { CryptoKey::PrivateKeyToPkcs8(mPrivateKey, priv, locker); } if (mPublicKey) { CryptoKey::PublicKeyToSpki(mPublicKey, pub, locker); } return JS_WriteUint32Pair(aWriter, mAttributes, CRYPTOKEY_SC_VERSION) && WriteBuffer(aWriter, mSymKey) && WriteBuffer(aWriter, priv) && WriteBuffer(aWriter, pub) && mAlgorithm.WriteStructuredClone(aWriter); }
bool EcKeyAlgorithm::WriteStructuredClone(JSStructuredCloneWriter* aWriter) const { return JS_WriteUint32Pair(aWriter, SCTAG_ECKEYALG, 0) && WriteString(aWriter, mNamedCurve) && WriteString(aWriter, mName); }
bool StructuredCloneBlob::Holder::WriteStructuredClone( JSContext* aCx, JSStructuredCloneWriter* aWriter, StructuredCloneHolder* aHolder) { auto& data = mBuffer->data(); if (!JS_WriteUint32Pair(aWriter, SCTAG_DOM_STRUCTURED_CLONE_HOLDER, 0) || !JS_WriteUint32Pair(aWriter, data.Size(), JS_STRUCTURED_CLONE_VERSION) || !JS_WriteUint32Pair(aWriter, aHolder->BlobImpls().Length(), BlobImpls().Length())) { return false; } aHolder->BlobImpls().AppendElements(BlobImpls()); return data.ForEachDataChunk([&](const char* aData, size_t aSize) { return JS_WriteBytes(aWriter, aData, aSize); }); }
inline bool WriteString(JSStructuredCloneWriter* aWriter, const nsString& aString) { MOZ_ASSERT(aWriter); size_t charSize = sizeof(nsString::char_type); return JS_WriteUint32Pair(aWriter, aString.Length(), 0) && JS_WriteBytes(aWriter, aString.get(), aString.Length() * charSize); }
bool RTCCertificate::WriteCertificate(JSStructuredCloneWriter* aWriter, const nsNSSShutDownPreventionLock& /*proof*/) const { ScopedCERTCertificateList certs(CERT_CertListFromCert(mCertificate.get())); if (!certs || certs->len <= 0) { return false; } if (!JS_WriteUint32Pair(aWriter, certs->certs[0].len, 0)) { return false; } return JS_WriteBytes(aWriter, certs->certs[0].data, certs->certs[0].len); }
bool URLParams::WriteStructuredClone(JSStructuredCloneWriter* aWriter) const { const uint32_t& nParams = mParams.Length(); if (!JS_WriteUint32Pair(aWriter, nParams, 0)) { return false; } for (uint32_t i = 0; i < nParams; ++i) { if (!WriteString(aWriter, mParams[i].mKey) || !WriteString(aWriter, mParams[i].mValue)) { return false; } } return true; }
bool LabeledBlob::WriteStructuredClone(JSContext* cx, JSStructuredCloneWriter* writer) { nsresult rv; nsCOMPtr<nsILabeledBlobService> ilbs(do_GetService(LABELEDBLOBSERVICE_CID, &rv)); if (NS_FAILED(rv)) { return false; } nsLabeledBlobService* lbs = static_cast<nsLabeledBlobService*>(ilbs.get()); if (!lbs) { return false; } if (JS_WriteUint32Pair(writer, SCTAG_DOM_LABELEDBLOB, lbs->mLabeledBlobList.Length())) { lbs->mLabeledBlobList.AppendElement(this); return true; } return false; }
static bool GetDataStoresStructuredCloneCallbacksWrite(JSContext* aCx, JSStructuredCloneWriter* aWriter, JS::Handle<JSObject*> aObj, void* aClosure) { AssertIsOnMainThread(); PromiseWorkerProxy* proxy = static_cast<PromiseWorkerProxy*>(aClosure); NS_ASSERTION(proxy, "must have proxy!"); if (!JS_WriteUint32Pair(aWriter, WORKER_DATA_STORES_TAG, 0)) { MOZ_ASSERT(false, "cannot write pair for WORKER_DATA_STORES_TAG!"); return false; } JS::Rooted<JSObject*> storeObj(aCx, aObj); DataStore* store = nullptr; nsresult rv = UNWRAP_OBJECT(DataStore, storeObj, store); if (NS_FAILED(rv)) { MOZ_ASSERT(false, "cannot unwrap the DataStore object!"); return false; } // We keep the data store alive here. proxy->StoreISupports(store); // Construct the nsMainThreadPtrHolder pointing to the data store. nsMainThreadPtrHolder<DataStore>* dataStoreholder = new nsMainThreadPtrHolder<DataStore>(store); // And write the dataStoreholder into the buffer. if (!JS_WriteBytes(aWriter, &dataStoreholder, sizeof(dataStoreholder))) { MOZ_ASSERT(false, "cannot write bytes for dataStoreholder!"); return false; } return true; }