js/xpconnect/src/XPCWrapper.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/xpconnect/src/XPCWrapper.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,98 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "xpcprivate.h"
    1.11 +#include "XPCWrapper.h"
    1.12 +#include "WrapperFactory.h"
    1.13 +#include "AccessCheck.h"
    1.14 +
    1.15 +using namespace xpc;
    1.16 +using namespace mozilla;
    1.17 +
    1.18 +namespace XPCNativeWrapper {
    1.19 +
    1.20 +static inline
    1.21 +bool
    1.22 +ThrowException(nsresult ex, JSContext *cx)
    1.23 +{
    1.24 +  XPCThrower::Throw(ex, cx);
    1.25 +
    1.26 +  return false;
    1.27 +}
    1.28 +
    1.29 +static bool
    1.30 +UnwrapNW(JSContext *cx, unsigned argc, jsval *vp)
    1.31 +{
    1.32 +  JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
    1.33 +  if (args.length() != 1) {
    1.34 +    return ThrowException(NS_ERROR_XPC_NOT_ENOUGH_ARGS, cx);
    1.35 +  }
    1.36 +
    1.37 +  JS::RootedValue v(cx, args[0]);
    1.38 +  if (!v.isObject() || !js::IsWrapper(&v.toObject())) {
    1.39 +    args.rval().set(v);
    1.40 +    return true;
    1.41 +  }
    1.42 +
    1.43 +  if (AccessCheck::wrapperSubsumes(&v.toObject())) {
    1.44 +    bool ok = xpc::WrapperFactory::WaiveXrayAndWrap(cx, &v);
    1.45 +    NS_ENSURE_TRUE(ok, false);
    1.46 +  }
    1.47 +
    1.48 +  args.rval().set(v);
    1.49 +  return true;
    1.50 +}
    1.51 +
    1.52 +static bool
    1.53 +XrayWrapperConstructor(JSContext *cx, unsigned argc, jsval *vp)
    1.54 +{
    1.55 +  JS::CallArgs args = CallArgsFromVp(argc, vp);
    1.56 +  if (args.length() == 0) {
    1.57 +    return ThrowException(NS_ERROR_XPC_NOT_ENOUGH_ARGS, cx);
    1.58 +  }
    1.59 +
    1.60 +  if (!args[0].isObject()) {
    1.61 +    args.rval().set(args[0]);
    1.62 +    return true;
    1.63 +  }
    1.64 +
    1.65 +  args.rval().setObject(*js::UncheckedUnwrap(&args[0].toObject()));
    1.66 +  return JS_WrapValue(cx, args.rval());
    1.67 +}
    1.68 +// static
    1.69 +bool
    1.70 +AttachNewConstructorObject(JSContext *aCx, JS::HandleObject aGlobalObject)
    1.71 +{
    1.72 +  // Pushing a JSContext calls ActivateDebugger which calls this function, so
    1.73 +  // we can't use an AutoJSContext here until JSD is gone.
    1.74 +  JSAutoCompartment ac(aCx, aGlobalObject);
    1.75 +  JSFunction *xpcnativewrapper =
    1.76 +    JS_DefineFunction(aCx, aGlobalObject, "XPCNativeWrapper",
    1.77 +                      XrayWrapperConstructor, 1,
    1.78 +                      JSPROP_READONLY | JSPROP_PERMANENT | JSFUN_STUB_GSOPS | JSFUN_CONSTRUCTOR);
    1.79 +  if (!xpcnativewrapper) {
    1.80 +    return false;
    1.81 +  }
    1.82 +  JS::RootedObject obj(aCx, JS_GetFunctionObject(xpcnativewrapper));
    1.83 +  return JS_DefineFunction(aCx, obj, "unwrap", UnwrapNW, 1,
    1.84 +                           JSPROP_READONLY | JSPROP_PERMANENT) != nullptr;
    1.85 +}
    1.86 +
    1.87 +} // namespace XPCNativeWrapper
    1.88 +
    1.89 +namespace XPCWrapper {
    1.90 +
    1.91 +JSObject *
    1.92 +UnsafeUnwrapSecurityWrapper(JSObject *obj)
    1.93 +{
    1.94 +  if (js::IsProxy(obj)) {
    1.95 +    return js::UncheckedUnwrap(obj);
    1.96 +  }
    1.97 +
    1.98 +  return obj;
    1.99 +}
   1.100 +
   1.101 +} // namespace XPCWrapper

mercurial