2013-07-03 11:24:32 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=4 sw=4 et tw=80:
|
|
|
|
*
|
|
|
|
* 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/. */
|
|
|
|
|
|
|
|
#include "JavaScriptShared.h"
|
2013-11-18 21:21:43 +04:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2014-10-07 02:45:42 +04:00
|
|
|
#include "mozilla/dom/CPOWManagerGetter.h"
|
2014-05-17 03:40:37 +04:00
|
|
|
#include "mozilla/dom/TabChild.h"
|
2013-07-03 11:24:32 +04:00
|
|
|
#include "jsfriendapi.h"
|
|
|
|
#include "xpcprivate.h"
|
2014-09-25 15:13:29 +04:00
|
|
|
#include "WrapperFactory.h"
|
2014-05-29 05:05:22 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-07-03 11:24:32 +04:00
|
|
|
|
|
|
|
using namespace js;
|
|
|
|
using namespace JS;
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::jsipc;
|
|
|
|
|
2014-05-17 03:40:35 +04:00
|
|
|
IdToObjectMap::IdToObjectMap()
|
2013-07-03 11:24:32 +04:00
|
|
|
: table_(SystemAllocPolicy())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-05-17 03:40:35 +04:00
|
|
|
IdToObjectMap::init()
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2014-05-17 03:40:36 +04:00
|
|
|
if (table_.initialized())
|
|
|
|
return true;
|
2013-07-03 11:24:32 +04:00
|
|
|
return table_.init(32);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-13 20:49:17 +03:00
|
|
|
IdToObjectMap::trace(JSTracer* trc, uint64_t minimumId)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2016-10-19 01:58:33 +03:00
|
|
|
for (Table::Range r(table_.all()); !r.empty(); r.popFront()) {
|
2017-01-13 20:49:17 +03:00
|
|
|
if (r.front().key().serialNumber() >= minimumId)
|
2016-10-19 01:58:33 +03:00
|
|
|
JS::TraceEdge(trc, &r.front().value(), "ipc-object");
|
|
|
|
}
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
2014-05-17 03:40:37 +04:00
|
|
|
void
|
2014-09-02 13:07:22 +04:00
|
|
|
IdToObjectMap::sweep()
|
2014-05-17 03:40:37 +04:00
|
|
|
{
|
|
|
|
for (Table::Enum e(table_); !e.empty(); e.popFront()) {
|
2015-03-29 01:22:11 +03:00
|
|
|
JS::Heap<JSObject*>* objp = &e.front().value();
|
2014-09-24 15:54:11 +04:00
|
|
|
JS_UpdateWeakPointerAfterGC(objp);
|
|
|
|
if (!*objp)
|
2014-05-17 03:40:37 +04:00
|
|
|
e.removeFront();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JSObject*
|
2014-05-17 03:40:35 +04:00
|
|
|
IdToObjectMap::find(ObjectId id)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2014-05-17 03:40:35 +04:00
|
|
|
Table::Ptr p = table_.lookup(id);
|
2013-07-03 11:24:32 +04:00
|
|
|
if (!p)
|
2013-09-19 23:24:53 +04:00
|
|
|
return nullptr;
|
2013-11-26 05:35:09 +04:00
|
|
|
return p->value();
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
2016-11-18 16:18:09 +03:00
|
|
|
JSObject*
|
|
|
|
IdToObjectMap::findPreserveColor(ObjectId id)
|
|
|
|
{
|
|
|
|
Table::Ptr p = table_.lookup(id);
|
|
|
|
if (!p)
|
|
|
|
return nullptr;
|
|
|
|
return p->value().unbarrieredGet();
|
|
|
|
}
|
|
|
|
|
2013-07-03 11:24:32 +04:00
|
|
|
bool
|
2015-03-29 01:22:11 +03:00
|
|
|
IdToObjectMap::add(ObjectId id, JSObject* obj)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
|
|
|
return table_.put(id, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-17 03:40:35 +04:00
|
|
|
IdToObjectMap::remove(ObjectId id)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
|
|
|
table_.remove(id);
|
|
|
|
}
|
|
|
|
|
2015-01-16 20:56:30 +03:00
|
|
|
void
|
|
|
|
IdToObjectMap::clear()
|
|
|
|
{
|
|
|
|
table_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IdToObjectMap::empty() const
|
|
|
|
{
|
|
|
|
return table_.empty();
|
|
|
|
}
|
|
|
|
|
2016-02-07 20:08:55 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool
|
|
|
|
IdToObjectMap::has(const ObjectId& id, const JSObject* obj) const
|
|
|
|
{
|
|
|
|
auto p = table_.lookup(id);
|
|
|
|
if (!p)
|
|
|
|
return false;
|
2016-10-29 01:11:56 +03:00
|
|
|
return p->value() == obj;
|
2016-02-07 20:08:55 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-07-03 11:24:32 +04:00
|
|
|
bool
|
2014-05-17 03:40:35 +04:00
|
|
|
ObjectToIdMap::init()
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2015-07-15 12:22:04 +03:00
|
|
|
return table_.initialized() || table_.init(32);
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
2014-09-24 15:54:11 +04:00
|
|
|
void
|
2015-03-29 01:22:11 +03:00
|
|
|
ObjectToIdMap::trace(JSTracer* trc)
|
2014-09-24 15:54:11 +04:00
|
|
|
{
|
2015-11-25 22:59:09 +03:00
|
|
|
table_.trace(trc);
|
2014-09-24 15:54:11 +04:00
|
|
|
}
|
|
|
|
|
2013-07-03 11:24:32 +04:00
|
|
|
void
|
2014-09-02 13:07:22 +04:00
|
|
|
ObjectToIdMap::sweep()
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2015-11-25 22:59:09 +03:00
|
|
|
table_.sweep();
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjectId
|
2015-03-29 01:22:11 +03:00
|
|
|
ObjectToIdMap::find(JSObject* obj)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2015-07-15 12:22:04 +03:00
|
|
|
Table::Ptr p = table_.lookup(obj);
|
2013-07-03 11:24:32 +04:00
|
|
|
if (!p)
|
2014-09-25 15:13:29 +04:00
|
|
|
return ObjectId::nullId();
|
2013-11-26 05:35:09 +04:00
|
|
|
return p->value();
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-03-29 01:22:11 +03:00
|
|
|
ObjectToIdMap::add(JSContext* cx, JSObject* obj, ObjectId id)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2015-11-25 22:59:09 +03:00
|
|
|
return table_.put(obj, id);
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-03-29 01:22:11 +03:00
|
|
|
ObjectToIdMap::remove(JSObject* obj)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2015-07-15 12:22:04 +03:00
|
|
|
table_.remove(obj);
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
2015-01-16 20:56:30 +03:00
|
|
|
void
|
|
|
|
ObjectToIdMap::clear()
|
|
|
|
{
|
2015-07-15 12:22:04 +03:00
|
|
|
table_.clear();
|
2015-01-16 20:56:30 +03:00
|
|
|
}
|
|
|
|
|
2014-05-29 05:05:22 +04:00
|
|
|
bool JavaScriptShared::sLoggingInitialized;
|
|
|
|
bool JavaScriptShared::sLoggingEnabled;
|
|
|
|
bool JavaScriptShared::sStackLoggingEnabled;
|
|
|
|
|
2016-08-11 15:39:23 +03:00
|
|
|
JavaScriptShared::JavaScriptShared()
|
|
|
|
: refcount_(1),
|
2016-10-19 01:58:33 +03:00
|
|
|
nextSerialNumber_(1),
|
|
|
|
nextCPOWNumber_(1)
|
2014-05-17 03:40:37 +04:00
|
|
|
{
|
2014-05-29 05:05:22 +04:00
|
|
|
if (!sLoggingInitialized) {
|
|
|
|
sLoggingInitialized = true;
|
2014-08-21 00:01:32 +04:00
|
|
|
|
|
|
|
if (PR_GetEnv("MOZ_CPOW_LOG")) {
|
|
|
|
sLoggingEnabled = true;
|
2014-12-19 04:34:24 +03:00
|
|
|
sStackLoggingEnabled = strstr(PR_GetEnv("MOZ_CPOW_LOG"), "stacks");
|
2014-08-21 00:01:32 +04:00
|
|
|
} else {
|
|
|
|
Preferences::AddBoolVarCache(&sLoggingEnabled,
|
|
|
|
"dom.ipc.cpows.log.enabled", false);
|
|
|
|
Preferences::AddBoolVarCache(&sStackLoggingEnabled,
|
|
|
|
"dom.ipc.cpows.log.stack", false);
|
|
|
|
}
|
2014-05-29 05:05:22 +04:00
|
|
|
}
|
2014-05-17 03:40:37 +04:00
|
|
|
}
|
|
|
|
|
2015-01-16 20:56:30 +03:00
|
|
|
JavaScriptShared::~JavaScriptShared()
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(cpows_.empty());
|
|
|
|
}
|
|
|
|
|
2013-07-03 11:24:32 +04:00
|
|
|
bool
|
|
|
|
JavaScriptShared::init()
|
|
|
|
{
|
|
|
|
if (!objects_.init())
|
|
|
|
return false;
|
2014-05-17 03:40:36 +04:00
|
|
|
if (!cpows_.init())
|
|
|
|
return false;
|
2014-09-25 15:13:29 +04:00
|
|
|
if (!unwaivedObjectIds_.init())
|
|
|
|
return false;
|
|
|
|
if (!waivedObjectIds_.init())
|
2014-05-17 03:40:37 +04:00
|
|
|
return false;
|
|
|
|
|
2013-07-03 11:24:32 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-17 03:40:37 +04:00
|
|
|
void
|
|
|
|
JavaScriptShared::decref()
|
|
|
|
{
|
|
|
|
refcount_--;
|
|
|
|
if (!refcount_)
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JavaScriptShared::incref()
|
|
|
|
{
|
|
|
|
refcount_++;
|
|
|
|
}
|
|
|
|
|
2013-07-03 11:24:32 +04:00
|
|
|
bool
|
2015-03-29 01:22:11 +03:00
|
|
|
JavaScriptShared::convertIdToGeckoString(JSContext* cx, JS::HandleId id, nsString* to)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
|
|
|
RootedValue idval(cx);
|
2014-01-17 22:08:51 +04:00
|
|
|
if (!JS_IdToValue(cx, id, &idval))
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
|
|
|
|
2013-11-16 16:31:36 +04:00
|
|
|
RootedString str(cx, ToString(cx, idval));
|
2013-07-03 11:24:32 +04:00
|
|
|
if (!str)
|
|
|
|
return false;
|
|
|
|
|
2014-07-12 11:43:08 +04:00
|
|
|
return AssignJSString(cx, *to, str);
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-03-29 01:22:11 +03:00
|
|
|
JavaScriptShared::convertGeckoStringToId(JSContext* cx, const nsString& from, JS::MutableHandleId to)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
|
|
|
RootedString str(cx, JS_NewUCStringCopyN(cx, from.BeginReading(), from.Length()));
|
|
|
|
if (!str)
|
|
|
|
return false;
|
|
|
|
|
2014-04-02 18:28:03 +04:00
|
|
|
return JS_StringToId(cx, str, to);
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-03-29 01:22:11 +03:00
|
|
|
JavaScriptShared::toVariant(JSContext* cx, JS::HandleValue from, JSVariant* to)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
|
|
|
switch (JS_TypeOfValue(cx, from)) {
|
2017-02-13 19:26:16 +03:00
|
|
|
case JSTYPE_UNDEFINED:
|
2014-05-17 03:40:36 +04:00
|
|
|
*to = UndefinedVariant();
|
2013-07-03 11:24:32 +04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case JSTYPE_OBJECT:
|
|
|
|
case JSTYPE_FUNCTION:
|
|
|
|
{
|
2013-08-24 02:53:46 +04:00
|
|
|
RootedObject obj(cx, from.toObjectOrNull());
|
2013-07-03 11:24:32 +04:00
|
|
|
if (!obj) {
|
2015-06-30 04:36:44 +03:00
|
|
|
MOZ_ASSERT(from.isNull());
|
2014-05-17 03:40:36 +04:00
|
|
|
*to = NullVariant();
|
2013-07-03 11:24:32 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xpc_JSObjectIsID(cx, obj)) {
|
|
|
|
JSIID iid;
|
2015-03-29 01:22:11 +03:00
|
|
|
const nsID* id = xpc_JSObjectToID(cx, obj);
|
2013-07-03 11:24:32 +04:00
|
|
|
ConvertID(*id, &iid);
|
|
|
|
*to = iid;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-17 03:40:36 +04:00
|
|
|
ObjectVariant objVar;
|
|
|
|
if (!toObjectVariant(cx, obj, &objVar))
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
2014-05-17 03:40:36 +04:00
|
|
|
*to = objVar;
|
2013-07-03 11:24:32 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-07 13:29:03 +04:00
|
|
|
case JSTYPE_SYMBOL:
|
|
|
|
{
|
|
|
|
RootedSymbol sym(cx, from.toSymbol());
|
|
|
|
|
|
|
|
SymbolVariant symVar;
|
|
|
|
if (!toSymbolVariant(cx, sym, &symVar))
|
|
|
|
return false;
|
|
|
|
*to = symVar;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-03 11:24:32 +04:00
|
|
|
case JSTYPE_STRING:
|
|
|
|
{
|
2014-07-05 19:30:54 +04:00
|
|
|
nsAutoJSString autoStr;
|
|
|
|
if (!autoStr.init(cx, from))
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
2014-07-05 19:30:54 +04:00
|
|
|
*to = autoStr;
|
2013-07-03 11:24:32 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JSTYPE_NUMBER:
|
2014-04-28 06:47:02 +04:00
|
|
|
if (from.isInt32())
|
2013-07-03 11:24:32 +04:00
|
|
|
*to = double(from.toInt32());
|
|
|
|
else
|
|
|
|
*to = from.toDouble();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case JSTYPE_BOOLEAN:
|
|
|
|
*to = from.toBoolean();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-03-29 01:22:11 +03:00
|
|
|
JavaScriptShared::fromVariant(JSContext* cx, const JSVariant& from, MutableHandleValue to)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
|
|
|
switch (from.type()) {
|
2014-05-17 03:40:36 +04:00
|
|
|
case JSVariant::TUndefinedVariant:
|
2013-07-03 11:24:32 +04:00
|
|
|
to.set(UndefinedValue());
|
|
|
|
return true;
|
|
|
|
|
2014-05-17 03:40:36 +04:00
|
|
|
case JSVariant::TNullVariant:
|
|
|
|
to.set(NullValue());
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case JSVariant::TObjectVariant:
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2015-03-29 01:22:11 +03:00
|
|
|
JSObject* obj = fromObjectVariant(cx, from.get_ObjectVariant());
|
2014-05-17 03:40:36 +04:00
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
to.set(ObjectValue(*obj));
|
2013-07-03 11:24:32 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-07 13:29:03 +04:00
|
|
|
case JSVariant::TSymbolVariant:
|
|
|
|
{
|
2015-03-29 01:22:11 +03:00
|
|
|
Symbol* sym = fromSymbolVariant(cx, from.get_SymbolVariant());
|
2014-10-07 13:29:03 +04:00
|
|
|
if (!sym)
|
|
|
|
return false;
|
|
|
|
to.setSymbol(sym);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-03 11:24:32 +04:00
|
|
|
case JSVariant::Tdouble:
|
|
|
|
to.set(JS_NumberValue(from.get_double()));
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case JSVariant::Tbool:
|
2015-06-30 21:20:56 +03:00
|
|
|
to.setBoolean(from.get_bool());
|
2013-07-03 11:24:32 +04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case JSVariant::TnsString:
|
|
|
|
{
|
2015-03-29 01:22:11 +03:00
|
|
|
const nsString& old = from.get_nsString();
|
|
|
|
JSString* str = JS_NewUCStringCopyN(cx, old.BeginReading(), old.Length());
|
2013-07-03 11:24:32 +04:00
|
|
|
if (!str)
|
|
|
|
return false;
|
|
|
|
to.set(StringValue(str));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JSVariant::TJSIID:
|
|
|
|
{
|
|
|
|
nsID iid;
|
2015-03-29 01:22:11 +03:00
|
|
|
const JSIID& id = from.get_JSIID();
|
2013-07-03 11:24:32 +04:00
|
|
|
ConvertID(id, &iid);
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JSCompartment* compartment = GetContextCompartment(cx);
|
2013-07-03 11:24:32 +04:00
|
|
|
RootedObject global(cx, JS_GetGlobalForCompartmentOrNull(cx, compartment));
|
2015-03-29 01:22:11 +03:00
|
|
|
JSObject* obj = xpc_NewIDObject(cx, global, iid);
|
2013-07-03 11:24:32 +04:00
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
to.set(ObjectValue(*obj));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2014-10-07 13:29:02 +04:00
|
|
|
MOZ_CRASH("NYI");
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-07 13:29:02 +04:00
|
|
|
bool
|
2015-03-29 01:22:11 +03:00
|
|
|
JavaScriptShared::toJSIDVariant(JSContext* cx, HandleId from, JSIDVariant* to)
|
2014-10-07 13:29:02 +04:00
|
|
|
{
|
|
|
|
if (JSID_IS_STRING(from)) {
|
|
|
|
nsAutoJSString autoStr;
|
|
|
|
if (!autoStr.init(cx, JSID_TO_STRING(from)))
|
|
|
|
return false;
|
|
|
|
*to = autoStr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (JSID_IS_INT(from)) {
|
|
|
|
*to = JSID_TO_INT(from);
|
|
|
|
return true;
|
|
|
|
}
|
2014-10-07 13:29:03 +04:00
|
|
|
if (JSID_IS_SYMBOL(from)) {
|
|
|
|
SymbolVariant symVar;
|
|
|
|
if (!toSymbolVariant(cx, JSID_TO_SYMBOL(from), &symVar))
|
|
|
|
return false;
|
|
|
|
*to = symVar;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(false);
|
2014-10-07 13:29:02 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-03-29 01:22:11 +03:00
|
|
|
JavaScriptShared::fromJSIDVariant(JSContext* cx, const JSIDVariant& from, MutableHandleId to)
|
2014-10-07 13:29:02 +04:00
|
|
|
{
|
|
|
|
switch (from.type()) {
|
2014-10-07 13:29:03 +04:00
|
|
|
case JSIDVariant::TSymbolVariant: {
|
2015-03-29 01:22:11 +03:00
|
|
|
Symbol* sym = fromSymbolVariant(cx, from.get_SymbolVariant());
|
2014-10-07 13:29:03 +04:00
|
|
|
if (!sym)
|
|
|
|
return false;
|
|
|
|
to.set(SYMBOL_TO_JSID(sym));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-07 13:29:02 +04:00
|
|
|
case JSIDVariant::TnsString:
|
|
|
|
return convertGeckoStringToId(cx, from.get_nsString(), to);
|
|
|
|
|
|
|
|
case JSIDVariant::Tint32_t:
|
|
|
|
to.set(INT_TO_JSID(from.get_int32_t()));
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-07 13:29:03 +04:00
|
|
|
bool
|
2015-03-29 01:22:11 +03:00
|
|
|
JavaScriptShared::toSymbolVariant(JSContext* cx, JS::Symbol* symArg, SymbolVariant* symVarp)
|
2014-10-07 13:29:03 +04:00
|
|
|
{
|
|
|
|
RootedSymbol sym(cx, symArg);
|
|
|
|
MOZ_ASSERT(sym);
|
|
|
|
|
|
|
|
SymbolCode code = GetSymbolCode(sym);
|
|
|
|
if (static_cast<uint32_t>(code) < WellKnownSymbolLimit) {
|
|
|
|
*symVarp = WellKnownSymbol(static_cast<uint32_t>(code));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (code == SymbolCode::InSymbolRegistry) {
|
|
|
|
nsAutoJSString autoStr;
|
|
|
|
if (!autoStr.init(cx, GetSymbolDescription(sym)))
|
|
|
|
return false;
|
|
|
|
*symVarp = RegisteredSymbol(autoStr);
|
|
|
|
return true;
|
|
|
|
}
|
2014-10-16 20:39:38 +04:00
|
|
|
|
2016-08-14 14:39:28 +03:00
|
|
|
JS_ReportErrorASCII(cx, "unique symbol can't be used with CPOW");
|
2014-10-07 13:29:03 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JS::Symbol*
|
2016-10-20 23:37:26 +03:00
|
|
|
JavaScriptShared::fromSymbolVariant(JSContext* cx, const SymbolVariant& symVar)
|
2014-10-07 13:29:03 +04:00
|
|
|
{
|
|
|
|
switch (symVar.type()) {
|
|
|
|
case SymbolVariant::TWellKnownSymbol: {
|
|
|
|
uint32_t which = symVar.get_WellKnownSymbol().which();
|
|
|
|
if (which < WellKnownSymbolLimit)
|
|
|
|
return GetWellKnownSymbol(cx, static_cast<SymbolCode>(which));
|
|
|
|
MOZ_ASSERT(false, "bad child data");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SymbolVariant::TRegisteredSymbol: {
|
|
|
|
nsString key = symVar.get_RegisteredSymbol().key();
|
|
|
|
RootedString str(cx, JS_NewUCStringCopyN(cx, key.get(), key.Length()));
|
|
|
|
if (!str)
|
|
|
|
return nullptr;
|
|
|
|
return GetSymbolFor(cx, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-03 11:24:32 +04:00
|
|
|
/* static */ void
|
2015-03-29 01:22:11 +03:00
|
|
|
JavaScriptShared::ConvertID(const nsID& from, JSIID* to)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
|
|
|
to->m0() = from.m0;
|
|
|
|
to->m1() = from.m1;
|
|
|
|
to->m2() = from.m2;
|
|
|
|
to->m3_0() = from.m3[0];
|
|
|
|
to->m3_1() = from.m3[1];
|
|
|
|
to->m3_2() = from.m3[2];
|
|
|
|
to->m3_3() = from.m3[3];
|
|
|
|
to->m3_4() = from.m3[4];
|
|
|
|
to->m3_5() = from.m3[5];
|
|
|
|
to->m3_6() = from.m3[6];
|
|
|
|
to->m3_7() = from.m3[7];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
2015-03-29 01:22:11 +03:00
|
|
|
JavaScriptShared::ConvertID(const JSIID& from, nsID* to)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
|
|
|
to->m0 = from.m0();
|
|
|
|
to->m1 = from.m1();
|
|
|
|
to->m2 = from.m2();
|
|
|
|
to->m3[0] = from.m3_0();
|
|
|
|
to->m3[1] = from.m3_1();
|
|
|
|
to->m3[2] = from.m3_2();
|
|
|
|
to->m3[3] = from.m3_3();
|
|
|
|
to->m3[4] = from.m3_4();
|
|
|
|
to->m3[5] = from.m3_5();
|
|
|
|
to->m3[6] = from.m3_6();
|
|
|
|
to->m3[7] = from.m3_7();
|
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JSObject*
|
|
|
|
JavaScriptShared::findObjectById(JSContext* cx, const ObjectId& objId)
|
2014-05-17 03:40:37 +04:00
|
|
|
{
|
2014-09-25 15:13:29 +04:00
|
|
|
RootedObject obj(cx, objects_.find(objId));
|
2014-05-17 03:40:37 +04:00
|
|
|
if (!obj) {
|
2016-08-14 14:39:28 +03:00
|
|
|
JS_ReportErrorASCII(cx, "operation not possible on dead CPOW");
|
2014-05-17 03:40:37 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-09-13 04:41:18 +04:00
|
|
|
// Each process has a dedicated compartment for CPOW targets. All CPOWs
|
|
|
|
// from the other process point to objects in this scope. From there, they
|
|
|
|
// can access objects in other compartments using cross-compartment
|
|
|
|
// wrappers.
|
|
|
|
JSAutoCompartment ac(cx, scopeForTargetObjects());
|
2014-09-25 15:13:29 +04:00
|
|
|
if (objId.hasXrayWaiver()) {
|
2014-11-03 18:10:27 +03:00
|
|
|
{
|
|
|
|
JSAutoCompartment ac2(cx, obj);
|
2015-11-06 21:03:51 +03:00
|
|
|
obj = js::ToWindowProxyIfWindow(obj);
|
|
|
|
MOZ_ASSERT(obj);
|
2014-11-03 18:10:27 +03:00
|
|
|
}
|
2014-09-25 15:13:29 +04:00
|
|
|
if (!xpc::WrapperFactory::WaiveXrayAndWrap(cx, &obj))
|
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
if (!JS_WrapObject(cx, &obj))
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-05-17 03:40:37 +04:00
|
|
|
return obj;
|
2014-05-17 03:40:37 +04:00
|
|
|
}
|
|
|
|
|
2014-11-22 17:43:56 +03:00
|
|
|
static const uint64_t UnknownPropertyOp = 1;
|
2013-07-03 11:24:32 +04:00
|
|
|
|
|
|
|
bool
|
2016-01-28 13:28:04 +03:00
|
|
|
JavaScriptShared::fromDescriptor(JSContext* cx, Handle<PropertyDescriptor> desc,
|
2015-03-29 01:22:11 +03:00
|
|
|
PPropertyDescriptor* out)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2013-04-30 21:29:40 +04:00
|
|
|
out->attrs() = desc.attributes();
|
|
|
|
if (!toVariant(cx, desc.value(), &out->value()))
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
|
|
|
|
2014-10-20 20:47:27 +04:00
|
|
|
if (!toObjectOrNullVariant(cx, desc.object(), &out->obj()))
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
|
|
|
|
2013-04-30 21:29:40 +04:00
|
|
|
if (!desc.getter()) {
|
2013-07-03 11:24:32 +04:00
|
|
|
out->getter() = 0;
|
2013-04-30 21:29:40 +04:00
|
|
|
} else if (desc.hasGetterObject()) {
|
2015-03-29 01:22:11 +03:00
|
|
|
JSObject* getter = desc.getterObject();
|
2014-05-17 03:40:36 +04:00
|
|
|
ObjectVariant objVar;
|
|
|
|
if (!toObjectVariant(cx, getter, &objVar))
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
2014-05-17 03:40:36 +04:00
|
|
|
out->getter() = objVar;
|
2013-07-03 11:24:32 +04:00
|
|
|
} else {
|
2014-11-22 21:23:39 +03:00
|
|
|
MOZ_ASSERT(desc.getter() != JS_PropertyStub);
|
|
|
|
out->getter() = UnknownPropertyOp;
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
2013-04-30 21:29:40 +04:00
|
|
|
if (!desc.setter()) {
|
2013-07-03 11:24:32 +04:00
|
|
|
out->setter() = 0;
|
2013-04-30 21:29:40 +04:00
|
|
|
} else if (desc.hasSetterObject()) {
|
2015-03-29 01:22:11 +03:00
|
|
|
JSObject* setter = desc.setterObject();
|
2014-05-17 03:40:36 +04:00
|
|
|
ObjectVariant objVar;
|
|
|
|
if (!toObjectVariant(cx, setter, &objVar))
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
2014-05-17 03:40:36 +04:00
|
|
|
out->setter() = objVar;
|
2013-07-03 11:24:32 +04:00
|
|
|
} else {
|
2014-11-22 21:23:39 +03:00
|
|
|
MOZ_ASSERT(desc.setter() != JS_StrictPropertyStub);
|
|
|
|
out->setter() = UnknownPropertyOp;
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
bool
|
2015-03-29 01:22:11 +03:00
|
|
|
UnknownPropertyStub(JSContext* cx, HandleObject obj, HandleId id, MutableHandleValue vp)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2016-08-14 14:39:28 +03:00
|
|
|
JS_ReportErrorASCII(cx, "getter could not be wrapped via CPOWs");
|
2013-08-07 10:59:54 +04:00
|
|
|
return false;
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
bool
|
2015-03-29 01:22:11 +03:00
|
|
|
UnknownStrictPropertyStub(JSContext* cx, HandleObject obj, HandleId id, MutableHandleValue vp,
|
|
|
|
ObjectOpResult& result)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2016-08-14 14:39:28 +03:00
|
|
|
JS_ReportErrorASCII(cx, "setter could not be wrapped via CPOWs");
|
2013-08-07 10:59:54 +04:00
|
|
|
return false;
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-03-29 01:22:11 +03:00
|
|
|
JavaScriptShared::toDescriptor(JSContext* cx, const PPropertyDescriptor& in,
|
2016-01-28 13:28:04 +03:00
|
|
|
MutableHandle<PropertyDescriptor> out)
|
2013-07-03 11:24:32 +04:00
|
|
|
{
|
2013-04-30 21:29:40 +04:00
|
|
|
out.setAttributes(in.attrs());
|
2014-05-17 03:40:35 +04:00
|
|
|
if (!fromVariant(cx, in.value(), out.value()))
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
2014-10-20 20:47:27 +04:00
|
|
|
out.object().set(fromObjectOrNullVariant(cx, in.obj()));
|
2013-07-03 11:24:32 +04:00
|
|
|
|
2014-05-17 03:40:36 +04:00
|
|
|
if (in.getter().type() == GetterSetter::Tuint64_t && !in.getter().get_uint64_t()) {
|
2013-04-30 21:29:40 +04:00
|
|
|
out.setGetter(nullptr);
|
2013-07-03 11:24:32 +04:00
|
|
|
} else if (in.attrs() & JSPROP_GETTER) {
|
2013-08-08 03:15:57 +04:00
|
|
|
Rooted<JSObject*> getter(cx);
|
2014-05-17 03:40:36 +04:00
|
|
|
getter = fromObjectVariant(cx, in.getter().get_ObjectVariant());
|
|
|
|
if (!getter)
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
2015-01-30 22:56:15 +03:00
|
|
|
out.setGetter(JS_DATA_TO_FUNC_PTR(JSGetterOp, getter.get()));
|
2013-07-03 11:24:32 +04:00
|
|
|
} else {
|
2014-11-22 17:43:56 +03:00
|
|
|
out.setGetter(UnknownPropertyStub);
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
2014-05-17 03:40:36 +04:00
|
|
|
if (in.setter().type() == GetterSetter::Tuint64_t && !in.setter().get_uint64_t()) {
|
2013-04-30 21:29:40 +04:00
|
|
|
out.setSetter(nullptr);
|
2013-07-03 11:24:32 +04:00
|
|
|
} else if (in.attrs() & JSPROP_SETTER) {
|
2013-08-08 03:15:57 +04:00
|
|
|
Rooted<JSObject*> setter(cx);
|
2014-05-17 03:40:36 +04:00
|
|
|
setter = fromObjectVariant(cx, in.setter().get_ObjectVariant());
|
|
|
|
if (!setter)
|
2013-07-03 11:24:32 +04:00
|
|
|
return false;
|
2015-01-30 22:56:15 +03:00
|
|
|
out.setSetter(JS_DATA_TO_FUNC_PTR(JSSetterOp, setter.get()));
|
2013-07-03 11:24:32 +04:00
|
|
|
} else {
|
2014-11-22 17:43:56 +03:00
|
|
|
out.setSetter(UnknownStrictPropertyStub);
|
2013-07-03 11:24:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-20 20:47:27 +04:00
|
|
|
bool
|
2015-03-29 01:22:11 +03:00
|
|
|
JavaScriptShared::toObjectOrNullVariant(JSContext* cx, JSObject* obj, ObjectOrNullVariant* objVarp)
|
2014-10-20 20:47:27 +04:00
|
|
|
{
|
|
|
|
if (!obj) {
|
|
|
|
*objVarp = NullVariant();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectVariant objVar;
|
|
|
|
if (!toObjectVariant(cx, obj, &objVar))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*objVarp = objVar;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JSObject*
|
2016-10-20 23:37:26 +03:00
|
|
|
JavaScriptShared::fromObjectOrNullVariant(JSContext* cx, const ObjectOrNullVariant& objVar)
|
2014-10-20 20:47:27 +04:00
|
|
|
{
|
|
|
|
if (objVar.type() == ObjectOrNullVariant::TNullVariant)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return fromObjectVariant(cx, objVar.get_ObjectVariant());
|
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
CrossProcessCpowHolder::CrossProcessCpowHolder(dom::CPOWManagerGetter* managerGetter,
|
|
|
|
const InfallibleTArray<CpowEntry>& cpows)
|
2014-10-07 02:45:42 +04:00
|
|
|
: js_(nullptr),
|
2017-01-14 00:50:35 +03:00
|
|
|
cpows_(cpows),
|
|
|
|
unwrapped_(false)
|
2014-10-07 02:45:42 +04:00
|
|
|
{
|
|
|
|
// Only instantiate the CPOW manager if we might need it later.
|
|
|
|
if (cpows.Length())
|
|
|
|
js_ = managerGetter->GetCPOWManager();
|
|
|
|
}
|
|
|
|
|
2017-01-14 00:50:35 +03:00
|
|
|
CrossProcessCpowHolder::~CrossProcessCpowHolder()
|
|
|
|
{
|
|
|
|
if (cpows_.Length() && !unwrapped_) {
|
|
|
|
// This should only happen if a message manager message
|
|
|
|
// containing CPOWs gets ignored for some reason. We need to
|
|
|
|
// unwrap every incoming CPOW in this process to ensure that
|
|
|
|
// the corresponding part of the CPOW in the other process
|
|
|
|
// will eventually be collected. The scope for this object
|
|
|
|
// doesn't really matter, because it immediately becomes
|
|
|
|
// garbage.
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (!jsapi.Init(xpc::PrivilegedJunkScope()))
|
|
|
|
return;
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
JS::Rooted<JSObject*> cpows(cx);
|
|
|
|
js_->Unwrap(cx, cpows_, &cpows);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-11 02:05:39 +04:00
|
|
|
bool
|
2015-03-29 01:22:11 +03:00
|
|
|
CrossProcessCpowHolder::ToObject(JSContext* cx, JS::MutableHandleObject objp)
|
2013-07-11 02:05:39 +04:00
|
|
|
{
|
2017-01-14 00:50:35 +03:00
|
|
|
unwrapped_ = true;
|
|
|
|
|
2014-10-07 02:45:42 +04:00
|
|
|
if (!cpows_.Length())
|
|
|
|
return true;
|
|
|
|
|
2013-07-11 02:05:39 +04:00
|
|
|
return js_->Unwrap(cx, cpows_, objp);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-03-29 01:22:11 +03:00
|
|
|
JavaScriptShared::Unwrap(JSContext* cx, const InfallibleTArray<CpowEntry>& aCpows,
|
2013-10-16 04:02:23 +04:00
|
|
|
JS::MutableHandleObject objp)
|
2013-07-11 02:05:39 +04:00
|
|
|
{
|
2013-10-16 04:02:23 +04:00
|
|
|
objp.set(nullptr);
|
2013-07-11 02:05:39 +04:00
|
|
|
|
|
|
|
if (!aCpows.Length())
|
|
|
|
return true;
|
|
|
|
|
2015-01-24 18:38:08 +03:00
|
|
|
RootedObject obj(cx, JS_NewPlainObject(cx));
|
2013-07-11 02:05:39 +04:00
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
RootedValue v(cx);
|
|
|
|
RootedString str(cx);
|
|
|
|
for (size_t i = 0; i < aCpows.Length(); i++) {
|
2015-03-29 01:22:11 +03:00
|
|
|
const nsString& name = aCpows[i].name();
|
2013-07-11 02:05:39 +04:00
|
|
|
|
2014-05-17 03:40:35 +04:00
|
|
|
if (!fromVariant(cx, aCpows[i].value(), &v))
|
2013-07-11 02:05:39 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!JS_DefineUCProperty(cx,
|
|
|
|
obj,
|
|
|
|
name.BeginReading(),
|
|
|
|
name.Length(),
|
|
|
|
v,
|
|
|
|
JSPROP_ENUMERATE))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-16 04:02:23 +04:00
|
|
|
objp.set(obj);
|
2013-07-11 02:05:39 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-03-29 01:22:11 +03:00
|
|
|
JavaScriptShared::Wrap(JSContext* cx, HandleObject aObj, InfallibleTArray<CpowEntry>* outCpows)
|
2013-07-11 02:05:39 +04:00
|
|
|
{
|
|
|
|
if (!aObj)
|
|
|
|
return true;
|
|
|
|
|
2015-08-05 21:02:00 +03:00
|
|
|
Rooted<IdVector> ids(cx, IdVector(cx));
|
|
|
|
if (!JS_Enumerate(cx, aObj, &ids))
|
2013-07-11 02:05:39 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
RootedId id(cx);
|
|
|
|
RootedValue v(cx);
|
|
|
|
for (size_t i = 0; i < ids.length(); i++) {
|
|
|
|
id = ids[i];
|
|
|
|
|
|
|
|
nsString str;
|
|
|
|
if (!convertIdToGeckoString(cx, id, &str))
|
|
|
|
return false;
|
|
|
|
|
2013-07-26 13:00:38 +04:00
|
|
|
if (!JS_GetPropertyById(cx, aObj, id, &v))
|
2013-07-11 02:05:39 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
JSVariant var;
|
|
|
|
if (!toVariant(cx, v, &var))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
outCpows->AppendElement(CpowEntry(str, var));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-01-27 00:32:18 +03:00
|
|
|
|
|
|
|
CPOWManager*
|
|
|
|
mozilla::jsipc::CPOWManagerFor(PJavaScriptParent* aParent)
|
|
|
|
{
|
2015-03-29 01:22:11 +03:00
|
|
|
return static_cast<JavaScriptParent*>(aParent);
|
2015-01-27 00:32:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CPOWManager*
|
|
|
|
mozilla::jsipc::CPOWManagerFor(PJavaScriptChild* aChild)
|
|
|
|
{
|
2015-03-29 01:22:11 +03:00
|
|
|
return static_cast<JavaScriptChild*>(aChild);
|
2015-01-27 00:32:18 +03:00
|
|
|
}
|