PopupBlocker.h (3891B)
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_PopupBlocker_h 8 #define mozilla_dom_PopupBlocker_h 9 10 #include <stdint.h> 11 12 #include "mozilla/TimeStamp.h" 13 14 class AutoPopupStatePusherInternal; 15 class nsIPrincipal; 16 17 namespace mozilla { 18 class WidgetEvent; 19 namespace dom { 20 class Event; 21 22 class PopupBlocker final { 23 public: 24 // Popup control state enum. The values in this enum must go from most 25 // permissive to least permissive so that it's safe to push state in 26 // all situations. Pushing popup state onto the stack never makes the 27 // current popup state less permissive. 28 // Keep this in sync with PopupBlockerState webidl dictionary! 29 enum PopupControlState { 30 openAllowed = 0, // open that window without worries 31 openControlled, // it's a popup, but allow it 32 openBlocked, // it's a popup, but not from an allowed event 33 openAbused, // it's a popup. disallow it, but allow domain override. 34 openOverridden // disallow window open 35 }; 36 37 static PopupControlState PushPopupControlState(PopupControlState aState, 38 bool aForce); 39 40 static void PopPopupControlState(PopupControlState aState); 41 42 static PopupControlState GetPopupControlState(); 43 44 static void PopupStatePusherCreated(); 45 static void PopupStatePusherDestroyed(); 46 47 static uint32_t GetPopupPermission(nsIPrincipal* aPrincipal); 48 49 static PopupBlocker::PopupControlState GetEventPopupControlState( 50 WidgetEvent* aEvent, Event* aDOMEvent = nullptr); 51 52 // Returns if a external protocol iframe is allowed. 53 static bool ConsumeTimerTokenForExternalProtocolIframe(); 54 55 // Returns when the last external protocol iframe has been allowed. 56 static TimeStamp WhenLastExternalProtocolIframeAllowed(); 57 58 // Reset the last external protocol iframe timestamp. 59 static void ResetLastExternalProtocolIframeAllowed(); 60 61 // These method track the number of popup which is considered as a spam popup. 62 static void RegisterOpenPopupSpam(); 63 static void UnregisterOpenPopupSpam(); 64 static uint32_t GetOpenPopupSpamCount(); 65 66 static void Initialize(); 67 static void Shutdown(); 68 }; 69 70 } // namespace dom 71 } // namespace mozilla 72 73 #ifdef MOZILLA_INTERNAL_API 74 # define AUTO_POPUP_STATE_PUSHER AutoPopupStatePusherInternal 75 #else 76 # define AUTO_POPUP_STATE_PUSHER AutoPopupStatePusherExternal 77 #endif 78 79 // Helper class that helps with pushing and popping popup control 80 // state. Note that this class looks different from within code that's 81 // part of the layout library than it does in code outside the layout 82 // library. We give the two object layouts different names so the symbols 83 // don't conflict, but code should always use the name 84 // |AutoPopupStatePusher|. 85 class MOZ_RAII AUTO_POPUP_STATE_PUSHER final { 86 public: 87 #ifdef MOZILLA_INTERNAL_API 88 explicit AUTO_POPUP_STATE_PUSHER( 89 mozilla::dom::PopupBlocker::PopupControlState aState, 90 bool aForce = false); 91 ~AUTO_POPUP_STATE_PUSHER(); 92 #else 93 AUTO_POPUP_STATE_PUSHER(nsPIDOMWindowOuter* aWindow, 94 mozilla::dom::PopupBlocker::PopupControlState aState) 95 : mWindow(aWindow), mOldState(openAbused) { 96 if (aWindow) { 97 mOldState = PopupBlocker::PushPopupControlState(aState, false); 98 } 99 } 100 101 ~AUTO_POPUP_STATE_PUSHER() { 102 if (mWindow) { 103 PopupBlocker::PopPopupControlState(mOldState); 104 } 105 } 106 #endif 107 108 protected: 109 #ifndef MOZILLA_INTERNAL_API 110 nsCOMPtr<nsPIDOMWindowOuter> mWindow; 111 #endif 112 mozilla::dom::PopupBlocker::PopupControlState mOldState; 113 }; 114 115 #define AutoPopupStatePusher AUTO_POPUP_STATE_PUSHER 116 117 #endif // mozilla_PopupBlocker_h