Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsPopupWindowManager.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "nsCRT.h" |
michael@0 | 9 | #include "nsIServiceManager.h" |
michael@0 | 10 | #include "nsIPrefService.h" |
michael@0 | 11 | #include "nsIPrefBranch.h" |
michael@0 | 12 | #include "nsIPrincipal.h" |
michael@0 | 13 | #include "nsIURI.h" |
michael@0 | 14 | |
michael@0 | 15 | /** |
michael@0 | 16 | * The Popup Window Manager maintains popup window permissions by website. |
michael@0 | 17 | */ |
michael@0 | 18 | |
michael@0 | 19 | static const char kPopupDisablePref[] = "dom.disable_open_during_load"; |
michael@0 | 20 | |
michael@0 | 21 | //***************************************************************************** |
michael@0 | 22 | //*** nsPopupWindowManager object management and nsISupports |
michael@0 | 23 | //***************************************************************************** |
michael@0 | 24 | |
michael@0 | 25 | nsPopupWindowManager::nsPopupWindowManager() : |
michael@0 | 26 | mPolicy(ALLOW_POPUP) |
michael@0 | 27 | { |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | nsPopupWindowManager::~nsPopupWindowManager() |
michael@0 | 31 | { |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | NS_IMPL_ISUPPORTS(nsPopupWindowManager, |
michael@0 | 35 | nsIPopupWindowManager, |
michael@0 | 36 | nsIObserver, |
michael@0 | 37 | nsISupportsWeakReference) |
michael@0 | 38 | |
michael@0 | 39 | nsresult |
michael@0 | 40 | nsPopupWindowManager::Init() |
michael@0 | 41 | { |
michael@0 | 42 | nsresult rv; |
michael@0 | 43 | mPermissionManager = do_GetService(NS_PERMISSIONMANAGER_CONTRACTID); |
michael@0 | 44 | |
michael@0 | 45 | nsCOMPtr<nsIPrefBranch> prefBranch = |
michael@0 | 46 | do_GetService(NS_PREFSERVICE_CONTRACTID, &rv); |
michael@0 | 47 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 48 | bool permission; |
michael@0 | 49 | rv = prefBranch->GetBoolPref(kPopupDisablePref, &permission); |
michael@0 | 50 | if (NS_FAILED(rv)) { |
michael@0 | 51 | permission = true; |
michael@0 | 52 | } |
michael@0 | 53 | mPolicy = permission ? (uint32_t) DENY_POPUP : (uint32_t) ALLOW_POPUP; |
michael@0 | 54 | |
michael@0 | 55 | prefBranch->AddObserver(kPopupDisablePref, this, true); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | return NS_OK; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | //***************************************************************************** |
michael@0 | 62 | //*** nsPopupWindowManager::nsIPopupWindowManager |
michael@0 | 63 | //***************************************************************************** |
michael@0 | 64 | |
michael@0 | 65 | NS_IMETHODIMP |
michael@0 | 66 | nsPopupWindowManager::TestPermission(nsIPrincipal* aPrincipal, |
michael@0 | 67 | uint32_t *aPermission) |
michael@0 | 68 | { |
michael@0 | 69 | NS_ENSURE_ARG_POINTER(aPrincipal); |
michael@0 | 70 | NS_ENSURE_ARG_POINTER(aPermission); |
michael@0 | 71 | |
michael@0 | 72 | uint32_t permit; |
michael@0 | 73 | *aPermission = mPolicy; |
michael@0 | 74 | |
michael@0 | 75 | if (mPermissionManager) { |
michael@0 | 76 | if (NS_SUCCEEDED(mPermissionManager->TestPermissionFromPrincipal(aPrincipal, "popup", &permit))) { |
michael@0 | 77 | // Share some constants between interfaces? |
michael@0 | 78 | if (permit == nsIPermissionManager::ALLOW_ACTION) { |
michael@0 | 79 | *aPermission = ALLOW_POPUP; |
michael@0 | 80 | } else if (permit == nsIPermissionManager::DENY_ACTION) { |
michael@0 | 81 | *aPermission = DENY_POPUP; |
michael@0 | 82 | } |
michael@0 | 83 | } |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | return NS_OK; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | //***************************************************************************** |
michael@0 | 90 | //*** nsPopupWindowManager::nsIObserver |
michael@0 | 91 | //***************************************************************************** |
michael@0 | 92 | NS_IMETHODIMP |
michael@0 | 93 | nsPopupWindowManager::Observe(nsISupports *aSubject, |
michael@0 | 94 | const char *aTopic, |
michael@0 | 95 | const char16_t *aData) |
michael@0 | 96 | { |
michael@0 | 97 | nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(aSubject); |
michael@0 | 98 | NS_ASSERTION(!nsCRT::strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic), |
michael@0 | 99 | "unexpected topic - we only deal with pref changes!"); |
michael@0 | 100 | |
michael@0 | 101 | if (prefBranch) { |
michael@0 | 102 | // refresh our local copy of the "disable popups" pref |
michael@0 | 103 | bool permission = true; |
michael@0 | 104 | prefBranch->GetBoolPref(kPopupDisablePref, &permission); |
michael@0 | 105 | |
michael@0 | 106 | mPolicy = permission ? (uint32_t) DENY_POPUP : (uint32_t) ALLOW_POPUP; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | return NS_OK; |
michael@0 | 110 | } |