summaryrefslogtreecommitdiffstats
path: root/dom/base/PopupBlocker.h
blob: bc6a5daf9751f59f30a792fea671947fd7dad839 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_PopupBlocker_h
#define mozilla_dom_PopupBlocker_h

#include <stdint.h>
#include "mozilla/Attributes.h"
#include "mozilla/TimeStamp.h"

class AutoPopupStatePusherInternal;
class nsIPrincipal;

namespace mozilla {
class WidgetEvent;
namespace dom {
class Event;

class PopupBlocker final {
 public:
  // Popup control state enum. The values in this enum must go from most
  // permissive to least permissive so that it's safe to push state in
  // all situations. Pushing popup state onto the stack never makes the
  // current popup state less permissive.
  // Keep this in sync with PopupBlockerState webidl dictionary!
  enum PopupControlState {
    openAllowed = 0,  // open that window without worries
    openControlled,   // it's a popup, but allow it
    openBlocked,      // it's a popup, but not from an allowed event
    openAbused,       // it's a popup. disallow it, but allow domain override.
    openOverridden    // disallow window open
  };

  static PopupControlState PushPopupControlState(PopupControlState aState,
                                                 bool aForce);

  static void PopPopupControlState(PopupControlState aState);

  static PopupControlState GetPopupControlState();

  static void PopupStatePusherCreated();
  static void PopupStatePusherDestroyed();

  static uint32_t GetPopupPermission(nsIPrincipal* aPrincipal);

  static PopupBlocker::PopupControlState GetEventPopupControlState(
      WidgetEvent* aEvent, Event* aDOMEvent = nullptr);

  // Returns if a external protocol iframe is allowed.
  static bool ConsumeTimerTokenForExternalProtocolIframe();

  // Returns when the last external protocol iframe has been allowed.
  static TimeStamp WhenLastExternalProtocolIframeAllowed();

  // Reset the last external protocol iframe timestamp.
  static void ResetLastExternalProtocolIframeAllowed();

  // These method track the number of popup which is considered as a spam popup.
  static void RegisterOpenPopupSpam();
  static void UnregisterOpenPopupSpam();
  static uint32_t GetOpenPopupSpamCount();

  static void Initialize();
  static void Shutdown();
};

}  // namespace dom
}  // namespace mozilla

#ifdef MOZILLA_INTERNAL_API
#  define AUTO_POPUP_STATE_PUSHER AutoPopupStatePusherInternal
#else
#  define AUTO_POPUP_STATE_PUSHER AutoPopupStatePusherExternal
#endif

// Helper class that helps with pushing and popping popup control
// state. Note that this class looks different from within code that's
// part of the layout library than it does in code outside the layout
// library.  We give the two object layouts different names so the symbols
// don't conflict, but code should always use the name
// |AutoPopupStatePusher|.
class MOZ_RAII AUTO_POPUP_STATE_PUSHER final {
 public:
#ifdef MOZILLA_INTERNAL_API
  explicit AUTO_POPUP_STATE_PUSHER(
      mozilla::dom::PopupBlocker::PopupControlState aState,
      bool aForce = false);
  ~AUTO_POPUP_STATE_PUSHER();
#else
  AUTO_POPUP_STATE_PUSHER(nsPIDOMWindowOuter* aWindow,
                          mozilla::dom::PopupBlocker::PopupControlState aState)
      : mWindow(aWindow), mOldState(openAbused) {
    if (aWindow) {
      mOldState = PopupBlocker::PushPopupControlState(aState, false);
    }
  }

  ~AUTO_POPUP_STATE_PUSHER() {
    if (mWindow) {
      PopupBlocker::PopPopupControlState(mOldState);
    }
  }
#endif

 protected:
#ifndef MOZILLA_INTERNAL_API
  nsCOMPtr<nsPIDOMWindowOuter> mWindow;
#endif
  mozilla::dom::PopupBlocker::PopupControlState mOldState;
};

#define AutoPopupStatePusher AUTO_POPUP_STATE_PUSHER

#endif  // mozilla_PopupBlocker_h