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_PopupBlocker_h
8 #define mozilla_dom_PopupBlocker_h
11 #include "mozilla/BasicEvents.h"
18 class PopupBlocker final
{
20 // Popup control state enum. The values in this enum must go from most
21 // permissive to least permissive so that it's safe to push state in
22 // all situations. Pushing popup state onto the stack never makes the
23 // current popup state less permissive.
24 // Keep this in sync with PopupBlockerState webidl dictionary!
25 enum PopupControlState
{
26 openAllowed
= 0, // open that window without worries
27 openControlled
, // it's a popup, but allow it
28 openBlocked
, // it's a popup, but not from an allowed event
29 openAbused
, // it's a popup. disallow it, but allow domain override.
30 openOverridden
// disallow window open
33 static PopupControlState
PushPopupControlState(PopupControlState aState
,
36 static void PopPopupControlState(PopupControlState aState
);
38 static PopupControlState
GetPopupControlState();
40 static void PopupStatePusherCreated();
41 static void PopupStatePusherDestroyed();
43 // This method checks if the principal is allowed by open popups by user
44 // permissions. In this case, the caller should not block popups.
45 static bool CanShowPopupByPermission(nsIPrincipal
* aPrincipal
);
47 static uint32_t GetPopupPermission(nsIPrincipal
* aPrincipal
);
49 // This method returns true if the caller is allowed to show a popup, and it
50 // consumes the popup token for the current event. There is just 1 popup
52 // This method returns true if the token has been already consumed but
53 // aPrincipal is the system principal.
54 static bool TryUsePopupOpeningToken(nsIPrincipal
* aPrincipal
);
56 static bool IsPopupOpeningTokenUnused();
58 static PopupBlocker::PopupControlState
GetEventPopupControlState(
59 WidgetEvent
* aEvent
, Event
* aDOMEvent
= nullptr);
61 // Returns if a external protocol iframe is allowed.
62 static bool ConsumeTimerTokenForExternalProtocolIframe();
64 // Returns when the last external protocol iframe has been allowed.
65 static TimeStamp
WhenLastExternalProtocolIframeAllowed();
67 // Reset the last external protocol iframe timestamp.
68 static void ResetLastExternalProtocolIframeAllowed();
70 // These method track the number of popup which is considered as a spam popup.
71 static void RegisterOpenPopupSpam();
72 static void UnregisterOpenPopupSpam();
73 static uint32_t GetOpenPopupSpamCount();
75 static void Initialize();
76 static void Shutdown();
80 } // namespace mozilla
82 #ifdef MOZILLA_INTERNAL_API
83 # define AUTO_POPUP_STATE_PUSHER AutoPopupStatePusherInternal
85 # define AUTO_POPUP_STATE_PUSHER AutoPopupStatePusherExternal
88 // Helper class that helps with pushing and popping popup control
89 // state. Note that this class looks different from within code that's
90 // part of the layout library than it does in code outside the layout
91 // library. We give the two object layouts different names so the symbols
92 // don't conflict, but code should always use the name
93 // |AutoPopupStatePusher|.
94 class MOZ_RAII AUTO_POPUP_STATE_PUSHER final
{
96 #ifdef MOZILLA_INTERNAL_API
97 explicit AUTO_POPUP_STATE_PUSHER(
98 mozilla::dom::PopupBlocker::PopupControlState aState
,
100 ~AUTO_POPUP_STATE_PUSHER();
102 AUTO_POPUP_STATE_PUSHER(nsPIDOMWindowOuter
* aWindow
,
103 mozilla::dom::PopupBlocker::PopupControlState aState
)
104 : mWindow(aWindow
), mOldState(openAbused
) {
106 mOldState
= PopupBlocker::PushPopupControlState(aState
, false);
110 ~AUTO_POPUP_STATE_PUSHER() {
112 PopupBlocker::PopPopupControlState(mOldState
);
118 #ifndef MOZILLA_INTERNAL_API
119 nsCOMPtr
<nsPIDOMWindowOuter
> mWindow
;
121 mozilla::dom::PopupBlocker::PopupControlState mOldState
;
124 #define AutoPopupStatePusher AUTO_POPUP_STATE_PUSHER
126 #endif // mozilla_PopupBlocker_h