2013-04-16 20:47:10 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
2012-05-21 11:12:37 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-06-03 22:27:29 +00:00
|
|
|
|
2014-09-09 21:31:20 +00:00
|
|
|
#ifndef jswrapper_h
|
|
|
|
#define jswrapper_h
|
2010-06-03 22:27:29 +00:00
|
|
|
|
2011-11-20 20:22:51 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2011-11-14 18:31:46 +00:00
|
|
|
|
2015-02-10 18:58:18 +00:00
|
|
|
#include "js/Proxy.h"
|
2010-06-03 22:27:29 +00:00
|
|
|
|
2011-09-09 03:29:15 +00:00
|
|
|
namespace js {
|
2010-09-26 03:05:36 +00:00
|
|
|
|
2012-01-02 18:05:19 +00:00
|
|
|
class DummyFrameGuard;
|
|
|
|
|
2014-01-30 01:20:16 +00:00
|
|
|
/*
|
|
|
|
* Helper for Wrapper::New default options.
|
|
|
|
*
|
|
|
|
* Callers of Wrapper::New() who wish to specify a prototype for the created
|
|
|
|
* Wrapper, *MUST* construct a WrapperOptions with a JSContext.
|
|
|
|
*/
|
|
|
|
class MOZ_STACK_CLASS WrapperOptions : public ProxyOptions {
|
|
|
|
public:
|
2014-09-10 22:52:36 +00:00
|
|
|
WrapperOptions() : ProxyOptions(false),
|
2014-01-30 01:20:16 +00:00
|
|
|
proto_()
|
|
|
|
{}
|
|
|
|
|
2014-09-10 22:52:36 +00:00
|
|
|
explicit WrapperOptions(JSContext *cx) : ProxyOptions(false),
|
2014-05-26 01:46:24 +00:00
|
|
|
proto_()
|
2014-01-30 01:20:16 +00:00
|
|
|
{
|
2014-08-13 22:42:00 +00:00
|
|
|
proto_.emplace(cx);
|
2014-01-30 01:20:16 +00:00
|
|
|
}
|
|
|
|
|
2014-01-30 01:57:36 +00:00
|
|
|
inline JSObject *proto() const;
|
2014-01-30 01:20:16 +00:00
|
|
|
WrapperOptions &setProto(JSObject *protoArg) {
|
2014-10-01 17:17:51 +00:00
|
|
|
MOZ_ASSERT(proto_);
|
2014-08-13 22:42:00 +00:00
|
|
|
*proto_ = protoArg;
|
2014-01-30 01:20:16 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
mozilla::Maybe<JS::RootedObject> proto_;
|
|
|
|
};
|
|
|
|
|
2012-07-08 17:04:14 +00:00
|
|
|
/*
|
2012-10-29 15:52:53 +00:00
|
|
|
* A wrapper is a proxy with a target object to which it generally forwards
|
2014-10-08 17:09:08 +00:00
|
|
|
* operations, but may restrict access to certain operations or instrument the
|
|
|
|
* methods in various ways. A wrapper is distinct from a Direct Proxy Handler
|
|
|
|
* in the sense that it can be "unwrapped" in C++, exposing the underlying
|
2012-10-29 15:52:53 +00:00
|
|
|
* object (Direct Proxy Handlers have an underlying target object, but don't
|
|
|
|
* expect to expose this object via any kind of unwrapping operation). Callers
|
2014-10-08 17:09:08 +00:00
|
|
|
* should be careful to avoid unwrapping security wrappers in the wrong
|
|
|
|
* context.
|
2012-07-08 17:04:14 +00:00
|
|
|
*/
|
2012-10-29 15:52:53 +00:00
|
|
|
class JS_FRIEND_API(Wrapper) : public DirectProxyHandler
|
2011-09-09 03:29:15 +00:00
|
|
|
{
|
2012-04-19 18:19:41 +00:00
|
|
|
unsigned mFlags;
|
2012-06-28 02:10:37 +00:00
|
|
|
|
2010-07-02 01:06:33 +00:00
|
|
|
public:
|
2013-02-25 21:54:18 +00:00
|
|
|
using BaseProxyHandler::Action;
|
2012-04-19 18:19:41 +00:00
|
|
|
|
2012-06-28 02:10:37 +00:00
|
|
|
enum Flags {
|
|
|
|
CROSS_COMPARTMENT = 1 << 0,
|
|
|
|
LAST_USED_FLAG = CROSS_COMPARTMENT
|
|
|
|
};
|
2010-06-03 22:27:29 +00:00
|
|
|
|
2013-09-05 00:58:22 +00:00
|
|
|
virtual bool defaultValue(JSContext *cx, HandleObject obj, JSType hint,
|
2014-06-27 11:44:06 +00:00
|
|
|
MutableHandleValue vp) const MOZ_OVERRIDE;
|
2013-09-05 00:58:22 +00:00
|
|
|
|
2015-02-26 20:58:59 +00:00
|
|
|
static JSObject *New(JSContext *cx, JSObject *obj, const Wrapper *handler,
|
2014-09-10 22:52:36 +00:00
|
|
|
const WrapperOptions &options = WrapperOptions());
|
2012-06-28 02:10:37 +00:00
|
|
|
|
2014-06-27 11:44:04 +00:00
|
|
|
static JSObject *Renew(JSContext *cx, JSObject *existing, JSObject *obj, const Wrapper *handler);
|
2012-09-12 00:14:24 +00:00
|
|
|
|
2014-06-27 11:44:08 +00:00
|
|
|
static const Wrapper *wrapperHandler(JSObject *wrapper);
|
2012-06-28 02:10:37 +00:00
|
|
|
|
2013-04-30 22:41:12 +00:00
|
|
|
static JSObject *wrappedObject(JSObject *wrapper);
|
2012-06-28 02:10:37 +00:00
|
|
|
|
|
|
|
unsigned flags() const {
|
|
|
|
return mFlags;
|
|
|
|
}
|
|
|
|
|
2014-08-28 20:47:16 +00:00
|
|
|
explicit MOZ_CONSTEXPR Wrapper(unsigned aFlags, bool aHasPrototype = false,
|
|
|
|
bool aHasSecurityPolicy = false)
|
|
|
|
: DirectProxyHandler(&family, aHasPrototype, aHasSecurityPolicy),
|
|
|
|
mFlags(aFlags)
|
|
|
|
{ }
|
2012-04-15 23:43:14 +00:00
|
|
|
|
2014-06-27 11:44:06 +00:00
|
|
|
virtual bool finalizeInBackground(Value priv) const MOZ_OVERRIDE;
|
2014-09-10 22:52:36 +00:00
|
|
|
virtual bool isConstructor(JSObject *obj) const MOZ_OVERRIDE;
|
2013-10-04 11:29:36 +00:00
|
|
|
|
2014-08-28 00:09:06 +00:00
|
|
|
static const char family;
|
2014-06-27 11:44:04 +00:00
|
|
|
static const Wrapper singleton;
|
|
|
|
static const Wrapper singletonWithPrototype;
|
2014-01-30 01:20:16 +00:00
|
|
|
|
|
|
|
static JSObject *defaultProto;
|
2010-06-23 21:35:10 +00:00
|
|
|
};
|
|
|
|
|
2014-01-30 01:57:36 +00:00
|
|
|
inline JSObject *
|
|
|
|
WrapperOptions::proto() const
|
|
|
|
{
|
2014-08-13 22:42:00 +00:00
|
|
|
return proto_ ? *proto_ : Wrapper::defaultProto;
|
2014-01-30 01:57:36 +00:00
|
|
|
}
|
|
|
|
|
2010-06-24 21:45:32 +00:00
|
|
|
/* Base class for all cross compartment wrapper handlers. */
|
2012-10-29 15:52:53 +00:00
|
|
|
class JS_FRIEND_API(CrossCompartmentWrapper) : public Wrapper
|
2011-09-09 03:29:15 +00:00
|
|
|
{
|
2010-06-24 21:45:32 +00:00
|
|
|
public:
|
2014-08-28 20:47:16 +00:00
|
|
|
explicit MOZ_CONSTEXPR CrossCompartmentWrapper(unsigned aFlags, bool aHasPrototype = false,
|
|
|
|
bool aHasSecurityPolicy = false)
|
|
|
|
: Wrapper(CROSS_COMPARTMENT | aFlags, aHasPrototype, aHasSecurityPolicy)
|
|
|
|
{ }
|
2010-06-25 22:58:09 +00:00
|
|
|
|
2014-10-08 17:09:08 +00:00
|
|
|
/* Standard internal methods. */
|
2013-03-21 22:23:47 +00:00
|
|
|
virtual bool getOwnPropertyDescriptor(JSContext *cx, HandleObject wrapper, HandleId id,
|
2014-06-27 11:44:06 +00:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc) const MOZ_OVERRIDE;
|
2013-03-21 22:23:47 +00:00
|
|
|
virtual bool defineProperty(JSContext *cx, HandleObject wrapper, HandleId id,
|
2015-01-30 17:37:07 +00:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
ObjectOpResult &result) const MOZ_OVERRIDE;
|
2014-09-26 20:16:36 +00:00
|
|
|
virtual bool ownPropertyKeys(JSContext *cx, HandleObject wrapper,
|
|
|
|
AutoIdVector &props) const MOZ_OVERRIDE;
|
2015-02-04 16:20:04 +00:00
|
|
|
virtual bool delete_(JSContext *cx, HandleObject wrapper, HandleId id,
|
|
|
|
ObjectOpResult &result) const MOZ_OVERRIDE;
|
2014-12-16 23:28:38 +00:00
|
|
|
virtual bool enumerate(JSContext *cx, HandleObject wrapper, MutableHandleObject objp) const MOZ_OVERRIDE;
|
2015-02-05 22:36:50 +00:00
|
|
|
virtual bool getPrototype(JSContext *cx, HandleObject proxy,
|
|
|
|
MutableHandleObject protop) const MOZ_OVERRIDE;
|
|
|
|
virtual bool setPrototype(JSContext *cx, HandleObject proxy, HandleObject proto,
|
|
|
|
ObjectOpResult &result) const MOZ_OVERRIDE;
|
2014-12-16 23:28:38 +00:00
|
|
|
|
2014-09-23 20:03:40 +00:00
|
|
|
virtual bool setImmutablePrototype(JSContext *cx, HandleObject proxy,
|
|
|
|
bool *succeeded) const MOZ_OVERRIDE;
|
2015-02-04 18:01:01 +00:00
|
|
|
virtual bool preventExtensions(JSContext *cx, HandleObject wrapper,
|
|
|
|
ObjectOpResult &result) const MOZ_OVERRIDE;
|
2014-10-21 18:40:04 +00:00
|
|
|
virtual bool isExtensible(JSContext *cx, HandleObject wrapper, bool *extensible) const MOZ_OVERRIDE;
|
2014-06-27 11:44:06 +00:00
|
|
|
virtual bool has(JSContext *cx, HandleObject wrapper, HandleId id, bool *bp) const MOZ_OVERRIDE;
|
2013-03-21 22:23:48 +00:00
|
|
|
virtual bool get(JSContext *cx, HandleObject wrapper, HandleObject receiver,
|
2014-06-27 11:44:06 +00:00
|
|
|
HandleId id, MutableHandleValue vp) const MOZ_OVERRIDE;
|
2015-02-04 01:51:40 +00:00
|
|
|
virtual bool set(JSContext *cx, HandleObject wrapper, HandleObject receiver, HandleId id,
|
|
|
|
MutableHandleValue vp, ObjectOpResult &result) const MOZ_OVERRIDE;
|
2014-10-08 17:09:08 +00:00
|
|
|
virtual bool call(JSContext *cx, HandleObject wrapper, const CallArgs &args) const MOZ_OVERRIDE;
|
|
|
|
virtual bool construct(JSContext *cx, HandleObject wrapper, const CallArgs &args) const MOZ_OVERRIDE;
|
|
|
|
|
|
|
|
/* SpiderMonkey extensions. */
|
|
|
|
virtual bool getPropertyDescriptor(JSContext *cx, HandleObject wrapper, HandleId id,
|
|
|
|
MutableHandle<JSPropertyDescriptor> desc) const MOZ_OVERRIDE;
|
|
|
|
virtual bool hasOwn(JSContext *cx, HandleObject wrapper, HandleId id, bool *bp) const MOZ_OVERRIDE;
|
2014-10-09 03:01:55 +00:00
|
|
|
virtual bool getOwnEnumerablePropertyKeys(JSContext *cx, HandleObject wrapper,
|
|
|
|
AutoIdVector &props) const MOZ_OVERRIDE;
|
2012-07-04 00:44:22 +00:00
|
|
|
virtual bool nativeCall(JSContext *cx, IsAcceptableThis test, NativeImpl impl,
|
2014-06-27 11:44:06 +00:00
|
|
|
CallArgs args) const MOZ_OVERRIDE;
|
2013-03-21 22:23:48 +00:00
|
|
|
virtual bool hasInstance(JSContext *cx, HandleObject wrapper, MutableHandleValue v,
|
2014-06-27 11:44:06 +00:00
|
|
|
bool *bp) const MOZ_OVERRIDE;
|
|
|
|
virtual const char *className(JSContext *cx, HandleObject proxy) const MOZ_OVERRIDE;
|
2013-03-21 22:23:48 +00:00
|
|
|
virtual JSString *fun_toString(JSContext *cx, HandleObject wrapper,
|
2014-06-27 11:44:06 +00:00
|
|
|
unsigned indent) const MOZ_OVERRIDE;
|
|
|
|
virtual bool regexp_toShared(JSContext *cx, HandleObject proxy, RegExpGuard *g) const MOZ_OVERRIDE;
|
2014-08-18 21:18:39 +00:00
|
|
|
virtual bool boxedValue_unbox(JSContext *cx, HandleObject proxy, MutableHandleValue vp) const MOZ_OVERRIDE;
|
2013-03-21 22:23:48 +00:00
|
|
|
virtual bool defaultValue(JSContext *cx, HandleObject wrapper, JSType hint,
|
2014-06-27 11:44:06 +00:00
|
|
|
MutableHandleValue vp) const MOZ_OVERRIDE;
|
2011-07-08 00:31:24 +00:00
|
|
|
|
2014-06-27 11:44:04 +00:00
|
|
|
static const CrossCompartmentWrapper singleton;
|
|
|
|
static const CrossCompartmentWrapper singletonWithPrototype;
|
2010-06-24 21:45:32 +00:00
|
|
|
};
|
|
|
|
|
2011-10-04 17:50:25 +00:00
|
|
|
/*
|
|
|
|
* Base class for security wrappers. A security wrapper is potentially hiding
|
|
|
|
* all or part of some wrapped object thus SecurityWrapper defaults to denying
|
|
|
|
* access to the wrappee. This is the opposite of Wrapper which tries to be
|
|
|
|
* completely transparent.
|
|
|
|
*
|
|
|
|
* NB: Currently, only a few ProxyHandler operations are overridden to deny
|
|
|
|
* access, relying on derived SecurityWrapper to block access when necessary.
|
|
|
|
*/
|
|
|
|
template <class Base>
|
|
|
|
class JS_FRIEND_API(SecurityWrapper) : public Base
|
|
|
|
{
|
|
|
|
public:
|
2014-08-28 20:47:16 +00:00
|
|
|
explicit MOZ_CONSTEXPR SecurityWrapper(unsigned flags, bool hasPrototype = false)
|
|
|
|
: Base(flags, hasPrototype, /* hasSecurityPolicy = */ true)
|
|
|
|
{ }
|
2011-10-04 17:50:25 +00:00
|
|
|
|
2013-03-21 22:23:48 +00:00
|
|
|
virtual bool enter(JSContext *cx, HandleObject wrapper, HandleId id, Wrapper::Action act,
|
2014-06-27 11:44:06 +00:00
|
|
|
bool *bp) const MOZ_OVERRIDE;
|
2014-10-08 17:09:08 +00:00
|
|
|
|
|
|
|
virtual bool defineProperty(JSContext *cx, HandleObject wrapper, HandleId id,
|
2015-01-30 17:37:07 +00:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
ObjectOpResult &result) const MOZ_OVERRIDE;
|
2014-10-08 17:09:08 +00:00
|
|
|
virtual bool isExtensible(JSContext *cx, HandleObject wrapper, bool *extensible) const MOZ_OVERRIDE;
|
2015-02-04 18:01:01 +00:00
|
|
|
virtual bool preventExtensions(JSContext *cx, HandleObject wrapper,
|
|
|
|
ObjectOpResult &result) const MOZ_OVERRIDE;
|
2015-02-05 22:36:50 +00:00
|
|
|
virtual bool setPrototype(JSContext *cx, HandleObject proxy, HandleObject proto,
|
|
|
|
ObjectOpResult &result) const MOZ_OVERRIDE;
|
2014-09-23 20:03:40 +00:00
|
|
|
virtual bool setImmutablePrototype(JSContext *cx, HandleObject proxy, bool *succeeded) const MOZ_OVERRIDE;
|
2014-10-08 17:09:08 +00:00
|
|
|
|
2012-07-04 00:44:22 +00:00
|
|
|
virtual bool nativeCall(JSContext *cx, IsAcceptableThis test, NativeImpl impl,
|
2014-06-27 11:44:06 +00:00
|
|
|
CallArgs args) const MOZ_OVERRIDE;
|
2013-03-21 22:23:48 +00:00
|
|
|
virtual bool objectClassIs(HandleObject obj, ESClassValue classValue,
|
2014-06-27 11:44:06 +00:00
|
|
|
JSContext *cx) const MOZ_OVERRIDE;
|
|
|
|
virtual bool regexp_toShared(JSContext *cx, HandleObject proxy, RegExpGuard *g) const MOZ_OVERRIDE;
|
2014-08-18 21:18:39 +00:00
|
|
|
virtual bool boxedValue_unbox(JSContext *cx, HandleObject proxy, MutableHandleValue vp) const MOZ_OVERRIDE;
|
2014-10-08 17:09:08 +00:00
|
|
|
virtual bool defaultValue(JSContext *cx, HandleObject wrapper, JSType hint,
|
|
|
|
MutableHandleValue vp) const MOZ_OVERRIDE;
|
2012-12-21 06:33:26 +00:00
|
|
|
|
2014-10-08 17:09:08 +00:00
|
|
|
// Allow isCallable and isConstructor. They used to be class-level, and so could not be guarded
|
|
|
|
// against.
|
2013-12-13 20:01:30 +00:00
|
|
|
|
2013-12-05 20:58:20 +00:00
|
|
|
virtual bool watch(JSContext *cx, JS::HandleObject proxy, JS::HandleId id,
|
2014-06-27 11:44:06 +00:00
|
|
|
JS::HandleObject callable) const MOZ_OVERRIDE;
|
|
|
|
virtual bool unwatch(JSContext *cx, JS::HandleObject proxy, JS::HandleId id) const MOZ_OVERRIDE;
|
2013-12-05 20:58:20 +00:00
|
|
|
|
2012-12-21 06:33:26 +00:00
|
|
|
/*
|
|
|
|
* Allow our subclasses to select the superclass behavior they want without
|
|
|
|
* needing to specify an exact superclass.
|
|
|
|
*/
|
|
|
|
typedef Base Permissive;
|
|
|
|
typedef SecurityWrapper<Base> Restrictive;
|
2011-10-04 17:50:25 +00:00
|
|
|
};
|
|
|
|
|
2012-10-29 15:52:53 +00:00
|
|
|
typedef SecurityWrapper<Wrapper> SameCompartmentSecurityWrapper;
|
2011-10-04 17:50:25 +00:00
|
|
|
typedef SecurityWrapper<CrossCompartmentWrapper> CrossCompartmentSecurityWrapper;
|
|
|
|
|
2010-06-24 21:45:32 +00:00
|
|
|
extern JSObject *
|
2015-02-26 20:58:59 +00:00
|
|
|
TransparentObjectWrapper(JSContext *cx, HandleObject existing, HandleObject obj);
|
2010-06-24 21:45:32 +00:00
|
|
|
|
2012-02-21 18:31:35 +00:00
|
|
|
inline bool
|
2013-04-30 22:41:12 +00:00
|
|
|
IsWrapper(JSObject *obj)
|
2012-02-21 18:31:35 +00:00
|
|
|
{
|
2014-08-28 00:09:06 +00:00
|
|
|
return IsProxy(obj) && GetProxyHandler(obj)->family() == &Wrapper::family;
|
2012-02-21 18:31:35 +00:00
|
|
|
}
|
2012-01-26 13:55:27 +00:00
|
|
|
|
|
|
|
// Given a JSObject, returns that object stripped of wrappers. If
|
|
|
|
// stopAtOuter is true, then this returns the outer window if it was
|
|
|
|
// previously wrapped. Otherwise, this returns the first object for
|
|
|
|
// which JSObject::isWrapper returns false.
|
2012-05-22 21:54:08 +00:00
|
|
|
JS_FRIEND_API(JSObject *)
|
2013-10-07 16:44:37 +00:00
|
|
|
UncheckedUnwrap(JSObject *obj, bool stopAtOuter = true, unsigned *flagsp = nullptr);
|
2012-01-26 13:55:27 +00:00
|
|
|
|
2012-03-23 21:59:27 +00:00
|
|
|
// Given a JSObject, returns that object stripped of wrappers. At each stage,
|
|
|
|
// the security wrapper has the opportunity to veto the unwrap. Since checked
|
|
|
|
// code should never be unwrapping outer window wrappers, we always stop at
|
|
|
|
// outer windows.
|
2012-05-22 21:54:08 +00:00
|
|
|
JS_FRIEND_API(JSObject *)
|
2013-04-30 22:41:12 +00:00
|
|
|
CheckedUnwrap(JSObject *obj, bool stopAtOuter = true);
|
2012-03-23 21:59:27 +00:00
|
|
|
|
2012-07-18 11:51:28 +00:00
|
|
|
// Unwrap only the outermost security wrapper, with the same semantics as
|
|
|
|
// above. This is the checked version of Wrapper::wrappedObject.
|
|
|
|
JS_FRIEND_API(JSObject *)
|
2013-04-30 22:41:12 +00:00
|
|
|
UnwrapOneChecked(JSObject *obj, bool stopAtOuter = true);
|
2012-07-18 11:51:28 +00:00
|
|
|
|
2012-06-04 08:13:18 +00:00
|
|
|
JS_FRIEND_API(bool)
|
2013-04-30 22:41:12 +00:00
|
|
|
IsCrossCompartmentWrapper(JSObject *obj);
|
2012-05-22 21:54:08 +00:00
|
|
|
|
|
|
|
void
|
2012-09-03 23:42:22 +00:00
|
|
|
NukeCrossCompartmentWrapper(JSContext *cx, JSObject *wrapper);
|
2011-10-04 14:06:54 +00:00
|
|
|
|
2012-07-04 10:13:01 +00:00
|
|
|
bool
|
|
|
|
RemapWrapper(JSContext *cx, JSObject *wobj, JSObject *newTarget);
|
|
|
|
|
2012-07-23 15:57:39 +00:00
|
|
|
JS_FRIEND_API(bool)
|
2012-07-04 10:13:01 +00:00
|
|
|
RemapAllWrappersForObject(JSContext *cx, JSObject *oldTarget,
|
|
|
|
JSObject *newTarget);
|
2012-07-04 10:13:01 +00:00
|
|
|
|
|
|
|
// API to recompute all cross-compartment wrappers whose source and target
|
|
|
|
// match the given filters.
|
|
|
|
JS_FRIEND_API(bool)
|
|
|
|
RecomputeWrappers(JSContext *cx, const CompartmentFilter &sourceFilter,
|
|
|
|
const CompartmentFilter &targetFilter);
|
|
|
|
|
2011-10-04 14:06:54 +00:00
|
|
|
} /* namespace js */
|
2010-06-03 22:27:29 +00:00
|
|
|
|
2013-06-20 00:59:46 +00:00
|
|
|
#endif /* jswrapper_h */
|