Bug 1306222 - Introducing GlobalObject::GetSubjectPrincipal, r=ehsan
authorAndrea Marchesini <amarchesini@mozilla.com>
Fri, 30 Sep 2016 19:54:13 +0200
changeset 316084 069d41a294b8179a226e359de8c9936e003f5309
parent 316083 a08af19f3b3c08a431da4951537ac76ed58c7da2
child 316085 d1ea28929a0cd640b17ec9817ad6c66e7812dc30
push id30759
push userphilringnalda@gmail.com
push dateSat, 01 Oct 2016 06:25:09 +0000
treeherdermozilla-central@fcc62bbf09ee [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs1306222
milestone52.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1306222 - Introducing GlobalObject::GetSubjectPrincipal, r=ehsan
dom/bindings/BindingDeclarations.h
dom/bindings/BindingUtils.cpp
--- a/dom/bindings/BindingDeclarations.h
+++ b/dom/bindings/BindingDeclarations.h
@@ -21,16 +21,17 @@
 #include "mozilla/RootedRefPtr.h"
 
 #include "mozilla/dom/DOMString.h"
 
 #include "nsCOMPtr.h"
 #include "nsStringGlue.h"
 #include "nsTArray.h"
 
+class nsIPrincipal;
 class nsWrapperCache;
 
 namespace mozilla {
 namespace dom {
 
 // Struct that serves as a base class for all dictionaries.  Particularly useful
 // so we can use IsBaseOf to detect dictionary template arguments.
 struct DictionaryBase
@@ -103,16 +104,20 @@ public:
     return mCx;
   }
 
   bool Failed() const
   {
     return !Get();
   }
 
+  // It returns the subjectPrincipal if called on the main-thread, otherwise
+  // a nullptr is returned.
+  nsIPrincipal* GetSubjectPrincipal() 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
@@ -2249,16 +2249,29 @@ GlobalObject::GetAsSupports() const
   }
 
   MOZ_ASSERT(!mGlobalObject);
 
   Throw(mCx, NS_ERROR_XPC_BAD_CONVERT_JS);
   return nullptr;
 }
 
+nsIPrincipal*
+GlobalObject::GetSubjectPrincipal() const
+{
+  if (!NS_IsMainThread()) {
+    return nullptr;
+  }
+
+  JSCompartment* compartment = js::GetContextCompartment(mCx);
+  MOZ_ASSERT(compartment);
+  JSPrincipals* principals = JS_GetCompartmentPrincipals(compartment);
+  return nsJSPrincipals::get(principals);
+}
+
 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;
     }