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: */
|
2012-03-31 08:42:20 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
|
|
|
|
2013-08-14 10:56:21 +04:00
|
|
|
#include "BindingUtils.h"
|
|
|
|
|
2012-11-07 03:23:14 +04:00
|
|
|
#include <algorithm>
|
2012-07-18 14:36:08 +04:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2012-12-15 03:58:45 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2013-05-02 22:38:19 +04:00
|
|
|
#include "mozilla/FloatingPoint.h"
|
2013-06-13 09:18:35 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2014-05-09 01:16:00 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2015-04-07 05:22:03 +03:00
|
|
|
#include "mozilla/unused.h"
|
2015-02-05 20:53:01 +03:00
|
|
|
#include "mozilla/UseCounter.h"
|
2012-12-15 03:58:45 +04:00
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
#include "AccessCheck.h"
|
2013-06-01 10:56:23 +04:00
|
|
|
#include "jsfriendapi.h"
|
2014-02-05 22:38:17 +04:00
|
|
|
#include "nsContentUtils.h"
|
2014-05-25 22:31:07 +04:00
|
|
|
#include "nsGlobalWindow.h"
|
2015-01-09 02:06:33 +03:00
|
|
|
#include "nsIDocShell.h"
|
2013-06-01 10:56:23 +04:00
|
|
|
#include "nsIDOMGlobalPropertyInitializer.h"
|
2014-05-25 22:31:07 +04:00
|
|
|
#include "nsIPermissionManager.h"
|
2014-02-05 22:38:17 +04:00
|
|
|
#include "nsIPrincipal.h"
|
2012-09-26 18:17:46 +04:00
|
|
|
#include "nsIXPConnect.h"
|
2014-08-02 00:23:48 +04:00
|
|
|
#include "nsUTF8Utils.h"
|
2012-05-22 17:46:20 +04:00
|
|
|
#include "WrapperFactory.h"
|
2012-05-18 12:29:40 +04:00
|
|
|
#include "xpcprivate.h"
|
2012-09-26 18:17:46 +04:00
|
|
|
#include "XrayWrapper.h"
|
2013-07-04 17:24:49 +04:00
|
|
|
#include "nsPrintfCString.h"
|
2016-02-17 22:23:39 +03:00
|
|
|
#include "mozilla/Snprintf.h"
|
2015-02-05 20:53:01 +03:00
|
|
|
#include "nsGlobalWindow.h"
|
2012-05-18 12:29:40 +04:00
|
|
|
|
2013-12-12 05:51:58 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2013-09-03 16:01:53 +04:00
|
|
|
#include "mozilla/dom/DOMError.h"
|
|
|
|
#include "mozilla/dom/DOMErrorBinding.h"
|
2015-10-09 23:48:10 +03:00
|
|
|
#include "mozilla/dom/DOMException.h"
|
2014-09-15 18:51:40 +04:00
|
|
|
#include "mozilla/dom/ElementBinding.h"
|
2013-01-07 03:55:14 +04:00
|
|
|
#include "mozilla/dom/HTMLObjectElement.h"
|
|
|
|
#include "mozilla/dom/HTMLObjectElementBinding.h"
|
2013-03-17 18:42:05 +04:00
|
|
|
#include "mozilla/dom/HTMLSharedObjectElement.h"
|
|
|
|
#include "mozilla/dom/HTMLEmbedElementBinding.h"
|
|
|
|
#include "mozilla/dom/HTMLAppletElementBinding.h"
|
2014-03-21 20:18:24 +04:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2015-03-06 09:08:06 +03:00
|
|
|
#include "mozilla/dom/ResolveSystemBinding.h"
|
2016-02-11 07:31:33 +03:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
|
|
|
#include "mozilla/dom/WorkerScope.h"
|
2015-01-27 00:32:18 +03:00
|
|
|
#include "mozilla/jsipc/CrossProcessObjectWrappers.h"
|
2014-08-08 17:03:29 +04:00
|
|
|
#include "nsDOMClassInfo.h"
|
2015-04-07 04:36:55 +03:00
|
|
|
#include "ipc/ErrorIPCUtils.h"
|
2015-02-05 20:53:01 +03:00
|
|
|
#include "mozilla/UseCounter.h"
|
2013-01-07 03:55:14 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2016-03-11 05:05:10 +03:00
|
|
|
const JSErrorFormatString ErrorFormatString[] = {
|
2015-02-04 14:46:26 +03:00
|
|
|
#define MSG_DEF(_name, _argc, _exn, _str) \
|
2016-03-04 23:21:57 +03:00
|
|
|
{ #_name, _str, _argc, _exn },
|
2012-07-18 14:36:08 +04:00
|
|
|
#include "mozilla/dom/Errors.msg"
|
|
|
|
#undef MSG_DEF
|
|
|
|
};
|
|
|
|
|
2015-09-15 21:47:04 +03:00
|
|
|
#define MSG_DEF(_name, _argc, _exn, _str) \
|
|
|
|
static_assert(_argc < JS::MaxNumErrorArguments, \
|
|
|
|
#_name " must only have as many error arguments as the JS engine can support");
|
|
|
|
#include "mozilla/dom/Errors.msg"
|
|
|
|
#undef MSG_DEF
|
|
|
|
|
2012-07-18 14:36:08 +04:00
|
|
|
const JSErrorFormatString*
|
2014-07-15 17:14:38 +04:00
|
|
|
GetErrorMessage(void* aUserRef, const unsigned aErrorNumber)
|
2012-07-18 14:36:08 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aErrorNumber < ArrayLength(ErrorFormatString));
|
|
|
|
return &ErrorFormatString[aErrorNumber];
|
|
|
|
}
|
|
|
|
|
2015-09-15 21:47:04 +03:00
|
|
|
uint16_t
|
|
|
|
GetErrorArgCount(const ErrNum aErrorNumber)
|
|
|
|
{
|
|
|
|
return GetErrorMessage(nullptr, aErrorNumber)->argCount;
|
|
|
|
}
|
|
|
|
|
2012-07-18 14:36:08 +04:00
|
|
|
bool
|
|
|
|
ThrowErrorMessage(JSContext* aCx, const ErrNum aErrorNumber, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, aErrorNumber);
|
2013-06-17 21:07:03 +04:00
|
|
|
JS_ReportErrorNumberVA(aCx, GetErrorMessage, nullptr,
|
2012-07-18 14:36:08 +04:00
|
|
|
static_cast<const unsigned>(aErrorNumber), ap);
|
|
|
|
va_end(ap);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-17 21:07:03 +04:00
|
|
|
bool
|
2013-06-18 00:31:13 +04:00
|
|
|
ThrowInvalidThis(JSContext* aCx, const JS::CallArgs& aArgs,
|
2016-01-06 11:54:00 +03:00
|
|
|
bool aSecurityError, const char* aInterfaceName)
|
2013-06-17 21:07:03 +04:00
|
|
|
{
|
|
|
|
NS_ConvertASCIItoUTF16 ifaceName(aInterfaceName);
|
2013-06-18 00:31:13 +04:00
|
|
|
// This should only be called for DOM methods/getters/setters, which
|
|
|
|
// are JSNative-backed functions, so we can assume that
|
|
|
|
// JS_ValueToFunction and JS_GetFunctionDisplayId will both return
|
|
|
|
// non-null and that JS_GetStringCharsZ returns non-null.
|
2013-06-17 21:07:03 +04:00
|
|
|
JS::Rooted<JSFunction*> func(aCx, JS_ValueToFunction(aCx, aArgs.calleev()));
|
|
|
|
MOZ_ASSERT(func);
|
|
|
|
JS::Rooted<JSString*> funcName(aCx, JS_GetFunctionDisplayId(func));
|
|
|
|
MOZ_ASSERT(funcName);
|
2014-07-12 11:43:06 +04:00
|
|
|
nsAutoJSString funcNameStr;
|
|
|
|
if (!funcNameStr.init(aCx, funcName)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-06 11:54:00 +03:00
|
|
|
const ErrNum errorNumber = aSecurityError ?
|
|
|
|
MSG_METHOD_THIS_UNWRAPPING_DENIED :
|
|
|
|
MSG_METHOD_THIS_DOES_NOT_IMPLEMENT_INTERFACE;
|
|
|
|
MOZ_RELEASE_ASSERT(GetErrorArgCount(errorNumber) <= 2);
|
2013-06-17 21:07:03 +04:00
|
|
|
JS_ReportErrorNumberUC(aCx, GetErrorMessage, nullptr,
|
2016-01-06 11:54:00 +03:00
|
|
|
static_cast<const unsigned>(errorNumber),
|
2014-07-12 11:43:06 +04:00
|
|
|
funcNameStr.get(), ifaceName.get());
|
2013-06-17 21:07:03 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-08 00:56:14 +04:00
|
|
|
bool
|
|
|
|
ThrowInvalidThis(JSContext* aCx, const JS::CallArgs& aArgs,
|
2016-01-06 11:54:00 +03:00
|
|
|
bool aSecurityError,
|
2014-02-08 00:56:14 +04:00
|
|
|
prototypes::ID aProtoId)
|
|
|
|
{
|
2016-01-06 11:54:00 +03:00
|
|
|
return ThrowInvalidThis(aCx, aArgs, aSecurityError,
|
2014-02-10 19:24:38 +04:00
|
|
|
NamesOfInterfacesWithProtos(aProtoId));
|
2014-02-08 00:56:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ThrowNoSetterArg(JSContext* aCx, prototypes::ID aProtoId)
|
|
|
|
{
|
|
|
|
nsPrintfCString errorMessage("%s attribute setter",
|
2014-02-10 19:24:38 +04:00
|
|
|
NamesOfInterfacesWithProtos(aProtoId));
|
2014-02-08 00:56:14 +04:00
|
|
|
return ThrowErrorMessage(aCx, MSG_MISSING_ARGUMENTS, errorMessage.get());
|
|
|
|
}
|
|
|
|
|
2012-11-07 03:23:14 +04:00
|
|
|
} // namespace dom
|
|
|
|
|
|
|
|
struct ErrorResult::Message {
|
2015-10-26 16:50:00 +03:00
|
|
|
Message() { MOZ_COUNT_CTOR(ErrorResult::Message); }
|
|
|
|
~Message() { MOZ_COUNT_DTOR(ErrorResult::Message); }
|
|
|
|
|
2012-11-07 03:23:14 +04:00
|
|
|
nsTArray<nsString> mArgs;
|
|
|
|
dom::ErrNum mErrorNumber;
|
2015-09-15 21:47:04 +03:00
|
|
|
|
|
|
|
bool HasCorrectNumberOfArguments()
|
|
|
|
{
|
|
|
|
return GetErrorArgCount(mErrorNumber) == mArgs.Length();
|
|
|
|
}
|
2012-11-07 03:23:14 +04:00
|
|
|
};
|
|
|
|
|
2015-09-15 21:47:04 +03:00
|
|
|
nsTArray<nsString>&
|
|
|
|
ErrorResult::CreateErrorMessageHelper(const dom::ErrNum errorNumber, nsresult errorType)
|
|
|
|
{
|
|
|
|
mResult = errorType;
|
|
|
|
|
|
|
|
mMessage = new Message();
|
|
|
|
mMessage->mErrorNumber = errorNumber;
|
|
|
|
return mMessage->mArgs;
|
|
|
|
}
|
|
|
|
|
2015-04-07 04:36:55 +03:00
|
|
|
void
|
|
|
|
ErrorResult::SerializeMessage(IPC::Message* aMsg) const
|
|
|
|
{
|
|
|
|
using namespace IPC;
|
2015-10-09 23:48:10 +03:00
|
|
|
MOZ_ASSERT(mUnionState == HasMessage);
|
2015-04-07 04:36:55 +03:00
|
|
|
MOZ_ASSERT(mMessage);
|
|
|
|
WriteParam(aMsg, mMessage->mArgs);
|
|
|
|
WriteParam(aMsg, mMessage->mErrorNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ErrorResult::DeserializeMessage(const IPC::Message* aMsg, void** aIter)
|
|
|
|
{
|
|
|
|
using namespace IPC;
|
|
|
|
nsAutoPtr<Message> readMessage(new Message());
|
|
|
|
if (!ReadParam(aMsg, aIter, &readMessage->mArgs) ||
|
|
|
|
!ReadParam(aMsg, aIter, &readMessage->mErrorNumber)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-15 21:47:04 +03:00
|
|
|
if (!readMessage->HasCorrectNumberOfArguments()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-09 23:48:10 +03:00
|
|
|
MOZ_ASSERT(mUnionState == HasNothing);
|
2015-04-07 04:36:55 +03:00
|
|
|
mMessage = readMessage.forget();
|
2015-04-15 21:09:28 +03:00
|
|
|
#ifdef DEBUG
|
2015-10-09 23:48:10 +03:00
|
|
|
mUnionState = HasMessage;
|
|
|
|
#endif // DEBUG
|
2015-04-07 04:36:55 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-07 03:23:14 +04:00
|
|
|
void
|
2015-11-21 00:29:41 +03:00
|
|
|
ErrorResult::SetPendingExceptionWithMessage(JSContext* aCx)
|
2012-11-07 03:23:14 +04:00
|
|
|
{
|
2015-11-21 00:29:41 +03:00
|
|
|
MOZ_ASSERT(mMessage, "SetPendingExceptionWithMessage() can be called only once");
|
2015-10-09 23:48:10 +03:00
|
|
|
MOZ_ASSERT(mUnionState == HasMessage);
|
2012-11-07 03:23:14 +04:00
|
|
|
|
|
|
|
Message* message = mMessage;
|
2015-09-15 21:47:04 +03:00
|
|
|
MOZ_RELEASE_ASSERT(message->HasCorrectNumberOfArguments());
|
2012-11-07 03:23:14 +04:00
|
|
|
const uint32_t argCount = message->mArgs.Length();
|
2015-09-15 21:47:04 +03:00
|
|
|
const char16_t* args[JS::MaxNumErrorArguments + 1];
|
2012-11-07 03:23:14 +04:00
|
|
|
for (uint32_t i = 0; i < argCount; ++i) {
|
|
|
|
args[i] = message->mArgs.ElementAt(i).get();
|
|
|
|
}
|
|
|
|
args[argCount] = nullptr;
|
|
|
|
|
|
|
|
JS_ReportErrorNumberUCArray(aCx, dom::GetErrorMessage, nullptr,
|
|
|
|
static_cast<const unsigned>(message->mErrorNumber),
|
|
|
|
argCount > 0 ? args : nullptr);
|
|
|
|
|
2013-02-19 17:30:28 +04:00
|
|
|
ClearMessage();
|
2015-11-21 00:29:41 +03:00
|
|
|
mResult = NS_OK;
|
2013-02-19 17:30:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ErrorResult::ClearMessage()
|
|
|
|
{
|
2015-04-27 05:38:17 +03:00
|
|
|
MOZ_ASSERT(IsErrorWithMessage());
|
|
|
|
delete mMessage;
|
|
|
|
mMessage = nullptr;
|
2015-04-15 21:09:28 +03:00
|
|
|
#ifdef DEBUG
|
2015-10-09 23:48:10 +03:00
|
|
|
mUnionState = HasNothing;
|
|
|
|
#endif // DEBUG
|
2012-11-07 03:23:14 +04:00
|
|
|
}
|
|
|
|
|
2013-02-19 20:54:40 +04:00
|
|
|
void
|
2013-04-11 22:31:06 +04:00
|
|
|
ErrorResult::ThrowJSException(JSContext* cx, JS::Handle<JS::Value> exn)
|
2013-02-19 20:54:40 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mMightHaveUnreportedJSException,
|
|
|
|
"Why didn't you tell us you planned to throw a JS exception?");
|
|
|
|
|
2015-10-09 23:48:10 +03:00
|
|
|
ClearUnionData();
|
2013-02-19 20:54:40 +04:00
|
|
|
|
2013-04-09 01:05:22 +04:00
|
|
|
// Make sure mJSException is initialized _before_ we try to root it. But
|
|
|
|
// don't set it to exn yet, because we don't want to do that until after we
|
|
|
|
// root.
|
2015-01-14 10:59:06 +03:00
|
|
|
mJSException.setUndefined();
|
2014-04-16 12:47:57 +04:00
|
|
|
if (!js::AddRawValueRoot(cx, &mJSException, "ErrorResult::mJSException")) {
|
2013-02-19 20:54:40 +04:00
|
|
|
// Don't use NS_ERROR_DOM_JS_EXCEPTION, because that indicates we have
|
|
|
|
// in fact rooted mJSException.
|
|
|
|
mResult = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
} else {
|
|
|
|
mJSException = exn;
|
|
|
|
mResult = NS_ERROR_DOM_JS_EXCEPTION;
|
2015-10-09 23:48:10 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
mUnionState = HasJSException;
|
|
|
|
#endif // DEBUG
|
2013-02-19 20:54:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-11-21 00:29:41 +03:00
|
|
|
ErrorResult::SetPendingJSException(JSContext* cx)
|
2013-02-19 20:54:40 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mMightHaveUnreportedJSException,
|
|
|
|
"Why didn't you tell us you planned to handle JS exceptions?");
|
2015-10-09 23:48:10 +03:00
|
|
|
MOZ_ASSERT(mUnionState == HasJSException);
|
2013-10-26 20:19:05 +04:00
|
|
|
|
|
|
|
JS::Rooted<JS::Value> exception(cx, mJSException);
|
|
|
|
if (JS_WrapValue(cx, &exception)) {
|
2013-09-19 11:54:01 +04:00
|
|
|
JS_SetPendingException(cx, exception);
|
2013-02-19 20:54:40 +04:00
|
|
|
}
|
2013-10-26 20:19:05 +04:00
|
|
|
mJSException = exception;
|
2013-02-19 20:54:40 +04:00
|
|
|
// If JS_WrapValue failed, not much we can do about it... No matter
|
|
|
|
// what, go ahead and unroot mJSException.
|
2014-04-16 12:47:57 +04:00
|
|
|
js::RemoveRawValueRoot(cx, &mJSException);
|
2015-01-03 01:08:33 +03:00
|
|
|
|
2015-11-21 00:29:41 +03:00
|
|
|
mResult = NS_OK;
|
2015-10-09 23:48:10 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
mUnionState = HasNothing;
|
|
|
|
#endif // DEBUG
|
2013-02-19 20:54:40 +04:00
|
|
|
}
|
|
|
|
|
2015-10-09 23:48:10 +03:00
|
|
|
struct ErrorResult::DOMExceptionInfo {
|
|
|
|
DOMExceptionInfo(nsresult rv, const nsACString& message)
|
|
|
|
: mMessage(message)
|
|
|
|
, mRv(rv)
|
|
|
|
{}
|
|
|
|
|
|
|
|
nsCString mMessage;
|
|
|
|
nsresult mRv;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
ErrorResult::SerializeDOMExceptionInfo(IPC::Message* aMsg) const
|
|
|
|
{
|
|
|
|
using namespace IPC;
|
|
|
|
MOZ_ASSERT(mDOMExceptionInfo);
|
2015-10-09 23:48:10 +03:00
|
|
|
MOZ_ASSERT(mUnionState == HasDOMExceptionInfo);
|
2015-10-09 23:48:10 +03:00
|
|
|
WriteParam(aMsg, mDOMExceptionInfo->mMessage);
|
|
|
|
WriteParam(aMsg, mDOMExceptionInfo->mRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ErrorResult::DeserializeDOMExceptionInfo(const IPC::Message* aMsg, void** aIter)
|
|
|
|
{
|
|
|
|
using namespace IPC;
|
|
|
|
nsCString message;
|
|
|
|
nsresult rv;
|
|
|
|
if (!ReadParam(aMsg, aIter, &message) ||
|
|
|
|
!ReadParam(aMsg, aIter, &rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-09 23:48:10 +03:00
|
|
|
MOZ_ASSERT(mUnionState == HasNothing);
|
2015-10-09 23:48:10 +03:00
|
|
|
MOZ_ASSERT(IsDOMException());
|
|
|
|
mDOMExceptionInfo = new DOMExceptionInfo(rv, message);
|
|
|
|
#ifdef DEBUG
|
2015-10-09 23:48:10 +03:00
|
|
|
mUnionState = HasDOMExceptionInfo;
|
|
|
|
#endif // DEBUG
|
2015-10-09 23:48:10 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ErrorResult::ThrowDOMException(nsresult rv, const nsACString& message)
|
|
|
|
{
|
|
|
|
ClearUnionData();
|
|
|
|
|
|
|
|
mResult = NS_ERROR_DOM_DOMEXCEPTION;
|
|
|
|
mDOMExceptionInfo = new DOMExceptionInfo(rv, message);
|
|
|
|
#ifdef DEBUG
|
2015-10-09 23:48:10 +03:00
|
|
|
mUnionState = HasDOMExceptionInfo;
|
2015-10-09 23:48:10 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-11-21 00:29:41 +03:00
|
|
|
ErrorResult::SetPendingDOMException(JSContext* cx)
|
2015-10-09 23:48:10 +03:00
|
|
|
{
|
2015-11-21 00:29:41 +03:00
|
|
|
MOZ_ASSERT(mDOMExceptionInfo,
|
|
|
|
"SetPendingDOMException() can be called only once");
|
2015-10-09 23:48:10 +03:00
|
|
|
MOZ_ASSERT(mUnionState == HasDOMExceptionInfo);
|
2015-10-09 23:48:10 +03:00
|
|
|
|
|
|
|
dom::Throw(cx, mDOMExceptionInfo->mRv, mDOMExceptionInfo->mMessage);
|
|
|
|
|
|
|
|
ClearDOMExceptionInfo();
|
2015-11-21 00:29:41 +03:00
|
|
|
mResult = NS_OK;
|
2015-10-09 23:48:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ErrorResult::ClearDOMExceptionInfo()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsDOMException());
|
2015-10-09 23:48:10 +03:00
|
|
|
MOZ_ASSERT(mUnionState == HasDOMExceptionInfo || !mDOMExceptionInfo);
|
2015-10-09 23:48:10 +03:00
|
|
|
delete mDOMExceptionInfo;
|
|
|
|
mDOMExceptionInfo = nullptr;
|
|
|
|
#ifdef DEBUG
|
2015-10-09 23:48:10 +03:00
|
|
|
mUnionState = HasNothing;
|
|
|
|
#endif // DEBUG
|
2015-10-09 23:48:10 +03:00
|
|
|
}
|
|
|
|
|
2015-10-09 23:48:10 +03:00
|
|
|
void
|
|
|
|
ErrorResult::ClearUnionData()
|
|
|
|
{
|
|
|
|
if (IsJSException()) {
|
|
|
|
JSContext* cx = nsContentUtils::GetDefaultJSContextForThread();
|
|
|
|
MOZ_ASSERT(cx);
|
|
|
|
mJSException.setUndefined();
|
|
|
|
js::RemoveRawValueRoot(cx, &mJSException);
|
2015-10-09 23:48:10 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
mUnionState = HasNothing;
|
|
|
|
#endif // DEBUG
|
2015-10-09 23:48:10 +03:00
|
|
|
} else if (IsErrorWithMessage()) {
|
|
|
|
ClearMessage();
|
2015-10-09 23:48:10 +03:00
|
|
|
} else if (IsDOMException()) {
|
|
|
|
ClearDOMExceptionInfo();
|
2015-10-09 23:48:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-27 16:18:52 +03:00
|
|
|
void
|
2015-11-21 00:29:41 +03:00
|
|
|
ErrorResult::SetPendingGenericErrorException(JSContext* cx)
|
2015-04-27 16:18:52 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!IsErrorWithMessage());
|
|
|
|
MOZ_ASSERT(!IsJSException());
|
2015-10-09 23:48:10 +03:00
|
|
|
MOZ_ASSERT(!IsDOMException());
|
2015-04-27 16:18:52 +03:00
|
|
|
dom::Throw(cx, ErrorCode());
|
2015-11-21 00:29:41 +03:00
|
|
|
mResult = NS_OK;
|
2013-07-04 17:24:49 +04:00
|
|
|
}
|
|
|
|
|
2015-04-07 05:22:03 +03:00
|
|
|
ErrorResult&
|
|
|
|
ErrorResult::operator=(ErrorResult&& aRHS)
|
|
|
|
{
|
2015-10-09 23:48:10 +03:00
|
|
|
// Clear out any union members we may have right now, before we
|
|
|
|
// start writing to it.
|
|
|
|
ClearUnionData();
|
|
|
|
|
2015-04-07 05:22:03 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
mMightHaveUnreportedJSException = aRHS.mMightHaveUnreportedJSException;
|
|
|
|
aRHS.mMightHaveUnreportedJSException = false;
|
|
|
|
#endif
|
|
|
|
if (aRHS.IsErrorWithMessage()) {
|
|
|
|
mMessage = aRHS.mMessage;
|
|
|
|
aRHS.mMessage = nullptr;
|
|
|
|
} else if (aRHS.IsJSException()) {
|
|
|
|
JSContext* cx = nsContentUtils::GetDefaultJSContextForThread();
|
|
|
|
MOZ_ASSERT(cx);
|
|
|
|
mJSException.setUndefined();
|
|
|
|
if (!js::AddRawValueRoot(cx, &mJSException, "ErrorResult::mJSException")) {
|
|
|
|
MOZ_CRASH("Could not root mJSException, we're about to OOM");
|
|
|
|
}
|
|
|
|
mJSException = aRHS.mJSException;
|
|
|
|
aRHS.mJSException.setUndefined();
|
|
|
|
js::RemoveRawValueRoot(cx, &aRHS.mJSException);
|
2015-10-09 23:48:10 +03:00
|
|
|
} else if (aRHS.IsDOMException()) {
|
|
|
|
mDOMExceptionInfo = aRHS.mDOMExceptionInfo;
|
|
|
|
aRHS.mDOMExceptionInfo = nullptr;
|
2015-04-07 05:22:03 +03:00
|
|
|
} else {
|
|
|
|
// Null out the union on both sides for hygiene purposes.
|
|
|
|
mMessage = aRHS.mMessage = nullptr;
|
|
|
|
}
|
2015-10-09 23:48:10 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mUnionState = aRHS.mUnionState;
|
|
|
|
aRHS.mUnionState = HasNothing;
|
|
|
|
#endif // DEBUG
|
|
|
|
|
2015-04-07 05:22:03 +03:00
|
|
|
// Note: It's important to do this last, since this affects the condition
|
|
|
|
// checks above!
|
|
|
|
mResult = aRHS.mResult;
|
|
|
|
aRHS.mResult = NS_OK;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-11-18 22:07:58 +03:00
|
|
|
void
|
|
|
|
ErrorResult::CloneTo(ErrorResult& aRv) const
|
|
|
|
{
|
|
|
|
aRv.ClearUnionData();
|
|
|
|
aRv.mResult = mResult;
|
|
|
|
#ifdef DEBUG
|
|
|
|
aRv.mMightHaveUnreportedJSException = mMightHaveUnreportedJSException;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (IsErrorWithMessage()) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
aRv.mUnionState = HasMessage;
|
|
|
|
#endif
|
|
|
|
aRv.mMessage = new Message();
|
|
|
|
aRv.mMessage->mArgs = mMessage->mArgs;
|
|
|
|
aRv.mMessage->mErrorNumber = mMessage->mErrorNumber;
|
|
|
|
} else if (IsDOMException()) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
aRv.mUnionState = HasDOMExceptionInfo;
|
|
|
|
#endif
|
|
|
|
aRv.mDOMExceptionInfo = new DOMExceptionInfo(mDOMExceptionInfo->mRv,
|
|
|
|
mDOMExceptionInfo->mMessage);
|
|
|
|
} else if (IsJSException()) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
aRv.mUnionState = HasJSException;
|
|
|
|
#endif
|
|
|
|
JSContext* cx = nsContentUtils::RootingCxForThread();
|
|
|
|
JS::Rooted<JS::Value> exception(cx, mJSException);
|
|
|
|
aRv.ThrowJSException(cx, exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-27 05:38:17 +03:00
|
|
|
void
|
|
|
|
ErrorResult::SuppressException()
|
|
|
|
{
|
|
|
|
WouldReportJSException();
|
2015-10-09 23:48:10 +03:00
|
|
|
ClearUnionData();
|
2015-04-27 05:38:17 +03:00
|
|
|
// We don't use AssignErrorCode, because we want to override existing error
|
|
|
|
// states, which AssignErrorCode is not allowed to do.
|
|
|
|
mResult = NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-11-21 00:29:40 +03:00
|
|
|
void
|
|
|
|
ErrorResult::SetPendingException(JSContext* cx)
|
|
|
|
{
|
|
|
|
if (IsUncatchableException()) {
|
|
|
|
// Nuke any existing exception on cx, to make sure we're uncatchable.
|
|
|
|
JS_ClearPendingException(cx);
|
|
|
|
// Don't do any reporting. Just return, to create an
|
|
|
|
// uncatchable exception.
|
2015-11-21 00:29:41 +03:00
|
|
|
mResult = NS_OK;
|
2015-11-21 00:29:40 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (IsJSContextException()) {
|
2016-03-10 12:50:56 +03:00
|
|
|
// Whatever we need to throw is on the JSContext already.
|
|
|
|
MOZ_ASSERT(JS_IsExceptionPending(cx));
|
2015-11-21 00:29:41 +03:00
|
|
|
mResult = NS_OK;
|
2015-11-21 00:29:40 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (IsErrorWithMessage()) {
|
2015-11-21 00:29:41 +03:00
|
|
|
SetPendingExceptionWithMessage(cx);
|
2015-11-21 00:29:40 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (IsJSException()) {
|
2015-11-21 00:29:41 +03:00
|
|
|
SetPendingJSException(cx);
|
2015-11-21 00:29:40 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (IsDOMException()) {
|
2015-11-21 00:29:41 +03:00
|
|
|
SetPendingDOMException(cx);
|
2015-11-21 00:29:40 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-11-21 00:29:41 +03:00
|
|
|
SetPendingGenericErrorException(cx);
|
2015-11-21 00:29:40 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 18:38:31 +03:00
|
|
|
void
|
|
|
|
ErrorResult::StealExceptionFromJSContext(JSContext* cx)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mMightHaveUnreportedJSException,
|
|
|
|
"Why didn't you tell us you planned to throw a JS exception?");
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> exn(cx);
|
|
|
|
if (!JS_GetPendingException(cx, &exn)) {
|
|
|
|
ThrowUncatchableException();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ThrowJSException(cx, exn);
|
|
|
|
JS_ClearPendingException(cx);
|
|
|
|
}
|
|
|
|
|
2016-03-10 12:50:56 +03:00
|
|
|
void
|
|
|
|
ErrorResult::NoteJSContextException(JSContext* aCx)
|
|
|
|
{
|
|
|
|
if (JS_IsExceptionPending(aCx)) {
|
|
|
|
mResult = NS_ERROR_DOM_EXCEPTION_ON_JSCONTEXT;
|
|
|
|
} else {
|
|
|
|
mResult = NS_ERROR_UNCATCHABLE_EXCEPTION;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-07 03:23:14 +04:00
|
|
|
namespace dom {
|
|
|
|
|
2012-05-09 14:00:48 +04:00
|
|
|
bool
|
2013-05-08 06:34:56 +04:00
|
|
|
DefineConstants(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
const ConstantSpec* cs)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2014-01-22 06:11:17 +04:00
|
|
|
JS::Rooted<JS::Value> value(cx);
|
2012-03-31 08:42:20 +04:00
|
|
|
for (; cs->name; ++cs) {
|
2014-01-22 06:11:17 +04:00
|
|
|
value = cs->value;
|
2013-08-09 02:53:04 +04:00
|
|
|
bool ok =
|
2014-01-22 06:11:17 +04:00
|
|
|
JS_DefineProperty(cx, obj, cs->name, value,
|
2012-03-31 08:42:20 +04:00
|
|
|
JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
if (!ok) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-23 20:44:48 +04:00
|
|
|
static inline bool
|
2013-05-08 06:34:56 +04:00
|
|
|
Define(JSContext* cx, JS::Handle<JSObject*> obj, const JSFunctionSpec* spec) {
|
2012-05-23 20:44:48 +04:00
|
|
|
return JS_DefineFunctions(cx, obj, spec);
|
|
|
|
}
|
|
|
|
static inline bool
|
2013-05-08 06:34:56 +04:00
|
|
|
Define(JSContext* cx, JS::Handle<JSObject*> obj, const JSPropertySpec* spec) {
|
2012-05-23 20:44:48 +04:00
|
|
|
return JS_DefineProperties(cx, obj, spec);
|
|
|
|
}
|
|
|
|
static inline bool
|
2013-05-08 06:34:56 +04:00
|
|
|
Define(JSContext* cx, JS::Handle<JSObject*> obj, const ConstantSpec* spec) {
|
2012-05-23 20:44:48 +04:00
|
|
|
return DefineConstants(cx, obj, spec);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
DefinePrefable(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
const Prefable<T>* props)
|
2012-05-23 20:44:48 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(props);
|
|
|
|
MOZ_ASSERT(props->specs);
|
|
|
|
do {
|
|
|
|
// Define if enabled
|
2013-02-19 20:54:40 +04:00
|
|
|
if (props->isEnabled(cx, obj)) {
|
2012-05-23 20:44:48 +04:00
|
|
|
if (!Define(cx, obj, props->specs)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while ((++props)->specs);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-04 17:02:10 +04:00
|
|
|
bool
|
|
|
|
DefineUnforgeableMethods(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
const Prefable<const JSFunctionSpec>* props)
|
|
|
|
{
|
|
|
|
return DefinePrefable(cx, obj, props);
|
|
|
|
}
|
|
|
|
|
2012-10-25 00:10:49 +04:00
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
DefineUnforgeableAttributes(JSContext* cx, JS::Handle<JSObject*> obj,
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const JSPropertySpec>* props)
|
2012-10-25 00:10:49 +04:00
|
|
|
{
|
|
|
|
return DefinePrefable(cx, obj, props);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-02 17:28:17 +04:00
|
|
|
// We should use JSFunction objects for interface objects, but we need a custom
|
|
|
|
// hasInstance hook because we have new interface objects on prototype chains of
|
2015-10-27 23:25:14 +03:00
|
|
|
// old (XPConnect-based) bindings. We also need Xrays and arbitrary numbers of
|
|
|
|
// reserved slots (e.g. for named constructors). So we define a custom
|
|
|
|
// funToString ObjectOps member for interface objects.
|
|
|
|
JSString*
|
|
|
|
InterfaceObjectToString(JSContext* aCx, JS::Handle<JSObject*> aObject,
|
|
|
|
unsigned /* indent */)
|
2012-05-02 17:28:17 +04:00
|
|
|
{
|
2015-10-27 23:25:14 +03:00
|
|
|
const js::Class* clasp = js::GetObjectClass(aObject);
|
|
|
|
MOZ_ASSERT(IsDOMIfaceAndProtoClass(clasp));
|
2012-05-02 17:28:17 +04:00
|
|
|
|
2014-09-15 18:45:38 +04:00
|
|
|
const DOMIfaceAndProtoJSClass* ifaceAndProtoJSClass =
|
|
|
|
DOMIfaceAndProtoJSClass::FromJSClass(clasp);
|
2015-10-27 23:25:14 +03:00
|
|
|
return JS_NewStringCopyZ(aCx, ifaceAndProtoJSClass->mToString);
|
2012-05-02 17:28:17 +04:00
|
|
|
}
|
|
|
|
|
2013-08-02 11:41:57 +04:00
|
|
|
bool
|
2012-10-09 22:50:27 +04:00
|
|
|
Constructor(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
2014-03-19 21:05:04 +04:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
2012-10-09 22:50:27 +04:00
|
|
|
const JS::Value& v =
|
2014-03-19 21:05:04 +04:00
|
|
|
js::GetFunctionNativeReserved(&args.callee(),
|
2012-10-09 22:50:27 +04:00
|
|
|
CONSTRUCTOR_NATIVE_HOLDER_RESERVED_SLOT);
|
2013-01-22 14:51:15 +04:00
|
|
|
const JSNativeHolder* nativeHolder =
|
|
|
|
static_cast<const JSNativeHolder*>(v.toPrivate());
|
2012-10-09 22:50:27 +04:00
|
|
|
return (nativeHolder->mNative)(cx, argc, vp);
|
|
|
|
}
|
|
|
|
|
2013-01-22 14:51:15 +04:00
|
|
|
static JSObject*
|
2013-05-02 07:44:12 +04:00
|
|
|
CreateConstructor(JSContext* cx, JS::Handle<JSObject*> global, const char* name,
|
2013-01-22 14:51:15 +04:00
|
|
|
const JSNativeHolder* nativeHolder, unsigned ctorNargs)
|
|
|
|
{
|
|
|
|
JSFunction* fun = js::NewFunctionWithReserved(cx, Constructor, ctorNargs,
|
2015-03-09 19:50:07 +03:00
|
|
|
JSFUN_CONSTRUCTOR, name);
|
2013-01-22 14:51:15 +04:00
|
|
|
if (!fun) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* constructor = JS_GetFunctionObject(fun);
|
|
|
|
js::SetFunctionNativeReserved(constructor,
|
|
|
|
CONSTRUCTOR_NATIVE_HOLDER_RESERVED_SLOT,
|
|
|
|
js::PrivateValue(const_cast<JSNativeHolder*>(nativeHolder)));
|
|
|
|
return constructor;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-05-02 07:44:12 +04:00
|
|
|
DefineConstructor(JSContext* cx, JS::Handle<JSObject*> global, const char* name,
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Handle<JSObject*> constructor)
|
2013-01-22 14:51:15 +04:00
|
|
|
{
|
2013-08-09 02:53:04 +04:00
|
|
|
bool alreadyDefined;
|
2013-01-22 14:51:15 +04:00
|
|
|
if (!JS_AlreadyHasOwnProperty(cx, global, name, &alreadyDefined)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is Enumerable: False per spec.
|
|
|
|
return alreadyDefined ||
|
2015-05-01 22:03:17 +03:00
|
|
|
JS_DefineProperty(cx, global, name, constructor, JSPROP_RESOLVING);
|
2013-01-22 14:51:15 +04:00
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
static JSObject*
|
2013-05-02 07:44:12 +04:00
|
|
|
CreateInterfaceObject(JSContext* cx, JS::Handle<JSObject*> global,
|
2013-05-02 22:38:19 +04:00
|
|
|
JS::Handle<JSObject*> constructorProto,
|
2014-08-05 14:46:42 +04:00
|
|
|
const js::Class* constructorClass,
|
2013-01-22 14:51:15 +04:00
|
|
|
const JSNativeHolder* constructorNative,
|
|
|
|
unsigned ctorNargs, const NamedConstructor* namedConstructors,
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Handle<JSObject*> proto,
|
2012-10-09 22:50:05 +04:00
|
|
|
const NativeProperties* properties,
|
|
|
|
const NativeProperties* chromeOnlyProperties,
|
2013-08-08 01:40:00 +04:00
|
|
|
const char* name, bool defineOnGlobal)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Rooted<JSObject*> constructor(cx);
|
2012-05-03 08:35:38 +04:00
|
|
|
if (constructorClass) {
|
2013-05-02 22:38:19 +04:00
|
|
|
MOZ_ASSERT(constructorProto);
|
2015-01-31 18:18:51 +03:00
|
|
|
constructor = JS_NewObjectWithGivenProto(cx, Jsvalify(constructorClass),
|
2015-02-26 17:40:07 +03:00
|
|
|
constructorProto);
|
2012-05-03 08:35:38 +04:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(constructorNative);
|
2013-05-02 22:38:19 +04:00
|
|
|
MOZ_ASSERT(constructorProto == JS_GetFunctionPrototype(cx, global));
|
2013-01-22 14:51:15 +04:00
|
|
|
constructor = CreateConstructor(cx, global, name, constructorNative,
|
|
|
|
ctorNargs);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
if (!constructor) {
|
2013-10-28 18:04:12 +04:00
|
|
|
return nullptr;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2013-05-02 22:38:19 +04:00
|
|
|
if (constructorClass) {
|
2014-01-22 06:11:17 +04:00
|
|
|
if (!JS_DefineProperty(cx, constructor, "length", ctorNargs,
|
2014-11-24 15:56:32 +03:00
|
|
|
JSPROP_READONLY)) {
|
2014-10-25 08:50:28 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Might as well intern, since we're going to need an atomized
|
|
|
|
// version of name anyway when we stick our constructor on the
|
|
|
|
// global.
|
2015-06-30 17:58:31 +03:00
|
|
|
JS::Rooted<JSString*> nameStr(cx, JS_AtomizeAndPinString(cx, name));
|
2014-10-25 08:50:28 +04:00
|
|
|
if (!nameStr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_DefineProperty(cx, constructor, "name", nameStr, JSPROP_READONLY)) {
|
2013-10-28 18:04:12 +04:00
|
|
|
return nullptr;
|
2012-12-14 23:10:49 +04:00
|
|
|
}
|
2012-05-02 17:28:17 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (properties) {
|
2016-03-31 01:57:20 +03:00
|
|
|
if (properties->HasStaticMethods() &&
|
|
|
|
!DefinePrefable(cx, constructor, properties->StaticMethods())) {
|
2012-10-09 22:50:05 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-31 01:57:20 +03:00
|
|
|
if (properties->HasStaticAttributes() &&
|
|
|
|
!DefinePrefable(cx, constructor, properties->StaticAttributes())) {
|
2012-10-19 11:34:28 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-31 01:57:20 +03:00
|
|
|
if (properties->HasConstants() &&
|
|
|
|
!DefinePrefable(cx, constructor, properties->Constants())) {
|
2012-10-09 22:50:05 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chromeOnlyProperties) {
|
2016-03-31 01:57:20 +03:00
|
|
|
if (chromeOnlyProperties->HasStaticMethods() &&
|
|
|
|
!DefinePrefable(cx, constructor,
|
|
|
|
chromeOnlyProperties->StaticMethods())) {
|
2012-10-09 22:50:05 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-31 01:57:20 +03:00
|
|
|
if (chromeOnlyProperties->HasStaticAttributes() &&
|
2012-10-19 11:34:28 +04:00
|
|
|
!DefinePrefable(cx, constructor,
|
2016-03-31 01:57:20 +03:00
|
|
|
chromeOnlyProperties->StaticAttributes())) {
|
2012-10-19 11:34:28 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-31 01:57:20 +03:00
|
|
|
if (chromeOnlyProperties->HasConstants() &&
|
|
|
|
!DefinePrefable(cx, constructor, chromeOnlyProperties->Constants())) {
|
2012-10-09 22:50:05 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (proto && !JS_LinkConstructorAndPrototype(cx, constructor, proto)) {
|
2013-10-28 18:04:12 +04:00
|
|
|
return nullptr;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2013-08-08 01:40:00 +04:00
|
|
|
if (defineOnGlobal && !DefineConstructor(cx, global, name, constructor)) {
|
2013-01-22 14:51:15 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (namedConstructors) {
|
|
|
|
int namedConstructorSlot = DOM_INTERFACE_SLOTS_BASE;
|
|
|
|
while (namedConstructors->mName) {
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Rooted<JSObject*> namedConstructor(cx,
|
|
|
|
CreateConstructor(cx, global, namedConstructors->mName,
|
|
|
|
&namedConstructors->mHolder,
|
|
|
|
namedConstructors->mNargs));
|
2013-01-22 14:51:15 +04:00
|
|
|
if (!namedConstructor ||
|
|
|
|
!JS_DefineProperty(cx, namedConstructor, "prototype",
|
2014-10-29 22:06:31 +03:00
|
|
|
proto,
|
2014-10-29 22:06:32 +03:00
|
|
|
JSPROP_PERMANENT | JSPROP_READONLY,
|
2014-10-29 22:06:31 +03:00
|
|
|
JS_STUBGETTER, JS_STUBSETTER) ||
|
2013-08-08 01:40:00 +04:00
|
|
|
(defineOnGlobal &&
|
|
|
|
!DefineConstructor(cx, global, namedConstructors->mName,
|
|
|
|
namedConstructor))) {
|
2013-01-22 14:51:15 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
js::SetReservedSlot(constructor, namedConstructorSlot++,
|
|
|
|
JS::ObjectValue(*namedConstructor));
|
|
|
|
++namedConstructors;
|
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return constructor;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
2013-05-02 07:44:12 +04:00
|
|
|
CreateInterfacePrototypeObject(JSContext* cx, JS::Handle<JSObject*> global,
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Handle<JSObject*> parentProto,
|
2014-08-05 14:46:42 +04:00
|
|
|
const js::Class* protoClass,
|
2012-10-09 22:50:05 +04:00
|
|
|
const NativeProperties* properties,
|
2016-03-29 22:50:38 +03:00
|
|
|
const NativeProperties* chromeOnlyProperties,
|
|
|
|
const char* const* unscopableNames)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Rooted<JSObject*> ourProto(cx,
|
2015-03-09 19:49:50 +03:00
|
|
|
JS_NewObjectWithUniqueType(cx, Jsvalify(protoClass), parentProto));
|
2014-02-16 01:12:34 +04:00
|
|
|
if (!ourProto ||
|
|
|
|
!DefineProperties(cx, ourProto, properties, chromeOnlyProperties)) {
|
2013-10-28 18:04:12 +04:00
|
|
|
return nullptr;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2016-03-29 22:50:38 +03:00
|
|
|
if (unscopableNames) {
|
|
|
|
JS::Rooted<JSObject*> unscopableObj(cx, JS_NewPlainObject(cx));
|
|
|
|
if (!unscopableObj) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; *unscopableNames; ++unscopableNames) {
|
|
|
|
if (!JS_DefineProperty(cx, unscopableObj, *unscopableNames,
|
|
|
|
JS::TrueHandleValue, JSPROP_ENUMERATE)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<jsid> unscopableId(cx,
|
|
|
|
SYMBOL_TO_JSID(JS::GetWellKnownSymbol(cx, JS::SymbolCode::unscopables)));
|
|
|
|
// Readonly and non-enumerable to match Array.prototype.
|
|
|
|
if (!JS_DefinePropertyById(cx, ourProto, unscopableId, unscopableObj,
|
|
|
|
JSPROP_READONLY)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-16 01:12:34 +04:00
|
|
|
return ourProto;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DefineProperties(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
const NativeProperties* properties,
|
|
|
|
const NativeProperties* chromeOnlyProperties)
|
|
|
|
{
|
2012-10-09 22:50:05 +04:00
|
|
|
if (properties) {
|
2016-03-31 01:57:20 +03:00
|
|
|
if (properties->HasMethods() &&
|
|
|
|
!DefinePrefable(cx, obj, properties->Methods())) {
|
2014-02-16 01:12:34 +04:00
|
|
|
return false;
|
2012-10-09 22:50:05 +04:00
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2016-03-31 01:57:20 +03:00
|
|
|
if (properties->HasAttributes() &&
|
|
|
|
!DefinePrefable(cx, obj, properties->Attributes())) {
|
2014-02-16 01:12:34 +04:00
|
|
|
return false;
|
2012-10-09 22:50:05 +04:00
|
|
|
}
|
|
|
|
|
2016-03-31 01:57:20 +03:00
|
|
|
if (properties->HasConstants() &&
|
|
|
|
!DefinePrefable(cx, obj, properties->Constants())) {
|
2014-02-16 01:12:34 +04:00
|
|
|
return false;
|
2012-10-09 22:50:05 +04:00
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (chromeOnlyProperties) {
|
2016-03-31 01:57:20 +03:00
|
|
|
if (chromeOnlyProperties->HasMethods() &&
|
|
|
|
!DefinePrefable(cx, obj, chromeOnlyProperties->Methods())) {
|
2014-02-16 01:12:34 +04:00
|
|
|
return false;
|
2012-10-09 22:50:05 +04:00
|
|
|
}
|
|
|
|
|
2016-03-31 01:57:20 +03:00
|
|
|
if (chromeOnlyProperties->HasAttributes() &&
|
|
|
|
!DefinePrefable(cx, obj, chromeOnlyProperties->Attributes())) {
|
2014-02-16 01:12:34 +04:00
|
|
|
return false;
|
2012-10-09 22:50:05 +04:00
|
|
|
}
|
|
|
|
|
2016-03-31 01:57:20 +03:00
|
|
|
if (chromeOnlyProperties->HasConstants() &&
|
|
|
|
!DefinePrefable(cx, obj, chromeOnlyProperties->Constants())) {
|
2014-02-16 01:12:34 +04:00
|
|
|
return false;
|
2012-10-09 22:50:05 +04:00
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2014-02-16 01:12:34 +04:00
|
|
|
return true;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
void
|
2013-05-02 07:44:12 +04:00
|
|
|
CreateInterfaceObjects(JSContext* cx, JS::Handle<JSObject*> global,
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Handle<JSObject*> protoProto,
|
2014-08-05 14:46:42 +04:00
|
|
|
const js::Class* protoClass, JS::Heap<JSObject*>* protoCache,
|
2013-05-02 22:38:19 +04:00
|
|
|
JS::Handle<JSObject*> constructorProto,
|
2014-08-05 14:46:42 +04:00
|
|
|
const js::Class* constructorClass, const JSNativeHolder* constructor,
|
2013-01-22 14:51:15 +04:00
|
|
|
unsigned ctorNargs, const NamedConstructor* namedConstructors,
|
2014-05-20 00:37:59 +04:00
|
|
|
JS::Heap<JSObject*>* constructorCache,
|
2012-10-09 22:50:05 +04:00
|
|
|
const NativeProperties* properties,
|
|
|
|
const NativeProperties* chromeOnlyProperties,
|
2016-03-29 22:50:38 +03:00
|
|
|
const char* name, bool defineOnGlobal,
|
|
|
|
const char* const* unscopableNames)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2012-05-03 08:35:38 +04:00
|
|
|
MOZ_ASSERT(protoClass || constructorClass || constructor,
|
|
|
|
"Need at least one class or a constructor!");
|
2012-10-09 22:50:05 +04:00
|
|
|
MOZ_ASSERT(!((properties &&
|
2016-03-31 01:57:20 +03:00
|
|
|
(properties->HasMethods() || properties->HasAttributes())) ||
|
2012-10-09 22:50:05 +04:00
|
|
|
(chromeOnlyProperties &&
|
2016-03-31 01:57:20 +03:00
|
|
|
(chromeOnlyProperties->HasMethods() ||
|
|
|
|
chromeOnlyProperties->HasAttributes()))) || protoClass,
|
2012-03-31 08:42:20 +04:00
|
|
|
"Methods or properties but no protoClass!");
|
2012-10-19 11:34:28 +04:00
|
|
|
MOZ_ASSERT(!((properties &&
|
2016-03-31 01:57:20 +03:00
|
|
|
(properties->HasStaticMethods() ||
|
|
|
|
properties->HasStaticAttributes())) ||
|
2012-10-19 11:34:28 +04:00
|
|
|
(chromeOnlyProperties &&
|
2016-03-31 01:57:20 +03:00
|
|
|
(chromeOnlyProperties->HasStaticMethods() ||
|
|
|
|
chromeOnlyProperties->HasStaticAttributes()))) ||
|
2012-10-09 22:50:05 +04:00
|
|
|
constructorClass || constructor,
|
2012-05-03 08:35:38 +04:00
|
|
|
"Static methods but no constructorClass or constructor!");
|
|
|
|
MOZ_ASSERT(bool(name) == bool(constructorClass || constructor),
|
2012-03-31 08:42:20 +04:00
|
|
|
"Must have name precisely when we have an interface object");
|
2012-05-03 08:35:38 +04:00
|
|
|
MOZ_ASSERT(!constructorClass || !constructor);
|
2012-10-09 22:50:27 +04:00
|
|
|
MOZ_ASSERT(!protoClass == !protoCache,
|
|
|
|
"If, and only if, there is an interface prototype object we need "
|
|
|
|
"to cache it");
|
|
|
|
MOZ_ASSERT(!(constructorClass || constructor) == !constructorCache,
|
|
|
|
"If, and only if, there is an interface object we need to cache "
|
|
|
|
"it");
|
2016-04-21 01:04:56 +03:00
|
|
|
MOZ_ASSERT(constructorProto || (!constructorClass && !constructor),
|
|
|
|
"Must have a constructor proto if we plan to create a constructor "
|
|
|
|
"object");
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Rooted<JSObject*> proto(cx);
|
2012-03-31 08:42:20 +04:00
|
|
|
if (protoClass) {
|
2013-05-02 07:44:11 +04:00
|
|
|
proto =
|
|
|
|
CreateInterfacePrototypeObject(cx, global, protoProto, protoClass,
|
2016-03-29 22:50:38 +03:00
|
|
|
properties, chromeOnlyProperties,
|
|
|
|
unscopableNames);
|
2012-03-31 08:42:20 +04:00
|
|
|
if (!proto) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
2012-08-08 09:26:18 +04:00
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
*protoCache = proto;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
else {
|
2013-05-02 07:44:11 +04:00
|
|
|
MOZ_ASSERT(!proto);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* interface;
|
2012-05-03 08:35:38 +04:00
|
|
|
if (constructorClass || constructor) {
|
2013-05-02 22:38:19 +04:00
|
|
|
interface = CreateInterfaceObject(cx, global, constructorProto,
|
|
|
|
constructorClass, constructor,
|
2013-01-22 14:51:15 +04:00
|
|
|
ctorNargs, namedConstructors, proto,
|
2013-08-08 01:40:00 +04:00
|
|
|
properties, chromeOnlyProperties, name,
|
|
|
|
defineOnGlobal);
|
2012-03-31 08:42:20 +04:00
|
|
|
if (!interface) {
|
2012-10-09 22:50:27 +04:00
|
|
|
if (protoCache) {
|
|
|
|
// If we fail we need to make sure to clear the value of protoCache we
|
|
|
|
// set above.
|
|
|
|
*protoCache = nullptr;
|
|
|
|
}
|
|
|
|
return;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
2012-10-09 22:50:27 +04:00
|
|
|
*constructorCache = interface;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-27 13:20:04 +04:00
|
|
|
bool
|
|
|
|
NativeInterface2JSObjectAndThrowIfFailed(JSContext* aCx,
|
2013-04-30 01:33:42 +04:00
|
|
|
JS::Handle<JSObject*> aScope,
|
2013-10-12 09:02:39 +04:00
|
|
|
JS::MutableHandle<JS::Value> aRetval,
|
2012-03-31 08:42:20 +04:00
|
|
|
xpcObjectHelper& aHelper,
|
|
|
|
const nsIID* aIID,
|
|
|
|
bool aAllowNativeWrapper)
|
|
|
|
{
|
2014-04-09 02:27:18 +04:00
|
|
|
js::AssertSameCompartment(aCx, aScope);
|
2012-03-31 08:42:20 +04:00
|
|
|
nsresult rv;
|
2013-09-10 01:58:29 +04:00
|
|
|
// Inline some logic from XPCConvert::NativeInterfaceToJSObject that we need
|
|
|
|
// on all threads.
|
|
|
|
nsWrapperCache *cache = aHelper.GetWrapperCache();
|
|
|
|
|
|
|
|
if (cache && cache->IsDOMBinding()) {
|
2013-10-16 02:34:13 +04:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, cache->GetWrapper());
|
2013-09-10 01:58:29 +04:00
|
|
|
if (!obj) {
|
2015-05-14 00:07:34 +03:00
|
|
|
obj = cache->WrapObject(aCx, nullptr);
|
2013-09-10 01:58:29 +04:00
|
|
|
}
|
|
|
|
|
2013-10-16 04:02:23 +04:00
|
|
|
if (obj && aAllowNativeWrapper && !JS_WrapObject(aCx, &obj)) {
|
2013-09-10 01:58:29 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj) {
|
2013-10-12 09:02:39 +04:00
|
|
|
aRetval.setObject(*obj);
|
2013-09-10 01:58:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-10-28 18:04:12 +04:00
|
|
|
if (!XPCConvert::NativeInterface2JSObject(aRetval, nullptr, aHelper, aIID,
|
|
|
|
nullptr, aAllowNativeWrapper, &rv)) {
|
2012-03-31 08:42:20 +04:00
|
|
|
// I can't tell if NativeInterface2JSObject throws JS exceptions
|
|
|
|
// or not. This is a sloppy stab at the right semantics; the
|
|
|
|
// method really ought to be fixed to behave consistently.
|
|
|
|
if (!JS_IsExceptionPending(aCx)) {
|
2013-09-09 07:29:21 +04:00
|
|
|
Throw(aCx, NS_FAILED(rv) ? rv : NS_ERROR_UNEXPECTED);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-09 22:59:02 +04:00
|
|
|
bool
|
|
|
|
TryPreserveWrapper(JSObject* obj)
|
|
|
|
{
|
2013-07-01 11:14:36 +04:00
|
|
|
MOZ_ASSERT(IsDOMObject(obj));
|
|
|
|
|
2013-06-01 10:56:02 +04:00
|
|
|
if (nsISupports* native = UnwrapDOMObjectToISupports(obj)) {
|
2012-11-09 22:59:02 +04:00
|
|
|
nsWrapperCache* cache = nullptr;
|
|
|
|
CallQueryInterface(native, &cache);
|
|
|
|
if (cache) {
|
2013-06-23 11:15:42 +04:00
|
|
|
cache->PreserveWrapper(native);
|
2012-11-09 22:59:02 +04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this DOMClass is not cycle collected, then it isn't wrappercached,
|
|
|
|
// so it does not need to be preserved. If it is cycle collected, then
|
|
|
|
// we can't tell if it is wrappercached or not, so we just return false.
|
2014-06-19 00:19:19 +04:00
|
|
|
const DOMJSClass* domClass = GetDOMClass(obj);
|
2012-11-09 22:59:02 +04:00
|
|
|
return domClass && !domClass->mParticipant;
|
|
|
|
}
|
|
|
|
|
2014-05-20 00:37:59 +04:00
|
|
|
// Can only be called with a DOM JSClass.
|
2013-08-09 02:53:04 +04:00
|
|
|
bool
|
2014-05-20 00:37:59 +04:00
|
|
|
InstanceClassHasProtoAtDepth(const js::Class* clasp,
|
|
|
|
uint32_t protoID, uint32_t depth)
|
2012-08-08 09:26:18 +04:00
|
|
|
{
|
2014-06-19 00:19:19 +04:00
|
|
|
const DOMJSClass* domClass = DOMJSClass::FromJSClass(clasp);
|
|
|
|
return static_cast<uint32_t>(domClass->mInterfaceChain[depth]) == protoID;
|
2012-08-08 09:26:18 +04:00
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
// Only set allowNativeWrapper to false if you really know you need it, if in
|
|
|
|
// doubt use true. Setting it to false disables security wrappers.
|
|
|
|
bool
|
2013-04-30 01:33:42 +04:00
|
|
|
XPCOMObjectToJsval(JSContext* cx, JS::Handle<JSObject*> scope,
|
|
|
|
xpcObjectHelper& helper, const nsIID* iid,
|
2013-10-12 09:02:39 +04:00
|
|
|
bool allowNativeWrapper, JS::MutableHandle<JS::Value> rval)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2012-11-27 13:20:04 +04:00
|
|
|
if (!NativeInterface2JSObjectAndThrowIfFailed(cx, scope, rval, helper, iid,
|
2012-03-31 08:42:20 +04:00
|
|
|
allowNativeWrapper)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2013-10-16 02:34:13 +04:00
|
|
|
JSObject* jsobj = rval.toObjectOrNull();
|
2015-03-07 00:33:31 +03:00
|
|
|
if (jsobj &&
|
|
|
|
js::GetGlobalForObjectCrossCompartment(jsobj) == jsobj) {
|
2012-03-31 08:42:20 +04:00
|
|
|
NS_ASSERTION(js::GetObjectClass(jsobj)->flags & JSCLASS_IS_GLOBAL,
|
|
|
|
"Why did we recreate this wrapper?");
|
2015-03-07 00:33:31 +03:00
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-04 06:22:16 +04:00
|
|
|
bool
|
2014-04-10 08:58:43 +04:00
|
|
|
VariantToJsval(JSContext* aCx, nsIVariant* aVariant,
|
|
|
|
JS::MutableHandle<JS::Value> aRetval)
|
2013-04-04 06:22:16 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2013-06-05 08:11:19 +04:00
|
|
|
if (!XPCVariant::VariantDataToJS(aVariant, &rv, aRetval)) {
|
2013-04-04 06:22:16 +04:00
|
|
|
// Does it throw? Who knows
|
|
|
|
if (!JS_IsExceptionPending(aCx)) {
|
2013-09-09 07:29:21 +04:00
|
|
|
Throw(aCx, NS_FAILED(rv) ? rv : NS_ERROR_UNEXPECTED);
|
2013-04-04 06:22:16 +04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-02 11:41:57 +04:00
|
|
|
bool
|
2012-03-31 08:42:20 +04:00
|
|
|
QueryInterface(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
2013-06-08 06:45:46 +04:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JS::Value> thisv(cx, JS_THIS(cx, vp));
|
2013-04-30 01:33:42 +04:00
|
|
|
if (thisv.isNull())
|
2012-03-31 08:42:20 +04:00
|
|
|
return false;
|
|
|
|
|
2012-06-12 17:44:21 +04:00
|
|
|
// Get the object. It might be a security wrapper, in which case we do a checked
|
|
|
|
// unwrap.
|
2013-04-30 01:33:42 +04:00
|
|
|
JS::Rooted<JSObject*> origObj(cx, &thisv.toObject());
|
2014-10-22 19:40:50 +04:00
|
|
|
JS::Rooted<JSObject*> obj(cx, js::CheckedUnwrap(origObj,
|
2015-11-06 21:03:52 +03:00
|
|
|
/* stopAtWindowProxy = */ false));
|
2012-11-14 21:56:26 +04:00
|
|
|
if (!obj) {
|
|
|
|
JS_ReportError(cx, "Permission denied to access object");
|
2012-06-12 17:44:21 +04:00
|
|
|
return false;
|
2012-11-14 21:56:26 +04:00
|
|
|
}
|
2012-06-12 17:44:21 +04:00
|
|
|
|
2014-02-16 01:12:33 +04:00
|
|
|
// Switch this to UnwrapDOMObjectToISupports once our global objects are
|
|
|
|
// using new bindings.
|
2014-10-22 19:40:51 +04:00
|
|
|
nsCOMPtr<nsISupports> native;
|
2014-10-23 23:21:46 +04:00
|
|
|
UnwrapArg<nsISupports>(obj, getter_AddRefs(native));
|
2013-06-01 10:56:02 +04:00
|
|
|
if (!native) {
|
2013-09-09 07:29:21 +04:00
|
|
|
return Throw(cx, NS_ERROR_FAILURE);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc < 1) {
|
2013-09-09 07:29:21 +04:00
|
|
|
return Throw(cx, NS_ERROR_XPC_NOT_ENOUGH_ARGS);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2013-10-16 02:34:13 +04:00
|
|
|
if (!args[0].isObject()) {
|
2013-09-09 07:29:21 +04:00
|
|
|
return Throw(cx, NS_ERROR_XPC_BAD_CONVERT_JS);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2014-10-22 19:40:51 +04:00
|
|
|
nsCOMPtr<nsIJSID> iid;
|
2014-10-22 19:40:50 +04:00
|
|
|
obj = &args[0].toObject();
|
2014-10-23 23:21:46 +04:00
|
|
|
if (NS_FAILED(UnwrapArg<nsIJSID>(obj, getter_AddRefs(iid)))) {
|
2013-09-09 07:29:21 +04:00
|
|
|
return Throw(cx, NS_ERROR_XPC_BAD_CONVERT_JS);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(iid);
|
|
|
|
|
|
|
|
if (iid->GetID()->Equals(NS_GET_IID(nsIClassInfo))) {
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIClassInfo> ci = do_QueryInterface(native, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
2013-09-09 07:29:21 +04:00
|
|
|
return Throw(cx, rv);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
|
2014-04-10 08:58:43 +04:00
|
|
|
return WrapObject(cx, ci, &NS_GET_IID(nsIClassInfo), args.rval());
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
2012-06-12 17:44:21 +04:00
|
|
|
|
2013-01-14 14:29:49 +04:00
|
|
|
nsCOMPtr<nsISupports> unused;
|
|
|
|
nsresult rv = native->QueryInterface(*iid->GetID(), getter_AddRefs(unused));
|
|
|
|
if (NS_FAILED(rv)) {
|
2013-09-09 07:29:21 +04:00
|
|
|
return Throw(cx, rv);
|
2013-01-14 14:29:49 +04:00
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
*vp = thisv;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-12 00:26:52 +04:00
|
|
|
void
|
2014-02-16 01:12:33 +04:00
|
|
|
GetInterfaceImpl(JSContext* aCx, nsIInterfaceRequestor* aRequestor,
|
2014-06-12 00:26:52 +04:00
|
|
|
nsWrapperCache* aCache, nsIJSID* aIID,
|
|
|
|
JS::MutableHandle<JS::Value> aRetval, ErrorResult& aError)
|
2014-02-16 01:12:33 +04:00
|
|
|
{
|
|
|
|
const nsID* iid = aIID->GetID();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsISupports> result;
|
2014-02-16 01:12:33 +04:00
|
|
|
aError = aRequestor->GetInterface(*iid, getter_AddRefs(result));
|
|
|
|
if (aError.Failed()) {
|
2014-06-12 00:26:52 +04:00
|
|
|
return;
|
2014-02-16 01:12:33 +04:00
|
|
|
}
|
|
|
|
|
2014-06-12 00:26:52 +04:00
|
|
|
if (!WrapObject(aCx, result, iid, aRetval)) {
|
2014-02-16 01:12:33 +04:00
|
|
|
aError.Throw(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 03:30:27 +04:00
|
|
|
bool
|
|
|
|
UnforgeableValueOf(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
|
|
|
args.rval().set(args.thisv());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-02 11:41:57 +04:00
|
|
|
bool
|
2012-05-03 08:35:38 +04:00
|
|
|
ThrowingConstructor(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
2012-08-04 11:44:00 +04:00
|
|
|
return ThrowErrorMessage(cx, MSG_ILLEGAL_CONSTRUCTOR);
|
2012-05-03 08:35:38 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 01:38:31 +04:00
|
|
|
bool
|
|
|
|
ThrowConstructorWithoutNew(JSContext* cx, const char* name)
|
|
|
|
{
|
|
|
|
return ThrowErrorMessage(cx, MSG_CONSTRUCTOR_WITHOUT_NEW, name);
|
|
|
|
}
|
|
|
|
|
2015-07-31 20:30:55 +03:00
|
|
|
inline const NativePropertyHooks*
|
|
|
|
GetNativePropertyHooksFromConstructorFunction(JS::Handle<JSObject*> obj)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(JS_IsNativeFunction(obj, Constructor));
|
|
|
|
const JS::Value& v =
|
|
|
|
js::GetFunctionNativeReserved(obj,
|
|
|
|
CONSTRUCTOR_NATIVE_HOLDER_RESERVED_SLOT);
|
|
|
|
const JSNativeHolder* nativeHolder =
|
|
|
|
static_cast<const JSNativeHolder*>(v.toPrivate());
|
|
|
|
return nativeHolder->mPropertyHooks;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
inline const NativePropertyHooks*
|
2013-05-04 03:29:08 +04:00
|
|
|
GetNativePropertyHooks(JSContext *cx, JS::Handle<JSObject*> obj,
|
2014-09-15 18:51:40 +04:00
|
|
|
DOMObjectType& type)
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
2014-08-22 13:25:35 +04:00
|
|
|
const js::Class* clasp = js::GetObjectClass(obj);
|
|
|
|
|
|
|
|
const DOMJSClass* domClass = GetDOMClass(clasp);
|
2012-12-07 00:21:19 +04:00
|
|
|
if (domClass) {
|
2014-09-15 18:51:40 +04:00
|
|
|
bool isGlobal = (clasp->flags & JSCLASS_DOM_GLOBAL) != 0;
|
|
|
|
type = isGlobal ? eGlobalInstance : eInstance;
|
2012-10-09 22:50:27 +04:00
|
|
|
return domClass->mNativeHooks;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JS_ObjectIsFunction(cx, obj)) {
|
|
|
|
type = eInterface;
|
2015-07-31 20:30:55 +03:00
|
|
|
return GetNativePropertyHooksFromConstructorFunction(obj);
|
2012-10-09 22:50:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(IsDOMIfaceAndProtoClass(js::GetObjectClass(obj)));
|
|
|
|
const DOMIfaceAndProtoJSClass* ifaceAndProtoJSClass =
|
|
|
|
DOMIfaceAndProtoJSClass::FromJSClass(js::GetObjectClass(obj));
|
|
|
|
type = ifaceAndProtoJSClass->mType;
|
|
|
|
return ifaceAndProtoJSClass->mNativeHooks;
|
|
|
|
}
|
|
|
|
|
2015-03-07 00:33:31 +03:00
|
|
|
static JSObject*
|
|
|
|
XrayCreateFunction(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JSNativeWrapper native, unsigned nargs, JS::Handle<jsid> id)
|
|
|
|
{
|
2015-03-09 19:50:08 +03:00
|
|
|
JSFunction* fun = js::NewFunctionByIdWithReserved(cx, native.op, nargs, 0, id);
|
2015-03-07 00:33:31 +03:00
|
|
|
if (!fun) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SET_JITINFO(fun, native.info);
|
|
|
|
JSObject* obj = JS_GetFunctionObject(fun);
|
|
|
|
js::SetFunctionNativeReserved(obj, XRAY_DOM_FUNCTION_PARENT_WRAPPER_SLOT,
|
|
|
|
JS::ObjectValue(*wrapper));
|
|
|
|
#ifdef DEBUG
|
|
|
|
js::SetFunctionNativeReserved(obj, XRAY_DOM_FUNCTION_NATIVE_SLOT_FOR_SELF,
|
|
|
|
JS::ObjectValue(*obj));
|
|
|
|
#endif
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
static bool
|
2013-05-04 03:29:08 +04:00
|
|
|
XrayResolveAttribute(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
2016-03-31 01:57:20 +03:00
|
|
|
const Prefable<const JSPropertySpec>* attributes,
|
|
|
|
const jsid* attributeIds,
|
2016-01-28 13:28:04 +03:00
|
|
|
const JSPropertySpec* attributeSpecs,
|
|
|
|
JS::MutableHandle<JS::PropertyDescriptor> desc,
|
2014-08-22 13:25:07 +04:00
|
|
|
bool& cacheOnHolder)
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
|
|
|
for (; attributes->specs; ++attributes) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (attributes->isEnabled(cx, obj)) {
|
2012-10-09 22:50:27 +04:00
|
|
|
// Set i to be the index into our full list of ids/specs that we're
|
|
|
|
// looking at now.
|
|
|
|
size_t i = attributes->specs - attributeSpecs;
|
2013-12-17 06:27:43 +04:00
|
|
|
for ( ; attributeIds[i] != JSID_VOID; ++i) {
|
2012-10-09 22:50:27 +04:00
|
|
|
if (id == attributeIds[i]) {
|
2014-08-22 13:25:07 +04:00
|
|
|
cacheOnHolder = true;
|
|
|
|
|
2013-04-03 04:20:38 +04:00
|
|
|
const JSPropertySpec& attrSpec = attributeSpecs[i];
|
2012-10-09 22:50:27 +04:00
|
|
|
// Because of centralization, we need to make sure we fault in the
|
|
|
|
// JitInfos as well. At present, until the JSAPI changes, the easiest
|
|
|
|
// way to do this is wrap them up as functions ourselves.
|
2014-10-29 22:06:31 +03:00
|
|
|
desc.setAttributes(attrSpec.flags);
|
2012-10-09 22:50:27 +04:00
|
|
|
// They all have getters, so we can just make it.
|
2015-03-07 00:33:31 +03:00
|
|
|
JS::Rooted<JSObject*> funobj(cx,
|
|
|
|
XrayCreateFunction(cx, wrapper, attrSpec.getter.native, 0, id));
|
|
|
|
if (!funobj)
|
2012-10-09 22:50:27 +04:00
|
|
|
return false;
|
2013-08-12 15:09:14 +04:00
|
|
|
desc.setGetterObject(funobj);
|
|
|
|
desc.attributesRef() |= JSPROP_GETTER;
|
2014-10-29 22:06:31 +03:00
|
|
|
if (attrSpec.setter.native.op) {
|
2012-10-09 22:50:27 +04:00
|
|
|
// We have a setter! Make it.
|
2015-03-07 00:33:31 +03:00
|
|
|
funobj =
|
|
|
|
XrayCreateFunction(cx, wrapper, attrSpec.setter.native, 1, id);
|
|
|
|
if (!funobj)
|
2012-10-09 22:50:27 +04:00
|
|
|
return false;
|
2013-08-12 15:09:14 +04:00
|
|
|
desc.setSetterObject(funobj);
|
|
|
|
desc.attributesRef() |= JSPROP_SETTER;
|
2012-10-09 22:50:27 +04:00
|
|
|
} else {
|
2013-08-12 15:09:14 +04:00
|
|
|
desc.setSetter(nullptr);
|
2012-10-09 22:50:27 +04:00
|
|
|
}
|
2013-08-12 15:09:14 +04:00
|
|
|
desc.object().set(wrapper);
|
2012-10-09 22:50:27 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-12 03:32:16 +04:00
|
|
|
static bool
|
|
|
|
XrayResolveMethod(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
|
|
|
const Prefable<const JSFunctionSpec>* methods,
|
2016-03-31 01:57:20 +03:00
|
|
|
const jsid* methodIds,
|
2014-07-23 23:36:21 +04:00
|
|
|
const JSFunctionSpec* methodSpecs,
|
2016-01-28 13:28:04 +03:00
|
|
|
JS::MutableHandle<JS::PropertyDescriptor> desc,
|
2014-08-22 13:25:07 +04:00
|
|
|
bool& cacheOnHolder)
|
2014-07-12 03:32:16 +04:00
|
|
|
{
|
|
|
|
const Prefable<const JSFunctionSpec>* method;
|
|
|
|
for (method = methods; method->specs; ++method) {
|
|
|
|
if (method->isEnabled(cx, obj)) {
|
|
|
|
// Set i to be the index into our full list of ids/specs that we're
|
|
|
|
// looking at now.
|
2014-07-23 23:36:21 +04:00
|
|
|
size_t i = method->specs - methodSpecs;
|
2014-07-12 03:32:16 +04:00
|
|
|
for ( ; methodIds[i] != JSID_VOID; ++i) {
|
|
|
|
if (id == methodIds[i]) {
|
2014-08-22 13:25:07 +04:00
|
|
|
cacheOnHolder = true;
|
|
|
|
|
2014-07-23 23:36:21 +04:00
|
|
|
const JSFunctionSpec& methodSpec = methodSpecs[i];
|
2015-03-07 00:33:31 +03:00
|
|
|
JSObject *funobj;
|
2014-07-12 03:32:16 +04:00
|
|
|
if (methodSpec.selfHostedName) {
|
2015-03-07 00:33:31 +03:00
|
|
|
JSFunction* fun =
|
|
|
|
JS::GetSelfHostedFunction(cx, methodSpec.selfHostedName, id,
|
|
|
|
methodSpec.nargs);
|
2014-07-12 03:32:16 +04:00
|
|
|
if (!fun) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(!methodSpec.call.op, "Bad FunctionSpec declaration: non-null native");
|
|
|
|
MOZ_ASSERT(!methodSpec.call.info, "Bad FunctionSpec declaration: non-null jitinfo");
|
2015-03-07 00:33:31 +03:00
|
|
|
funobj = JS_GetFunctionObject(fun);
|
2014-07-12 03:32:16 +04:00
|
|
|
} else {
|
2015-03-07 00:33:31 +03:00
|
|
|
funobj = XrayCreateFunction(cx, wrapper, methodSpec.call,
|
|
|
|
methodSpec.nargs, id);
|
|
|
|
if (!funobj) {
|
2014-07-12 03:32:16 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
desc.value().setObject(*funobj);
|
|
|
|
desc.setAttributes(methodSpec.flags);
|
|
|
|
desc.object().set(wrapper);
|
|
|
|
desc.setSetter(nullptr);
|
|
|
|
desc.setGetter(nullptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-18 15:22:08 +04:00
|
|
|
// Try to resolve a property as an unforgeable property from the given
|
|
|
|
// NativeProperties, if it's there. nativeProperties is allowed to be null (in
|
|
|
|
// which case we of course won't resolve anything).
|
2014-09-15 18:51:40 +04:00
|
|
|
static bool
|
2013-09-25 22:38:29 +04:00
|
|
|
XrayResolveUnforgeableProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
2016-01-28 13:28:04 +03:00
|
|
|
JS::MutableHandle<JS::PropertyDescriptor> desc,
|
2014-08-22 13:25:07 +04:00
|
|
|
bool& cacheOnHolder,
|
2013-09-25 22:38:29 +04:00
|
|
|
const NativeProperties* nativeProperties)
|
|
|
|
{
|
2014-07-12 03:32:16 +04:00
|
|
|
if (!nativeProperties) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-31 01:57:20 +03:00
|
|
|
if (nativeProperties->HasUnforgeableAttributes()) {
|
2014-07-12 03:32:16 +04:00
|
|
|
if (!XrayResolveAttribute(cx, wrapper, obj, id,
|
2016-03-31 01:57:20 +03:00
|
|
|
nativeProperties->UnforgeableAttributes(),
|
|
|
|
nativeProperties->UnforgeableAttributeIds(),
|
|
|
|
nativeProperties->UnforgeableAttributeSpecs(),
|
2014-08-22 13:25:07 +04:00
|
|
|
desc, cacheOnHolder)) {
|
2014-07-12 03:32:16 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc.object()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 01:57:20 +03:00
|
|
|
if (nativeProperties->HasUnforgeableMethods()) {
|
2014-07-12 03:32:16 +04:00
|
|
|
if (!XrayResolveMethod(cx, wrapper, obj, id,
|
2016-03-31 01:57:20 +03:00
|
|
|
nativeProperties->UnforgeableMethods(),
|
|
|
|
nativeProperties->UnforgeableMethodIds(),
|
|
|
|
nativeProperties->UnforgeableMethodSpecs(),
|
2014-08-22 13:25:07 +04:00
|
|
|
desc, cacheOnHolder)) {
|
2014-07-12 03:32:16 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc.object()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2013-09-25 22:38:29 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
static bool
|
2013-05-04 03:29:08 +04:00
|
|
|
XrayResolveProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
2016-01-28 13:28:04 +03:00
|
|
|
JS::MutableHandle<JS::PropertyDescriptor> desc,
|
2014-08-22 13:25:07 +04:00
|
|
|
bool& cacheOnHolder, DOMObjectType type,
|
2012-10-09 22:50:05 +04:00
|
|
|
const NativeProperties* nativeProperties)
|
2012-07-18 15:52:07 +04:00
|
|
|
{
|
2016-03-31 01:57:20 +03:00
|
|
|
bool hasMethods = false;
|
2012-10-09 22:50:27 +04:00
|
|
|
if (type == eInterface) {
|
2016-03-31 01:57:20 +03:00
|
|
|
hasMethods = nativeProperties->HasStaticMethods();
|
2012-10-09 22:50:27 +04:00
|
|
|
} else {
|
2016-03-31 01:57:20 +03:00
|
|
|
hasMethods = nativeProperties->HasMethods();
|
|
|
|
}
|
|
|
|
if (hasMethods) {
|
|
|
|
const Prefable<const JSFunctionSpec>* methods;
|
|
|
|
const jsid* methodIds;
|
|
|
|
const JSFunctionSpec* methodSpecs;
|
|
|
|
if (type == eInterface) {
|
|
|
|
methods = nativeProperties->StaticMethods();
|
|
|
|
methodIds = nativeProperties->StaticMethodIds();
|
|
|
|
methodSpecs = nativeProperties->StaticMethodSpecs();
|
|
|
|
} else {
|
|
|
|
methods = nativeProperties->Methods();
|
|
|
|
methodIds = nativeProperties->MethodIds();
|
|
|
|
methodSpecs = nativeProperties->MethodSpecs();
|
|
|
|
}
|
2015-03-28 03:44:44 +03:00
|
|
|
JS::Rooted<jsid> methodId(cx);
|
|
|
|
if (nativeProperties->iteratorAliasMethodIndex != -1 &&
|
|
|
|
id == SYMBOL_TO_JSID(
|
|
|
|
JS::GetWellKnownSymbol(cx, JS::SymbolCode::iterator))) {
|
|
|
|
methodId =
|
2016-03-31 01:57:20 +03:00
|
|
|
nativeProperties->MethodIds()[nativeProperties->iteratorAliasMethodIndex];
|
2015-03-28 03:44:44 +03:00
|
|
|
} else {
|
|
|
|
methodId = id;
|
|
|
|
}
|
|
|
|
if (!XrayResolveMethod(cx, wrapper, obj, methodId, methods, methodIds,
|
2014-08-22 13:25:07 +04:00
|
|
|
methodSpecs, desc, cacheOnHolder)) {
|
2014-07-12 03:32:16 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (desc.object()) {
|
|
|
|
return true;
|
2012-07-18 15:52:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-19 11:34:28 +04:00
|
|
|
if (type == eInterface) {
|
2016-03-31 01:57:20 +03:00
|
|
|
if (nativeProperties->HasStaticAttributes()) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (!XrayResolveAttribute(cx, wrapper, obj, id,
|
2016-03-31 01:57:20 +03:00
|
|
|
nativeProperties->StaticAttributes(),
|
|
|
|
nativeProperties->StaticAttributeIds(),
|
|
|
|
nativeProperties->StaticAttributeSpecs(),
|
|
|
|
desc, cacheOnHolder)) {
|
2012-10-19 11:34:28 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-08-12 15:09:14 +04:00
|
|
|
if (desc.object()) {
|
2012-10-19 11:34:28 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-03-31 01:57:20 +03:00
|
|
|
if (nativeProperties->HasAttributes()) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (!XrayResolveAttribute(cx, wrapper, obj, id,
|
2016-03-31 01:57:20 +03:00
|
|
|
nativeProperties->Attributes(),
|
|
|
|
nativeProperties->AttributeIds(),
|
|
|
|
nativeProperties->AttributeSpecs(),
|
|
|
|
desc, cacheOnHolder)) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-08-12 15:09:14 +04:00
|
|
|
if (desc.object()) {
|
2013-09-26 02:22:33 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-07-18 15:52:07 +04:00
|
|
|
}
|
|
|
|
|
2016-03-31 01:57:20 +03:00
|
|
|
if (nativeProperties->HasConstants()) {
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const ConstantSpec>* constant;
|
2016-03-31 01:57:20 +03:00
|
|
|
for (constant = nativeProperties->Constants(); constant->specs; ++constant) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (constant->isEnabled(cx, obj)) {
|
2012-10-09 22:50:05 +04:00
|
|
|
// Set i to be the index into our full list of ids/specs that we're
|
|
|
|
// looking at now.
|
2016-03-31 01:57:20 +03:00
|
|
|
size_t i = constant->specs - nativeProperties->ConstantSpecs();
|
|
|
|
for ( ; nativeProperties->ConstantIds()[i] != JSID_VOID; ++i) {
|
|
|
|
if (id == nativeProperties->ConstantIds()[i]) {
|
2014-08-22 13:25:07 +04:00
|
|
|
cacheOnHolder = true;
|
|
|
|
|
2013-08-12 15:09:14 +04:00
|
|
|
desc.setAttributes(JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT);
|
|
|
|
desc.object().set(wrapper);
|
2016-03-31 01:57:20 +03:00
|
|
|
desc.value().set(nativeProperties->ConstantSpecs()[i].value);
|
2012-10-09 22:50:05 +04:00
|
|
|
return true;
|
|
|
|
}
|
2012-07-18 15:52:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
static bool
|
2013-05-04 03:29:08 +04:00
|
|
|
ResolvePrototypeOrConstructor(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2014-03-22 21:04:09 +04:00
|
|
|
size_t protoAndIfaceCacheIndex, unsigned attrs,
|
2016-01-28 13:28:04 +03:00
|
|
|
JS::MutableHandle<JS::PropertyDescriptor> desc,
|
2014-09-02 16:17:43 +04:00
|
|
|
bool& cacheOnHolder)
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> global(cx, js::GetGlobalForObjectCrossCompartment(obj));
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
|
|
|
JSAutoCompartment ac(cx, global);
|
2014-03-22 21:04:09 +04:00
|
|
|
ProtoAndIfaceCache& protoAndIfaceCache = *GetProtoAndIfaceCache(global);
|
2013-12-11 01:04:55 +04:00
|
|
|
JSObject* protoOrIface =
|
2014-03-22 21:04:09 +04:00
|
|
|
protoAndIfaceCache.EntrySlotIfExists(protoAndIfaceCacheIndex);
|
2012-10-09 22:50:27 +04:00
|
|
|
if (!protoOrIface) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-22 13:25:07 +04:00
|
|
|
|
|
|
|
cacheOnHolder = true;
|
|
|
|
|
2013-08-12 15:09:14 +04:00
|
|
|
desc.object().set(wrapper);
|
|
|
|
desc.setAttributes(attrs);
|
2014-11-22 21:23:39 +03:00
|
|
|
desc.setGetter(nullptr);
|
|
|
|
desc.setSetter(nullptr);
|
2013-08-12 15:09:14 +04:00
|
|
|
desc.value().set(JS::ObjectValue(*protoOrIface));
|
2012-10-09 22:50:27 +04:00
|
|
|
}
|
|
|
|
return JS_WrapPropertyDescriptor(cx, desc);
|
|
|
|
}
|
|
|
|
|
2013-10-18 15:22:08 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
static void
|
|
|
|
DEBUG_CheckXBLCallable(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
// In general, we shouldn't have cross-compartment wrappers here, because
|
|
|
|
// we should be running in an XBL scope, and the content prototype should
|
|
|
|
// contain wrappers to functions defined in the XBL scope. But if the node
|
|
|
|
// has been adopted into another compartment, those prototypes will now point
|
|
|
|
// to a different XBL scope (which is ok).
|
|
|
|
MOZ_ASSERT_IF(js::IsCrossCompartmentWrapper(obj),
|
|
|
|
xpc::IsContentXBLScope(js::GetObjectCompartment(js::UncheckedUnwrap(obj))));
|
|
|
|
MOZ_ASSERT(JS::IsCallable(obj));
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:51:40 +04:00
|
|
|
static void
|
2016-01-28 13:28:04 +03:00
|
|
|
DEBUG_CheckXBLLookup(JSContext *cx, JS::PropertyDescriptor *desc)
|
2013-10-18 15:22:08 +04:00
|
|
|
{
|
|
|
|
if (!desc->obj)
|
|
|
|
return;
|
|
|
|
if (!desc->value.isUndefined()) {
|
|
|
|
MOZ_ASSERT(desc->value.isObject());
|
|
|
|
DEBUG_CheckXBLCallable(cx, &desc->value.toObject());
|
|
|
|
}
|
|
|
|
if (desc->getter) {
|
|
|
|
MOZ_ASSERT(desc->attrs & JSPROP_GETTER);
|
|
|
|
DEBUG_CheckXBLCallable(cx, JS_FUNC_TO_DATA_PTR(JSObject *, desc->getter));
|
|
|
|
}
|
|
|
|
if (desc->setter) {
|
|
|
|
MOZ_ASSERT(desc->attrs & JSPROP_SETTER);
|
|
|
|
DEBUG_CheckXBLCallable(cx, JS_FUNC_TO_DATA_PTR(JSObject *, desc->setter));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define DEBUG_CheckXBLLookup(a, b) {}
|
|
|
|
#endif
|
|
|
|
|
2014-09-15 18:51:40 +04:00
|
|
|
/* static */ bool
|
2013-10-18 15:22:08 +04:00
|
|
|
XrayResolveOwnProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
2016-01-28 13:28:04 +03:00
|
|
|
JS::MutableHandle<JS::PropertyDescriptor> desc,
|
2013-10-18 15:22:08 +04:00
|
|
|
bool& cacheOnHolder)
|
|
|
|
{
|
|
|
|
cacheOnHolder = false;
|
|
|
|
|
|
|
|
DOMObjectType type;
|
|
|
|
const NativePropertyHooks *nativePropertyHooks =
|
2014-09-15 18:51:40 +04:00
|
|
|
GetNativePropertyHooks(cx, obj, type);
|
|
|
|
const NativePropertiesHolder& nativeProperties =
|
|
|
|
nativePropertyHooks->mNativeProperties;
|
2014-09-09 17:19:10 +04:00
|
|
|
ResolveOwnProperty resolveOwnProperty =
|
|
|
|
nativePropertyHooks->mResolveOwnProperty;
|
2013-10-18 15:22:08 +04:00
|
|
|
|
2014-09-09 17:19:10 +04:00
|
|
|
if (type == eNamedPropertiesObject) {
|
|
|
|
// None of these should be cached on the holder, since they're dynamic.
|
|
|
|
return resolveOwnProperty(cx, wrapper, obj, id, desc);
|
2013-10-18 15:22:08 +04:00
|
|
|
}
|
|
|
|
|
2014-09-09 17:19:10 +04:00
|
|
|
// Check for unforgeable properties first.
|
2014-09-15 18:51:40 +04:00
|
|
|
if (IsInstance(type)) {
|
2013-10-18 15:22:08 +04:00
|
|
|
const NativePropertiesHolder& nativeProperties =
|
|
|
|
nativePropertyHooks->mNativeProperties;
|
|
|
|
if (!XrayResolveUnforgeableProperty(cx, wrapper, obj, id, desc, cacheOnHolder,
|
|
|
|
nativeProperties.regular)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-09 17:19:10 +04:00
|
|
|
|
2014-09-15 18:51:40 +04:00
|
|
|
if (!desc.object() && xpc::AccessCheck::isChrome(wrapper) &&
|
|
|
|
!XrayResolveUnforgeableProperty(cx, wrapper, obj, id, desc, cacheOnHolder,
|
2013-10-18 15:22:08 +04:00
|
|
|
nativeProperties.chromeOnly)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-09 17:19:10 +04:00
|
|
|
|
2013-10-18 15:22:08 +04:00
|
|
|
if (desc.object()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:51:40 +04:00
|
|
|
if (IsInstance(type)) {
|
2014-09-09 17:19:10 +04:00
|
|
|
if (resolveOwnProperty) {
|
|
|
|
if (!resolveOwnProperty(cx, wrapper, obj, id, desc)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc.object()) {
|
|
|
|
// None of these should be cached on the holder, since they're dynamic.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:51:40 +04:00
|
|
|
// If we're a special scope for in-content XBL, our script expects to see
|
|
|
|
// the bound XBL methods and attributes when accessing content. However,
|
|
|
|
// these members are implemented in content via custom-spliced prototypes,
|
|
|
|
// and thus aren't visible through Xray wrappers unless we handle them
|
|
|
|
// explicitly. So we check if we're running in such a scope, and if so,
|
|
|
|
// whether the wrappee is a bound element. If it is, we do a lookup via
|
|
|
|
// specialized XBL machinery.
|
|
|
|
//
|
|
|
|
// While we have to do some sketchy walking through content land, we should
|
|
|
|
// be protected by read-only/non-configurable properties, and any functions
|
|
|
|
// we end up with should _always_ be living in our own scope (the XBL scope).
|
|
|
|
// Make sure to assert that.
|
|
|
|
Element* element;
|
|
|
|
if (xpc::ObjectScope(wrapper)->IsContentXBLScope() &&
|
|
|
|
NS_SUCCEEDED(UNWRAP_OBJECT(Element, obj, element))) {
|
|
|
|
if (!nsContentUtils::LookupBindingMember(cx, element, id, desc)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-18 15:22:08 +04:00
|
|
|
|
2014-09-15 18:51:40 +04:00
|
|
|
DEBUG_CheckXBLLookup(cx, desc.address());
|
2014-09-09 17:19:10 +04:00
|
|
|
|
2014-09-15 18:51:40 +04:00
|
|
|
if (desc.object()) {
|
|
|
|
// XBL properties shouldn't be cached on the holder, as they might be
|
|
|
|
// shadowed by own properties returned from mResolveOwnProperty.
|
|
|
|
desc.object().set(wrapper);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For non-global instance Xrays there are no other properties, so return
|
|
|
|
// here for them.
|
2015-11-20 12:00:00 +03:00
|
|
|
if (type != eGlobalInstance) {
|
2014-09-15 18:51:40 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (type == eInterface) {
|
2014-09-15 18:45:38 +04:00
|
|
|
if (IdEquals(id, "prototype")) {
|
|
|
|
return nativePropertyHooks->mPrototypeID == prototypes::id::_ID_Count ||
|
|
|
|
ResolvePrototypeOrConstructor(cx, wrapper, obj,
|
|
|
|
nativePropertyHooks->mPrototypeID,
|
|
|
|
JSPROP_PERMANENT | JSPROP_READONLY,
|
|
|
|
desc, cacheOnHolder);
|
|
|
|
}
|
2014-09-15 18:51:40 +04:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(IsInterfacePrototype(type));
|
2012-10-09 22:50:27 +04:00
|
|
|
|
2014-09-15 18:51:40 +04:00
|
|
|
if (IdEquals(id, "constructor")) {
|
|
|
|
return nativePropertyHooks->mConstructorID == constructors::id::_ID_Count ||
|
|
|
|
ResolvePrototypeOrConstructor(cx, wrapper, obj,
|
|
|
|
nativePropertyHooks->mConstructorID,
|
|
|
|
0, desc, cacheOnHolder);
|
|
|
|
}
|
2012-10-09 22:50:27 +04:00
|
|
|
|
2014-09-15 18:51:40 +04:00
|
|
|
// The properties for globals live on the instance, so return here as there
|
|
|
|
// are no properties on their interface prototype object.
|
2015-11-20 12:00:00 +03:00
|
|
|
if (type == eGlobalInterfacePrototype) {
|
2014-09-15 18:51:40 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-10-09 22:50:27 +04:00
|
|
|
|
|
|
|
if (nativeProperties.regular &&
|
2014-08-22 13:25:07 +04:00
|
|
|
!XrayResolveProperty(cx, wrapper, obj, id, desc, cacheOnHolder, type,
|
2012-10-09 22:50:27 +04:00
|
|
|
nativeProperties.regular)) {
|
2012-10-09 22:50:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-12 15:09:14 +04:00
|
|
|
if (!desc.object() &&
|
2012-10-09 22:50:27 +04:00
|
|
|
nativeProperties.chromeOnly &&
|
2012-10-09 22:50:05 +04:00
|
|
|
xpc::AccessCheck::isChrome(js::GetObjectCompartment(wrapper)) &&
|
2014-08-22 13:25:07 +04:00
|
|
|
!XrayResolveProperty(cx, wrapper, obj, id, desc, cacheOnHolder, type,
|
2012-10-09 22:50:27 +04:00
|
|
|
nativeProperties.chromeOnly)) {
|
2012-10-09 22:50:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-29 13:38:57 +04:00
|
|
|
bool
|
|
|
|
XrayDefineProperty(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
|
2016-01-28 13:28:04 +03:00
|
|
|
JS::Handle<JS::PropertyDescriptor> desc,
|
2015-01-30 20:37:07 +03:00
|
|
|
JS::ObjectOpResult &result, bool *defined)
|
2013-05-29 13:38:57 +04:00
|
|
|
{
|
|
|
|
if (!js::IsProxy(obj))
|
2015-01-30 20:37:07 +03:00
|
|
|
return true;
|
2013-05-29 13:38:57 +04:00
|
|
|
|
2014-06-27 15:44:08 +04:00
|
|
|
const DOMProxyHandler* handler = GetDOMProxyHandler(obj);
|
2015-01-30 20:37:07 +03:00
|
|
|
return handler->defineProperty(cx, wrapper, id, desc, result, defined);
|
2013-05-29 13:38:57 +04:00
|
|
|
}
|
|
|
|
|
2014-07-23 23:36:20 +04:00
|
|
|
template<typename SpecType>
|
2012-10-09 22:50:27 +04:00
|
|
|
bool
|
2014-09-27 00:16:36 +04:00
|
|
|
XrayAttributeOrMethodKeys(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
|
|
|
const Prefable<const SpecType>* list,
|
2016-03-31 01:57:20 +03:00
|
|
|
const jsid* ids, const SpecType* specList,
|
2014-09-27 00:16:36 +04:00
|
|
|
unsigned flags, JS::AutoIdVector& props)
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
2014-07-23 23:36:20 +04:00
|
|
|
for (; list->specs; ++list) {
|
|
|
|
if (list->isEnabled(cx, obj)) {
|
2012-10-09 22:50:27 +04:00
|
|
|
// Set i to be the index into our full list of ids/specs that we're
|
|
|
|
// looking at now.
|
2014-07-23 23:36:20 +04:00
|
|
|
size_t i = list->specs - specList;
|
|
|
|
for ( ; ids[i] != JSID_VOID; ++i) {
|
2014-09-18 21:30:38 +04:00
|
|
|
// Skip non-enumerable properties and symbol-keyed properties unless
|
|
|
|
// they are specially requested via flags.
|
2012-11-05 20:58:03 +04:00
|
|
|
if (((flags & JSITER_HIDDEN) ||
|
2014-07-23 23:36:20 +04:00
|
|
|
(specList[i].flags & JSPROP_ENUMERATE)) &&
|
2014-09-18 21:30:38 +04:00
|
|
|
((flags & JSITER_SYMBOLS) || !JSID_IS_SYMBOL(ids[i])) &&
|
2014-07-23 23:36:20 +04:00
|
|
|
!props.append(ids[i])) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-31 01:57:20 +03:00
|
|
|
#define ADD_KEYS_IF_DEFINED(FieldName) { \
|
|
|
|
if (nativeProperties->Has##FieldName##s() && \
|
2014-09-27 00:16:36 +04:00
|
|
|
!XrayAttributeOrMethodKeys(cx, wrapper, obj, \
|
2016-03-31 01:57:20 +03:00
|
|
|
nativeProperties->FieldName##s(), \
|
|
|
|
nativeProperties->FieldName##Ids(), \
|
|
|
|
nativeProperties->FieldName##Specs(), \
|
2014-09-27 00:16:36 +04:00
|
|
|
flags, props)) { \
|
2014-07-23 23:36:21 +04:00
|
|
|
return false; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
bool
|
2014-09-27 00:16:36 +04:00
|
|
|
XrayOwnPropertyKeys(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
|
|
|
unsigned flags, JS::AutoIdVector& props,
|
|
|
|
DOMObjectType type,
|
|
|
|
const NativeProperties* nativeProperties)
|
2012-07-18 15:52:07 +04:00
|
|
|
{
|
2014-09-09 17:19:10 +04:00
|
|
|
MOZ_ASSERT(type != eNamedPropertiesObject);
|
|
|
|
|
2014-09-15 18:51:40 +04:00
|
|
|
if (IsInstance(type)) {
|
2016-03-31 01:57:20 +03:00
|
|
|
ADD_KEYS_IF_DEFINED(UnforgeableMethod);
|
|
|
|
ADD_KEYS_IF_DEFINED(UnforgeableAttribute);
|
2015-11-20 12:00:00 +03:00
|
|
|
if (type == eGlobalInstance) {
|
2016-03-31 01:57:20 +03:00
|
|
|
ADD_KEYS_IF_DEFINED(Method);
|
|
|
|
ADD_KEYS_IF_DEFINED(Attribute);
|
2014-09-15 18:51:40 +04:00
|
|
|
}
|
2014-07-23 23:36:21 +04:00
|
|
|
} else if (type == eInterface) {
|
2016-03-31 01:57:20 +03:00
|
|
|
ADD_KEYS_IF_DEFINED(StaticMethod);
|
|
|
|
ADD_KEYS_IF_DEFINED(StaticAttribute);
|
2015-11-20 12:00:00 +03:00
|
|
|
} else if (type != eGlobalInterfacePrototype) {
|
2014-09-15 18:51:40 +04:00
|
|
|
MOZ_ASSERT(IsInterfacePrototype(type));
|
2016-03-31 01:57:20 +03:00
|
|
|
ADD_KEYS_IF_DEFINED(Method);
|
|
|
|
ADD_KEYS_IF_DEFINED(Attribute);
|
2012-07-18 15:52:07 +04:00
|
|
|
}
|
|
|
|
|
2016-03-31 01:57:20 +03:00
|
|
|
if (nativeProperties->HasConstants()) {
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const ConstantSpec>* constant;
|
2016-03-31 01:57:20 +03:00
|
|
|
for (constant = nativeProperties->Constants(); constant->specs; ++constant) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (constant->isEnabled(cx, obj)) {
|
2012-10-09 22:50:05 +04:00
|
|
|
// Set i to be the index into our full list of ids/specs that we're
|
|
|
|
// looking at now.
|
2016-03-31 01:57:20 +03:00
|
|
|
size_t i = constant->specs - nativeProperties->ConstantSpecs();
|
|
|
|
for ( ; nativeProperties->ConstantIds()[i] != JSID_VOID; ++i) {
|
|
|
|
if (!props.append(nativeProperties->ConstantIds()[i])) {
|
2012-10-09 22:50:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-07-18 15:52:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-27 00:16:36 +04:00
|
|
|
#undef ADD_KEYS_IF_DEFINED
|
2014-07-23 23:36:21 +04:00
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
bool
|
2014-09-27 00:16:36 +04:00
|
|
|
XrayOwnNativePropertyKeys(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
const NativePropertyHooks* nativePropertyHooks,
|
|
|
|
DOMObjectType type, JS::Handle<JSObject*> obj,
|
|
|
|
unsigned flags, JS::AutoIdVector& props)
|
2012-10-09 22:50:05 +04:00
|
|
|
{
|
2014-09-09 17:19:10 +04:00
|
|
|
MOZ_ASSERT(type != eNamedPropertiesObject);
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
if (type == eInterface &&
|
|
|
|
nativePropertyHooks->mPrototypeID != prototypes::id::_ID_Count &&
|
|
|
|
!AddStringToIDVector(cx, props, "prototype")) {
|
2012-10-09 22:50:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:51:40 +04:00
|
|
|
if (IsInterfacePrototype(type) &&
|
2012-10-09 22:50:27 +04:00
|
|
|
nativePropertyHooks->mConstructorID != constructors::id::_ID_Count &&
|
2012-11-05 20:58:03 +04:00
|
|
|
(flags & JSITER_HIDDEN) &&
|
2012-10-09 22:50:27 +04:00
|
|
|
!AddStringToIDVector(cx, props, "constructor")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const NativePropertiesHolder& nativeProperties =
|
|
|
|
nativePropertyHooks->mNativeProperties;
|
|
|
|
|
|
|
|
if (nativeProperties.regular &&
|
2014-09-27 00:16:36 +04:00
|
|
|
!XrayOwnPropertyKeys(cx, wrapper, obj, flags, props, type,
|
|
|
|
nativeProperties.regular)) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nativeProperties.chromeOnly &&
|
2012-10-09 22:50:05 +04:00
|
|
|
xpc::AccessCheck::isChrome(js::GetObjectCompartment(wrapper)) &&
|
2014-09-27 00:16:36 +04:00
|
|
|
!XrayOwnPropertyKeys(cx, wrapper, obj, flags, props, type,
|
|
|
|
nativeProperties.chromeOnly)) {
|
2012-10-09 22:50:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:27 +04:00
|
|
|
bool
|
2014-09-27 00:16:36 +04:00
|
|
|
XrayOwnPropertyKeys(JSContext* cx, JS::Handle<JSObject*> wrapper,
|
|
|
|
JS::Handle<JSObject*> obj,
|
|
|
|
unsigned flags, JS::AutoIdVector& props)
|
2012-10-09 22:50:27 +04:00
|
|
|
{
|
|
|
|
DOMObjectType type;
|
|
|
|
const NativePropertyHooks* nativePropertyHooks =
|
2014-09-15 18:51:40 +04:00
|
|
|
GetNativePropertyHooks(cx, obj, type);
|
2014-09-09 17:19:10 +04:00
|
|
|
EnumerateOwnProperties enumerateOwnProperties =
|
|
|
|
nativePropertyHooks->mEnumerateOwnProperties;
|
|
|
|
|
|
|
|
if (type == eNamedPropertiesObject) {
|
|
|
|
return enumerateOwnProperties(cx, wrapper, obj, props);
|
|
|
|
}
|
2012-10-09 22:50:27 +04:00
|
|
|
|
2014-09-15 18:51:40 +04:00
|
|
|
if (IsInstance(type)) {
|
|
|
|
// FIXME https://bugzilla.mozilla.org/show_bug.cgi?id=1071189
|
|
|
|
// Should do something about XBL properties too.
|
2014-09-09 17:19:10 +04:00
|
|
|
if (enumerateOwnProperties &&
|
|
|
|
!enumerateOwnProperties(cx, wrapper, obj, props)) {
|
2012-10-09 22:50:27 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 12:00:00 +03:00
|
|
|
return type == eGlobalInterfacePrototype ||
|
2014-09-27 00:16:36 +04:00
|
|
|
XrayOwnNativePropertyKeys(cx, wrapper, nativePropertyHooks, type,
|
|
|
|
obj, flags, props);
|
2012-10-09 22:50:27 +04:00
|
|
|
}
|
|
|
|
|
2015-02-26 17:40:08 +03:00
|
|
|
NativePropertyHooks sEmptyNativePropertyHooks = {
|
2012-10-09 22:50:27 +04:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
{
|
|
|
|
nullptr,
|
|
|
|
nullptr
|
|
|
|
},
|
|
|
|
prototypes::id::_ID_Count,
|
|
|
|
constructors::id::_ID_Count,
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
2016-04-01 03:00:01 +03:00
|
|
|
const js::ClassOps sBoringInterfaceObjectClassClassOps = {
|
|
|
|
nullptr, /* addProperty */
|
|
|
|
nullptr, /* delProperty */
|
|
|
|
nullptr, /* getProperty */
|
|
|
|
nullptr, /* setProperty */
|
|
|
|
nullptr, /* enumerate */
|
|
|
|
nullptr, /* resolve */
|
|
|
|
nullptr, /* mayResolve */
|
|
|
|
nullptr, /* finalize */
|
|
|
|
ThrowingConstructor, /* call */
|
|
|
|
InterfaceHasInstance, /* hasInstance */
|
|
|
|
ThrowingConstructor, /* construct */
|
|
|
|
nullptr, /* trace */
|
|
|
|
};
|
|
|
|
|
2016-03-23 23:00:29 +03:00
|
|
|
const js::ObjectOps sInterfaceObjectClassObjectOps = {
|
|
|
|
nullptr, /* lookupProperty */
|
|
|
|
nullptr, /* defineProperty */
|
|
|
|
nullptr, /* hasProperty */
|
|
|
|
nullptr, /* getProperty */
|
|
|
|
nullptr, /* setProperty */
|
|
|
|
nullptr, /* getOwnPropertyDescriptor */
|
|
|
|
nullptr, /* deleteProperty */
|
|
|
|
nullptr, /* watch */
|
|
|
|
nullptr, /* unwatch */
|
|
|
|
nullptr, /* getElements */
|
|
|
|
nullptr, /* enumerate */
|
|
|
|
InterfaceObjectToString, /* funToString */
|
|
|
|
};
|
|
|
|
|
2012-05-22 17:46:20 +04:00
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
GetPropertyOnPrototype(JSContext* cx, JS::Handle<JSObject*> proxy,
|
2015-09-18 01:14:41 +03:00
|
|
|
JS::Handle<JS::Value> receiver, JS::Handle<jsid> id,
|
|
|
|
bool* found, JS::MutableHandle<JS::Value> vp)
|
2012-05-22 17:46:20 +04:00
|
|
|
{
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> proto(cx);
|
2013-05-05 11:03:14 +04:00
|
|
|
if (!js::GetObjectProto(cx, proxy, &proto)) {
|
2012-09-04 03:42:10 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-05-22 17:46:20 +04:00
|
|
|
if (!proto) {
|
|
|
|
*found = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-02 23:02:36 +03:00
|
|
|
if (!JS_HasPropertyById(cx, proto, id, found)) {
|
2012-05-22 17:46:20 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-02 23:02:36 +03:00
|
|
|
if (!*found) {
|
2012-05-22 17:46:20 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-18 01:14:41 +03:00
|
|
|
return JS_ForwardGetPropertyTo(cx, proto, id, receiver, vp);
|
2012-05-22 17:46:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
HasPropertyOnPrototype(JSContext* cx, JS::Handle<JSObject*> proxy,
|
2015-01-02 23:02:36 +03:00
|
|
|
JS::Handle<jsid> id, bool* has)
|
2012-05-22 17:46:20 +04:00
|
|
|
{
|
2015-01-02 23:02:36 +03:00
|
|
|
JS::Rooted<JSObject*> proto(cx);
|
|
|
|
if (!js::GetObjectProto(cx, proxy, &proto)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!proto) {
|
|
|
|
*has = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_HasPropertyById(cx, proto, id, has);
|
2013-07-09 18:45:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AppendNamedPropertyIds(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
nsTArray<nsString>& names,
|
|
|
|
bool shadowPrototypeProperties,
|
|
|
|
JS::AutoIdVector& props)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < names.Length(); ++i) {
|
|
|
|
JS::Rooted<JS::Value> v(cx);
|
2013-10-12 09:02:39 +04:00
|
|
|
if (!xpc::NonVoidStringToJsval(cx, names[i], &v)) {
|
2013-07-09 18:45:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<jsid> id(cx);
|
2014-01-17 22:08:51 +04:00
|
|
|
if (!JS_ValueToId(cx, v, &id)) {
|
2013-07-09 18:45:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-02 23:02:36 +03:00
|
|
|
bool shouldAppend = shadowPrototypeProperties;
|
|
|
|
if (!shouldAppend) {
|
|
|
|
bool has;
|
|
|
|
if (!HasPropertyOnPrototype(cx, proxy, id, &has)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
shouldAppend = !has;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldAppend) {
|
2013-07-09 18:45:13 +04:00
|
|
|
if (!props.append(id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-05-22 17:46:20 +04:00
|
|
|
}
|
|
|
|
|
2013-05-08 01:18:03 +04:00
|
|
|
bool
|
2014-02-07 06:08:28 +04:00
|
|
|
DictionaryBase::ParseJSON(JSContext* aCx,
|
|
|
|
const nsAString& aJSON,
|
|
|
|
JS::MutableHandle<JS::Value> aVal)
|
2012-10-16 18:52:10 +04:00
|
|
|
{
|
2012-10-16 19:37:44 +04:00
|
|
|
if (aJSON.IsEmpty()) {
|
2013-05-08 01:18:03 +04:00
|
|
|
return true;
|
2012-10-16 18:52:10 +04:00
|
|
|
}
|
2014-07-22 08:43:21 +04:00
|
|
|
return JS_ParseJSON(aCx, PromiseFlatString(aJSON).get(), aJSON.Length(), aVal);
|
2012-10-16 18:52:10 +04:00
|
|
|
}
|
|
|
|
|
2014-08-01 07:50:19 +04:00
|
|
|
bool
|
|
|
|
DictionaryBase::StringifyToJSON(JSContext* aCx,
|
2016-02-11 07:31:33 +03:00
|
|
|
JS::Handle<JSObject*> aObj,
|
2014-09-09 22:00:22 +04:00
|
|
|
nsAString& aJSON) const
|
2014-08-01 07:50:19 +04:00
|
|
|
{
|
2016-02-11 07:31:33 +03:00
|
|
|
return JS::ToJSONMaybeSafely(aCx, aObj, AppendJSONToString, &aJSON);
|
2014-08-01 07:50:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
bool
|
2014-07-22 08:43:21 +04:00
|
|
|
DictionaryBase::AppendJSONToString(const char16_t* aJSONData,
|
2014-08-01 07:50:19 +04:00
|
|
|
uint32_t aDataLength,
|
|
|
|
void* aString)
|
|
|
|
{
|
|
|
|
nsAString* string = static_cast<nsAString*>(aString);
|
2014-07-22 08:43:21 +04:00
|
|
|
string->Append(aJSONData, aDataLength);
|
2014-08-01 07:50:19 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-26 18:17:46 +04:00
|
|
|
nsresult
|
2013-10-16 02:34:13 +04:00
|
|
|
ReparentWrapper(JSContext* aCx, JS::Handle<JSObject*> aObjArg)
|
2012-09-26 18:17:46 +04:00
|
|
|
{
|
2014-04-09 02:27:15 +04:00
|
|
|
js::AssertSameCompartment(aCx, aObjArg);
|
|
|
|
|
2014-11-13 23:23:11 +03:00
|
|
|
// Check if we're anywhere near the stack limit before we reach the
|
|
|
|
// transplanting code, since it has no good way to handle errors. This uses
|
|
|
|
// the untrusted script limit, which is not strictly necessary since no
|
|
|
|
// actual script should run.
|
2014-08-21 05:14:56 +04:00
|
|
|
JS_CHECK_RECURSION_CONSERVATIVE(aCx, return NS_ERROR_FAILURE);
|
2014-03-07 03:26:17 +04:00
|
|
|
|
2013-10-16 02:34:13 +04:00
|
|
|
JS::Rooted<JSObject*> aObj(aCx, aObjArg);
|
2014-06-19 00:19:19 +04:00
|
|
|
const DOMJSClass* domClass = GetDOMClass(aObj);
|
2012-09-26 18:17:46 +04:00
|
|
|
|
2015-02-26 17:40:07 +03:00
|
|
|
// DOM things are always parented to globals.
|
2015-03-07 00:33:31 +03:00
|
|
|
JS::Rooted<JSObject*> oldParent(aCx,
|
|
|
|
js::GetGlobalForObjectCrossCompartment(aObj));
|
2015-02-26 17:40:07 +03:00
|
|
|
MOZ_ASSERT(js::GetGlobalForObjectCrossCompartment(oldParent) == oldParent);
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> newParent(aCx, domClass->mGetParent(aCx, aObj));
|
2015-02-26 17:40:07 +03:00
|
|
|
MOZ_ASSERT(js::GetGlobalForObjectCrossCompartment(newParent) == newParent);
|
2012-09-26 18:17:46 +04:00
|
|
|
|
|
|
|
JSAutoCompartment oldAc(aCx, oldParent);
|
|
|
|
|
2013-10-23 16:02:42 +04:00
|
|
|
JSCompartment* oldCompartment = js::GetObjectCompartment(oldParent);
|
|
|
|
JSCompartment* newCompartment = js::GetObjectCompartment(newParent);
|
|
|
|
if (oldCompartment == newCompartment) {
|
2015-02-26 17:40:07 +03:00
|
|
|
MOZ_ASSERT(oldParent == newParent);
|
2012-09-26 18:17:46 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-01 10:56:02 +04:00
|
|
|
nsISupports* native = UnwrapDOMObjectToISupports(aObj);
|
|
|
|
if (!native) {
|
2012-09-26 18:17:46 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-16 21:02:57 +04:00
|
|
|
bool isProxy = js::IsProxy(aObj);
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> expandoObject(aCx);
|
2013-04-16 21:02:57 +04:00
|
|
|
if (isProxy) {
|
|
|
|
expandoObject = DOMProxyHandler::GetAndClearExpandoObject(aObj);
|
|
|
|
}
|
|
|
|
|
2012-09-26 18:17:46 +04:00
|
|
|
JSAutoCompartment newAc(aCx, newParent);
|
|
|
|
|
|
|
|
// First we clone the reflector. We get a copy of its properties and clone its
|
2015-03-05 16:39:53 +03:00
|
|
|
// expando chain.
|
2012-09-26 18:17:46 +04:00
|
|
|
|
2015-02-26 17:40:07 +03:00
|
|
|
JS::Handle<JSObject*> proto = (domClass->mGetProto)(aCx, newParent);
|
2012-09-26 18:17:46 +04:00
|
|
|
if (!proto) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-02-26 23:58:59 +03:00
|
|
|
JS::Rooted<JSObject*> newobj(aCx, JS_CloneObject(aCx, aObj, proto));
|
2012-09-26 18:17:46 +04:00
|
|
|
if (!newobj) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> propertyHolder(aCx);
|
2015-03-05 16:39:53 +03:00
|
|
|
JS::Rooted<JSObject*> copyFrom(aCx, isProxy ? expandoObject : aObj);
|
|
|
|
if (copyFrom) {
|
2015-05-14 00:07:34 +03:00
|
|
|
propertyHolder = JS_NewObjectWithGivenProto(aCx, nullptr, nullptr);
|
2015-03-05 16:39:53 +03:00
|
|
|
if (!propertyHolder) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2012-09-26 18:17:46 +04:00
|
|
|
}
|
|
|
|
|
2015-03-05 16:39:53 +03:00
|
|
|
if (!JS_CopyPropertiesFrom(aCx, propertyHolder, copyFrom)) {
|
2012-09-26 18:17:46 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2015-03-05 16:39:53 +03:00
|
|
|
} else {
|
|
|
|
propertyHolder = nullptr;
|
|
|
|
}
|
2012-09-26 18:17:46 +04:00
|
|
|
|
2015-03-05 16:39:53 +03:00
|
|
|
// Expandos from other compartments are attached to the target JS object.
|
|
|
|
// Copy them over, and let the old ones die a natural death.
|
|
|
|
|
|
|
|
// Note that at this point the DOM_OBJECT_SLOT for |newobj| has not been set.
|
|
|
|
// CloneExpandoChain() will use this property of |newobj| when it calls
|
|
|
|
// preserveWrapper() via attachExpandoObject() if |aObj| has expandos set, and
|
|
|
|
// preserveWrapper() will not do anything in this case. This is safe because
|
2015-03-06 18:06:57 +03:00
|
|
|
// if expandos are present then the wrapper will already have been preserved
|
|
|
|
// for this native.
|
2015-03-05 16:39:53 +03:00
|
|
|
if (!xpc::XrayUtils::CloneExpandoChain(aCx, newobj, aObj)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-09-26 18:17:46 +04:00
|
|
|
}
|
|
|
|
|
2015-03-05 16:39:53 +03:00
|
|
|
// We've set up |newobj|, so we make it own the native by setting its reserved
|
|
|
|
// slot and nulling out the reserved slot of |obj|.
|
|
|
|
//
|
|
|
|
// NB: It's important to do this _after_ copying the properties to
|
|
|
|
// propertyHolder. Otherwise, an object with |foo.x === foo| will
|
|
|
|
// crash when JS_CopyPropertiesFrom tries to call wrap() on foo.x.
|
|
|
|
js::SetReservedOrProxyPrivateSlot(newobj, DOM_OBJECT_SLOT,
|
|
|
|
js::GetReservedOrProxyPrivateSlot(aObj, DOM_OBJECT_SLOT));
|
|
|
|
js::SetReservedOrProxyPrivateSlot(aObj, DOM_OBJECT_SLOT, JS::PrivateValue(nullptr));
|
|
|
|
|
2014-03-19 20:35:45 +04:00
|
|
|
aObj = xpc::TransplantObject(aCx, aObj, newobj);
|
|
|
|
if (!aObj) {
|
2013-10-17 12:00:02 +04:00
|
|
|
MOZ_CRASH();
|
2012-09-26 18:17:46 +04:00
|
|
|
}
|
|
|
|
|
2014-03-19 20:35:45 +04:00
|
|
|
nsWrapperCache* cache = nullptr;
|
|
|
|
CallQueryInterface(native, &cache);
|
2012-09-26 18:17:46 +04:00
|
|
|
bool preserving = cache->PreservingWrapper();
|
|
|
|
cache->SetPreservingWrapper(false);
|
|
|
|
cache->SetWrapper(aObj);
|
|
|
|
cache->SetPreservingWrapper(preserving);
|
2013-04-16 21:02:57 +04:00
|
|
|
|
|
|
|
if (propertyHolder) {
|
2014-03-17 20:17:58 +04:00
|
|
|
JS::Rooted<JSObject*> copyTo(aCx);
|
2013-04-16 21:02:57 +04:00
|
|
|
if (isProxy) {
|
|
|
|
copyTo = DOMProxyHandler::EnsureExpandoObject(aCx, aObj);
|
|
|
|
} else {
|
|
|
|
copyTo = aObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!copyTo || !JS_CopyPropertiesFrom(aCx, copyTo, propertyHolder)) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
2012-09-26 18:17:46 +04:00
|
|
|
}
|
|
|
|
|
2013-03-17 18:42:05 +04:00
|
|
|
nsObjectLoadingContent* htmlobject;
|
2013-11-21 16:51:16 +04:00
|
|
|
nsresult rv = UNWRAP_OBJECT(HTMLObjectElement, aObj, htmlobject);
|
2013-03-17 18:42:05 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
rv = UnwrapObject<prototypes::id::HTMLEmbedElement,
|
2013-11-21 16:51:15 +04:00
|
|
|
HTMLSharedObjectElement>(aObj, htmlobject);
|
2013-03-17 18:42:05 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
rv = UnwrapObject<prototypes::id::HTMLAppletElement,
|
2013-11-21 16:51:15 +04:00
|
|
|
HTMLSharedObjectElement>(aObj, htmlobject);
|
2013-03-17 18:42:05 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
htmlobject = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (htmlobject) {
|
2013-01-07 03:55:14 +04:00
|
|
|
htmlobject->SetupProtoChain(aCx, aObj);
|
|
|
|
}
|
2012-09-26 18:17:46 +04:00
|
|
|
|
2015-02-26 17:40:07 +03:00
|
|
|
// Now we can just return the wrapper
|
2012-09-26 18:17:46 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-08-23 09:17:08 +04:00
|
|
|
GlobalObject::GlobalObject(JSContext* aCx, JSObject* aObject)
|
|
|
|
: mGlobalJSObject(aCx),
|
|
|
|
mCx(aCx),
|
|
|
|
mGlobalObject(nullptr)
|
2012-12-03 20:07:49 +04:00
|
|
|
{
|
2014-06-16 22:08:00 +04:00
|
|
|
MOZ_ASSERT(mCx);
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, aObject);
|
|
|
|
if (js::IsWrapper(obj)) {
|
2015-11-06 21:03:52 +03:00
|
|
|
obj = js::CheckedUnwrap(obj, /* stopAtWindowProxy = */ false);
|
2013-05-04 03:29:08 +04:00
|
|
|
if (!obj) {
|
2013-08-23 09:17:08 +04:00
|
|
|
// We should never end up here on a worker thread, since there shouldn't
|
|
|
|
// be any security wrappers to worry about.
|
|
|
|
if (!MOZ_LIKELY(NS_IsMainThread())) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
2013-09-09 07:29:21 +04:00
|
|
|
Throw(aCx, NS_ERROR_XPC_SECURITY_MANAGER_VETO);
|
2013-08-23 09:17:08 +04:00
|
|
|
return;
|
2012-12-03 20:07:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-21 07:19:43 +04:00
|
|
|
mGlobalJSObject = js::GetGlobalForObjectCrossCompartment(obj);
|
2012-12-03 20:07:49 +04:00
|
|
|
}
|
|
|
|
|
2013-08-23 09:17:08 +04:00
|
|
|
nsISupports*
|
|
|
|
GlobalObject::GetAsSupports() const
|
2012-12-03 20:07:49 +04:00
|
|
|
{
|
2013-08-23 09:17:08 +04:00
|
|
|
if (mGlobalObject) {
|
|
|
|
return mGlobalObject;
|
2012-12-03 20:07:49 +04:00
|
|
|
}
|
|
|
|
|
2014-10-22 19:40:50 +04:00
|
|
|
MOZ_ASSERT(!js::IsWrapper(mGlobalJSObject));
|
|
|
|
|
|
|
|
// Most of our globals are DOM objects. Try that first. Note that this
|
|
|
|
// assumes that either the first nsISupports in the object is the canonical
|
|
|
|
// one or that we don't care about the canonical nsISupports here.
|
|
|
|
mGlobalObject = UnwrapDOMObjectToISupports(mGlobalJSObject);
|
|
|
|
if (mGlobalObject) {
|
2014-03-07 01:05:11 +04:00
|
|
|
return mGlobalObject;
|
2014-02-26 01:34:55 +04:00
|
|
|
}
|
|
|
|
|
2014-10-22 19:40:50 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "All our worker globals are DOM objects");
|
2012-12-03 20:07:49 +04:00
|
|
|
|
2014-10-22 19:40:50 +04:00
|
|
|
// Remove everything below here once all our global objects are using new
|
|
|
|
// bindings. If that ever happens; it would need to include Sandbox and
|
|
|
|
// BackstagePass.
|
|
|
|
|
|
|
|
// See whether mGlobalJSObject is an XPCWrappedNative. This will redo the
|
|
|
|
// IsWrapper bit above and the UnwrapDOMObjectToISupports in the case when
|
|
|
|
// we're not actually an XPCWrappedNative, but this should be a rare-ish case
|
|
|
|
// anyway.
|
|
|
|
mGlobalObject = xpc::UnwrapReflectorToISupports(mGlobalJSObject);
|
|
|
|
if (mGlobalObject) {
|
|
|
|
return mGlobalObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
// And now a final hack. Sandbox is not a reflector, but it does have an
|
|
|
|
// nsIGlobalObject hanging out in its private slot. Handle that case here,
|
|
|
|
// (though again, this will do the useless UnwrapDOMObjectToISupports if we
|
|
|
|
// got here for something that is somehow not a DOM object, not an
|
|
|
|
// XPCWrappedNative _and_ not a Sandbox).
|
|
|
|
if (XPCConvert::GetISupportsFromJSObject(mGlobalJSObject, &mGlobalObject)) {
|
|
|
|
return mGlobalObject;
|
2012-12-03 20:07:49 +04:00
|
|
|
}
|
|
|
|
|
2014-10-22 19:40:50 +04:00
|
|
|
MOZ_ASSERT(!mGlobalObject);
|
|
|
|
|
|
|
|
Throw(mCx, NS_ERROR_XPC_BAD_CONVERT_JS);
|
|
|
|
return nullptr;
|
2012-12-03 20:07:49 +04:00
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
InterfaceHasInstance(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
JS::Handle<JSObject*> instance,
|
2013-08-09 02:53:04 +04:00
|
|
|
bool* bp)
|
2013-01-08 22:05:36 +04:00
|
|
|
{
|
|
|
|
const DOMIfaceAndProtoJSClass* clasp =
|
|
|
|
DOMIfaceAndProtoJSClass::FromJSClass(js::GetObjectClass(obj));
|
|
|
|
|
2015-11-06 21:03:52 +03:00
|
|
|
const DOMJSClass* domClass =
|
|
|
|
GetDOMClass(js::UncheckedUnwrap(instance, /* stopAtWindowProxy = */ false));
|
2013-01-08 22:05:36 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(!domClass || clasp->mPrototypeID != prototypes::id::_ID_Count,
|
|
|
|
"Why do we have a hasInstance hook if we don't have a prototype "
|
|
|
|
"ID?");
|
|
|
|
|
2013-02-22 13:25:24 +04:00
|
|
|
if (domClass &&
|
|
|
|
domClass->mInterfaceChain[clasp->mDepth] == clasp->mPrototypeID) {
|
|
|
|
*bp = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-11 03:47:04 +04:00
|
|
|
if (jsipc::IsWrappedCPOW(instance)) {
|
2013-08-02 03:45:17 +04:00
|
|
|
bool boolp = false;
|
2014-07-11 03:47:04 +04:00
|
|
|
if (!jsipc::DOMInstanceOf(cx, js::CheckedUnwrap(instance), clasp->mPrototypeID,
|
|
|
|
clasp->mDepth, &boolp)) {
|
2013-08-02 03:45:17 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*bp = boolp;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JS::Value> protov(cx);
|
2013-07-26 13:00:38 +04:00
|
|
|
DebugOnly<bool> ok = JS_GetProperty(cx, obj, "prototype", &protov);
|
2013-02-22 13:25:24 +04:00
|
|
|
MOZ_ASSERT(ok, "Someone messed with our prototype property?");
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> interfacePrototype(cx, &protov.toObject());
|
2013-02-22 13:25:24 +04:00
|
|
|
MOZ_ASSERT(IsDOMIfaceAndProtoClass(js::GetObjectClass(interfacePrototype)),
|
|
|
|
"Someone messed with our prototype property?");
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> proto(cx);
|
2013-07-31 20:20:33 +04:00
|
|
|
if (!JS_GetPrototype(cx, instance, &proto)) {
|
2013-02-22 13:25:24 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (proto) {
|
|
|
|
if (proto == interfacePrototype) {
|
|
|
|
*bp = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-31 20:20:33 +04:00
|
|
|
if (!JS_GetPrototype(cx, proto, &proto)) {
|
2013-02-22 13:25:24 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*bp = false;
|
2013-01-08 22:05:36 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
bool
|
2013-06-21 17:12:46 +04:00
|
|
|
InterfaceHasInstance(JSContext* cx, JS::Handle<JSObject*> obj, JS::MutableHandle<JS::Value> vp,
|
2013-08-09 02:53:04 +04:00
|
|
|
bool* bp)
|
2013-01-08 22:05:36 +04:00
|
|
|
{
|
|
|
|
if (!vp.isObject()) {
|
|
|
|
*bp = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> instanceObject(cx, &vp.toObject());
|
|
|
|
return InterfaceHasInstance(cx, obj, instanceObject, bp);
|
2013-01-08 22:05:36 +04:00
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
bool
|
2013-08-02 03:45:17 +04:00
|
|
|
InterfaceHasInstance(JSContext* cx, int prototypeID, int depth,
|
|
|
|
JS::Handle<JSObject*> instance,
|
2013-08-09 02:53:04 +04:00
|
|
|
bool* bp)
|
2013-08-02 03:45:17 +04:00
|
|
|
{
|
2014-06-19 00:19:19 +04:00
|
|
|
const DOMJSClass* domClass = GetDOMClass(js::UncheckedUnwrap(instance));
|
2013-08-02 03:45:17 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(!domClass || prototypeID != prototypes::id::_ID_Count,
|
|
|
|
"Why do we have a hasInstance hook if we don't have a prototype "
|
|
|
|
"ID?");
|
|
|
|
|
|
|
|
*bp = (domClass && domClass->mInterfaceChain[depth] == prototypeID);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-08 23:50:58 +04:00
|
|
|
bool
|
2013-09-25 22:38:30 +04:00
|
|
|
ReportLenientThisUnwrappingFailure(JSContext* cx, JSObject* obj)
|
2013-04-09 01:04:21 +04:00
|
|
|
{
|
2013-09-25 22:38:30 +04:00
|
|
|
JS::Rooted<JSObject*> rootedObj(cx, obj);
|
|
|
|
GlobalObject global(cx, rootedObj);
|
2013-05-08 23:50:58 +04:00
|
|
|
if (global.Failed()) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(global.GetAsSupports());
|
2013-04-09 01:04:21 +04:00
|
|
|
if (window && window->GetDoc()) {
|
|
|
|
window->GetDoc()->WarnOnceAbout(nsIDocument::eLenientThis);
|
|
|
|
}
|
2013-05-08 23:50:58 +04:00
|
|
|
return true;
|
2013-04-09 01:04:21 +04:00
|
|
|
}
|
|
|
|
|
2013-05-20 21:47:08 +04:00
|
|
|
bool
|
2015-01-08 19:57:19 +03:00
|
|
|
GetContentGlobalForJSImplementedObject(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
nsIGlobalObject** globalObj)
|
2013-05-20 21:47:08 +04:00
|
|
|
{
|
|
|
|
// Be very careful to not get tricked here.
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!xpc::AccessCheck::isChrome(js::GetObjectCompartment(obj))) {
|
|
|
|
NS_RUNTIMEABORT("Should have a chrome object here");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the content-side object.
|
|
|
|
JS::Rooted<JS::Value> domImplVal(cx);
|
2013-07-26 13:00:38 +04:00
|
|
|
if (!JS_GetProperty(cx, obj, "__DOM_IMPL__", &domImplVal)) {
|
2013-05-20 21:47:08 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!domImplVal.isObject()) {
|
2013-06-17 21:07:03 +04:00
|
|
|
ThrowErrorMessage(cx, MSG_NOT_OBJECT, "Value");
|
2013-05-20 21:47:08 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go ahead and get the global from it. GlobalObject will handle
|
|
|
|
// doing unwrapping as needed.
|
|
|
|
GlobalObject global(cx, &domImplVal.toObject());
|
|
|
|
if (global.Failed()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-08 19:57:19 +03:00
|
|
|
DebugOnly<nsresult> rv = CallQueryInterface(global.GetAsSupports(), globalObj);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
MOZ_ASSERT(*globalObj);
|
2013-05-20 21:47:08 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-08 19:57:19 +03:00
|
|
|
already_AddRefed<nsIGlobalObject>
|
2016-03-12 00:43:31 +03:00
|
|
|
ConstructJSImplementation(const char* aContractId,
|
2013-06-01 10:56:23 +04:00
|
|
|
const GlobalObject& aGlobal,
|
|
|
|
JS::MutableHandle<JSObject*> aObject,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2015-01-08 19:57:19 +03:00
|
|
|
// Get the global object to use as a parent and for initialization.
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!global) {
|
2013-06-01 10:56:23 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-12 00:43:31 +03:00
|
|
|
ConstructJSImplementation(aContractId, global, aObject, aRv);
|
2014-03-28 08:03:03 +04:00
|
|
|
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-01-08 19:57:19 +03:00
|
|
|
return global.forget();
|
2014-03-28 08:03:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-03-12 00:43:31 +03:00
|
|
|
ConstructJSImplementation(const char* aContractId,
|
2015-01-08 19:57:19 +03:00
|
|
|
nsIGlobalObject* aGlobal,
|
2014-03-28 08:03:03 +04:00
|
|
|
JS::MutableHandle<JSObject*> aObject,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2016-03-12 00:43:31 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-12-12 05:51:58 +04:00
|
|
|
// Make sure to divorce ourselves from the calling JS while creating and
|
2013-06-01 10:56:23 +04:00
|
|
|
// initializing the object, so exceptions from that will get reported
|
|
|
|
// properly, since those are never exceptions that a spec wants to be thrown.
|
2013-12-12 05:51:58 +04:00
|
|
|
{
|
2014-04-15 07:27:00 +04:00
|
|
|
AutoNoJSAPI nojsapi;
|
2013-12-12 05:51:58 +04:00
|
|
|
|
2013-06-01 10:56:23 +04:00
|
|
|
// Get the XPCOM component containing the JS implementation.
|
2014-10-02 04:37:50 +04:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsISupports> implISupports = do_CreateInstance(aContractId, &rv);
|
2013-06-01 10:56:23 +04:00
|
|
|
if (!implISupports) {
|
2014-11-24 19:57:49 +03:00
|
|
|
nsPrintfCString msg("Failed to get JS implementation for contract \"%s\"",
|
|
|
|
aContractId);
|
|
|
|
NS_WARNING(msg.get());
|
2014-10-02 04:37:50 +04:00
|
|
|
aRv.Throw(rv);
|
2014-03-28 08:03:03 +04:00
|
|
|
return;
|
2013-06-01 10:56:23 +04:00
|
|
|
}
|
2015-01-08 19:57:19 +03:00
|
|
|
// Initialize the object, if it implements nsIDOMGlobalPropertyInitializer
|
|
|
|
// and our global is a window.
|
2013-06-01 10:56:23 +04:00
|
|
|
nsCOMPtr<nsIDOMGlobalPropertyInitializer> gpi =
|
|
|
|
do_QueryInterface(implISupports);
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aGlobal);
|
2013-06-01 10:56:23 +04:00
|
|
|
if (gpi) {
|
2016-03-12 00:43:31 +03:00
|
|
|
JS::Rooted<JS::Value> initReturn(nsContentUtils::RootingCxForThread());
|
2015-01-08 19:57:19 +03:00
|
|
|
rv = gpi->Init(window, &initReturn);
|
2013-06-01 10:56:23 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
2014-03-28 08:03:03 +04:00
|
|
|
return;
|
2013-06-01 10:56:23 +04:00
|
|
|
}
|
2013-09-27 03:46:42 +04:00
|
|
|
// With JS-implemented WebIDL, the return value of init() is not used to determine
|
|
|
|
// if init() failed, so init() should only return undefined. Any kind of permission
|
|
|
|
// or pref checking must happen by adding an attribute to the WebIDL interface.
|
|
|
|
if (!initReturn.isUndefined()) {
|
|
|
|
MOZ_ASSERT(false, "The init() method for JS-implemented WebIDL should not return anything");
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
2013-06-01 10:56:23 +04:00
|
|
|
}
|
|
|
|
// Extract the JS implementation from the XPCOM object.
|
|
|
|
nsCOMPtr<nsIXPConnectWrappedJS> implWrapped =
|
2014-10-02 04:37:50 +04:00
|
|
|
do_QueryInterface(implISupports, &rv);
|
2013-06-01 10:56:23 +04:00
|
|
|
MOZ_ASSERT(implWrapped, "Failed to get wrapped JS from XPCOM component.");
|
|
|
|
if (!implWrapped) {
|
2014-10-02 04:37:50 +04:00
|
|
|
aRv.Throw(rv);
|
2014-03-28 08:03:03 +04:00
|
|
|
return;
|
2013-06-01 10:56:23 +04:00
|
|
|
}
|
|
|
|
aObject.set(implWrapped->GetJSObject());
|
|
|
|
if (!aObject) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-13 09:18:35 +04:00
|
|
|
bool
|
|
|
|
NonVoidByteStringToJsval(JSContext *cx, const nsACString &str,
|
|
|
|
JS::MutableHandle<JS::Value> rval)
|
|
|
|
{
|
|
|
|
// ByteStrings are not UTF-8 encoded.
|
|
|
|
JSString* jsStr = JS_NewStringCopyN(cx, str.Data(), str.Length());
|
|
|
|
|
|
|
|
if (!jsStr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
rval.setString(jsStr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-02 00:23:48 +04:00
|
|
|
|
|
|
|
template<typename T> static void
|
2014-11-20 14:58:00 +03:00
|
|
|
NormalizeUSVStringInternal(JSContext* aCx, T& aString)
|
2014-08-02 00:23:48 +04:00
|
|
|
{
|
|
|
|
char16_t* start = aString.BeginWriting();
|
|
|
|
// Must use const here because we can't pass char** to UTF16CharEnumerator as
|
|
|
|
// it expects const char**. Unclear why this is illegal...
|
|
|
|
const char16_t* nextChar = start;
|
|
|
|
const char16_t* end = aString.Data() + aString.Length();
|
|
|
|
while (nextChar < end) {
|
|
|
|
uint32_t enumerated = UTF16CharEnumerator::NextChar(&nextChar, end);
|
|
|
|
if (enumerated == UCS2_REPLACEMENT_CHAR) {
|
|
|
|
int32_t lastCharIndex = (nextChar - start) - 1;
|
|
|
|
start[lastCharIndex] = static_cast<char16_t>(enumerated);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-20 14:58:00 +03:00
|
|
|
NormalizeUSVString(JSContext* aCx, nsAString& aString)
|
2014-08-02 00:23:48 +04:00
|
|
|
{
|
2014-11-20 14:58:00 +03:00
|
|
|
NormalizeUSVStringInternal(aCx, aString);
|
2014-08-02 00:23:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-20 14:58:00 +03:00
|
|
|
NormalizeUSVString(JSContext* aCx, binding_detail::FakeString& aString)
|
2014-08-02 00:23:48 +04:00
|
|
|
{
|
2014-11-20 14:58:00 +03:00
|
|
|
NormalizeUSVStringInternal(aCx, aString);
|
2014-08-02 00:23:48 +04:00
|
|
|
}
|
|
|
|
|
2013-06-13 09:18:35 +04:00
|
|
|
bool
|
|
|
|
ConvertJSValueToByteString(JSContext* cx, JS::Handle<JS::Value> v,
|
2014-07-03 15:05:03 +04:00
|
|
|
bool nullable, nsACString& result)
|
2013-06-13 09:18:35 +04:00
|
|
|
{
|
2013-09-30 18:29:10 +04:00
|
|
|
JS::Rooted<JSString*> s(cx);
|
2013-06-13 09:18:35 +04:00
|
|
|
if (v.isString()) {
|
|
|
|
s = v.toString();
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (nullable && v.isNullOrUndefined()) {
|
|
|
|
result.SetIsVoid(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-16 16:31:36 +04:00
|
|
|
s = JS::ToString(cx, v);
|
2013-06-13 09:18:35 +04:00
|
|
|
if (!s) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-02 15:04:14 +04:00
|
|
|
// Conversion from Javascript string to ByteString is only valid if all
|
|
|
|
// characters < 256. This is always the case for Latin1 strings.
|
2013-06-13 09:18:35 +04:00
|
|
|
size_t length;
|
2014-07-03 15:05:03 +04:00
|
|
|
if (!js::StringHasLatin1Chars(s)) {
|
2014-07-02 15:04:14 +04:00
|
|
|
// ThrowErrorMessage can GC, so we first scan the string for bad chars
|
|
|
|
// and report the error outside the AutoCheckCannotGC scope.
|
|
|
|
bool foundBadChar = false;
|
|
|
|
size_t badCharIndex;
|
2014-07-22 08:43:21 +04:00
|
|
|
char16_t badChar;
|
2014-07-02 15:04:14 +04:00
|
|
|
{
|
|
|
|
JS::AutoCheckCannotGC nogc;
|
2014-07-22 08:43:21 +04:00
|
|
|
const char16_t* chars = JS_GetTwoByteStringCharsAndLength(cx, nogc, s, &length);
|
2014-07-02 15:04:14 +04:00
|
|
|
if (!chars) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-13 09:18:35 +04:00
|
|
|
|
2014-07-02 15:04:14 +04:00
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
|
|
if (chars[i] > 255) {
|
|
|
|
badCharIndex = i;
|
|
|
|
badChar = chars[i];
|
|
|
|
foundBadChar = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundBadChar) {
|
|
|
|
MOZ_ASSERT(badCharIndex < length);
|
|
|
|
MOZ_ASSERT(badChar > 255);
|
2013-06-13 09:18:35 +04:00
|
|
|
// The largest unsigned 64 bit number (18,446,744,073,709,551,615) has
|
|
|
|
// 20 digits, plus one more for the null terminator.
|
|
|
|
char index[21];
|
2013-07-18 21:59:53 +04:00
|
|
|
static_assert(sizeof(size_t) <= 8, "index array too small");
|
2016-02-17 22:23:39 +03:00
|
|
|
snprintf_literal(index, "%d", badCharIndex);
|
2014-07-22 08:43:21 +04:00
|
|
|
// A char16_t is 16 bits long. The biggest unsigned 16 bit
|
2013-06-13 09:18:35 +04:00
|
|
|
// number (65,535) has 5 digits, plus one more for the null
|
|
|
|
// terminator.
|
2014-07-02 15:04:14 +04:00
|
|
|
char badCharArray[6];
|
2014-07-22 08:43:21 +04:00
|
|
|
static_assert(sizeof(char16_t) <= 2, "badCharArray too small");
|
2016-02-17 22:23:39 +03:00
|
|
|
snprintf_literal(badCharArray, "%d", badChar);
|
2014-07-02 15:04:14 +04:00
|
|
|
ThrowErrorMessage(cx, MSG_INVALID_BYTESTRING, index, badCharArray);
|
2013-06-13 09:18:35 +04:00
|
|
|
return false;
|
|
|
|
}
|
2014-07-02 15:04:14 +04:00
|
|
|
} else {
|
2014-07-03 15:05:03 +04:00
|
|
|
length = js::GetStringLength(s);
|
2013-06-13 09:18:35 +04:00
|
|
|
}
|
|
|
|
|
2014-07-03 12:03:56 +04:00
|
|
|
static_assert(js::MaxStringLength < UINT32_MAX,
|
|
|
|
"length+1 shouldn't overflow");
|
|
|
|
|
2016-03-17 06:04:10 +03:00
|
|
|
if (!result.SetLength(length, fallible)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-19 17:23:19 +04:00
|
|
|
JS_EncodeStringToBuffer(cx, s, result.BeginWriting(), length);
|
2013-06-13 09:18:35 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-05 22:38:17 +04:00
|
|
|
bool
|
|
|
|
IsInPrivilegedApp(JSContext* aCx, JSObject* aObj)
|
|
|
|
{
|
|
|
|
using mozilla::dom::workers::GetWorkerPrivateFromContext;
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return GetWorkerPrivateFromContext(aCx)->IsInPrivilegedApp();
|
|
|
|
}
|
|
|
|
|
2014-05-13 13:58:00 +04:00
|
|
|
nsIPrincipal* principal = nsContentUtils::ObjectPrincipal(aObj);
|
2014-02-05 22:38:17 +04:00
|
|
|
uint16_t appStatus = principal->GetAppStatus();
|
|
|
|
return (appStatus == nsIPrincipal::APP_STATUS_CERTIFIED ||
|
2014-05-09 01:16:00 +04:00
|
|
|
appStatus == nsIPrincipal::APP_STATUS_PRIVILEGED) ||
|
|
|
|
Preferences::GetBool("dom.ignore_webidl_scope_checks", false);
|
2014-02-05 22:38:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsInCertifiedApp(JSContext* aCx, JSObject* aObj)
|
|
|
|
{
|
|
|
|
using mozilla::dom::workers::GetWorkerPrivateFromContext;
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return GetWorkerPrivateFromContext(aCx)->IsInCertifiedApp();
|
|
|
|
}
|
|
|
|
|
2014-05-13 13:58:00 +04:00
|
|
|
nsIPrincipal* principal = nsContentUtils::ObjectPrincipal(aObj);
|
2014-05-09 01:16:00 +04:00
|
|
|
return principal->GetAppStatus() == nsIPrincipal::APP_STATUS_CERTIFIED ||
|
|
|
|
Preferences::GetBool("dom.ignore_webidl_scope_checks", false);
|
2014-02-05 22:38:17 +04:00
|
|
|
}
|
|
|
|
|
2013-11-19 10:05:42 +04:00
|
|
|
void
|
|
|
|
FinalizeGlobal(JSFreeOp* aFreeOp, JSObject* aObj)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(js::GetObjectClass(aObj)->flags & JSCLASS_DOM_GLOBAL);
|
|
|
|
mozilla::dom::DestroyProtoAndIfaceCache(aObj);
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
bool
|
|
|
|
ResolveGlobal(JSContext* aCx, JS::Handle<JSObject*> aObj,
|
2014-11-08 03:07:12 +03:00
|
|
|
JS::Handle<jsid> aId, bool* aResolvedp)
|
2013-11-05 18:16:26 +04:00
|
|
|
{
|
2015-03-06 09:08:06 +03:00
|
|
|
MOZ_ASSERT(JS_IsGlobalObject(aObj),
|
|
|
|
"Should have a global here, since we plan to resolve standard "
|
|
|
|
"classes!");
|
|
|
|
|
2014-11-08 03:07:12 +03:00
|
|
|
return JS_ResolveStandardClass(aCx, aObj, aId, aResolvedp);
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
|
|
|
|
2015-04-28 19:25:55 +03:00
|
|
|
bool
|
|
|
|
MayResolveGlobal(const JSAtomState& aNames, jsid aId, JSObject* aMaybeObj)
|
|
|
|
{
|
|
|
|
return JS_MayResolveStandardClass(aNames, aId, aMaybeObj);
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
bool
|
|
|
|
EnumerateGlobal(JSContext* aCx, JS::Handle<JSObject*> aObj)
|
|
|
|
{
|
2015-03-06 09:08:06 +03:00
|
|
|
MOZ_ASSERT(JS_IsGlobalObject(aObj),
|
|
|
|
"Should have a global here, since we plan to enumerate standard "
|
|
|
|
"classes!");
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
return JS_EnumerateStandardClasses(aCx, aObj);
|
|
|
|
}
|
|
|
|
|
2014-05-25 22:31:07 +04:00
|
|
|
bool
|
2015-07-14 05:25:26 +03:00
|
|
|
CheckAnyPermissions(JSContext* aCx, JSObject* aObj, const char* const aPermissions[])
|
2014-05-25 22:31:07 +04:00
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> rootedObj(aCx, aObj);
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* window = xpc::WindowGlobalOrNull(rootedObj)->AsInner();
|
2014-05-25 22:31:07 +04:00
|
|
|
if (!window) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPermissionManager> permMgr = services::GetPermissionManager();
|
|
|
|
NS_ENSURE_TRUE(permMgr, false);
|
|
|
|
|
|
|
|
do {
|
|
|
|
uint32_t permission = nsIPermissionManager::DENY_ACTION;
|
|
|
|
permMgr->TestPermissionFromWindow(window, *aPermissions, &permission);
|
|
|
|
if (permission == nsIPermissionManager::ALLOW_ACTION) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} while (*(++aPermissions));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-03 11:03:35 +03:00
|
|
|
bool
|
|
|
|
CheckAllPermissions(JSContext* aCx, JSObject* aObj, const char* const aPermissions[])
|
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> rootedObj(aCx, aObj);
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* window = xpc::WindowGlobalOrNull(rootedObj)->AsInner();
|
2015-07-03 11:03:35 +03:00
|
|
|
if (!window) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPermissionManager> permMgr = services::GetPermissionManager();
|
|
|
|
NS_ENSURE_TRUE(permMgr, false);
|
|
|
|
|
|
|
|
do {
|
|
|
|
uint32_t permission = nsIPermissionManager::DENY_ACTION;
|
|
|
|
permMgr->TestPermissionFromWindow(window, *aPermissions, &permission);
|
|
|
|
if (permission != nsIPermissionManager::ALLOW_ACTION) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} while (*(++aPermissions));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-01 20:02:36 +03:00
|
|
|
bool
|
|
|
|
IsNonExposedGlobal(JSContext* aCx, JSObject* aGlobal,
|
|
|
|
uint32_t aNonExposedGlobals)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNonExposedGlobals, "Why did we get called?");
|
|
|
|
MOZ_ASSERT((aNonExposedGlobals &
|
|
|
|
~(GlobalNames::Window |
|
|
|
|
GlobalNames::BackstagePass |
|
|
|
|
GlobalNames::DedicatedWorkerGlobalScope |
|
|
|
|
GlobalNames::SharedWorkerGlobalScope |
|
|
|
|
GlobalNames::ServiceWorkerGlobalScope |
|
|
|
|
GlobalNames::WorkerDebuggerGlobalScope)) == 0,
|
|
|
|
"Unknown non-exposed global type");
|
|
|
|
|
|
|
|
const char* name = js::GetObjectClass(aGlobal)->name;
|
|
|
|
|
|
|
|
if ((aNonExposedGlobals & GlobalNames::Window) &&
|
|
|
|
!strcmp(name, "Window")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((aNonExposedGlobals & GlobalNames::BackstagePass) &&
|
|
|
|
!strcmp(name, "BackstagePass")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((aNonExposedGlobals & GlobalNames::DedicatedWorkerGlobalScope) &&
|
|
|
|
!strcmp(name, "DedicatedWorkerGlobalScope")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((aNonExposedGlobals & GlobalNames::SharedWorkerGlobalScope) &&
|
|
|
|
!strcmp(name, "SharedWorkerGlobalScope")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((aNonExposedGlobals & GlobalNames::ServiceWorkerGlobalScope) &&
|
|
|
|
!strcmp(name, "ServiceWorkerGlobalScope")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((aNonExposedGlobals & GlobalNames::WorkerDebuggerGlobalScope) &&
|
|
|
|
!strcmp(name, "WorkerDebuggerGlobalScopex")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-09 02:06:33 +03:00
|
|
|
void
|
2016-01-30 20:05:36 +03:00
|
|
|
HandlePrerenderingViolation(nsPIDOMWindowInner* aWindow)
|
2015-01-09 02:06:33 +03:00
|
|
|
{
|
|
|
|
// Suspend the window and its workers, and its children too.
|
|
|
|
aWindow->SuspendTimeouts();
|
|
|
|
|
|
|
|
// Suspend event handling on the document
|
|
|
|
nsCOMPtr<nsIDocument> doc = aWindow->GetExtantDoc();
|
|
|
|
if (doc) {
|
|
|
|
doc->SuppressEventHandling(nsIDocument::eEvents);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 03:32:19 +04:00
|
|
|
bool
|
2015-01-09 02:06:33 +03:00
|
|
|
EnforceNotInPrerendering(JSContext* aCx, JSObject* aObj)
|
2014-08-21 03:32:19 +04:00
|
|
|
{
|
2015-01-09 02:06:33 +03:00
|
|
|
JS::Rooted<JSObject*> thisObj(aCx, js::CheckedUnwrap(aObj));
|
|
|
|
if (!thisObj) {
|
|
|
|
// Without a this object, we cannot check the safety.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
nsGlobalWindow* window = xpc::WindowGlobalOrNull(thisObj);
|
|
|
|
if (!window) {
|
|
|
|
// Without a window, we cannot check the safety.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-04 09:16:21 +03:00
|
|
|
if (window->GetIsPrerendered()) {
|
2016-01-30 20:05:36 +03:00
|
|
|
HandlePrerenderingViolation(window->AsInner());
|
2015-01-09 02:06:33 +03:00
|
|
|
// When the bindings layer sees a false return value, it returns false form
|
|
|
|
// the JSNative in order to trigger an uncatchable exception.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-08-21 03:32:19 +04:00
|
|
|
}
|
|
|
|
|
2014-02-08 00:56:14 +04:00
|
|
|
bool
|
|
|
|
GenericBindingGetter(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
|
|
|
const JSJitInfo *info = FUNCTION_VALUE_TO_JITINFO(args.calleev());
|
|
|
|
prototypes::ID protoID = static_cast<prototypes::ID>(info->protoID);
|
|
|
|
if (!args.thisv().isObject()) {
|
2016-01-06 11:54:00 +03:00
|
|
|
return ThrowInvalidThis(cx, args, false, protoID);
|
2014-02-08 00:56:14 +04:00
|
|
|
}
|
|
|
|
JS::Rooted<JSObject*> obj(cx, &args.thisv().toObject());
|
|
|
|
|
|
|
|
void* self;
|
|
|
|
{
|
|
|
|
nsresult rv = UnwrapObject<void>(obj, self, protoID, info->depth);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return ThrowInvalidThis(cx, args,
|
2016-01-06 11:54:00 +03:00
|
|
|
rv == NS_ERROR_XPC_SECURITY_MANAGER_VETO,
|
2014-02-08 00:56:14 +04:00
|
|
|
protoID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(info->type() == JSJitInfo::Getter);
|
|
|
|
JSJitGetterOp getter = info->getter;
|
2014-05-15 21:26:23 +04:00
|
|
|
bool ok = getter(cx, obj, self, JSJitGetterCallArgs(args));
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (ok) {
|
|
|
|
AssertReturnTypeMatchesJitinfo(info, args.rval());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return ok;
|
2014-02-08 00:56:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GenericBindingSetter(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
|
|
|
const JSJitInfo *info = FUNCTION_VALUE_TO_JITINFO(args.calleev());
|
|
|
|
prototypes::ID protoID = static_cast<prototypes::ID>(info->protoID);
|
|
|
|
if (!args.thisv().isObject()) {
|
2016-01-06 11:54:00 +03:00
|
|
|
return ThrowInvalidThis(cx, args, false, protoID);
|
2014-02-08 00:56:14 +04:00
|
|
|
}
|
|
|
|
JS::Rooted<JSObject*> obj(cx, &args.thisv().toObject());
|
|
|
|
|
|
|
|
void* self;
|
|
|
|
{
|
|
|
|
nsresult rv = UnwrapObject<void>(obj, self, protoID, info->depth);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return ThrowInvalidThis(cx, args,
|
2016-01-06 11:54:00 +03:00
|
|
|
rv == NS_ERROR_XPC_SECURITY_MANAGER_VETO,
|
2014-02-08 00:56:14 +04:00
|
|
|
protoID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (args.length() == 0) {
|
|
|
|
return ThrowNoSetterArg(cx, protoID);
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(info->type() == JSJitInfo::Setter);
|
|
|
|
JSJitSetterOp setter = info->setter;
|
|
|
|
if (!setter(cx, obj, self, JSJitSetterCallArgs(args))) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-15 21:26:23 +04:00
|
|
|
args.rval().setUndefined();
|
|
|
|
#ifdef DEBUG
|
|
|
|
AssertReturnTypeMatchesJitinfo(info, args.rval());
|
|
|
|
#endif
|
2014-02-08 00:56:14 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GenericBindingMethod(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
|
|
|
const JSJitInfo *info = FUNCTION_VALUE_TO_JITINFO(args.calleev());
|
|
|
|
prototypes::ID protoID = static_cast<prototypes::ID>(info->protoID);
|
|
|
|
if (!args.thisv().isObject()) {
|
2016-01-06 11:54:00 +03:00
|
|
|
return ThrowInvalidThis(cx, args, false, protoID);
|
2014-02-08 00:56:14 +04:00
|
|
|
}
|
|
|
|
JS::Rooted<JSObject*> obj(cx, &args.thisv().toObject());
|
|
|
|
|
|
|
|
void* self;
|
|
|
|
{
|
|
|
|
nsresult rv = UnwrapObject<void>(obj, self, protoID, info->depth);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return ThrowInvalidThis(cx, args,
|
2016-01-06 11:54:00 +03:00
|
|
|
rv == NS_ERROR_XPC_SECURITY_MANAGER_VETO,
|
2014-02-08 00:56:14 +04:00
|
|
|
protoID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(info->type() == JSJitInfo::Method);
|
|
|
|
JSJitMethodOp method = info->method;
|
2014-05-15 21:26:23 +04:00
|
|
|
bool ok = method(cx, obj, self, JSJitMethodCallArgs(args));
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (ok) {
|
|
|
|
AssertReturnTypeMatchesJitinfo(info, args.rval());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return ok;
|
2014-02-08 00:56:14 +04:00
|
|
|
}
|
|
|
|
|
2014-03-21 20:18:24 +04:00
|
|
|
bool
|
|
|
|
GenericPromiseReturningBindingMethod(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
|
|
|
// Make sure to save the callee before someone maybe messes with rval().
|
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
|
|
|
JS::Rooted<JSObject*> callee(cx, &args.callee());
|
|
|
|
|
|
|
|
// We could invoke GenericBindingMethod here, but that involves an
|
|
|
|
// extra call. Manually inline it instead.
|
|
|
|
const JSJitInfo *info = FUNCTION_VALUE_TO_JITINFO(args.calleev());
|
|
|
|
prototypes::ID protoID = static_cast<prototypes::ID>(info->protoID);
|
|
|
|
if (!args.thisv().isObject()) {
|
2016-01-06 11:54:00 +03:00
|
|
|
ThrowInvalidThis(cx, args, false, protoID);
|
2014-03-21 20:18:24 +04:00
|
|
|
return ConvertExceptionToPromise(cx, xpc::XrayAwareCalleeGlobal(callee),
|
|
|
|
args.rval());
|
|
|
|
}
|
|
|
|
JS::Rooted<JSObject*> obj(cx, &args.thisv().toObject());
|
|
|
|
|
|
|
|
void* self;
|
|
|
|
{
|
|
|
|
nsresult rv = UnwrapObject<void>(obj, self, protoID, info->depth);
|
|
|
|
if (NS_FAILED(rv)) {
|
2016-01-06 11:54:00 +03:00
|
|
|
ThrowInvalidThis(cx, args, rv == NS_ERROR_XPC_SECURITY_MANAGER_VETO,
|
2014-03-21 20:18:24 +04:00
|
|
|
protoID);
|
|
|
|
return ConvertExceptionToPromise(cx, xpc::XrayAwareCalleeGlobal(callee),
|
|
|
|
args.rval());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(info->type() == JSJitInfo::Method);
|
|
|
|
JSJitMethodOp method = info->method;
|
|
|
|
bool ok = method(cx, obj, self, JSJitMethodCallArgs(args));
|
|
|
|
if (ok) {
|
2014-05-15 21:26:23 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
AssertReturnTypeMatchesJitinfo(info, args.rval());
|
|
|
|
#endif
|
2014-03-21 20:18:24 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-15 21:26:23 +04:00
|
|
|
// Promise-returning methods always return objects
|
|
|
|
MOZ_ASSERT(info->returnType() == JSVAL_TYPE_OBJECT);
|
2014-03-21 20:18:24 +04:00
|
|
|
return ConvertExceptionToPromise(cx, xpc::XrayAwareCalleeGlobal(callee),
|
|
|
|
args.rval());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
StaticMethodPromiseWrapper(JSContext* cx, unsigned argc, JS::Value* vp)
|
|
|
|
{
|
|
|
|
// Make sure to save the callee before someone maybe messes with rval().
|
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
|
|
|
JS::Rooted<JSObject*> callee(cx, &args.callee());
|
|
|
|
|
|
|
|
const JSJitInfo *info = FUNCTION_VALUE_TO_JITINFO(args.calleev());
|
|
|
|
MOZ_ASSERT(info);
|
|
|
|
MOZ_ASSERT(info->type() == JSJitInfo::StaticMethod);
|
|
|
|
|
|
|
|
bool ok = info->staticMethod(cx, argc, vp);
|
|
|
|
if (ok) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-21 20:18:24 +04:00
|
|
|
return ConvertExceptionToPromise(cx, xpc::XrayAwareCalleeGlobal(callee),
|
|
|
|
args.rval());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ConvertExceptionToPromise(JSContext* cx,
|
|
|
|
JSObject* promiseScope,
|
|
|
|
JS::MutableHandle<JS::Value> rval)
|
|
|
|
{
|
2016-02-10 01:40:31 +03:00
|
|
|
#ifndef SPIDERMONKEY_PROMISE
|
2014-03-21 20:18:24 +04:00
|
|
|
GlobalObject global(cx, promiseScope);
|
|
|
|
if (global.Failed()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> exn(cx);
|
|
|
|
if (!JS_GetPendingException(cx, &exn)) {
|
2015-02-21 07:58:36 +03:00
|
|
|
// This is very important: if there is no pending exception here but we're
|
|
|
|
// ending up in this code, that means the callee threw an uncatchable
|
|
|
|
// exception. Just propagate that out as-is.
|
2014-03-21 20:18:24 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_ClearPendingException(cx);
|
2015-11-25 23:48:08 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> globalObj =
|
|
|
|
do_QueryInterface(global.GetAsSupports());
|
|
|
|
if (!globalObj) {
|
|
|
|
ErrorResult rv;
|
|
|
|
rv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return !rv.MaybeSetPendingException(cx);
|
|
|
|
}
|
|
|
|
|
2014-03-21 20:18:24 +04:00
|
|
|
ErrorResult rv;
|
2015-11-25 23:48:08 +03:00
|
|
|
RefPtr<Promise> promise = Promise::Reject(globalObj, cx, exn, rv);
|
2015-11-21 00:29:40 +03:00
|
|
|
if (rv.MaybeSetPendingException(cx)) {
|
|
|
|
// We just give up. We put the exception from the ErrorResult on
|
|
|
|
// the JSContext just to make sure to not leak memory on the
|
|
|
|
// ErrorResult, but now just put the original exception back.
|
2014-03-21 20:18:24 +04:00
|
|
|
JS_SetPendingException(cx, exn);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-26 22:25:20 +03:00
|
|
|
return GetOrCreateDOMReflector(cx, promise, rval);
|
2016-02-10 01:40:31 +03:00
|
|
|
#else // SPIDERMONKEY_PROMISE
|
|
|
|
{
|
|
|
|
JSAutoCompartment ac(cx, promiseScope);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> exn(cx);
|
|
|
|
if (!JS_GetPendingException(cx, &exn)) {
|
|
|
|
// This is very important: if there is no pending exception here but we're
|
|
|
|
// ending up in this code, that means the callee threw an uncatchable
|
|
|
|
// exception. Just propagate that out as-is.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_ClearPendingException(cx);
|
|
|
|
|
|
|
|
JSObject* promise = JS::CallOriginalPromiseReject(cx, exn);
|
|
|
|
if (!promise) {
|
|
|
|
// We just give up. Put the exception back.
|
|
|
|
JS_SetPendingException(cx, exn);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rval.setObject(*promise);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now make sure we rewrap promise back into the compartment we want
|
|
|
|
return JS_WrapValue(cx, rval);
|
|
|
|
#endif // SPIDERMONKEY_PROMISE
|
2014-03-21 20:18:24 +04:00
|
|
|
}
|
|
|
|
|
2014-04-08 22:48:37 +04:00
|
|
|
/* static */
|
|
|
|
void
|
|
|
|
CreateGlobalOptions<nsGlobalWindow>::TraceGlobal(JSTracer* aTrc, JSObject* aObj)
|
|
|
|
{
|
2014-07-29 19:48:32 +04:00
|
|
|
xpc::TraceXPCGlobal(aTrc, aObj);
|
2014-04-08 22:48:37 +04:00
|
|
|
}
|
|
|
|
|
2014-08-08 17:03:29 +04:00
|
|
|
static bool sRegisteredDOMNames = false;
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RegisterDOMNames()
|
|
|
|
{
|
|
|
|
if (sRegisteredDOMNames) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = nsDOMClassInfo::Init();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ERROR("Could not initialize nsDOMClassInfo");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register new DOM bindings
|
|
|
|
nsScriptNameSpaceManager* nameSpaceManager = GetNameSpaceManager();
|
|
|
|
if (!nameSpaceManager) {
|
|
|
|
NS_ERROR("Could not initialize nsScriptNameSpaceManager");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mozilla::dom::Register(nameSpaceManager);
|
|
|
|
|
|
|
|
sRegisteredDOMNames = true;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-08 22:48:37 +04:00
|
|
|
/* static */
|
|
|
|
bool
|
|
|
|
CreateGlobalOptions<nsGlobalWindow>::PostCreateGlobal(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGlobal)
|
|
|
|
{
|
2014-08-08 17:03:29 +04:00
|
|
|
nsresult rv = RegisterDOMNames();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return Throw(aCx, rv);
|
|
|
|
}
|
|
|
|
|
2014-07-03 20:40:52 +04:00
|
|
|
// Invoking the XPCWrappedNativeScope constructor automatically hooks it
|
|
|
|
// up to the compartment of aGlobal.
|
|
|
|
(void) new XPCWrappedNativeScope(aCx, aGlobal);
|
|
|
|
return true;
|
2014-04-08 22:48:37 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 21:26:23 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
|
|
AssertReturnTypeMatchesJitinfo(const JSJitInfo* aJitInfo,
|
|
|
|
JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
|
|
|
switch (aJitInfo->returnType()) {
|
|
|
|
case JSVAL_TYPE_UNKNOWN:
|
|
|
|
// Any value is good.
|
|
|
|
break;
|
|
|
|
case JSVAL_TYPE_DOUBLE:
|
|
|
|
// The value could actually be an int32 value as well.
|
|
|
|
MOZ_ASSERT(aValue.isNumber());
|
|
|
|
break;
|
|
|
|
case JSVAL_TYPE_INT32:
|
|
|
|
MOZ_ASSERT(aValue.isInt32());
|
|
|
|
break;
|
|
|
|
case JSVAL_TYPE_UNDEFINED:
|
|
|
|
MOZ_ASSERT(aValue.isUndefined());
|
|
|
|
break;
|
|
|
|
case JSVAL_TYPE_BOOLEAN:
|
|
|
|
MOZ_ASSERT(aValue.isBoolean());
|
|
|
|
break;
|
|
|
|
case JSVAL_TYPE_STRING:
|
|
|
|
MOZ_ASSERT(aValue.isString());
|
|
|
|
break;
|
|
|
|
case JSVAL_TYPE_NULL:
|
|
|
|
MOZ_ASSERT(aValue.isNull());
|
|
|
|
break;
|
|
|
|
case JSVAL_TYPE_OBJECT:
|
|
|
|
MOZ_ASSERT(aValue.isObject());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Someone messed up their jitinfo type.
|
|
|
|
MOZ_ASSERT(false, "Unexpected JSValueType stored in jitinfo");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-08-20 05:12:15 +04:00
|
|
|
bool
|
|
|
|
CallerSubsumes(JSObject *aObject)
|
|
|
|
{
|
|
|
|
nsIPrincipal* objPrin = nsContentUtils::ObjectPrincipal(js::UncheckedUnwrap(aObject));
|
|
|
|
return nsContentUtils::SubjectPrincipal()->Subsumes(objPrin);
|
|
|
|
}
|
|
|
|
|
2014-10-23 23:21:46 +04:00
|
|
|
nsresult
|
|
|
|
UnwrapArgImpl(JS::Handle<JSObject*> src,
|
|
|
|
const nsIID &iid,
|
|
|
|
void **ppArg)
|
|
|
|
{
|
2016-01-30 20:05:36 +03:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsISupports *iface = xpc::UnwrapReflectorToISupports(src);
|
|
|
|
if (iface) {
|
|
|
|
if (NS_FAILED(iface->QueryInterface(iid, ppArg))) {
|
|
|
|
return NS_ERROR_XPC_BAD_CONVERT_JS;
|
2015-01-30 21:54:43 +03:00
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-10-23 23:21:46 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
RefPtr<nsXPCWrappedJS> wrappedJS;
|
|
|
|
nsresult rv = nsXPCWrappedJS::GetNewOrUsed(src, iid, getter_AddRefs(wrappedJS));
|
|
|
|
if (NS_FAILED(rv) || !wrappedJS) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-10-23 23:21:46 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
// We need to go through the QueryInterface logic to make this return
|
|
|
|
// the right thing for the various 'special' interfaces; e.g.
|
|
|
|
// nsIPropertyBag. We must use AggregatedQueryInterface in cases where
|
|
|
|
// there is an outer to avoid nasty recursion.
|
|
|
|
return wrappedJS->QueryInterface(iid, ppArg);
|
|
|
|
}
|
2014-10-23 23:21:46 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsresult
|
|
|
|
UnwrapWindowProxyImpl(JS::Handle<JSObject*> src,
|
|
|
|
nsPIDOMWindowOuter** ppArg)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> inner;
|
|
|
|
nsresult rv = UnwrapArg<nsPIDOMWindowInner>(src, getter_AddRefs(inner));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> outer = inner->GetOuterWindow();
|
|
|
|
outer.forget(ppArg);
|
|
|
|
return NS_OK;
|
2014-10-23 23:21:46 +04:00
|
|
|
}
|
|
|
|
|
2015-03-06 09:08:06 +03:00
|
|
|
bool
|
|
|
|
SystemGlobalResolve(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
JS::Handle<jsid> id, bool* resolvedp)
|
|
|
|
{
|
|
|
|
if (!ResolveGlobal(cx, obj, id, resolvedp)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*resolvedp) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ResolveSystemBinding(cx, obj, id, resolvedp);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SystemGlobalEnumerate(JSContext* cx, JS::Handle<JSObject*> obj)
|
|
|
|
{
|
|
|
|
bool ignored = false;
|
|
|
|
return EnumerateGlobal(cx, obj) &&
|
|
|
|
ResolveSystemBinding(cx, obj, JSID_VOIDHANDLE, &ignored);
|
|
|
|
}
|
|
|
|
|
2015-05-06 09:42:27 +03:00
|
|
|
template<decltype(JS::NewMapObject) Method>
|
|
|
|
bool
|
|
|
|
GetMaplikeSetlikeBackingObject(JSContext* aCx, JS::Handle<JSObject*> aObj,
|
|
|
|
size_t aSlotIndex,
|
|
|
|
JS::MutableHandle<JSObject*> aBackingObj,
|
|
|
|
bool* aBackingObjCreated)
|
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> reflector(aCx);
|
|
|
|
reflector = IsDOMObject(aObj) ? aObj : js::UncheckedUnwrap(aObj,
|
2015-11-06 21:03:52 +03:00
|
|
|
/* stopAtWindowProxy = */ false);
|
2015-05-06 09:42:27 +03:00
|
|
|
|
|
|
|
// Retrieve the backing object from the reserved slot on the maplike/setlike
|
|
|
|
// object. If it doesn't exist yet, create it.
|
|
|
|
JS::Rooted<JS::Value> slotValue(aCx);
|
|
|
|
slotValue = js::GetReservedSlot(reflector, aSlotIndex);
|
|
|
|
if (slotValue.isUndefined()) {
|
|
|
|
// Since backing object access can happen in non-originating compartments,
|
|
|
|
// make sure to create the backing object in reflector compartment.
|
|
|
|
{
|
|
|
|
JSAutoCompartment ac(aCx, reflector);
|
|
|
|
JS::Rooted<JSObject*> newBackingObj(aCx);
|
|
|
|
newBackingObj.set(Method(aCx));
|
|
|
|
if (NS_WARN_IF(!newBackingObj)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
js::SetReservedSlot(reflector, aSlotIndex, JS::ObjectValue(*newBackingObj));
|
|
|
|
}
|
|
|
|
slotValue = js::GetReservedSlot(reflector, aSlotIndex);
|
|
|
|
*aBackingObjCreated = true;
|
|
|
|
} else {
|
|
|
|
*aBackingObjCreated = false;
|
|
|
|
}
|
|
|
|
if (!MaybeWrapNonDOMObjectValue(aCx, &slotValue)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
aBackingObj.set(&slotValue.toObject());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GetMaplikeBackingObject(JSContext* aCx, JS::Handle<JSObject*> aObj,
|
|
|
|
size_t aSlotIndex,
|
|
|
|
JS::MutableHandle<JSObject*> aBackingObj,
|
|
|
|
bool* aBackingObjCreated)
|
|
|
|
{
|
|
|
|
return GetMaplikeSetlikeBackingObject<JS::NewMapObject>(aCx, aObj, aSlotIndex,
|
|
|
|
aBackingObj,
|
|
|
|
aBackingObjCreated);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GetSetlikeBackingObject(JSContext* aCx, JS::Handle<JSObject*> aObj,
|
|
|
|
size_t aSlotIndex,
|
|
|
|
JS::MutableHandle<JSObject*> aBackingObj,
|
|
|
|
bool* aBackingObjCreated)
|
|
|
|
{
|
|
|
|
return GetMaplikeSetlikeBackingObject<JS::NewSetObject>(aCx, aObj, aSlotIndex,
|
|
|
|
aBackingObj,
|
|
|
|
aBackingObjCreated);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ForEachHandler(JSContext* aCx, unsigned aArgc, JS::Value* aVp)
|
|
|
|
{
|
|
|
|
JS::CallArgs args = CallArgsFromVp(aArgc, aVp);
|
|
|
|
// Unpack callback and object from slots
|
|
|
|
JS::Rooted<JS::Value>
|
|
|
|
callbackFn(aCx, js::GetFunctionNativeReserved(&args.callee(),
|
|
|
|
FOREACH_CALLBACK_SLOT));
|
|
|
|
JS::Rooted<JS::Value>
|
|
|
|
maplikeOrSetlikeObj(aCx,
|
|
|
|
js::GetFunctionNativeReserved(&args.callee(),
|
|
|
|
FOREACH_MAPLIKEORSETLIKEOBJ_SLOT));
|
|
|
|
MOZ_ASSERT(aArgc == 3);
|
|
|
|
JS::AutoValueVector newArgs(aCx);
|
|
|
|
// Arguments are passed in as value, key, object. Keep value and key, replace
|
|
|
|
// object with the maplike/setlike object.
|
|
|
|
newArgs.append(args.get(0));
|
|
|
|
newArgs.append(args.get(1));
|
|
|
|
newArgs.append(maplikeOrSetlikeObj);
|
|
|
|
JS::Rooted<JS::Value> rval(aCx, JS::UndefinedValue());
|
|
|
|
// Now actually call the user specified callback
|
|
|
|
return JS::Call(aCx, args.thisv(), callbackFn, newArgs, &rval);
|
|
|
|
}
|
2015-07-13 18:25:42 +03:00
|
|
|
|
2015-07-31 20:30:55 +03:00
|
|
|
static inline prototypes::ID
|
|
|
|
GetProtoIdForNewtarget(JS::Handle<JSObject*> aNewTarget)
|
|
|
|
{
|
|
|
|
const js::Class* newTargetClass = js::GetObjectClass(aNewTarget);
|
|
|
|
if (IsDOMIfaceAndProtoClass(newTargetClass)) {
|
|
|
|
const DOMIfaceAndProtoJSClass* newTargetIfaceClass =
|
|
|
|
DOMIfaceAndProtoJSClass::FromJSClass(newTargetClass);
|
|
|
|
if (newTargetIfaceClass->mType == eInterface) {
|
|
|
|
return newTargetIfaceClass->mPrototypeID;
|
|
|
|
}
|
|
|
|
} else if (JS_IsNativeFunction(aNewTarget, Constructor)) {
|
|
|
|
return GetNativePropertyHooksFromConstructorFunction(aNewTarget)->mPrototypeID;
|
|
|
|
}
|
|
|
|
|
|
|
|
return prototypes::id::_ID_Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GetDesiredProto(JSContext* aCx, const JS::CallArgs& aCallArgs,
|
|
|
|
JS::MutableHandle<JSObject*> aDesiredProto)
|
|
|
|
{
|
|
|
|
if (!aCallArgs.isConstructing()) {
|
|
|
|
aDesiredProto.set(nullptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The desired prototype depends on the actual constructor that was invoked,
|
|
|
|
// which is passed to us as the newTarget in the callargs. We want to do
|
|
|
|
// something akin to the ES6 specification's GetProtototypeFromConstructor (so
|
|
|
|
// get .prototype on the newTarget, with a fallback to some sort of default).
|
|
|
|
|
|
|
|
// First, a fast path for the case when the the constructor is in fact one of
|
|
|
|
// our DOM constructors. This is safe because on those the "constructor"
|
|
|
|
// property is non-configurable and non-writable, so we don't have to do the
|
|
|
|
// slow JS_GetProperty call.
|
|
|
|
JS::Rooted<JSObject*> newTarget(aCx, &aCallArgs.newTarget().toObject());
|
|
|
|
JS::Rooted<JSObject*> originalNewTarget(aCx, newTarget);
|
|
|
|
// See whether we have a known DOM constructor here, such that we can take a
|
|
|
|
// fast path.
|
|
|
|
prototypes::ID protoID = GetProtoIdForNewtarget(newTarget);
|
|
|
|
if (protoID == prototypes::id::_ID_Count) {
|
|
|
|
// We might still have a cross-compartment wrapper for a known DOM
|
|
|
|
// constructor.
|
|
|
|
newTarget = js::CheckedUnwrap(newTarget);
|
|
|
|
if (newTarget && newTarget != originalNewTarget) {
|
|
|
|
protoID = GetProtoIdForNewtarget(newTarget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (protoID != prototypes::id::_ID_Count) {
|
|
|
|
ProtoAndIfaceCache& protoAndIfaceCache =
|
|
|
|
*GetProtoAndIfaceCache(js::GetGlobalForObjectCrossCompartment(newTarget));
|
|
|
|
aDesiredProto.set(protoAndIfaceCache.EntrySlotMustExist(protoID));
|
|
|
|
if (newTarget != originalNewTarget) {
|
|
|
|
return JS_WrapObject(aCx, aDesiredProto);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slow path. This basically duplicates the ES6 spec's
|
|
|
|
// GetPrototypeFromConstructor except that instead of taking a string naming
|
|
|
|
// the fallback prototype we just fall back to using null and assume that our
|
|
|
|
// caller will then pick the right default. The actual defaulting behavior
|
|
|
|
// here still needs to be defined in the Web IDL specification.
|
|
|
|
//
|
|
|
|
// Note that it's very important to do this property get on originalNewTarget,
|
|
|
|
// not our unwrapped newTarget, since we want to get Xray behavior here as
|
|
|
|
// needed.
|
|
|
|
// XXXbz for speed purposes, using a preinterned id here sure would be nice.
|
|
|
|
JS::Rooted<JS::Value> protoVal(aCx);
|
|
|
|
if (!JS_GetProperty(aCx, originalNewTarget, "prototype", &protoVal)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!protoVal.isObject()) {
|
|
|
|
aDesiredProto.set(nullptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
aDesiredProto.set(&protoVal.toObject());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
namespace binding_detail {
|
|
|
|
void
|
|
|
|
AssertReflectorHasGivenProto(JSContext* aCx, JSObject* aReflector,
|
|
|
|
JS::Handle<JSObject*> aGivenProto)
|
|
|
|
{
|
|
|
|
if (!aGivenProto) {
|
|
|
|
// Nothing to assert here
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> reflector(aCx, aReflector);
|
|
|
|
JSAutoCompartment ac(aCx, reflector);
|
|
|
|
JS::Rooted<JSObject*> reflectorProto(aCx);
|
|
|
|
bool ok = JS_GetPrototype(aCx, reflector, &reflectorProto);
|
|
|
|
MOZ_ASSERT(ok);
|
|
|
|
// aGivenProto may not be in the right compartment here, so we
|
|
|
|
// have to wrap it to compare.
|
|
|
|
JS::Rooted<JSObject*> givenProto(aCx, aGivenProto);
|
|
|
|
ok = JS_WrapObject(aCx, &givenProto);
|
|
|
|
MOZ_ASSERT(ok);
|
|
|
|
MOZ_ASSERT(givenProto == reflectorProto,
|
|
|
|
"How are we supposed to change the proto now?");
|
|
|
|
}
|
|
|
|
} // namespace binding_detail
|
|
|
|
#endif // DEBUG
|
|
|
|
|
2015-02-05 20:53:01 +03:00
|
|
|
void
|
|
|
|
SetDocumentAndPageUseCounter(JSContext* aCx, JSObject* aObject,
|
|
|
|
UseCounter aUseCounter)
|
|
|
|
{
|
|
|
|
nsGlobalWindow* win = xpc::WindowGlobalOrNull(js::UncheckedUnwrap(aObject));
|
|
|
|
if (win && win->GetDocument()) {
|
|
|
|
win->GetDocument()->SetDocumentAndPageUseCounter(aUseCounter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-28 21:25:04 +03:00
|
|
|
void
|
|
|
|
DeprecationWarning(JSContext* aCx, JSObject* aObject,
|
|
|
|
nsIDocument::DeprecatedOperations aOperation)
|
|
|
|
{
|
|
|
|
GlobalObject global(aCx, aObject);
|
|
|
|
if (global.Failed()) {
|
|
|
|
NS_ERROR("Could not create global for DeprecationWarning");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(global.GetAsSupports());
|
2015-09-28 21:25:04 +03:00
|
|
|
if (window && window->GetExtantDoc()) {
|
|
|
|
window->GetExtantDoc()->WarnOnceAbout(aOperation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 07:31:33 +03:00
|
|
|
namespace binding_detail {
|
|
|
|
JSObject*
|
|
|
|
UnprivilegedJunkScopeOrWorkerGlobal()
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
return xpc::UnprivilegedJunkScope();
|
|
|
|
}
|
|
|
|
|
2016-02-18 06:56:36 +03:00
|
|
|
return workers::GetCurrentThreadWorkerGlobal();
|
2016-02-11 07:31:33 +03:00
|
|
|
}
|
|
|
|
} // namespace binding_detail
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|