dom/base/ScriptSettings.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/base/ScriptSettings.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,297 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +// vim: ft=cpp tw=78 sw=2 et ts=2
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "mozilla/dom/ScriptSettings.h"
    1.11 +#include "mozilla/ThreadLocal.h"
    1.12 +#include "mozilla/Assertions.h"
    1.13 +
    1.14 +#include "jsapi.h"
    1.15 +#include "xpcpublic.h"
    1.16 +#include "nsIGlobalObject.h"
    1.17 +#include "nsIScriptGlobalObject.h"
    1.18 +#include "nsIScriptContext.h"
    1.19 +#include "nsContentUtils.h"
    1.20 +#include "nsTArray.h"
    1.21 +#include "nsJSUtils.h"
    1.22 +
    1.23 +namespace mozilla {
    1.24 +namespace dom {
    1.25 +
    1.26 +class ScriptSettingsStack;
    1.27 +static mozilla::ThreadLocal<ScriptSettingsStack*> sScriptSettingsTLS;
    1.28 +
    1.29 +ScriptSettingsStackEntry ScriptSettingsStackEntry::NoJSAPISingleton;
    1.30 +
    1.31 +class ScriptSettingsStack {
    1.32 +public:
    1.33 +  static ScriptSettingsStack& Ref() {
    1.34 +    return *sScriptSettingsTLS.get();
    1.35 +  }
    1.36 +  ScriptSettingsStack() {};
    1.37 +
    1.38 +  void Push(ScriptSettingsStackEntry* aSettings) {
    1.39 +    // The bottom-most entry must always be a candidate entry point.
    1.40 +    MOZ_ASSERT_IF(mStack.Length() == 0 || mStack.LastElement()->NoJSAPI(),
    1.41 +                  aSettings->mIsCandidateEntryPoint);
    1.42 +    mStack.AppendElement(aSettings);
    1.43 +  }
    1.44 +
    1.45 +  void PushNoJSAPI() {
    1.46 +    mStack.AppendElement(&ScriptSettingsStackEntry::NoJSAPISingleton);
    1.47 +  }
    1.48 +
    1.49 +  void Pop() {
    1.50 +    MOZ_ASSERT(mStack.Length() > 0);
    1.51 +    mStack.RemoveElementAt(mStack.Length() - 1);
    1.52 +  }
    1.53 +
    1.54 +  ScriptSettingsStackEntry* Incumbent() {
    1.55 +    if (!mStack.Length()) {
    1.56 +      return nullptr;
    1.57 +    }
    1.58 +    return mStack.LastElement();
    1.59 +  }
    1.60 +
    1.61 +  nsIGlobalObject* IncumbentGlobal() {
    1.62 +    ScriptSettingsStackEntry *entry = Incumbent();
    1.63 +    return entry ? entry->mGlobalObject : nullptr;
    1.64 +  }
    1.65 +
    1.66 +  ScriptSettingsStackEntry* EntryPoint() {
    1.67 +    if (!mStack.Length())
    1.68 +      return nullptr;
    1.69 +    for (int i = mStack.Length() - 1; i >= 0; --i) {
    1.70 +      if (mStack[i]->mIsCandidateEntryPoint) {
    1.71 +        return mStack[i];
    1.72 +      }
    1.73 +    }
    1.74 +    MOZ_ASSUME_UNREACHABLE("Non-empty stack should always have an entry point");
    1.75 +  }
    1.76 +
    1.77 +  nsIGlobalObject* EntryGlobal() {
    1.78 +    ScriptSettingsStackEntry *entry = EntryPoint();
    1.79 +    return entry ? entry->mGlobalObject : nullptr;
    1.80 +  }
    1.81 +
    1.82 +private:
    1.83 +  // These pointers are caller-owned.
    1.84 +  nsTArray<ScriptSettingsStackEntry*> mStack;
    1.85 +};
    1.86 +
    1.87 +void
    1.88 +InitScriptSettings()
    1.89 +{
    1.90 +  if (!sScriptSettingsTLS.initialized()) {
    1.91 +    bool success = sScriptSettingsTLS.init();
    1.92 +    if (!success) {
    1.93 +      MOZ_CRASH();
    1.94 +    }
    1.95 +  }
    1.96 +
    1.97 +  ScriptSettingsStack* ptr = new ScriptSettingsStack();
    1.98 +  sScriptSettingsTLS.set(ptr);
    1.99 +}
   1.100 +
   1.101 +void DestroyScriptSettings()
   1.102 +{
   1.103 +  ScriptSettingsStack* ptr = sScriptSettingsTLS.get();
   1.104 +  MOZ_ASSERT(ptr);
   1.105 +  sScriptSettingsTLS.set(nullptr);
   1.106 +  delete ptr;
   1.107 +}
   1.108 +
   1.109 +// This mostly gets the entry global, but doesn't entirely match the spec in
   1.110 +// certain edge cases. It's good enough for some purposes, but not others. If
   1.111 +// you want to call this function, ping bholley and describe your use-case.
   1.112 +nsIGlobalObject*
   1.113 +BrokenGetEntryGlobal()
   1.114 +{
   1.115 +  // We need the current JSContext in order to check the JS for
   1.116 +  // scripted frames that may have appeared since anyone last
   1.117 +  // manipulated the stack. If it's null, that means that there
   1.118 +  // must be no entry global on the stack.
   1.119 +  JSContext *cx = nsContentUtils::GetCurrentJSContextForThread();
   1.120 +  if (!cx) {
   1.121 +    MOZ_ASSERT(ScriptSettingsStack::Ref().EntryGlobal() == nullptr);
   1.122 +    return nullptr;
   1.123 +  }
   1.124 +
   1.125 +  return nsJSUtils::GetDynamicScriptGlobal(cx);
   1.126 +}
   1.127 +
   1.128 +// Note: When we're ready to expose it, GetEntryGlobal will look similar to
   1.129 +// GetIncumbentGlobal below.
   1.130 +
   1.131 +nsIGlobalObject*
   1.132 +GetIncumbentGlobal()
   1.133 +{
   1.134 +  // We need the current JSContext in order to check the JS for
   1.135 +  // scripted frames that may have appeared since anyone last
   1.136 +  // manipulated the stack. If it's null, that means that there
   1.137 +  // must be no entry global on the stack, and therefore no incumbent
   1.138 +  // global either.
   1.139 +  JSContext *cx = nsContentUtils::GetCurrentJSContextForThread();
   1.140 +  if (!cx) {
   1.141 +    MOZ_ASSERT(ScriptSettingsStack::Ref().EntryGlobal() == nullptr);
   1.142 +    return nullptr;
   1.143 +  }
   1.144 +
   1.145 +  // See what the JS engine has to say. If we've got a scripted caller
   1.146 +  // override in place, the JS engine will lie to us and pretend that
   1.147 +  // there's nothing on the JS stack, which will cause us to check the
   1.148 +  // incumbent script stack below.
   1.149 +  if (JSObject *global = JS::GetScriptedCallerGlobal(cx)) {
   1.150 +    return xpc::GetNativeForGlobal(global);
   1.151 +  }
   1.152 +
   1.153 +  // Ok, nothing from the JS engine. Let's use whatever's on the
   1.154 +  // explicit stack.
   1.155 +  return ScriptSettingsStack::Ref().IncumbentGlobal();
   1.156 +}
   1.157 +
   1.158 +nsIPrincipal*
   1.159 +GetWebIDLCallerPrincipal()
   1.160 +{
   1.161 +  MOZ_ASSERT(NS_IsMainThread());
   1.162 +  ScriptSettingsStackEntry *entry = ScriptSettingsStack::Ref().EntryPoint();
   1.163 +
   1.164 +  // If we have an entry point that is not the NoJSAPI singleton, we know it
   1.165 +  // must be an AutoEntryScript.
   1.166 +  if (!entry || entry->NoJSAPI()) {
   1.167 +    return nullptr;
   1.168 +  }
   1.169 +  AutoEntryScript* aes = static_cast<AutoEntryScript*>(entry);
   1.170 +
   1.171 +  // We can't yet rely on the Script Settings Stack to properly determine the
   1.172 +  // entry script, because there are still lots of places in the tree where we
   1.173 +  // don't yet use an AutoEntryScript (bug 951991 tracks this work). In the
   1.174 +  // mean time though, we can make some observations to hack around the
   1.175 +  // problem:
   1.176 +  //
   1.177 +  // (1) All calls into JS-implemented WebIDL go through CallSetup, which goes
   1.178 +  //     through AutoEntryScript.
   1.179 +  // (2) The top candidate entry point in the Script Settings Stack is the
   1.180 +  //     entry point if and only if no other JSContexts have been pushed on
   1.181 +  //     top of the push made by that entry's AutoEntryScript.
   1.182 +  //
   1.183 +  // Because of (1), all of the cases where we might return a non-null
   1.184 +  // WebIDL Caller are guaranteed to have put an entry on the Script Settings
   1.185 +  // Stack, so we can restrict our search to that. Moreover, (2) gives us a
   1.186 +  // criterion to determine whether an entry in the Script Setting Stack means
   1.187 +  // that we should return a non-null WebIDL Caller.
   1.188 +  //
   1.189 +  // Once we fix bug 951991, this can all be simplified.
   1.190 +  if (!aes->CxPusherIsStackTop()) {
   1.191 +    return nullptr;
   1.192 +  }
   1.193 +
   1.194 +  return aes->mWebIDLCallerPrincipal;
   1.195 +}
   1.196 +
   1.197 +static JSContext*
   1.198 +FindJSContext(nsIGlobalObject* aGlobalObject)
   1.199 +{
   1.200 +  MOZ_ASSERT(NS_IsMainThread());
   1.201 +  JSContext *cx = nullptr;
   1.202 +  nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aGlobalObject);
   1.203 +  if (sgo && sgo->GetScriptContext()) {
   1.204 +    cx = sgo->GetScriptContext()->GetNativeContext();
   1.205 +  }
   1.206 +  if (!cx) {
   1.207 +    cx = nsContentUtils::GetSafeJSContext();
   1.208 +  }
   1.209 +  return cx;
   1.210 +}
   1.211 +
   1.212 +AutoJSAPI::AutoJSAPI()
   1.213 +  : mCx(nsContentUtils::GetDefaultJSContextForThread())
   1.214 +{
   1.215 +  if (NS_IsMainThread()) {
   1.216 +    mCxPusher.construct(mCx);
   1.217 +  }
   1.218 +
   1.219 +  // Leave the cx in a null compartment.
   1.220 +  mNullAc.construct(mCx);
   1.221 +}
   1.222 +
   1.223 +AutoJSAPI::AutoJSAPI(JSContext *aCx, bool aIsMainThread, bool aSkipNullAc)
   1.224 +  : mCx(aCx)
   1.225 +{
   1.226 +  MOZ_ASSERT_IF(aIsMainThread, NS_IsMainThread());
   1.227 +  if (aIsMainThread) {
   1.228 +    mCxPusher.construct(mCx);
   1.229 +  }
   1.230 +
   1.231 +  // In general we want to leave the cx in a null compartment, but we let
   1.232 +  // subclasses skip this if they plan to immediately enter a compartment.
   1.233 +  if (!aSkipNullAc) {
   1.234 +    mNullAc.construct(mCx);
   1.235 +  }
   1.236 +}
   1.237 +
   1.238 +AutoJSAPIWithErrorsReportedToWindow::AutoJSAPIWithErrorsReportedToWindow(nsIScriptContext* aScx)
   1.239 +  : AutoJSAPI(aScx->GetNativeContext(), /* aIsMainThread = */ true)
   1.240 +{
   1.241 +}
   1.242 +
   1.243 +AutoJSAPIWithErrorsReportedToWindow::AutoJSAPIWithErrorsReportedToWindow(nsIGlobalObject* aGlobalObject)
   1.244 +  : AutoJSAPI(FindJSContext(aGlobalObject), /* aIsMainThread = */ true)
   1.245 +{
   1.246 +}
   1.247 +
   1.248 +AutoEntryScript::AutoEntryScript(nsIGlobalObject* aGlobalObject,
   1.249 +                                 bool aIsMainThread,
   1.250 +                                 JSContext* aCx)
   1.251 +  : AutoJSAPI(aCx ? aCx : FindJSContext(aGlobalObject), aIsMainThread, /* aSkipNullAc = */ true)
   1.252 +  , ScriptSettingsStackEntry(aGlobalObject, /* aCandidate = */ true)
   1.253 +  , mAc(cx(), aGlobalObject->GetGlobalJSObject())
   1.254 +  , mStack(ScriptSettingsStack::Ref())
   1.255 +{
   1.256 +  MOZ_ASSERT(aGlobalObject);
   1.257 +  MOZ_ASSERT_IF(!aCx, aIsMainThread); // cx is mandatory off-main-thread.
   1.258 +  MOZ_ASSERT_IF(aCx && aIsMainThread, aCx == FindJSContext(aGlobalObject));
   1.259 +  mStack.Push(this);
   1.260 +}
   1.261 +
   1.262 +AutoEntryScript::~AutoEntryScript()
   1.263 +{
   1.264 +  MOZ_ASSERT(mStack.Incumbent() == this);
   1.265 +  mStack.Pop();
   1.266 +}
   1.267 +
   1.268 +AutoIncumbentScript::AutoIncumbentScript(nsIGlobalObject* aGlobalObject)
   1.269 +  : ScriptSettingsStackEntry(aGlobalObject, /* aCandidate = */ false)
   1.270 +  , mStack(ScriptSettingsStack::Ref())
   1.271 +  , mCallerOverride(nsContentUtils::GetCurrentJSContextForThread())
   1.272 +{
   1.273 +  mStack.Push(this);
   1.274 +}
   1.275 +
   1.276 +AutoIncumbentScript::~AutoIncumbentScript()
   1.277 +{
   1.278 +  MOZ_ASSERT(mStack.Incumbent() == this);
   1.279 +  mStack.Pop();
   1.280 +}
   1.281 +
   1.282 +AutoNoJSAPI::AutoNoJSAPI(bool aIsMainThread)
   1.283 +  : mStack(ScriptSettingsStack::Ref())
   1.284 +{
   1.285 +  MOZ_ASSERT_IF(nsContentUtils::GetCurrentJSContextForThread(),
   1.286 +                !JS_IsExceptionPending(nsContentUtils::GetCurrentJSContextForThread()));
   1.287 +  if (aIsMainThread) {
   1.288 +    mCxPusher.construct(static_cast<JSContext*>(nullptr),
   1.289 +                        /* aAllowNull = */ true);
   1.290 +  }
   1.291 +  mStack.PushNoJSAPI();
   1.292 +}
   1.293 +
   1.294 +AutoNoJSAPI::~AutoNoJSAPI()
   1.295 +{
   1.296 +  mStack.Pop();
   1.297 +}
   1.298 +
   1.299 +} // namespace dom
   1.300 +} // namespace mozilla

mercurial