1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=2 sw=2 et tw=78: */
3 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is mozilla.org code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * The Mozilla Foundation.
20 : * Portions created by the Initial Developer are Copyright (C) 2005
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Johnny Stenback <jst@mozilla.org> (original author)
25 : * Brendan Eich <brendan@mozilla.org>
26 : * Boris Zbarsky <bzbarsky@mit.edu>
27 : * Blake Kaplan <mrbkap@gmail.com>
28 : *
29 : * Alternatively, the contents of this file may be used under the terms of
30 : * either the GNU General Public License Version 2 or later (the "GPL"), or
31 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
32 : * in which case the provisions of the GPL or the LGPL are applicable instead
33 : * of those above. If you wish to allow use of your version of this file only
34 : * under the terms of either the GPL or the LGPL, and not to allow others to
35 : * use your version of this file under the terms of the MPL, indicate your
36 : * decision by deleting the provisions above and replace them with the notice
37 : * and other provisions required by the GPL or the LGPL. If you do not delete
38 : * the provisions above, a recipient may use your version of this file under
39 : * the terms of any one of the MPL, the GPL or the LGPL.
40 : *
41 : * ***** END LICENSE BLOCK ***** */
42 :
43 : #include "XPCWrapper.h"
44 : #include "AccessCheck.h"
45 : #include "WrapperFactory.h"
46 :
47 : namespace XPCNativeWrapper {
48 :
49 : static inline
50 : JSBool
51 0 : ThrowException(nsresult ex, JSContext *cx)
52 : {
53 0 : XPCThrower::Throw(ex, cx);
54 :
55 0 : return false;
56 : }
57 :
58 : static JSBool
59 0 : UnwrapNW(JSContext *cx, unsigned argc, jsval *vp)
60 : {
61 0 : if (argc != 1) {
62 0 : return ThrowException(NS_ERROR_XPC_NOT_ENOUGH_ARGS, cx);
63 : }
64 :
65 0 : jsval v = JS_ARGV(cx, vp)[0];
66 0 : if (JSVAL_IS_PRIMITIVE(v)) {
67 0 : return ThrowException(NS_ERROR_INVALID_ARG, cx);
68 : }
69 :
70 0 : JSObject *obj = JSVAL_TO_OBJECT(v);
71 0 : if (!js::IsWrapper(obj)) {
72 0 : JS_SET_RVAL(cx, vp, v);
73 0 : return true;
74 : }
75 :
76 0 : if (xpc::WrapperFactory::IsXrayWrapper(obj) &&
77 0 : !xpc::WrapperFactory::IsPartiallyTransparent(obj)) {
78 0 : return JS_GetProperty(cx, obj, "wrappedJSObject", vp);
79 : }
80 :
81 0 : JS_SET_RVAL(cx, vp, v);
82 0 : return true;
83 : }
84 :
85 : static JSBool
86 0 : XrayWrapperConstructor(JSContext *cx, unsigned argc, jsval *vp)
87 : {
88 0 : if (argc == 0) {
89 0 : return ThrowException(NS_ERROR_XPC_NOT_ENOUGH_ARGS, cx);
90 : }
91 :
92 0 : if (JSVAL_IS_PRIMITIVE(vp[2])) {
93 0 : return ThrowException(NS_ERROR_ILLEGAL_VALUE, cx);
94 : }
95 :
96 0 : JSObject *obj = JSVAL_TO_OBJECT(vp[2]);
97 0 : if (!js::IsWrapper(obj)) {
98 0 : *vp = OBJECT_TO_JSVAL(obj);
99 0 : return true;
100 : }
101 :
102 0 : obj = js::UnwrapObject(obj);
103 :
104 0 : *vp = OBJECT_TO_JSVAL(obj);
105 0 : return JS_WrapValue(cx, vp);
106 : }
107 : // static
108 : bool
109 15475 : AttachNewConstructorObject(XPCCallContext &ccx, JSObject *aGlobalObject)
110 : {
111 : JSFunction *xpcnativewrapper =
112 : JS_DefineFunction(ccx, aGlobalObject, "XPCNativeWrapper",
113 : XrayWrapperConstructor, 1,
114 15475 : JSPROP_READONLY | JSPROP_PERMANENT | JSFUN_STUB_GSOPS | JSFUN_CONSTRUCTOR);
115 15475 : if (!xpcnativewrapper) {
116 0 : return false;
117 : }
118 : return JS_DefineFunction(ccx, JS_GetFunctionObject(xpcnativewrapper), "unwrap", UnwrapNW, 1,
119 15475 : JSPROP_READONLY | JSPROP_PERMANENT) != nsnull;
120 : }
121 : }
122 :
123 : namespace XPCWrapper {
124 :
125 : JSObject *
126 469040 : Unwrap(JSContext *cx, JSObject *wrapper, bool stopAtOuter)
127 : {
128 469040 : if (js::IsWrapper(wrapper)) {
129 46 : if (xpc::AccessCheck::isScriptAccessOnly(cx, wrapper))
130 0 : return nsnull;
131 46 : return js::UnwrapObject(wrapper, stopAtOuter);
132 : }
133 :
134 468994 : return nsnull;
135 : }
136 :
137 : JSObject *
138 2515 : UnsafeUnwrapSecurityWrapper(JSObject *obj)
139 : {
140 2515 : if (js::IsProxy(obj)) {
141 2515 : return js::UnwrapObject(obj);
142 : }
143 :
144 0 : return obj;
145 : }
146 :
147 : }
|