Bug 1686668 [wpt PR 27185] - Update wpt metadata, a=testonly
[gecko.git] / dom / base / PostMessageEvent.h
blob66dc029d1d62c2f788996efcd0a9a4025decb00a
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/. */
7 #ifndef mozilla_dom_PostMessageEvent_h
8 #define mozilla_dom_PostMessageEvent_h
10 #include "js/StructuredClone.h"
11 #include "js/TypeDecls.h"
12 #include "mozilla/dom/DOMTypes.h"
13 #include "mozilla/dom/ipc/StructuredCloneData.h"
14 #include "mozilla/dom/StructuredCloneHolder.h"
15 #include "nsCOMPtr.h"
16 #include "mozilla/Maybe.h"
17 #include "mozilla/MaybeOneOf.h"
18 #include "mozilla/RefPtr.h"
19 #include "nsThreadUtils.h"
21 class nsGlobalWindowOuter;
22 class nsGlobalWindowInner;
23 class nsIPrincipal;
24 class nsIURI;
26 namespace mozilla {
27 class ErrorResult;
29 namespace dom {
31 class BrowsingContext;
32 class Event;
33 class EventTarget;
35 /**
36 * Class used to represent events generated by calls to Window.postMessage,
37 * which asynchronously creates and dispatches events.
39 class PostMessageEvent final : public Runnable {
40 public:
41 NS_DECL_NSIRUNNABLE
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) {}
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()) {}
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);
73 void UnpackFrom(const ClonedOrErrorMessageData& aMessageData) {
74 if (aMessageData.type() != ClonedOrErrorMessageData::TClonedMessageData) {
75 return;
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>().CopyFromClonedMessageDataForChild(
82 aMessageData);
85 void DispatchToTargetThread(ErrorResult& aError);
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();
96 void Dispatch(nsGlobalWindowInner* aTargetWindow, Event* aEvent);
98 void DispatchError(JSContext* aCx, nsGlobalWindowInner* aTargetWindow,
99 mozilla::dom::EventTarget* aEventTarget);
101 RefPtr<BrowsingContext> mSource;
102 nsString mCallerOrigin;
103 RefPtr<nsGlobalWindowOuter> mTargetWindow;
104 nsCOMPtr<nsIPrincipal> mProvidedPrincipal;
105 // If the postMessage call was made on a WindowProxy whose Window lives in a
106 // separate process then mHolder will contain a StructuredCloneData, else
107 // it'll contain a StructuredCloneHolder.
108 MaybeOneOf<StructuredCloneHolder, ipc::StructuredCloneData> mHolder;
109 uint64_t mCallerWindowID;
110 const Maybe<nsID> mCallerAgentClusterId;
111 nsCOMPtr<nsIURI> mCallerURI;
112 // if callerURI is null, then we can use script location for reporting errors
113 // to console
114 const Maybe<nsCString> mScriptLocation;
115 // This is only set to a relevant value if mCallerWindowID doesn't contain a
116 // value.
117 bool mIsFromPrivateWindow;
120 } // namespace dom
121 } // namespace mozilla
123 #endif // mozilla_dom_PostMessageEvent_h