1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sw=4 et tw=80:
3 : *
4 : * ***** BEGIN LICENSE BLOCK *****
5 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 : *
7 : * The contents of this file are subject to the Mozilla Public License Version
8 : * 1.1 (the "License"); you may not use this file except in compliance with
9 : * the License. You may obtain a copy of the License at
10 : * http://www.mozilla.org/MPL/
11 : *
12 : * Software distributed under the License is distributed on an "AS IS" basis,
13 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 : * for the specific language governing rights and limitations under the
15 : * License.
16 : *
17 : * The Original Code is mozilla.org code.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * The Mozilla Foundation.
21 : * Portions created by the Initial Developer are Copyright (C) 2010
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : * Ben Newman <b{enjam,newma}n@mozilla.com> (original author)
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either of the GNU General Public License Version 2 or later (the "GPL"),
29 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #ifndef mozilla_jsipc_ObjectWrapperParent_h__
42 : #define mozilla_jsipc_ObjectWrapperParent_h__
43 :
44 : #include "mozilla/jsipc/PObjectWrapperParent.h"
45 : #include "jsapi.h"
46 : #include "jsclass.h"
47 : #include "nsAutoJSValHolder.h"
48 :
49 : namespace mozilla {
50 : namespace jsipc {
51 :
52 : class ContextWrapperParent;
53 :
54 0 : class OperationChecker {
55 : public:
56 : virtual void CheckOperation(JSContext* cx,
57 : OperationStatus* status) = 0;
58 : };
59 :
60 : class ObjectWrapperParent
61 : : public PObjectWrapperParent
62 : , public OperationChecker
63 0 : {
64 : public:
65 :
66 0 : ObjectWrapperParent()
67 0 : : mObj(NULL)
68 0 : {}
69 :
70 : JSObject* GetJSObject(JSContext* cx) const;
71 :
72 0 : JSObject* GetJSObjectOrNull() const {
73 0 : return mObj;
74 : }
75 :
76 : jsval GetJSVal(JSContext* cx) const {
77 : return OBJECT_TO_JSVAL(GetJSObject(cx));
78 : }
79 :
80 : void CheckOperation(JSContext* cx,
81 : OperationStatus* status);
82 :
83 : static const js::Class sCPOW_JSClass;
84 :
85 : protected:
86 :
87 : void ActorDestroy(ActorDestroyReason why);
88 :
89 : ContextWrapperParent* Manager();
90 :
91 : private:
92 :
93 : mutable JSObject* mObj;
94 :
95 : static JSBool
96 : CPOW_AddProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
97 :
98 : static JSBool
99 : CPOW_DelProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
100 :
101 : static JSBool
102 : CPOW_GetProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
103 :
104 : static JSBool
105 : CPOW_SetProperty(JSContext *cx, JSObject *obj, jsid id, JSBool strict, jsval *vp);
106 :
107 : JSBool NewEnumerateInit(JSContext* cx, jsval* statep, jsid* idp);
108 : JSBool NewEnumerateNext(JSContext* cx, jsval* statep, jsid* idp);
109 : JSBool NewEnumerateDestroy(JSContext* cx, jsval state);
110 : static JSBool
111 : CPOW_NewEnumerate(JSContext *cx, JSObject *obj, JSIterateOp enum_op,
112 : jsval *statep, jsid *idp);
113 :
114 : static JSBool
115 : CPOW_NewResolve(JSContext *cx, JSObject *obj, jsid id, unsigned flags,
116 : JSObject **objp);
117 :
118 : static JSBool
119 : CPOW_Convert(JSContext *cx, JSObject *obj, JSType type, jsval *vp);
120 :
121 : static void
122 : CPOW_Finalize(JSContext* cx, JSObject* obj);
123 :
124 : static JSBool
125 : CPOW_Call(JSContext* cx, unsigned argc, jsval* vp);
126 :
127 : static JSBool
128 : CPOW_Construct(JSContext *cx, unsigned argc, jsval *vp);
129 :
130 : static JSBool
131 : CPOW_HasInstance(JSContext *cx, JSObject *obj, const jsval *v, JSBool *bp);
132 :
133 : static JSBool
134 : CPOW_Equality(JSContext *cx, JSObject *obj, const jsval *v, JSBool *bp);
135 :
136 : static bool jsval_to_JSVariant(JSContext* cx, jsval from, JSVariant* to);
137 : static bool jsval_from_JSVariant(JSContext* cx, const JSVariant& from,
138 : jsval* to);
139 : static bool
140 : JSObject_to_PObjectWrapperParent(JSObject* from, PObjectWrapperParent** to);
141 : static bool
142 : JSObject_from_PObjectWrapperParent(JSContext* cx,
143 : const PObjectWrapperParent* from,
144 : JSObject** to);
145 : static bool
146 : jsval_from_PObjectWrapperParent(JSContext* cx,
147 : const PObjectWrapperParent* from,
148 : jsval* to);
149 : };
150 :
151 : template <class StatusOwnerPolicy>
152 : class AutoCheckOperationBase
153 : : public StatusOwnerPolicy
154 : {
155 : JSContext* const mContext;
156 : OperationChecker* const mChecker;
157 :
158 : protected:
159 :
160 0 : AutoCheckOperationBase(JSContext* cx,
161 : OperationChecker* checker)
162 : : mContext(cx)
163 0 : , mChecker(checker)
164 0 : {}
165 :
166 0 : virtual ~AutoCheckOperationBase() {
167 0 : mChecker->CheckOperation(mContext, StatusOwnerPolicy::StatusPtr());
168 0 : }
169 :
170 : public:
171 :
172 0 : bool Ok() {
173 : return (StatusOwnerPolicy::StatusPtr()->type() == OperationStatus::TJSBool &&
174 0 : StatusOwnerPolicy::StatusPtr()->get_JSBool());
175 : }
176 : };
177 :
178 : }}
179 :
180 : #endif
|