From f9ba8ad57a83e1276794ff3779884b0dbde734c7 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Wed, 5 Feb 2014 13:38:17 -0500 Subject: [PATCH] Bug 958667 part 3. Add helper methods for testing whether things should be exposed in privilegd or certified apps. r=peterv --- dom/bindings/BindingUtils.cpp | 28 ++++++++++++++++++++++++++++ dom/bindings/BindingUtils.h | 14 ++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/dom/bindings/BindingUtils.cpp b/dom/bindings/BindingUtils.cpp index b327bc7a350c..d0a082ed1006 100644 --- a/dom/bindings/BindingUtils.cpp +++ b/dom/bindings/BindingUtils.cpp @@ -18,7 +18,9 @@ #include "AccessCheck.h" #include "jsfriendapi.h" #include "js/OldDebugAPI.h" +#include "nsContentUtils.h" #include "nsIDOMGlobalPropertyInitializer.h" +#include "nsIPrincipal.h" #include "nsIXPConnect.h" #include "WrapperFactory.h" #include "xpcprivate.h" @@ -2148,6 +2150,32 @@ ThreadsafeCheckIsChrome(JSContext* aCx, JSObject* aObj) GetWorkerPrivateFromContext(aCx)->UsesSystemPrincipal(); } +bool +IsInPrivilegedApp(JSContext* aCx, JSObject* aObj) +{ + using mozilla::dom::workers::GetWorkerPrivateFromContext; + if (!NS_IsMainThread()) { + return GetWorkerPrivateFromContext(aCx)->IsInPrivilegedApp(); + } + + nsIPrincipal* principal = nsContentUtils::GetObjectPrincipal(aObj); + uint16_t appStatus = principal->GetAppStatus(); + return (appStatus == nsIPrincipal::APP_STATUS_CERTIFIED || + appStatus == nsIPrincipal::APP_STATUS_PRIVILEGED); +} + +bool +IsInCertifiedApp(JSContext* aCx, JSObject* aObj) +{ + using mozilla::dom::workers::GetWorkerPrivateFromContext; + if (!NS_IsMainThread()) { + return GetWorkerPrivateFromContext(aCx)->IsInCertifiedApp(); + } + + nsIPrincipal* principal = nsContentUtils::GetObjectPrincipal(aObj); + return principal->GetAppStatus() == nsIPrincipal::APP_STATUS_CERTIFIED; +} + void TraceGlobal(JSTracer* aTrc, JSObject* aObj) { diff --git a/dom/bindings/BindingUtils.h b/dom/bindings/BindingUtils.h index e35893cac867..70b078ad4071 100644 --- a/dom/bindings/BindingUtils.h +++ b/dom/bindings/BindingUtils.h @@ -2354,6 +2354,20 @@ public: bool ThreadsafeCheckIsChrome(JSContext* aCx, JSObject* aObj); +/* + * Helper function for testing whether the given object comes from a + * privileged app. + */ +bool +IsInPrivilegedApp(JSContext* aCx, JSObject* aObj); + +/* + * Helper function for testing whether the given object comes from a + * certified app. + */ +bool +IsInCertifiedApp(JSContext* aCx, JSObject* aObj); + void TraceGlobal(JSTracer* aTrc, JSObject* aObj);