1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Communicator client code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * David Hyatt <hyatt@netscape.com> (Original Author)
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either of the GNU General Public License Version 2 or later (the "GPL"),
27 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsIAtom.h"
40 : #include "nsString.h"
41 : #include "jsapi.h"
42 : #include "nsIContent.h"
43 : #include "nsIDocument.h"
44 : #include "nsString.h"
45 : #include "nsXBLProtoImplProperty.h"
46 : #include "nsUnicharUtils.h"
47 : #include "nsReadableUtils.h"
48 : #include "nsIScriptContext.h"
49 : #include "nsIScriptGlobalObject.h"
50 : #include "nsContentUtils.h"
51 : #include "nsXBLPrototypeBinding.h"
52 : #include "nsXBLSerialize.h"
53 :
54 0 : nsXBLProtoImplProperty::nsXBLProtoImplProperty(const PRUnichar* aName,
55 : const PRUnichar* aGetter,
56 : const PRUnichar* aSetter,
57 : const PRUnichar* aReadOnly) :
58 : nsXBLProtoImplMember(aName),
59 : mGetterText(nsnull),
60 : mSetterText(nsnull),
61 : mJSAttributes(JSPROP_ENUMERATE)
62 : #ifdef DEBUG
63 0 : , mIsCompiled(false)
64 : #endif
65 : {
66 0 : MOZ_COUNT_CTOR(nsXBLProtoImplProperty);
67 :
68 0 : if (aReadOnly) {
69 0 : nsAutoString readOnly; readOnly.Assign(*aReadOnly);
70 0 : if (readOnly.LowerCaseEqualsLiteral("true"))
71 0 : mJSAttributes |= JSPROP_READONLY;
72 : }
73 :
74 0 : if (aGetter)
75 0 : AppendGetterText(nsDependentString(aGetter));
76 0 : if (aSetter)
77 0 : AppendSetterText(nsDependentString(aSetter));
78 0 : }
79 :
80 0 : nsXBLProtoImplProperty::nsXBLProtoImplProperty(const PRUnichar* aName,
81 : const bool aIsReadOnly)
82 : : nsXBLProtoImplMember(aName),
83 : mGetterText(nsnull),
84 : mSetterText(nsnull),
85 : mJSAttributes(JSPROP_ENUMERATE)
86 : #ifdef DEBUG
87 0 : , mIsCompiled(false)
88 : #endif
89 : {
90 0 : MOZ_COUNT_CTOR(nsXBLProtoImplProperty);
91 :
92 0 : if (aIsReadOnly)
93 0 : mJSAttributes |= JSPROP_READONLY;
94 0 : }
95 :
96 0 : nsXBLProtoImplProperty::~nsXBLProtoImplProperty()
97 : {
98 0 : MOZ_COUNT_DTOR(nsXBLProtoImplProperty);
99 :
100 0 : if (!(mJSAttributes & JSPROP_GETTER)) {
101 0 : delete mGetterText;
102 : }
103 :
104 0 : if (!(mJSAttributes & JSPROP_SETTER)) {
105 0 : delete mSetterText;
106 : }
107 0 : }
108 :
109 : void
110 0 : nsXBLProtoImplProperty::AppendGetterText(const nsAString& aText)
111 : {
112 0 : NS_PRECONDITION(!mIsCompiled,
113 : "Must not be compiled when accessing getter text");
114 0 : if (!mGetterText) {
115 0 : mGetterText = new nsXBLTextWithLineNumber();
116 0 : if (!mGetterText)
117 0 : return;
118 : }
119 :
120 0 : mGetterText->AppendText(aText);
121 : }
122 :
123 : void
124 0 : nsXBLProtoImplProperty::AppendSetterText(const nsAString& aText)
125 : {
126 0 : NS_PRECONDITION(!mIsCompiled,
127 : "Must not be compiled when accessing setter text");
128 0 : if (!mSetterText) {
129 0 : mSetterText = new nsXBLTextWithLineNumber();
130 0 : if (!mSetterText)
131 0 : return;
132 : }
133 :
134 0 : mSetterText->AppendText(aText);
135 : }
136 :
137 : void
138 0 : nsXBLProtoImplProperty::SetGetterLineNumber(PRUint32 aLineNumber)
139 : {
140 0 : NS_PRECONDITION(!mIsCompiled,
141 : "Must not be compiled when accessing getter text");
142 0 : if (!mGetterText) {
143 0 : mGetterText = new nsXBLTextWithLineNumber();
144 0 : if (!mGetterText)
145 0 : return;
146 : }
147 :
148 0 : mGetterText->SetLineNumber(aLineNumber);
149 : }
150 :
151 : void
152 0 : nsXBLProtoImplProperty::SetSetterLineNumber(PRUint32 aLineNumber)
153 : {
154 0 : NS_PRECONDITION(!mIsCompiled,
155 : "Must not be compiled when accessing setter text");
156 0 : if (!mSetterText) {
157 0 : mSetterText = new nsXBLTextWithLineNumber();
158 0 : if (!mSetterText)
159 0 : return;
160 : }
161 :
162 0 : mSetterText->SetLineNumber(aLineNumber);
163 : }
164 :
165 : const char* gPropertyArgs[] = { "val" };
166 :
167 : nsresult
168 0 : nsXBLProtoImplProperty::InstallMember(nsIScriptContext* aContext,
169 : nsIContent* aBoundElement,
170 : void* aScriptObject,
171 : void* aTargetClassObject,
172 : const nsCString& aClassStr)
173 : {
174 0 : NS_PRECONDITION(mIsCompiled,
175 : "Should not be installing an uncompiled property");
176 0 : JSContext* cx = aContext->GetNativeContext();
177 :
178 0 : nsIDocument *ownerDoc = aBoundElement->OwnerDoc();
179 : nsIScriptGlobalObject *sgo;
180 :
181 0 : if (!(sgo = ownerDoc->GetScopeObject())) {
182 0 : return NS_ERROR_UNEXPECTED;
183 : }
184 :
185 0 : JSObject * scriptObject = (JSObject *) aScriptObject;
186 0 : NS_ASSERTION(scriptObject, "uh-oh, script Object should NOT be null or bad things will happen");
187 0 : if (!scriptObject)
188 0 : return NS_ERROR_FAILURE;
189 :
190 0 : JSObject * targetClassObject = (JSObject *) aTargetClassObject;
191 0 : JSObject * globalObject = sgo->GetGlobalJSObject();
192 :
193 : // now we want to reevaluate our property using aContext and the script object for this window...
194 0 : if ((mJSGetterObject || mJSSetterObject) && targetClassObject) {
195 0 : JSObject * getter = nsnull;
196 0 : JSAutoRequest ar(cx);
197 0 : JSAutoEnterCompartment ac;
198 :
199 0 : if (!ac.enter(cx, globalObject))
200 0 : return NS_ERROR_UNEXPECTED;
201 :
202 0 : if (mJSGetterObject)
203 0 : if (!(getter = ::JS_CloneFunctionObject(cx, mJSGetterObject, globalObject)))
204 0 : return NS_ERROR_OUT_OF_MEMORY;
205 :
206 0 : JSObject * setter = nsnull;
207 0 : if (mJSSetterObject)
208 0 : if (!(setter = ::JS_CloneFunctionObject(cx, mJSSetterObject, globalObject)))
209 0 : return NS_ERROR_OUT_OF_MEMORY;
210 :
211 0 : nsDependentString name(mName);
212 0 : if (!::JS_DefineUCProperty(cx, targetClassObject,
213 : reinterpret_cast<const jschar*>(mName),
214 : name.Length(), JSVAL_VOID,
215 : JS_DATA_TO_FUNC_PTR(JSPropertyOp, getter),
216 : JS_DATA_TO_FUNC_PTR(JSStrictPropertyOp, setter),
217 0 : mJSAttributes))
218 0 : return NS_ERROR_OUT_OF_MEMORY;
219 : }
220 0 : return NS_OK;
221 : }
222 :
223 : nsresult
224 0 : nsXBLProtoImplProperty::CompileMember(nsIScriptContext* aContext, const nsCString& aClassStr,
225 : JSObject* aClassObject)
226 : {
227 0 : NS_PRECONDITION(!mIsCompiled,
228 : "Trying to compile an already-compiled property");
229 0 : NS_PRECONDITION(aClassObject,
230 : "Must have class object to compile");
231 :
232 0 : if (!mName)
233 0 : return NS_ERROR_FAILURE; // Without a valid name, we can't install the member.
234 :
235 : // We have a property.
236 0 : nsresult rv = NS_OK;
237 :
238 0 : nsCAutoString functionUri;
239 0 : if (mGetterText || mSetterText) {
240 0 : functionUri = aClassStr;
241 0 : PRInt32 hash = functionUri.RFindChar('#');
242 0 : if (hash != kNotFound) {
243 0 : functionUri.Truncate(hash);
244 : }
245 : }
246 :
247 0 : bool deletedGetter = false;
248 0 : if (mGetterText && mGetterText->GetText()) {
249 0 : nsDependentString getter(mGetterText->GetText());
250 0 : if (!getter.IsEmpty()) {
251 : // Compile into a temp object so we don't wipe out mGetterText
252 0 : JSObject* getterObject = nsnull;
253 : rv = aContext->CompileFunction(aClassObject,
254 0 : NS_LITERAL_CSTRING("get_") +
255 0 : NS_ConvertUTF16toUTF8(mName),
256 : 0,
257 : nsnull,
258 : getter,
259 : functionUri.get(),
260 : mGetterText->GetLineNumber(),
261 : JSVERSION_LATEST,
262 : true,
263 0 : &getterObject);
264 :
265 : // Make sure we free mGetterText here before setting mJSGetterObject, since
266 : // that'll overwrite mGetterText
267 0 : delete mGetterText;
268 0 : deletedGetter = true;
269 0 : mJSGetterObject = getterObject;
270 :
271 0 : if (mJSGetterObject && NS_SUCCEEDED(rv)) {
272 0 : mJSAttributes |= JSPROP_GETTER | JSPROP_SHARED;
273 : }
274 0 : if (NS_FAILED(rv)) {
275 0 : mJSGetterObject = nsnull;
276 0 : mJSAttributes &= ~JSPROP_GETTER;
277 : /*chaining to return failure*/
278 : }
279 : }
280 : } // if getter is not empty
281 :
282 0 : if (!deletedGetter) { // Empty getter
283 0 : delete mGetterText;
284 0 : mJSGetterObject = nsnull;
285 : }
286 :
287 0 : if (NS_FAILED(rv)) {
288 : // We failed to compile our getter. So either we've set it to null, or
289 : // it's still set to the text object. In either case, it's safe to return
290 : // the error here, since then we'll be cleaned up as uncompiled and that
291 : // will be ok. Going on and compiling the setter and _then_ returning an
292 : // error, on the other hand, will try to clean up a compiled setter as
293 : // uncompiled and crash.
294 0 : return rv;
295 : }
296 :
297 0 : bool deletedSetter = false;
298 0 : if (mSetterText && mSetterText->GetText()) {
299 0 : nsDependentString setter(mSetterText->GetText());
300 0 : if (!setter.IsEmpty()) {
301 : // Compile into a temp object so we don't wipe out mSetterText
302 0 : JSObject* setterObject = nsnull;
303 : rv = aContext->CompileFunction(aClassObject,
304 0 : NS_LITERAL_CSTRING("set_") +
305 0 : NS_ConvertUTF16toUTF8(mName),
306 : 1,
307 : gPropertyArgs,
308 : setter,
309 : functionUri.get(),
310 : mSetterText->GetLineNumber(),
311 : JSVERSION_LATEST,
312 : true,
313 0 : &setterObject);
314 :
315 : // Make sure we free mSetterText here before setting mJSGetterObject, since
316 : // that'll overwrite mSetterText
317 0 : delete mSetterText;
318 0 : deletedSetter = true;
319 0 : mJSSetterObject = setterObject;
320 :
321 0 : if (mJSSetterObject && NS_SUCCEEDED(rv)) {
322 0 : mJSAttributes |= JSPROP_SETTER | JSPROP_SHARED;
323 : }
324 0 : if (NS_FAILED(rv)) {
325 0 : mJSSetterObject = nsnull;
326 0 : mJSAttributes &= ~JSPROP_SETTER;
327 : /*chaining to return failure*/
328 : }
329 : }
330 : } // if setter wasn't empty....
331 :
332 0 : if (!deletedSetter) { // Empty setter
333 0 : delete mSetterText;
334 0 : mJSSetterObject = nsnull;
335 : }
336 :
337 : #ifdef DEBUG
338 0 : mIsCompiled = NS_SUCCEEDED(rv);
339 : #endif
340 :
341 0 : return rv;
342 : }
343 :
344 : void
345 0 : nsXBLProtoImplProperty::Trace(TraceCallback aCallback, void *aClosure) const
346 : {
347 0 : if (mJSAttributes & JSPROP_GETTER) {
348 : aCallback(nsIProgrammingLanguage::JAVASCRIPT, mJSGetterObject,
349 0 : "mJSGetterObject", aClosure);
350 : }
351 :
352 0 : if (mJSAttributes & JSPROP_SETTER) {
353 : aCallback(nsIProgrammingLanguage::JAVASCRIPT, mJSSetterObject,
354 0 : "mJSSetterObject", aClosure);
355 : }
356 0 : }
357 :
358 : nsresult
359 0 : nsXBLProtoImplProperty::Read(nsIScriptContext* aContext,
360 : nsIObjectInputStream* aStream,
361 : XBLBindingSerializeDetails aType)
362 : {
363 0 : if (aType == XBLBinding_Serialize_GetterProperty ||
364 : aType == XBLBinding_Serialize_GetterSetterProperty) {
365 : JSObject* getterObject;
366 0 : nsresult rv = XBL_DeserializeFunction(aContext, aStream, &getterObject);
367 0 : NS_ENSURE_SUCCESS(rv, rv);
368 :
369 0 : mJSGetterObject = getterObject;
370 0 : mJSAttributes |= JSPROP_GETTER | JSPROP_SHARED;
371 : }
372 :
373 0 : if (aType == XBLBinding_Serialize_SetterProperty ||
374 : aType == XBLBinding_Serialize_GetterSetterProperty) {
375 : JSObject* setterObject;
376 0 : nsresult rv = XBL_DeserializeFunction(aContext, aStream, &setterObject);
377 0 : NS_ENSURE_SUCCESS(rv, rv);
378 :
379 0 : mJSSetterObject = setterObject;
380 0 : mJSAttributes |= JSPROP_SETTER | JSPROP_SHARED;
381 : }
382 :
383 : #ifdef DEBUG
384 0 : mIsCompiled = true;
385 : #endif
386 :
387 0 : return NS_OK;
388 : }
389 :
390 : nsresult
391 0 : nsXBLProtoImplProperty::Write(nsIScriptContext* aContext,
392 : nsIObjectOutputStream* aStream)
393 : {
394 : XBLBindingSerializeDetails type;
395 :
396 0 : if (mJSAttributes & JSPROP_GETTER) {
397 : type = mJSAttributes & JSPROP_SETTER ?
398 : XBLBinding_Serialize_GetterSetterProperty :
399 0 : XBLBinding_Serialize_GetterProperty;
400 : }
401 : else {
402 0 : type = XBLBinding_Serialize_SetterProperty;
403 : }
404 :
405 0 : if (mJSAttributes & JSPROP_READONLY) {
406 0 : type |= XBLBinding_Serialize_ReadOnly;
407 : }
408 :
409 0 : nsresult rv = aStream->Write8(type);
410 0 : NS_ENSURE_SUCCESS(rv, rv);
411 0 : rv = aStream->WriteWStringZ(mName);
412 0 : NS_ENSURE_SUCCESS(rv, rv);
413 :
414 0 : if (mJSAttributes & JSPROP_GETTER) {
415 0 : rv = XBL_SerializeFunction(aContext, aStream, mJSGetterObject);
416 0 : NS_ENSURE_SUCCESS(rv, rv);
417 : }
418 :
419 0 : if (mJSAttributes & JSPROP_SETTER) {
420 0 : rv = XBL_SerializeFunction(aContext, aStream, mJSSetterObject);
421 0 : NS_ENSURE_SUCCESS(rv, rv);
422 : }
423 :
424 0 : return NS_OK;
425 : }
|