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"
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
;
31 class BrowsingContext
;
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
{
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
) {
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(
85 void DispatchToTargetThread(ErrorResult
& aError
);
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
);
96 MOZ_CAN_RUN_SCRIPT
void Dispatch(nsGlobalWindowInner
* aTargetWindow
,
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
);
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
117 const Maybe
<nsCString
> mScriptLocation
;
118 // This is only set to a relevant value if mCallerWindowID doesn't contain a
120 bool mIsFromPrivateWindow
;
124 } // namespace mozilla
126 #endif // mozilla_dom_PostMessageEvent_h