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 nsContentPermissionHelper_h
8 #define nsContentPermissionHelper_h
10 #include "nsIContentPermissionPrompt.h"
12 #include "nsIMutableArray.h"
13 #include "mozilla/dom/PContentPermissionRequestChild.h"
14 #include "mozilla/dom/ipc/IdType.h"
15 #include "mozilla/PermissionDelegateHandler.h"
17 // Microsoft's API Name hackery sucks
18 // XXXbz Doing this in a header is a gigantic footgun. See
19 // https://bugzilla.mozilla.org/show_bug.cgi?id=932421#c3 for why.
22 class nsPIDOMWindowInner
;
23 class nsContentPermissionRequestProxy
;
25 // Forward declare IPC::Principal here which is defined in
26 // PermissionMessageUtils.h. Include this file will transitively includes
27 // "windows.h" and it defines
28 // #define CreateEvent CreateEventW
29 // #define LoadImage LoadImageW
30 // That will mess up windows build.
39 class PermissionRequest
;
40 class ContentPermissionRequestParent
;
41 class PContentPermissionRequestParent
;
43 class ContentPermissionType
: public nsIContentPermissionType
{
46 NS_DECL_NSICONTENTPERMISSIONTYPE
48 ContentPermissionType(const nsACString
& aType
,
49 const nsTArray
<nsString
>& aOptions
);
52 virtual ~ContentPermissionType();
55 nsTArray
<nsString
> mOptions
;
58 class nsContentPermissionUtils
{
60 static uint32_t ConvertPermissionRequestToArray(
61 nsTArray
<PermissionRequest
>& aSrcArray
, nsIMutableArray
* aDesArray
);
63 static uint32_t ConvertArrayToPermissionRequest(
64 nsIArray
* aSrcArray
, nsTArray
<PermissionRequest
>& aDesArray
);
66 static nsresult
CreatePermissionArray(const nsACString
& aType
,
67 const nsTArray
<nsString
>& aOptions
,
68 nsIArray
** aTypesArray
);
70 static PContentPermissionRequestParent
* CreateContentPermissionRequestParent(
71 const nsTArray
<PermissionRequest
>& aRequests
, Element
* aElement
,
72 nsIPrincipal
* aPrincipal
, nsIPrincipal
* aTopLevelPrincipal
,
73 const bool aIsHandlingUserInput
,
74 const bool aMaybeUnsafePermissionDelegate
, const TabId
& aTabId
);
76 static nsresult
AskPermission(nsIContentPermissionRequest
* aRequest
,
77 nsPIDOMWindowInner
* aWindow
);
79 static nsTArray
<PContentPermissionRequestParent
*>
80 GetContentPermissionRequestParentById(const TabId
& aTabId
);
82 static void NotifyRemoveContentPermissionRequestParent(
83 PContentPermissionRequestParent
* aParent
);
85 static nsTArray
<PContentPermissionRequestChild
*>
86 GetContentPermissionRequestChildById(const TabId
& aTabId
);
88 static void NotifyRemoveContentPermissionRequestChild(
89 PContentPermissionRequestChild
* aChild
);
92 nsresult
TranslateChoices(
93 JS::HandleValue aChoices
,
94 const nsTArray
<PermissionRequest
>& aPermissionRequests
,
95 nsTArray
<PermissionChoice
>& aTranslatedChoices
);
97 class ContentPermissionRequestBase
: public nsIContentPermissionRequest
{
99 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
100 NS_DECL_CYCLE_COLLECTION_CLASS(ContentPermissionRequestBase
)
102 NS_IMETHOD
GetTypes(nsIArray
** aTypes
) override
;
103 NS_IMETHOD
GetPrincipal(nsIPrincipal
** aPrincipal
) override
;
104 NS_IMETHOD
GetDelegatePrincipal(const nsACString
& aType
,
105 nsIPrincipal
** aPrincipal
) override
;
106 NS_IMETHOD
GetTopLevelPrincipal(nsIPrincipal
** aTopLevelPrincipal
) override
;
107 NS_IMETHOD
GetWindow(mozIDOMWindow
** aWindow
) override
;
108 NS_IMETHOD
GetElement(mozilla::dom::Element
** aElement
) override
;
109 NS_IMETHOD
GetIsHandlingUserInput(bool* aIsHandlingUserInput
) override
;
110 NS_IMETHOD
GetMaybeUnsafePermissionDelegate(
111 bool* aMaybeUnsafePermissionDelegate
) override
;
112 // Overrides for Allow() and Cancel() aren't provided by this class.
113 // That is the responsibility of the subclasses.
115 enum class PromptResult
{
120 nsresult
ShowPrompt(PromptResult
& aResult
);
122 PromptResult
CheckPromptPrefs();
124 // Check if the permission has an opportunity to request.
125 bool CheckPermissionDelegate();
127 enum class DelayedTaskType
{
132 void RequestDelayedTask(nsIEventTarget
* aTarget
, DelayedTaskType aType
);
135 ContentPermissionRequestBase(nsIPrincipal
* aPrincipal
,
136 nsPIDOMWindowInner
* aWindow
,
137 const nsACString
& aPrefName
,
138 const nsACString
& aType
);
139 virtual ~ContentPermissionRequestBase() = default;
141 nsCOMPtr
<nsIPrincipal
> mPrincipal
;
142 nsCOMPtr
<nsIPrincipal
> mTopLevelPrincipal
;
143 nsCOMPtr
<nsPIDOMWindowInner
> mWindow
;
144 RefPtr
<PermissionDelegateHandler
> mPermissionHandler
;
147 bool mIsHandlingUserInput
;
148 bool mMaybeUnsafePermissionDelegate
;
152 } // namespace mozilla
154 using mozilla::dom::ContentPermissionRequestParent
;
156 class nsContentPermissionRequestProxy
: public nsIContentPermissionRequest
{
159 NS_DECL_NSICONTENTPERMISSIONREQUEST
161 explicit nsContentPermissionRequestProxy(
162 ContentPermissionRequestParent
* parent
);
164 nsresult
Init(const nsTArray
<mozilla::dom::PermissionRequest
>& requests
);
166 void OnParentDestroyed();
169 virtual ~nsContentPermissionRequestProxy();
171 // Non-owning pointer to the ContentPermissionRequestParent object which owns
173 ContentPermissionRequestParent
* mParent
;
174 nsTArray
<mozilla::dom::PermissionRequest
> mPermissionRequests
;
178 * RemotePermissionRequest will send a prompt ipdl request to b2g process.
180 class RemotePermissionRequest final
181 : public mozilla::dom::PContentPermissionRequestChild
{
183 NS_INLINE_DECL_REFCOUNTING(RemotePermissionRequest
)
185 RemotePermissionRequest(nsIContentPermissionRequest
* aRequest
,
186 nsPIDOMWindowInner
* aWindow
);
188 // It will be called when prompt dismissed. MOZ_CAN_RUN_SCRIPT_BOUNDARY
189 // because we don't have MOZ_CAN_RUN_SCRIPT bits in IPC code yet.
190 MOZ_CAN_RUN_SCRIPT_BOUNDARY
191 mozilla::ipc::IPCResult
RecvNotifyResult(
192 const bool& aAllow
, nsTArray
<PermissionChoice
>&& aChoices
);
206 bool IPCOpen() const { return mIPCOpen
&& !mDestroyed
; }
209 virtual ~RemotePermissionRequest();
212 void DoAllow(JS::HandleValue aChoices
);
216 nsCOMPtr
<nsIContentPermissionRequest
> mRequest
;
217 nsCOMPtr
<nsPIDOMWindowInner
> mWindow
;
222 #endif // nsContentPermissionHelper_h