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 "nsIAtom.h"
8 #include "nsString.h"
9 #include "jsapi.h"
10 #include "nsIContent.h"
11 #include "nsXBLProtoImplProperty.h"
12 #include "nsUnicharUtils.h"
13 #include "nsReadableUtils.h"
14 #include "nsJSUtils.h"
15 #include "nsXBLPrototypeBinding.h"
16 #include "nsXBLSerialize.h"
17 #include "xpcpublic.h"
18 
19 using namespace mozilla;
20 using namespace mozilla::dom;
21 
nsXBLProtoImplProperty(const char16_t * aName,const char16_t * aGetter,const char16_t * aSetter,const char16_t * aReadOnly,uint32_t aLineNumber)22 nsXBLProtoImplProperty::nsXBLProtoImplProperty(const char16_t* aName,
23                                                const char16_t* aGetter,
24                                                const char16_t* aSetter,
25                                                const char16_t* aReadOnly,
26                                                uint32_t aLineNumber) :
27   nsXBLProtoImplMember(aName),
28   mJSAttributes(JSPROP_ENUMERATE)
29 #ifdef DEBUG
30   , mIsCompiled(false)
31 #endif
32 {
33   MOZ_COUNT_CTOR(nsXBLProtoImplProperty);
34 
35   if (aReadOnly) {
36     nsAutoString readOnly; readOnly.Assign(*aReadOnly);
37     if (readOnly.LowerCaseEqualsLiteral("true"))
38       mJSAttributes |= JSPROP_READONLY;
39   }
40 
41   if (aGetter) {
42     AppendGetterText(nsDependentString(aGetter));
43     SetGetterLineNumber(aLineNumber);
44   }
45   if (aSetter) {
46     AppendSetterText(nsDependentString(aSetter));
47     SetSetterLineNumber(aLineNumber);
48   }
49 }
50 
nsXBLProtoImplProperty(const char16_t * aName,const bool aIsReadOnly)51 nsXBLProtoImplProperty::nsXBLProtoImplProperty(const char16_t* aName,
52                                                const bool aIsReadOnly)
53   : nsXBLProtoImplMember(aName),
54     mJSAttributes(JSPROP_ENUMERATE)
55 #ifdef DEBUG
56   , mIsCompiled(false)
57 #endif
58 {
59   MOZ_COUNT_CTOR(nsXBLProtoImplProperty);
60 
61   if (aIsReadOnly)
62     mJSAttributes |= JSPROP_READONLY;
63 }
64 
~nsXBLProtoImplProperty()65 nsXBLProtoImplProperty::~nsXBLProtoImplProperty()
66 {
67   MOZ_COUNT_DTOR(nsXBLProtoImplProperty);
68 
69   if (!mGetter.IsCompiled()) {
70     delete mGetter.GetUncompiled();
71   }
72 
73   if (!mSetter.IsCompiled()) {
74     delete mSetter.GetUncompiled();
75   }
76 }
77 
EnsureUncompiledText(PropertyOp & aPropertyOp)78 void nsXBLProtoImplProperty::EnsureUncompiledText(PropertyOp& aPropertyOp)
79 {
80   if (!aPropertyOp.GetUncompiled()) {
81     nsXBLTextWithLineNumber* text = new nsXBLTextWithLineNumber();
82     aPropertyOp.SetUncompiled(text);
83   }
84 }
85 
86 void
AppendGetterText(const nsAString & aText)87 nsXBLProtoImplProperty::AppendGetterText(const nsAString& aText)
88 {
89   NS_PRECONDITION(!mIsCompiled,
90                   "Must not be compiled when accessing getter text");
91   EnsureUncompiledText(mGetter);
92   mGetter.GetUncompiled()->AppendText(aText);
93 }
94 
95 void
AppendSetterText(const nsAString & aText)96 nsXBLProtoImplProperty::AppendSetterText(const nsAString& aText)
97 {
98   NS_PRECONDITION(!mIsCompiled,
99                   "Must not be compiled when accessing setter text");
100   EnsureUncompiledText(mSetter);
101   mSetter.GetUncompiled()->AppendText(aText);
102 }
103 
104 void
SetGetterLineNumber(uint32_t aLineNumber)105 nsXBLProtoImplProperty::SetGetterLineNumber(uint32_t aLineNumber)
106 {
107   NS_PRECONDITION(!mIsCompiled,
108                   "Must not be compiled when accessing getter text");
109   EnsureUncompiledText(mGetter);
110   mGetter.GetUncompiled()->SetLineNumber(aLineNumber);
111 }
112 
113 void
SetSetterLineNumber(uint32_t aLineNumber)114 nsXBLProtoImplProperty::SetSetterLineNumber(uint32_t aLineNumber)
115 {
116   NS_PRECONDITION(!mIsCompiled,
117                   "Must not be compiled when accessing setter text");
118   EnsureUncompiledText(mSetter);
119   mSetter.GetUncompiled()->SetLineNumber(aLineNumber);
120 }
121 
122 const char* gPropertyArgs[] = { "val" };
123 
124 nsresult
InstallMember(JSContext * aCx,JS::Handle<JSObject * > aTargetClassObject)125 nsXBLProtoImplProperty::InstallMember(JSContext *aCx,
126                                       JS::Handle<JSObject*> aTargetClassObject)
127 {
128   NS_PRECONDITION(mIsCompiled,
129                   "Should not be installing an uncompiled property");
130   MOZ_ASSERT(mGetter.IsCompiled() && mSetter.IsCompiled());
131   MOZ_ASSERT(js::IsObjectInContextCompartment(aTargetClassObject, aCx));
132 
133 #ifdef DEBUG
134   {
135     JS::Rooted<JSObject*> globalObject(aCx, JS_GetGlobalForObject(aCx, aTargetClassObject));
136     MOZ_ASSERT(xpc::IsInContentXBLScope(globalObject) ||
137                xpc::IsInAddonScope(globalObject) ||
138                globalObject == xpc::GetXBLScope(aCx, globalObject));
139     MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx) == globalObject);
140   }
141 #endif
142 
143   JS::Rooted<JSObject*> getter(aCx, mGetter.GetJSFunction());
144   JS::Rooted<JSObject*> setter(aCx, mSetter.GetJSFunction());
145   if (getter || setter) {
146     if (getter) {
147       if (!(getter = JS::CloneFunctionObject(aCx, getter)))
148         return NS_ERROR_OUT_OF_MEMORY;
149     }
150 
151     if (setter) {
152       if (!(setter = JS::CloneFunctionObject(aCx, setter)))
153         return NS_ERROR_OUT_OF_MEMORY;
154     }
155 
156     nsDependentString name(mName);
157     if (!::JS_DefineUCProperty(aCx, aTargetClassObject,
158                                static_cast<const char16_t*>(mName),
159                                name.Length(), JS::UndefinedHandleValue, mJSAttributes,
160                                JS_DATA_TO_FUNC_PTR(JSNative, getter.get()),
161                                JS_DATA_TO_FUNC_PTR(JSNative, setter.get())))
162       return NS_ERROR_OUT_OF_MEMORY;
163   }
164   return NS_OK;
165 }
166 
167 nsresult
CompileMember(AutoJSAPI & jsapi,const nsString & aClassStr,JS::Handle<JSObject * > aClassObject)168 nsXBLProtoImplProperty::CompileMember(AutoJSAPI& jsapi, const nsString& aClassStr,
169                                       JS::Handle<JSObject*> aClassObject)
170 {
171   AssertInCompilationScope();
172   NS_PRECONDITION(!mIsCompiled,
173                   "Trying to compile an already-compiled property");
174   NS_PRECONDITION(aClassObject,
175                   "Must have class object to compile");
176   MOZ_ASSERT(!mGetter.IsCompiled() && !mSetter.IsCompiled());
177   JSContext *cx = jsapi.cx();
178 
179   if (!mName)
180     return NS_ERROR_FAILURE; // Without a valid name, we can't install the member.
181 
182   // We have a property.
183   nsresult rv = NS_OK;
184 
185   nsAutoCString functionUri;
186   if (mGetter.GetUncompiled() || mSetter.GetUncompiled()) {
187     functionUri = NS_ConvertUTF16toUTF8(aClassStr);
188     int32_t hash = functionUri.RFindChar('#');
189     if (hash != kNotFound) {
190       functionUri.Truncate(hash);
191     }
192   }
193 
194   bool deletedGetter = false;
195   nsXBLTextWithLineNumber *getterText = mGetter.GetUncompiled();
196   if (getterText && getterText->GetText()) {
197     nsDependentString getter(getterText->GetText());
198     if (!getter.IsEmpty()) {
199       JSAutoCompartment ac(cx, aClassObject);
200       JS::CompileOptions options(cx);
201       options.setFileAndLine(functionUri.get(), getterText->GetLineNumber())
202              .setVersion(JSVERSION_LATEST);
203       nsCString name = NS_LITERAL_CSTRING("get_") + NS_ConvertUTF16toUTF8(mName);
204       JS::Rooted<JSObject*> getterObject(cx);
205       JS::AutoObjectVector emptyVector(cx);
206       rv = nsJSUtils::CompileFunction(jsapi, emptyVector, options, name, 0,
207                                       nullptr, getter, getterObject.address());
208 
209       delete getterText;
210       deletedGetter = true;
211 
212       mGetter.SetJSFunction(getterObject);
213 
214       if (mGetter.GetJSFunction() && NS_SUCCEEDED(rv)) {
215         mJSAttributes |= JSPROP_GETTER | JSPROP_SHARED;
216       }
217       if (NS_FAILED(rv)) {
218         mGetter.SetJSFunction(nullptr);
219         mJSAttributes &= ~JSPROP_GETTER;
220         /*chaining to return failure*/
221       }
222     }
223   } // if getter is not empty
224 
225   if (!deletedGetter) {  // Empty getter
226     delete getterText;
227     mGetter.SetJSFunction(nullptr);
228   }
229 
230   if (NS_FAILED(rv)) {
231     // We failed to compile our getter.  So either we've set it to null, or
232     // it's still set to the text object.  In either case, it's safe to return
233     // the error here, since then we'll be cleaned up as uncompiled and that
234     // will be ok.  Going on and compiling the setter and _then_ returning an
235     // error, on the other hand, will try to clean up a compiled setter as
236     // uncompiled and crash.
237     return rv;
238   }
239 
240   bool deletedSetter = false;
241   nsXBLTextWithLineNumber *setterText = mSetter.GetUncompiled();
242   if (setterText && setterText->GetText()) {
243     nsDependentString setter(setterText->GetText());
244     if (!setter.IsEmpty()) {
245       JSAutoCompartment ac(cx, aClassObject);
246       JS::CompileOptions options(cx);
247       options.setFileAndLine(functionUri.get(), setterText->GetLineNumber())
248              .setVersion(JSVERSION_LATEST);
249       nsCString name = NS_LITERAL_CSTRING("set_") + NS_ConvertUTF16toUTF8(mName);
250       JS::Rooted<JSObject*> setterObject(cx);
251       JS::AutoObjectVector emptyVector(cx);
252       rv = nsJSUtils::CompileFunction(jsapi, emptyVector, options, name, 1,
253                                       gPropertyArgs, setter,
254                                       setterObject.address());
255 
256       delete setterText;
257       deletedSetter = true;
258       mSetter.SetJSFunction(setterObject);
259 
260       if (mSetter.GetJSFunction() && NS_SUCCEEDED(rv)) {
261         mJSAttributes |= JSPROP_SETTER | JSPROP_SHARED;
262       }
263       if (NS_FAILED(rv)) {
264         mSetter.SetJSFunction(nullptr);
265         mJSAttributes &= ~JSPROP_SETTER;
266         /*chaining to return failure*/
267       }
268     }
269   } // if setter wasn't empty....
270 
271   if (!deletedSetter) {  // Empty setter
272     delete setterText;
273     mSetter.SetJSFunction(nullptr);
274   }
275 
276 #ifdef DEBUG
277   mIsCompiled = NS_SUCCEEDED(rv);
278 #endif
279 
280   return rv;
281 }
282 
283 void
Trace(const TraceCallbacks & aCallbacks,void * aClosure)284 nsXBLProtoImplProperty::Trace(const TraceCallbacks& aCallbacks, void *aClosure)
285 {
286   if (mJSAttributes & JSPROP_GETTER) {
287     aCallbacks.Trace(&mGetter.AsHeapObject(), "mGetter", aClosure);
288   }
289 
290   if (mJSAttributes & JSPROP_SETTER) {
291     aCallbacks.Trace(&mSetter.AsHeapObject(), "mSetter", aClosure);
292   }
293 }
294 
295 nsresult
Read(nsIObjectInputStream * aStream,XBLBindingSerializeDetails aType)296 nsXBLProtoImplProperty::Read(nsIObjectInputStream* aStream,
297                              XBLBindingSerializeDetails aType)
298 {
299   AssertInCompilationScope();
300   MOZ_ASSERT(!mIsCompiled);
301   MOZ_ASSERT(!mGetter.GetUncompiled() && !mSetter.GetUncompiled());
302 
303   AutoJSContext cx;
304   JS::Rooted<JSObject*> getterObject(cx);
305   if (aType == XBLBinding_Serialize_GetterProperty ||
306       aType == XBLBinding_Serialize_GetterSetterProperty) {
307     nsresult rv = XBL_DeserializeFunction(aStream, &getterObject);
308     NS_ENSURE_SUCCESS(rv, rv);
309 
310     mJSAttributes |= JSPROP_GETTER | JSPROP_SHARED;
311   }
312   mGetter.SetJSFunction(getterObject);
313 
314   JS::Rooted<JSObject*> setterObject(cx);
315   if (aType == XBLBinding_Serialize_SetterProperty ||
316       aType == XBLBinding_Serialize_GetterSetterProperty) {
317     nsresult rv = XBL_DeserializeFunction(aStream, &setterObject);
318     NS_ENSURE_SUCCESS(rv, rv);
319 
320     mJSAttributes |= JSPROP_SETTER | JSPROP_SHARED;
321   }
322   mSetter.SetJSFunction(setterObject);
323 
324 #ifdef DEBUG
325   mIsCompiled = true;
326 #endif
327 
328   return NS_OK;
329 }
330 
331 nsresult
Write(nsIObjectOutputStream * aStream)332 nsXBLProtoImplProperty::Write(nsIObjectOutputStream* aStream)
333 {
334   AssertInCompilationScope();
335   XBLBindingSerializeDetails type;
336 
337   if (mJSAttributes & JSPROP_GETTER) {
338     type = mJSAttributes & JSPROP_SETTER ?
339            XBLBinding_Serialize_GetterSetterProperty :
340            XBLBinding_Serialize_GetterProperty;
341   }
342   else {
343     type = XBLBinding_Serialize_SetterProperty;
344   }
345 
346   if (mJSAttributes & JSPROP_READONLY) {
347     type |= XBLBinding_Serialize_ReadOnly;
348   }
349 
350   nsresult rv = aStream->Write8(type);
351   NS_ENSURE_SUCCESS(rv, rv);
352   rv = aStream->WriteWStringZ(mName);
353   NS_ENSURE_SUCCESS(rv, rv);
354 
355   MOZ_ASSERT_IF(mJSAttributes & (JSPROP_GETTER | JSPROP_SETTER), mIsCompiled);
356 
357   if (mJSAttributes & JSPROP_GETTER) {
358     JS::Rooted<JSObject*> function(RootingCx(), mGetter.GetJSFunction());
359     rv = XBL_SerializeFunction(aStream, function);
360     NS_ENSURE_SUCCESS(rv, rv);
361   }
362 
363   if (mJSAttributes & JSPROP_SETTER) {
364     JS::Rooted<JSObject*> function(RootingCx(), mSetter.GetJSFunction());
365     rv = XBL_SerializeFunction(aStream, function);
366     NS_ENSURE_SUCCESS(rv, rv);
367   }
368 
369   return NS_OK;
370 }
371