Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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"
8 #include "nsCRT.h"
9 #include "nsIServiceManager.h"
10 #include "nsIPrefService.h"
11 #include "nsIPrefBranch.h"
12 #include "nsIPrincipal.h"
13 #include "nsIURI.h"
15 /**
16 * The Popup Window Manager maintains popup window permissions by website.
17 */
19 static const char kPopupDisablePref[] = "dom.disable_open_during_load";
21 //*****************************************************************************
22 //*** nsPopupWindowManager object management and nsISupports
23 //*****************************************************************************
25 nsPopupWindowManager::nsPopupWindowManager() :
26 mPolicy(ALLOW_POPUP)
27 {
28 }
30 nsPopupWindowManager::~nsPopupWindowManager()
31 {
32 }
34 NS_IMPL_ISUPPORTS(nsPopupWindowManager,
35 nsIPopupWindowManager,
36 nsIObserver,
37 nsISupportsWeakReference)
39 nsresult
40 nsPopupWindowManager::Init()
41 {
42 nsresult rv;
43 mPermissionManager = do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
45 nsCOMPtr<nsIPrefBranch> prefBranch =
46 do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
47 if (NS_SUCCEEDED(rv)) {
48 bool permission;
49 rv = prefBranch->GetBoolPref(kPopupDisablePref, &permission);
50 if (NS_FAILED(rv)) {
51 permission = true;
52 }
53 mPolicy = permission ? (uint32_t) DENY_POPUP : (uint32_t) ALLOW_POPUP;
55 prefBranch->AddObserver(kPopupDisablePref, this, true);
56 }
58 return NS_OK;
59 }
61 //*****************************************************************************
62 //*** nsPopupWindowManager::nsIPopupWindowManager
63 //*****************************************************************************
65 NS_IMETHODIMP
66 nsPopupWindowManager::TestPermission(nsIPrincipal* aPrincipal,
67 uint32_t *aPermission)
68 {
69 NS_ENSURE_ARG_POINTER(aPrincipal);
70 NS_ENSURE_ARG_POINTER(aPermission);
72 uint32_t permit;
73 *aPermission = mPolicy;
75 if (mPermissionManager) {
76 if (NS_SUCCEEDED(mPermissionManager->TestPermissionFromPrincipal(aPrincipal, "popup", &permit))) {
77 // Share some constants between interfaces?
78 if (permit == nsIPermissionManager::ALLOW_ACTION) {
79 *aPermission = ALLOW_POPUP;
80 } else if (permit == nsIPermissionManager::DENY_ACTION) {
81 *aPermission = DENY_POPUP;
82 }
83 }
84 }
86 return NS_OK;
87 }
89 //*****************************************************************************
90 //*** nsPopupWindowManager::nsIObserver
91 //*****************************************************************************
92 NS_IMETHODIMP
93 nsPopupWindowManager::Observe(nsISupports *aSubject,
94 const char *aTopic,
95 const char16_t *aData)
96 {
97 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(aSubject);
98 NS_ASSERTION(!nsCRT::strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic),
99 "unexpected topic - we only deal with pref changes!");
101 if (prefBranch) {
102 // refresh our local copy of the "disable popups" pref
103 bool permission = true;
104 prefBranch->GetBoolPref(kPopupDisablePref, &permission);
106 mPolicy = permission ? (uint32_t) DENY_POPUP : (uint32_t) ALLOW_POPUP;
107 }
109 return NS_OK;
110 }