js/xpconnect/src/XPCWrapper.cpp

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:d6ccb323bbad
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "xpcprivate.h"
8 #include "XPCWrapper.h"
9 #include "WrapperFactory.h"
10 #include "AccessCheck.h"
11
12 using namespace xpc;
13 using namespace mozilla;
14
15 namespace XPCNativeWrapper {
16
17 static inline
18 bool
19 ThrowException(nsresult ex, JSContext *cx)
20 {
21 XPCThrower::Throw(ex, cx);
22
23 return false;
24 }
25
26 static bool
27 UnwrapNW(JSContext *cx, unsigned argc, jsval *vp)
28 {
29 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
30 if (args.length() != 1) {
31 return ThrowException(NS_ERROR_XPC_NOT_ENOUGH_ARGS, cx);
32 }
33
34 JS::RootedValue v(cx, args[0]);
35 if (!v.isObject() || !js::IsWrapper(&v.toObject())) {
36 args.rval().set(v);
37 return true;
38 }
39
40 if (AccessCheck::wrapperSubsumes(&v.toObject())) {
41 bool ok = xpc::WrapperFactory::WaiveXrayAndWrap(cx, &v);
42 NS_ENSURE_TRUE(ok, false);
43 }
44
45 args.rval().set(v);
46 return true;
47 }
48
49 static bool
50 XrayWrapperConstructor(JSContext *cx, unsigned argc, jsval *vp)
51 {
52 JS::CallArgs args = CallArgsFromVp(argc, vp);
53 if (args.length() == 0) {
54 return ThrowException(NS_ERROR_XPC_NOT_ENOUGH_ARGS, cx);
55 }
56
57 if (!args[0].isObject()) {
58 args.rval().set(args[0]);
59 return true;
60 }
61
62 args.rval().setObject(*js::UncheckedUnwrap(&args[0].toObject()));
63 return JS_WrapValue(cx, args.rval());
64 }
65 // static
66 bool
67 AttachNewConstructorObject(JSContext *aCx, JS::HandleObject aGlobalObject)
68 {
69 // Pushing a JSContext calls ActivateDebugger which calls this function, so
70 // we can't use an AutoJSContext here until JSD is gone.
71 JSAutoCompartment ac(aCx, aGlobalObject);
72 JSFunction *xpcnativewrapper =
73 JS_DefineFunction(aCx, aGlobalObject, "XPCNativeWrapper",
74 XrayWrapperConstructor, 1,
75 JSPROP_READONLY | JSPROP_PERMANENT | JSFUN_STUB_GSOPS | JSFUN_CONSTRUCTOR);
76 if (!xpcnativewrapper) {
77 return false;
78 }
79 JS::RootedObject obj(aCx, JS_GetFunctionObject(xpcnativewrapper));
80 return JS_DefineFunction(aCx, obj, "unwrap", UnwrapNW, 1,
81 JSPROP_READONLY | JSPROP_PERMANENT) != nullptr;
82 }
83
84 } // namespace XPCNativeWrapper
85
86 namespace XPCWrapper {
87
88 JSObject *
89 UnsafeUnwrapSecurityWrapper(JSObject *obj)
90 {
91 if (js::IsProxy(obj)) {
92 return js::UncheckedUnwrap(obj);
93 }
94
95 return obj;
96 }
97
98 } // namespace XPCWrapper

mercurial