2013-12-11 17:51:57 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
// vim: ft=cpp tw=78 sw=2 et ts=2
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2013-12-11 17:51:57 -08:00
|
|
|
#include "mozilla/ThreadLocal.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
2013-12-11 17:51:57 -08:00
|
|
|
|
2013-12-11 17:51:57 -08:00
|
|
|
#include "jsapi.h"
|
2013-12-11 17:51:57 -08:00
|
|
|
#include "xpcpublic.h"
|
2013-12-11 17:51:57 -08:00
|
|
|
#include "nsIGlobalObject.h"
|
2013-12-11 17:51:57 -08:00
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsContentUtils.h"
|
2013-12-11 17:51:57 -08:00
|
|
|
#include "nsTArray.h"
|
2014-01-07 10:53:31 -08:00
|
|
|
#include "nsJSUtils.h"
|
2013-12-11 17:51:57 -08:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-12-11 17:51:57 -08:00
|
|
|
class ScriptSettingsStack;
|
|
|
|
static mozilla::ThreadLocal<ScriptSettingsStack*> sScriptSettingsTLS;
|
|
|
|
|
|
|
|
ScriptSettingsStackEntry ScriptSettingsStackEntry::SystemSingleton;
|
|
|
|
|
|
|
|
class ScriptSettingsStack {
|
|
|
|
public:
|
|
|
|
static ScriptSettingsStack& Ref() {
|
|
|
|
return *sScriptSettingsTLS.get();
|
|
|
|
}
|
|
|
|
ScriptSettingsStack() {};
|
|
|
|
|
|
|
|
void Push(ScriptSettingsStackEntry* aSettings) {
|
|
|
|
// The bottom-most entry must always be a candidate entry point.
|
|
|
|
MOZ_ASSERT_IF(mStack.Length() == 0 || mStack.LastElement()->IsSystemSingleton(),
|
|
|
|
aSettings->mIsCandidateEntryPoint);
|
|
|
|
mStack.AppendElement(aSettings);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PushSystem() {
|
|
|
|
mStack.AppendElement(&ScriptSettingsStackEntry::SystemSingleton);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Pop() {
|
|
|
|
MOZ_ASSERT(mStack.Length() > 0);
|
|
|
|
mStack.RemoveElementAt(mStack.Length() - 1);
|
|
|
|
}
|
|
|
|
|
2014-02-14 22:36:43 -08:00
|
|
|
ScriptSettingsStackEntry* Incumbent() {
|
2013-12-11 17:51:57 -08:00
|
|
|
if (!mStack.Length()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-02-14 22:36:43 -08:00
|
|
|
return mStack.LastElement();
|
2013-12-11 17:51:57 -08:00
|
|
|
}
|
|
|
|
|
2014-02-14 22:36:43 -08:00
|
|
|
nsIGlobalObject* IncumbentGlobal() {
|
|
|
|
ScriptSettingsStackEntry *entry = Incumbent();
|
|
|
|
return entry ? entry->mGlobalObject : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptSettingsStackEntry* EntryPoint() {
|
2013-12-11 17:51:57 -08:00
|
|
|
if (!mStack.Length())
|
|
|
|
return nullptr;
|
|
|
|
for (int i = mStack.Length() - 1; i >= 0; --i) {
|
|
|
|
if (mStack[i]->mIsCandidateEntryPoint) {
|
2014-02-14 22:36:43 -08:00
|
|
|
return mStack[i];
|
2013-12-11 17:51:57 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
MOZ_ASSUME_UNREACHABLE("Non-empty stack should always have an entry point");
|
|
|
|
}
|
|
|
|
|
2014-02-14 22:36:43 -08:00
|
|
|
nsIGlobalObject* EntryGlobal() {
|
|
|
|
ScriptSettingsStackEntry *entry = EntryPoint();
|
|
|
|
return entry ? entry->mGlobalObject : nullptr;
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:51:57 -08:00
|
|
|
private:
|
|
|
|
// These pointers are caller-owned.
|
|
|
|
nsTArray<ScriptSettingsStackEntry*> mStack;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
InitScriptSettings()
|
|
|
|
{
|
|
|
|
if (!sScriptSettingsTLS.initialized()) {
|
|
|
|
bool success = sScriptSettingsTLS.init();
|
|
|
|
if (!success) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptSettingsStack* ptr = new ScriptSettingsStack();
|
|
|
|
sScriptSettingsTLS.set(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DestroyScriptSettings()
|
|
|
|
{
|
|
|
|
ScriptSettingsStack* ptr = sScriptSettingsTLS.get();
|
|
|
|
MOZ_ASSERT(ptr);
|
|
|
|
sScriptSettingsTLS.set(nullptr);
|
|
|
|
delete ptr;
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:53:31 -08:00
|
|
|
// This mostly gets the entry global, but doesn't entirely match the spec in
|
|
|
|
// certain edge cases. It's good enough for some purposes, but not others. If
|
|
|
|
// you want to call this function, ping bholley and describe your use-case.
|
|
|
|
nsIGlobalObject*
|
|
|
|
BrokenGetEntryGlobal()
|
|
|
|
{
|
|
|
|
// We need the current JSContext in order to check the JS for
|
|
|
|
// scripted frames that may have appeared since anyone last
|
|
|
|
// manipulated the stack. If it's null, that means that there
|
2014-02-14 22:36:43 -08:00
|
|
|
// must be no entry global on the stack.
|
2014-01-07 10:53:31 -08:00
|
|
|
JSContext *cx = nsContentUtils::GetCurrentJSContextForThread();
|
|
|
|
if (!cx) {
|
2014-02-14 22:36:43 -08:00
|
|
|
MOZ_ASSERT(ScriptSettingsStack::Ref().EntryGlobal() == nullptr);
|
2014-01-07 10:53:31 -08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsJSUtils::GetDynamicScriptGlobal(cx);
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:51:57 -08:00
|
|
|
// Note: When we're ready to expose it, GetEntryGlobal will look similar to
|
|
|
|
// GetIncumbentGlobal below.
|
|
|
|
|
|
|
|
nsIGlobalObject*
|
|
|
|
GetIncumbentGlobal()
|
|
|
|
{
|
|
|
|
// We need the current JSContext in order to check the JS for
|
|
|
|
// scripted frames that may have appeared since anyone last
|
|
|
|
// manipulated the stack. If it's null, that means that there
|
2014-02-14 22:36:43 -08:00
|
|
|
// must be no entry global on the stack, and therefore no incumbent
|
2013-12-11 17:51:57 -08:00
|
|
|
// global either.
|
|
|
|
JSContext *cx = nsContentUtils::GetCurrentJSContextForThread();
|
|
|
|
if (!cx) {
|
2014-02-14 22:36:43 -08:00
|
|
|
MOZ_ASSERT(ScriptSettingsStack::Ref().EntryGlobal() == nullptr);
|
2013-12-11 17:51:57 -08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See what the JS engine has to say. If we've got a scripted caller
|
|
|
|
// override in place, the JS engine will lie to us and pretend that
|
|
|
|
// there's nothing on the JS stack, which will cause us to check the
|
|
|
|
// incumbent script stack below.
|
|
|
|
JS::RootedScript script(cx);
|
|
|
|
if (JS_DescribeScriptedCaller(cx, &script, nullptr)) {
|
|
|
|
JS::RootedObject global(cx, JS_GetGlobalFromScript(script));
|
|
|
|
MOZ_ASSERT(global);
|
|
|
|
return xpc::GetNativeForGlobal(global);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, nothing from the JS engine. Let's use whatever's on the
|
|
|
|
// explicit stack.
|
2014-02-14 22:36:43 -08:00
|
|
|
return ScriptSettingsStack::Ref().IncumbentGlobal();
|
2014-02-14 16:13:38 -08:00
|
|
|
}
|
|
|
|
|
2014-02-14 22:36:44 -08:00
|
|
|
nsIPrincipal*
|
|
|
|
GetWebIDLCallerPrincipal()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
ScriptSettingsStackEntry *entry = ScriptSettingsStack::Ref().EntryPoint();
|
|
|
|
|
|
|
|
// If we have an entry point that is not the system singleton, we know it
|
|
|
|
// must be an AutoEntryScript.
|
|
|
|
if (!entry || entry->IsSystemSingleton()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
AutoEntryScript* aes = static_cast<AutoEntryScript*>(entry);
|
|
|
|
|
|
|
|
// We can't yet rely on the Script Settings Stack to properly determine the
|
|
|
|
// entry script, because there are still lots of places in the tree where we
|
|
|
|
// don't yet use an AutoEntryScript (bug 951991 tracks this work). In the
|
|
|
|
// mean time though, we can make some observations to hack around the
|
|
|
|
// problem:
|
|
|
|
//
|
|
|
|
// (1) All calls into JS-implemented WebIDL go through CallSetup, which goes
|
|
|
|
// through AutoEntryScript.
|
|
|
|
// (2) The top candidate entry point in the Script Settings Stack is the
|
|
|
|
// entry point if and only if no other JSContexts have been pushed on
|
|
|
|
// top of the push made by that entry's AutoEntryScript.
|
|
|
|
//
|
|
|
|
// Because of (1), all of the cases where we might return a non-null
|
|
|
|
// WebIDL Caller are guaranteed to have put an entry on the Script Settings
|
|
|
|
// Stack, so we can restrict our search to that. Moreover, (2) gives us a
|
|
|
|
// criterion to determine whether an entry in the Script Setting Stack means
|
|
|
|
// that we should return a non-null WebIDL Caller.
|
|
|
|
//
|
|
|
|
// Once we fix bug 951991, this can all be simplified.
|
|
|
|
if (!aes->mCxPusher.ref().IsStackTop()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return aes->mWebIDLCallerPrincipal;
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:51:57 -08:00
|
|
|
AutoEntryScript::AutoEntryScript(nsIGlobalObject* aGlobalObject,
|
|
|
|
bool aIsMainThread,
|
|
|
|
JSContext* aCx)
|
2014-02-14 22:36:43 -08:00
|
|
|
: ScriptSettingsStackEntry(aGlobalObject, /* aCandidate = */ true)
|
|
|
|
, mStack(ScriptSettingsStack::Ref())
|
2013-12-11 17:51:57 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aGlobalObject);
|
|
|
|
if (!aCx) {
|
|
|
|
// If the caller didn't provide a cx, hunt one down. This isn't exactly
|
|
|
|
// fast, but the callers that care about performance can pass an explicit
|
|
|
|
// cx for now. Eventually, the whole cx pushing thing will go away
|
|
|
|
// entirely.
|
|
|
|
MOZ_ASSERT(aIsMainThread, "cx is mandatory off-main-thread");
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aGlobalObject);
|
|
|
|
if (sgo && sgo->GetScriptContext()) {
|
|
|
|
aCx = sgo->GetScriptContext()->GetNativeContext();
|
|
|
|
}
|
|
|
|
if (!aCx) {
|
|
|
|
aCx = nsContentUtils::GetSafeJSContext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (aIsMainThread) {
|
2014-02-14 22:36:43 -08:00
|
|
|
mCxPusher.construct(aCx);
|
2013-12-11 17:51:57 -08:00
|
|
|
}
|
|
|
|
mAc.construct(aCx, aGlobalObject->GetGlobalJSObject());
|
2014-02-14 22:36:43 -08:00
|
|
|
mStack.Push(this);
|
2013-12-11 17:51:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
AutoEntryScript::~AutoEntryScript()
|
|
|
|
{
|
2014-02-14 22:36:43 -08:00
|
|
|
MOZ_ASSERT(mStack.Incumbent() == this);
|
2013-12-11 17:51:57 -08:00
|
|
|
mStack.Pop();
|
2013-12-11 17:51:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
AutoIncumbentScript::AutoIncumbentScript(nsIGlobalObject* aGlobalObject)
|
2014-02-14 22:36:43 -08:00
|
|
|
: ScriptSettingsStackEntry(aGlobalObject, /* aCandidate = */ false)
|
|
|
|
, mStack(ScriptSettingsStack::Ref())
|
2013-12-11 17:51:57 -08:00
|
|
|
, mCallerOverride(nsContentUtils::GetCurrentJSContextForThread())
|
2013-12-11 17:51:57 -08:00
|
|
|
{
|
2014-02-14 22:36:43 -08:00
|
|
|
mStack.Push(this);
|
2013-12-11 17:51:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
AutoIncumbentScript::~AutoIncumbentScript()
|
|
|
|
{
|
2014-02-14 22:36:43 -08:00
|
|
|
MOZ_ASSERT(mStack.Incumbent() == this);
|
2013-12-11 17:51:57 -08:00
|
|
|
mStack.Pop();
|
2013-12-11 17:51:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
AutoSystemCaller::AutoSystemCaller(bool aIsMainThread)
|
2013-12-11 17:51:57 -08:00
|
|
|
: mStack(ScriptSettingsStack::Ref())
|
2013-12-11 17:51:57 -08:00
|
|
|
{
|
|
|
|
if (aIsMainThread) {
|
2014-02-14 22:36:43 -08:00
|
|
|
mCxPusher.construct(static_cast<JSContext*>(nullptr),
|
|
|
|
/* aAllowNull = */ true);
|
2013-12-11 17:51:57 -08:00
|
|
|
}
|
2013-12-11 17:51:57 -08:00
|
|
|
mStack.PushSystem();
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoSystemCaller::~AutoSystemCaller()
|
|
|
|
{
|
|
|
|
mStack.Pop();
|
2013-12-11 17:51:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|