2012-03-31 08:42:20 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
|
|
|
|
/* vim: set ts=2 sw=2 et tw=79: */
|
|
|
|
/* 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>
|
|
|
|
|
2013-08-02 03:45:17 +04:00
|
|
|
#include "JavaScriptParent.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"
|
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"
|
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"
|
2013-08-18 02:50:18 +04:00
|
|
|
#include "prprf.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"
|
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"
|
2013-09-09 07:29:21 +04:00
|
|
|
#include "WorkerPrivate.h"
|
2014-08-08 17:03:29 +04:00
|
|
|
#include "nsDOMClassInfo.h"
|
2013-01-07 03:55:14 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2012-07-18 14:36:08 +04:00
|
|
|
JSErrorFormatString ErrorFormatString[] = {
|
|
|
|
#define MSG_DEF(_name, _argc, _str) \
|
|
|
|
{ _str, _argc, JSEXN_TYPEERR },
|
|
|
|
#include "mozilla/dom/Errors.msg"
|
|
|
|
#undef MSG_DEF
|
|
|
|
};
|
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
const ErrNum aErrorNumber,
|
|
|
|
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;
|
|
|
|
}
|
2013-06-17 21:07:03 +04:00
|
|
|
JS_ReportErrorNumberUC(aCx, GetErrorMessage, nullptr,
|
2013-06-18 00:31:13 +04:00
|
|
|
static_cast<const unsigned>(aErrorNumber),
|
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,
|
|
|
|
const ErrNum aErrorNumber,
|
|
|
|
prototypes::ID aProtoId)
|
|
|
|
{
|
|
|
|
return ThrowInvalidThis(aCx, aArgs, aErrorNumber,
|
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 {
|
|
|
|
nsTArray<nsString> mArgs;
|
|
|
|
dom::ErrNum mErrorNumber;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
ErrorResult::ThrowTypeError(const dom::ErrNum errorNumber, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, errorNumber);
|
2013-02-19 20:54:40 +04:00
|
|
|
if (IsJSException()) {
|
|
|
|
// We have rooted our mJSException, and we don't have the info
|
|
|
|
// needed to unroot here, so just bail.
|
|
|
|
va_end(ap);
|
|
|
|
MOZ_ASSERT(false,
|
|
|
|
"Ignoring ThrowTypeError call because we have a JS exception");
|
|
|
|
return;
|
|
|
|
}
|
2012-11-07 03:23:14 +04:00
|
|
|
if (IsTypeError()) {
|
|
|
|
delete mMessage;
|
|
|
|
}
|
|
|
|
mResult = NS_ERROR_TYPE_ERR;
|
|
|
|
Message* message = new Message();
|
|
|
|
message->mErrorNumber = errorNumber;
|
2014-07-15 17:14:38 +04:00
|
|
|
uint16_t argCount = dom::GetErrorMessage(nullptr, errorNumber)->argCount;
|
2012-11-07 03:23:14 +04:00
|
|
|
MOZ_ASSERT(argCount <= 10);
|
|
|
|
argCount = std::min<uint16_t>(argCount, 10);
|
|
|
|
while (argCount--) {
|
|
|
|
message->mArgs.AppendElement(*va_arg(ap, nsString*));
|
|
|
|
}
|
|
|
|
mMessage = message;
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ErrorResult::ReportTypeError(JSContext* aCx)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mMessage, "ReportTypeError() can be called only once");
|
|
|
|
|
|
|
|
Message* message = mMessage;
|
|
|
|
const uint32_t argCount = message->mArgs.Length();
|
2014-07-22 08:43:21 +04:00
|
|
|
const char16_t* args[11];
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ErrorResult::ClearMessage()
|
|
|
|
{
|
|
|
|
if (IsTypeError()) {
|
|
|
|
delete mMessage;
|
|
|
|
mMessage = nullptr;
|
|
|
|
}
|
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?");
|
|
|
|
|
|
|
|
if (IsTypeError()) {
|
|
|
|
delete mMessage;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
mJSException = JS::UndefinedValue();
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ErrorResult::ReportJSException(JSContext* cx)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mMightHaveUnreportedJSException,
|
|
|
|
"Why didn't you tell us you planned to handle JS exceptions?");
|
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);
|
2013-02-19 20:54:40 +04:00
|
|
|
}
|
|
|
|
|
2013-09-03 16:01:53 +04:00
|
|
|
void
|
|
|
|
ErrorResult::ReportJSExceptionFromJSImplementation(JSContext* aCx)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mMightHaveUnreportedJSException,
|
|
|
|
"Why didn't you tell us you planned to handle JS exceptions?");
|
|
|
|
|
|
|
|
dom::DOMError* domError;
|
2013-11-21 16:51:16 +04:00
|
|
|
nsresult rv = UNWRAP_OBJECT(DOMError, &mJSException.toObject(), domError);
|
2013-09-03 16:01:53 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Unwrapping really shouldn't fail here, if mExceptionHandling is set to
|
|
|
|
// eRethrowContentExceptions then the CallSetup destructor only stores an
|
|
|
|
// exception if it unwraps to DOMError. If we reach this then either
|
|
|
|
// mExceptionHandling wasn't set to eRethrowContentExceptions and we
|
|
|
|
// shouldn't be calling ReportJSExceptionFromJSImplementation or something
|
|
|
|
// went really wrong.
|
|
|
|
NS_RUNTIMEABORT("We stored a non-DOMError exception!");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString message;
|
|
|
|
domError->GetMessage(message);
|
|
|
|
|
2013-10-18 15:22:09 +04:00
|
|
|
JS_ReportError(aCx, "%hs", message.get());
|
2014-04-16 12:47:57 +04:00
|
|
|
js::RemoveRawValueRoot(aCx, &mJSException);
|
2014-02-13 11:38:36 +04:00
|
|
|
|
2013-09-03 16:01:53 +04:00
|
|
|
// We no longer have a useful exception but we do want to signal that an error
|
|
|
|
// occured.
|
|
|
|
mResult = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-06-12 05:41:21 +04:00
|
|
|
void
|
|
|
|
ErrorResult::StealJSException(JSContext* cx,
|
|
|
|
JS::MutableHandle<JS::Value> value)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mMightHaveUnreportedJSException,
|
|
|
|
"Must call WouldReportJSException unconditionally in all codepaths that might call StealJSException");
|
|
|
|
MOZ_ASSERT(IsJSException(), "No exception to steal");
|
|
|
|
|
|
|
|
value.set(mJSException);
|
2014-04-16 12:47:57 +04:00
|
|
|
js::RemoveRawValueRoot(cx, &mJSException);
|
2013-06-12 05:41:21 +04:00
|
|
|
mResult = NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-04 17:24:49 +04:00
|
|
|
void
|
|
|
|
ErrorResult::ReportNotEnoughArgsError(JSContext* cx,
|
|
|
|
const char* ifaceName,
|
|
|
|
const char* memberName)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(ErrorCode() == NS_ERROR_XPC_NOT_ENOUGH_ARGS);
|
|
|
|
|
|
|
|
nsPrintfCString errorMessage("%s.%s", ifaceName, memberName);
|
|
|
|
ThrowErrorMessage(cx, dom::MSG_MISSING_ARGUMENTS, errorMessage.get());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
// old (XPConnect-based) bindings. Because Function.prototype.toString throws if
|
|
|
|
// passed a non-Function object we also need to provide our own toString method
|
|
|
|
// for interface objects.
|
|
|
|
|
2013-10-16 02:34:13 +04:00
|
|
|
static bool
|
2012-05-02 17:28:17 +04:00
|
|
|
InterfaceObjectToString(JSContext* cx, unsigned argc, JS::Value *vp)
|
|
|
|
{
|
2013-10-12 09:02:39 +04:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
2013-10-21 21:52:22 +04:00
|
|
|
if (!args.thisv().isObject()) {
|
2013-10-28 18:04:12 +04:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr,
|
|
|
|
JSMSG_CANT_CONVERT_TO, "null", "object");
|
2012-05-02 17:28:17 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:45:38 +04:00
|
|
|
JS::Rooted<JSObject*> thisObj(cx, &args.thisv().toObject());
|
|
|
|
JS::Rooted<JSObject*> obj(cx, js::CheckedUnwrap(thisObj, /* stopAtOuter = */ false));
|
|
|
|
if (!obj) {
|
|
|
|
JS_ReportError(cx, "Permission denied to access object");
|
2014-07-15 00:19:36 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-05-02 17:28:17 +04:00
|
|
|
|
2014-09-15 18:45:38 +04:00
|
|
|
const js::Class* clasp = js::GetObjectClass(obj);
|
|
|
|
if (!IsDOMIfaceAndProtoClass(clasp)) {
|
|
|
|
JS_ReportError(cx, "toString called on incompatible object");
|
2012-05-02 17:28:17 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:45:38 +04:00
|
|
|
const DOMIfaceAndProtoJSClass* ifaceAndProtoJSClass =
|
|
|
|
DOMIfaceAndProtoJSClass::FromJSClass(clasp);
|
|
|
|
JS::Rooted<JSString*> str(cx,
|
|
|
|
JS_NewStringCopyZ(cx,
|
|
|
|
ifaceAndProtoJSClass->mToString));
|
|
|
|
if (!str) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-02 17:28:17 +04:00
|
|
|
|
2014-09-15 18:45:38 +04:00
|
|
|
args.rval().setString(str);
|
|
|
|
return true;
|
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,
|
|
|
|
JSFUN_CONSTRUCTOR, global,
|
|
|
|
name);
|
|
|
|
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 ||
|
2014-01-22 06:11:17 +04:00
|
|
|
JS_DefineProperty(cx, global, name, constructor, 0);
|
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);
|
2014-08-05 14:46:42 +04:00
|
|
|
constructor = JS_NewObject(cx, Jsvalify(constructorClass), constructorProto,
|
|
|
|
global);
|
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) {
|
2012-12-07 00:21:18 +04:00
|
|
|
// Have to shadow Function.prototype.toString, since that throws
|
|
|
|
// on things that are not js::FunctionClass.
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Rooted<JSFunction*> toString(cx,
|
2014-09-15 18:45:38 +04:00
|
|
|
JS_DefineFunction(cx, constructor, "toString", InterfaceObjectToString,
|
|
|
|
0, 0));
|
2012-05-02 17:28:17 +04:00
|
|
|
if (!toString) {
|
2013-10-28 18:04:12 +04:00
|
|
|
return nullptr;
|
2012-05-02 17:28:17 +04:00
|
|
|
}
|
|
|
|
|
2014-01-22 06:11:17 +04:00
|
|
|
if (!JS_DefineProperty(cx, constructor, "length", ctorNargs,
|
|
|
|
JSPROP_READONLY | JSPROP_PERMANENT)) {
|
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) {
|
|
|
|
if (properties->staticMethods &&
|
|
|
|
!DefinePrefable(cx, constructor, properties->staticMethods)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-10-19 11:34:28 +04:00
|
|
|
if (properties->staticAttributes &&
|
|
|
|
!DefinePrefable(cx, constructor, properties->staticAttributes)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (properties->constants &&
|
|
|
|
!DefinePrefable(cx, constructor, properties->constants)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chromeOnlyProperties) {
|
|
|
|
if (chromeOnlyProperties->staticMethods &&
|
|
|
|
!DefinePrefable(cx, constructor, chromeOnlyProperties->staticMethods)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-10-19 11:34:28 +04:00
|
|
|
if (chromeOnlyProperties->staticAttributes &&
|
|
|
|
!DefinePrefable(cx, constructor,
|
|
|
|
chromeOnlyProperties->staticAttributes)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (chromeOnlyProperties->constants &&
|
|
|
|
!DefinePrefable(cx, constructor, chromeOnlyProperties->constants)) {
|
|
|
|
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-01-22 06:11:17 +04:00
|
|
|
proto, JSPROP_PERMANENT | JSPROP_READONLY,
|
|
|
|
JS_PropertyStub, JS_StrictPropertyStub) ||
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-16 01:12:34 +04:00
|
|
|
bool
|
|
|
|
DefineWebIDLBindingUnforgeablePropertiesOnXPCObject(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> obj,
|
|
|
|
const NativeProperties* properties)
|
|
|
|
{
|
|
|
|
if (properties->unforgeableAttributes &&
|
|
|
|
!DefinePrefable(cx, obj, properties->unforgeableAttributes)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-22 15:09:41 +04:00
|
|
|
bool
|
2013-10-08 20:18:40 +04:00
|
|
|
DefineWebIDLBindingPropertiesOnXPCObject(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> obj,
|
2014-02-16 01:12:34 +04:00
|
|
|
const NativeProperties* properties)
|
2012-11-22 15:09:41 +04:00
|
|
|
{
|
|
|
|
if (properties->methods &&
|
2013-10-08 20:18:40 +04:00
|
|
|
!DefinePrefable(cx, obj, properties->methods)) {
|
2012-11-22 15:09:41 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (properties->attributes &&
|
2013-10-08 20:18:40 +04:00
|
|
|
!DefinePrefable(cx, obj, properties->attributes)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-22 15:09:41 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
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,
|
|
|
|
const NativeProperties* chromeOnlyProperties)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2013-05-02 07:44:11 +04:00
|
|
|
JS::Rooted<JSObject*> ourProto(cx,
|
2014-08-05 14:46:42 +04:00
|
|
|
JS_NewObjectWithUniqueType(cx, Jsvalify(protoClass), parentProto, global));
|
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
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if (properties->methods &&
|
2014-02-16 01:12:34 +04:00
|
|
|
!DefinePrefable(cx, obj, properties->methods)) {
|
|
|
|
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 (properties->attributes &&
|
2014-02-16 01:12:34 +04:00
|
|
|
!DefinePrefable(cx, obj, properties->attributes)) {
|
|
|
|
return false;
|
2012-10-09 22:50:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (properties->constants &&
|
2014-02-16 01:12:34 +04:00
|
|
|
!DefinePrefable(cx, obj, properties->constants)) {
|
|
|
|
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) {
|
|
|
|
if (chromeOnlyProperties->methods &&
|
2014-02-16 01:12:34 +04:00
|
|
|
!DefinePrefable(cx, obj, chromeOnlyProperties->methods)) {
|
|
|
|
return false;
|
2012-10-09 22:50:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (chromeOnlyProperties->attributes &&
|
2014-02-16 01:12:34 +04:00
|
|
|
!DefinePrefable(cx, obj, chromeOnlyProperties->attributes)) {
|
|
|
|
return false;
|
2012-10-09 22:50:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (chromeOnlyProperties->constants &&
|
2014-02-16 01:12:34 +04:00
|
|
|
!DefinePrefable(cx, obj, chromeOnlyProperties->constants)) {
|
|
|
|
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,
|
2013-08-08 01:40:00 +04:00
|
|
|
const char* name, bool defineOnGlobal)
|
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 &&
|
|
|
|
(properties->methods || properties->attributes)) ||
|
|
|
|
(chromeOnlyProperties &&
|
|
|
|
(chromeOnlyProperties->methods ||
|
|
|
|
chromeOnlyProperties->attributes))) || 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 &&
|
|
|
|
(properties->staticMethods || properties->staticAttributes)) ||
|
|
|
|
(chromeOnlyProperties &&
|
|
|
|
(chromeOnlyProperties->staticMethods ||
|
|
|
|
chromeOnlyProperties->staticAttributes))) ||
|
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");
|
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,
|
|
|
|
properties, chromeOnlyProperties);
|
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) {
|
2014-04-09 02:27:18 +04:00
|
|
|
obj = cache->WrapObject(aCx);
|
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();
|
2012-03-31 08:42:20 +04:00
|
|
|
if (jsobj && !js::GetObjectParent(jsobj))
|
|
|
|
NS_ASSERTION(js::GetObjectClass(jsobj)->flags & JSCLASS_IS_GLOBAL,
|
|
|
|
"Why did we recreate this wrapper?");
|
|
|
|
#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,
|
|
|
|
/* stopAtOuter = */ 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();
|
|
|
|
|
|
|
|
nsRefPtr<nsISupports> result;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
MOZ_ASSERT(JS_IsNativeFunction(obj, Constructor));
|
|
|
|
type = eInterface;
|
|
|
|
const JS::Value& v =
|
|
|
|
js::GetFunctionNativeReserved(obj,
|
|
|
|
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->mPropertyHooks;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(IsDOMIfaceAndProtoClass(js::GetObjectClass(obj)));
|
|
|
|
const DOMIfaceAndProtoJSClass* ifaceAndProtoJSClass =
|
|
|
|
DOMIfaceAndProtoJSClass::FromJSClass(js::GetObjectClass(obj));
|
|
|
|
type = ifaceAndProtoJSClass->mType;
|
|
|
|
return ifaceAndProtoJSClass->mNativeHooks;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const JSPropertySpec>* attributes, jsid* attributeIds,
|
2014-08-22 13:25:07 +04:00
|
|
|
const JSPropertySpec* attributeSpecs, JS::MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
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.
|
2013-08-12 15:09:14 +04:00
|
|
|
desc.setAttributes(attrSpec.flags & ~JSPROP_NATIVE_ACCESSORS);
|
2012-10-09 22:50:27 +04:00
|
|
|
// They all have getters, so we can just make it.
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSFunction*> fun(cx,
|
2014-01-10 21:11:31 +04:00
|
|
|
JS_NewFunctionById(cx, (JSNative)attrSpec.getter.propertyOp.op,
|
2014-03-04 22:05:07 +04:00
|
|
|
0, 0, wrapper, id));
|
2012-10-09 22:50:27 +04:00
|
|
|
if (!fun)
|
|
|
|
return false;
|
2014-01-10 21:11:31 +04:00
|
|
|
SET_JITINFO(fun, attrSpec.getter.propertyOp.info);
|
2012-10-09 22:50:27 +04:00
|
|
|
JSObject *funobj = JS_GetFunctionObject(fun);
|
2013-08-12 15:09:14 +04:00
|
|
|
desc.setGetterObject(funobj);
|
|
|
|
desc.attributesRef() |= JSPROP_GETTER;
|
2014-01-10 21:11:31 +04:00
|
|
|
if (attrSpec.setter.propertyOp.op) {
|
2012-10-09 22:50:27 +04:00
|
|
|
// We have a setter! Make it.
|
2014-01-10 21:11:31 +04:00
|
|
|
fun = JS_NewFunctionById(cx, (JSNative)attrSpec.setter.propertyOp.op, 1, 0,
|
2014-03-04 22:05:07 +04:00
|
|
|
wrapper, id);
|
2012-10-09 22:50:27 +04:00
|
|
|
if (!fun)
|
|
|
|
return false;
|
2014-01-10 21:11:31 +04:00
|
|
|
SET_JITINFO(fun, attrSpec.setter.propertyOp.info);
|
2012-10-09 22:50:27 +04:00
|
|
|
funobj = JS_GetFunctionObject(fun);
|
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,
|
|
|
|
jsid* methodIds,
|
2014-07-23 23:36:21 +04:00
|
|
|
const JSFunctionSpec* methodSpecs,
|
2014-08-22 13:25:07 +04:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
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];
|
2014-07-12 03:32:16 +04:00
|
|
|
JSFunction *fun;
|
|
|
|
if (methodSpec.selfHostedName) {
|
|
|
|
fun = JS::GetSelfHostedFunction(cx, methodSpec.selfHostedName, id, methodSpec.nargs);
|
|
|
|
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");
|
|
|
|
} else {
|
|
|
|
fun = JS_NewFunctionById(cx, methodSpec.call.op, methodSpec.nargs, 0, wrapper, id);
|
|
|
|
if (!fun) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
SET_JITINFO(fun, methodSpec.call.info);
|
|
|
|
}
|
|
|
|
JSObject *funobj = JS_GetFunctionObject(fun);
|
|
|
|
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,
|
|
|
|
JS::MutableHandle<JSPropertyDescriptor> 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nativeProperties->unforgeableAttributes) {
|
|
|
|
if (!XrayResolveAttribute(cx, wrapper, obj, id,
|
2013-09-25 22:38:29 +04: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nativeProperties->unforgeableMethods) {
|
|
|
|
if (!XrayResolveMethod(cx, wrapper, obj, id,
|
|
|
|
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,
|
2014-08-22 13:25:07 +04:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
bool& cacheOnHolder, DOMObjectType type,
|
2012-10-09 22:50:05 +04:00
|
|
|
const NativeProperties* nativeProperties)
|
2012-07-18 15:52:07 +04:00
|
|
|
{
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const JSFunctionSpec>* methods;
|
2012-10-09 22:50:27 +04:00
|
|
|
jsid* methodIds;
|
2014-07-23 23:36:21 +04:00
|
|
|
const JSFunctionSpec* methodSpecs;
|
2012-10-09 22:50:27 +04:00
|
|
|
if (type == eInterface) {
|
|
|
|
methods = nativeProperties->staticMethods;
|
|
|
|
methodIds = nativeProperties->staticMethodIds;
|
2014-07-23 23:36:21 +04:00
|
|
|
methodSpecs = nativeProperties->staticMethodSpecs;
|
2012-10-09 22:50:27 +04:00
|
|
|
} else {
|
|
|
|
methods = nativeProperties->methods;
|
|
|
|
methodIds = nativeProperties->methodIds;
|
2014-07-23 23:36:21 +04:00
|
|
|
methodSpecs = nativeProperties->methodSpecs;
|
2012-10-09 22:50:27 +04:00
|
|
|
}
|
|
|
|
if (methods) {
|
2014-07-12 03:32:16 +04:00
|
|
|
if (!XrayResolveMethod(cx, wrapper, obj, id, 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) {
|
|
|
|
if (nativeProperties->staticAttributes) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (!XrayResolveAttribute(cx, wrapper, obj, id,
|
2012-10-19 11:34:28 +04:00
|
|
|
nativeProperties->staticAttributes,
|
|
|
|
nativeProperties->staticAttributeIds,
|
2014-08-22 13:25:07 +04:00
|
|
|
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 {
|
2012-10-09 22:50:27 +04:00
|
|
|
if (nativeProperties->attributes) {
|
2013-02-19 20:54:40 +04:00
|
|
|
if (!XrayResolveAttribute(cx, wrapper, obj, id,
|
2012-10-09 22:50:27 +04:00
|
|
|
nativeProperties->attributes,
|
|
|
|
nativeProperties->attributeIds,
|
2014-08-22 13:25:07 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (nativeProperties->constants) {
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const ConstantSpec>* constant;
|
2012-10-09 22:50:05 +04: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.
|
|
|
|
size_t i = constant->specs - nativeProperties->constantSpecs;
|
2013-12-17 06:27:43 +04:00
|
|
|
for ( ; nativeProperties->constantIds[i] != JSID_VOID; ++i) {
|
2012-10-09 22:50:05 +04:00
|
|
|
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);
|
|
|
|
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,
|
2014-08-22 13:25:07 +04:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> 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);
|
|
|
|
desc.setGetter(JS_PropertyStub);
|
|
|
|
desc.setSetter(JS_StrictPropertyStub);
|
|
|
|
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
|
2013-10-18 15:22:08 +04:00
|
|
|
DEBUG_CheckXBLLookup(JSContext *cx, JSPropertyDescriptor *desc)
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
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.
|
|
|
|
if (type != eGlobalInstance || !GlobalPropertiesAreOwn()) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IdEquals(id, "toString") && !JS_ObjectIsFunction(cx, obj)) {
|
|
|
|
MOZ_ASSERT(IsDOMIfaceAndProtoClass(js::GetObjectClass(obj)));
|
|
|
|
|
|
|
|
JS::Rooted<JSFunction*> toString(cx, JS_NewFunction(cx, InterfaceObjectToString, 0, 0, wrapper, "toString"));
|
|
|
|
if (!toString) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cacheOnHolder = true;
|
|
|
|
|
|
|
|
FillPropertyDescriptor(desc, wrapper, 0,
|
|
|
|
JS::ObjectValue(*JS_GetFunctionObject(toString)));
|
|
|
|
|
|
|
|
return JS_WrapPropertyDescriptor(cx, desc);
|
|
|
|
}
|
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.
|
|
|
|
if (type == eGlobalInterfacePrototype && GlobalPropertiesAreOwn()) {
|
|
|
|
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,
|
2013-04-30 21:29:40 +04:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc, bool* defined)
|
2013-05-29 13:38:57 +04:00
|
|
|
{
|
|
|
|
if (!js::IsProxy(obj))
|
|
|
|
return true;
|
|
|
|
|
2014-06-27 15:44:08 +04:00
|
|
|
const DOMProxyHandler* handler = GetDOMProxyHandler(obj);
|
2013-05-29 13:38:57 +04:00
|
|
|
return handler->defineProperty(cx, wrapper, id, desc, defined);
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
jsid* ids, const SpecType* specList,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-09-27 00:16:36 +04:00
|
|
|
#define ADD_KEYS_IF_DEFINED(fieldName) { \
|
2014-07-23 23:36:21 +04:00
|
|
|
if (nativeProperties->fieldName##s && \
|
2014-09-27 00:16:36 +04:00
|
|
|
!XrayAttributeOrMethodKeys(cx, wrapper, obj, \
|
|
|
|
nativeProperties->fieldName##s, \
|
|
|
|
nativeProperties->fieldName##Ids, \
|
|
|
|
nativeProperties->fieldName##Specs, \
|
|
|
|
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)) {
|
2014-09-27 00:16:36 +04:00
|
|
|
ADD_KEYS_IF_DEFINED(unforgeableMethod);
|
|
|
|
ADD_KEYS_IF_DEFINED(unforgeableAttribute);
|
2014-09-15 18:51:40 +04:00
|
|
|
if (type == eGlobalInstance && GlobalPropertiesAreOwn()) {
|
2014-09-27 00:16:36 +04: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) {
|
2014-09-27 00:16:36 +04:00
|
|
|
ADD_KEYS_IF_DEFINED(staticMethod);
|
|
|
|
ADD_KEYS_IF_DEFINED(staticAttribute);
|
2014-09-15 18:51:40 +04:00
|
|
|
} else if (type != eGlobalInterfacePrototype || !GlobalPropertiesAreOwn()) {
|
|
|
|
MOZ_ASSERT(IsInterfacePrototype(type));
|
2014-09-27 00:16:36 +04:00
|
|
|
ADD_KEYS_IF_DEFINED(method);
|
|
|
|
ADD_KEYS_IF_DEFINED(attribute);
|
2012-07-18 15:52:07 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 22:50:05 +04:00
|
|
|
if (nativeProperties->constants) {
|
2013-04-03 04:20:38 +04:00
|
|
|
const Prefable<const ConstantSpec>* constant;
|
2012-10-09 22:50:05 +04: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.
|
|
|
|
size_t i = constant->specs - nativeProperties->constantSpecs;
|
2013-12-17 06:27:43 +04:00
|
|
|
for ( ; nativeProperties->constantIds[i] != JSID_VOID; ++i) {
|
2012-10-09 22:50:05 +04:00
|
|
|
if (!props.append(nativeProperties->constantIds[i])) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:51:40 +04:00
|
|
|
return (type == eGlobalInterfacePrototype && GlobalPropertiesAreOwn()) ||
|
2014-09-27 00:16:36 +04:00
|
|
|
XrayOwnNativePropertyKeys(cx, wrapper, nativePropertyHooks, type,
|
|
|
|
obj, flags, props);
|
2012-10-09 22:50:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NativePropertyHooks sWorkerNativePropertyHooks = {
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
{
|
|
|
|
nullptr,
|
|
|
|
nullptr
|
|
|
|
},
|
|
|
|
prototypes::id::_ID_Count,
|
|
|
|
constructors::id::_ID_Count,
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
2012-05-22 17:46:20 +04:00
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
GetPropertyOnPrototype(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::Handle<jsid> id, bool* found,
|
2012-05-22 17:46:20 +04:00
|
|
|
JS::Value* vp)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
bool hasProp;
|
2012-05-22 17:46:20 +04:00
|
|
|
if (!JS_HasPropertyById(cx, proto, id, &hasProp)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*found = hasProp;
|
|
|
|
if (!hasProp || !vp) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-26 13:00:38 +04:00
|
|
|
JS::Rooted<JS::Value> value(cx);
|
|
|
|
if (!JS_ForwardGetPropertyTo(cx, proto, id, proxy, &value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*vp = value;
|
|
|
|
return true;
|
2012-05-22 17:46:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-05-04 03:29:08 +04:00
|
|
|
HasPropertyOnPrototype(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
JS::Handle<jsid> id)
|
2012-05-22 17:46:20 +04:00
|
|
|
{
|
|
|
|
bool found;
|
2013-07-09 18:45:13 +04:00
|
|
|
// We ignore an error from GetPropertyOnPrototype. We pass nullptr
|
|
|
|
// for vp so that GetPropertyOnPrototype won't actually do a get.
|
2014-10-02 20:34:16 +04:00
|
|
|
return !GetPropertyOnPrototype(cx, proxy, id, &found, nullptr) || found;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shadowPrototypeProperties || !HasPropertyOnPrototype(cx, proxy, id)) {
|
|
|
|
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,
|
|
|
|
JS::MutableHandle<JS::Value> aValue,
|
2014-09-09 22:00:22 +04:00
|
|
|
nsAString& aJSON) const
|
2014-08-01 07:50:19 +04:00
|
|
|
{
|
|
|
|
return JS_Stringify(aCx, aValue, JS::NullPtr(), JS::NullHandleValue,
|
|
|
|
AppendJSONToString, &aJSON);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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-10-09 22:50:27 +04:00
|
|
|
|
2012-09-26 18:17:46 +04:00
|
|
|
// Dynamically ensure that two objects don't end up with the same reserved slot.
|
2013-04-12 07:20:18 +04:00
|
|
|
class MOZ_STACK_CLASS AutoCloneDOMObjectSlotGuard
|
2012-09-26 18:17:46 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-05-04 03:29:08 +04:00
|
|
|
AutoCloneDOMObjectSlotGuard(JSContext* aCx, JSObject* aOld, JSObject* aNew)
|
|
|
|
: mOldReflector(aCx, aOld), mNewReflector(aCx, aNew)
|
2012-09-26 18:17:46 +04:00
|
|
|
{
|
2014-10-18 03:57:52 +04:00
|
|
|
MOZ_ASSERT(js::GetReservedOrProxyPrivateSlot(aOld, DOM_OBJECT_SLOT) ==
|
|
|
|
js::GetReservedOrProxyPrivateSlot(aNew, DOM_OBJECT_SLOT));
|
2012-09-26 18:17:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
~AutoCloneDOMObjectSlotGuard()
|
|
|
|
{
|
2014-10-18 03:57:52 +04:00
|
|
|
if (js::GetReservedOrProxyPrivateSlot(mOldReflector, DOM_OBJECT_SLOT).toPrivate()) {
|
|
|
|
js::SetReservedOrProxyPrivateSlot(mNewReflector, DOM_OBJECT_SLOT,
|
|
|
|
JS::PrivateValue(nullptr));
|
2012-09-26 18:17:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> mOldReflector;
|
|
|
|
JS::Rooted<JSObject*> mNewReflector;
|
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-03-07 03:26:17 +04:00
|
|
|
// Check if we're near the stack limit before we get anywhere near the
|
2014-08-21 05:14:56 +04:00
|
|
|
// transplanting code. We use a conservative check since we'll use a little
|
|
|
|
// more space before we actually hit the critical "can't fail" path.
|
|
|
|
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
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> oldParent(aCx, JS_GetParent(aObj));
|
|
|
|
JS::Rooted<JSObject*> newParent(aCx, domClass->mGetParent(aCx, aObj));
|
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) {
|
2012-09-26 18:17:46 +04:00
|
|
|
if (!JS_SetParent(aCx, aObj, newParent)) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
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
|
|
|
|
// expando chain. The only part that is dangerous here is that if we have to
|
|
|
|
// return early we must avoid ending up with two reflectors pointing to the
|
|
|
|
// same native. Other than that, the objects we create will just go away.
|
|
|
|
|
2013-05-02 07:44:12 +04:00
|
|
|
JS::Rooted<JSObject*> global(aCx,
|
|
|
|
js::GetGlobalForObjectCrossCompartment(newParent));
|
|
|
|
JS::Handle<JSObject*> proto = (domClass->mGetProto)(aCx, global);
|
2012-09-26 18:17:46 +04:00
|
|
|
if (!proto) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> newobj(aCx, JS_CloneObject(aCx, aObj, proto, newParent));
|
2012-09-26 18:17:46 +04:00
|
|
|
if (!newobj) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-10-18 03:57:52 +04:00
|
|
|
js::SetReservedOrProxyPrivateSlot(newobj, DOM_OBJECT_SLOT,
|
|
|
|
js::GetReservedOrProxyPrivateSlot(aObj, DOM_OBJECT_SLOT));
|
2012-09-26 18:17:46 +04:00
|
|
|
|
|
|
|
// At this point, both |aObj| and |newobj| point to the same native
|
|
|
|
// which is bad, because one of them will end up being finalized with a
|
|
|
|
// native it does not own. |cloneGuard| ensures that if we exit before
|
|
|
|
// clearing |aObj|'s reserved slot the reserved slot of |newobj| will be
|
|
|
|
// set to null. |aObj| will go away soon, because we swap it with
|
|
|
|
// another object during the transplant and let that object die.
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> propertyHolder(aCx);
|
2012-09-26 18:17:46 +04:00
|
|
|
{
|
2013-05-04 03:29:08 +04:00
|
|
|
AutoCloneDOMObjectSlotGuard cloneGuard(aCx, aObj, newobj);
|
2012-09-26 18:17:46 +04:00
|
|
|
|
2013-05-04 03:29:08 +04:00
|
|
|
JS::Rooted<JSObject*> copyFrom(aCx, isProxy ? expandoObject : aObj);
|
2013-04-16 21:02:57 +04:00
|
|
|
if (copyFrom) {
|
2014-01-16 21:48:58 +04:00
|
|
|
propertyHolder = JS_NewObjectWithGivenProto(aCx, nullptr, JS::NullPtr(),
|
2013-04-16 21:02:57 +04:00
|
|
|
newParent);
|
|
|
|
if (!propertyHolder) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2012-09-26 18:17:46 +04:00
|
|
|
|
2013-04-16 21:02:57 +04:00
|
|
|
if (!JS_CopyPropertiesFrom(aCx, propertyHolder, copyFrom)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
propertyHolder = nullptr;
|
2012-09-26 18:17:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expandos from other compartments are attached to the target JS object.
|
|
|
|
// Copy them over, and let the old ones die a natural death.
|
|
|
|
if (!xpc::XrayUtils::CloneExpandoChain(aCx, newobj, aObj)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've set up |newobj|, so we make it own the native by 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.
|
2014-10-18 03:57:52 +04:00
|
|
|
js::SetReservedOrProxyPrivateSlot(aObj, DOM_OBJECT_SLOT, JS::PrivateValue(nullptr));
|
2012-09-26 18:17:46 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
// Now we can just fix up the parent and return the wrapper
|
|
|
|
|
|
|
|
if (newParent && !JS_SetParent(aCx, aObj, newParent)) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
obj = js::CheckedUnwrap(obj, /* stopAtOuter = */ false);
|
|
|
|
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));
|
|
|
|
|
2014-06-19 00:19:19 +04:00
|
|
|
const DOMJSClass* domClass = GetDOMClass(js::UncheckedUnwrap(instance, /* stopAtOuter = */ 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;
|
|
|
|
}
|
2013-08-23 09:17:08 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> 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
|
|
|
|
GetWindowForJSImplementedObject(JSContext* cx, JS::Handle<JSObject*> obj,
|
|
|
|
nsPIDOMWindow** window)
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's OK if we have null here: that just means the content-side
|
|
|
|
// object really wasn't associated with any window.
|
2013-08-23 09:17:08 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win(do_QueryInterface(global.GetAsSupports()));
|
2013-05-20 21:47:08 +04:00
|
|
|
win.forget(window);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-01 10:56:23 +04:00
|
|
|
already_AddRefed<nsPIDOMWindow>
|
|
|
|
ConstructJSImplementation(JSContext* aCx, const char* aContractId,
|
|
|
|
const GlobalObject& aGlobal,
|
|
|
|
JS::MutableHandle<JSObject*> aObject,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
// Get the window to use as a parent and for initialization.
|
2013-08-23 09:17:08 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
|
2013-06-01 10:56:23 +04:00
|
|
|
if (!window) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-03-28 08:03:03 +04:00
|
|
|
ConstructJSImplementation(aCx, aContractId, window, aObject, aRv);
|
|
|
|
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return window.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ConstructJSImplementation(JSContext* aCx, const char* aContractId,
|
|
|
|
nsPIDOMWindow* aWindow,
|
|
|
|
JS::MutableHandle<JSObject*> aObject,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
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) {
|
|
|
|
NS_WARNING("Failed to get JS implementation for contract");
|
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
|
|
|
}
|
|
|
|
// Initialize the object, if it implements nsIDOMGlobalPropertyInitializer.
|
|
|
|
nsCOMPtr<nsIDOMGlobalPropertyInitializer> gpi =
|
|
|
|
do_QueryInterface(implISupports);
|
|
|
|
if (gpi) {
|
|
|
|
JS::Rooted<JS::Value> initReturn(aCx);
|
2014-10-02 04:37:50 +04:00
|
|
|
rv = gpi->Init(aWindow, &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
|
|
|
|
NormalizeScalarValueStringInternal(JSContext* aCx, T& aString)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
NormalizeScalarValueString(JSContext* aCx, nsAString& aString)
|
|
|
|
{
|
|
|
|
NormalizeScalarValueStringInternal(aCx, aString);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NormalizeScalarValueString(JSContext* aCx, binding_detail::FakeString& aString)
|
|
|
|
{
|
|
|
|
NormalizeScalarValueStringInternal(aCx, aString);
|
|
|
|
}
|
|
|
|
|
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");
|
2014-07-02 15:04:14 +04:00
|
|
|
PR_snprintf(index, sizeof(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");
|
2014-07-02 15:04:14 +04:00
|
|
|
PR_snprintf(badCharArray, sizeof(badCharArray), "%d", badChar);
|
|
|
|
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");
|
|
|
|
|
2013-06-13 09:18:35 +04:00
|
|
|
result.SetLength(length);
|
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
|
|
|
}
|
|
|
|
|
2014-04-08 22:48:37 +04:00
|
|
|
#ifdef DEBUG
|
2013-11-05 18:16:26 +04:00
|
|
|
void
|
2014-04-08 22:48:37 +04:00
|
|
|
VerifyTraceProtoAndIfaceCacheCalled(JSTracer *trc, void **thingp,
|
|
|
|
JSGCTraceKind kind)
|
2013-11-05 18:16:26 +04:00
|
|
|
{
|
2014-04-08 22:48:37 +04:00
|
|
|
// We don't do anything here, we only want to verify that
|
|
|
|
// TraceProtoAndIfaceCache was called.
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
2014-04-08 22:48:37 +04:00
|
|
|
#endif
|
2013-11-05 18:16:26 +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-04-26 01:11:02 +04:00
|
|
|
JS::Handle<jsid> aId, JS::MutableHandle<JSObject*> aObjp)
|
2013-11-05 18:16:26 +04:00
|
|
|
{
|
|
|
|
bool resolved;
|
|
|
|
if (!JS_ResolveStandardClass(aCx, aObj, aId, &resolved)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aObjp.set(resolved ? aObj.get() : nullptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
EnumerateGlobal(JSContext* aCx, JS::Handle<JSObject*> aObj)
|
|
|
|
{
|
|
|
|
return JS_EnumerateStandardClasses(aCx, aObj);
|
|
|
|
}
|
|
|
|
|
2014-05-25 22:31:07 +04:00
|
|
|
bool
|
|
|
|
CheckPermissions(JSContext* aCx, JSObject* aObj, const char* const aPermissions[])
|
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> rootedObj(aCx, aObj);
|
|
|
|
nsPIDOMWindow* window = xpc::WindowGlobalOrNull(rootedObj);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-08-21 03:32:19 +04:00
|
|
|
bool
|
|
|
|
CheckSafetyInPrerendering(JSContext* aCx, JSObject* aObj)
|
|
|
|
{
|
|
|
|
//TODO: Check if page is being prerendered.
|
|
|
|
//Returning false for now.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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()) {
|
|
|
|
return ThrowInvalidThis(cx, args,
|
|
|
|
MSG_GETTER_THIS_DOES_NOT_IMPLEMENT_INTERFACE,
|
|
|
|
protoID);
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
GetInvalidThisErrorForGetter(rv == NS_ERROR_XPC_SECURITY_MANAGER_VETO),
|
|
|
|
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()) {
|
|
|
|
return ThrowInvalidThis(cx, args,
|
|
|
|
MSG_SETTER_THIS_DOES_NOT_IMPLEMENT_INTERFACE,
|
|
|
|
protoID);
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
GetInvalidThisErrorForSetter(rv == NS_ERROR_XPC_SECURITY_MANAGER_VETO),
|
|
|
|
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()) {
|
|
|
|
return ThrowInvalidThis(cx, args,
|
|
|
|
MSG_METHOD_THIS_DOES_NOT_IMPLEMENT_INTERFACE,
|
|
|
|
protoID);
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
GetInvalidThisErrorForMethod(rv == NS_ERROR_XPC_SECURITY_MANAGER_VETO),
|
|
|
|
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()) {
|
|
|
|
ThrowInvalidThis(cx, args,
|
|
|
|
MSG_METHOD_THIS_DOES_NOT_IMPLEMENT_INTERFACE,
|
|
|
|
protoID);
|
|
|
|
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)) {
|
|
|
|
ThrowInvalidThis(cx, args,
|
|
|
|
GetInvalidThisErrorForMethod(rv == NS_ERROR_XPC_SECURITY_MANAGER_VETO),
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
GlobalObject global(cx, promiseScope);
|
|
|
|
if (global.Failed()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> exn(cx);
|
|
|
|
if (!JS_GetPendingException(cx, &exn)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_ClearPendingException(cx);
|
|
|
|
ErrorResult rv;
|
2014-06-16 20:52:00 +04:00
|
|
|
nsRefPtr<Promise> promise = Promise::Reject(global, exn, rv);
|
2014-03-21 20:18:24 +04:00
|
|
|
if (rv.Failed()) {
|
|
|
|
// We just give up. Make sure to not leak memory on the
|
|
|
|
// ErrorResult, but then just put the original exception back.
|
|
|
|
ThrowMethodFailedWithDetails(cx, rv, "", "");
|
|
|
|
JS_SetPendingException(cx, exn);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-09 02:27:19 +04:00
|
|
|
return WrapNewBindingObject(cx, promise, rval);
|
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)
|
|
|
|
{
|
|
|
|
nsISupports *iface = xpc::UnwrapReflectorToISupports(src);
|
|
|
|
if (iface) {
|
|
|
|
if (NS_FAILED(iface->QueryInterface(iid, ppArg))) {
|
|
|
|
return NS_ERROR_XPC_BAD_CONVERT_JS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsXPCWrappedJS> wrappedJS;
|
|
|
|
nsresult rv = nsXPCWrappedJS::GetNewOrUsed(src, iid, getter_AddRefs(wrappedJS));
|
|
|
|
if (NS_FAILED(rv) || !wrappedJS) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|