Bug 897913 part 3. Enable Promise in chrome and certified apps, even when preffed off. r=bholley, pending review from baku

This commit is contained in:
Boris Zbarsky 2013-08-07 17:40:20 -04:00
parent 31d74062e9
commit 70215e3681
4 changed files with 38 additions and 9 deletions

View File

@ -13,4 +13,9 @@ LIBRARY_NAME = dompromise_s
LIBXUL_LIBRARY = 1
FAIL_ON_WARNINGS := 1
LOCAL_INCLUDES += \
-I$(topsrcdir)/dom/workers \
-I$(topsrcdir)/dom/base \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@ -11,6 +11,8 @@
#include "PromiseCallback.h"
#include "nsContentUtils.h"
#include "nsPIDOMWindow.h"
#include "WorkerPrivate.h"
#include "nsJSPrincipals.h"
namespace mozilla {
namespace dom {
@ -110,6 +112,26 @@ Promise::PrefEnabled()
return Preferences::GetBool("dom.promise.enabled", false);
}
/* static */ bool
Promise::EnabledForScope(JSContext* aCx, JSObject* /* unused */)
{
// Enable if the pref is enabled or if we're chrome or if we're a
// certified app.
if (PrefEnabled()) {
return true;
}
// Note that we have no concept of a certified app in workers.
// XXXbz well, why not?
if (!NS_IsMainThread()) {
return workers::GetWorkerPrivateFromContext(aCx)->IsChromeWorker();
}
nsIPrincipal* prin = nsContentUtils::GetSubjectPrincipal();
return nsContentUtils::IsSystemPrincipal(prin) ||
prin->GetAppStatus() == nsIPrincipal::APP_STATUS_CERTIFIED;
}
static void
EnterCompartment(Maybe<JSAutoCompartment>& aAc, JSContext* aCx,
const Optional<JS::Handle<JS::Value> >& aValue)
@ -125,8 +147,6 @@ EnterCompartment(Maybe<JSAutoCompartment>& aAc, JSContext* aCx,
Promise::Constructor(const GlobalObject& aGlobal, JSContext* aCx,
PromiseInit& aInit, ErrorResult& aRv)
{
MOZ_ASSERT(PrefEnabled());
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.Get());
if (!window) {
aRv.Throw(NS_ERROR_UNEXPECTED);
@ -155,8 +175,6 @@ Promise::Constructor(const GlobalObject& aGlobal, JSContext* aCx,
Promise::Resolve(const GlobalObject& aGlobal, JSContext* aCx,
JS::Handle<JS::Value> aValue, ErrorResult& aRv)
{
MOZ_ASSERT(PrefEnabled());
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.Get());
if (!window) {
aRv.Throw(NS_ERROR_UNEXPECTED);
@ -174,8 +192,6 @@ Promise::Resolve(const GlobalObject& aGlobal, JSContext* aCx,
Promise::Reject(const GlobalObject& aGlobal, JSContext* aCx,
JS::Handle<JS::Value> aValue, ErrorResult& aRv)
{
MOZ_ASSERT(PrefEnabled());
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.Get());
if (!window) {
aRv.Throw(NS_ERROR_UNEXPECTED);

View File

@ -41,6 +41,7 @@ public:
~Promise();
static bool PrefEnabled();
static bool EnabledForScope(JSContext* aCx, JSObject* /* unused */);
// WebIDL

View File

@ -7,6 +7,7 @@
* http://dom.spec.whatwg.org/#promises
*/
[Func="mozilla::dom::Promise::EnabledForScope"]
interface PromiseResolver {
// TODO bug 875289 - void fulfill(optional any value);
void resolve(optional any value);
@ -16,12 +17,18 @@ interface PromiseResolver {
callback PromiseInit = void (PromiseResolver resolver);
callback AnyCallback = any (optional any value);
[PrefControlled, Constructor(PromiseInit init)]
[Func="mozilla::dom::Promise::EnabledForScope", Constructor(PromiseInit init)]
interface Promise {
// TODO bug 875289 - static Promise fulfill(any value);
[Creator, Throws]
// Disable the static methods when the interface object is supposed to be
// disabled, just in case some code decides to walk over to .constructor from
// the proto of a promise object or someone screws up and manages to create a
// Promise object in this scope without having resolved the interface object
// first.
[Creator, Throws, Func="mozilla::dom::Promise::EnabledForScope"]
static Promise resolve(any value); // same as any(value)
[Creator, Throws]
[Creator, Throws, Func="mozilla::dom::Promise::EnabledForScope"]
static Promise reject(any value);
[Creator]