tor-browser

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

WindowGlobalChild.h (8680B)


      1 /* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 8 -*- */
      2 /* vim: set sw=2 ts=8 et tw=80 ft=cpp : */
      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
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef mozilla_dom_WindowGlobalChild_h
      8 #define mozilla_dom_WindowGlobalChild_h
      9 
     10 #include "mozilla/RefPtr.h"
     11 #include "mozilla/WeakPtr.h"
     12 #include "mozilla/dom/Document.h"
     13 #include "mozilla/dom/PWindowGlobalChild.h"
     14 #include "mozilla/dom/WindowGlobalActor.h"
     15 #include "mozilla/dom/WindowProxyHolder.h"
     16 #include "nsRefPtrHashtable.h"
     17 #include "nsWrapperCache.h"
     18 
     19 class nsGlobalWindowInner;
     20 class nsDocShell;
     21 
     22 namespace mozilla::dom {
     23 
     24 class BrowsingContext;
     25 class WindowContext;
     26 class WindowGlobalParent;
     27 class JSWindowActorChild;
     28 class JSActorMessageMeta;
     29 class BrowserChild;
     30 
     31 /**
     32 * Actor for a single nsGlobalWindowInner. This actor is used to communicate
     33 * information to the parent process asynchronously.
     34 */
     35 class WindowGlobalChild final : public WindowGlobalActor,
     36                                public nsWrapperCache,
     37                                public PWindowGlobalChild,
     38                                public SupportsWeakPtr {
     39  friend class PWindowGlobalChild;
     40 
     41 public:
     42  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
     43  NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(WindowGlobalChild)
     44 
     45  static already_AddRefed<WindowGlobalChild> GetByInnerWindowId(
     46      uint64_t aInnerWindowId);
     47 
     48  static already_AddRefed<WindowGlobalChild> GetByInnerWindowId(
     49      const GlobalObject& aGlobal, uint64_t aInnerWindowId) {
     50    return GetByInnerWindowId(aInnerWindowId);
     51  }
     52 
     53  dom::BrowsingContext* BrowsingContext() override;
     54  dom::WindowContext* WindowContext() const { return mWindowContext; }
     55  nsGlobalWindowInner* GetWindowGlobal() const { return mWindowGlobal; }
     56 
     57  Nullable<WindowProxyHolder> GetContentWindow();
     58 
     59  // Has this actor been shut down
     60  bool IsClosed() { return !CanSend(); }
     61  void Destroy();
     62 
     63  // Check if this actor is managed by PInProcess, as-in the document is loaded
     64  // in the chrome process.
     65  bool IsInProcess() { return XRE_IsParentProcess(); }
     66 
     67  nsIURI* GetDocumentURI() override { return mDocumentURI; }
     68  void SetDocumentURI(nsIURI* aDocumentURI);
     69  // See the corresponding comment for `UpdateDocumentPrincipal` in
     70  // PWindowGlobal on why and when this is allowed
     71  void SetDocumentPrincipal(nsIPrincipal* aNewDocumentPrincipal,
     72                            nsIPrincipal* aNewDocumentStoragePrincipal);
     73 
     74  nsIPrincipal* DocumentPrincipal() { return mDocumentPrincipal; }
     75 
     76  // The Window ID for this WindowGlobal
     77  uint64_t InnerWindowId();
     78  uint64_t OuterWindowId();
     79 
     80  uint64_t ContentParentId();
     81 
     82  int64_t BeforeUnloadListeners() { return mBeforeUnloadListeners; }
     83  void BeforeUnloadAdded();
     84  void BeforeUnloadRemoved();
     85 
     86  void NavigateAdded();
     87  void NavigateRemoved();
     88 
     89  bool IsCurrentGlobal();
     90 
     91  bool IsProcessRoot();
     92 
     93  // Get the other side of this actor if it is an in-process actor. Returns
     94  // |nullptr| if the actor has been torn down, or is not in-process.
     95  already_AddRefed<WindowGlobalParent> GetParentActor();
     96 
     97  // Get this actor's manager if it is not an in-process actor. Returns
     98  // |nullptr| if the actor has been torn down, or is in-process.
     99  already_AddRefed<BrowserChild> GetBrowserChild();
    100 
    101  // Get a JS actor object by name.
    102  already_AddRefed<JSWindowActorChild> GetActor(JSContext* aCx,
    103                                                const nsACString& aName,
    104                                                ErrorResult& aRv);
    105  already_AddRefed<JSWindowActorChild> GetExistingActor(
    106      const nsACString& aName);
    107 
    108  // Create and initialize the WindowGlobalChild object.
    109  static already_AddRefed<WindowGlobalChild> Create(
    110      nsGlobalWindowInner* aWindow);
    111  static already_AddRefed<WindowGlobalChild> CreateDisconnected(
    112      const WindowGlobalInit& aInit);
    113 
    114  void Init();
    115 
    116  void InitWindowGlobal(nsGlobalWindowInner* aWindow);
    117 
    118  // Called when a new document is loaded in this WindowGlobalChild.
    119  void OnNewDocument(Document* aNewDocument);
    120 
    121  // Returns true if this WindowGlobal is same-origin with the given
    122  // WindowContext. Out-of-process WindowContexts are supported, and are assumed
    123  // to be cross-origin.
    124  //
    125  // The given WindowContext must be in the same BrowsingContextGroup as this
    126  // window global.
    127  bool IsSameOriginWith(const dom::WindowContext* aOther) const;
    128 
    129  bool SameOriginWithTop();
    130 
    131  // Returns `true` if this WindowGlobal is allowed to navigate the given
    132  // BrowsingContext. BrowsingContexts which are currently out-of-process are
    133  // supported, and assumed to be cross-origin.
    134  //
    135  // The given BrowsingContext must be in the same BrowsingContextGroup as this
    136  // WindowGlobal.
    137  bool CanNavigate(dom::BrowsingContext* aTarget, bool aConsiderOpener = true);
    138 
    139  // Using the rules for choosing a browsing context we try to find
    140  // the browsing context with the given name in the set of
    141  // transitively reachable browsing contexts. Performs access control
    142  // checks with regard to this.
    143  // See
    144  // https://html.spec.whatwg.org/multipage/browsers.html#the-rules-for-choosing-a-browsing-context-given-a-browsing-context-name.
    145  dom::BrowsingContext* FindBrowsingContextWithName(
    146      const nsAString& aName, bool aUseEntryGlobalForAccessCheck = true);
    147 
    148  nsISupports* GetParentObject();
    149  JSObject* WrapObject(JSContext* aCx,
    150                       JS::Handle<JSObject*> aGivenProto) override;
    151 
    152  void UnblockBFCacheFor(BFCacheStatus aStatus);
    153  void BlockBFCacheFor(BFCacheStatus aStatus);
    154 
    155 protected:
    156  const nsACString& GetRemoteType() const override;
    157 
    158  already_AddRefed<JSActor> InitJSActor(JS::Handle<JSObject*> aMaybeActor,
    159                                        const nsACString& aName,
    160                                        ErrorResult& aRv) override;
    161  mozilla::ipc::IProtocol* AsNativeActor() override { return this; }
    162 
    163  // IPC messages
    164  mozilla::ipc::IPCResult RecvRawMessage(
    165      const JSActorMessageMeta& aMeta, JSIPCValue&& aData,
    166      const UniquePtr<ClonedMessageData>& aStack);
    167 
    168  MOZ_CAN_RUN_SCRIPT_BOUNDARY
    169  mozilla::ipc::IPCResult RecvMakeFrameLocal(
    170      const MaybeDiscarded<dom::BrowsingContext>& aFrameContext,
    171      uint64_t aPendingSwitchId);
    172 
    173  MOZ_CAN_RUN_SCRIPT_BOUNDARY
    174  mozilla::ipc::IPCResult RecvMakeFrameRemote(
    175      const MaybeDiscarded<dom::BrowsingContext>& aFrameContext,
    176      ManagedEndpoint<PBrowserBridgeChild>&& aEndpoint, const TabId& aTabId,
    177      const LayersId& aLayersId, MakeFrameRemoteResolver&& aResolve);
    178 
    179  mozilla::ipc::IPCResult RecvDrawSnapshot(const Maybe<IntRect>& aRect,
    180                                           const float& aScale,
    181                                           const nscolor& aBackgroundColor,
    182                                           const uint32_t& aFlags,
    183                                           DrawSnapshotResolver&& aResolve);
    184 
    185  mozilla::ipc::IPCResult RecvDispatchSecurityPolicyViolation(
    186      const nsString& aViolationEventJSON);
    187 
    188  mozilla::ipc::IPCResult RecvSaveStorageAccessPermissionGranted();
    189 
    190  mozilla::ipc::IPCResult RecvAddBlockedFrameNodeByClassifier(
    191      const MaybeDiscardedBrowsingContext& aNode);
    192 
    193  mozilla::ipc::IPCResult RecvResetScalingZoom();
    194 
    195  mozilla::ipc::IPCResult RecvRestoreDocShellState(
    196      const dom::sessionstore::DocShellRestoreState& aState,
    197      RestoreDocShellStateResolver&& aResolve);
    198 
    199  // TODO: Use MOZ_CAN_RUN_SCRIPT when it gains IPDL support (bug 1539864)
    200  MOZ_CAN_RUN_SCRIPT_BOUNDARY mozilla::ipc::IPCResult RecvRestoreTabContent(
    201      dom::SessionStoreRestoreData* aData,
    202      RestoreTabContentResolver&& aResolve);
    203 
    204  mozilla::ipc::IPCResult RecvNotifyPermissionChange(const nsCString& aType,
    205                                                     uint32_t aPermission);
    206 
    207  // TODO: Use MOZ_CAN_RUN_SCRIPT when it gains IPDL support (bug 1539864)
    208  MOZ_CAN_RUN_SCRIPT_BOUNDARY mozilla::ipc::IPCResult RecvProcessCloseRequest(
    209      const MaybeDiscarded<dom::BrowsingContext>& aFrameContext);
    210 
    211  virtual void ActorDestroy(ActorDestroyReason aWhy) override;
    212 
    213 private:
    214  WindowGlobalChild(dom::WindowContext* aWindowContext,
    215                    nsIPrincipal* aPrincipal, nsIURI* aURI);
    216 
    217  ~WindowGlobalChild();
    218 
    219  RefPtr<nsGlobalWindowInner> mWindowGlobal;
    220  RefPtr<dom::WindowContext> mWindowContext;
    221  nsCOMPtr<nsIPrincipal> mDocumentPrincipal;
    222  RefPtr<dom::FeaturePolicy> mContainerFeaturePolicy;
    223  nsCOMPtr<nsIURI> mDocumentURI;
    224  int64_t mBeforeUnloadListeners = 0;
    225 };
    226 
    227 }  // namespace mozilla::dom
    228 
    229 #endif  // !defined(mozilla_dom_WindowGlobalChild_h)