Bug 1321879 part 1. Add an API on GlobalObject to get the caller type, for use in things like WebIDL constructors. r=bholley
authorBoris Zbarsky <bzbarsky@mit.edu>
Tue, 06 Dec 2016 23:41:52 -1000
changeset 325382 29e59ce35d703ee271be1bb1c40023d3d0246bae
parent 325381 2d9cbc261fc0e4f09c0d7c240640c4e57bdd6d87
child 325383 c0c3a434a0b2a034394e97ae1440ca17e0ba171f
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewersbholley
bugs1321879
milestone53.0a1
Bug 1321879 part 1. Add an API on GlobalObject to get the caller type, for use in things like WebIDL constructors. r=bholley
dom/bindings/BindingDeclarations.h
dom/bindings/BindingUtils.cpp
--- a/dom/bindings/BindingDeclarations.h
+++ b/dom/bindings/BindingDeclarations.h
@@ -79,16 +79,18 @@ struct AllOwningUnionBase {
 };
 
 
 struct EnumEntry {
   const char* value;
   size_t length;
 };
 
+enum class CallerType : uint32_t;
+
 class MOZ_STACK_CLASS GlobalObject
 {
 public:
   GlobalObject(JSContext* aCx, JSObject* aObject);
 
   JSObject* Get() const
   {
     return mGlobalJSObject;
@@ -108,16 +110,20 @@ public:
   {
     return !Get();
   }
 
   // It returns the subjectPrincipal if called on the main-thread, otherwise
   // a nullptr is returned.
   nsIPrincipal* GetSubjectPrincipal() const;
 
+  // Get the caller type.  Note that this needs to be called before anyone has
+  // had a chance to mess with the JSContext.
+  dom::CallerType CallerType() const;
+
 protected:
   JS::Rooted<JSObject*> mGlobalJSObject;
   JSContext* mCx;
   mutable nsISupports* MOZ_UNSAFE_REF("Valid because GlobalObject is a stack "
                                       "class, and mGlobalObject points to the "
                                       "global, so it won't be destroyed as long "
                                       "as GlobalObject lives on the stack") mGlobalObject;
 };
--- a/dom/bindings/BindingUtils.cpp
+++ b/dom/bindings/BindingUtils.cpp
@@ -2310,16 +2310,23 @@ GlobalObject::GetSubjectPrincipal() cons
   }
 
   JSCompartment* compartment = js::GetContextCompartment(mCx);
   MOZ_ASSERT(compartment);
   JSPrincipals* principals = JS_GetCompartmentPrincipals(compartment);
   return nsJSPrincipals::get(principals);
 }
 
+CallerType
+GlobalObject::CallerType() const
+{
+  return nsContentUtils::ThreadsafeIsSystemCaller(mCx) ?
+    dom::CallerType::System : dom::CallerType::NonSystem;
+}
+
 static bool
 CallOrdinaryHasInstance(JSContext* cx, JS::CallArgs& args)
 {
     JS::Rooted<JSObject*> thisObj(cx, &args.thisv().toObject());
     bool isInstance;
     if (!JS::OrdinaryHasInstance(cx, thisObj, args.get(0), &isInstance)) {
       return false;
     }