2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-04-25 06:30:54 +04:00
|
|
|
|
|
|
|
#include "nsStructuredCloneContainer.h"
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
2014-08-05 20:18:30 +04:00
|
|
|
#include "nsIGlobalObject.h"
|
2011-04-25 06:30:54 +04:00
|
|
|
#include "nsIVariant.h"
|
2013-03-17 11:55:15 +04:00
|
|
|
#include "nsIXPConnect.h"
|
2011-04-25 06:30:54 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsContentUtils.h"
|
2013-08-18 02:50:18 +04:00
|
|
|
#include "jsapi.h"
|
2014-08-05 20:18:30 +04:00
|
|
|
#include "xpcpublic.h"
|
2011-12-28 12:13:38 +04:00
|
|
|
|
|
|
|
#include "mozilla/Base64.h"
|
2014-08-05 20:18:30 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2011-12-28 12:13:38 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2015-08-26 15:17:23 +03:00
|
|
|
using namespace mozilla::dom;
|
2011-04-25 06:30:54 +04:00
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsStructuredCloneContainer)
|
|
|
|
NS_IMPL_RELEASE(nsStructuredCloneContainer)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsStructuredCloneContainer)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStructuredCloneContainer)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
nsStructuredCloneContainer::nsStructuredCloneContainer() : mVersion(0) {}
|
|
|
|
|
2020-02-21 13:41:47 +03:00
|
|
|
nsStructuredCloneContainer::~nsStructuredCloneContainer() = default;
|
2011-04-25 06:30:54 +04:00
|
|
|
|
2015-08-26 15:17:23 +03:00
|
|
|
NS_IMETHODIMP
|
2015-04-16 20:22:15 +03:00
|
|
|
nsStructuredCloneContainer::InitFromJSVal(JS::Handle<JS::Value> aData,
|
|
|
|
JSContext* aCx) {
|
2015-09-09 10:11:38 +03:00
|
|
|
if (DataLength()) {
|
2011-04-25 06:30:54 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-08-26 15:17:23 +03:00
|
|
|
ErrorResult rv;
|
2015-09-02 19:20:30 +03:00
|
|
|
Write(aCx, aData, rv);
|
2015-08-26 15:17:23 +03:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
2020-02-14 18:25:59 +03:00
|
|
|
// XXX propagate the error message as well.
|
|
|
|
// We cannot StealNSResult because we threw a DOM exception.
|
|
|
|
rv.SuppressException();
|
|
|
|
return NS_ERROR_DOM_DATA_CLONE_ERR;
|
2015-08-26 15:17:23 +03:00
|
|
|
}
|
2011-05-17 00:36:12 +04:00
|
|
|
|
2015-09-09 10:11:38 +03:00
|
|
|
mVersion = JS_STRUCTURED_CLONE_VERSION;
|
2011-04-25 06:30:54 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-26 15:17:23 +03:00
|
|
|
NS_IMETHODIMP
|
2011-04-25 06:30:54 +04:00
|
|
|
nsStructuredCloneContainer::InitFromBase64(const nsAString& aData,
|
2016-02-23 23:02:32 +03:00
|
|
|
uint32_t aFormatVersion) {
|
2015-09-09 10:11:38 +03:00
|
|
|
if (DataLength()) {
|
2015-08-26 15:17:23 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2011-04-25 06:30:54 +04:00
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 data(aData);
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString binaryData;
|
2011-12-28 12:13:38 +04:00
|
|
|
nsresult rv = Base64Decode(data, binaryData);
|
2011-04-25 06:30:54 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-09-09 10:11:38 +03:00
|
|
|
if (!CopyExternalData(binaryData.get(), binaryData.Length())) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2011-04-25 06:30:54 +04:00
|
|
|
|
|
|
|
mVersion = aFormatVersion;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-30 22:44:14 +03:00
|
|
|
nsresult nsStructuredCloneContainer::DeserializeToJsval(
|
|
|
|
JSContext* aCx, JS::MutableHandle<JS::Value> aValue) {
|
|
|
|
aValue.setNull();
|
2013-05-04 11:52:57 +04:00
|
|
|
JS::Rooted<JS::Value> jsStateObj(aCx);
|
2015-08-26 15:17:23 +03:00
|
|
|
|
2015-09-09 10:11:38 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
Read(aCx, &jsStateObj, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
2020-02-14 18:25:59 +03:00
|
|
|
// XXX propagate the error message as well.
|
|
|
|
// We cannot StealNSResult because we threw a DOM exception.
|
|
|
|
rv.SuppressException();
|
|
|
|
return NS_ERROR_DOM_DATA_CLONE_ERR;
|
2015-08-26 15:17:23 +03:00
|
|
|
}
|
2011-04-25 06:30:54 +04:00
|
|
|
|
2015-07-30 22:44:14 +03:00
|
|
|
aValue.set(jsStateObj);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-26 15:17:23 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStructuredCloneContainer::DeserializeToVariant(JSContext* aCx,
|
|
|
|
nsIVariant** aData) {
|
2015-07-30 22:44:14 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aData);
|
|
|
|
*aData = nullptr;
|
|
|
|
|
2015-09-09 10:11:38 +03:00
|
|
|
if (!DataLength()) {
|
2015-08-26 15:17:23 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-07-30 22:44:14 +03:00
|
|
|
// Deserialize to a JS::Value.
|
|
|
|
JS::Rooted<JS::Value> jsStateObj(aCx);
|
|
|
|
nsresult rv = DeserializeToJsval(aCx, &jsStateObj);
|
2015-08-26 15:17:23 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2015-07-30 22:44:14 +03:00
|
|
|
|
2013-04-12 02:52:10 +04:00
|
|
|
// Now wrap the JS::Value as an nsIVariant.
|
2011-04-25 06:30:54 +04:00
|
|
|
nsCOMPtr<nsIVariant> varStateObj;
|
2018-09-06 17:01:58 +03:00
|
|
|
nsCOMPtr<nsIXPConnect> xpconnect = nsIXPConnect::XPConnect();
|
2011-04-25 06:30:54 +04:00
|
|
|
NS_ENSURE_STATE(xpconnect);
|
2014-01-09 21:39:36 +04:00
|
|
|
xpconnect->JSValToVariant(aCx, jsStateObj, getter_AddRefs(varStateObj));
|
2011-04-25 06:30:54 +04:00
|
|
|
NS_ENSURE_STATE(varStateObj);
|
|
|
|
|
2015-08-26 15:17:23 +03:00
|
|
|
varStateObj.forget(aData);
|
2011-04-25 06:30:54 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-26 15:17:23 +03:00
|
|
|
NS_IMETHODIMP
|
2011-04-25 06:30:54 +04:00
|
|
|
nsStructuredCloneContainer::GetDataAsBase64(nsAString& aOut) {
|
|
|
|
aOut.Truncate();
|
|
|
|
|
2015-09-09 10:11:38 +03:00
|
|
|
if (!DataLength()) {
|
2015-08-26 15:17:23 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-09-09 10:11:38 +03:00
|
|
|
if (HasClonedDOMObjects()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2015-08-26 15:17:23 +03:00
|
|
|
}
|
|
|
|
|
2018-03-16 02:56:09 +03:00
|
|
|
auto iter = Data().Start();
|
2016-04-22 13:04:20 +03:00
|
|
|
size_t size = Data().Size();
|
|
|
|
nsAutoCString binaryData;
|
|
|
|
binaryData.SetLength(size);
|
2019-03-27 23:41:17 +03:00
|
|
|
if (!Data().ReadBytes(iter, binaryData.BeginWriting(), size)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString base64Data;
|
2011-12-28 12:13:38 +04:00
|
|
|
nsresult rv = Base64Encode(binaryData, base64Data);
|
2015-08-26 15:17:23 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2011-04-25 06:30:54 +04:00
|
|
|
|
2017-08-29 10:14:41 +03:00
|
|
|
if (!CopyASCIItoUTF16(base64Data, aOut, fallible)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2011-04-25 06:30:54 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-26 15:17:23 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStructuredCloneContainer::GetSerializedNBytes(uint64_t* aSize) {
|
2011-04-25 06:30:54 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aSize);
|
|
|
|
|
2015-09-09 10:11:38 +03:00
|
|
|
if (!DataLength()) {
|
2015-08-26 15:17:23 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-09-09 10:11:38 +03:00
|
|
|
*aSize = DataLength();
|
2011-04-25 06:30:54 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-26 15:17:23 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStructuredCloneContainer::GetFormatVersion(uint32_t* aFormatVersion) {
|
2011-04-25 06:30:54 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aFormatVersion);
|
2015-08-26 15:17:23 +03:00
|
|
|
|
2015-09-09 10:11:38 +03:00
|
|
|
if (!DataLength()) {
|
2015-08-26 15:17:23 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-04-25 06:30:54 +04:00
|
|
|
*aFormatVersion = mVersion;
|
|
|
|
return NS_OK;
|
|
|
|
}
|