tor-browser

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

PostMessageEvent.h (5002B)


      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
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef mozilla_dom_PostMessageEvent_h
      8 #define mozilla_dom_PostMessageEvent_h
      9 
     10 #include "js/StructuredClone.h"
     11 #include "js/TypeDecls.h"
     12 #include "mozilla/Maybe.h"
     13 #include "mozilla/MaybeOneOf.h"
     14 #include "mozilla/RefPtr.h"
     15 #include "mozilla/dom/DOMTypes.h"
     16 #include "mozilla/dom/StructuredCloneHolder.h"
     17 #include "mozilla/dom/ipc/StructuredCloneData.h"
     18 #include "nsCOMPtr.h"
     19 #include "nsThreadUtils.h"
     20 
     21 class nsGlobalWindowOuter;
     22 class nsGlobalWindowInner;
     23 class nsIPrincipal;
     24 class nsIURI;
     25 
     26 namespace mozilla {
     27 class ErrorResult;
     28 
     29 namespace dom {
     30 
     31 class BrowsingContext;
     32 class Event;
     33 class EventTarget;
     34 
     35 /**
     36 * Class used to represent events generated by calls to Window.postMessage,
     37 * which asynchronously creates and dispatches events.
     38 */
     39 class PostMessageEvent final : public Runnable {
     40 public:
     41  NS_DECL_NSIRUNNABLE
     42 
     43  // aCallerWindowID should not be 0.
     44  PostMessageEvent(BrowsingContext* aSource, const nsAString& aCallerOrigin,
     45                   nsGlobalWindowOuter* aTargetWindow,
     46                   nsIPrincipal* aProvidedPrincipal, uint64_t aCallerWindowID,
     47                   nsIURI* aCallerURI, const nsCString& aScriptLocation,
     48                   const Maybe<nsID>& aCallerAgentClusterId)
     49      : PostMessageEvent(aSource, aCallerOrigin, aTargetWindow,
     50                         aProvidedPrincipal, aCallerWindowID, aCallerURI,
     51                         aScriptLocation, false, aCallerAgentClusterId) {}
     52 
     53  // To be used when the caller's window lives in a different process.
     54  PostMessageEvent(BrowsingContext* aSource, const nsAString& aCallerOrigin,
     55                   nsGlobalWindowOuter* aTargetWindow,
     56                   nsIPrincipal* aProvidedPrincipal, uint64_t aCallerWindowID,
     57                   nsIURI* aCallerURI, const nsCString& aScriptLocation,
     58                   bool aIsFromPrivateWindow)
     59      : PostMessageEvent(aSource, aCallerOrigin, aTargetWindow,
     60                         aProvidedPrincipal, aCallerWindowID, aCallerURI,
     61                         aScriptLocation, aIsFromPrivateWindow, Nothing()) {}
     62 
     63  void Write(JSContext* aCx, JS::Handle<JS::Value> aMessage,
     64             JS::Handle<JS::Value> aTransfer,
     65             const JS::CloneDataPolicy& aClonePolicy, ErrorResult& aError) {
     66    mHolder.construct<StructuredCloneHolder>(
     67        StructuredCloneHolder::CloningSupported,
     68        StructuredCloneHolder::TransferringSupported,
     69        JS::StructuredCloneScope::SameProcess);
     70    mHolder.ref<StructuredCloneHolder>().Write(aCx, aMessage, aTransfer,
     71                                               aClonePolicy, aError);
     72  }
     73  void UnpackFrom(const ClonedOrErrorMessageData& aMessageData) {
     74    if (aMessageData.type() != ClonedOrErrorMessageData::TClonedMessageData) {
     75      return;
     76    }
     77 
     78    mHolder.construct<ipc::StructuredCloneData>();
     79    // FIXME Want to steal!
     80    //       See https://bugzilla.mozilla.org/show_bug.cgi?id=1516349.
     81    mHolder.ref<ipc::StructuredCloneData>().CopyFromClonedMessageData(
     82        aMessageData);
     83  }
     84 
     85  void DispatchToTargetThread(ErrorResult& aError);
     86 
     87 private:
     88  PostMessageEvent(BrowsingContext* aSource, const nsAString& aCallerOrigin,
     89                   nsGlobalWindowOuter* aTargetWindow,
     90                   nsIPrincipal* aProvidedPrincipal, uint64_t aCallerWindowID,
     91                   nsIURI* aCallerURI, const nsCString& aScriptLocation,
     92                   bool aIsFromPrivateWindow,
     93                   const Maybe<nsID>& aCallerAgentClusterId);
     94  ~PostMessageEvent();
     95 
     96  MOZ_CAN_RUN_SCRIPT void Dispatch(nsGlobalWindowInner* aTargetWindow,
     97                                   Event* aEvent);
     98 
     99  // TODO: Convert this to MOZ_CAN_RUN_SCRIPT (bug 1415230)
    100  MOZ_CAN_RUN_SCRIPT_BOUNDARY void DispatchError(
    101      JSContext* aCx, nsGlobalWindowInner* aTargetWindow,
    102      mozilla::dom::EventTarget* aEventTarget);
    103 
    104  RefPtr<BrowsingContext> mSource;
    105  nsString mCallerOrigin;
    106  RefPtr<nsGlobalWindowOuter> mTargetWindow;
    107  nsCOMPtr<nsIPrincipal> mProvidedPrincipal;
    108  // If the postMessage call was made on a WindowProxy whose Window lives in a
    109  // separate process then mHolder will contain a StructuredCloneData, else
    110  // it'll contain a StructuredCloneHolder.
    111  MaybeOneOf<StructuredCloneHolder, ipc::StructuredCloneData> mHolder;
    112  uint64_t mCallerWindowID;
    113  const Maybe<nsID> mCallerAgentClusterId;
    114  nsCOMPtr<nsIURI> mCallerURI;
    115  // if callerURI is null, then we can use script location for reporting errors
    116  // to console
    117  const Maybe<nsCString> mScriptLocation;
    118  // This is only set to a relevant value if mCallerWindowID doesn't contain a
    119  // value.
    120  bool mIsFromPrivateWindow;
    121 };
    122 
    123 }  // namespace dom
    124 }  // namespace mozilla
    125 
    126 #endif  // mozilla_dom_PostMessageEvent_h