tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

SimpleGlobalObject.h (4009B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 /**
      8 * A simplere nsIGlobalObject implementation that can be used to set up a new
      9 * global without anything interesting in it other than the JS builtins.  This
     10 * is safe to use on both mainthread and worker threads.
     11 */
     12 
     13 #ifndef mozilla_dom_SimpleGlobalObject_h__
     14 #define mozilla_dom_SimpleGlobalObject_h__
     15 
     16 #include "js/TypeDecls.h"
     17 #include "js/Value.h"
     18 #include "nsContentUtils.h"
     19 #include "nsCycleCollectionParticipant.h"
     20 #include "nsIGlobalObject.h"
     21 #include "nsISupportsImpl.h"
     22 #include "nsThreadUtils.h"
     23 #include "nsWrapperCache.h"
     24 
     25 namespace mozilla::dom {
     26 
     27 class SimpleGlobalObject final : public nsIGlobalObject, public nsWrapperCache {
     28 public:
     29  enum class GlobalType {
     30    BindingDetail,  // Should only be used by DOM bindings code.
     31    WorkerDebuggerSandbox,
     32    NotSimpleGlobal  // Sentinel to be used by BasicGlobalType.
     33  };
     34 
     35  // Create a new JS global object that can be used to do some work.  This
     36  // global will NOT have any DOM APIs exposed in it, will not be visible to the
     37  // debugger, and will not have a useful concept of principals, so don't try to
     38  // use it with any DOM objects.  Apart from that, running code with
     39  // side-effects is safe in this global.  Importantly, when you are first
     40  // handed this global it's guaranteed to have pristine built-ins.  The
     41  // corresponding nsIGlobalObject* for this global object will be a
     42  // SimpleGlobalObject of the type provided; JS::GetPrivate on the returned
     43  // JSObject* will return the SimpleGlobalObject*.
     44  //
     45  // If the provided prototype value is undefined, it is ignored.  If it's an
     46  // object or null, it's set as the prototype of the created global.  If it's
     47  // anything else, this function returns null.
     48  //
     49  // Note that creating new globals is not cheap and should not be done
     50  // gratuitously.  Please think carefully before you use this function.
     51  static JSObject* Create(GlobalType globalType, JS::Handle<JS::Value> proto =
     52                                                     JS::UndefinedHandleValue);
     53 
     54  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
     55  NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(SimpleGlobalObject)
     56 
     57  // Gets the GlobalType of this SimpleGlobalObject.
     58  GlobalType Type() const { return mType; }
     59 
     60  // Gets the GlobalType of the SimpleGlobalObject for the given JSObject*, if
     61  // the given JSObject* is the global corresponding to a SimpleGlobalObject.
     62  // Oherwise, returns GlobalType::NotSimpleGlobal.
     63  static GlobalType SimpleGlobalType(JSObject* obj);
     64 
     65  JSObject* GetGlobalJSObject() override { return GetWrapper(); }
     66  JSObject* GetGlobalJSObjectPreserveColor() const override {
     67    return GetWrapperPreserveColor();
     68  }
     69 
     70  OriginTrials Trials() const override { return {}; }
     71 
     72  nsISerialEventTarget* SerialEventTarget() const final {
     73    return NS_GetCurrentThread();
     74  }
     75  nsresult Dispatch(already_AddRefed<nsIRunnable>&& aRunnable) const final {
     76    return NS_DispatchToCurrentThread(std::move(aRunnable));
     77  }
     78 
     79  JSObject* WrapObject(JSContext* cx,
     80                       JS::Handle<JSObject*> aGivenProto) override {
     81    MOZ_CRASH("SimpleGlobalObject doesn't use DOM bindings!");
     82  }
     83 
     84  bool ShouldResistFingerprinting(RFPTarget aTarget) const override {
     85    return nsContentUtils::ShouldResistFingerprinting(
     86        "Presently we don't have enough context to make an informed decision"
     87        "on JS Sandboxes. See 1782853",
     88        aTarget);
     89  }
     90 
     91 private:
     92  SimpleGlobalObject(JSObject* global, GlobalType type) : mType(type) {
     93    SetWrapper(global);
     94  }
     95 
     96  virtual ~SimpleGlobalObject() { MOZ_ASSERT(!GetWrapperMaybeDead()); }
     97 
     98  const GlobalType mType;
     99 };
    100 
    101 }  // namespace mozilla::dom
    102 
    103 #endif /* mozilla_dom_SimpleGlobalObject_h__ */