Line data Source code
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 : using namespace JS;
15 :
16 : namespace XPCNativeWrapper {
17 :
18 : static inline
19 : bool
20 0 : ThrowException(nsresult ex, JSContext* cx)
21 : {
22 0 : XPCThrower::Throw(ex, cx);
23 :
24 0 : return false;
25 : }
26 :
27 : static bool
28 0 : UnwrapNW(JSContext* cx, unsigned argc, Value* vp)
29 : {
30 0 : JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
31 0 : if (args.length() != 1) {
32 0 : return ThrowException(NS_ERROR_XPC_NOT_ENOUGH_ARGS, cx);
33 : }
34 :
35 0 : JS::RootedValue v(cx, args[0]);
36 0 : if (!v.isObject() || !js::IsCrossCompartmentWrapper(&v.toObject()) ||
37 0 : !WrapperFactory::AllowWaiver(&v.toObject())) {
38 0 : args.rval().set(v);
39 0 : return true;
40 : }
41 :
42 0 : bool ok = xpc::WrapperFactory::WaiveXrayAndWrap(cx, &v);
43 0 : NS_ENSURE_TRUE(ok, false);
44 0 : args.rval().set(v);
45 0 : return true;
46 : }
47 :
48 : static bool
49 0 : XrayWrapperConstructor(JSContext* cx, unsigned argc, Value* vp)
50 : {
51 0 : JS::CallArgs args = CallArgsFromVp(argc, vp);
52 0 : if (args.length() == 0) {
53 0 : return ThrowException(NS_ERROR_XPC_NOT_ENOUGH_ARGS, cx);
54 : }
55 :
56 0 : if (!args[0].isObject()) {
57 0 : args.rval().set(args[0]);
58 0 : return true;
59 : }
60 :
61 0 : args.rval().setObject(*js::UncheckedUnwrap(&args[0].toObject()));
62 0 : return JS_WrapValue(cx, args.rval());
63 : }
64 : // static
65 : bool
66 288 : AttachNewConstructorObject(JSContext* aCx, JS::HandleObject aGlobalObject)
67 : {
68 : // Pushing a JSContext calls ActivateDebugger which calls this function, so
69 : // we can't use an AutoJSContext here until JSD is gone.
70 576 : JSAutoCompartment ac(aCx, aGlobalObject);
71 : JSFunction* xpcnativewrapper =
72 288 : JS_DefineFunction(aCx, aGlobalObject, "XPCNativeWrapper",
73 : XrayWrapperConstructor, 1,
74 288 : JSPROP_READONLY | JSPROP_PERMANENT | JSFUN_STUB_GSOPS | JSFUN_CONSTRUCTOR);
75 288 : if (!xpcnativewrapper) {
76 0 : return false;
77 : }
78 576 : JS::RootedObject obj(aCx, JS_GetFunctionObject(xpcnativewrapper));
79 576 : return JS_DefineFunction(aCx, obj, "unwrap", UnwrapNW, 1,
80 288 : JSPROP_READONLY | JSPROP_PERMANENT) != nullptr;
81 : }
82 :
83 : } // namespace XPCNativeWrapper
84 :
85 : namespace XPCWrapper {
86 :
87 : JSObject*
88 0 : UnsafeUnwrapSecurityWrapper(JSObject* obj)
89 : {
90 0 : if (js::IsProxy(obj)) {
91 0 : return js::UncheckedUnwrap(obj);
92 : }
93 :
94 0 : return obj;
95 : }
96 :
97 : } // namespace XPCWrapper
|