extensions/cookie/nsPopupWindowManager.cpp

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:7b42257ab68e
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/. */
5
6 #include "nsPopupWindowManager.h"
7
8 #include "nsCRT.h"
9 #include "nsIServiceManager.h"
10 #include "nsIPrefService.h"
11 #include "nsIPrefBranch.h"
12 #include "nsIPrincipal.h"
13 #include "nsIURI.h"
14
15 /**
16 * The Popup Window Manager maintains popup window permissions by website.
17 */
18
19 static const char kPopupDisablePref[] = "dom.disable_open_during_load";
20
21 //*****************************************************************************
22 //*** nsPopupWindowManager object management and nsISupports
23 //*****************************************************************************
24
25 nsPopupWindowManager::nsPopupWindowManager() :
26 mPolicy(ALLOW_POPUP)
27 {
28 }
29
30 nsPopupWindowManager::~nsPopupWindowManager()
31 {
32 }
33
34 NS_IMPL_ISUPPORTS(nsPopupWindowManager,
35 nsIPopupWindowManager,
36 nsIObserver,
37 nsISupportsWeakReference)
38
39 nsresult
40 nsPopupWindowManager::Init()
41 {
42 nsresult rv;
43 mPermissionManager = do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
44
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;
54
55 prefBranch->AddObserver(kPopupDisablePref, this, true);
56 }
57
58 return NS_OK;
59 }
60
61 //*****************************************************************************
62 //*** nsPopupWindowManager::nsIPopupWindowManager
63 //*****************************************************************************
64
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);
71
72 uint32_t permit;
73 *aPermission = mPolicy;
74
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 }
85
86 return NS_OK;
87 }
88
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!");
100
101 if (prefBranch) {
102 // refresh our local copy of the "disable popups" pref
103 bool permission = true;
104 prefBranch->GetBoolPref(kPopupDisablePref, &permission);
105
106 mPolicy = permission ? (uint32_t) DENY_POPUP : (uint32_t) ALLOW_POPUP;
107 }
108
109 return NS_OK;
110 }

mercurial