1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsPopupWindowManager.h"
9 #include "nsIServiceManager.h"
10 #include "nsIPrefService.h"
11 #include "nsIPrefBranch.h"
12 #include "nsIPrincipal.h"
14 #include "mozilla/Services.h"
17 * The Popup Window Manager maintains popup window permissions by website.
20 static const char kPopupDisablePref
[] = "dom.disable_open_during_load";
22 //*****************************************************************************
23 //*** nsPopupWindowManager object management and nsISupports
24 //*****************************************************************************
26 nsPopupWindowManager::nsPopupWindowManager() :
31 nsPopupWindowManager::~nsPopupWindowManager()
35 NS_IMPL_ISUPPORTS(nsPopupWindowManager
,
36 nsIPopupWindowManager
,
38 nsISupportsWeakReference
)
41 nsPopupWindowManager::Init()
44 mPermissionManager
= mozilla::services::GetPermissionManager();
46 nsCOMPtr
<nsIPrefBranch
> prefBranch
=
47 do_GetService(NS_PREFSERVICE_CONTRACTID
, &rv
);
48 if (NS_SUCCEEDED(rv
)) {
50 rv
= prefBranch
->GetBoolPref(kPopupDisablePref
, &permission
);
54 mPolicy
= permission
? (uint32_t) DENY_POPUP
: (uint32_t) ALLOW_POPUP
;
56 prefBranch
->AddObserver(kPopupDisablePref
, this, true);
62 //*****************************************************************************
63 //*** nsPopupWindowManager::nsIPopupWindowManager
64 //*****************************************************************************
67 nsPopupWindowManager::TestPermission(nsIPrincipal
* aPrincipal
,
68 uint32_t *aPermission
)
70 NS_ENSURE_ARG_POINTER(aPrincipal
);
71 NS_ENSURE_ARG_POINTER(aPermission
);
74 *aPermission
= mPolicy
;
76 if (mPermissionManager
) {
77 if (NS_SUCCEEDED(mPermissionManager
->TestPermissionFromPrincipal(aPrincipal
, "popup", &permit
))) {
78 // Share some constants between interfaces?
79 if (permit
== nsIPermissionManager::ALLOW_ACTION
) {
80 *aPermission
= ALLOW_POPUP
;
81 } else if (permit
== nsIPermissionManager::DENY_ACTION
) {
82 *aPermission
= DENY_POPUP
;
90 //*****************************************************************************
91 //*** nsPopupWindowManager::nsIObserver
92 //*****************************************************************************
94 nsPopupWindowManager::Observe(nsISupports
*aSubject
,
96 const char16_t
*aData
)
98 nsCOMPtr
<nsIPrefBranch
> prefBranch
= do_QueryInterface(aSubject
);
99 NS_ASSERTION(!nsCRT::strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID
, aTopic
),
100 "unexpected topic - we only deal with pref changes!");
103 // refresh our local copy of the "disable popups" pref
104 bool permission
= true;
105 prefBranch
->GetBoolPref(kPopupDisablePref
, &permission
);
107 mPolicy
= permission
? (uint32_t) DENY_POPUP
: (uint32_t) ALLOW_POPUP
;